2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\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 modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 * Creates all the demo application tasks, then starts the scheduler.
\r
70 * Main. c also creates a task called "Print". This only executes every five
\r
71 * seconds but has the highest priority so is guaranteed to get processor time.
\r
72 * Its main function is to check that all the other tasks are still operational.
\r
73 * Nearly all the tasks in the demo application maintain a unique count that is
\r
74 * incremented each time the task successfully completes its function. Should any
\r
75 * error occur within the task the count is permanently halted. The print task
\r
76 * checks the count of each task to ensure it has changed since the last time the
\r
77 * print task executed. If any count is found not to have changed the print task
\r
78 * displays an appropriate message, halts, and flashes the on board LED rapidly.
\r
79 * If all the tasks are still incrementing their unique counts the print task
\r
80 * displays an "OK" message.
\r
82 * The LED flash tasks do not maintain a count as they already provide visual
\r
83 * feedback of their status.
\r
85 * The print task blocks on the queue into which messages that require displaying
\r
86 * are posted. It will therefore only block for the full 5 seconds if no messages
\r
87 * are posted onto the queue.
\r
89 * Main. c also provides a demonstration of how the trace visualisation utility can
\r
90 * be used, and how the scheduler can be stopped.
\r
92 * On the Flashlite it is preferable not to try to write to the console during
\r
93 * real time operation. The built in LED is toggled every cycle of the print task
\r
94 * that does not encounter any errors, so the console IO may be removed if required.
\r
95 * The build in LED will start flashing rapidly if any task reports an error.
\r
101 + Previously, if an error occurred in a task the on board LED was stopped from
\r
102 toggling. Now if an error occurs the check task enters an infinite loop,
\r
103 toggling the LED rapidly.
\r
105 Changes from V1.2.3
\r
107 + The integer and comtest tasks are now used when the cooperative scheduler
\r
108 is being used. Previously they were only used with the preemptive
\r
111 Changes from V1.2.5
\r
113 + Made the communications RX task a higher priority.
\r
115 Changes from V2.0.0
\r
117 + Delay periods are now specified using variables and constants of
\r
118 portTickType rather than unsigned long.
\r
121 #include <stdlib.h>
\r
123 #include "FreeRTOS.h"
\r
125 #include "partest.h"
\r
126 #include "serial.h"
\r
128 /* Demo file headers. */
\r
129 #include "BlockQ.h"
\r
133 #include "integer.h"
\r
135 #include "comtest.h"
\r
136 #include "fileio.h"
\r
137 #include "semtest.h"
\r
139 /* Priority definitions for all the tasks in the demo application. */
\r
140 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
141 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
142 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 5 )
\r
143 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
144 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
145 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
146 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
148 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 256 )
\r
149 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\r
151 /* Constant definitions for accessing the build in LED on the Flashlite 186. */
\r
152 #define mainLED_REG_DIR ( ( unsigned short ) 0xff78 )
\r
153 #define mainLED_REG ( ( unsigned short ) 0xff7a )
\r
155 /* If an error is detected in a task then the vErrorChecks() task will enter
\r
156 an infinite loop flashing the LED at this rate. */
\r
157 #define mainERROR_FLASH_RATE ( ( portTickType ) 100 / portTICK_RATE_MS )
\r
159 /* Task function for the "Print" task as described at the top of the file. */
\r
160 static void vErrorChecks( void *pvParameters );
\r
162 /* Function that checks the unique count of all the other tasks as described at
\r
163 the top of the file. */
\r
164 static void prvCheckOtherTasksAreStillRunning( void );
\r
166 /* Functions to setup and use the built in LED on the Flashlite 186 board. */
\r
167 static void prvToggleLED( void );
\r
168 static void prvInitLED( void );
\r
170 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
172 static void prvCheckForKeyPresses( void );
\r
174 /* Buffer used by the trace visualisation utility. */
\r
175 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
177 /*-----------------------------------------------------------*/
\r
180 /* Initialise hardware and utilities. */
\r
181 vParTestInitialise();
\r
182 vPrintInitialise();
\r
185 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
187 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );
\r
188 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
189 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
190 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
191 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
192 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
194 /* Create the "Print" task as described at the top of the file. */
\r
195 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
197 /* This task has to be created last as it keeps account of the number of tasks
\r
198 it expects to see running. */
\r
199 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
201 /* Set the scheduler running. This function will not return unless a task
\r
202 calls vTaskEndScheduler(). */
\r
203 vTaskStartScheduler();
\r
207 /*-----------------------------------------------------------*/
\r
209 static void vErrorChecks( void *pvParameters )
\r
211 portTickType xExpectedWakeTime;
\r
212 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;
\r
213 const long lMaxAllowableTimeDifference = ( long ) 0;
\r
214 portTickType xWakeTime;
\r
215 long lTimeDifference;
\r
216 const char *pcReceivedMessage;
\r
217 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
219 /* Stop warnings. */
\r
220 ( void ) pvParameters;
\r
222 /* Loop continuously, blocking, then checking all the other tasks are still
\r
223 running, before blocking once again. This task blocks on the queue of messages
\r
224 that require displaying so will wake either by its time out expiring, or a
\r
225 message becoming available. */
\r
228 /* Calculate the time we will unblock if no messages are received
\r
229 on the queue. This is used to check that we have not blocked for too long. */
\r
230 xExpectedWakeTime = xTaskGetTickCount();
\r
231 xExpectedWakeTime += xPrintRate;
\r
233 /* Block waiting for either a time out or a message to be posted that
\r
234 required displaying. */
\r
235 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
237 /* Was a message received? */
\r
238 if( pcReceivedMessage == NULL )
\r
240 /* A message was not received so we timed out, did we unblock at the
\r
242 xWakeTime = xTaskGetTickCount();
\r
244 /* Calculate the difference between the time we unblocked and the
\r
245 time we should have unblocked. */
\r
246 if( xWakeTime > xExpectedWakeTime )
\r
248 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
252 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
255 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
257 /* We blocked too long - create a message that will get
\r
258 printed out the next time around. */
\r
259 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
262 /* Check the other tasks are still running, just in case. */
\r
263 prvCheckOtherTasksAreStillRunning();
\r
267 /* We unblocked due to a message becoming available. Send the message
\r
269 vDisplayMessage( pcReceivedMessage );
\r
272 /* Key presses are used to invoke the trace visualisation utility, or
\r
273 end the program. */
\r
274 prvCheckForKeyPresses();
\r
276 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */
\r
277 /*-----------------------------------------------------------*/
\r
279 static void prvCheckForKeyPresses( void )
\r
286 taskENTER_CRITICAL();
\r
288 taskEXIT_CRITICAL();
\r
292 unsigned long ulBufferLength;
\r
294 /* Key presses can be used to start/stop the trace utility, or end the
\r
299 /* Only define keys for turning on and off the trace if the trace
\r
301 #if configUSE_TRACE_FACILITY == 1
\r
302 case 't' : vTaskList( pcWriteBuffer );
\r
303 vWriteMessageToDisk( pcWriteBuffer );
\r
306 /* The legacy trace is no longer supported. Use FreeRTOS+Trace instead.
\r
307 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
310 case 'e' : ulBufferLength = ulTaskEndTrace();
\r
311 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
315 default : vTaskEndScheduler();
\r
321 ( void ) pcWriteBuffer;
\r
324 /*-----------------------------------------------------------*/
\r
326 static void prvCheckOtherTasksAreStillRunning( void )
\r
328 short sErrorHasOccurred = pdFALSE;
\r
330 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
332 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
333 sErrorHasOccurred = pdTRUE;
\r
336 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
338 vDisplayMessage( "Integer maths task count unchanged!\r\n" );
\r
339 sErrorHasOccurred = pdTRUE;
\r
342 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
344 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
345 sErrorHasOccurred = pdTRUE;
\r
348 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
350 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
351 sErrorHasOccurred = pdTRUE;
\r
354 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
356 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
357 sErrorHasOccurred = pdTRUE;
\r
360 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
362 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
363 sErrorHasOccurred = pdTRUE;
\r
366 if( sErrorHasOccurred == pdFALSE )
\r
368 vDisplayMessage( "OK " );
\r
369 /* Toggle the LED if everything is okay so we know if an error occurs even if not
\r
370 using console IO. */
\r
377 /* An error has occurred in one of the tasks. Don't go any further and
\r
378 flash the LED rapidly in case console IO is not being used. */
\r
380 vTaskDelay( mainERROR_FLASH_RATE );
\r
384 /*-----------------------------------------------------------*/
\r
386 static void prvInitLED( void )
\r
388 unsigned short usPortDirection;
\r
389 const unsigned short usLEDOut = 0x400;
\r
391 /* Set the LED bit to an output. */
\r
393 usPortDirection = inpw( mainLED_REG_DIR );
\r
394 usPortDirection &= ~usLEDOut;
\r
395 outpw( mainLED_REG_DIR, usPortDirection );
\r
397 /*-----------------------------------------------------------*/
\r
399 static void prvToggleLED( void )
\r
401 static short sLED = pdTRUE;
\r
402 unsigned short usLEDState;
\r
403 const unsigned short usLEDBit = 0x400;
\r
405 /* Flip the state of the LED. */
\r
406 usLEDState = inpw( mainLED_REG );
\r
409 usLEDState &= ~usLEDBit;
\r
413 usLEDState |= usLEDBit;
\r
415 outpw( mainLED_REG, usLEDState );
\r