]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/NEC_V850ES_IAR/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / NEC_V850ES_IAR / main.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
31  * documentation provides more details of the standard demo application tasks.\r
32  * In addition to the standard demo tasks, the following tasks and tests are\r
33  * defined and/or created within this file:\r
34  *\r
35  * "Check" task -  This only executes every three seconds but has a high priority\r
36  * to ensure it gets processor time.  Its main function is to check that all the\r
37  * standard demo tasks are still operational.  If everything is running as\r
38  * expected then the check task will toggle an LED every 3 seconds.  An error\r
39  * being discovered in any task will cause the toggle rate to increase to 500ms.\r
40  *\r
41  * "Reg test" tasks - These fill the registers with known values, then check\r
42  * that each register still contains its expected value.  Each task uses\r
43  * different values.  The tasks run with very low priority so get preempted very\r
44  * frequently.  A register containing an unexpected value is indicative of an\r
45  * error in the context switching mechanism.\r
46  *\r
47  */\r
48 \r
49 /* Standard include files. */\r
50 #include <stdlib.h>\r
51 #include <string.h>\r
52 \r
53 /* Scheduler include files. */\r
54 #include "FreeRTOS.h"\r
55 #include "task.h"\r
56 \r
57 /* Demo file headers. */\r
58 #include <intrinsics.h>\r
59 #include "BlockQ.h"\r
60 #include "death.h"\r
61 #include "flash.h"\r
62 #include "partest.h"\r
63 #include "semtest.h"\r
64 #include "PollQ.h"\r
65 #include "GenQTest.h"\r
66 #include "QPeek.h"\r
67 #include "recmutex.h"\r
68 #include "comtest2.h"\r
69 \r
70 /*\r
71  * Priority definitions for most of the tasks in the demo application.  Some\r
72  * tasks just use the idle priority.\r
73  */\r
74 #define mainFLASH_PRIORITY                                      ( tskIDLE_PRIORITY + 1 )\r
75 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
76 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
77 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
78 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
79 #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
80 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
81 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
82 #define mainCOMTEST_PRIORITY                            ( tskIDLE_PRIORITY + 1 )\r
83 \r
84 /* Passed into the check task just as a test that the parameter passing\r
85 mechanism is working correctly. */\r
86 #define mainCHECK_PARAMETER                                     ( ( void * ) 0x12345678 )\r
87 \r
88 /* The period between executions of the check task. */\r
89 #define mainNO_ERROR_DELAY              ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
90 #define mainERROR_DELAY                 ( ( TickType_t ) 500 / portTICK_PERIOD_MS )\r
91 \r
92 /* There are no spare LEDs for the comtest tasks, so this is just set to an\r
93 invalid number. */\r
94 #define mainCOMTEST_LED                 ( 4 )\r
95 \r
96 /* The baud rate used by the comtest task. */\r
97 #define mainBAUD_RATE                   ( 9600 )\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /* The implementation of the 'check' task as described at the top of this file. */\r
102 static void prvCheckTask( void *pvParameters );\r
103 \r
104 /* Just sets up the LED outputs.  Most generic setup is done in\r
105 __low_level_init(). */\r
106 static void prvSetupHardware( void );\r
107 \r
108 /* The RegTest functions as described at the top of this file. */\r
109 extern void vRegTest1( void *pvParameters );\r
110 extern void vRegTest2( void *pvParameters );\r
111 \r
112 /* A variable that will get set to fail if a RegTest task finds an error.  The\r
113 variable is inspected by the 'Check' task. */\r
114 static volatile long lRegTestStatus = pdPASS;\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 /* Create all the demo tasks then start the scheduler. */\r
119 void main( void )\r
120 {\r
121         /* Just sets up the LED outputs. */\r
122         prvSetupHardware();\r
123 \r
124         /* Standard demo tasks. */\r
125         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
126         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
127         vStartQueuePeekTasks();\r
128         \r
129         /* Create the check task as described at the top of this file. */\r
130         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, mainCHECK_PARAMETER, mainCHECK_TASK_PRIORITY, NULL );\r
131 \r
132         /* Create the RegTest tasks as described at the top of this file. */\r
133         xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
134         xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
135 \r
136         #ifdef __IAR_V850ES_Fx3__\r
137         {\r
138                 /* The extra IO required for the com test and led flashing tasks is only\r
139                 available on the application board, not the target boards. */   \r
140                 vAltStartComTestTasks( mainCOMTEST_PRIORITY, mainBAUD_RATE, mainCOMTEST_LED );\r
141                 vStartLEDFlashTasks( mainFLASH_PRIORITY );\r
142                 \r
143                 /* The Fx3 also has enough RAM to run loads more tasks. */\r
144                 vStartRecursiveMutexTasks();\r
145                 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
146                 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );                              \r
147         }\r
148         #endif  \r
149         \r
150         /* The suicide tasks must be created last as they need to know how many\r
151         tasks were running prior to their creation in order to ascertain whether\r
152         or not the correct/expected number of tasks are running at any given time. */\r
153     vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
154         \r
155         /* Start the scheduler. */\r
156         vTaskStartScheduler();\r
157 \r
158         /* If this line is reached then vTaskStartScheduler() returned because there\r
159         was insufficient heap memory remaining for the idle task to be created. */\r
160         for( ;; );\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 static void prvCheckTask( void *pvParameters )\r
165 {\r
166 TickType_t xDelayPeriod = mainNO_ERROR_DELAY, xLastWakeTime;\r
167 unsigned portBASE_TYPE uxLEDToUse = 0;\r
168 \r
169         /* Ensure parameter is passed in correctly. */\r
170         if( pvParameters != mainCHECK_PARAMETER )\r
171         {\r
172                 xDelayPeriod = mainERROR_DELAY;\r
173         }\r
174         \r
175         /* Initialise xLastWakeTime before it is used.  After this point it is not\r
176         written to directly. */\r
177         xLastWakeTime = xTaskGetTickCount();\r
178         \r
179         /* Cycle for ever, delaying then checking all the other tasks are still\r
180         operating without error. */\r
181         for( ;; )\r
182         {\r
183                 /* Wait until it is time to check all the other tasks again. */\r
184                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );\r
185                 \r
186                 if( lRegTestStatus != pdPASS )\r
187                 {\r
188                         xDelayPeriod = mainERROR_DELAY;\r
189                 }\r
190                 \r
191                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
192                 {\r
193                         xDelayPeriod = mainERROR_DELAY;\r
194                 }\r
195 \r
196                 if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
197                 {\r
198                         xDelayPeriod = mainERROR_DELAY;\r
199                 }\r
200 \r
201                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
202             {\r
203                 xDelayPeriod = mainERROR_DELAY;\r
204             }\r
205 \r
206                 if( xIsCreateTaskStillRunning() != pdTRUE )\r
207             {\r
208                 xDelayPeriod = mainERROR_DELAY;\r
209             }\r
210 \r
211                 /* The Fx3 runs more tasks, so more checks are performed. */            \r
212                 #ifdef __IAR_V850ES_Fx3__\r
213                 {\r
214                         if( xAreComTestTasksStillRunning() != pdTRUE )\r
215                         {\r
216                                 xDelayPeriod = mainERROR_DELAY;\r
217                         }\r
218                         \r
219                         if( xArePollingQueuesStillRunning() != pdTRUE )\r
220                         {\r
221                                 xDelayPeriod = mainERROR_DELAY;\r
222                         }\r
223 \r
224                         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
225                         {\r
226                                 xDelayPeriod = mainERROR_DELAY;\r
227                         }\r
228                         \r
229                         if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
230                         {\r
231                                 xDelayPeriod = mainERROR_DELAY;\r
232                         }               \r
233                         \r
234                         /* The application board has more LEDs and uses the flash tasks\r
235                         so the check task instead uses LED3 as LED3 is still spare. */\r
236                         uxLEDToUse = 3;\r
237                 }\r
238                 #endif\r
239 \r
240                 /* Toggle the LED.  The toggle rate will depend on whether or not an\r
241                 error has been found in any tasks. */\r
242                 vParTestToggleLED( uxLEDToUse );\r
243         }\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 static void prvSetupHardware( void )\r
248 {\r
249         /* Setup the LED outputs. */\r
250         vParTestInitialise();\r
251 \r
252         /* Any additional hardware configuration can be added here. */\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r
256 void vApplicationStackOverflowHook( void )\r
257 {\r
258         /* This will be called if a task overflows its stack.  pxCurrentTCB\r
259         can be inspected to see which is the offending task. */\r
260         for( ;; );\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 void vRegTestFailed( void )\r
265 {\r
266         /* Called by the RegTest tasks if an error is found.  lRegTestStatus is\r
267         inspected by the check task. */\r
268         lRegTestStatus = pdFAIL;\r
269         \r
270         /* Do not return from here as the reg test tasks clobber all registers so\r
271         function calls may not function correctly. */\r
272         for( ;; );\r
273 }\r