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