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