]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Flshlite/main.c
Prepare for V7.4.0 release.
[freertos] / FreeRTOS / Demo / Flshlite / main.c
1 /*\r
2     FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not itcan be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * Creates all the demo application tasks, then starts the scheduler.\r
77  *\r
78  * Main. c also creates a task called "Print".  This only executes every five \r
79  * seconds but has the highest priority so is guaranteed to get processor time.  \r
80  * Its main function is to check that all the other tasks are still operational.  \r
81  * Nearly all the tasks in the demo application maintain a unique count that is \r
82  * incremented each time the task successfully completes its function.  Should any \r
83  * error occur within the task the count is permanently halted.  The print task \r
84  * checks the count of each task to ensure it has changed since the last time the \r
85  * print task executed.  If any count is found not to have changed the print task\r
86  * displays an appropriate message, halts, and flashes the on board LED rapidly.\r
87  * If all the tasks are still incrementing their unique counts the print task\r
88  * displays an "OK" message.\r
89  *\r
90  * The LED flash tasks do not maintain a count as they already provide visual\r
91  * feedback of their status.\r
92  *\r
93  * The print task blocks on the queue into which messages that require displaying\r
94  * are posted.  It will therefore only block for the full 5 seconds if no messages\r
95  * are posted onto the queue.\r
96  *\r
97  * Main. c also provides a demonstration of how the trace visualisation utility can\r
98  * be used, and how the scheduler can be stopped.\r
99  *\r
100  * On the Flashlite it is preferable not to try to write to the console during\r
101  * real time operation.  The built in LED is toggled every cycle of the print task\r
102  * that does not encounter any errors, so the console IO may be removed if required.\r
103  * The build in LED will start flashing rapidly if any task reports an error.\r
104  */\r
105 \r
106 /*\r
107 Changes from V1.01:\r
108 \r
109         + Previously, if an error occurred in a task the on board LED was stopped from\r
110           toggling.  Now if an error occurs the check task enters an infinite loop,\r
111           toggling the LED rapidly.\r
112 \r
113 Changes from V1.2.3\r
114 \r
115         + The integer and comtest tasks are now used when the cooperative scheduler \r
116           is being used.  Previously they were only used with the preemptive\r
117           scheduler.\r
118 \r
119 Changes from V1.2.5\r
120 \r
121         + Made the communications RX task a higher priority.\r
122 \r
123 Changes from V2.0.0\r
124 \r
125         + Delay periods are now specified using variables and constants of\r
126           portTickType rather than unsigned long.\r
127 */\r
128 \r
129 #include <stdlib.h>\r
130 #include <conio.h>\r
131 #include "FreeRTOS.h"\r
132 #include "task.h"\r
133 #include "partest.h"\r
134 #include "serial.h"\r
135 \r
136 /* Demo file headers. */\r
137 #include "BlockQ.h"\r
138 #include "PollQ.h"\r
139 #include "death.h"\r
140 #include "flash.h"\r
141 #include "integer.h"\r
142 #include "print.h"\r
143 #include "comtest.h"\r
144 #include "fileio.h"\r
145 #include "semtest.h"\r
146 \r
147 /* Priority definitions for all the tasks in the demo application. */\r
148 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
149 #define mainCREATOR_TASK_PRIORITY               ( tskIDLE_PRIORITY + 3 )\r
150 #define mainPRINT_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 5 )\r
151 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
152 #define mainQUEUE_BLOCK_PRIORITY                ( tskIDLE_PRIORITY + 3 )\r
153 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 3 )\r
154 #define mainSEMAPHORE_TASK_PRIORITY             ( tskIDLE_PRIORITY + 1 )\r
155 \r
156 #define mainPRINT_STACK_SIZE            ( ( unsigned short ) 256 )\r
157 #define mainDEBUG_LOG_BUFFER_SIZE       ( ( unsigned short ) 20480 )\r
158 \r
159 /* Constant definitions for accessing the build in LED on the Flashlite 186. */\r
160 #define mainLED_REG_DIR                         ( ( unsigned short ) 0xff78 )\r
161 #define mainLED_REG                             ( ( unsigned short ) 0xff7a )\r
162 \r
163 /* If an error is detected in a task then the vErrorChecks() task will enter\r
164 an infinite loop flashing the LED at this rate. */\r
165 #define mainERROR_FLASH_RATE            ( ( portTickType ) 100 / portTICK_RATE_MS )\r
166 \r
167 /* Task function for the "Print" task as described at the top of the file. */\r
168 static void vErrorChecks( void *pvParameters );\r
169 \r
170 /* Function that checks the unique count of all the other tasks as described at\r
171 the top of the file. */\r
172 static void prvCheckOtherTasksAreStillRunning( void );\r
173 \r
174 /* Functions to setup and use the built in LED on the Flashlite 186 board. */\r
175 static void prvToggleLED( void );\r
176 static void prvInitLED( void );\r
177 \r
178 /* Key presses can be used to start/stop the trace visualisation utility or stop\r
179 the scheduler. */\r
180 static void     prvCheckForKeyPresses( void );\r
181 \r
182 /* Buffer used by the trace visualisation utility. */\r
183 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];\r
184 \r
185 /*-----------------------------------------------------------*/\r
186 short main( void )\r
187 {\r
188         /* Initialise hardware and utilities. */\r
189         vParTestInitialise();\r
190         vPrintInitialise();\r
191         prvInitLED();\r
192 \r
193         /* CREATE ALL THE DEMO APPLICATION TASKS. */\r
194 \r
195         vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );\r
196         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
197         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
198         vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );\r
199         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
200         vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );\r
201 \r
202         /* Create the "Print" task as described at the top of the file. */\r
203         xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );\r
204 \r
205         /* This task has to be created last as it keeps account of the number of tasks\r
206         it expects to see running. */\r
207         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
208 \r
209         /* Set the scheduler running.  This function will not return unless a task\r
210         calls vTaskEndScheduler(). */\r
211         vTaskStartScheduler();\r
212 \r
213         return 1;\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 static void vErrorChecks( void *pvParameters )\r
218 {\r
219 portTickType xExpectedWakeTime;\r
220 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;\r
221 const long lMaxAllowableTimeDifference = ( long ) 0;\r
222 portTickType xWakeTime;\r
223 long lTimeDifference;\r
224 const char *pcReceivedMessage;\r
225 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";\r
226 \r
227         /* Stop warnings. */\r
228     ( void ) pvParameters;\r
229 \r
230         /* Loop continuously, blocking, then checking all the other tasks are still\r
231         running, before blocking once again.  This task blocks on the queue of messages\r
232         that require displaying so will wake either by its time out expiring, or a\r
233         message becoming available. */\r
234         for( ;; )\r
235         {\r
236                 /* Calculate the time we will unblock if no messages are received\r
237                 on the queue.  This is used to check that we have not blocked for too long. */\r
238                 xExpectedWakeTime = xTaskGetTickCount();\r
239                 xExpectedWakeTime += xPrintRate;\r
240 \r
241                 /* Block waiting for either a time out or a message to be posted that\r
242                 required displaying. */\r
243                 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );\r
244 \r
245                 /* Was a message received? */\r
246                 if( pcReceivedMessage == NULL )\r
247                 {\r
248                         /* A message was not received so we timed out, did we unblock at the\r
249                         expected time? */\r
250                         xWakeTime = xTaskGetTickCount();\r
251 \r
252                         /* Calculate the difference between the time we unblocked and the\r
253                         time we should have unblocked. */\r
254                         if( xWakeTime > xExpectedWakeTime )\r
255                         {\r
256                                 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );\r
257                         }\r
258                         else\r
259                         {\r
260                                 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );\r
261                         }\r
262 \r
263                         if( lTimeDifference > lMaxAllowableTimeDifference )\r
264                         {\r
265                                 /* We blocked too long - create a message that will get\r
266                                 printed out the next time around. */\r
267                                 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );\r
268                         }\r
269 \r
270                         /* Check the other tasks are still running, just in case. */\r
271                         prvCheckOtherTasksAreStillRunning();\r
272                 }\r
273                 else\r
274                 {\r
275                         /* We unblocked due to a message becoming available.  Send the message\r
276                         for printing. */\r
277                         vDisplayMessage( pcReceivedMessage );\r
278                 }\r
279 \r
280                 /* Key presses are used to invoke the trace visualisation utility, or\r
281                 end the program. */\r
282                 prvCheckForKeyPresses();\r
283         }\r
284 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */\r
285 /*-----------------------------------------------------------*/\r
286 \r
287 static void      prvCheckForKeyPresses( void )\r
288 {\r
289         #ifdef USE_STDIO\r
290 \r
291         short sIn;\r
292 \r
293         \r
294                 taskENTER_CRITICAL();\r
295                         sIn = kbhit();\r
296                 taskEXIT_CRITICAL();\r
297 \r
298                 if( sIn )\r
299                 {\r
300                         unsigned long ulBufferLength;\r
301 \r
302                         /* Key presses can be used to start/stop the trace utility, or end the\r
303                         program. */\r
304                         sIn = getch();\r
305                         switch( sIn )\r
306                         {\r
307                                 /* Only define keys for turning on and off the trace if the trace\r
308                                 is being used. */\r
309                                 #if configUSE_TRACE_FACILITY == 1\r
310                                         case 't' :      vTaskList( pcWriteBuffer );\r
311                                                                 vWriteMessageToDisk( pcWriteBuffer );\r
312                                                                 break;\r
313 \r
314                                         /* The legacy trace is no longer supported.  Use FreeRTOS+Trace instead.\r
315                                         case 's' :      vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );\r
316                                                                 break;\r
317 \r
318                                         case 'e' :      ulBufferLength = ulTaskEndTrace();\r
319                                                                 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );\r
320                                                                 break;*/\r
321                                 #endif\r
322 \r
323                                 default  :      vTaskEndScheduler();\r
324                                                         break;\r
325                         }\r
326                 }\r
327 \r
328         #else\r
329                 ( void ) pcWriteBuffer;\r
330         #endif\r
331 }\r
332 /*-----------------------------------------------------------*/\r
333 \r
334 static void prvCheckOtherTasksAreStillRunning( void )\r
335 {\r
336 short sErrorHasOccurred = pdFALSE;\r
337 \r
338         if( xAreComTestTasksStillRunning() != pdTRUE )\r
339         {\r
340                 vDisplayMessage( "Com test count unchanged!\r\n" );\r
341                 sErrorHasOccurred = pdTRUE;\r
342         }\r
343 \r
344         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
345         {\r
346                 vDisplayMessage( "Integer maths task count unchanged!\r\n" );\r
347                 sErrorHasOccurred = pdTRUE;\r
348         }\r
349 \r
350         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
351         {\r
352                 vDisplayMessage( "Blocking queues count unchanged!\r\n" );\r
353                 sErrorHasOccurred = pdTRUE;\r
354         }\r
355 \r
356         if( xArePollingQueuesStillRunning() != pdTRUE )\r
357         {\r
358                 vDisplayMessage( "Polling queue count unchanged!\r\n" );\r
359                 sErrorHasOccurred = pdTRUE;\r
360         }\r
361 \r
362         if( xIsCreateTaskStillRunning() != pdTRUE )\r
363         {\r
364                 vDisplayMessage( "Incorrect number of tasks running!\r\n" );\r
365                 sErrorHasOccurred = pdTRUE;\r
366         }\r
367 \r
368         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
369         {\r
370                 vDisplayMessage( "Semaphore take count unchanged!\r\n" );\r
371                 sErrorHasOccurred = pdTRUE;\r
372         }\r
373 \r
374         if( sErrorHasOccurred == pdFALSE )\r
375         {\r
376                 vDisplayMessage( "OK " );\r
377                 /* Toggle the LED if everything is okay so we know if an error occurs even if not\r
378                 using console IO. */\r
379                 prvToggleLED();\r
380         }\r
381         else\r
382         {\r
383                 for( ;; )\r
384                 {\r
385                         /* An error has occurred in one of the tasks.  Don't go any further and\r
386                         flash the LED rapidly in case console IO is not being used. */\r
387                         prvToggleLED();\r
388                         vTaskDelay( mainERROR_FLASH_RATE );\r
389                 }\r
390         }\r
391 }\r
392 /*-----------------------------------------------------------*/\r
393 \r
394 static void prvInitLED( void )\r
395 {\r
396 unsigned short usPortDirection;\r
397 const unsigned short usLEDOut = 0x400;\r
398 \r
399         /* Set the LED bit to an output. */\r
400 \r
401         usPortDirection = inpw( mainLED_REG_DIR );\r
402         usPortDirection &= ~usLEDOut;\r
403         outpw( mainLED_REG_DIR, usPortDirection );\r
404 }\r
405 /*-----------------------------------------------------------*/\r
406 \r
407 static void prvToggleLED( void )\r
408 {\r
409 static short sLED = pdTRUE;\r
410 unsigned short usLEDState;\r
411 const unsigned short usLEDBit = 0x400;\r
412 \r
413         /* Flip the state of the LED. */\r
414         usLEDState = inpw( mainLED_REG );\r
415         if( sLED )\r
416         {\r
417                 usLEDState &= ~usLEDBit;\r
418         }\r
419         else\r
420         {\r
421                 usLEDState |= usLEDBit;\r
422         }\r
423         outpw( mainLED_REG, usLEDState );\r
424 \r
425         sLED = !sLED;\r
426 }\r
427 \r
428 \r