2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * Creates all the demo application tasks, then starts the scheduler.
\r
31 * Main. c also creates a task called "Print". This only executes every five
\r
32 * seconds but has the highest priority so is guaranteed to get processor time.
\r
33 * Its main function is to check that all the other tasks are still operational.
\r
34 * Nearly all the tasks in the demo application maintain a unique count that is
\r
35 * incremented each time the task successfully completes its function. Should any
\r
36 * error occur within the task the count is permanently halted. The print task
\r
37 * checks the count of each task to ensure it has changed since the last time the
\r
38 * print task executed. If any count is found not to have changed the print task
\r
39 * displays an appropriate message, halts, and flashes the on board LED rapidly.
\r
40 * If all the tasks are still incrementing their unique counts the print task
\r
41 * displays an "OK" message.
\r
43 * The LED flash tasks do not maintain a count as they already provide visual
\r
44 * feedback of their status.
\r
46 * The print task blocks on the queue into which messages that require displaying
\r
47 * are posted. It will therefore only block for the full 5 seconds if no messages
\r
48 * are posted onto the queue.
\r
50 * Main. c also provides a demonstration of how the trace visualisation utility can
\r
51 * be used, and how the scheduler can be stopped.
\r
53 * On the Flashlite it is preferable not to try to write to the console during
\r
54 * real time operation. The built in LED is toggled every cycle of the print task
\r
55 * that does not encounter any errors, so the console IO may be removed if required.
\r
56 * The build in LED will start flashing rapidly if any task reports an error.
\r
62 + Previously, if an error occurred in a task the on board LED was stopped from
\r
63 toggling. Now if an error occurs the check task enters an infinite loop,
\r
64 toggling the LED rapidly.
\r
68 + The integer and comtest tasks are now used when the cooperative scheduler
\r
69 is being used. Previously they were only used with the preemptive
\r
74 + Made the communications RX task a higher priority.
\r
78 + Delay periods are now specified using variables and constants of
\r
79 TickType_t rather than unsigned long.
\r
84 #include "FreeRTOS.h"
\r
86 #include "partest.h"
\r
89 /* Demo file headers. */
\r
94 #include "integer.h"
\r
96 #include "comtest.h"
\r
98 #include "semtest.h"
\r
100 /* Priority definitions for all the tasks in the demo application. */
\r
101 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
102 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
103 #define mainPRINT_TASK_PRIORITY ( tskIDLE_PRIORITY + 5 )
\r
104 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
105 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
106 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
107 #define mainSEMAPHORE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
109 #define mainPRINT_STACK_SIZE ( ( unsigned short ) 256 )
\r
110 #define mainDEBUG_LOG_BUFFER_SIZE ( ( unsigned short ) 20480 )
\r
112 /* Constant definitions for accessing the build in LED on the Flashlite 186. */
\r
113 #define mainLED_REG_DIR ( ( unsigned short ) 0xff78 )
\r
114 #define mainLED_REG ( ( unsigned short ) 0xff7a )
\r
116 /* If an error is detected in a task then the vErrorChecks() task will enter
\r
117 an infinite loop flashing the LED at this rate. */
\r
118 #define mainERROR_FLASH_RATE ( ( TickType_t ) 100 / portTICK_PERIOD_MS )
\r
120 /* Task function for the "Print" task as described at the top of the file. */
\r
121 static void vErrorChecks( void *pvParameters );
\r
123 /* Function that checks the unique count of all the other tasks as described at
\r
124 the top of the file. */
\r
125 static void prvCheckOtherTasksAreStillRunning( void );
\r
127 /* Functions to setup and use the built in LED on the Flashlite 186 board. */
\r
128 static void prvToggleLED( void );
\r
129 static void prvInitLED( void );
\r
131 /* Key presses can be used to start/stop the trace visualisation utility or stop
\r
133 static void prvCheckForKeyPresses( void );
\r
135 /* Buffer used by the trace visualisation utility. */
\r
136 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];
\r
138 /*-----------------------------------------------------------*/
\r
141 /* Initialise hardware and utilities. */
\r
142 vParTestInitialise();
\r
143 vPrintInitialise();
\r
146 /* CREATE ALL THE DEMO APPLICATION TASKS. */
\r
148 vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );
\r
149 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
150 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
151 vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );
\r
152 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
153 vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );
\r
155 /* Create the "Print" task as described at the top of the file. */
\r
156 xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );
\r
158 /* This task has to be created last as it keeps account of the number of tasks
\r
159 it expects to see running. */
\r
160 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
162 /* Set the scheduler running. This function will not return unless a task
\r
163 calls vTaskEndScheduler(). */
\r
164 vTaskStartScheduler();
\r
168 /*-----------------------------------------------------------*/
\r
170 static void vErrorChecks( void *pvParameters )
\r
172 TickType_t xExpectedWakeTime;
\r
173 const TickType_t xPrintRate = ( TickType_t ) 5000 / portTICK_PERIOD_MS;
\r
174 const long lMaxAllowableTimeDifference = ( long ) 0;
\r
175 TickType_t xWakeTime;
\r
176 long lTimeDifference;
\r
177 const char *pcReceivedMessage;
\r
178 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";
\r
180 /* Stop warnings. */
\r
181 ( void ) pvParameters;
\r
183 /* Loop continuously, blocking, then checking all the other tasks are still
\r
184 running, before blocking once again. This task blocks on the queue of messages
\r
185 that require displaying so will wake either by its time out expiring, or a
\r
186 message becoming available. */
\r
189 /* Calculate the time we will unblock if no messages are received
\r
190 on the queue. This is used to check that we have not blocked for too long. */
\r
191 xExpectedWakeTime = xTaskGetTickCount();
\r
192 xExpectedWakeTime += xPrintRate;
\r
194 /* Block waiting for either a time out or a message to be posted that
\r
195 required displaying. */
\r
196 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );
\r
198 /* Was a message received? */
\r
199 if( pcReceivedMessage == NULL )
\r
201 /* A message was not received so we timed out, did we unblock at the
\r
203 xWakeTime = xTaskGetTickCount();
\r
205 /* Calculate the difference between the time we unblocked and the
\r
206 time we should have unblocked. */
\r
207 if( xWakeTime > xExpectedWakeTime )
\r
209 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );
\r
213 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );
\r
216 if( lTimeDifference > lMaxAllowableTimeDifference )
\r
218 /* We blocked too long - create a message that will get
\r
219 printed out the next time around. */
\r
220 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );
\r
223 /* Check the other tasks are still running, just in case. */
\r
224 prvCheckOtherTasksAreStillRunning();
\r
228 /* We unblocked due to a message becoming available. Send the message
\r
230 vDisplayMessage( pcReceivedMessage );
\r
233 /* Key presses are used to invoke the trace visualisation utility, or
\r
234 end the program. */
\r
235 prvCheckForKeyPresses();
\r
237 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */
\r
238 /*-----------------------------------------------------------*/
\r
240 static void prvCheckForKeyPresses( void )
\r
247 taskENTER_CRITICAL();
\r
249 taskEXIT_CRITICAL();
\r
253 unsigned long ulBufferLength;
\r
255 /* Key presses can be used to start/stop the trace utility, or end the
\r
260 /* Only define keys for turning on and off the trace if the trace
\r
262 #if configUSE_TRACE_FACILITY == 1
\r
263 case 't' : vTaskList( pcWriteBuffer );
\r
264 vWriteMessageToDisk( pcWriteBuffer );
\r
267 /* The legacy trace is no longer supported. Use FreeRTOS+Trace instead.
\r
268 case 's' : vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );
\r
271 case 'e' : ulBufferLength = ulTaskEndTrace();
\r
272 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );
\r
276 default : vTaskEndScheduler();
\r
282 ( void ) pcWriteBuffer;
\r
285 /*-----------------------------------------------------------*/
\r
287 static void prvCheckOtherTasksAreStillRunning( void )
\r
289 short sErrorHasOccurred = pdFALSE;
\r
291 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
293 vDisplayMessage( "Com test count unchanged!\r\n" );
\r
294 sErrorHasOccurred = pdTRUE;
\r
297 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
299 vDisplayMessage( "Integer maths task count unchanged!\r\n" );
\r
300 sErrorHasOccurred = pdTRUE;
\r
303 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
305 vDisplayMessage( "Blocking queues count unchanged!\r\n" );
\r
306 sErrorHasOccurred = pdTRUE;
\r
309 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
311 vDisplayMessage( "Polling queue count unchanged!\r\n" );
\r
312 sErrorHasOccurred = pdTRUE;
\r
315 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
317 vDisplayMessage( "Incorrect number of tasks running!\r\n" );
\r
318 sErrorHasOccurred = pdTRUE;
\r
321 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
323 vDisplayMessage( "Semaphore take count unchanged!\r\n" );
\r
324 sErrorHasOccurred = pdTRUE;
\r
327 if( sErrorHasOccurred == pdFALSE )
\r
329 vDisplayMessage( "OK " );
\r
330 /* Toggle the LED if everything is okay so we know if an error occurs even if not
\r
331 using console IO. */
\r
338 /* An error has occurred in one of the tasks. Don't go any further and
\r
339 flash the LED rapidly in case console IO is not being used. */
\r
341 vTaskDelay( mainERROR_FLASH_RATE );
\r
345 /*-----------------------------------------------------------*/
\r
347 static void prvInitLED( void )
\r
349 unsigned short usPortDirection;
\r
350 const unsigned short usLEDOut = 0x400;
\r
352 /* Set the LED bit to an output. */
\r
354 usPortDirection = inpw( mainLED_REG_DIR );
\r
355 usPortDirection &= ~usLEDOut;
\r
356 outpw( mainLED_REG_DIR, usPortDirection );
\r
358 /*-----------------------------------------------------------*/
\r
360 static void prvToggleLED( void )
\r
362 static short sLED = pdTRUE;
\r
363 unsigned short usLEDState;
\r
364 const unsigned short usLEDBit = 0x400;
\r
366 /* Flip the state of the LED. */
\r
367 usLEDState = inpw( mainLED_REG );
\r
370 usLEDState &= ~usLEDBit;
\r
374 usLEDState |= usLEDBit;
\r
376 outpw( mainLED_REG, usLEDState );
\r