3 * Copyright (C) 2020 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 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
30 The processor MUST be in supervisor mode when vTaskStartScheduler is
31 called. The demo applications included in the FreeRTOS.org download switch
32 to supervisor mode prior to main being called. If you are not using one of
33 these demo application projects then ensure Supervisor mode is used.
37 * Creates all the demo application tasks, then starts the scheduler. The WEB
38 * documentation provides more details of the demo application tasks. The SAM7
39 * includes a sample USB that emulates a Joystick input to a USB host.
41 * Main.c also creates a task called "Check". This only executes every three
42 * seconds but has the highest priority so is guaranteed to get processor time.
43 * Its main function is to check that all the other tasks are still operational.
44 * Each task (other than the "flash" tasks) maintains a unique count that is
45 * incremented each time the task successfully completes its function. Should
46 * any error occur within such a task the count is permanently halted. The
47 * check task inspects the count of each task to ensure it has changed since
48 * the last time the check task executed. If all the count variables have
49 * changed all the tasks are still executing error free, and the check task
50 * toggles the onboard LED. Should any task contain an error at any time
51 * the LED toggle rate will change from 3 seconds to 500ms.
55 /* Standard includes. */
58 /* Scheduler includes. */
62 /* Demo application includes. */
71 #include "USB/USBSample.h"
73 /* Priorities for the demo application tasks. */
74 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
75 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
76 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
77 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
78 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
79 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
80 #define mainUSB_PRIORITY ( tskIDLE_PRIORITY + 2 )
82 /* Constants required by the 'Check' task. */
83 #define mainNO_ERROR_FLASH_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
84 #define mainERROR_FLASH_PERIOD ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
85 #define mainCHECK_TASK_LED ( 3 )
87 /* Constants for the ComTest tasks. */
88 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
89 #define mainCOM_TEST_LED ( 4 ) /* Off the board. */
92 * The task that executes at the highest priority and calls
93 * prvCheckOtherTasksAreStillRunning(). See the description at the top
96 static void vErrorChecks( void *pvParameters );
99 * Configure the processor for use with the Atmel demo board. Setup is minimal
100 * as the low level init function (called from the startup asm file) takes care
103 static void prvSetupHardware( void );
106 * Checks that all the demo application tasks are still executing without error
107 * - as described at the top of the file.
109 static long prvCheckOtherTasksAreStillRunning( void );
112 /*-----------------------------------------------------------*/
115 * Starts all the other tasks, then starts the scheduler.
119 /* Setup any hardware that has not already been configured by the low
120 level init routines. */
123 /* Initialise the LED outputs for use by the demo application tasks. */
124 vParTestInitialise();
126 /* Start all the standard demo application tasks. */
127 vStartIntegerMathTasks( tskIDLE_PRIORITY );
128 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
129 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
130 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
131 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
132 vStartDynamicPriorityTasks();
133 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
135 /* Also start the USB demo which is just for the SAM7. */
136 xTaskCreate( vUSBDemoTask, "USB", configMINIMAL_STACK_SIZE, NULL, mainUSB_PRIORITY, NULL );
138 /* Start the check task - which is defined in this file. */
139 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
141 /* Start the scheduler.
143 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
144 The processor MUST be in supervisor mode when vTaskStartScheduler is
145 called. The demo applications included in the FreeRTOS.org download switch
146 to supervisor mode prior to main being called. If you are not using one of
147 these demo application projects then ensure Supervisor mode is used here. */
149 vTaskStartScheduler();
151 /* We should never get here as control is now taken by the scheduler. */
154 /*-----------------------------------------------------------*/
156 static void prvSetupHardware( void )
158 /* When using the JTAG debugger the hardware is not always initialised to
159 the correct default state. This line just ensures that this does not
160 cause all interrupts to be masked at the start. */
161 AT91C_BASE_AIC->AIC_EOICR = 0;
163 /* Most setup is performed by the low level init function called from the
166 /* Configure the PIO Lines corresponding to LED1 to LED4 to be outputs as
167 well as the UART Tx line. */
168 AT91F_PIO_CfgOutput( AT91C_BASE_PIOA, LED_MASK );
170 /* Enable the peripheral clock. */
171 AT91F_PMC_EnablePeriphClock( AT91C_BASE_PMC, 1 << AT91C_ID_PIOA );
173 /*-----------------------------------------------------------*/
175 static void vErrorChecks( void *pvParameters )
177 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
179 /* The parameters are not used in this task. */
180 ( void ) pvParameters;
182 /* Cycle for ever, delaying then checking all the other tasks are still
183 operating without error. If an error is detected then the delay period
184 is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
185 the on board LED flash rate will increase. */
189 /* Delay until it is time to execute again. */
190 vTaskDelay( xDelayPeriod );
192 /* Check all the standard demo application tasks are executing without
194 if( prvCheckOtherTasksAreStillRunning() != pdPASS )
196 /* An error has been detected in one of the tasks - flash faster. */
197 xDelayPeriod = mainERROR_FLASH_PERIOD;
200 vParTestToggleLED( mainCHECK_TASK_LED );
203 /*-----------------------------------------------------------*/
205 static long prvCheckOtherTasksAreStillRunning( void )
207 long lReturn = ( long ) pdPASS;
209 /* Check all the demo tasks (other than the flash tasks) to ensure
210 that they are all still running, and that none of them have detected
213 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
215 lReturn = ( long ) pdFAIL;
218 if( xArePollingQueuesStillRunning() != pdTRUE )
220 lReturn = ( long ) pdFAIL;
223 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
225 lReturn = ( long ) pdFAIL;
228 if( xAreBlockingQueuesStillRunning() != pdTRUE )
230 lReturn = ( long ) pdFAIL;
233 if( xAreComTestTasksStillRunning() != pdTRUE )
235 lReturn = ( long ) pdFAIL;
238 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
240 lReturn = ( long ) pdFAIL;
245 /*-----------------------------------------------------------*/