]> begriffs open source - cmsis-freertos/blob - Demo/ARM7_STR71x_IAR/main.c
osEventFlagsWait: Fix flag comparison
[cmsis-freertos] / Demo / ARM7_STR71x_IAR / main.c
1 /*
2  * FreeRTOS Kernel V10.2.1
3  * Copyright (C) 2019 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.
39  *
40  * Main.c also creates a task called "Check".  This only executes every three
41  * seconds but has the highest priority so is guaranteed to get processor time.
42  * Its main function is to check that all the other tasks are still operational.
43  * Each task (other than the "flash" tasks) maintains a unique count that is
44  * incremented each time the task successfully completes its function.  Should
45  * any error occur within such a task the count is permanently halted.  The
46  * check task inspects the count of each task to ensure it has changed since
47  * the last time the check task executed.  If all the count variables have
48  * changed all the tasks are still executing error free, and the check task
49  * toggles the onboard LED.  Should any task contain an error at any time
50  * the LED toggle rate will change from 3 seconds to 500ms.
51  *
52  */
53
54 /* Library includes. */
55 #include "RCCU.h"
56 #include "wdg.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
72 /* Priorities for the demo application tasks. */
73 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )
74 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )
75 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )
76 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )
77 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )
78 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )
79
80 /* Constants required by the 'Check' task. */
81 #define mainNO_ERROR_FLASH_PERIOD       ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )
82 #define mainERROR_FLASH_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS  )
83 #define mainCHECK_TASK_LED                      ( 4 )
84
85 /* Constants for the ComTest tasks. */
86 #define mainCOM_TEST_BAUD_RATE          ( ( unsigned long ) 115200 )
87 #define mainCOM_TEST_LED                        ( 6 ) /* The LED built onto the kickstart board. */
88
89 /*
90  * The task that executes at the highest priority and calls
91  * prvCheckOtherTasksAreStillRunning().  See the description at the top
92  * of the file.
93  */
94 static void vErrorChecks( void *pvParameters );
95
96 /*
97  * Configure the processor for use with the IAR STR71x demo board.  This
98  * just sets the PLL for the required frequency.
99  */
100 static void prvSetupHardware( void );
101
102 /*
103  * Checks that all the demo application tasks are still executing without error
104  * - as described at the top of the file.  Called by vErrorChecks().
105  */
106 static long prvCheckOtherTasksAreStillRunning( void );
107
108
109 /*-----------------------------------------------------------*/
110
111 /*
112  * Starts all the other tasks, then starts the scheduler.
113  */
114 void main( void )
115 {
116         /* Setup any hardware that has not already been configured by the low
117         level init routines. */
118         prvSetupHardware();
119
120         /* Initialise the LED outputs for use by the demo application tasks. */
121         vParTestInitialise();
122
123         /* Start all the standard demo application tasks. */
124         vStartIntegerMathTasks( tskIDLE_PRIORITY );
125         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
126         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
127         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
128         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
129         vStartDynamicPriorityTasks();
130         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
131
132         /* Start the check task - which is defined in this file. */
133         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
134
135         /* Start the scheduler.
136
137         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
138         The processor MUST be in supervisor mode when vTaskStartScheduler is
139         called.  The demo applications included in the FreeRTOS.org download switch
140         to supervisor mode prior to main being called.  If you are not using one of
141         these demo application projects then ensure Supervisor mode is used here. */
142
143         vTaskStartScheduler();
144
145         /* We should never get here as control is now taken by the scheduler. */
146         return;
147 }
148 /*-----------------------------------------------------------*/
149
150 static void prvSetupHardware( void )
151 {
152     /* Setup the PLL to generate a 48MHz clock from the 4MHz CLK. */
153
154     /* Turn of the div by two. */
155         RCCU_Div2Config( DISABLE );
156
157     /* 48MHz = ( 4MHz * 12 ) / 1 */
158         RCCU_PLL1Config( RCCU_PLL1_Mul_12, RCCU_Div_1 );
159     RCCU_RCLKSourceConfig( RCCU_PLL1_Output );
160 }
161 /*-----------------------------------------------------------*/
162
163 static void vErrorChecks( void *pvParameters )
164 {
165 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
166 TickType_t xLastWakeTime;
167
168         /* The parameters are not used in this task. */
169         ( void ) pvParameters;
170
171         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
172         functions correctly. */
173         xLastWakeTime = xTaskGetTickCount();
174
175         /* Cycle for ever, delaying then checking all the other tasks are still
176         operating without error.  If an error is detected then the delay period
177         is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
178         the on board LED flash rate will increase. */
179
180         for( ;; )
181         {
182                 /* Delay until it is time to execute again.  The delay period is
183                 shorter following an error so the LED flashes faster. */
184                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
185         
186                 /* Check all the standard demo application tasks are executing without
187                 error. */
188                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )
189                 {
190                         /* An error has been detected in one of the tasks - flash faster. */
191                         xDelayPeriod = mainERROR_FLASH_PERIOD;
192                 }
193                 
194                 vParTestToggleLED( mainCHECK_TASK_LED );
195         }
196 }
197 /*-----------------------------------------------------------*/
198
199 static long prvCheckOtherTasksAreStillRunning( void )
200 {
201 long lReturn = ( long ) pdPASS;
202
203         /* Check all the demo tasks (other than the flash tasks) to ensure
204         that they are all still running, and that none of them have detected
205         an error. */
206
207         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
208         {
209                 lReturn = ( long ) pdFAIL;
210         }
211
212         if( xArePollingQueuesStillRunning() != pdTRUE )
213         {
214                 lReturn = ( long ) pdFAIL;
215         }
216
217         if( xAreSemaphoreTasksStillRunning() != pdTRUE )
218         {
219                 lReturn = ( long ) pdFAIL;
220         }
221
222         if( xAreBlockingQueuesStillRunning() != pdTRUE )
223         {
224                 lReturn = ( long ) pdFAIL;
225         }
226
227         if( xAreComTestTasksStillRunning() != pdTRUE )
228         {
229                 lReturn = ( long ) pdFAIL;
230         }
231
232         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
233         {
234                 lReturn = ( long ) pdFAIL;
235         }
236
237         return lReturn;
238 }
239 /*-----------------------------------------------------------*/
240
241