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