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