2 FreeRTOS V7.4.0 - Copyright (C) 2013 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
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 * Creates all the demo application tasks, then starts the scheduler.
\r
78 * Main. c also creates a task called "Print". This only executes every five
\r
79 * seconds but has the highest priority so is guaranteed to get processor time.
\r
80 * Its main function is to check that all the other tasks are still operational.
\r
81 * Nearly all the tasks in the demo application maintain a unique count that is
\r
82 * incremented each time the task successfully completes its function. Should any
\r
83 * error occur within the task the count is permanently halted. The print task
\r
84 * checks the count of each task to ensure it has changed since the last time the
\r
85 * print task executed. If any count is found not to have changed the print task
\r
86 * displays an appropriate message, halts, and flashes the on board LED rapidly.
\r
87 * If all the tasks are still incrementing their unique counts the print task
\r
88 * displays an "OK" message.
\r
90 * The LED flash tasks do not maintain a count as they already provide visual
\r
91 * feedback of their status.
\r
93 * The print task blocks on the queue into which messages that require displaying
\r
94 * are posted. It will therefore only block for the full 5 seconds if no messages
\r
95 * are posted onto the queue.
\r
97 * Main. c also provides a demonstration of how the trace visualisation utility can
\r
98 * be used, and how the scheduler can be stopped.
\r
100 * On the Flashlite it is preferable not to try to write to the console during
\r
101 * real time operation. The built in LED is toggled every cycle of the print task
\r
102 * that does not encounter any errors, so the console IO may be removed if required.
\r
103 * The build in LED will start flashing rapidly if any task reports an error.
\r
107 Changes from V1.01:
\r
109 + Previously, if an error occurred in a task the on board LED was stopped from
\r
110 toggling. Now if an error occurs the check task enters an infinite loop,
\r
111 toggling the LED rapidly.
\r
113 Changes from V1.2.3
\r
115 + The integer and comtest tasks are now used when the cooperative scheduler
\r
116 is being used. Previously they were only used with the preemptive
\r
119 Changes from V1.2.5
\r
121 + Made the communications RX task a higher priority.
\r
123 Changes from V2.0.0
\r
125 + Delay periods are now specified using variables and constants of
\r
126 portTickType rather than unsigned long.
\r
129 #include <stdlib.h>
\r
131 #include "FreeRTOS.h"
\r
133 #include "partest.h"
\r
134 #include "serial.h"
\r
136 /* Demo file headers. */
\r
137 #include "BlockQ.h"
\r
141 #include "integer.h"
\r
143 #include "comtest.h"
\r
144 #include "fileio.h"
\r
145 #include "semtest.h"
\r
147 /* Priority definitions for all the tasks in the demo application. */
\r
148 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
149 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
150 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 5 )
\r
151 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
152 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
153 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
154 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
156 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 256 )
\r
157 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\r
159 /* Constant definitions for accessing the build in LED on the Flashlite 186. */
\r
160 #define mainLED_REG_DIR ( ( unsigned short ) 0xff78 )
\r
161 #define mainLED_REG ( ( unsigned short ) 0xff7a )
\r
163 /* If an error is detected in a task then the vErrorChecks() task will enter
\r
164 an infinite loop flashing the LED at this rate. */
\r
165 #define mainERROR_FLASH_RATE ( ( portTickType ) 100 / portTICK_RATE_MS )
\r
167 /* Task function for the "Print" task as described at the top of the file. */
\r
168 static void vErrorChecks( void *pvParameters );
\r
170 /* Function that checks the unique count of all the other tasks as described at
\r
171 the top of the file. */
\r
172 static void prvCheckOtherTasksAreStillRunning( void );
\r
174 /* Functions to setup and use the built in LED on the Flashlite 186 board. */
\r
175 static void prvToggleLED( void );
\r
176 static void prvInitLED( void );
\r
178 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
180 static void prvCheckForKeyPresses( void );
\r
182 /* Buffer used by the trace visualisation utility. */
\r
183 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
185 /*-----------------------------------------------------------*/
\r
188 /* Initialise hardware and utilities. */
\r
189 vParTestInitialise();
\r
190 vPrintInitialise();
\r
193 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
195 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );
\r
196 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
197 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
198 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
199 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
200 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
202 /* Create the "Print" task as described at the top of the file. */
\r
203 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
205 /* This task has to be created last as it keeps account of the number of tasks
\r
206 it expects to see running. */
\r
207 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
209 /* Set the scheduler running. This function will not return unless a task
\r
210 calls vTaskEndScheduler(). */
\r
211 vTaskStartScheduler();
\r
215 /*-----------------------------------------------------------*/
\r
217 static void vErrorChecks( void *pvParameters )
\r
219 portTickType xExpectedWakeTime;
\r
220 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;
\r
221 const long lMaxAllowableTimeDifference = ( long ) 0;
\r
222 portTickType xWakeTime;
\r
223 long lTimeDifference;
\r
224 const char *pcReceivedMessage;
\r
225 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
227 /* Stop warnings. */
\r
228 ( void ) pvParameters;
\r
230 /* Loop continuously, blocking, then checking all the other tasks are still
\r
231 running, before blocking once again. This task blocks on the queue of messages
\r
232 that require displaying so will wake either by its time out expiring, or a
\r
233 message becoming available. */
\r
236 /* Calculate the time we will unblock if no messages are received
\r
237 on the queue. This is used to check that we have not blocked for too long. */
\r
238 xExpectedWakeTime = xTaskGetTickCount();
\r
239 xExpectedWakeTime += xPrintRate;
\r
241 /* Block waiting for either a time out or a message to be posted that
\r
242 required displaying. */
\r
243 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
245 /* Was a message received? */
\r
246 if( pcReceivedMessage == NULL )
\r
248 /* A message was not received so we timed out, did we unblock at the
\r
250 xWakeTime = xTaskGetTickCount();
\r
252 /* Calculate the difference between the time we unblocked and the
\r
253 time we should have unblocked. */
\r
254 if( xWakeTime > xExpectedWakeTime )
\r
256 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
260 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
263 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
265 /* We blocked too long - create a message that will get
\r
266 printed out the next time around. */
\r
267 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
270 /* Check the other tasks are still running, just in case. */
\r
271 prvCheckOtherTasksAreStillRunning();
\r
275 /* We unblocked due to a message becoming available. Send the message
\r
277 vDisplayMessage( pcReceivedMessage );
\r
280 /* Key presses are used to invoke the trace visualisation utility, or
\r
281 end the program. */
\r
282 prvCheckForKeyPresses();
\r
284 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */
\r
285 /*-----------------------------------------------------------*/
\r
287 static void prvCheckForKeyPresses( void )
\r
294 taskENTER_CRITICAL();
\r
296 taskEXIT_CRITICAL();
\r
300 unsigned long ulBufferLength;
\r
302 /* Key presses can be used to start/stop the trace utility, or end the
\r
307 /* Only define keys for turning on and off the trace if the trace
\r
309 #if configUSE_TRACE_FACILITY == 1
\r
310 case 't' : vTaskList( pcWriteBuffer );
\r
311 vWriteMessageToDisk( pcWriteBuffer );
\r
314 /* The legacy trace is no longer supported. Use FreeRTOS+Trace instead.
\r
315 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
318 case 'e' : ulBufferLength = ulTaskEndTrace();
\r
319 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
323 default : vTaskEndScheduler();
\r
329 ( void ) pcWriteBuffer;
\r
332 /*-----------------------------------------------------------*/
\r
334 static void prvCheckOtherTasksAreStillRunning( void )
\r
336 short sErrorHasOccurred = pdFALSE;
\r
338 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
340 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
341 sErrorHasOccurred = pdTRUE;
\r
344 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
346 vDisplayMessage( "Integer maths task count unchanged!\r\n" );
\r
347 sErrorHasOccurred = pdTRUE;
\r
350 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
352 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
353 sErrorHasOccurred = pdTRUE;
\r
356 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
358 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
359 sErrorHasOccurred = pdTRUE;
\r
362 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
364 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
365 sErrorHasOccurred = pdTRUE;
\r
368 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
370 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
371 sErrorHasOccurred = pdTRUE;
\r
374 if( sErrorHasOccurred == pdFALSE )
\r
376 vDisplayMessage( "OK " );
\r
377 /* Toggle the LED if everything is okay so we know if an error occurs even if not
\r
378 using console IO. */
\r
385 /* An error has occurred in one of the tasks. Don't go any further and
\r
386 flash the LED rapidly in case console IO is not being used. */
\r
388 vTaskDelay( mainERROR_FLASH_RATE );
\r
392 /*-----------------------------------------------------------*/
\r
394 static void prvInitLED( void )
\r
396 unsigned short usPortDirection;
\r
397 const unsigned short usLEDOut = 0x400;
\r
399 /* Set the LED bit to an output. */
\r
401 usPortDirection = inpw( mainLED_REG_DIR );
\r
402 usPortDirection &= ~usLEDOut;
\r
403 outpw( mainLED_REG_DIR, usPortDirection );
\r
405 /*-----------------------------------------------------------*/
\r
407 static void prvToggleLED( void )
\r
409 static short sLED = pdTRUE;
\r
410 unsigned short usLEDState;
\r
411 const unsigned short usLEDBit = 0x400;
\r
413 /* Flip the state of the LED. */
\r
414 usLEDState = inpw( mainLED_REG );
\r
417 usLEDState &= ~usLEDBit;
\r
421 usLEDState |= usLEDBit;
\r
423 outpw( mainLED_REG, usLEDState );
\r