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