2 FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 * Creates all the demo application tasks, then starts the scheduler.
\r
73 * Main. c also creates a task called "Print". This only executes every five
\r
74 * seconds but has the highest priority so is guaranteed to get processor time.
\r
75 * Its main function is to check that all the other tasks are still operational.
\r
76 * Nearly all the tasks in the demo application maintain a unique count that is
\r
77 * incremented each time the task successfully completes its function. Should any
\r
78 * error occur within the task the count is permanently halted. The print task
\r
79 * checks the count of each task to ensure it has changed since the last time the
\r
80 * print task executed. If any count is found not to have changed the print task
\r
81 * displays an appropriate message, halts, and flashes the on board LED rapidly.
\r
82 * If all the tasks are still incrementing their unique counts the print task
\r
83 * displays an "OK" message.
\r
85 * The LED flash tasks do not maintain a count as they already provide visual
\r
86 * feedback of their status.
\r
88 * The print task blocks on the queue into which messages that require displaying
\r
89 * are posted. It will therefore only block for the full 5 seconds if no messages
\r
90 * are posted onto the queue.
\r
92 * Main. c also provides a demonstration of how the trace visualisation utility can
\r
93 * be used, and how the scheduler can be stopped.
\r
95 * On the Flashlite it is preferable not to try to write to the console during
\r
96 * real time operation. The built in LED is toggled every cycle of the print task
\r
97 * that does not encounter any errors, so the console IO may be removed if required.
\r
98 * The build in LED will start flashing rapidly if any task reports an error.
\r
102 Changes from V1.01:
\r
104 + Previously, if an error occurred in a task the on board LED was stopped from
\r
105 toggling. Now if an error occurs the check task enters an infinite loop,
\r
106 toggling the LED rapidly.
\r
108 Changes from V1.2.3
\r
110 + The integer and comtest tasks are now used when the cooperative scheduler
\r
111 is being used. Previously they were only used with the preemptive
\r
114 Changes from V1.2.5
\r
116 + Made the communications RX task a higher priority.
\r
118 Changes from V2.0.0
\r
120 + Delay periods are now specified using variables and constants of
\r
121 TickType_t rather than unsigned long.
\r
124 #include <stdlib.h>
\r
126 #include "FreeRTOS.h"
\r
128 #include "partest.h"
\r
129 #include "serial.h"
\r
131 /* Demo file headers. */
\r
132 #include "BlockQ.h"
\r
136 #include "integer.h"
\r
138 #include "comtest.h"
\r
139 #include "fileio.h"
\r
140 #include "semtest.h"
\r
142 /* Priority definitions for all the tasks in the demo application. */
\r
143 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
144 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
145 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 5 )
\r
146 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
147 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
148 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
149 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
151 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 256 )
\r
152 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\r
154 /* Constant definitions for accessing the build in LED on the Flashlite 186. */
\r
155 #define mainLED_REG_DIR ( ( unsigned short ) 0xff78 )
\r
156 #define mainLED_REG ( ( unsigned short ) 0xff7a )
\r
158 /* If an error is detected in a task then the vErrorChecks() task will enter
\r
159 an infinite loop flashing the LED at this rate. */
\r
160 #define mainERROR_FLASH_RATE ( ( TickType_t ) 100 / portTICK_PERIOD_MS )
\r
162 /* Task function for the "Print" task as described at the top of the file. */
\r
163 static void vErrorChecks( void *pvParameters );
\r
165 /* Function that checks the unique count of all the other tasks as described at
\r
166 the top of the file. */
\r
167 static void prvCheckOtherTasksAreStillRunning( void );
\r
169 /* Functions to setup and use the built in LED on the Flashlite 186 board. */
\r
170 static void prvToggleLED( void );
\r
171 static void prvInitLED( void );
\r
173 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
175 static void prvCheckForKeyPresses( void );
\r
177 /* Buffer used by the trace visualisation utility. */
\r
178 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
180 /*-----------------------------------------------------------*/
\r
183 /* Initialise hardware and utilities. */
\r
184 vParTestInitialise();
\r
185 vPrintInitialise();
\r
188 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
190 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );
\r
191 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
192 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
193 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
194 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
195 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
197 /* Create the "Print" task as described at the top of the file. */
\r
198 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
200 /* This task has to be created last as it keeps account of the number of tasks
\r
201 it expects to see running. */
\r
202 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
204 /* Set the scheduler running. This function will not return unless a task
\r
205 calls vTaskEndScheduler(). */
\r
206 vTaskStartScheduler();
\r
210 /*-----------------------------------------------------------*/
\r
212 static void vErrorChecks( void *pvParameters )
\r
214 TickType_t xExpectedWakeTime;
\r
215 const TickType_t xPrintRate = ( TickType_t ) 5000 / portTICK_PERIOD_MS;
\r
216 const long lMaxAllowableTimeDifference = ( long ) 0;
\r
217 TickType_t xWakeTime;
\r
218 long lTimeDifference;
\r
219 const char *pcReceivedMessage;
\r
220 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
222 /* Stop warnings. */
\r
223 ( void ) pvParameters;
\r
225 /* Loop continuously, blocking, then checking all the other tasks are still
\r
226 running, before blocking once again. This task blocks on the queue of messages
\r
227 that require displaying so will wake either by its time out expiring, or a
\r
228 message becoming available. */
\r
231 /* Calculate the time we will unblock if no messages are received
\r
232 on the queue. This is used to check that we have not blocked for too long. */
\r
233 xExpectedWakeTime = xTaskGetTickCount();
\r
234 xExpectedWakeTime += xPrintRate;
\r
236 /* Block waiting for either a time out or a message to be posted that
\r
237 required displaying. */
\r
238 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
240 /* Was a message received? */
\r
241 if( pcReceivedMessage == NULL )
\r
243 /* A message was not received so we timed out, did we unblock at the
\r
245 xWakeTime = xTaskGetTickCount();
\r
247 /* Calculate the difference between the time we unblocked and the
\r
248 time we should have unblocked. */
\r
249 if( xWakeTime > xExpectedWakeTime )
\r
251 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
255 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
258 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
260 /* We blocked too long - create a message that will get
\r
261 printed out the next time around. */
\r
262 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
265 /* Check the other tasks are still running, just in case. */
\r
266 prvCheckOtherTasksAreStillRunning();
\r
270 /* We unblocked due to a message becoming available. Send the message
\r
272 vDisplayMessage( pcReceivedMessage );
\r
275 /* Key presses are used to invoke the trace visualisation utility, or
\r
276 end the program. */
\r
277 prvCheckForKeyPresses();
\r
279 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */
\r
280 /*-----------------------------------------------------------*/
\r
282 static void prvCheckForKeyPresses( void )
\r
289 taskENTER_CRITICAL();
\r
291 taskEXIT_CRITICAL();
\r
295 unsigned long ulBufferLength;
\r
297 /* Key presses can be used to start/stop the trace utility, or end the
\r
302 /* Only define keys for turning on and off the trace if the trace
\r
304 #if configUSE_TRACE_FACILITY == 1
\r
305 case 't' : vTaskList( pcWriteBuffer );
\r
306 vWriteMessageToDisk( pcWriteBuffer );
\r
309 /* The legacy trace is no longer supported. Use FreeRTOS+Trace instead.
\r
310 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
313 case 'e' : ulBufferLength = ulTaskEndTrace();
\r
314 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
318 default : vTaskEndScheduler();
\r
324 ( void ) pcWriteBuffer;
\r
327 /*-----------------------------------------------------------*/
\r
329 static void prvCheckOtherTasksAreStillRunning( void )
\r
331 short sErrorHasOccurred = pdFALSE;
\r
333 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
335 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
336 sErrorHasOccurred = pdTRUE;
\r
339 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
341 vDisplayMessage( "Integer maths task count unchanged!\r\n" );
\r
342 sErrorHasOccurred = pdTRUE;
\r
345 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
347 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
348 sErrorHasOccurred = pdTRUE;
\r
351 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
353 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
354 sErrorHasOccurred = pdTRUE;
\r
357 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
359 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
360 sErrorHasOccurred = pdTRUE;
\r
363 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
365 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
366 sErrorHasOccurred = pdTRUE;
\r
369 if( sErrorHasOccurred == pdFALSE )
\r
371 vDisplayMessage( "OK " );
\r
372 /* Toggle the LED if everything is okay so we know if an error occurs even if not
\r
373 using console IO. */
\r
380 /* An error has occurred in one of the tasks. Don't go any further and
\r
381 flash the LED rapidly in case console IO is not being used. */
\r
383 vTaskDelay( mainERROR_FLASH_RATE );
\r
387 /*-----------------------------------------------------------*/
\r
389 static void prvInitLED( void )
\r
391 unsigned short usPortDirection;
\r
392 const unsigned short usLEDOut = 0x400;
\r
394 /* Set the LED bit to an output. */
\r
396 usPortDirection = inpw( mainLED_REG_DIR );
\r
397 usPortDirection &= ~usLEDOut;
\r
398 outpw( mainLED_REG_DIR, usPortDirection );
\r
400 /*-----------------------------------------------------------*/
\r
402 static void prvToggleLED( void )
\r
404 static short sLED = pdTRUE;
\r
405 unsigned short usLEDState;
\r
406 const unsigned short usLEDBit = 0x400;
\r
408 /* Flip the state of the LED. */
\r
409 usLEDState = inpw( mainLED_REG );
\r
412 usLEDState &= ~usLEDBit;
\r
416 usLEDState |= usLEDBit;
\r
418 outpw( mainLED_REG, usLEDState );
\r