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