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