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