]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RL78_multiple_IAR/main_full.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / RL78_multiple_IAR / main_full.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /******************************************************************************\r
71  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
72  * project, and a more comprehensive test and demo application.  The\r
73  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
74  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
75  * in main.c.  This file implements the comprehensive test and demo version.\r
76  *\r
77  * NOTE 2:  This file only contains the source code that is specific to the\r
78  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
79  * required to configure the hardware, along with an example of how to write an\r
80  * interrupt service routine, are defined in main.c.\r
81  ******************************************************************************\r
82  *\r
83  * main_full() creates all the demo application tasks and two software timers,\r
84  * then starts the scheduler.  The web documentation provides more details of\r
85  * the standard demo application tasks, which provide no particular\r
86  * functionality, but do provide a good example of how to use the FreeRTOS API.\r
87  *\r
88  * In addition to the standard demo tasks, the following tasks, tests and\r
89  * timers are created within this file:\r
90  *\r
91  * "Reg test" tasks - These fill the registers with known values, then check\r
92  * that each register still contains its expected value.  Each task uses a\r
93  * different set of values.  The reg test tasks execute with a very low priority,\r
94  * so get preempted very frequently.  A register containing an unexpected value\r
95  * is indicative of an error in the context switching mechanism.\r
96  *\r
97  * The "Demo" Timer and Callback Function:\r
98  * The demo timer callback function does nothing more than increment a variable.\r
99  * The period of the demo timer is set relative to the period of the check timer\r
100  * (described below).  This allows the check timer to know how many times the\r
101  * demo timer callback function should execute between each execution of the\r
102  * check timer callback function.  The variable incremented in the demo timer\r
103  * callback function is used to determine how many times the callback function\r
104  * has executed.\r
105  *\r
106  * The "Check" Timer and Callback Function:\r
107  * The check timer period is initially set to three seconds.  The check timer\r
108  * callback function checks that all the standard demo tasks, the reg test\r
109  * tasks, and the demo timer are not only still executing, but are executing\r
110  * without reporting any errors.  If the check timer discovers that a task or\r
111  * timer has stalled, or reported an error, then it changes its own period from\r
112  * the initial three seconds, to just 200ms.  The check timer callback function\r
113  * also toggles an LED each time it is called.  This provides a visual\r
114  * indication of the system status:  If the LED toggles every three seconds,\r
115  * then no issues have been discovered.  If the LED toggles every 200ms, then\r
116  * an issue has been discovered with at least one task.\r
117  *\r
118  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
119  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
120  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
121  *\r
122  */\r
123 \r
124 /* Scheduler include files. */\r
125 #include "FreeRTOS.h"\r
126 #include "task.h"\r
127 #include "timers.h"\r
128 \r
129 /* Standard demo includes. */\r
130 #include "dynamic.h"\r
131 #include "PollQ.h"\r
132 #include "blocktim.h"\r
133 \r
134 /* Hardware includes. */\r
135 #include "demo_specific_io.h"\r
136 \r
137 /* The period at which the check timer will expire, in ms, provided no errors\r
138 have been reported by any of the standard demo tasks.  ms are converted to the\r
139 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
140 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
141 \r
142 /* The period at which the check timer will expire, in ms, if an error has been\r
143 reported in one of the standard demo tasks, the check tasks, or the demo timer.\r
144 ms are converted to the equivalent in ticks using the portTICK_PERIOD_MS\r
145 constant. */\r
146 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
147 \r
148 /* These two definitions are used to set the period of the demo timer.  The demo\r
149 timer period is always relative to the check timer period, so the check timer\r
150 can determine if the demo timer has expired the expected number of times between\r
151 its own executions. */\r
152 #define mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT       ( 100UL )\r
153 #define mainDEMO_TIMER_PERIOD_MS                        ( mainCHECK_TIMER_PERIOD_MS / mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT )\r
154 \r
155 /* A block time of zero simply means "don't block". */\r
156 #define mainDONT_BLOCK                                          ( 0U )\r
157 \r
158 /* Values that are passed as parameters into the reg test tasks (purely to\r
159 ensure task parameters are passed correctly). */\r
160 #define mainREG_TEST_1_PARAMETER                        ( ( void * ) 0x1234 )\r
161 #define mainREG_TEST_2_PARAMETER                        ( ( void * ) 0x5678 )\r
162 \r
163 /*-----------------------------------------------------------*/\r
164 \r
165 /*\r
166  * The 'check' timer callback function, as described at the top of this file.\r
167  */\r
168 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
169 \r
170 /*\r
171  * The 'demo' timer callback function, as described at the top of this file.\r
172  */\r
173 static void prvDemoTimerCallback( TimerHandle_t xTimer );\r
174 \r
175 /*\r
176  * Functions that define the RegTest tasks, as described at the top of this\r
177  * file.  The RegTest tasks are written (necessarily) in assembler.  Their\r
178  * entry points are written in C to allow for easy checking of the task\r
179  * parameter values.\r
180  */\r
181 extern void vRegTest1Task( void );\r
182 extern void vRegTest2Task( void );\r
183 static void prvRegTest1Entry( void *pvParameters );\r
184 static void prvRegTest2Entry( void *pvParameters );\r
185 \r
186 /*\r
187  * Called if a RegTest task discovers an error as a mechanism to stop the\r
188  * tasks loop counter incrementing (so the check task can detect that an\r
189  * error exists).\r
190  */\r
191 void vRegTestError( void );\r
192 \r
193 /*\r
194  * Called by main() to create the more comprehensive application if\r
195  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
196  */\r
197 void main_full( void );\r
198 \r
199 /*-----------------------------------------------------------*/\r
200 \r
201 /* Variables that are incremented on each cycle of the two reg tests to allow\r
202 the check timer to know that they are still executing. */\r
203 unsigned short usRegTest1LoopCounter = 0, usRegTest2LoopCounter;\r
204 \r
205 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
206 function. */\r
207 static TimerHandle_t xCheckTimer = NULL;\r
208 \r
209 /* The demo timer.  This uses prvDemoTimerCallback() as its callback function. */\r
210 static TimerHandle_t xDemoTimer = NULL;\r
211 \r
212 /* This variable is incremented each time the demo timer expires. */\r
213 static volatile unsigned long ulDemoSoftwareTimerCounter = 0UL;\r
214 \r
215 /*-----------------------------------------------------------*/\r
216 \r
217 void main_full( void )\r
218 {\r
219         /* Creates all the tasks and timers, then starts the scheduler. */\r
220 \r
221         /* First create the 'standard demo' tasks.  These are used to demonstrate\r
222         API functions being used and also to test the kernel port.  More information\r
223         is provided on the FreeRTOS.org WEB site. */\r
224         vStartDynamicPriorityTasks();\r
225         vStartPolledQueueTasks( tskIDLE_PRIORITY );\r
226         vCreateBlockTimeTasks();\r
227 \r
228         /* Create the RegTest tasks as described at the top of this file. */\r
229         xTaskCreate( prvRegTest1Entry,                          /* The function that implements the task. */\r
230                                  "Reg1",                                                /* Text name for the task - to assist debugging only, not used by the kernel. */\r
231                                  configMINIMAL_STACK_SIZE,              /* The size of the stack allocated to the task (in words, not bytes). */\r
232                                  mainREG_TEST_1_PARAMETER,      /* The parameter passed into the task. */\r
233                                  tskIDLE_PRIORITY,                              /* The priority at which the task will execute. */\r
234                                  NULL );                                                /* Used to pass the handle of the created task out to the function caller - not used in this case. */\r
235 \r
236         xTaskCreate( prvRegTest2Entry, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
237 \r
238         /* Create the software timer that performs the 'check' functionality,\r
239         as described at the top of this file. */\r
240         xCheckTimer = xTimerCreate( "CheckTimer",                                       /* A text name, purely to help debugging. */\r
241                                                                 ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */\r
242                                                                 pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
243                                                                 ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
244                                                                 prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */\r
245                                                           );\r
246 \r
247         /* Create the software timer that just increments a variable for demo\r
248         purposes. */\r
249         xDemoTimer = xTimerCreate(  "DemoTimer",/* A text name, purely to help debugging. */\r
250                                                                 ( mainDEMO_TIMER_PERIOD_MS ),           /* The timer period, in this case it is always calculated relative to the check timer period (see the definition of mainDEMO_TIMER_PERIOD_MS). */\r
251                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
252                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
253                                                                 prvDemoTimerCallback                            /* The callback function that inspects the status of all the other tasks. */\r
254                                                           );\r
255 \r
256         /* Start both the check timer and the demo timer.  The timers won't actually\r
257         start until the scheduler is started. */\r
258         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
259         xTimerStart( xDemoTimer, mainDONT_BLOCK );\r
260 \r
261         /* Finally start the scheduler running. */\r
262         vTaskStartScheduler();\r
263 \r
264         /* If all is well execution will never reach here as the scheduler will be\r
265         running.  If this null loop is reached then it is likely there was\r
266         insufficient FreeRTOS heap available for the idle task and/or timer task to\r
267         be created.  See http://www.freertos.org/a00111.html. */\r
268         for( ;; );\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 static void prvDemoTimerCallback( TimerHandle_t xTimer )\r
273 {\r
274         /* Remove compiler warning about unused parameter. */\r
275         ( void ) xTimer;\r
276 \r
277         /* The demo timer has expired.  All it does is increment a variable.  The\r
278         period of the demo timer is relative to that of the check timer, so the\r
279         check timer knows how many times this variable should have been incremented\r
280         between each execution of the check timer's own callback. */\r
281         ulDemoSoftwareTimerCounter++;\r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
286 {\r
287 static portBASE_TYPE xChangedTimerPeriodAlready = pdFALSE, xErrorStatus = pdPASS;\r
288 static unsigned short usLastRegTest1Counter = 0, usLastRegTest2Counter = 0;\r
289 \r
290         /* Remove compiler warning about unused parameter. */\r
291         ( void ) xTimer;\r
292 \r
293         /* Inspect the status of the standard demo tasks. */\r
294         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
295         {\r
296                 xErrorStatus = pdFAIL;\r
297         }\r
298 \r
299         if( xArePollingQueuesStillRunning() != pdTRUE )\r
300         {\r
301                 xErrorStatus = pdFAIL;\r
302         }\r
303 \r
304         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
305         {\r
306                 xErrorStatus = pdFAIL;\r
307         }\r
308 \r
309         /* Indicate an error if either of the reg test loop counters have not\r
310         incremented since the last time this function was called. */\r
311         if( usLastRegTest1Counter == usRegTest1LoopCounter )\r
312         {\r
313                 xErrorStatus = pdFAIL;\r
314         }\r
315         else\r
316         {\r
317                 usLastRegTest1Counter = usRegTest1LoopCounter;\r
318         }\r
319 \r
320         if( usLastRegTest2Counter == usRegTest2LoopCounter )\r
321         {\r
322                 xErrorStatus = pdFAIL;\r
323         }\r
324         else\r
325         {\r
326                 usLastRegTest2Counter = usRegTest2LoopCounter;\r
327         }\r
328 \r
329         /* Ensure that the demo software timer has expired\r
330         mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT times in between\r
331         each call of this function.  A critical section is not required to access\r
332         ulDemoSoftwareTimerCounter as the variable is only accessed from another\r
333         software timer callback, and only one software timer callback can be\r
334         executing at any time. */\r
335         if( ( ulDemoSoftwareTimerCounter < ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT - 1 ) ) ||\r
336             ( ulDemoSoftwareTimerCounter > ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT + 1 ) )\r
337           )\r
338         {\r
339                 xErrorStatus = pdFAIL;\r
340         }\r
341         else\r
342         {\r
343                 ulDemoSoftwareTimerCounter = 0UL;\r
344         }\r
345 \r
346         if( ( xErrorStatus == pdFAIL ) && ( xChangedTimerPeriodAlready == pdFALSE ) )\r
347         {\r
348                 /* An error has occurred, but the timer's period has not yet been changed,\r
349                 change it now, and remember that it has been changed.  Shortening the\r
350                 timer's period means the LED will toggle at a faster rate, giving a\r
351                 visible indication that something has gone wrong. */\r
352                 xChangedTimerPeriodAlready = pdTRUE;\r
353 \r
354                 /* This call to xTimerChangePeriod() uses a zero block time.  Functions\r
355                 called from inside of a timer callback function must *never* attempt to\r
356                 block. */\r
357                 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
358         }\r
359 \r
360         /* Toggle the LED.  The toggle rate will depend on whether or not an error\r
361         has been found in any tasks. */\r
362         LED_BIT = !LED_BIT;\r
363 }\r
364 /*-----------------------------------------------------------*/\r
365 \r
366 void vRegTestError( void )\r
367 {\r
368         /* Called by both reg test tasks if an error is found.  There is no way out\r
369         of this function so the loop counter of the calling task will stop\r
370         incrementing, which will result in the check timer signaling an error. */\r
371         for( ;; );\r
372 }\r
373 /*-----------------------------------------------------------*/\r
374 \r
375 static void prvRegTest1Entry( void *pvParameters )\r
376 {\r
377         /* If the parameter has its expected value then start the first reg test\r
378         task (this is only done to test that the RTOS port is correctly handling\r
379         task parameters. */\r
380         if( pvParameters == mainREG_TEST_1_PARAMETER )\r
381         {\r
382                 vRegTest1Task();\r
383         }\r
384         else\r
385         {\r
386                 vRegTestError();\r
387         }\r
388 \r
389         /* It is not possible to get here as neither of the two functions called\r
390         above will ever return. */\r
391 }\r
392 /*-----------------------------------------------------------*/\r
393 \r
394 static void prvRegTest2Entry( void *pvParameters )\r
395 {\r
396         /* If the parameter has its expected value then start the first reg test\r
397         task (this is only done to test that the RTOS port is correctly handling\r
398         task parameters. */\r
399         if( pvParameters == mainREG_TEST_2_PARAMETER )\r
400         {\r
401                 vRegTest2Task();\r
402         }\r
403         else\r
404         {\r
405                 vRegTestError();\r
406         }\r
407 \r
408         /* It is not possible to get here as neither of the two functions called\r
409         above will ever return. */\r
410 }\r
411 /*-----------------------------------------------------------*/\r
412 \r