2 FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
49 * Creates all the demo application tasks, then starts the scheduler.
\r
51 * Main. c also creates a task called "Print". This only executes every five
\r
52 * seconds but has the highest priority so is guaranteed to get processor time.
\r
53 * Its main function is to check that all the other tasks are still operational.
\r
54 * Nearly all the tasks in the demo application maintain a unique count that is
\r
55 * incremented each time the task successfully completes its function. Should any
\r
56 * error occur within the task the count is permanently halted. The print task
\r
57 * checks the count of each task to ensure it has changed since the last time the
\r
58 * print task executed. If any count is found not to have changed the print task
\r
59 * displays an appropriate message, halts, and flashes the on board LED rapidly.
\r
60 * If all the tasks are still incrementing their unique counts the print task
\r
61 * displays an "OK" message.
\r
63 * The LED flash tasks do not maintain a count as they already provide visual
\r
64 * feedback of their status.
\r
66 * The print task blocks on the queue into which messages that require displaying
\r
67 * are posted. It will therefore only block for the full 5 seconds if no messages
\r
68 * are posted onto the queue.
\r
70 * Main. c also provides a demonstration of how the trace visualisation utility can
\r
71 * be used, and how the scheduler can be stopped.
\r
73 * On the Flashlite it is preferable not to try to write to the console during
\r
74 * real time operation. The built in LED is toggled every cycle of the print task
\r
75 * that does not encounter any errors, so the console IO may be removed if required.
\r
76 * The build in LED will start flashing rapidly if any task reports an error.
\r
82 + Previously, if an error occurred in a task the on board LED was stopped from
\r
83 toggling. Now if an error occurs the check task enters an infinite loop,
\r
84 toggling the LED rapidly.
\r
88 + The integer and comtest tasks are now used when the cooperative scheduler
\r
89 is being used. Previously they were only used with the preemptive
\r
94 + Made the communications RX task a higher priority.
\r
98 + Delay periods are now specified using variables and constants of
\r
99 portTickType rather than unsigned portLONG.
\r
102 #include <stdlib.h>
\r
104 #include "FreeRTOS.h"
\r
106 #include "partest.h"
\r
107 #include "serial.h"
\r
109 /* Demo file headers. */
\r
110 #include "BlockQ.h"
\r
114 #include "integer.h"
\r
116 #include "comtest.h"
\r
117 #include "fileio.h"
\r
118 #include "semtest.h"
\r
120 /* Priority definitions for all the tasks in the demo application. */
\r
121 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
122 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
123 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 5 )
\r
124 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
125 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
126 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
127 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
129 #define mainPRINT_STACK_SIZE ( ( unsigned portSHORT ) 256 )
\r
130 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned portSHORT ) 20480 )
\r
132 /* Constant definitions for accessing the build in LED on the Flashlite 186. */
\r
133 #define mainLED_REG_DIR ( ( unsigned portSHORT ) 0xff78 )
\r
134 #define mainLED_REG ( ( unsigned portSHORT ) 0xff7a )
\r
136 /* If an error is detected in a task then the vErrorChecks() task will enter
\r
137 an infinite loop flashing the LED at this rate. */
\r
138 #define mainERROR_FLASH_RATE ( ( portTickType ) 100 / portTICK_RATE_MS )
\r
140 /* Task function for the "Print" task as described at the top of the file. */
\r
141 static void vErrorChecks( void *pvParameters );
\r
143 /* Function that checks the unique count of all the other tasks as described at
\r
144 the top of the file. */
\r
145 static void prvCheckOtherTasksAreStillRunning( void );
\r
147 /* Functions to setup and use the built in LED on the Flashlite 186 board. */
\r
148 static void prvToggleLED( void );
\r
149 static void prvInitLED( void );
\r
151 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
153 static void prvCheckForKeyPresses( void );
\r
155 /* Buffer used by the trace visualisation utility. */
\r
156 static portCHAR pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
158 /*-----------------------------------------------------------*/
\r
159 portSHORT main( void )
\r
161 /* Initialise hardware and utilities. */
\r
162 vParTestInitialise();
\r
163 vPrintInitialise();
\r
166 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
168 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );
\r
169 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
170 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
171 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
172 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
173 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
175 /* Create the "Print" task as described at the top of the file. */
\r
176 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
178 /* This task has to be created last as it keeps account of the number of tasks
\r
179 it expects to see running. */
\r
180 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
182 /* Set the scheduler running. This function will not return unless a task
\r
183 calls vTaskEndScheduler(). */
\r
184 vTaskStartScheduler();
\r
188 /*-----------------------------------------------------------*/
\r
190 static void vErrorChecks( void *pvParameters )
\r
192 portTickType xExpectedWakeTime;
\r
193 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;
\r
194 const portLONG lMaxAllowableTimeDifference = ( portLONG ) 0;
\r
195 portTickType xWakeTime;
\r
196 portLONG lTimeDifference;
\r
197 const portCHAR *pcReceivedMessage;
\r
198 const portCHAR * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
200 /* Stop warnings. */
\r
201 ( void ) pvParameters;
\r
203 /* Loop continuously, blocking, then checking all the other tasks are still
\r
204 running, before blocking once again. This task blocks on the queue of messages
\r
205 that require displaying so will wake either by its time out expiring, or a
\r
206 message becoming available. */
\r
209 /* Calculate the time we will unblock if no messages are received
\r
210 on the queue. This is used to check that we have not blocked for too long. */
\r
211 xExpectedWakeTime = xTaskGetTickCount();
\r
212 xExpectedWakeTime += xPrintRate;
\r
214 /* Block waiting for either a time out or a message to be posted that
\r
215 required displaying. */
\r
216 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
218 /* Was a message received? */
\r
219 if( pcReceivedMessage == NULL )
\r
221 /* A message was not received so we timed out, did we unblock at the
\r
223 xWakeTime = xTaskGetTickCount();
\r
225 /* Calculate the difference between the time we unblocked and the
\r
226 time we should have unblocked. */
\r
227 if( xWakeTime > xExpectedWakeTime )
\r
229 lTimeDifference = ( portLONG ) ( xWakeTime - xExpectedWakeTime );
\r
233 lTimeDifference = ( portLONG ) ( xExpectedWakeTime - xWakeTime );
\r
236 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
238 /* We blocked too long - create a message that will get
\r
239 printed out the next time around. */
\r
240 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
243 /* Check the other tasks are still running, just in case. */
\r
244 prvCheckOtherTasksAreStillRunning();
\r
248 /* We unblocked due to a message becoming available. Send the message
\r
250 vDisplayMessage( pcReceivedMessage );
\r
253 /* Key presses are used to invoke the trace visualisation utility, or
\r
254 end the program. */
\r
255 prvCheckForKeyPresses();
\r
257 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */
\r
258 /*-----------------------------------------------------------*/
\r
260 static void prvCheckForKeyPresses( void )
\r
267 taskENTER_CRITICAL();
\r
269 taskEXIT_CRITICAL();
\r
273 unsigned portLONG ulBufferLength;
\r
275 /* Key presses can be used to start/stop the trace utility, or end the
\r
280 /* Only define keys for turning on and off the trace if the trace
\r
282 #if configUSE_TRACE_FACILITY == 1
\r
283 case 't' : vTaskList( pcWriteBuffer );
\r
284 vWriteMessageToDisk( pcWriteBuffer );
\r
287 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
290 case 'e' : ulBufferLength = ulTaskEndTrace();
\r
291 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
295 default : vTaskEndScheduler();
\r
301 ( void ) pcWriteBuffer;
\r
304 /*-----------------------------------------------------------*/
\r
306 static void prvCheckOtherTasksAreStillRunning( void )
\r
308 portSHORT sErrorHasOccurred = pdFALSE;
\r
310 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
312 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
313 sErrorHasOccurred = pdTRUE;
\r
316 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
318 vDisplayMessage( "Integer maths task count unchanged!\r\n" );
\r
319 sErrorHasOccurred = pdTRUE;
\r
322 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
324 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
325 sErrorHasOccurred = pdTRUE;
\r
328 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
330 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
331 sErrorHasOccurred = pdTRUE;
\r
334 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
336 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
337 sErrorHasOccurred = pdTRUE;
\r
340 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
342 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
343 sErrorHasOccurred = pdTRUE;
\r
346 if( sErrorHasOccurred == pdFALSE )
\r
348 vDisplayMessage( "OK " );
\r
349 /* Toggle the LED if everything is okay so we know if an error occurs even if not
\r
350 using console IO. */
\r
357 /* An error has occurred in one of the tasks. Don't go any further and
\r
358 flash the LED rapidly in case console IO is not being used. */
\r
360 vTaskDelay( mainERROR_FLASH_RATE );
\r
364 /*-----------------------------------------------------------*/
\r
366 static void prvInitLED( void )
\r
368 unsigned portSHORT usPortDirection;
\r
369 const unsigned portSHORT usLEDOut = 0x400;
\r
371 /* Set the LED bit to an output. */
\r
373 usPortDirection = inpw( mainLED_REG_DIR );
\r
374 usPortDirection &= ~usLEDOut;
\r
375 outpw( mainLED_REG_DIR, usPortDirection );
\r
377 /*-----------------------------------------------------------*/
\r
379 static void prvToggleLED( void )
\r
381 static portSHORT sLED = pdTRUE;
\r
382 unsigned portSHORT usLEDState;
\r
383 const unsigned portSHORT usLEDBit = 0x400;
\r
385 /* Flip the state of the LED. */
\r
386 usLEDState = inpw( mainLED_REG );
\r
389 usLEDState &= ~usLEDBit;
\r
393 usLEDState |= usLEDBit;
\r
395 outpw( mainLED_REG, usLEDState );
\r