]> begriffs open source - cmsis-freertos/blob - Demo/ARM7_AT91SAM7S64_IAR/main.c
Updated pack to FreeRTOS 10.4.4
[cmsis-freertos] / Demo / ARM7_AT91SAM7S64_IAR / main.c
1 /*
2  * FreeRTOS V202107.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /* 
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.
34 */
35
36 /*
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.
40  * 
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.
52  *
53  */
54
55 /* Standard includes. */
56 #include <stdlib.h>
57
58 /* Scheduler includes. */
59 #include "FreeRTOS.h"
60 #include "task.h"
61
62 /* Demo application includes. */
63 #include "flash.h"
64 #include "integer.h"
65 #include "PollQ.h"
66 #include "BlockQ.h"
67 #include "semtest.h"
68 #include "dynamic.h"
69 #include "partest.h"
70 #include "comtest2.h"
71 #include "USB/USBSample.h"
72
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 )
81
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 )
86
87 /* Constants for the ComTest tasks. */
88 #define mainCOM_TEST_BAUD_RATE          ( ( unsigned long ) 115200 )
89 #define mainCOM_TEST_LED                        ( 4 ) /* Off the board. */
90
91 /*
92  * The task that executes at the highest priority and calls 
93  * prvCheckOtherTasksAreStillRunning().  See the description at the top
94  * of the file.
95  */
96 static void vErrorChecks( void *pvParameters );
97
98 /*
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
101  * of most things.
102  */
103 static void prvSetupHardware( void );
104
105 /*
106  * Checks that all the demo application tasks are still executing without error
107  * - as described at the top of the file.
108  */
109 static long prvCheckOtherTasksAreStillRunning( void );
110
111
112 /*-----------------------------------------------------------*/
113
114 /*
115  * Starts all the other tasks, then starts the scheduler. 
116  */
117 void main( void )
118 {
119         /* Setup any hardware that has not already been configured by the low
120         level init routines. */
121         prvSetupHardware();
122
123         /* Initialise the LED outputs for use by the demo application tasks. */
124         vParTestInitialise();
125
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 );
134         
135         /* Also start the USB demo which is just for the SAM7. */
136         xTaskCreate( vUSBDemoTask, "USB", configMINIMAL_STACK_SIZE, NULL, mainUSB_PRIORITY, NULL );
137         
138         /* Start the check task - which is defined in this file. */
139         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
140
141         /* Start the scheduler.
142
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. */
148
149         vTaskStartScheduler();
150
151         /* We should never get here as control is now taken by the scheduler. */
152         return;
153 }
154 /*-----------------------------------------------------------*/
155
156 static void prvSetupHardware( void )
157 {
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;
162         
163         /* Most setup is performed by the low level init function called from the 
164         startup asm file. */
165
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 );
169         
170         /* Enable the peripheral clock. */
171         AT91F_PMC_EnablePeriphClock( AT91C_BASE_PMC, 1 << AT91C_ID_PIOA );
172 }
173 /*-----------------------------------------------------------*/
174
175 static void vErrorChecks( void *pvParameters )
176 {
177 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
178
179         /* The parameters are not used in this task. */
180         ( void ) pvParameters;
181
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. */
186
187         for( ;; )
188         {
189                 /* Delay until it is time to execute again. */
190                 vTaskDelay( xDelayPeriod );
191         
192                 /* Check all the standard demo application tasks are executing without 
193                 error. */
194                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )
195                 {
196                         /* An error has been detected in one of the tasks - flash faster. */
197                         xDelayPeriod = mainERROR_FLASH_PERIOD;
198                 }
199                 
200                 vParTestToggleLED( mainCHECK_TASK_LED );
201         }
202 }
203 /*-----------------------------------------------------------*/
204
205 static long prvCheckOtherTasksAreStillRunning( void )
206 {
207 long lReturn = ( long ) pdPASS;
208
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
211         an error. */
212
213         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
214         {
215                 lReturn = ( long ) pdFAIL;
216         }
217
218         if( xArePollingQueuesStillRunning() != pdTRUE )
219         {
220                 lReturn = ( long ) pdFAIL;
221         }
222
223         if( xAreSemaphoreTasksStillRunning() != pdTRUE )
224         {
225                 lReturn = ( long ) pdFAIL;
226         }
227
228         if( xAreBlockingQueuesStillRunning() != pdTRUE )
229         {
230                 lReturn = ( long ) pdFAIL;
231         }
232
233         if( xAreComTestTasksStillRunning() != pdTRUE )
234         {
235                 lReturn = ( long ) pdFAIL;
236         }
237
238         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
239         {
240                 lReturn = ( long ) pdFAIL;
241         }
242
243         return lReturn;
244 }
245 /*-----------------------------------------------------------*/
246
247