2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\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
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\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
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\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
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\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
70 * Creates all the demo application tasks, then starts the scheduler.
\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
84 * The LED flash tasks do not maintain a count as they already provide visual
\r
85 * feedback of their status.
\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
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
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
101 Changes from V1.01:
\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
107 Changes from V1.2.3
\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
113 Changes from V1.2.5
\r
115 + Made the communications RX task a higher priority.
\r
117 Changes from V2.0.0
\r
119 + Delay periods are now specified using variables and constants of
\r
120 portTickType rather than unsigned long.
\r
123 #include <stdlib.h>
\r
125 #include "FreeRTOS.h"
\r
127 #include "partest.h"
\r
128 #include "serial.h"
\r
130 /* Demo file headers. */
\r
131 #include "BlockQ.h"
\r
135 #include "integer.h"
\r
137 #include "comtest.h"
\r
138 #include "fileio.h"
\r
139 #include "semtest.h"
\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
150 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 256 )
\r
151 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\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
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
161 /* Task function for the "Print" task as described at the top of the file. */
\r
162 static void vErrorChecks( void *pvParameters );
\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
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
172 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
174 static void prvCheckForKeyPresses( void );
\r
176 /* Buffer used by the trace visualisation utility. */
\r
177 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
179 /*-----------------------------------------------------------*/
\r
182 /* Initialise hardware and utilities. */
\r
183 vParTestInitialise();
\r
184 vPrintInitialise();
\r
187 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\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
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
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
203 /* Set the scheduler running. This function will not return unless a task
\r
204 calls vTaskEndScheduler(). */
\r
205 vTaskStartScheduler();
\r
209 /*-----------------------------------------------------------*/
\r
211 static void vErrorChecks( void *pvParameters )
\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
221 /* Stop warnings. */
\r
222 ( void ) pvParameters;
\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
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
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
239 /* Was a message received? */
\r
240 if( pcReceivedMessage == NULL )
\r
242 /* A message was not received so we timed out, did we unblock at the
\r
244 xWakeTime = xTaskGetTickCount();
\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
250 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
254 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
257 if( lTimeDifference > lMaxAllowableTimeDifference )
\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
264 /* Check the other tasks are still running, just in case. */
\r
265 prvCheckOtherTasksAreStillRunning();
\r
269 /* We unblocked due to a message becoming available. Send the message
\r
271 vDisplayMessage( pcReceivedMessage );
\r
274 /* Key presses are used to invoke the trace visualisation utility, or
\r
275 end the program. */
\r
276 prvCheckForKeyPresses();
\r
278 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */
\r
279 /*-----------------------------------------------------------*/
\r
281 static void prvCheckForKeyPresses( void )
\r
288 taskENTER_CRITICAL();
\r
290 taskEXIT_CRITICAL();
\r
294 unsigned long ulBufferLength;
\r
296 /* Key presses can be used to start/stop the trace utility, or end the
\r
301 /* Only define keys for turning on and off the trace if the trace
\r
303 #if configUSE_TRACE_FACILITY == 1
\r
304 case 't' : vTaskList( pcWriteBuffer );
\r
305 vWriteMessageToDisk( pcWriteBuffer );
\r
308 /* The legacy trace is no longer supported. Use FreeRTOS+Trace instead.
\r
309 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
312 case 'e' : ulBufferLength = ulTaskEndTrace();
\r
313 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
317 default : vTaskEndScheduler();
\r
323 ( void ) pcWriteBuffer;
\r
326 /*-----------------------------------------------------------*/
\r
328 static void prvCheckOtherTasksAreStillRunning( void )
\r
330 short sErrorHasOccurred = pdFALSE;
\r
332 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
334 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
335 sErrorHasOccurred = pdTRUE;
\r
338 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
340 vDisplayMessage( "Integer maths task count unchanged!\r\n" );
\r
341 sErrorHasOccurred = pdTRUE;
\r
344 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
346 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
347 sErrorHasOccurred = pdTRUE;
\r
350 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
352 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
353 sErrorHasOccurred = pdTRUE;
\r
356 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
358 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
359 sErrorHasOccurred = pdTRUE;
\r
362 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
364 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
365 sErrorHasOccurred = pdTRUE;
\r
368 if( sErrorHasOccurred == pdFALSE )
\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
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
382 vTaskDelay( mainERROR_FLASH_RATE );
\r
386 /*-----------------------------------------------------------*/
\r
388 static void prvInitLED( void )
\r
390 unsigned short usPortDirection;
\r
391 const unsigned short usLEDOut = 0x400;
\r
393 /* Set the LED bit to an output. */
\r
395 usPortDirection = inpw( mainLED_REG_DIR );
\r
396 usPortDirection &= ~usLEDOut;
\r
397 outpw( mainLED_REG_DIR, usPortDirection );
\r
399 /*-----------------------------------------------------------*/
\r
401 static void prvToggleLED( void )
\r
403 static short sLED = pdTRUE;
\r
404 unsigned short usLEDState;
\r
405 const unsigned short usLEDBit = 0x400;
\r
407 /* Flip the state of the LED. */
\r
408 usLEDState = inpw( mainLED_REG );
\r
411 usLEDState &= ~usLEDBit;
\r
415 usLEDState |= usLEDBit;
\r
417 outpw( mainLED_REG, usLEDState );
\r