]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Flshlite/main.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / Flshlite / main.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*\r
70  * Creates all the demo application tasks, then starts the scheduler.\r
71  *\r
72  * Main. c also creates a task called "Print".  This only executes every five \r
73  * seconds but has the highest priority so is guaranteed to get processor time.  \r
74  * Its main function is to check that all the other tasks are still operational.  \r
75  * Nearly all the tasks in the demo application maintain a unique count that is \r
76  * incremented each time the task successfully completes its function.  Should any \r
77  * error occur within the task the count is permanently halted.  The print task \r
78  * checks the count of each task to ensure it has changed since the last time the \r
79  * print task executed.  If any count is found not to have changed the print task\r
80  * displays an appropriate message, halts, and flashes the on board LED rapidly.\r
81  * If all the tasks are still incrementing their unique counts the print task\r
82  * displays an "OK" message.\r
83  *\r
84  * The LED flash tasks do not maintain a count as they already provide visual\r
85  * feedback of their status.\r
86  *\r
87  * The print task blocks on the queue into which messages that require displaying\r
88  * are posted.  It will therefore only block for the full 5 seconds if no messages\r
89  * are posted onto the queue.\r
90  *\r
91  * Main. c also provides a demonstration of how the trace visualisation utility can\r
92  * be used, and how the scheduler can be stopped.\r
93  *\r
94  * On the Flashlite it is preferable not to try to write to the console during\r
95  * real time operation.  The built in LED is toggled every cycle of the print task\r
96  * that does not encounter any errors, so the console IO may be removed if required.\r
97  * The build in LED will start flashing rapidly if any task reports an error.\r
98  */\r
99 \r
100 /*\r
101 Changes from V1.01:\r
102 \r
103         + Previously, if an error occurred in a task the on board LED was stopped from\r
104           toggling.  Now if an error occurs the check task enters an infinite loop,\r
105           toggling the LED rapidly.\r
106 \r
107 Changes from V1.2.3\r
108 \r
109         + The integer and comtest tasks are now used when the cooperative scheduler \r
110           is being used.  Previously they were only used with the preemptive\r
111           scheduler.\r
112 \r
113 Changes from V1.2.5\r
114 \r
115         + Made the communications RX task a higher priority.\r
116 \r
117 Changes from V2.0.0\r
118 \r
119         + Delay periods are now specified using variables and constants of\r
120           portTickType rather than unsigned long.\r
121 */\r
122 \r
123 #include <stdlib.h>\r
124 #include <conio.h>\r
125 #include "FreeRTOS.h"\r
126 #include "task.h"\r
127 #include "partest.h"\r
128 #include "serial.h"\r
129 \r
130 /* Demo file headers. */\r
131 #include "BlockQ.h"\r
132 #include "PollQ.h"\r
133 #include "death.h"\r
134 #include "flash.h"\r
135 #include "integer.h"\r
136 #include "print.h"\r
137 #include "comtest.h"\r
138 #include "fileio.h"\r
139 #include "semtest.h"\r
140 \r
141 /* Priority definitions for all the tasks in the demo application. */\r
142 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
143 #define mainCREATOR_TASK_PRIORITY               ( tskIDLE_PRIORITY + 3 )\r
144 #define mainPRINT_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 5 )\r
145 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
146 #define mainQUEUE_BLOCK_PRIORITY                ( tskIDLE_PRIORITY + 3 )\r
147 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 3 )\r
148 #define mainSEMAPHORE_TASK_PRIORITY             ( tskIDLE_PRIORITY + 1 )\r
149 \r
150 #define mainPRINT_STACK_SIZE            ( ( unsigned short ) 256 )\r
151 #define mainDEBUG_LOG_BUFFER_SIZE       ( ( unsigned short ) 20480 )\r
152 \r
153 /* Constant definitions for accessing the build in LED on the Flashlite 186. */\r
154 #define mainLED_REG_DIR                         ( ( unsigned short ) 0xff78 )\r
155 #define mainLED_REG                             ( ( unsigned short ) 0xff7a )\r
156 \r
157 /* If an error is detected in a task then the vErrorChecks() task will enter\r
158 an infinite loop flashing the LED at this rate. */\r
159 #define mainERROR_FLASH_RATE            ( ( portTickType ) 100 / portTICK_RATE_MS )\r
160 \r
161 /* Task function for the "Print" task as described at the top of the file. */\r
162 static void vErrorChecks( void *pvParameters );\r
163 \r
164 /* Function that checks the unique count of all the other tasks as described at\r
165 the top of the file. */\r
166 static void prvCheckOtherTasksAreStillRunning( void );\r
167 \r
168 /* Functions to setup and use the built in LED on the Flashlite 186 board. */\r
169 static void prvToggleLED( void );\r
170 static void prvInitLED( void );\r
171 \r
172 /* Key presses can be used to start/stop the trace visualisation utility or stop\r
173 the scheduler. */\r
174 static void     prvCheckForKeyPresses( void );\r
175 \r
176 /* Buffer used by the trace visualisation utility. */\r
177 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];\r
178 \r
179 /*-----------------------------------------------------------*/\r
180 short main( void )\r
181 {\r
182         /* Initialise hardware and utilities. */\r
183         vParTestInitialise();\r
184         vPrintInitialise();\r
185         prvInitLED();\r
186 \r
187         /* CREATE ALL THE DEMO APPLICATION TASKS. */\r
188 \r
189         vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );\r
190         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
191         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
192         vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );\r
193         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
194         vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );\r
195 \r
196         /* Create the "Print" task as described at the top of the file. */\r
197         xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );\r
198 \r
199         /* This task has to be created last as it keeps account of the number of tasks\r
200         it expects to see running. */\r
201         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
202 \r
203         /* Set the scheduler running.  This function will not return unless a task\r
204         calls vTaskEndScheduler(). */\r
205         vTaskStartScheduler();\r
206 \r
207         return 1;\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 static void vErrorChecks( void *pvParameters )\r
212 {\r
213 portTickType xExpectedWakeTime;\r
214 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;\r
215 const long lMaxAllowableTimeDifference = ( long ) 0;\r
216 portTickType xWakeTime;\r
217 long lTimeDifference;\r
218 const char *pcReceivedMessage;\r
219 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";\r
220 \r
221         /* Stop warnings. */\r
222     ( void ) pvParameters;\r
223 \r
224         /* Loop continuously, blocking, then checking all the other tasks are still\r
225         running, before blocking once again.  This task blocks on the queue of messages\r
226         that require displaying so will wake either by its time out expiring, or a\r
227         message becoming available. */\r
228         for( ;; )\r
229         {\r
230                 /* Calculate the time we will unblock if no messages are received\r
231                 on the queue.  This is used to check that we have not blocked for too long. */\r
232                 xExpectedWakeTime = xTaskGetTickCount();\r
233                 xExpectedWakeTime += xPrintRate;\r
234 \r
235                 /* Block waiting for either a time out or a message to be posted that\r
236                 required displaying. */\r
237                 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );\r
238 \r
239                 /* Was a message received? */\r
240                 if( pcReceivedMessage == NULL )\r
241                 {\r
242                         /* A message was not received so we timed out, did we unblock at the\r
243                         expected time? */\r
244                         xWakeTime = xTaskGetTickCount();\r
245 \r
246                         /* Calculate the difference between the time we unblocked and the\r
247                         time we should have unblocked. */\r
248                         if( xWakeTime > xExpectedWakeTime )\r
249                         {\r
250                                 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );\r
251                         }\r
252                         else\r
253                         {\r
254                                 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );\r
255                         }\r
256 \r
257                         if( lTimeDifference > lMaxAllowableTimeDifference )\r
258                         {\r
259                                 /* We blocked too long - create a message that will get\r
260                                 printed out the next time around. */\r
261                                 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );\r
262                         }\r
263 \r
264                         /* Check the other tasks are still running, just in case. */\r
265                         prvCheckOtherTasksAreStillRunning();\r
266                 }\r
267                 else\r
268                 {\r
269                         /* We unblocked due to a message becoming available.  Send the message\r
270                         for printing. */\r
271                         vDisplayMessage( pcReceivedMessage );\r
272                 }\r
273 \r
274                 /* Key presses are used to invoke the trace visualisation utility, or\r
275                 end the program. */\r
276                 prvCheckForKeyPresses();\r
277         }\r
278 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 static void      prvCheckForKeyPresses( void )\r
282 {\r
283         #ifdef USE_STDIO\r
284 \r
285         short sIn;\r
286 \r
287         \r
288                 taskENTER_CRITICAL();\r
289                         sIn = kbhit();\r
290                 taskEXIT_CRITICAL();\r
291 \r
292                 if( sIn )\r
293                 {\r
294                         unsigned long ulBufferLength;\r
295 \r
296                         /* Key presses can be used to start/stop the trace utility, or end the\r
297                         program. */\r
298                         sIn = getch();\r
299                         switch( sIn )\r
300                         {\r
301                                 /* Only define keys for turning on and off the trace if the trace\r
302                                 is being used. */\r
303                                 #if configUSE_TRACE_FACILITY == 1\r
304                                         case 't' :      vTaskList( pcWriteBuffer );\r
305                                                                 vWriteMessageToDisk( pcWriteBuffer );\r
306                                                                 break;\r
307 \r
308                                         /* The legacy trace is no longer supported.  Use FreeRTOS+Trace instead.\r
309                                         case 's' :      vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );\r
310                                                                 break;\r
311 \r
312                                         case 'e' :      ulBufferLength = ulTaskEndTrace();\r
313                                                                 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );\r
314                                                                 break;*/\r
315                                 #endif\r
316 \r
317                                 default  :      vTaskEndScheduler();\r
318                                                         break;\r
319                         }\r
320                 }\r
321 \r
322         #else\r
323                 ( void ) pcWriteBuffer;\r
324         #endif\r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r
328 static void prvCheckOtherTasksAreStillRunning( void )\r
329 {\r
330 short sErrorHasOccurred = pdFALSE;\r
331 \r
332         if( xAreComTestTasksStillRunning() != pdTRUE )\r
333         {\r
334                 vDisplayMessage( "Com test count unchanged!\r\n" );\r
335                 sErrorHasOccurred = pdTRUE;\r
336         }\r
337 \r
338         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
339         {\r
340                 vDisplayMessage( "Integer maths task count unchanged!\r\n" );\r
341                 sErrorHasOccurred = pdTRUE;\r
342         }\r
343 \r
344         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
345         {\r
346                 vDisplayMessage( "Blocking queues count unchanged!\r\n" );\r
347                 sErrorHasOccurred = pdTRUE;\r
348         }\r
349 \r
350         if( xArePollingQueuesStillRunning() != pdTRUE )\r
351         {\r
352                 vDisplayMessage( "Polling queue count unchanged!\r\n" );\r
353                 sErrorHasOccurred = pdTRUE;\r
354         }\r
355 \r
356         if( xIsCreateTaskStillRunning() != pdTRUE )\r
357         {\r
358                 vDisplayMessage( "Incorrect number of tasks running!\r\n" );\r
359                 sErrorHasOccurred = pdTRUE;\r
360         }\r
361 \r
362         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
363         {\r
364                 vDisplayMessage( "Semaphore take count unchanged!\r\n" );\r
365                 sErrorHasOccurred = pdTRUE;\r
366         }\r
367 \r
368         if( sErrorHasOccurred == pdFALSE )\r
369         {\r
370                 vDisplayMessage( "OK " );\r
371                 /* Toggle the LED if everything is okay so we know if an error occurs even if not\r
372                 using console IO. */\r
373                 prvToggleLED();\r
374         }\r
375         else\r
376         {\r
377                 for( ;; )\r
378                 {\r
379                         /* An error has occurred in one of the tasks.  Don't go any further and\r
380                         flash the LED rapidly in case console IO is not being used. */\r
381                         prvToggleLED();\r
382                         vTaskDelay( mainERROR_FLASH_RATE );\r
383                 }\r
384         }\r
385 }\r
386 /*-----------------------------------------------------------*/\r
387 \r
388 static void prvInitLED( void )\r
389 {\r
390 unsigned short usPortDirection;\r
391 const unsigned short usLEDOut = 0x400;\r
392 \r
393         /* Set the LED bit to an output. */\r
394 \r
395         usPortDirection = inpw( mainLED_REG_DIR );\r
396         usPortDirection &= ~usLEDOut;\r
397         outpw( mainLED_REG_DIR, usPortDirection );\r
398 }\r
399 /*-----------------------------------------------------------*/\r
400 \r
401 static void prvToggleLED( void )\r
402 {\r
403 static short sLED = pdTRUE;\r
404 unsigned short usLEDState;\r
405 const unsigned short usLEDBit = 0x400;\r
406 \r
407         /* Flip the state of the LED. */\r
408         usLEDState = inpw( mainLED_REG );\r
409         if( sLED )\r
410         {\r
411                 usLEDState &= ~usLEDBit;\r
412         }\r
413         else\r
414         {\r
415                 usLEDState |= usLEDBit;\r
416         }\r
417         outpw( mainLED_REG, usLEDState );\r
418 \r
419         sLED = !sLED;\r
420 }\r
421 \r
422 \r