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