2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
97 * Creates all the demo application tasks, then starts the scheduler.
\r
99 * Main. c also creates a task called "Print". This only executes every five
\r
100 * seconds but has the highest priority so is guaranteed to get processor time.
\r
101 * Its main function is to check that all the other tasks are still operational.
\r
102 * Nearly all the tasks in the demo application maintain a unique count that is
\r
103 * incremented each time the task successfully completes its function. Should any
\r
104 * error occur within the task the count is permanently halted. The print task
\r
105 * checks the count of each task to ensure it has changed since the last time the
\r
106 * print task executed. If any count is found not to have changed the print task
\r
107 * displays an appropriate message, halts, and flashes the on board LED rapidly.
\r
108 * If all the tasks are still incrementing their unique counts the print task
\r
109 * displays an "OK" message.
\r
111 * The LED flash tasks do not maintain a count as they already provide visual
\r
112 * feedback of their status.
\r
114 * The print task blocks on the queue into which messages that require displaying
\r
115 * are posted. It will therefore only block for the full 5 seconds if no messages
\r
116 * are posted onto the queue.
\r
118 * Main. c also provides a demonstration of how the trace visualisation utility can
\r
119 * be used, and how the scheduler can be stopped.
\r
121 * On the Flashlite it is preferable not to try to write to the console during
\r
122 * real time operation. The built in LED is toggled every cycle of the print task
\r
123 * that does not encounter any errors, so the console IO may be removed if required.
\r
124 * The build in LED will start flashing rapidly if any task reports an error.
\r
128 Changes from V1.01:
\r
130 + Previously, if an error occurred in a task the on board LED was stopped from
\r
131 toggling. Now if an error occurs the check task enters an infinite loop,
\r
132 toggling the LED rapidly.
\r
134 Changes from V1.2.3
\r
136 + The integer and comtest tasks are now used when the cooperative scheduler
\r
137 is being used. Previously they were only used with the preemptive
\r
140 Changes from V1.2.5
\r
142 + Made the communications RX task a higher priority.
\r
144 Changes from V2.0.0
\r
146 + Delay periods are now specified using variables and constants of
\r
147 TickType_t rather than unsigned long.
\r
150 #include <stdlib.h>
\r
152 #include "FreeRTOS.h"
\r
154 #include "partest.h"
\r
155 #include "serial.h"
\r
157 /* Demo file headers. */
\r
158 #include "BlockQ.h"
\r
162 #include "integer.h"
\r
164 #include "comtest.h"
\r
165 #include "fileio.h"
\r
166 #include "semtest.h"
\r
168 /* Priority definitions for all the tasks in the demo application. */
\r
169 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
170 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
171 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 5 )
\r
172 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
173 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
174 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
175 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
177 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 256 )
\r
178 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\r
180 /* Constant definitions for accessing the build in LED on the Flashlite 186. */
\r
181 #define mainLED_REG_DIR ( ( unsigned short ) 0xff78 )
\r
182 #define mainLED_REG ( ( unsigned short ) 0xff7a )
\r
184 /* If an error is detected in a task then the vErrorChecks() task will enter
\r
185 an infinite loop flashing the LED at this rate. */
\r
186 #define mainERROR_FLASH_RATE ( ( TickType_t ) 100 / portTICK_PERIOD_MS )
\r
188 /* Task function for the "Print" task as described at the top of the file. */
\r
189 static void vErrorChecks( void *pvParameters );
\r
191 /* Function that checks the unique count of all the other tasks as described at
\r
192 the top of the file. */
\r
193 static void prvCheckOtherTasksAreStillRunning( void );
\r
195 /* Functions to setup and use the built in LED on the Flashlite 186 board. */
\r
196 static void prvToggleLED( void );
\r
197 static void prvInitLED( void );
\r
199 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
201 static void prvCheckForKeyPresses( void );
\r
203 /* Buffer used by the trace visualisation utility. */
\r
204 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
206 /*-----------------------------------------------------------*/
\r
209 /* Initialise hardware and utilities. */
\r
210 vParTestInitialise();
\r
211 vPrintInitialise();
\r
214 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
216 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );
\r
217 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
218 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
219 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
220 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
221 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
223 /* Create the "Print" task as described at the top of the file. */
\r
224 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
226 /* This task has to be created last as it keeps account of the number of tasks
\r
227 it expects to see running. */
\r
228 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
230 /* Set the scheduler running. This function will not return unless a task
\r
231 calls vTaskEndScheduler(). */
\r
232 vTaskStartScheduler();
\r
236 /*-----------------------------------------------------------*/
\r
238 static void vErrorChecks( void *pvParameters )
\r
240 TickType_t xExpectedWakeTime;
\r
241 const TickType_t xPrintRate = ( TickType_t ) 5000 / portTICK_PERIOD_MS;
\r
242 const long lMaxAllowableTimeDifference = ( long ) 0;
\r
243 TickType_t xWakeTime;
\r
244 long lTimeDifference;
\r
245 const char *pcReceivedMessage;
\r
246 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
248 /* Stop warnings. */
\r
249 ( void ) pvParameters;
\r
251 /* Loop continuously, blocking, then checking all the other tasks are still
\r
252 running, before blocking once again. This task blocks on the queue of messages
\r
253 that require displaying so will wake either by its time out expiring, or a
\r
254 message becoming available. */
\r
257 /* Calculate the time we will unblock if no messages are received
\r
258 on the queue. This is used to check that we have not blocked for too long. */
\r
259 xExpectedWakeTime = xTaskGetTickCount();
\r
260 xExpectedWakeTime += xPrintRate;
\r
262 /* Block waiting for either a time out or a message to be posted that
\r
263 required displaying. */
\r
264 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
266 /* Was a message received? */
\r
267 if( pcReceivedMessage == NULL )
\r
269 /* A message was not received so we timed out, did we unblock at the
\r
271 xWakeTime = xTaskGetTickCount();
\r
273 /* Calculate the difference between the time we unblocked and the
\r
274 time we should have unblocked. */
\r
275 if( xWakeTime > xExpectedWakeTime )
\r
277 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
281 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
284 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
286 /* We blocked too long - create a message that will get
\r
287 printed out the next time around. */
\r
288 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
291 /* Check the other tasks are still running, just in case. */
\r
292 prvCheckOtherTasksAreStillRunning();
\r
296 /* We unblocked due to a message becoming available. Send the message
\r
298 vDisplayMessage( pcReceivedMessage );
\r
301 /* Key presses are used to invoke the trace visualisation utility, or
\r
302 end the program. */
\r
303 prvCheckForKeyPresses();
\r
305 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */
\r
306 /*-----------------------------------------------------------*/
\r
308 static void prvCheckForKeyPresses( void )
\r
315 taskENTER_CRITICAL();
\r
317 taskEXIT_CRITICAL();
\r
321 unsigned long ulBufferLength;
\r
323 /* Key presses can be used to start/stop the trace utility, or end the
\r
328 /* Only define keys for turning on and off the trace if the trace
\r
330 #if configUSE_TRACE_FACILITY == 1
\r
331 case 't' : vTaskList( pcWriteBuffer );
\r
332 vWriteMessageToDisk( pcWriteBuffer );
\r
335 /* The legacy trace is no longer supported. Use FreeRTOS+Trace instead.
\r
336 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
339 case 'e' : ulBufferLength = ulTaskEndTrace();
\r
340 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
344 default : vTaskEndScheduler();
\r
350 ( void ) pcWriteBuffer;
\r
353 /*-----------------------------------------------------------*/
\r
355 static void prvCheckOtherTasksAreStillRunning( void )
\r
357 short sErrorHasOccurred = pdFALSE;
\r
359 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
361 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
362 sErrorHasOccurred = pdTRUE;
\r
365 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
367 vDisplayMessage( "Integer maths task count unchanged!\r\n" );
\r
368 sErrorHasOccurred = pdTRUE;
\r
371 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
373 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
374 sErrorHasOccurred = pdTRUE;
\r
377 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
379 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
380 sErrorHasOccurred = pdTRUE;
\r
383 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
385 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
386 sErrorHasOccurred = pdTRUE;
\r
389 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
391 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
392 sErrorHasOccurred = pdTRUE;
\r
395 if( sErrorHasOccurred == pdFALSE )
\r
397 vDisplayMessage( "OK " );
\r
398 /* Toggle the LED if everything is okay so we know if an error occurs even if not
\r
399 using console IO. */
\r
406 /* An error has occurred in one of the tasks. Don't go any further and
\r
407 flash the LED rapidly in case console IO is not being used. */
\r
409 vTaskDelay( mainERROR_FLASH_RATE );
\r
413 /*-----------------------------------------------------------*/
\r
415 static void prvInitLED( void )
\r
417 unsigned short usPortDirection;
\r
418 const unsigned short usLEDOut = 0x400;
\r
420 /* Set the LED bit to an output. */
\r
422 usPortDirection = inpw( mainLED_REG_DIR );
\r
423 usPortDirection &= ~usLEDOut;
\r
424 outpw( mainLED_REG_DIR, usPortDirection );
\r
426 /*-----------------------------------------------------------*/
\r
428 static void prvToggleLED( void )
\r
430 static short sLED = pdTRUE;
\r
431 unsigned short usLEDState;
\r
432 const unsigned short usLEDBit = 0x400;
\r
434 /* Flip the state of the LED. */
\r
435 usLEDState = inpw( mainLED_REG );
\r
438 usLEDState &= ~usLEDBit;
\r
442 usLEDState |= usLEDBit;
\r
444 outpw( mainLED_REG, usLEDState );
\r