]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RL78_multiple_IAR/main_full.c
Minor updates and change version number for V7.5.0 release.
[freertos] / FreeRTOS / Demo / RL78_multiple_IAR / main_full.c
1 /*\r
2     FreeRTOS V7.5.0 - 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  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
67  * project, and a more comprehensive test and demo application.  The\r
68  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
69  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
70  * in main.c.  This file implements the comprehensive test and demo version.\r
71  *\r
72  * NOTE 2:  This file only contains the source code that is specific to the\r
73  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
74  * required to configure the hardware, along with an example of how to write an\r
75  * interrupt service routine, are defined in main.c.\r
76  ******************************************************************************\r
77  *\r
78  * main_full() creates all the demo application tasks and two software timers,\r
79  * then starts the scheduler.  The web documentation provides more details of\r
80  * the standard demo application tasks, which provide no particular\r
81  * functionality, but do provide a good example of how to use the FreeRTOS API.\r
82  *\r
83  * In addition to the standard demo tasks, the following tasks, tests and\r
84  * timers are created within this file:\r
85  *\r
86  * "Reg test" tasks - These fill the registers with known values, then check\r
87  * that each register still contains its expected value.  Each task uses a\r
88  * different set of values.  The reg test tasks execute with a very low priority,\r
89  * so get preempted very frequently.  A register containing an unexpected value\r
90  * is indicative of an error in the context switching mechanism.\r
91  *\r
92  * The "Demo" Timer and Callback Function:\r
93  * The demo timer callback function does nothing more than increment a variable.\r
94  * The period of the demo timer is set relative to the period of the check timer\r
95  * (described below).  This allows the check timer to know how many times the\r
96  * demo timer callback function should execute between each execution of the\r
97  * check timer callback function.  The variable incremented in the demo timer\r
98  * callback function is used to determine how many times the callback function\r
99  * has executed.\r
100  *\r
101  * The "Check" Timer and Callback Function:\r
102  * The check timer period is initially set to three seconds.  The check timer\r
103  * callback function checks that all the standard demo tasks, the reg test\r
104  * tasks, and the demo timer are not only still executing, but are executing\r
105  * without reporting any errors.  If the check timer discovers that a task or\r
106  * timer has stalled, or reported an error, then it changes its own period from\r
107  * the initial three seconds, to just 200ms.  The check timer callback function\r
108  * also toggles an LED each time it is called.  This provides a visual\r
109  * indication of the system status:  If the LED toggles every three seconds,\r
110  * then no issues have been discovered.  If the LED toggles every 200ms, then\r
111  * an issue has been discovered with at least one task.\r
112  *\r
113  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
114  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
115  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
116  *\r
117  */\r
118 \r
119 /* Scheduler include files. */\r
120 #include "FreeRTOS.h"\r
121 #include "task.h"\r
122 #include "timers.h"\r
123 \r
124 /* Standard demo includes. */\r
125 #include "dynamic.h"\r
126 #include "PollQ.h"\r
127 #include "blocktim.h"\r
128 \r
129 /* Hardware includes. */\r
130 #include "demo_specific_io.h"\r
131 \r
132 /* The period at which the check timer will expire, in ms, provided no errors\r
133 have been reported by any of the standard demo tasks.  ms are converted to the\r
134 equivalent in ticks using the portTICK_RATE_MS constant. */\r
135 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
136 \r
137 /* The period at which the check timer will expire, in ms, if an error has been\r
138 reported in one of the standard demo tasks, the check tasks, or the demo timer.\r
139 ms are converted to the equivalent in ticks using the portTICK_RATE_MS\r
140 constant. */\r
141 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_RATE_MS )\r
142 \r
143 /* These two definitions are used to set the period of the demo timer.  The demo\r
144 timer period is always relative to the check timer period, so the check timer\r
145 can determine if the demo timer has expired the expected number of times between\r
146 its own executions. */\r
147 #define mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT       ( 100UL )\r
148 #define mainDEMO_TIMER_PERIOD_MS                        ( mainCHECK_TIMER_PERIOD_MS / mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT )\r
149 \r
150 /* A block time of zero simply means "don't block". */\r
151 #define mainDONT_BLOCK                                          ( 0U )\r
152 \r
153 /* Values that are passed as parameters into the reg test tasks (purely to\r
154 ensure task parameters are passed correctly). */\r
155 #define mainREG_TEST_1_PARAMETER                        ( ( void * ) 0x1234 )\r
156 #define mainREG_TEST_2_PARAMETER                        ( ( void * ) 0x5678 )\r
157 \r
158 /*-----------------------------------------------------------*/\r
159 \r
160 /*\r
161  * The 'check' timer callback function, as described at the top of this file.\r
162  */\r
163 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
164 \r
165 /*\r
166  * The 'demo' timer callback function, as described at the top of this file.\r
167  */\r
168 static void prvDemoTimerCallback( xTimerHandle xTimer );\r
169 \r
170 /*\r
171  * Functions that define the RegTest tasks, as described at the top of this\r
172  * file.  The RegTest tasks are written (necessarily) in assembler.  Their\r
173  * entry points are written in C to allow for easy checking of the task\r
174  * parameter values.\r
175  */\r
176 extern void vRegTest1Task( void );\r
177 extern void vRegTest2Task( void );\r
178 static void prvRegTest1Entry( void *pvParameters );\r
179 static void prvRegTest2Entry( void *pvParameters );\r
180 \r
181 /*\r
182  * Called if a RegTest task discovers an error as a mechanism to stop the\r
183  * tasks loop counter incrementing (so the check task can detect that an\r
184  * error exists).\r
185  */\r
186 void vRegTestError( void );\r
187 \r
188 /*\r
189  * Called by main() to create the more comprehensive application if\r
190  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
191  */\r
192 void main_full( void );\r
193 \r
194 /*-----------------------------------------------------------*/\r
195 \r
196 /* Variables that are incremented on each cycle of the two reg tests to allow\r
197 the check timer to know that they are still executing. */\r
198 unsigned short usRegTest1LoopCounter = 0, usRegTest2LoopCounter;\r
199 \r
200 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
201 function. */\r
202 static xTimerHandle xCheckTimer = NULL;\r
203 \r
204 /* The demo timer.  This uses prvDemoTimerCallback() as its callback function. */\r
205 static xTimerHandle xDemoTimer = NULL;\r
206 \r
207 /* This variable is incremented each time the demo timer expires. */\r
208 static volatile unsigned long ulDemoSoftwareTimerCounter = 0UL;\r
209 \r
210 /*-----------------------------------------------------------*/\r
211 \r
212 void main_full( void )\r
213 {\r
214         /* Creates all the tasks and timers, then starts the scheduler. */\r
215 \r
216         /* First create the 'standard demo' tasks.  These are used to demonstrate\r
217         API functions being used and also to test the kernel port.  More information\r
218         is provided on the FreeRTOS.org WEB site. */\r
219         vStartDynamicPriorityTasks();\r
220         vStartPolledQueueTasks( tskIDLE_PRIORITY );\r
221         vCreateBlockTimeTasks();\r
222 \r
223         /* Create the RegTest tasks as described at the top of this file. */\r
224         xTaskCreate( prvRegTest1Entry,                          /* The function that implements the task. */\r
225                                  ( const signed char * ) "Reg1",/* Text name for the task - to assist debugging only, not used by the kernel. */\r
226                                  configMINIMAL_STACK_SIZE,              /* The size of the stack allocated to the task (in words, not bytes). */\r
227                                  mainREG_TEST_1_PARAMETER,      /* The parameter passed into the task. */\r
228                                  tskIDLE_PRIORITY,                              /* The priority at which the task will execute. */\r
229                                  NULL );                                                /* Used to pass the handle of the created task out to the function caller - not used in this case. */\r
230 \r
231         xTaskCreate( prvRegTest2Entry, ( const signed char * ) "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
232 \r
233         /* Create the software timer that performs the 'check' functionality,\r
234         as described at the top of this file. */\r
235         xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
236                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
237                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
238                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
239                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
240                                                           );\r
241 \r
242         /* Create the software timer that just increments a variable for demo\r
243         purposes. */\r
244         xDemoTimer = xTimerCreate( ( const signed char * ) "DemoTimer",/* A text name, purely to help debugging. */\r
245                                                                 ( 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
246                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
247                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
248                                                                 prvDemoTimerCallback                            /* The callback function that inspects the status of all the other tasks. */\r
249                                                           );\r
250 \r
251         /* Start both the check timer and the demo timer.  The timers won't actually\r
252         start until the scheduler is started. */\r
253         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
254         xTimerStart( xDemoTimer, mainDONT_BLOCK );\r
255 \r
256         /* Finally start the scheduler running. */\r
257         vTaskStartScheduler();\r
258 \r
259         /* If all is well execution will never reach here as the scheduler will be\r
260         running.  If this null loop is reached then it is likely there was\r
261         insufficient FreeRTOS heap available for the idle task and/or timer task to\r
262         be created.  See http://www.freertos.org/a00111.html. */\r
263         for( ;; );\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 static void prvDemoTimerCallback( xTimerHandle xTimer )\r
268 {\r
269         /* Remove compiler warning about unused parameter. */\r
270         ( void ) xTimer;\r
271 \r
272         /* The demo timer has expired.  All it does is increment a variable.  The\r
273         period of the demo timer is relative to that of the check timer, so the\r
274         check timer knows how many times this variable should have been incremented\r
275         between each execution of the check timer's own callback. */\r
276         ulDemoSoftwareTimerCounter++;\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
281 {\r
282 static portBASE_TYPE xChangedTimerPeriodAlready = pdFALSE, xErrorStatus = pdPASS;\r
283 static unsigned short usLastRegTest1Counter = 0, usLastRegTest2Counter = 0;\r
284 \r
285         /* Remove compiler warning about unused parameter. */\r
286         ( void ) xTimer;\r
287 \r
288         /* Inspect the status of the standard demo tasks. */\r
289         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
290         {\r
291                 xErrorStatus = pdFAIL;\r
292         }\r
293 \r
294         if( xArePollingQueuesStillRunning() != pdTRUE )\r
295         {\r
296                 xErrorStatus = pdFAIL;\r
297         }\r
298 \r
299         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
300         {\r
301                 xErrorStatus = pdFAIL;\r
302         }\r
303 \r
304         /* Indicate an error if either of the reg test loop counters have not\r
305         incremented since the last time this function was called. */\r
306         if( usLastRegTest1Counter == usRegTest1LoopCounter )\r
307         {\r
308                 xErrorStatus = pdFAIL;\r
309         }\r
310         else\r
311         {\r
312                 usLastRegTest1Counter = usRegTest1LoopCounter;\r
313         }\r
314 \r
315         if( usLastRegTest2Counter == usRegTest2LoopCounter )\r
316         {\r
317                 xErrorStatus = pdFAIL;\r
318         }\r
319         else\r
320         {\r
321                 usLastRegTest2Counter = usRegTest2LoopCounter;\r
322         }\r
323 \r
324         /* Ensure that the demo software timer has expired\r
325         mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT times in between\r
326         each call of this function.  A critical section is not required to access\r
327         ulDemoSoftwareTimerCounter as the variable is only accessed from another\r
328         software timer callback, and only one software timer callback can be\r
329         executing at any time. */\r
330         if( ( ulDemoSoftwareTimerCounter < ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT - 1 ) ) ||\r
331             ( ulDemoSoftwareTimerCounter > ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT + 1 ) )\r
332           )\r
333         {\r
334                 xErrorStatus = pdFAIL;\r
335         }\r
336         else\r
337         {\r
338                 ulDemoSoftwareTimerCounter = 0UL;\r
339         }\r
340 \r
341         if( ( xErrorStatus == pdFAIL ) && ( xChangedTimerPeriodAlready == pdFALSE ) )\r
342         {\r
343                 /* An error has occurred, but the timer's period has not yet been changed,\r
344                 change it now, and remember that it has been changed.  Shortening the\r
345                 timer's period means the LED will toggle at a faster rate, giving a\r
346                 visible indication that something has gone wrong. */\r
347                 xChangedTimerPeriodAlready = pdTRUE;\r
348 \r
349                 /* This call to xTimerChangePeriod() uses a zero block time.  Functions\r
350                 called from inside of a timer callback function must *never* attempt to\r
351                 block. */\r
352                 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
353         }\r
354 \r
355         /* Toggle the LED.  The toggle rate will depend on whether or not an error\r
356         has been found in any tasks. */\r
357         LED_BIT = !LED_BIT;\r
358 }\r
359 /*-----------------------------------------------------------*/\r
360 \r
361 void vRegTestError( void )\r
362 {\r
363         /* Called by both reg test tasks if an error is found.  There is no way out\r
364         of this function so the loop counter of the calling task will stop\r
365         incrementing, which will result in the check timer signaling an error. */\r
366         for( ;; );\r
367 }\r
368 /*-----------------------------------------------------------*/\r
369 \r
370 static void prvRegTest1Entry( void *pvParameters )\r
371 {\r
372         /* If the parameter has its expected value then start the first reg test\r
373         task (this is only done to test that the RTOS port is correctly handling\r
374         task parameters. */\r
375         if( pvParameters == mainREG_TEST_1_PARAMETER )\r
376         {\r
377                 vRegTest1Task();\r
378         }\r
379         else\r
380         {\r
381                 vRegTestError();\r
382         }\r
383 \r
384         /* It is not possible to get here as neither of the two functions called\r
385         above will ever return. */\r
386 }\r
387 /*-----------------------------------------------------------*/\r
388 \r
389 static void prvRegTest2Entry( void *pvParameters )\r
390 {\r
391         /* If the parameter has its expected value then start the first reg test\r
392         task (this is only done to test that the RTOS port is correctly handling\r
393         task parameters. */\r
394         if( pvParameters == mainREG_TEST_2_PARAMETER )\r
395         {\r
396                 vRegTest2Task();\r
397         }\r
398         else\r
399         {\r
400                 vRegTestError();\r
401         }\r
402 \r
403         /* It is not possible to get here as neither of the two functions called\r
404         above will ever return. */\r
405 }\r
406 /*-----------------------------------------------------------*/\r
407 \r