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