2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * Creates all the demo application tasks, then starts the scheduler.
\r
32 * Main. c also creates a task called "Print". This only executes every five
\r
33 * seconds but has the highest priority so is guaranteed to get processor time.
\r
34 * Its main function is to check that all the other tasks are still operational.
\r
35 * Nearly all the tasks in the demo application maintain a unique count that is
\r
36 * incremented each time the task successfully completes its function. Should any
\r
37 * error occur within the task the count is permanently halted. The print task
\r
38 * checks the count of each task to ensure it has changed since the last time the
\r
39 * print task executed. If any count is found not to have changed the print task
\r
40 * displays an appropriate message, halts, and flashes the on board LED rapidly.
\r
41 * If all the tasks are still incrementing their unique counts the print task
\r
42 * displays an "OK" message.
\r
44 * The LED flash tasks do not maintain a count as they already provide visual
\r
45 * feedback of their status.
\r
47 * The print task blocks on the queue into which messages that require displaying
\r
48 * are posted. It will therefore only block for the full 5 seconds if no messages
\r
49 * are posted onto the queue.
\r
51 * Main. c also provides a demonstration of how the trace visualisation utility can
\r
52 * be used, and how the scheduler can be stopped.
\r
54 * On the Flashlite it is preferable not to try to write to the console during
\r
55 * real time operation. The built in LED is toggled every cycle of the print task
\r
56 * that does not encounter any errors, so the console IO may be removed if required.
\r
57 * The build in LED will start flashing rapidly if any task reports an error.
\r
63 + Previously, if an error occurred in a task the on board LED was stopped from
\r
64 toggling. Now if an error occurs the check task enters an infinite loop,
\r
65 toggling the LED rapidly.
\r
69 + The integer and comtest tasks are now used when the cooperative scheduler
\r
70 is being used. Previously they were only used with the preemptive
\r
75 + Made the communications RX task a higher priority.
\r
79 + Delay periods are now specified using variables and constants of
\r
80 TickType_t rather than unsigned long.
\r
85 #include "FreeRTOS.h"
\r
87 #include "partest.h"
\r
90 /* Demo file headers. */
\r
95 #include "integer.h"
\r
97 #include "comtest.h"
\r
99 #include "semtest.h"
\r
101 /* Priority definitions for all the tasks in the demo application. */
\r
102 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
103 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
104 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 5 )
\r
105 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
106 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
107 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
108 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
110 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 256 )
\r
111 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\r
113 /* Constant definitions for accessing the build in LED on the Flashlite 186. */
\r
114 #define mainLED_REG_DIR ( ( unsigned short ) 0xff78 )
\r
115 #define mainLED_REG ( ( unsigned short ) 0xff7a )
\r
117 /* If an error is detected in a task then the vErrorChecks() task will enter
\r
118 an infinite loop flashing the LED at this rate. */
\r
119 #define mainERROR_FLASH_RATE ( ( TickType_t ) 100 / portTICK_PERIOD_MS )
\r
121 /* Task function for the "Print" task as described at the top of the file. */
\r
122 static void vErrorChecks( void *pvParameters );
\r
124 /* Function that checks the unique count of all the other tasks as described at
\r
125 the top of the file. */
\r
126 static void prvCheckOtherTasksAreStillRunning( void );
\r
128 /* Functions to setup and use the built in LED on the Flashlite 186 board. */
\r
129 static void prvToggleLED( void );
\r
130 static void prvInitLED( void );
\r
132 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
134 static void prvCheckForKeyPresses( void );
\r
136 /* Buffer used by the trace visualisation utility. */
\r
137 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
139 /*-----------------------------------------------------------*/
\r
142 /* Initialise hardware and utilities. */
\r
143 vParTestInitialise();
\r
144 vPrintInitialise();
\r
147 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
149 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );
\r
150 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
151 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
152 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
153 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
154 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
156 /* Create the "Print" task as described at the top of the file. */
\r
157 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
159 /* This task has to be created last as it keeps account of the number of tasks
\r
160 it expects to see running. */
\r
161 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
163 /* Set the scheduler running. This function will not return unless a task
\r
164 calls vTaskEndScheduler(). */
\r
165 vTaskStartScheduler();
\r
169 /*-----------------------------------------------------------*/
\r
171 static void vErrorChecks( void *pvParameters )
\r
173 TickType_t xExpectedWakeTime;
\r
174 const TickType_t xPrintRate = ( TickType_t ) 5000 / portTICK_PERIOD_MS;
\r
175 const long lMaxAllowableTimeDifference = ( long ) 0;
\r
176 TickType_t xWakeTime;
\r
177 long lTimeDifference;
\r
178 const char *pcReceivedMessage;
\r
179 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
181 /* Stop warnings. */
\r
182 ( void ) pvParameters;
\r
184 /* Loop continuously, blocking, then checking all the other tasks are still
\r
185 running, before blocking once again. This task blocks on the queue of messages
\r
186 that require displaying so will wake either by its time out expiring, or a
\r
187 message becoming available. */
\r
190 /* Calculate the time we will unblock if no messages are received
\r
191 on the queue. This is used to check that we have not blocked for too long. */
\r
192 xExpectedWakeTime = xTaskGetTickCount();
\r
193 xExpectedWakeTime += xPrintRate;
\r
195 /* Block waiting for either a time out or a message to be posted that
\r
196 required displaying. */
\r
197 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
199 /* Was a message received? */
\r
200 if( pcReceivedMessage == NULL )
\r
202 /* A message was not received so we timed out, did we unblock at the
\r
204 xWakeTime = xTaskGetTickCount();
\r
206 /* Calculate the difference between the time we unblocked and the
\r
207 time we should have unblocked. */
\r
208 if( xWakeTime > xExpectedWakeTime )
\r
210 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
214 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
217 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
219 /* We blocked too long - create a message that will get
\r
220 printed out the next time around. */
\r
221 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
224 /* Check the other tasks are still running, just in case. */
\r
225 prvCheckOtherTasksAreStillRunning();
\r
229 /* We unblocked due to a message becoming available. Send the message
\r
231 vDisplayMessage( pcReceivedMessage );
\r
234 /* Key presses are used to invoke the trace visualisation utility, or
\r
235 end the program. */
\r
236 prvCheckForKeyPresses();
\r
238 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */
\r
239 /*-----------------------------------------------------------*/
\r
241 static void prvCheckForKeyPresses( void )
\r
248 taskENTER_CRITICAL();
\r
250 taskEXIT_CRITICAL();
\r
254 unsigned long ulBufferLength;
\r
256 /* Key presses can be used to start/stop the trace utility, or end the
\r
261 /* Only define keys for turning on and off the trace if the trace
\r
263 #if configUSE_TRACE_FACILITY == 1
\r
264 case 't' : vTaskList( pcWriteBuffer );
\r
265 vWriteMessageToDisk( pcWriteBuffer );
\r
268 /* The legacy trace is no longer supported. Use FreeRTOS+Trace instead.
\r
269 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
272 case 'e' : ulBufferLength = ulTaskEndTrace();
\r
273 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
277 default : vTaskEndScheduler();
\r
283 ( void ) pcWriteBuffer;
\r
286 /*-----------------------------------------------------------*/
\r
288 static void prvCheckOtherTasksAreStillRunning( void )
\r
290 short sErrorHasOccurred = pdFALSE;
\r
292 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
294 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
295 sErrorHasOccurred = pdTRUE;
\r
298 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
300 vDisplayMessage( "Integer maths task count unchanged!\r\n" );
\r
301 sErrorHasOccurred = pdTRUE;
\r
304 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
306 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
307 sErrorHasOccurred = pdTRUE;
\r
310 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
312 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
313 sErrorHasOccurred = pdTRUE;
\r
316 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
318 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
319 sErrorHasOccurred = pdTRUE;
\r
322 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
324 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
325 sErrorHasOccurred = pdTRUE;
\r
328 if( sErrorHasOccurred == pdFALSE )
\r
330 vDisplayMessage( "OK " );
\r
331 /* Toggle the LED if everything is okay so we know if an error occurs even if not
\r
332 using console IO. */
\r
339 /* An error has occurred in one of the tasks. Don't go any further and
\r
340 flash the LED rapidly in case console IO is not being used. */
\r
342 vTaskDelay( mainERROR_FLASH_RATE );
\r
346 /*-----------------------------------------------------------*/
\r
348 static void prvInitLED( void )
\r
350 unsigned short usPortDirection;
\r
351 const unsigned short usLEDOut = 0x400;
\r
353 /* Set the LED bit to an output. */
\r
355 usPortDirection = inpw( mainLED_REG_DIR );
\r
356 usPortDirection &= ~usLEDOut;
\r
357 outpw( mainLED_REG_DIR, usPortDirection );
\r
359 /*-----------------------------------------------------------*/
\r
361 static void prvToggleLED( void )
\r
363 static short sLED = pdTRUE;
\r
364 unsigned short usLEDState;
\r
365 const unsigned short usLEDBit = 0x400;
\r
367 /* Flip the state of the LED. */
\r
368 usLEDState = inpw( mainLED_REG );
\r
371 usLEDState &= ~usLEDBit;
\r
375 usLEDState |= usLEDBit;
\r
377 outpw( mainLED_REG, usLEDState );
\r