]> begriffs open source - cmsis-freertos/blob - Demo/NEC_V850ES_IAR/main.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / NEC_V850ES_IAR / main.c
1 /*
2  * FreeRTOS V202111.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  * Creates all the demo application tasks, then starts the scheduler.  The WEB
30  * documentation provides more details of the standard demo application tasks.
31  * In addition to the standard demo tasks, the following tasks and tests are
32  * defined and/or created within this file:
33  *
34  * "Check" task -  This only executes every three seconds but has a high priority
35  * to ensure it gets processor time.  Its main function is to check that all the
36  * standard demo tasks are still operational.  If everything is running as
37  * expected then the check task will toggle an LED every 3 seconds.  An error
38  * being discovered in any task will cause the toggle rate to increase to 500ms.
39  *
40  * "Reg test" tasks - These fill the registers with known values, then check
41  * that each register still contains its expected value.  Each task uses
42  * different values.  The tasks run with very low priority so get preempted very
43  * frequently.  A register containing an unexpected value is indicative of an
44  * error in the context switching mechanism.
45  *
46  */
47
48 /* Standard include files. */
49 #include <stdlib.h>
50 #include <string.h>
51
52 /* Scheduler include files. */
53 #include "FreeRTOS.h"
54 #include "task.h"
55
56 /* Demo file headers. */
57 #include <intrinsics.h>
58 #include "BlockQ.h"
59 #include "death.h"
60 #include "flash.h"
61 #include "partest.h"
62 #include "semtest.h"
63 #include "PollQ.h"
64 #include "GenQTest.h"
65 #include "QPeek.h"
66 #include "recmutex.h"
67 #include "comtest2.h"
68
69 /*
70  * Priority definitions for most of the tasks in the demo application.  Some
71  * tasks just use the idle priority.
72  */
73 #define mainFLASH_PRIORITY                                      ( tskIDLE_PRIORITY + 1 )
74 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )
75 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )
76 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )
77 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )
78 #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 2 )
79 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )
80 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )
81 #define mainCOMTEST_PRIORITY                            ( tskIDLE_PRIORITY + 1 )
82
83 /* Passed into the check task just as a test that the parameter passing
84 mechanism is working correctly. */
85 #define mainCHECK_PARAMETER                                     ( ( void * ) 0x12345678 )
86
87 /* The period between executions of the check task. */
88 #define mainNO_ERROR_DELAY              ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )
89 #define mainERROR_DELAY                 ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
90
91 /* There are no spare LEDs for the comtest tasks, so this is just set to an
92 invalid number. */
93 #define mainCOMTEST_LED                 ( 4 )
94
95 /* The baud rate used by the comtest task. */
96 #define mainBAUD_RATE                   ( 9600 )
97
98 /*-----------------------------------------------------------*/
99
100 /* The implementation of the 'check' task as described at the top of this file. */
101 static void prvCheckTask( void *pvParameters );
102
103 /* Just sets up the LED outputs.  Most generic setup is done in
104 __low_level_init(). */
105 static void prvSetupHardware( void );
106
107 /* The RegTest functions as described at the top of this file. */
108 extern void vRegTest1( void *pvParameters );
109 extern void vRegTest2( void *pvParameters );
110
111 /* A variable that will get set to fail if a RegTest task finds an error.  The
112 variable is inspected by the 'Check' task. */
113 static volatile long lRegTestStatus = pdPASS;
114
115 /*-----------------------------------------------------------*/
116
117 /* Create all the demo tasks then start the scheduler. */
118 void main( void )
119 {
120         /* Just sets up the LED outputs. */
121         prvSetupHardware();
122
123         /* Standard demo tasks. */
124         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
125         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
126         vStartQueuePeekTasks();
127
128         /* Create the check task as described at the top of this file. */
129         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, mainCHECK_PARAMETER, mainCHECK_TASK_PRIORITY, NULL );
130
131         /* Create the RegTest tasks as described at the top of this file. */
132         xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
133         xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
134
135         #ifdef __IAR_V850ES_Fx3__
136         {
137                 /* The extra IO required for the com test and led flashing tasks is only
138                 available on the application board, not the target boards. */
139                 vAltStartComTestTasks( mainCOMTEST_PRIORITY, mainBAUD_RATE, mainCOMTEST_LED );
140                 vStartLEDFlashTasks( mainFLASH_PRIORITY );
141
142                 /* The Fx3 also has enough RAM to run loads more tasks. */
143                 vStartRecursiveMutexTasks();
144                 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
145                 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
146         }
147         #endif
148
149         /* The suicide tasks must be created last as they need to know how many
150         tasks were running prior to their creation in order to ascertain whether
151         or not the correct/expected number of tasks are running at any given time. */
152     vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
153
154         /* Start the scheduler. */
155         vTaskStartScheduler();
156
157         /* If this line is reached then vTaskStartScheduler() returned because there
158         was insufficient heap memory remaining for the idle task to be created. */
159         for( ;; );
160 }
161 /*-----------------------------------------------------------*/
162
163 static void prvCheckTask( void *pvParameters )
164 {
165 TickType_t xDelayPeriod = mainNO_ERROR_DELAY, xLastWakeTime;
166 unsigned portBASE_TYPE uxLEDToUse = 0;
167
168         /* Ensure parameter is passed in correctly. */
169         if( pvParameters != mainCHECK_PARAMETER )
170         {
171                 xDelayPeriod = mainERROR_DELAY;
172         }
173
174         /* Initialise xLastWakeTime before it is used.  After this point it is not
175         written to directly. */
176         xLastWakeTime = xTaskGetTickCount();
177
178         /* Cycle for ever, delaying then checking all the other tasks are still
179         operating without error. */
180         for( ;; )
181         {
182                 /* Wait until it is time to check all the other tasks again. */
183                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
184
185                 if( lRegTestStatus != pdPASS )
186                 {
187                         xDelayPeriod = mainERROR_DELAY;
188                 }
189
190                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
191                 {
192                         xDelayPeriod = mainERROR_DELAY;
193                 }
194
195                 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
196                 {
197                         xDelayPeriod = mainERROR_DELAY;
198                 }
199
200                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
201             {
202                 xDelayPeriod = mainERROR_DELAY;
203             }
204
205                 if( xIsCreateTaskStillRunning() != pdTRUE )
206             {
207                 xDelayPeriod = mainERROR_DELAY;
208             }
209
210                 /* The Fx3 runs more tasks, so more checks are performed. */
211                 #ifdef __IAR_V850ES_Fx3__
212                 {
213                         if( xAreComTestTasksStillRunning() != pdTRUE )
214                         {
215                                 xDelayPeriod = mainERROR_DELAY;
216                         }
217
218                         if( xArePollingQueuesStillRunning() != pdTRUE )
219                         {
220                                 xDelayPeriod = mainERROR_DELAY;
221                         }
222
223                         if( xAreBlockingQueuesStillRunning() != pdTRUE )
224                         {
225                                 xDelayPeriod = mainERROR_DELAY;
226                         }
227
228                         if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
229                         {
230                                 xDelayPeriod = mainERROR_DELAY;
231                         }
232
233                         /* The application board has more LEDs and uses the flash tasks
234                         so the check task instead uses LED3 as LED3 is still spare. */
235                         uxLEDToUse = 3;
236                 }
237                 #endif
238
239                 /* Toggle the LED.  The toggle rate will depend on whether or not an
240                 error has been found in any tasks. */
241                 vParTestToggleLED( uxLEDToUse );
242         }
243 }
244 /*-----------------------------------------------------------*/
245
246 static void prvSetupHardware( void )
247 {
248         /* Setup the LED outputs. */
249         vParTestInitialise();
250
251         /* Any additional hardware configuration can be added here. */
252 }
253 /*-----------------------------------------------------------*/
254
255 void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName )
256 {
257         ( void ) xTask;
258         ( void ) pcTaskName;
259
260         /* This will be called if a task overflows its stack.  pxCurrentTCB
261         can be inspected to see which is the offending task. */
262         for( ;; );
263 }
264 /*-----------------------------------------------------------*/
265
266 void vRegTestFailed( void )
267 {
268         /* Called by the RegTest tasks if an error is found.  lRegTestStatus is
269         inspected by the check task. */
270         lRegTestStatus = pdFAIL;
271
272         /* Do not return from here as the reg test tasks clobber all registers so
273         function calls may not function correctly. */
274         for( ;; );
275 }