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
32 * vMain() is effectively the demo application entry point. It is called by
\r
33 * the main() function generated by the Processor Expert application.
\r
35 * vMain() creates all the demo application tasks, then starts the scheduler.
\r
36 * The WEB documentation provides more details of the demo application tasks.
\r
38 * Main.c also creates a task called "Check". This only executes every three
\r
39 * seconds but has the highest priority so is guaranteed to get processor time.
\r
40 * Its main function is to check that all the other tasks are still operational.
\r
41 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
42 * incremented each time the task successfully completes its function. Should
\r
43 * any error occur within such a task the count is permanently halted. The
\r
44 * check task inspects the count of each task to ensure it has changed since
\r
45 * the last time the check task executed. If all the count variables have
\r
46 * changed all the tasks are still executing error free, and the check task
\r
47 * toggles the onboard LED. Should any task contain an error at any time
\r
48 * the LED toggle rate will change from 3 seconds to 500ms.
\r
50 * This file also includes the functionality normally implemented within the
\r
51 * standard demo application file integer.c. Due to the limited memory
\r
52 * available on the microcontroller the functionality has been included within
\r
53 * the idle task hook [vApplicationIdleHook()] - instead of within the usual
\r
54 * separate task. See the documentation within integer.c for the rationale
\r
55 * of the integer task functionality.
\r
59 * The demo applications included with other FreeRTOS ports make use of the
\r
60 * standard ComTest tasks. These use a loopback connector to transmit and
\r
61 * receive RS232 characters between two tasks. The test is important for two
\r
64 * 1) It tests the mechanism of context switching from within an application
\r
67 * 2) It generates some randomised timing.
\r
69 * The demo board used to develop this port does not include an RS232 interface
\r
70 * so the ComTest tasks could not easily be included. Instead these two tests
\r
71 * are created using a 'Button Push' task.
\r
73 * The 'Button Push' task blocks on a queue, waiting for data to arrive. A
\r
74 * simple interrupt routine connected to the PP0 input on the demo board places
\r
75 * data in the queue each time the PP0 button is pushed (this button is built
\r
76 * onto the demo board). As the 'Button Push' task is created with a
\r
77 * relatively high priority it will unblock and want to execute as soon as data
\r
78 * arrives in the queue - resulting in a context switch within the PP0 input
\r
79 * ISR. If the data retrieved from the queue is that expected the 'Button Push'
\r
80 * task toggles LED 5. Therefore correct operation is indicated by the LED
\r
81 * toggling each time the PP0 button is pressed.
\r
83 * This test is not as satisfactory as the ComTest method - but the simple
\r
84 * nature of the port makes is just about adequate.
\r
88 /* Kernel includes. */
\r
89 #include "FreeRTOS.h"
\r
93 /* Demo application includes. */
\r
96 #include "dynamic.h"
\r
97 #include "partest.h"
\r
99 /* Processor expert includes. */
\r
100 #include "ButtonInterrupt.h"
\r
102 /*-----------------------------------------------------------
\r
104 -----------------------------------------------------------*/
\r
106 /* Priorities assigned to demo application tasks. */
\r
107 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
108 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
109 #define mainBUTTON_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
110 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
112 /* LED that is toggled by the check task. The check task periodically checks
\r
113 that all the other tasks are operating without error. If no errors are found
\r
114 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
115 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
116 #define mainCHECK_TASK_LED ( 7 )
\r
117 #define mainCHECK_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
\r
118 #define mainERROR_CHECK_PERIOD ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
\r
120 /* LED that is toggled by the button push interrupt. */
\r
121 #define mainBUTTON_PUSH_LED ( 5 )
\r
123 /* The constants used in the idle task calculation. */
\r
124 #define intgCONST1 ( ( long ) 123 )
\r
125 #define intgCONST2 ( ( long ) 234567 )
\r
126 #define intgCONST3 ( ( long ) -3 )
\r
127 #define intgCONST4 ( ( long ) 7 )
\r
128 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
\r
130 /* The length of the queue between is button push ISR and the Button Push task
\r
131 is greater than 1 to account for switch bounces generating multiple inputs. */
\r
132 #define mainBUTTON_QUEUE_SIZE 6
\r
134 /*-----------------------------------------------------------
\r
135 Local functions prototypes.
\r
136 -----------------------------------------------------------*/
\r
139 * The 'Check' task function. See the explanation at the top of the file.
\r
141 static void vErrorChecks( void* pvParameters );
\r
144 * The 'Button Push' task. See the explanation at the top of the file.
\r
146 static void vButtonTask( void *pvParameters );
\r
149 * The idle task hook - in which the integer task is implemented. See the
\r
150 * explanation at the top of the file.
\r
152 void vApplicationIdleHook( void );
\r
155 * Checks the unique counts of other tasks to ensure they are still operational.
\r
157 static long prvCheckOtherTasksAreStillRunning( void );
\r
161 /*-----------------------------------------------------------
\r
163 -----------------------------------------------------------*/
\r
165 /* A few tasks are defined within this file. This flag is used to indicate
\r
166 their status. If an error is detected in one of the locally defined tasks then
\r
167 this flag is set to pdTRUE. */
\r
168 portBASE_TYPE xLocalError = pdFALSE;
\r
170 /* The queue used to send data from the button push ISR to the Button Push
\r
172 static QueueHandle_t xButtonQueue;
\r
175 /*-----------------------------------------------------------*/
\r
178 * This is called from the main() function generated by the Processor Expert.
\r
182 /* Start some of the standard demo tasks. */
\r
183 vStartLEDFlashTasks( mainFLASH_PRIORITY );
\r
184 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
185 vStartDynamicPriorityTasks();
\r
187 /* Start the locally defined tasks. There is also a task implemented as
\r
189 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
190 xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_TASK_PRIORITY, NULL );
\r
192 /* All the tasks have been created - start the scheduler. */
\r
193 vTaskStartScheduler();
\r
195 /* Should not reach here! */
\r
198 /*-----------------------------------------------------------*/
\r
200 static void vErrorChecks( void *pvParameters )
\r
202 TickType_t xDelayPeriod = mainCHECK_PERIOD;
\r
203 TickType_t xLastWakeTime;
\r
205 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
206 functions correctly. */
\r
207 xLastWakeTime = xTaskGetTickCount();
\r
211 /* Delay until it is time to execute again. The delay period is
\r
212 shorter following an error. */
\r
213 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
215 /* Check all the demo application tasks are executing without
\r
216 error. If an error is found the delay period is shortened - this
\r
217 has the effect of increasing the flash rate of the 'check' task
\r
219 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
\r
221 /* An error has been detected in one of the tasks - flash faster. */
\r
222 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
225 /* Toggle the LED each cycle round. */
\r
226 vParTestToggleLED( mainCHECK_TASK_LED );
\r
229 /*-----------------------------------------------------------*/
\r
231 static long prvCheckOtherTasksAreStillRunning( void )
\r
233 portBASE_TYPE xAllTasksPassed = pdPASS;
\r
235 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
237 xAllTasksPassed = pdFAIL;
\r
240 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
242 xAllTasksPassed = pdFAIL;
\r
245 /* Also check the status flag for the tasks defined within this function. */
\r
246 if( xLocalError != pdFALSE )
\r
248 xAllTasksPassed = pdFAIL;
\r
251 return xAllTasksPassed;
\r
253 /*-----------------------------------------------------------*/
\r
255 void vApplicationIdleHook( void )
\r
257 /* This variable is effectively set to a constant so it is made volatile to
\r
258 ensure the compiler does not just get rid of it. */
\r
259 volatile long lValue;
\r
261 /* Keep performing a calculation and checking the result against a constant. */
\r
264 /* Perform the calculation. This will store partial value in
\r
265 registers, resulting in a good test of the context switch mechanism. */
\r
266 lValue = intgCONST1;
\r
267 lValue += intgCONST2;
\r
268 lValue *= intgCONST3;
\r
269 lValue /= intgCONST4;
\r
271 /* Did we perform the calculation correctly with no corruption? */
\r
272 if( lValue != intgEXPECTED_ANSWER )
\r
275 portENTER_CRITICAL();
\r
276 xLocalError = pdTRUE;
\r
277 portEXIT_CRITICAL();
\r
280 /* Yield in case cooperative scheduling is being used. */
\r
281 #if configUSE_PREEMPTION == 0
\r
288 /*-----------------------------------------------------------*/
\r
290 static void vButtonTask( void *pvParameters )
\r
292 unsigned portBASE_TYPE uxExpected = 1, uxReceived;
\r
294 /* Create the queue used by the producer and consumer. */
\r
295 xButtonQueue = xQueueCreate( mainBUTTON_QUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portBASE_TYPE ) );
\r
299 /* Now the queue is created it is safe to enable the button interrupt. */
\r
300 ButtonInterrupt_Enable();
\r
304 /* Simply wait for data to arrive from the button push interrupt. */
\r
305 if( xQueueReceive( xButtonQueue, &uxReceived, portMAX_DELAY ) == pdPASS )
\r
307 /* Was the data we received that expected? */
\r
308 if( uxReceived != uxExpected )
\r
311 portENTER_CRITICAL();
\r
312 xLocalError = pdTRUE;
\r
313 portEXIT_CRITICAL();
\r
317 /* Toggle the LED for every successful push. */
\r
318 vParTestToggleLED( mainBUTTON_PUSH_LED );
\r
326 /* Will only get here if the queue could not be created. */
\r
329 /*-----------------------------------------------------------*/
\r
331 #pragma CODE_SEG __NEAR_SEG NON_BANKED
\r
333 /* Button push ISR. */
\r
334 void interrupt vButtonPush( void )
\r
336 static unsigned portBASE_TYPE uxValToSend = 0;
\r
337 static unsigned long xHigherPriorityTaskWoken;
\r
339 xHigherPriorityTaskWoken = pdFALSE;
\r
341 /* Send an incrementing value to the button push task each run. */
\r
344 /* Clear the interrupt flag. */
\r
347 /* Send the incremented value down the queue. The button push task is
\r
348 blocked waiting for the data. As the button push task is high priority
\r
349 it will wake and a context switch should be performed before leaving
\r
351 xQueueSendFromISR( xButtonQueue, &uxValToSend, &xHigherPriorityTaskWoken );
\r
353 if( xHigherPriorityTaskWoken )
\r
355 /* NOTE: This macro can only be used if there are no local
\r
356 variables defined. This function uses a static variable so it's
\r
357 use is permitted. If the variable were not static portYIELD()
\r
358 would have to be used in it's place. */
\r
359 portTASK_SWITCH_FROM_ISR();
\r
363 #pragma CODE_SEG DEFAULT
\r