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