2 FreeRTOS.org V5.3.1 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the 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.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 * Creates all the demo application tasks, then starts the scheduler.
\r
55 * Main. c also creates a task called "Print". This only executes every five
\r
56 * seconds but has the highest priority so is guaranteed to get processor time.
\r
57 * Its main function is to check that all the other tasks are still operational.
\r
58 * Nearly all the tasks in the demo application maintain a unique count that is
\r
59 * incremented each time the task successfully completes its function. Should any
\r
60 * error occur within the task the count is permanently halted. The print task
\r
61 * checks the count of each task to ensure it has changed since the last time the
\r
62 * print task executed. If any count is found not to have changed the print task
\r
63 * displays an appropriate message, halts, and flashes the on board LED rapidly.
\r
64 * If all the tasks are still incrementing their unique counts the print task
\r
65 * displays an "OK" message.
\r
67 * The LED flash tasks do not maintain a count as they already provide visual
\r
68 * feedback of their status.
\r
70 * The print task blocks on the queue into which messages that require displaying
\r
71 * are posted. It will therefore only block for the full 5 seconds if no messages
\r
72 * are posted onto the queue.
\r
74 * Main. c also provides a demonstration of how the trace visualisation utility can
\r
75 * be used, and how the scheduler can be stopped.
\r
77 * On the Flashlite it is preferable not to try to write to the console during
\r
78 * real time operation. The built in LED is toggled every cycle of the print task
\r
79 * that does not encounter any errors, so the console IO may be removed if required.
\r
80 * The build in LED will start flashing rapidly if any task reports an error.
\r
86 + Previously, if an error occurred in a task the on board LED was stopped from
\r
87 toggling. Now if an error occurs the check task enters an infinite loop,
\r
88 toggling the LED rapidly.
\r
92 + The integer and comtest tasks are now used when the cooperative scheduler
\r
93 is being used. Previously they were only used with the preemptive
\r
98 + Made the communications RX task a higher priority.
\r
100 Changes from V2.0.0
\r
102 + Delay periods are now specified using variables and constants of
\r
103 portTickType rather than unsigned portLONG.
\r
106 #include <stdlib.h>
\r
108 #include "FreeRTOS.h"
\r
110 #include "partest.h"
\r
111 #include "serial.h"
\r
113 /* Demo file headers. */
\r
114 #include "BlockQ.h"
\r
118 #include "integer.h"
\r
120 #include "comtest.h"
\r
121 #include "fileio.h"
\r
122 #include "semtest.h"
\r
124 /* Priority definitions for all the tasks in the demo application. */
\r
125 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
126 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
127 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 5 )
\r
128 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
129 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
130 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
131 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
133 #define mainPRINT_STACK_SIZE ( ( unsigned portSHORT ) 256 )
\r
134 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned portSHORT ) 20480 )
\r
136 /* Constant definitions for accessing the build in LED on the Flashlite 186. */
\r
137 #define mainLED_REG_DIR ( ( unsigned portSHORT ) 0xff78 )
\r
138 #define mainLED_REG ( ( unsigned portSHORT ) 0xff7a )
\r
140 /* If an error is detected in a task then the vErrorChecks() task will enter
\r
141 an infinite loop flashing the LED at this rate. */
\r
142 #define mainERROR_FLASH_RATE ( ( portTickType ) 100 / portTICK_RATE_MS )
\r
144 /* Task function for the "Print" task as described at the top of the file. */
\r
145 static void vErrorChecks( void *pvParameters );
\r
147 /* Function that checks the unique count of all the other tasks as described at
\r
148 the top of the file. */
\r
149 static void prvCheckOtherTasksAreStillRunning( void );
\r
151 /* Functions to setup and use the built in LED on the Flashlite 186 board. */
\r
152 static void prvToggleLED( void );
\r
153 static void prvInitLED( void );
\r
155 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
157 static void prvCheckForKeyPresses( void );
\r
159 /* Buffer used by the trace visualisation utility. */
\r
160 static portCHAR pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
162 /*-----------------------------------------------------------*/
\r
163 portSHORT main( void )
\r
165 /* Initialise hardware and utilities. */
\r
166 vParTestInitialise();
\r
167 vPrintInitialise();
\r
170 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
172 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );
\r
173 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
174 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
175 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
176 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
177 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
179 /* Create the "Print" task as described at the top of the file. */
\r
180 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
182 /* This task has to be created last as it keeps account of the number of tasks
\r
183 it expects to see running. */
\r
184 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
186 /* Set the scheduler running. This function will not return unless a task
\r
187 calls vTaskEndScheduler(). */
\r
188 vTaskStartScheduler();
\r
192 /*-----------------------------------------------------------*/
\r
194 static void vErrorChecks( void *pvParameters )
\r
196 portTickType xExpectedWakeTime;
\r
197 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;
\r
198 const portLONG lMaxAllowableTimeDifference = ( portLONG ) 0;
\r
199 portTickType xWakeTime;
\r
200 portLONG lTimeDifference;
\r
201 const portCHAR *pcReceivedMessage;
\r
202 const portCHAR * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
204 /* Stop warnings. */
\r
205 ( void ) pvParameters;
\r
207 /* Loop continuously, blocking, then checking all the other tasks are still
\r
208 running, before blocking once again. This task blocks on the queue of messages
\r
209 that require displaying so will wake either by its time out expiring, or a
\r
210 message becoming available. */
\r
213 /* Calculate the time we will unblock if no messages are received
\r
214 on the queue. This is used to check that we have not blocked for too long. */
\r
215 xExpectedWakeTime = xTaskGetTickCount();
\r
216 xExpectedWakeTime += xPrintRate;
\r
218 /* Block waiting for either a time out or a message to be posted that
\r
219 required displaying. */
\r
220 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
222 /* Was a message received? */
\r
223 if( pcReceivedMessage == NULL )
\r
225 /* A message was not received so we timed out, did we unblock at the
\r
227 xWakeTime = xTaskGetTickCount();
\r
229 /* Calculate the difference between the time we unblocked and the
\r
230 time we should have unblocked. */
\r
231 if( xWakeTime > xExpectedWakeTime )
\r
233 lTimeDifference = ( portLONG ) ( xWakeTime - xExpectedWakeTime );
\r
237 lTimeDifference = ( portLONG ) ( xExpectedWakeTime - xWakeTime );
\r
240 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
242 /* We blocked too long - create a message that will get
\r
243 printed out the next time around. */
\r
244 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
247 /* Check the other tasks are still running, just in case. */
\r
248 prvCheckOtherTasksAreStillRunning();
\r
252 /* We unblocked due to a message becoming available. Send the message
\r
254 vDisplayMessage( pcReceivedMessage );
\r
257 /* Key presses are used to invoke the trace visualisation utility, or
\r
258 end the program. */
\r
259 prvCheckForKeyPresses();
\r
261 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */
\r
262 /*-----------------------------------------------------------*/
\r
264 static void prvCheckForKeyPresses( void )
\r
271 taskENTER_CRITICAL();
\r
273 taskEXIT_CRITICAL();
\r
277 unsigned portLONG ulBufferLength;
\r
279 /* Key presses can be used to start/stop the trace utility, or end the
\r
284 /* Only define keys for turning on and off the trace if the trace
\r
286 #if configUSE_TRACE_FACILITY == 1
\r
287 case 't' : vTaskList( pcWriteBuffer );
\r
288 vWriteMessageToDisk( pcWriteBuffer );
\r
291 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
294 case 'e' : ulBufferLength = ulTaskEndTrace();
\r
295 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
299 default : vTaskEndScheduler();
\r
305 ( void ) pcWriteBuffer;
\r
308 /*-----------------------------------------------------------*/
\r
310 static void prvCheckOtherTasksAreStillRunning( void )
\r
312 portSHORT sErrorHasOccurred = pdFALSE;
\r
314 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
316 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
317 sErrorHasOccurred = pdTRUE;
\r
320 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
322 vDisplayMessage( "Integer maths task count unchanged!\r\n" );
\r
323 sErrorHasOccurred = pdTRUE;
\r
326 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
328 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
329 sErrorHasOccurred = pdTRUE;
\r
332 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
334 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
335 sErrorHasOccurred = pdTRUE;
\r
338 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
340 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
341 sErrorHasOccurred = pdTRUE;
\r
344 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
346 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
347 sErrorHasOccurred = pdTRUE;
\r
350 if( sErrorHasOccurred == pdFALSE )
\r
352 vDisplayMessage( "OK " );
\r
353 /* Toggle the LED if everything is okay so we know if an error occurs even if not
\r
354 using console IO. */
\r
361 /* An error has occurred in one of the tasks. Don't go any further and
\r
362 flash the LED rapidly in case console IO is not being used. */
\r
364 vTaskDelay( mainERROR_FLASH_RATE );
\r
368 /*-----------------------------------------------------------*/
\r
370 static void prvInitLED( void )
\r
372 unsigned portSHORT usPortDirection;
\r
373 const unsigned portSHORT usLEDOut = 0x400;
\r
375 /* Set the LED bit to an output. */
\r
377 usPortDirection = inpw( mainLED_REG_DIR );
\r
378 usPortDirection &= ~usLEDOut;
\r
379 outpw( mainLED_REG_DIR, usPortDirection );
\r
381 /*-----------------------------------------------------------*/
\r
383 static void prvToggleLED( void )
\r
385 static portSHORT sLED = pdTRUE;
\r
386 unsigned portSHORT usLEDState;
\r
387 const unsigned portSHORT usLEDBit = 0x400;
\r
389 /* Flip the state of the LED. */
\r
390 usLEDState = inpw( mainLED_REG );
\r
393 usLEDState &= ~usLEDBit;
\r
397 usLEDState |= usLEDBit;
\r
399 outpw( mainLED_REG, usLEDState );
\r