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 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.
38 * Creates all the application tasks, then starts the scheduler.
40 * A task defined by the function vBasicWEBServer is created. This executes
41 * the lwIP stack and basic WEB server sample. A task defined by the function
42 * vUSBCDCTask. This executes the USB to serial CDC example. All the other
43 * tasks are from the set of standard demo tasks. The WEB documentation
44 * provides more details of the standard demo application tasks.
46 * Main.c also creates a task called "Check". This only executes every three
47 * seconds but has the highest priority so is guaranteed to get processor time.
48 * Its main function is to check the status of all the other demo application
49 * tasks. LED mainCHECK_LED is toggled every three seconds by the check task
50 * should no error conditions be detected in any of the standard demo tasks.
51 * The toggle rate increasing to 500ms indicates that at least one error has
54 * Main.c includes an idle hook function that simply periodically sends data
55 * to the USB task for transmission.
61 + Modified the stack sizes used by some tasks to permit use of the
62 command line GCC tools.
65 /* Library includes. */
69 /* Scheduler includes. */
73 /* Demo application includes. */
86 /* Hardware specific headers. */
88 #include "AT91SAM7X256.h"
90 /* Priorities/stacks for the various tasks within the demo application. */
91 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
92 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
93 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
94 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
95 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 1 )
96 #define mainWEBSERVER_PRIORITY ( tskIDLE_PRIORITY + 2 )
97 #define mainUSB_PRIORITY ( tskIDLE_PRIORITY + 1 )
98 #define mainUSB_TASK_STACK ( 200 )
100 /* The rate at which the on board LED will toggle when there is/is not an
102 #define mainNO_ERROR_FLASH_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
103 #define mainERROR_FLASH_PERIOD ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
105 /* The rate at which the idle hook sends data to the USB port. */
106 #define mainUSB_TX_FREQUENCY ( 100 / portTICK_PERIOD_MS )
108 /* The string that is transmitted down the USB port. */
109 #define mainFIRST_TX_CHAR 'a'
110 #define mainLAST_TX_CHAR 'z'
112 /* The LED used by the check task to indicate the system status. */
113 #define mainCHECK_LED ( 3 )
114 /*-----------------------------------------------------------*/
117 * Checks that all the demo application tasks are still executing without error
118 * - as described at the top of the file.
120 static long prvCheckOtherTasksAreStillRunning( void );
123 * The task that executes at the highest priority and calls
124 * prvCheckOtherTasksAreStillRunning(). See the description at the top
127 static void vErrorChecks( void *pvParameters );
130 * Configure the processor for use with the Atmel demo board. This is very
131 * minimal as most of the setup is performed in the startup code.
133 static void prvSetupHardware( void );
136 * The idle hook is just used to stream data to the USB port.
138 void vApplicationIdleHook( void );
139 /*-----------------------------------------------------------*/
142 * Setup hardware then start all the demo application tasks.
146 /* Setup the ports. */
149 /* Setup the IO required for the LED's. */
150 vParTestInitialise();
155 /* Create the lwIP task. This uses the lwIP RTOS abstraction layer.*/
156 sys_thread_new( vBasicWEBServer, ( void * ) NULL, mainWEBSERVER_PRIORITY );
158 /* Create the demo USB CDC task. */
159 xTaskCreate( vUSBCDCTask, "USB", mainUSB_TASK_STACK, NULL, mainUSB_PRIORITY, NULL );
161 /* Create the standard demo application tasks. */
162 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
163 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
164 vStartLEDFlashTasks( mainFLASH_PRIORITY );
165 vStartIntegerMathTasks( tskIDLE_PRIORITY );
166 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
168 /* Start the check task - which is defined in this file. */
169 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
171 /* Finally, start the scheduler.
173 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
174 The processor MUST be in supervisor mode when vTaskStartScheduler is
175 called. The demo applications included in the FreeRTOS.org download switch
176 to supervisor mode prior to main being called. If you are not using one of
177 these demo application projects then ensure Supervisor mode is used here. */
178 vTaskStartScheduler();
180 /* Should never get here! */
183 /*-----------------------------------------------------------*/
186 static void prvSetupHardware( void )
188 /* When using the JTAG debugger the hardware is not always initialised to
189 the correct default state. This line just ensures that this does not
190 cause all interrupts to be masked at the start. */
191 AT91C_BASE_AIC->AIC_EOICR = 0;
193 /* Most setup is performed by the low level init function called from the
196 Configure the PIO Lines corresponding to LED1 to LED4 to be outputs as
197 well as the UART Tx line. */
198 AT91C_BASE_PIOB->PIO_PER = LED_MASK; // Set in PIO mode
199 AT91C_BASE_PIOB->PIO_OER = LED_MASK; // Configure in Output
202 /* Enable the peripheral clock. */
203 AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA;
204 AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOB;
205 AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_EMAC;
207 /*-----------------------------------------------------------*/
209 static void vErrorChecks( void *pvParameters )
211 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
212 TickType_t xLastWakeTime;
214 /* The parameters are not used. */
215 ( void ) pvParameters;
217 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
218 functions correctly. */
219 xLastWakeTime = xTaskGetTickCount();
221 /* Cycle for ever, delaying then checking all the other tasks are still
222 operating without error. If an error is detected then the delay period
223 is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
224 the Check LED flash rate will increase. */
227 /* Delay until it is time to execute again. The delay period is
228 shorter following an error. */
229 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
231 /* Check all the standard demo application tasks are executing without
233 if( prvCheckOtherTasksAreStillRunning() != pdPASS )
235 /* An error has been detected in one of the tasks - flash faster. */
236 xDelayPeriod = mainERROR_FLASH_PERIOD;
239 vParTestToggleLED( mainCHECK_LED );
242 /*-----------------------------------------------------------*/
244 static long prvCheckOtherTasksAreStillRunning( void )
246 long lReturn = ( long ) pdPASS;
248 /* Check all the demo tasks (other than the flash tasks) to ensure
249 that they are all still running, and that none of them have detected
252 if( xArePollingQueuesStillRunning() != pdTRUE )
254 lReturn = ( long ) pdFAIL;
257 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
259 lReturn = ( long ) pdFAIL;
262 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
264 lReturn = ( long ) pdFAIL;
267 if( xAreBlockingQueuesStillRunning() != pdTRUE )
269 lReturn = ( long ) pdFAIL;
274 /*-----------------------------------------------------------*/
276 void vApplicationIdleHook( void )
278 static TickType_t xLastTx = 0;
281 /* The idle hook simply sends a string of characters to the USB port.
282 The characters will be buffered and sent once the port is connected. */
283 if( ( xTaskGetTickCount() - xLastTx ) > mainUSB_TX_FREQUENCY )
285 xLastTx = xTaskGetTickCount();
286 for( cTxByte = mainFIRST_TX_CHAR; cTxByte <= mainLAST_TX_CHAR; cTxByte++ )
288 vUSBSendByte( cTxByte );