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