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