2 FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * Creates all the demo application tasks, then starts the scheduler.
\r
57 * Main. c also creates a task called "Print". This only executes every five
\r
58 * seconds but has the highest priority so is guaranteed to get processor time.
\r
59 * Its main function is to check that all the other tasks are still operational.
\r
60 * Nearly all the tasks in the demo application maintain a unique count that is
\r
61 * incremented each time the task successfully completes its function. Should any
\r
62 * error occur within the task the count is permanently halted. The print task
\r
63 * checks the count of each task to ensure it has changed since the last time the
\r
64 * print task executed. If any count is found not to have changed the print task
\r
65 * displays an appropriate message, halts, and flashes the on board LED rapidly.
\r
66 * If all the tasks are still incrementing their unique counts the print task
\r
67 * displays an "OK" message.
\r
69 * The LED flash tasks do not maintain a count as they already provide visual
\r
70 * feedback of their status.
\r
72 * The print task blocks on the queue into which messages that require displaying
\r
73 * are posted. It will therefore only block for the full 5 seconds if no messages
\r
74 * are posted onto the queue.
\r
76 * Main. c also provides a demonstration of how the trace visualisation utility can
\r
77 * be used, and how the scheduler can be stopped.
\r
79 * On the Flashlite it is preferable not to try to write to the console during
\r
80 * real time operation. The built in LED is toggled every cycle of the print task
\r
81 * that does not encounter any errors, so the console IO may be removed if required.
\r
82 * The build in LED will start flashing rapidly if any task reports an error.
\r
88 + Previously, if an error occurred in a task the on board LED was stopped from
\r
89 toggling. Now if an error occurs the check task enters an infinite loop,
\r
90 toggling the LED rapidly.
\r
94 + The integer and comtest tasks are now used when the cooperative scheduler
\r
95 is being used. Previously they were only used with the preemptive
\r
100 + Made the communications RX task a higher priority.
\r
102 Changes from V2.0.0
\r
104 + Delay periods are now specified using variables and constants of
\r
105 portTickType rather than unsigned long.
\r
108 #include <stdlib.h>
\r
110 #include "FreeRTOS.h"
\r
112 #include "partest.h"
\r
113 #include "serial.h"
\r
115 /* Demo file headers. */
\r
116 #include "BlockQ.h"
\r
120 #include "integer.h"
\r
122 #include "comtest.h"
\r
123 #include "fileio.h"
\r
124 #include "semtest.h"
\r
126 /* Priority definitions for all the tasks in the demo application. */
\r
127 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
128 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
129 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 5 )
\r
130 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
131 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
132 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
133 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
135 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 256 )
\r
136 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\r
138 /* Constant definitions for accessing the build in LED on the Flashlite 186. */
\r
139 #define mainLED_REG_DIR ( ( unsigned short ) 0xff78 )
\r
140 #define mainLED_REG ( ( unsigned short ) 0xff7a )
\r
142 /* If an error is detected in a task then the vErrorChecks() task will enter
\r
143 an infinite loop flashing the LED at this rate. */
\r
144 #define mainERROR_FLASH_RATE ( ( portTickType ) 100 / portTICK_RATE_MS )
\r
146 /* Task function for the "Print" task as described at the top of the file. */
\r
147 static void vErrorChecks( void *pvParameters );
\r
149 /* Function that checks the unique count of all the other tasks as described at
\r
150 the top of the file. */
\r
151 static void prvCheckOtherTasksAreStillRunning( void );
\r
153 /* Functions to setup and use the built in LED on the Flashlite 186 board. */
\r
154 static void prvToggleLED( void );
\r
155 static void prvInitLED( void );
\r
157 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
159 static void prvCheckForKeyPresses( void );
\r
161 /* Buffer used by the trace visualisation utility. */
\r
162 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
164 /*-----------------------------------------------------------*/
\r
167 /* Initialise hardware and utilities. */
\r
168 vParTestInitialise();
\r
169 vPrintInitialise();
\r
172 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
174 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );
\r
175 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
176 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
177 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
178 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
179 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
181 /* Create the "Print" task as described at the top of the file. */
\r
182 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
184 /* This task has to be created last as it keeps account of the number of tasks
\r
185 it expects to see running. */
\r
186 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
188 /* Set the scheduler running. This function will not return unless a task
\r
189 calls vTaskEndScheduler(). */
\r
190 vTaskStartScheduler();
\r
194 /*-----------------------------------------------------------*/
\r
196 static void vErrorChecks( void *pvParameters )
\r
198 portTickType xExpectedWakeTime;
\r
199 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;
\r
200 const long lMaxAllowableTimeDifference = ( long ) 0;
\r
201 portTickType xWakeTime;
\r
202 long lTimeDifference;
\r
203 const char *pcReceivedMessage;
\r
204 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
206 /* Stop warnings. */
\r
207 ( void ) pvParameters;
\r
209 /* Loop continuously, blocking, then checking all the other tasks are still
\r
210 running, before blocking once again. This task blocks on the queue of messages
\r
211 that require displaying so will wake either by its time out expiring, or a
\r
212 message becoming available. */
\r
215 /* Calculate the time we will unblock if no messages are received
\r
216 on the queue. This is used to check that we have not blocked for too long. */
\r
217 xExpectedWakeTime = xTaskGetTickCount();
\r
218 xExpectedWakeTime += xPrintRate;
\r
220 /* Block waiting for either a time out or a message to be posted that
\r
221 required displaying. */
\r
222 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
224 /* Was a message received? */
\r
225 if( pcReceivedMessage == NULL )
\r
227 /* A message was not received so we timed out, did we unblock at the
\r
229 xWakeTime = xTaskGetTickCount();
\r
231 /* Calculate the difference between the time we unblocked and the
\r
232 time we should have unblocked. */
\r
233 if( xWakeTime > xExpectedWakeTime )
\r
235 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
239 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
242 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
244 /* We blocked too long - create a message that will get
\r
245 printed out the next time around. */
\r
246 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
249 /* Check the other tasks are still running, just in case. */
\r
250 prvCheckOtherTasksAreStillRunning();
\r
254 /* We unblocked due to a message becoming available. Send the message
\r
256 vDisplayMessage( pcReceivedMessage );
\r
259 /* Key presses are used to invoke the trace visualisation utility, or
\r
260 end the program. */
\r
261 prvCheckForKeyPresses();
\r
263 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */
\r
264 /*-----------------------------------------------------------*/
\r
266 static void prvCheckForKeyPresses( void )
\r
273 taskENTER_CRITICAL();
\r
275 taskEXIT_CRITICAL();
\r
279 unsigned long ulBufferLength;
\r
281 /* Key presses can be used to start/stop the trace utility, or end the
\r
286 /* Only define keys for turning on and off the trace if the trace
\r
288 #if configUSE_TRACE_FACILITY == 1
\r
289 case 't' : vTaskList( pcWriteBuffer );
\r
290 vWriteMessageToDisk( pcWriteBuffer );
\r
293 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
296 case 'e' : ulBufferLength = ulTaskEndTrace();
\r
297 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
301 default : vTaskEndScheduler();
\r
307 ( void ) pcWriteBuffer;
\r
310 /*-----------------------------------------------------------*/
\r
312 static void prvCheckOtherTasksAreStillRunning( void )
\r
314 short sErrorHasOccurred = pdFALSE;
\r
316 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
318 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
319 sErrorHasOccurred = pdTRUE;
\r
322 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
324 vDisplayMessage( "Integer maths task count unchanged!\r\n" );
\r
325 sErrorHasOccurred = pdTRUE;
\r
328 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
330 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
331 sErrorHasOccurred = pdTRUE;
\r
334 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
336 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
337 sErrorHasOccurred = pdTRUE;
\r
340 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
342 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
343 sErrorHasOccurred = pdTRUE;
\r
346 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
348 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
349 sErrorHasOccurred = pdTRUE;
\r
352 if( sErrorHasOccurred == pdFALSE )
\r
354 vDisplayMessage( "OK " );
\r
355 /* Toggle the LED if everything is okay so we know if an error occurs even if not
\r
356 using console IO. */
\r
363 /* An error has occurred in one of the tasks. Don't go any further and
\r
364 flash the LED rapidly in case console IO is not being used. */
\r
366 vTaskDelay( mainERROR_FLASH_RATE );
\r
370 /*-----------------------------------------------------------*/
\r
372 static void prvInitLED( void )
\r
374 unsigned short usPortDirection;
\r
375 const unsigned short usLEDOut = 0x400;
\r
377 /* Set the LED bit to an output. */
\r
379 usPortDirection = inpw( mainLED_REG_DIR );
\r
380 usPortDirection &= ~usLEDOut;
\r
381 outpw( mainLED_REG_DIR, usPortDirection );
\r
383 /*-----------------------------------------------------------*/
\r
385 static void prvToggleLED( void )
\r
387 static short sLED = pdTRUE;
\r
388 unsigned short usLEDState;
\r
389 const unsigned short usLEDBit = 0x400;
\r
391 /* Flip the state of the LED. */
\r
392 usLEDState = inpw( mainLED_REG );
\r
395 usLEDState &= ~usLEDBit;
\r
399 usLEDState |= usLEDBit;
\r
401 outpw( mainLED_REG, usLEDState );
\r