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