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