]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/NEC_V850ES_IAR/main.c
01563784d282e5c7f977c319b7ce7d1c4bbe1bf2
[freertos] / FreeRTOS / Demo / NEC_V850ES_IAR / main.c
1 /*\r
2     FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*\r
66  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
67  * documentation provides more details of the standard demo application tasks.\r
68  * In addition to the standard demo tasks, the following tasks and tests are\r
69  * defined and/or created within this file:\r
70  *\r
71  * "Check" task -  This only executes every three seconds but has a high priority\r
72  * to ensure it gets processor time.  Its main function is to check that all the\r
73  * standard demo tasks are still operational.  If everything is running as\r
74  * expected then the check task will toggle an LED every 3 seconds.  An error\r
75  * being discovered in any task will cause the toggle rate to increase to 500ms.\r
76  *\r
77  * "Reg test" tasks - These fill the registers with known values, then check\r
78  * that each register still contains its expected value.  Each task uses\r
79  * different values.  The tasks run with very low priority so get preempted very\r
80  * frequently.  A register containing an unexpected value is indicative of an\r
81  * error in the context switching mechanism.\r
82  *\r
83  */\r
84 \r
85 /* Standard include files. */\r
86 #include <stdlib.h>\r
87 #include <string.h>\r
88 \r
89 /* Scheduler include files. */\r
90 #include "FreeRTOS.h"\r
91 #include "task.h"\r
92 \r
93 /* Demo file headers. */\r
94 #include <intrinsics.h>\r
95 #include "BlockQ.h"\r
96 #include "death.h"\r
97 #include "flash.h"\r
98 #include "partest.h"\r
99 #include "semtest.h"\r
100 #include "PollQ.h"\r
101 #include "GenQTest.h"\r
102 #include "QPeek.h"\r
103 #include "recmutex.h"\r
104 #include "comtest2.h"\r
105 \r
106 /*\r
107  * Priority definitions for most of the tasks in the demo application.  Some\r
108  * tasks just use the idle priority.\r
109  */\r
110 #define mainFLASH_PRIORITY                                      ( tskIDLE_PRIORITY + 1 )\r
111 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
112 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 3 )\r
113 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
114 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
115 #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
116 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
117 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
118 #define mainCOMTEST_PRIORITY                            ( tskIDLE_PRIORITY + 1 )\r
119 \r
120 /* Passed into the check task just as a test that the parameter passing\r
121 mechanism is working correctly. */\r
122 #define mainCHECK_PARAMETER                                     ( ( void * ) 0x12345678 )\r
123 \r
124 /* The period between executions of the check task. */\r
125 #define mainNO_ERROR_DELAY              ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
126 #define mainERROR_DELAY                 ( ( portTickType ) 500 / portTICK_RATE_MS )\r
127 \r
128 /* There are no spare LEDs for the comtest tasks, so this is just set to an\r
129 invalid number. */\r
130 #define mainCOMTEST_LED                 ( 4 )\r
131 \r
132 /* The baud rate used by the comtest task. */\r
133 #define mainBAUD_RATE                   ( 9600 )\r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 /* The implementation of the 'check' task as described at the top of this file. */\r
138 static void prvCheckTask( void *pvParameters );\r
139 \r
140 /* Just sets up the LED outputs.  Most generic setup is done in\r
141 __low_level_init(). */\r
142 static void prvSetupHardware( void );\r
143 \r
144 /* The RegTest functions as described at the top of this file. */\r
145 extern void vRegTest1( void *pvParameters );\r
146 extern void vRegTest2( void *pvParameters );\r
147 \r
148 /* A variable that will get set to fail if a RegTest task finds an error.  The\r
149 variable is inspected by the 'Check' task. */\r
150 static volatile portLONG lRegTestStatus = pdPASS;\r
151 \r
152 /*-----------------------------------------------------------*/\r
153 \r
154 /* Create all the demo tasks then start the scheduler. */\r
155 void main( void )\r
156 {\r
157         /* Just sets up the LED outputs. */\r
158         prvSetupHardware();\r
159 \r
160         /* Standard demo tasks. */\r
161         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
162         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
163         vStartQueuePeekTasks();\r
164         \r
165         /* Create the check task as described at the top of this file. */\r
166         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, mainCHECK_PARAMETER, mainCHECK_TASK_PRIORITY, NULL );\r
167 \r
168         /* Create the RegTest tasks as described at the top of this file. */\r
169         xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
170         xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
171 \r
172         #ifdef __IAR_V850ES_Fx3__\r
173         {\r
174                 /* The extra IO required for the com test and led flashing tasks is only\r
175                 available on the application board, not the target boards. */   \r
176                 vAltStartComTestTasks( mainCOMTEST_PRIORITY, mainBAUD_RATE, mainCOMTEST_LED );\r
177                 vStartLEDFlashTasks( mainFLASH_PRIORITY );\r
178                 \r
179                 /* The Fx3 also has enough RAM to run loads more tasks. */\r
180                 vStartRecursiveMutexTasks();\r
181                 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
182                 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );                              \r
183         }\r
184         #endif  \r
185         \r
186         /* The suicide tasks must be created last as they need to know how many\r
187         tasks were running prior to their creation in order to ascertain whether\r
188         or not the correct/expected number of tasks are running at any given time. */\r
189     vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
190         \r
191         /* Start the scheduler. */\r
192         vTaskStartScheduler();\r
193 \r
194         /* If this line is reached then vTaskStartScheduler() returned because there\r
195         was insufficient heap memory remaining for the idle task to be created. */\r
196         for( ;; );\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 static void prvCheckTask( void *pvParameters )\r
201 {\r
202 portTickType xDelayPeriod = mainNO_ERROR_DELAY, xLastWakeTime;\r
203 unsigned portBASE_TYPE uxLEDToUse = 0;\r
204 \r
205         /* Ensure parameter is passed in correctly. */\r
206         if( pvParameters != mainCHECK_PARAMETER )\r
207         {\r
208                 xDelayPeriod = mainERROR_DELAY;\r
209         }\r
210         \r
211         /* Initialise xLastWakeTime before it is used.  After this point it is not\r
212         written to directly. */\r
213         xLastWakeTime = xTaskGetTickCount();\r
214         \r
215         /* Cycle for ever, delaying then checking all the other tasks are still\r
216         operating without error. */\r
217         for( ;; )\r
218         {\r
219                 /* Wait until it is time to check all the other tasks again. */\r
220                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );\r
221                 \r
222                 if( lRegTestStatus != pdPASS )\r
223                 {\r
224                         xDelayPeriod = mainERROR_DELAY;\r
225                 }\r
226                 \r
227                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
228                 {\r
229                         xDelayPeriod = mainERROR_DELAY;\r
230                 }\r
231 \r
232                 if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
233                 {\r
234                         xDelayPeriod = mainERROR_DELAY;\r
235                 }\r
236 \r
237                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
238             {\r
239                 xDelayPeriod = mainERROR_DELAY;\r
240             }\r
241 \r
242                 if( xIsCreateTaskStillRunning() != pdTRUE )\r
243             {\r
244                 xDelayPeriod = mainERROR_DELAY;\r
245             }\r
246 \r
247                 /* The Fx3 runs more tasks, so more checks are performed. */            \r
248                 #ifdef __IAR_V850ES_Fx3__\r
249                 {\r
250                         if( xAreComTestTasksStillRunning() != pdTRUE )\r
251                         {\r
252                                 xDelayPeriod = mainERROR_DELAY;\r
253                         }\r
254                         \r
255                         if( xArePollingQueuesStillRunning() != pdTRUE )\r
256                         {\r
257                                 xDelayPeriod = mainERROR_DELAY;\r
258                         }\r
259 \r
260                         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
261                         {\r
262                                 xDelayPeriod = mainERROR_DELAY;\r
263                         }\r
264                         \r
265                         if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
266                         {\r
267                                 xDelayPeriod = mainERROR_DELAY;\r
268                         }               \r
269                         \r
270                         /* The application board has more LEDs and uses the flash tasks\r
271                         so the check task instead uses LED3 as LED3 is still spare. */\r
272                         uxLEDToUse = 3;\r
273                 }\r
274                 #endif\r
275 \r
276                 /* Toggle the LED.  The toggle rate will depend on whether or not an\r
277                 error has been found in any tasks. */\r
278                 vParTestToggleLED( uxLEDToUse );\r
279         }\r
280 }\r
281 /*-----------------------------------------------------------*/\r
282 \r
283 static void prvSetupHardware( void )\r
284 {\r
285         /* Setup the LED outputs. */\r
286         vParTestInitialise();\r
287 \r
288         /* Any additional hardware configuration can be added here. */\r
289 }\r
290 /*-----------------------------------------------------------*/\r
291 \r
292 void vApplicationStackOverflowHook( void )\r
293 {\r
294         /* This will be called if a task overflows its stack.  pxCurrentTCB\r
295         can be inspected to see which is the offending task. */\r
296         for( ;; );\r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 void vRegTestFailed( void )\r
301 {\r
302         /* Called by the RegTest tasks if an error is found.  lRegTestStatus is\r
303         inspected by the check task. */\r
304         lRegTestStatus = pdFAIL;\r
305         \r
306         /* Do not return from here as the reg test tasks clobber all registers so\r
307         function calls may not function correctly. */\r
308         for( ;; );\r
309 }\r