2 FreeRTOS V6.0.0 - 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 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
50 * Creates all the demo application tasks, then starts the scheduler.
\r
52 * Main. c also creates a task called "Print". This only executes every five
\r
53 * seconds but has the highest priority so is guaranteed to get processor time.
\r
54 * Its main function is to check that all the other tasks are still operational.
\r
55 * Nearly all the tasks in the demo application maintain a unique count that is
\r
56 * incremented each time the task successfully completes its function. Should any
\r
57 * error occur within the task the count is permanently halted. The print task
\r
58 * checks the count of each task to ensure it has changed since the last time the
\r
59 * print task executed. If any count is found not to have changed the print task
\r
60 * displays an appropriate message, halts, and flashes the on board LED rapidly.
\r
61 * If all the tasks are still incrementing their unique counts the print task
\r
62 * displays an "OK" message.
\r
64 * The LED flash tasks do not maintain a count as they already provide visual
\r
65 * feedback of their status.
\r
67 * The print task blocks on the queue into which messages that require displaying
\r
68 * are posted. It will therefore only block for the full 5 seconds if no messages
\r
69 * are posted onto the queue.
\r
71 * Main. c also provides a demonstration of how the trace visualisation utility can
\r
72 * be used, and how the scheduler can be stopped.
\r
74 * On the Flashlite it is preferable not to try to write to the console during
\r
75 * real time operation. The built in LED is toggled every cycle of the print task
\r
76 * that does not encounter any errors, so the console IO may be removed if required.
\r
77 * The build in LED will start flashing rapidly if any task reports an error.
\r
83 + Previously, if an error occurred in a task the on board LED was stopped from
\r
84 toggling. Now if an error occurs the check task enters an infinite loop,
\r
85 toggling the LED rapidly.
\r
89 + The integer and comtest tasks are now used when the cooperative scheduler
\r
90 is being used. Previously they were only used with the preemptive
\r
95 + Made the communications RX task a higher priority.
\r
99 + Delay periods are now specified using variables and constants of
\r
100 portTickType rather than unsigned long.
\r
103 #include <stdlib.h>
\r
105 #include "FreeRTOS.h"
\r
107 #include "partest.h"
\r
108 #include "serial.h"
\r
110 /* Demo file headers. */
\r
111 #include "BlockQ.h"
\r
115 #include "integer.h"
\r
117 #include "comtest.h"
\r
118 #include "fileio.h"
\r
119 #include "semtest.h"
\r
121 /* Priority definitions for all the tasks in the demo application. */
\r
122 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
123 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
124 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 5 )
\r
125 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
126 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
127 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
128 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
130 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 256 )
\r
131 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\r
133 /* Constant definitions for accessing the build in LED on the Flashlite 186. */
\r
134 #define mainLED_REG_DIR ( ( unsigned short ) 0xff78 )
\r
135 #define mainLED_REG ( ( unsigned short ) 0xff7a )
\r
137 /* If an error is detected in a task then the vErrorChecks() task will enter
\r
138 an infinite loop flashing the LED at this rate. */
\r
139 #define mainERROR_FLASH_RATE ( ( portTickType ) 100 / portTICK_RATE_MS )
\r
141 /* Task function for the "Print" task as described at the top of the file. */
\r
142 static void vErrorChecks( void *pvParameters );
\r
144 /* Function that checks the unique count of all the other tasks as described at
\r
145 the top of the file. */
\r
146 static void prvCheckOtherTasksAreStillRunning( void );
\r
148 /* Functions to setup and use the built in LED on the Flashlite 186 board. */
\r
149 static void prvToggleLED( void );
\r
150 static void prvInitLED( void );
\r
152 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
154 static void prvCheckForKeyPresses( void );
\r
156 /* Buffer used by the trace visualisation utility. */
\r
157 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
159 /*-----------------------------------------------------------*/
\r
162 /* Initialise hardware and utilities. */
\r
163 vParTestInitialise();
\r
164 vPrintInitialise();
\r
167 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
169 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );
\r
170 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
171 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
172 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
173 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
174 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
176 /* Create the "Print" task as described at the top of the file. */
\r
177 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
179 /* This task has to be created last as it keeps account of the number of tasks
\r
180 it expects to see running. */
\r
181 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
183 /* Set the scheduler running. This function will not return unless a task
\r
184 calls vTaskEndScheduler(). */
\r
185 vTaskStartScheduler();
\r
189 /*-----------------------------------------------------------*/
\r
191 static void vErrorChecks( void *pvParameters )
\r
193 portTickType xExpectedWakeTime;
\r
194 const portTickType xPrintRate = ( portTickType ) 5000 / portTICK_RATE_MS;
\r
195 const long lMaxAllowableTimeDifference = ( long ) 0;
\r
196 portTickType xWakeTime;
\r
197 long lTimeDifference;
\r
198 const char *pcReceivedMessage;
\r
199 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
201 /* Stop warnings. */
\r
202 ( void ) pvParameters;
\r
204 /* Loop continuously, blocking, then checking all the other tasks are still
\r
205 running, before blocking once again. This task blocks on the queue of messages
\r
206 that require displaying so will wake either by its time out expiring, or a
\r
207 message becoming available. */
\r
210 /* Calculate the time we will unblock if no messages are received
\r
211 on the queue. This is used to check that we have not blocked for too long. */
\r
212 xExpectedWakeTime = xTaskGetTickCount();
\r
213 xExpectedWakeTime += xPrintRate;
\r
215 /* Block waiting for either a time out or a message to be posted that
\r
216 required displaying. */
\r
217 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
219 /* Was a message received? */
\r
220 if( pcReceivedMessage == NULL )
\r
222 /* A message was not received so we timed out, did we unblock at the
\r
224 xWakeTime = xTaskGetTickCount();
\r
226 /* Calculate the difference between the time we unblocked and the
\r
227 time we should have unblocked. */
\r
228 if( xWakeTime > xExpectedWakeTime )
\r
230 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
234 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
237 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
239 /* We blocked too long - create a message that will get
\r
240 printed out the next time around. */
\r
241 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
244 /* Check the other tasks are still running, just in case. */
\r
245 prvCheckOtherTasksAreStillRunning();
\r
249 /* We unblocked due to a message becoming available. Send the message
\r
251 vDisplayMessage( pcReceivedMessage );
\r
254 /* Key presses are used to invoke the trace visualisation utility, or
\r
255 end the program. */
\r
256 prvCheckForKeyPresses();
\r
258 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */
\r
259 /*-----------------------------------------------------------*/
\r
261 static void prvCheckForKeyPresses( void )
\r
268 taskENTER_CRITICAL();
\r
270 taskEXIT_CRITICAL();
\r
274 unsigned long ulBufferLength;
\r
276 /* Key presses can be used to start/stop the trace utility, or end the
\r
281 /* Only define keys for turning on and off the trace if the trace
\r
283 #if configUSE_TRACE_FACILITY == 1
\r
284 case 't' : vTaskList( pcWriteBuffer );
\r
285 vWriteMessageToDisk( pcWriteBuffer );
\r
288 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
291 case 'e' : ulBufferLength = ulTaskEndTrace();
\r
292 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
296 default : vTaskEndScheduler();
\r
302 ( void ) pcWriteBuffer;
\r
305 /*-----------------------------------------------------------*/
\r
307 static void prvCheckOtherTasksAreStillRunning( void )
\r
309 short sErrorHasOccurred = pdFALSE;
\r
311 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
313 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
314 sErrorHasOccurred = pdTRUE;
\r
317 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
319 vDisplayMessage( "Integer maths task count unchanged!\r\n" );
\r
320 sErrorHasOccurred = pdTRUE;
\r
323 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
325 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
326 sErrorHasOccurred = pdTRUE;
\r
329 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
331 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
332 sErrorHasOccurred = pdTRUE;
\r
335 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
337 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
338 sErrorHasOccurred = pdTRUE;
\r
341 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
343 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
344 sErrorHasOccurred = pdTRUE;
\r
347 if( sErrorHasOccurred == pdFALSE )
\r
349 vDisplayMessage( "OK " );
\r
350 /* Toggle the LED if everything is okay so we know if an error occurs even if not
\r
351 using console IO. */
\r
358 /* An error has occurred in one of the tasks. Don't go any further and
\r
359 flash the LED rapidly in case console IO is not being used. */
\r
361 vTaskDelay( mainERROR_FLASH_RATE );
\r
365 /*-----------------------------------------------------------*/
\r
367 static void prvInitLED( void )
\r
369 unsigned short usPortDirection;
\r
370 const unsigned short usLEDOut = 0x400;
\r
372 /* Set the LED bit to an output. */
\r
374 usPortDirection = inpw( mainLED_REG_DIR );
\r
375 usPortDirection &= ~usLEDOut;
\r
376 outpw( mainLED_REG_DIR, usPortDirection );
\r
378 /*-----------------------------------------------------------*/
\r
380 static void prvToggleLED( void )
\r
382 static short sLED = pdTRUE;
\r
383 unsigned short usLEDState;
\r
384 const unsigned short usLEDBit = 0x400;
\r
386 /* Flip the state of the LED. */
\r
387 usLEDState = inpw( mainLED_REG );
\r
390 usLEDState &= ~usLEDBit;
\r
394 usLEDState |= usLEDBit;
\r
396 outpw( mainLED_REG, usLEDState );
\r