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