]> git.sur5r.net Git - freertos/blob - Demo/Flshlite/main.c
New MicroBlaze port: Added a FreeRTOS exception handler, and installed it in each...
[freertos] / Demo / Flshlite / main.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * Creates all the demo application tasks, then starts the scheduler.\r
56  *\r
57  * Main. c also creates a task called "Print".  This only executes every five \r
58  * seconds but has the highest priority so is guaranteed to get processor time.  \r
59  * Its main function is to check that all the other tasks are still operational.  \r
60  * Nearly all the tasks in the demo application maintain a unique count that is \r
61  * incremented each time the task successfully completes its function.  Should any \r
62  * error occur within the task the count is permanently halted.  The print task \r
63  * checks the count of each task to ensure it has changed since the last time the \r
64  * print task executed.  If any count is found not to have changed the print task\r
65  * displays an appropriate message, halts, and flashes the on board LED rapidly.\r
66  * If all the tasks are still incrementing their unique counts the print task\r
67  * displays an "OK" message.\r
68  *\r
69  * The LED flash tasks do not maintain a count as they already provide visual\r
70  * feedback of their status.\r
71  *\r
72  * The print task blocks on the queue into which messages that require displaying\r
73  * are posted.  It will therefore only block for the full 5 seconds if no messages\r
74  * are posted onto the queue.\r
75  *\r
76  * Main. c also provides a demonstration of how the trace visualisation utility can\r
77  * be used, and how the scheduler can be stopped.\r
78  *\r
79  * On the Flashlite it is preferable not to try to write to the console during\r
80  * real time operation.  The built in LED is toggled every cycle of the print task\r
81  * that does not encounter any errors, so the console IO may be removed if required.\r
82  * The build in LED will start flashing rapidly if any task reports an error.\r
83  */\r
84 \r
85 /*\r
86 Changes from V1.01:\r
87 \r
88         + Previously, if an error occurred in a task the on board LED was stopped from\r
89           toggling.  Now if an error occurs the check task enters an infinite loop,\r
90           toggling the LED rapidly.\r
91 \r
92 Changes from V1.2.3\r
93 \r
94         + The integer and comtest tasks are now used when the cooperative scheduler \r
95           is being used.  Previously they were only used with the preemptive\r
96           scheduler.\r
97 \r
98 Changes from V1.2.5\r
99 \r
100         + Made the communications RX task a higher priority.\r
101 \r
102 Changes from V2.0.0\r
103 \r
104         + Delay periods are now specified using variables and constants of\r
105           portTickType rather than unsigned long.\r
106 */\r
107 \r
108 #include <stdlib.h>\r
109 #include <conio.h>\r
110 #include "FreeRTOS.h"\r
111 #include "task.h"\r
112 #include "partest.h"\r
113 #include "serial.h"\r
114 \r
115 /* Demo file headers. */\r
116 #include "BlockQ.h"\r
117 #include "PollQ.h"\r
118 #include "death.h"\r
119 #include "flash.h"\r
120 #include "integer.h"\r
121 #include "print.h"\r
122 #include "comtest.h"\r
123 #include "fileio.h"\r
124 #include "semtest.h"\r
125 \r
126 /* Priority definitions for all the tasks in the demo application. */\r
127 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
128 #define mainCREATOR_TASK_PRIORITY               ( tskIDLE_PRIORITY + 3 )\r
129 #define mainPRINT_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 5 )\r
130 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
131 #define mainQUEUE_BLOCK_PRIORITY                ( tskIDLE_PRIORITY + 3 )\r
132 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 3 )\r
133 #define mainSEMAPHORE_TASK_PRIORITY             ( tskIDLE_PRIORITY + 1 )\r
134 \r
135 #define mainPRINT_STACK_SIZE            ( ( unsigned short ) 256 )\r
136 #define mainDEBUG_LOG_BUFFER_SIZE       ( ( unsigned short ) 20480 )\r
137 \r
138 /* Constant definitions for accessing the build in LED on the Flashlite 186. */\r
139 #define mainLED_REG_DIR                         ( ( unsigned short ) 0xff78 )\r
140 #define mainLED_REG                             ( ( unsigned short ) 0xff7a )\r
141 \r
142 /* If an error is detected in a task then the vErrorChecks() task will enter\r
143 an infinite loop flashing the LED at this rate. */\r
144 #define mainERROR_FLASH_RATE            ( ( portTickType ) 100 / portTICK_RATE_MS )\r
145 \r
146 /* Task function for the "Print" task as described at the top of the file. */\r
147 static void vErrorChecks( void *pvParameters );\r
148 \r
149 /* Function that checks the unique count of all the other tasks as described at\r
150 the top of the file. */\r
151 static void prvCheckOtherTasksAreStillRunning( void );\r
152 \r
153 /* Functions to setup and use the built in LED on the Flashlite 186 board. */\r
154 static void prvToggleLED( void );\r
155 static void prvInitLED( void );\r
156 \r
157 /* Key presses can be used to start/stop the trace visualisation utility or stop\r
158 the scheduler. */\r
159 static void     prvCheckForKeyPresses( void );\r
160 \r
161 /* Buffer used by the trace visualisation utility. */\r
162 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];\r
163 \r
164 /*-----------------------------------------------------------*/\r
165 short main( void )\r
166 {\r
167         /* Initialise hardware and utilities. */\r
168         vParTestInitialise();\r
169         vPrintInitialise();\r
170         prvInitLED();\r
171 \r
172         /* CREATE ALL THE DEMO APPLICATION TASKS. */\r
173 \r
174         vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );\r
175         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
176         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
177         vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );\r
178         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
179         vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );\r
180 \r
181         /* Create the "Print" task as described at the top of the file. */\r
182         xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );\r
183 \r
184         /* This task has to be created last as it keeps account of the number of tasks\r
185         it expects to see running. */\r
186         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
187 \r
188         /* Set the scheduler running.  This function will not return unless a task\r
189         calls vTaskEndScheduler(). */\r
190         vTaskStartScheduler();\r
191 \r
192         return 1;\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static void vErrorChecks( void *pvParameters )\r
197 {\r
198 portTickType xExpectedWakeTime;\r
199 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;\r
200 const long lMaxAllowableTimeDifference = ( long ) 0;\r
201 portTickType xWakeTime;\r
202 long lTimeDifference;\r
203 const char *pcReceivedMessage;\r
204 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";\r
205 \r
206         /* Stop warnings. */\r
207     ( void ) pvParameters;\r
208 \r
209         /* Loop continuously, blocking, then checking all the other tasks are still\r
210         running, before blocking once again.  This task blocks on the queue of messages\r
211         that require displaying so will wake either by its time out expiring, or a\r
212         message becoming available. */\r
213         for( ;; )\r
214         {\r
215                 /* Calculate the time we will unblock if no messages are received\r
216                 on the queue.  This is used to check that we have not blocked for too long. */\r
217                 xExpectedWakeTime = xTaskGetTickCount();\r
218                 xExpectedWakeTime += xPrintRate;\r
219 \r
220                 /* Block waiting for either a time out or a message to be posted that\r
221                 required displaying. */\r
222                 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );\r
223 \r
224                 /* Was a message received? */\r
225                 if( pcReceivedMessage == NULL )\r
226                 {\r
227                         /* A message was not received so we timed out, did we unblock at the\r
228                         expected time? */\r
229                         xWakeTime = xTaskGetTickCount();\r
230 \r
231                         /* Calculate the difference between the time we unblocked and the\r
232                         time we should have unblocked. */\r
233                         if( xWakeTime > xExpectedWakeTime )\r
234                         {\r
235                                 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );\r
236                         }\r
237                         else\r
238                         {\r
239                                 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );\r
240                         }\r
241 \r
242                         if( lTimeDifference > lMaxAllowableTimeDifference )\r
243                         {\r
244                                 /* We blocked too long - create a message that will get\r
245                                 printed out the next time around. */\r
246                                 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );\r
247                         }\r
248 \r
249                         /* Check the other tasks are still running, just in case. */\r
250                         prvCheckOtherTasksAreStillRunning();\r
251                 }\r
252                 else\r
253                 {\r
254                         /* We unblocked due to a message becoming available.  Send the message\r
255                         for printing. */\r
256                         vDisplayMessage( pcReceivedMessage );\r
257                 }\r
258 \r
259                 /* Key presses are used to invoke the trace visualisation utility, or\r
260                 end the program. */\r
261                 prvCheckForKeyPresses();\r
262         }\r
263 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 static void      prvCheckForKeyPresses( void )\r
267 {\r
268         #ifdef USE_STDIO\r
269 \r
270         short sIn;\r
271 \r
272         \r
273                 taskENTER_CRITICAL();\r
274                         sIn = kbhit();\r
275                 taskEXIT_CRITICAL();\r
276 \r
277                 if( sIn )\r
278                 {\r
279                         unsigned long ulBufferLength;\r
280 \r
281                         /* Key presses can be used to start/stop the trace utility, or end the\r
282                         program. */\r
283                         sIn = getch();\r
284                         switch( sIn )\r
285                         {\r
286                                 /* Only define keys for turning on and off the trace if the trace\r
287                                 is being used. */\r
288                                 #if configUSE_TRACE_FACILITY == 1\r
289                                         case 't' :      vTaskList( pcWriteBuffer );\r
290                                                                 vWriteMessageToDisk( pcWriteBuffer );\r
291                                                                 break;\r
292 \r
293                                         case 's' :      vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );\r
294                                                                 break;\r
295 \r
296                                         case 'e' :      ulBufferLength = ulTaskEndTrace();\r
297                                                                 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );\r
298                                                                 break;\r
299                                 #endif\r
300 \r
301                                 default  :      vTaskEndScheduler();\r
302                                                         break;\r
303                         }\r
304                 }\r
305 \r
306         #else\r
307                 ( void ) pcWriteBuffer;\r
308         #endif\r
309 }\r
310 /*-----------------------------------------------------------*/\r
311 \r
312 static void prvCheckOtherTasksAreStillRunning( void )\r
313 {\r
314 short sErrorHasOccurred = pdFALSE;\r
315 \r
316         if( xAreComTestTasksStillRunning() != pdTRUE )\r
317         {\r
318                 vDisplayMessage( "Com test count unchanged!\r\n" );\r
319                 sErrorHasOccurred = pdTRUE;\r
320         }\r
321 \r
322         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
323         {\r
324                 vDisplayMessage( "Integer maths task count unchanged!\r\n" );\r
325                 sErrorHasOccurred = pdTRUE;\r
326         }\r
327 \r
328         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
329         {\r
330                 vDisplayMessage( "Blocking queues count unchanged!\r\n" );\r
331                 sErrorHasOccurred = pdTRUE;\r
332         }\r
333 \r
334         if( xArePollingQueuesStillRunning() != pdTRUE )\r
335         {\r
336                 vDisplayMessage( "Polling queue count unchanged!\r\n" );\r
337                 sErrorHasOccurred = pdTRUE;\r
338         }\r
339 \r
340         if( xIsCreateTaskStillRunning() != pdTRUE )\r
341         {\r
342                 vDisplayMessage( "Incorrect number of tasks running!\r\n" );\r
343                 sErrorHasOccurred = pdTRUE;\r
344         }\r
345 \r
346         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
347         {\r
348                 vDisplayMessage( "Semaphore take count unchanged!\r\n" );\r
349                 sErrorHasOccurred = pdTRUE;\r
350         }\r
351 \r
352         if( sErrorHasOccurred == pdFALSE )\r
353         {\r
354                 vDisplayMessage( "OK " );\r
355                 /* Toggle the LED if everything is okay so we know if an error occurs even if not\r
356                 using console IO. */\r
357                 prvToggleLED();\r
358         }\r
359         else\r
360         {\r
361                 for( ;; )\r
362                 {\r
363                         /* An error has occurred in one of the tasks.  Don't go any further and\r
364                         flash the LED rapidly in case console IO is not being used. */\r
365                         prvToggleLED();\r
366                         vTaskDelay( mainERROR_FLASH_RATE );\r
367                 }\r
368         }\r
369 }\r
370 /*-----------------------------------------------------------*/\r
371 \r
372 static void prvInitLED( void )\r
373 {\r
374 unsigned short usPortDirection;\r
375 const unsigned short usLEDOut = 0x400;\r
376 \r
377         /* Set the LED bit to an output. */\r
378 \r
379         usPortDirection = inpw( mainLED_REG_DIR );\r
380         usPortDirection &= ~usLEDOut;\r
381         outpw( mainLED_REG_DIR, usPortDirection );\r
382 }\r
383 /*-----------------------------------------------------------*/\r
384 \r
385 static void prvToggleLED( void )\r
386 {\r
387 static short sLED = pdTRUE;\r
388 unsigned short usLEDState;\r
389 const unsigned short usLEDBit = 0x400;\r
390 \r
391         /* Flip the state of the LED. */\r
392         usLEDState = inpw( mainLED_REG );\r
393         if( sLED )\r
394         {\r
395                 usLEDState &= ~usLEDBit;\r
396         }\r
397         else\r
398         {\r
399                 usLEDState |= usLEDBit;\r
400         }\r
401         outpw( mainLED_REG, usLEDState );\r
402 \r
403         sLED = !sLED;\r
404 }\r
405 \r
406 \r