2 * FreeRTOS Kernel V10.0.1
3 * Copyright (C) 2017 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
31 * vMain() is effectively the demo application entry point. It is called by
32 * the main() function generated by the Processor Expert application.
34 * vMain() creates all the demo application tasks, then starts the scheduler.
35 * The WEB documentation provides more details of the demo application tasks.
37 * Main.c also creates a task called "Check". This only executes every three
38 * seconds but has the highest priority so is guaranteed to get processor time.
39 * Its main function is to check that all the other tasks are still operational.
40 * Each task (other than the "flash" tasks) maintains a unique count that is
41 * incremented each time the task successfully completes its function. Should
42 * any error occur within such a task the count is permanently halted. The
43 * check task inspects the count of each task to ensure it has changed since
44 * the last time the check task executed. If all the count variables have
45 * changed all the tasks are still executing error free, and the check task
46 * toggles the onboard LED. Should any task contain an error at any time
47 * the LED toggle rate will change from 3 seconds to 500ms.
49 * This file also includes the functionality normally implemented within the
50 * standard demo application file integer.c. Due to the limited memory
51 * available on the microcontroller the functionality has been included within
52 * the idle task hook [vApplicationIdleHook()] - instead of within the usual
53 * separate task. See the documentation within integer.c for the rationale
54 * of the integer task functionality.
58 * The demo applications included with other FreeRTOS ports make use of the
59 * standard ComTest tasks. These use a loopback connector to transmit and
60 * receive RS232 characters between two tasks. The test is important for two
63 * 1) It tests the mechanism of context switching from within an application
66 * 2) It generates some randomised timing.
68 * The demo board used to develop this port does not include an RS232 interface
69 * so the ComTest tasks could not easily be included. Instead these two tests
70 * are created using a 'Button Push' task.
72 * The 'Button Push' task blocks on a queue, waiting for data to arrive. A
73 * simple interrupt routine connected to the PP0 input on the demo board places
74 * data in the queue each time the PP0 button is pushed (this button is built
75 * onto the demo board). As the 'Button Push' task is created with a
76 * relatively high priority it will unblock and want to execute as soon as data
77 * arrives in the queue - resulting in a context switch within the PP0 input
78 * ISR. If the data retrieved from the queue is that expected the 'Button Push'
79 * task toggles LED 5. Therefore correct operation is indicated by the LED
80 * toggling each time the PP0 button is pressed.
82 * This test is not as satisfactory as the ComTest method - but the simple
83 * nature of the port makes is just about adequate.
87 /* Kernel includes. */
92 /* Demo application includes. */
98 /* Processor expert includes. */
99 #include "ButtonInterrupt.h"
101 /*-----------------------------------------------------------
103 -----------------------------------------------------------*/
105 /* Priorities assigned to demo application tasks. */
106 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
107 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
108 #define mainBUTTON_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
109 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
111 /* LED that is toggled by the check task. The check task periodically checks
112 that all the other tasks are operating without error. If no errors are found
113 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
114 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
115 #define mainCHECK_TASK_LED ( 7 )
116 #define mainCHECK_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
117 #define mainERROR_CHECK_PERIOD ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
119 /* LED that is toggled by the button push interrupt. */
120 #define mainBUTTON_PUSH_LED ( 5 )
122 /* The constants used in the idle task calculation. */
123 #define intgCONST1 ( ( long ) 123 )
124 #define intgCONST2 ( ( long ) 234567 )
125 #define intgCONST3 ( ( long ) -3 )
126 #define intgCONST4 ( ( long ) 7 )
127 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
129 /* The length of the queue between is button push ISR and the Button Push task
130 is greater than 1 to account for switch bounces generating multiple inputs. */
131 #define mainBUTTON_QUEUE_SIZE 6
133 /*-----------------------------------------------------------
134 Local functions prototypes.
135 -----------------------------------------------------------*/
138 * The 'Check' task function. See the explanation at the top of the file.
140 static void vErrorChecks( void* pvParameters );
143 * The 'Button Push' task. See the explanation at the top of the file.
145 static void vButtonTask( void *pvParameters );
148 * The idle task hook - in which the integer task is implemented. See the
149 * explanation at the top of the file.
151 void vApplicationIdleHook( void );
154 * Checks the unique counts of other tasks to ensure they are still operational.
156 static long prvCheckOtherTasksAreStillRunning( void );
160 /*-----------------------------------------------------------
162 -----------------------------------------------------------*/
164 /* A few tasks are defined within this file. This flag is used to indicate
165 their status. If an error is detected in one of the locally defined tasks then
166 this flag is set to pdTRUE. */
167 portBASE_TYPE xLocalError = pdFALSE;
169 /* The queue used to send data from the button push ISR to the Button Push
171 static QueueHandle_t xButtonQueue;
174 /*-----------------------------------------------------------*/
177 * This is called from the main() function generated by the Processor Expert.
181 /* Start some of the standard demo tasks. */
182 vStartLEDFlashTasks( mainFLASH_PRIORITY );
183 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
184 vStartDynamicPriorityTasks();
186 /* Start the locally defined tasks. There is also a task implemented as
188 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
189 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_TASK_PRIORITY, NULL );
191 /* All the tasks have been created - start the scheduler. */
192 vTaskStartScheduler();
194 /* Should not reach here! */
197 /*-----------------------------------------------------------*/
199 static void vErrorChecks( void *pvParameters )
201 TickType_t xDelayPeriod = mainCHECK_PERIOD;
202 TickType_t xLastWakeTime;
204 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
205 functions correctly. */
206 xLastWakeTime = xTaskGetTickCount();
210 /* Delay until it is time to execute again. The delay period is
211 shorter following an error. */
212 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
214 /* Check all the demo application tasks are executing without
215 error. If an error is found the delay period is shortened - this
216 has the effect of increasing the flash rate of the 'check' task
218 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
220 /* An error has been detected in one of the tasks - flash faster. */
221 xDelayPeriod = mainERROR_CHECK_PERIOD;
224 /* Toggle the LED each cycle round. */
225 vParTestToggleLED( mainCHECK_TASK_LED );
228 /*-----------------------------------------------------------*/
230 static long prvCheckOtherTasksAreStillRunning( void )
232 portBASE_TYPE xAllTasksPassed = pdPASS;
234 if( xArePollingQueuesStillRunning() != pdTRUE )
236 xAllTasksPassed = pdFAIL;
239 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
241 xAllTasksPassed = pdFAIL;
244 /* Also check the status flag for the tasks defined within this function. */
245 if( xLocalError != pdFALSE )
247 xAllTasksPassed = pdFAIL;
250 return xAllTasksPassed;
252 /*-----------------------------------------------------------*/
254 void vApplicationIdleHook( void )
256 /* This variable is effectively set to a constant so it is made volatile to
257 ensure the compiler does not just get rid of it. */
258 volatile long lValue;
260 /* Keep performing a calculation and checking the result against a constant. */
263 /* Perform the calculation. This will store partial value in
264 registers, resulting in a good test of the context switch mechanism. */
266 lValue += intgCONST2;
267 lValue *= intgCONST3;
268 lValue /= intgCONST4;
270 /* Did we perform the calculation correctly with no corruption? */
271 if( lValue != intgEXPECTED_ANSWER )
274 portENTER_CRITICAL();
275 xLocalError = pdTRUE;
279 /* Yield in case cooperative scheduling is being used. */
280 #if configUSE_PREEMPTION == 0
287 /*-----------------------------------------------------------*/
289 static void vButtonTask( void *pvParameters )
291 unsigned portBASE_TYPE uxExpected = 1, uxReceived;
293 /* Create the queue used by the producer and consumer. */
294 xButtonQueue = xQueueCreate( mainBUTTON_QUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );
298 /* Now the queue is created it is safe to enable the button interrupt. */
299 ButtonInterrupt_Enable();
303 /* Simply wait for data to arrive from the button push interrupt. */
304 if( xQueueReceive( xButtonQueue, &uxReceived, portMAX_DELAY ) == pdPASS )
306 /* Was the data we received that expected? */
307 if( uxReceived != uxExpected )
310 portENTER_CRITICAL();
311 xLocalError = pdTRUE;
316 /* Toggle the LED for every successful push. */
317 vParTestToggleLED( mainBUTTON_PUSH_LED );
325 /* Will only get here if the queue could not be created. */
328 /*-----------------------------------------------------------*/
330 #pragma CODE_SEG __NEAR_SEG NON_BANKED
332 /* Button push ISR. */
333 void interrupt vButtonPush( void )
335 static unsigned portBASE_TYPE uxValToSend = 0;
336 static unsigned long xHigherPriorityTaskWoken;
338 xHigherPriorityTaskWoken = pdFALSE;
340 /* Send an incrementing value to the button push task each run. */
343 /* Clear the interrupt flag. */
346 /* Send the incremented value down the queue. The button push task is
347 blocked waiting for the data. As the button push task is high priority
348 it will wake and a context switch should be performed before leaving
350 xQueueSendFromISR( xButtonQueue, &uxValToSend, &xHigherPriorityTaskWoken );
352 if( xHigherPriorityTaskWoken )
354 /* NOTE: This macro can only be used if there are no local
355 variables defined. This function uses a static variable so it's
356 use is permitted. If the variable were not static portYIELD()
357 would have to be used in it's place. */
358 portTASK_SWITCH_FROM_ISR();
362 #pragma CODE_SEG DEFAULT