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