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