2 * FreeRTOS Kernel V10.1.1
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
29 * Creates all the demo application tasks, then starts the scheduler. The WEB
30 * documentation provides more details of the demo application tasks.
32 * This demo is configured to execute on the ES449 prototyping board from
33 * SoftBaugh. The ES449 has a built in LCD display and a single built in user
34 * LED. Therefore, in place of flashing an LED, the 'flash' and 'check' tasks
35 * toggle '*' characters on the LCD. The left most '*' represents LED 0, the
38 * Main. c also creates a task called 'Check'. This only executes every three
39 * seconds but has the highest priority so is guaranteed to get processor time.
40 * Its main function is to check that all the other tasks are still operational.
41 * Each task that does not flash an LED maintains a unique count that is
42 * incremented each time the task successfully completes its function. Should
43 * any error occur within such a task the count is permanently halted. The
44 * 'check' task inspects the count of each task to ensure it has changed since
45 * the last time the check task executed. If all the count variables have
46 * changed all the tasks are still executing error free, and the check task
47 * toggles an LED with a three second period. Should any task contain an error
48 * at any time the LED toggle rate will increase to 500ms.
50 * Please read the documentation for the MSP430 port available on
51 * http://www.FreeRTOS.org.
54 /* Standard includes. */
57 /* Scheduler includes. */
61 /* Demo application includes. */
68 /* Constants required for hardware setup. */
69 #define mainALL_BITS_OUTPUT ( ( unsigned char ) 0xff )
70 #define mainMAX_FREQUENCY ( ( unsigned char ) 121 )
72 /* Constants that define the LED's used by the various tasks. [in this case
73 the '*' characters on the LCD represent LED's] */
74 #define mainCHECK_LED ( 4 )
75 #define mainCOM_TEST_LED ( 10 )
77 /* Demo task priorities. */
78 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
79 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
80 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
81 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
83 /* Baud rate used by the COM test tasks. */
84 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 19200 )
86 /* The frequency at which the 'Check' tasks executes. See the comments at the
87 top of the page. When the system is operating error free the 'Check' task
88 toggles an LED every three seconds. If an error is discovered in any task the
89 rate is increased to 500 milliseconds. [in this case the '*' characters on the
90 LCD represent LED's]*/
91 #define mainNO_ERROR_CHECK_DELAY ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
92 #define mainERROR_CHECK_DELAY ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
94 /* The constants used in the calculation. */
95 #define intgCONST1 ( ( long ) 123 )
96 #define intgCONST2 ( ( long ) 234567 )
97 #define intgCONST3 ( ( long ) -3 )
98 #define intgCONST4 ( ( long ) 7 )
99 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
102 * The function that implements the Check task. See the comments at the head
103 * of the page for implementation details.
105 static void vErrorChecks( void *pvParameters );
108 * Called by the Check task. Returns pdPASS if all the other tasks are found
109 * to be operating without error - otherwise returns pdFAIL.
111 static short prvCheckOtherTasksAreStillRunning( void );
114 * Perform the hardware setup required by the ES449 in order to run the demo
117 static void prvSetupHardware( void );
120 portBASE_TYPE xLocalError = pdFALSE;
121 volatile unsigned long ulIdleLoops = 0UL;
123 /*-----------------------------------------------------------*/
126 * Start the demo application tasks - then start the real time scheduler.
130 /* Setup the hardware ready for the demo. */
132 vParTestInitialise();
134 /* Start the standard demo application tasks. */
135 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
136 vStartIntegerMathTasks( tskIDLE_PRIORITY );
137 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );
138 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
140 /* Start the 'Check' task which is defined in this file. */
141 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
143 /* Start the scheduler. */
144 vTaskStartScheduler();
146 /* As the scheduler has been started the demo applications tasks will be
147 executing and we should never get here! */
150 /*-----------------------------------------------------------*/
152 static portTASK_FUNCTION( vErrorChecks, pvParameters )
154 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_DELAY;
156 /* Cycle for ever, delaying then checking all the other tasks are still
157 operating without error. */
160 /* Wait until it is time to check again. The time we wait here depends
161 on whether an error has been detected or not. When an error is
162 detected the time is shortened resulting in a faster LED flash rate. */
163 vTaskDelay( xDelayPeriod );
165 /* See if the other tasks are all ok. */
166 if( prvCheckOtherTasksAreStillRunning() != pdPASS )
168 /* An error occurred in one of the tasks so shorten the delay
169 period - which has the effect of increasing the frequency of the
171 xDelayPeriod = mainERROR_CHECK_DELAY;
175 vParTestToggleLED( mainCHECK_LED );
178 /*-----------------------------------------------------------*/
180 static short prvCheckOtherTasksAreStillRunning( void )
182 static short sNoErrorFound = pdTRUE;
183 static unsigned long ulLastIdleLoopCount = 0UL;
185 /* The demo tasks maintain a count that increments every cycle of the task
186 provided that the task has never encountered an error. This function
187 checks the counts maintained by the tasks to ensure they are still being
188 incremented. A count remaining at the same value between calls therefore
189 indicates that an error has been detected. Only tasks that do not flash
190 an LED are checked. */
192 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
194 sNoErrorFound = pdFALSE;
197 if( xAreComTestTasksStillRunning() != pdTRUE )
199 sNoErrorFound = pdFALSE;
202 if( xArePollingQueuesStillRunning() != pdTRUE )
204 sNoErrorFound = pdFALSE;
207 if( xLocalError == pdTRUE )
209 sNoErrorFound = pdFALSE;
212 if( ulIdleLoops == ulLastIdleLoopCount )
214 sNoErrorFound = pdFALSE;
218 ulLastIdleLoopCount = ulIdleLoops;
221 return sNoErrorFound;
223 /*-----------------------------------------------------------*/
225 static void prvSetupHardware( void )
227 /* Stop the watchdog. */
228 WDTCTL = WDTPW + WDTHOLD;
230 /* Setup DCO+ for ( xtal * D * (N + 1) ) operation. */
231 FLL_CTL0 |= DCOPLUS + XCAP18PF;
233 /* X2 DCO frequency, 8MHz nominal DCO */
236 /* (121+1) x 32768 x 2 = 7.99 Mhz */
237 SCFQCTL = mainMAX_FREQUENCY;
239 /* Setup the IO. This is just copied from the demo supplied by SoftBaugh
240 for the ES449 demo board. */
247 /*-----------------------------------------------------------*/
249 /* The idle hook is just a copy of the standard integer maths tasks. See
250 Demo/Common/integer.c for rationale. */
252 void vApplicationIdleHook( void )
254 /* These variables are all effectively set to constants so they are volatile to
255 ensure the compiler does not just get rid of them. */
256 volatile long lValue;
258 /* Keep performing a calculation and checking the result against a constant. */
261 /* Perform the calculation. This will store partial value in
262 registers, resulting in a good test of the context switch mechanism. */
264 lValue += intgCONST2;
266 /* Yield in case cooperative scheduling is being used. */
267 #if configUSE_PREEMPTION == 0
273 /* Finish off the calculation. */
274 lValue *= intgCONST3;
275 lValue /= intgCONST4;
277 /* If the calculation is found to be incorrect we stop setting the
278 TaskHasExecuted variable so the check task can see an error has
280 if( lValue != intgEXPECTED_ANSWER ) /*lint !e774 volatile used to prevent this being optimised out. */
282 /* Don't bother with mutual exclusion - it is only read from the
283 check task and never written. */
284 xLocalError = pdTRUE;
286 /* Yield in case cooperative scheduling is being used. */
287 #if configUSE_PREEMPTION == 0
295 /* Place the processor into low power mode. */