2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * Creates all the demo application tasks, then starts the scheduler.
\r
68 * Main. c also creates a task called "Print". This only executes every five
\r
69 * seconds but has the highest priority so is guaranteed to get processor time.
\r
70 * Its main function is to check that all the other tasks are still operational.
\r
71 * Nearly all the tasks in the demo application maintain a unique count that is
\r
72 * incremented each time the task successfully completes its function. Should any
\r
73 * error occur within the task the count is permanently halted. The print task
\r
74 * checks the count of each task to ensure it has changed since the last time the
\r
75 * print task executed. If any count is found not to have changed the print task
\r
76 * displays an appropriate message, halts, and flashes the on board LED rapidly.
\r
77 * If all the tasks are still incrementing their unique counts the print task
\r
78 * displays an "OK" message.
\r
80 * The LED flash tasks do not maintain a count as they already provide visual
\r
81 * feedback of their status.
\r
83 * The print task blocks on the queue into which messages that require displaying
\r
84 * are posted. It will therefore only block for the full 5 seconds if no messages
\r
85 * are posted onto the queue.
\r
87 * Main. c also provides a demonstration of how the trace visualisation utility can
\r
88 * be used, and how the scheduler can be stopped.
\r
90 * On the Flashlite it is preferable not to try to write to the console during
\r
91 * real time operation. The built in LED is toggled every cycle of the print task
\r
92 * that does not encounter any errors, so the console IO may be removed if required.
\r
93 * The build in LED will start flashing rapidly if any task reports an error.
\r
99 + Previously, if an error occurred in a task the on board LED was stopped from
\r
100 toggling. Now if an error occurs the check task enters an infinite loop,
\r
101 toggling the LED rapidly.
\r
103 Changes from V1.2.3
\r
105 + The integer and comtest tasks are now used when the cooperative scheduler
\r
106 is being used. Previously they were only used with the preemptive
\r
109 Changes from V1.2.5
\r
111 + Made the communications RX task a higher priority.
\r
113 Changes from V2.0.0
\r
115 + Delay periods are now specified using variables and constants of
\r
116 portTickType rather than unsigned long.
\r
119 #include <stdlib.h>
\r
121 #include "FreeRTOS.h"
\r
123 #include "partest.h"
\r
124 #include "serial.h"
\r
126 /* Demo file headers. */
\r
127 #include "BlockQ.h"
\r
131 #include "integer.h"
\r
133 #include "comtest.h"
\r
134 #include "fileio.h"
\r
135 #include "semtest.h"
\r
137 /* Priority definitions for all the tasks in the demo application. */
\r
138 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
139 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
140 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 5 )
\r
141 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
142 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
143 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
144 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
146 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 256 )
\r
147 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\r
149 /* Constant definitions for accessing the build in LED on the Flashlite 186. */
\r
150 #define mainLED_REG_DIR ( ( unsigned short ) 0xff78 )
\r
151 #define mainLED_REG ( ( unsigned short ) 0xff7a )
\r
153 /* If an error is detected in a task then the vErrorChecks() task will enter
\r
154 an infinite loop flashing the LED at this rate. */
\r
155 #define mainERROR_FLASH_RATE ( ( portTickType ) 100 / portTICK_RATE_MS )
\r
157 /* Task function for the "Print" task as described at the top of the file. */
\r
158 static void vErrorChecks( void *pvParameters );
\r
160 /* Function that checks the unique count of all the other tasks as described at
\r
161 the top of the file. */
\r
162 static void prvCheckOtherTasksAreStillRunning( void );
\r
164 /* Functions to setup and use the built in LED on the Flashlite 186 board. */
\r
165 static void prvToggleLED( void );
\r
166 static void prvInitLED( void );
\r
168 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
170 static void prvCheckForKeyPresses( void );
\r
172 /* Buffer used by the trace visualisation utility. */
\r
173 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
175 /*-----------------------------------------------------------*/
\r
178 /* Initialise hardware and utilities. */
\r
179 vParTestInitialise();
\r
180 vPrintInitialise();
\r
183 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
185 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );
\r
186 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
187 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
188 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
189 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
190 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
192 /* Create the "Print" task as described at the top of the file. */
\r
193 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
195 /* This task has to be created last as it keeps account of the number of tasks
\r
196 it expects to see running. */
\r
197 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
199 /* Set the scheduler running. This function will not return unless a task
\r
200 calls vTaskEndScheduler(). */
\r
201 vTaskStartScheduler();
\r
205 /*-----------------------------------------------------------*/
\r
207 static void vErrorChecks( void *pvParameters )
\r
209 portTickType xExpectedWakeTime;
\r
210 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;
\r
211 const long lMaxAllowableTimeDifference = ( long ) 0;
\r
212 portTickType xWakeTime;
\r
213 long lTimeDifference;
\r
214 const char *pcReceivedMessage;
\r
215 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
217 /* Stop warnings. */
\r
218 ( void ) pvParameters;
\r
220 /* Loop continuously, blocking, then checking all the other tasks are still
\r
221 running, before blocking once again. This task blocks on the queue of messages
\r
222 that require displaying so will wake either by its time out expiring, or a
\r
223 message becoming available. */
\r
226 /* Calculate the time we will unblock if no messages are received
\r
227 on the queue. This is used to check that we have not blocked for too long. */
\r
228 xExpectedWakeTime = xTaskGetTickCount();
\r
229 xExpectedWakeTime += xPrintRate;
\r
231 /* Block waiting for either a time out or a message to be posted that
\r
232 required displaying. */
\r
233 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
235 /* Was a message received? */
\r
236 if( pcReceivedMessage == NULL )
\r
238 /* A message was not received so we timed out, did we unblock at the
\r
240 xWakeTime = xTaskGetTickCount();
\r
242 /* Calculate the difference between the time we unblocked and the
\r
243 time we should have unblocked. */
\r
244 if( xWakeTime > xExpectedWakeTime )
\r
246 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
250 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
253 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
255 /* We blocked too long - create a message that will get
\r
256 printed out the next time around. */
\r
257 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
260 /* Check the other tasks are still running, just in case. */
\r
261 prvCheckOtherTasksAreStillRunning();
\r
265 /* We unblocked due to a message becoming available. Send the message
\r
267 vDisplayMessage( pcReceivedMessage );
\r
270 /* Key presses are used to invoke the trace visualisation utility, or
\r
271 end the program. */
\r
272 prvCheckForKeyPresses();
\r
274 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */
\r
275 /*-----------------------------------------------------------*/
\r
277 static void prvCheckForKeyPresses( void )
\r
284 taskENTER_CRITICAL();
\r
286 taskEXIT_CRITICAL();
\r
290 unsigned long ulBufferLength;
\r
292 /* Key presses can be used to start/stop the trace utility, or end the
\r
297 /* Only define keys for turning on and off the trace if the trace
\r
299 #if configUSE_TRACE_FACILITY == 1
\r
300 case 't' : vTaskList( pcWriteBuffer );
\r
301 vWriteMessageToDisk( pcWriteBuffer );
\r
304 /* The legacy trace is no longer supported. Use FreeRTOS+Trace instead.
\r
305 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
308 case 'e' : ulBufferLength = ulTaskEndTrace();
\r
309 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
313 default : vTaskEndScheduler();
\r
319 ( void ) pcWriteBuffer;
\r
322 /*-----------------------------------------------------------*/
\r
324 static void prvCheckOtherTasksAreStillRunning( void )
\r
326 short sErrorHasOccurred = pdFALSE;
\r
328 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
330 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
331 sErrorHasOccurred = pdTRUE;
\r
334 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
336 vDisplayMessage( "Integer maths task count unchanged!\r\n" );
\r
337 sErrorHasOccurred = pdTRUE;
\r
340 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
342 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
343 sErrorHasOccurred = pdTRUE;
\r
346 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
348 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
349 sErrorHasOccurred = pdTRUE;
\r
352 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
354 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
355 sErrorHasOccurred = pdTRUE;
\r
358 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
360 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
361 sErrorHasOccurred = pdTRUE;
\r
364 if( sErrorHasOccurred == pdFALSE )
\r
366 vDisplayMessage( "OK " );
\r
367 /* Toggle the LED if everything is okay so we know if an error occurs even if not
\r
368 using console IO. */
\r
375 /* An error has occurred in one of the tasks. Don't go any further and
\r
376 flash the LED rapidly in case console IO is not being used. */
\r
378 vTaskDelay( mainERROR_FLASH_RATE );
\r
382 /*-----------------------------------------------------------*/
\r
384 static void prvInitLED( void )
\r
386 unsigned short usPortDirection;
\r
387 const unsigned short usLEDOut = 0x400;
\r
389 /* Set the LED bit to an output. */
\r
391 usPortDirection = inpw( mainLED_REG_DIR );
\r
392 usPortDirection &= ~usLEDOut;
\r
393 outpw( mainLED_REG_DIR, usPortDirection );
\r
395 /*-----------------------------------------------------------*/
\r
397 static void prvToggleLED( void )
\r
399 static short sLED = pdTRUE;
\r
400 unsigned short usLEDState;
\r
401 const unsigned short usLEDBit = 0x400;
\r
403 /* Flip the state of the LED. */
\r
404 usLEDState = inpw( mainLED_REG );
\r
407 usLEDState &= ~usLEDBit;
\r
411 usLEDState |= usLEDBit;
\r
413 outpw( mainLED_REG, usLEDState );
\r