]> begriffs open source - freertos/blob - Demo/RL78_RL78G13_Promo_Board_IAR/main.c
Correct the prototype of the function call vApplicationStackOverflowHook().
[freertos] / Demo / RL78_RL78G13_Promo_Board_IAR / main.c
1 /*\r
2     FreeRTOS V7.0.2 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  *\r
56  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
57  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
58  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
59  *\r
60  *\r
61  * main() creates the demo application tasks and timers, then starts the\r
62  * scheduler.\r
63  *\r
64  * This demo is configured to run on the RL78/G13 Promotion Board, which is\r
65  * fitted with a R5F100LEA microcontroller.  The R5F100LEA contains a little\r
66  * under 4K bytes of usable internal RAM.  The RAM size restricts the number of\r
67  * demo tasks that can be created, and the demo creates 13 tasks, 4 queues and\r
68  * two timers.  The RL78 range does however include parts with up to 32K bytes\r
69  * of RAM (at the time of writing).  Using FreeRTOS on such a part will allow an\r
70  * application to make a more comprehensive use of FreeRTOS tasks, and other\r
71  * FreeRTOS features.\r
72  *\r
73  * In addition to the standard demo tasks, the following tasks, tests and timers\r
74  * are created within this file:\r
75  *\r
76  * "Reg test" tasks - These fill the registers with known values, then check\r
77  * that each register still contains its expected value.  Each task uses a\r
78  * different set of values.  The reg test tasks execute with a very low priority,\r
79  * so get preempted very frequently.  A register containing an unexpected value\r
80  * is indicative of an error in the context switching mechanism.\r
81  *\r
82  * The "Demo" Timer and Callback Function:\r
83  * The demo timer callback function does nothing more than increment a variable.\r
84  * The period of the demo timer is set relative to the period of the check timer\r
85  * (described below).  This allows the check timer to know how many times the\r
86  * demo timer callback function should execute between each execution of the\r
87  * check timer callback function.  The variable incremented in the demo timer\r
88  * callback function is used to determine how many times the callback function\r
89  * has executed.\r
90  *\r
91  * The "Check" Timer and Callback Function:\r
92  * The check timer period is initially set to three seconds.  The check timer\r
93  * callback function checks that all the standard demo tasks, the reg test tasks,\r
94  * and the demo timer are not only still executing, but are executing without\r
95  * reporting any errors.  If the check timer discovers that a task or timer has\r
96  * stalled, or reported an error, then it changes its own period from the\r
97  * initial three seconds, to just 200ms.  The check timer callback function also\r
98  * toggles the user LED each time it is called.  This provides a visual\r
99  * indication of the system status:  If the LED toggles every three seconds,\r
100  * then no issues have been discovered.  If the LED toggles every 200ms, then an\r
101  * issue has been discovered with at least one task.\r
102  *\r
103  */\r
104 \r
105 /* Scheduler include files. */\r
106 #include "FreeRTOS.h"\r
107 #include "task.h"\r
108 #include "timers.h"\r
109 \r
110 /* Standard demo includes. */\r
111 #include "dynamic.h"\r
112 #include "PollQ.h"\r
113 #include "blocktim.h"\r
114 \r
115 /* The period at which the check timer will expire, in ms, provided no errors\r
116 have been reported by any of the standard demo tasks.  ms are converted to the\r
117 equivalent in ticks using the portTICK_RATE_MS constant. */\r
118 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
119 \r
120 /* The period at which the check timer will expire, in ms, if an error has been\r
121 reported in one of the standard demo tasks, the check tasks, or the demo timer.\r
122 ms are converted to the equivalent in ticks using the portTICK_RATE_MS\r
123 constant. */\r
124 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_RATE_MS )\r
125 \r
126 /* These two definitions are used to set the period of the demo timer.  The demo\r
127 timer period is always relative to the check timer period, so the check timer\r
128 can determine if the demo timer has expired the expected number of times between\r
129 its own executions. */\r
130 #define mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT       ( 100UL )\r
131 #define mainDEMO_TIMER_PERIOD_MS                        ( mainCHECK_TIMER_PERIOD_MS / mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT )\r
132 \r
133 /* The LED toggled by the check timer. */\r
134 #define mainLED_0                                               P7_bit.no7\r
135 \r
136 /* A block time of zero simple means "don't block". */\r
137 #define mainDONT_BLOCK                                          ( 0U )\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 /*\r
142  * The 'check' timer callback function, as described at the top of this file.\r
143  */\r
144 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
145 \r
146 /*\r
147  * The 'demo' timer callback function, as described at the top of this file.\r
148  */\r
149 static void prvDemoTimerCallback( xTimerHandle xTimer );\r
150 \r
151 /*\r
152  * This function is called from the C startup routine to setup the processor -\r
153  * in particular the clock source.\r
154  */\r
155 int __low_level_init(void);\r
156 \r
157 /*\r
158  * Functions that define the RegTest tasks, as described at the top of this file.\r
159  */\r
160 extern void vRegTest1( void *pvParameters );\r
161 extern void vRegTest2( void *pvParameters );\r
162 \r
163 \r
164 /*-----------------------------------------------------------*/\r
165 \r
166 /* If an error is discovered by one of the RegTest tasks then this flag is set\r
167 to pdFAIL.  The 'check' timer then inspects this flag to detect errors within\r
168 the RegTest tasks. */\r
169 static short sRegTestStatus = pdPASS;\r
170 \r
171 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
172 function. */\r
173 static xTimerHandle xCheckTimer = NULL;\r
174 \r
175 /* The demo timer.  This uses prvDemoTimerCallback() as its callback function. */\r
176 static xTimerHandle xDemoTimer = NULL;\r
177 \r
178 /* This variable is incremented each time the demo timer expires. */\r
179 static volatile unsigned long ulDemoSoftwareTimerCounter = 0UL;\r
180 \r
181 /* RL78/G13 Option Byte Definition. Watchdog disabled, LVI enabled, OCD interface\r
182 enabled. */\r
183 __root __far const unsigned char OptionByte[] @ 0x00C0 =\r
184 {\r
185         WATCHDOG_DISABLED, LVI_ENABLED, RESERVED_FF, OCD_ENABLED\r
186 };\r
187 \r
188 /* Security byte definition */\r
189 __root __far const unsigned char SecuIDCode[]  @ 0x00C4 =\r
190 {\r
191         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x54\r
192 };\r
193 \r
194 /*-----------------------------------------------------------*/\r
195 \r
196 short main( void )\r
197 {\r
198         /* Creates all the tasks and timers, then starts the scheduler. */\r
199 \r
200         /* First create the 'standard demo' tasks.  These are used to demonstrate\r
201         API functions being used and also to test the kernel port.  More information\r
202         is provided on the FreeRTOS.org WEB site. */\r
203         vStartDynamicPriorityTasks();\r
204         vStartPolledQueueTasks( tskIDLE_PRIORITY );\r
205         vCreateBlockTimeTasks();\r
206 \r
207         /* Create the RegTest tasks as described at the top of this file. */\r
208         xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
209         xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );      \r
210 \r
211         /* Create the software timer that performs the 'check' functionality,\r
212         as described at the top of this file. */\r
213         xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
214                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
215                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
216                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
217                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
218                                                           );\r
219                                                         \r
220         /* Create the software timer that just increments a variable for demo\r
221         purposes. */\r
222         xDemoTimer = xTimerCreate( ( const signed char * ) "DemoTimer",/* A text name, purely to help debugging. */\r
223                                                                 ( 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
224                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
225                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
226                                                                 prvDemoTimerCallback                            /* The callback function that inspects the status of all the other tasks. */\r
227                                                           );\r
228         \r
229         /* Start both the check timer and the demo timer.  The timers won't actually\r
230         start until the scheduler is started. */\r
231         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
232         xTimerStart( xDemoTimer, mainDONT_BLOCK );\r
233         \r
234         /* Finally start the scheduler running. */\r
235         vTaskStartScheduler();\r
236 \r
237         /* If this line is reached then vTaskStartScheduler() returned because there\r
238         was insufficient heap memory remaining for the idle task to be created. */\r
239         for( ;; );\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 static void prvDemoTimerCallback( xTimerHandle xTimer )\r
244 {\r
245         /* The demo timer has expired.  All it does is increment a variable.  The\r
246         period of the demo timer is relative to that of the check timer, so the\r
247         check timer knows how many times this variable should have been incremented\r
248         between each execution of the check timer's own callback. */\r
249         ulDemoSoftwareTimerCounter++;\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
254 {\r
255 static portBASE_TYPE xChangedTimerPeriodAlready = pdFALSE, xErrorStatus = pdPASS;\r
256 \r
257         /* Inspect the status of the standard demo tasks. */\r
258         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
259         {\r
260                 xErrorStatus = pdFAIL;\r
261         }\r
262         \r
263         if( xArePollingQueuesStillRunning() != pdTRUE )\r
264         {\r
265                 xErrorStatus = pdFAIL;\r
266         }\r
267         \r
268         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
269         {\r
270                 xErrorStatus = pdFAIL;\r
271         }\r
272 \r
273         /* Inspect the status of the reg test tasks. */\r
274         if( sRegTestStatus != pdPASS )\r
275         {\r
276                 xErrorStatus = pdFAIL;\r
277         }\r
278         \r
279         /* Ensure that the demo software timer has expired\r
280         mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT times in between\r
281         each call of this function.  A critical section is not required to access\r
282         ulDemoSoftwareTimerCounter as the variable is only accessed from another\r
283         software timer callback, and only one software timer callback can be\r
284         executing at any time. */\r
285         if( ( ulDemoSoftwareTimerCounter < ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT - 1 ) ) ||\r
286             ( ulDemoSoftwareTimerCounter > ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT + 1 ) )\r
287           )\r
288         {\r
289                 xErrorStatus = pdFAIL;\r
290         }\r
291         else\r
292         {\r
293                 ulDemoSoftwareTimerCounter = 0UL;\r
294         }\r
295         \r
296         if( ( xErrorStatus == pdFAIL ) && ( xChangedTimerPeriodAlready == pdFALSE ) )\r
297         {\r
298                 /* An error has occurred, but the timer's period has not yet been changed,\r
299                 change it now, and remember that it has been changed.  Shortening the\r
300                 timer's period means the LED will toggle at a faster rate, giving a\r
301                 visible indication that something has gone wrong. */\r
302                 xChangedTimerPeriodAlready = pdTRUE;\r
303                         \r
304                 /* This call to xTimerChangePeriod() uses a zero block time.  Functions\r
305                 called from inside of a timer callback function must *never* attempt to\r
306                 block. */\r
307                 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
308         }\r
309         \r
310         /* Toggle the LED.  The toggle rate will depend on whether or not an error\r
311         has been found in any tasks. */\r
312         mainLED_0 = !mainLED_0;\r
313 }\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 int __low_level_init(void)\r
317 {\r
318 unsigned portCHAR ucResetFlag = RESF;\r
319 \r
320         portDISABLE_INTERRUPTS();\r
321 \r
322         /* Clock Configuration:\r
323         In this port, to use the internal high speed clock source of the\r
324         microcontroller, define the configCLOCK_SOURCE as 1 in FreeRTOSConfig.h.  To\r
325         use an external clock define configCLOCK_SOURCE as 0. */\r
326         #if configCLOCK_SOURCE == 1\r
327         {\r
328                 /* Set fMX */\r
329                 CMC = 0x00;\r
330                 MSTOP = 1U;\r
331                 \r
332                 /* Set fMAIN */\r
333                 MCM0 = 0U;\r
334                 \r
335                 /* Set fSUB */\r
336                 XTSTOP = 1U;\r
337                 OSMC = 0x10;\r
338                 \r
339                 /* Set fCLK */\r
340                 CSS = 0U;\r
341                 \r
342                 /* Set fIH */\r
343                 HIOSTOP = 0U;\r
344         }\r
345         #else\r
346         {\r
347                 unsigned char ucTempStabset, ucTempStabWait;    \r
348 \r
349                 /* Set fMX */\r
350                 CMC = 0x41;\r
351                 OSTS = 0x07;\r
352                 MSTOP = 0U;\r
353                 ucTempStabset = 0xFF;\r
354                 \r
355                 do\r
356                 {\r
357                         ucTempStabWait = OSTC;\r
358                         ucTempStabWait &= ucTempStabset;\r
359                 }\r
360                 while( ucTempStabWait != ucTempStabset );\r
361                 \r
362                 /* Set fMAIN */\r
363                 MCM0 = 1U;\r
364                 \r
365                 /* Set fSUB */\r
366                 XTSTOP = 1U;\r
367                 OSMC = 0x10;\r
368                 \r
369                 /* Set fCLK */\r
370                 CSS = 0U;\r
371                 \r
372                 /* Set fIH */\r
373                 HIOSTOP = 0U;\r
374         }\r
375         #endif /* configCLOCK_SOURCE == 1 */\r
376         \r
377         /* LED port initialization - set port register. */\r
378         P7 &= 0x7F;\r
379         \r
380         /* Set port mode register. */\r
381         PM7 &= 0x7F;\r
382         \r
383         /* Switch pin initialization - enable pull-up resistor. */\r
384         PU12_bit.no0  = 1;\r
385 \r
386         return pdTRUE;\r
387 }\r
388 /*-----------------------------------------------------------*/\r
389 \r
390 void vRegTestError( void )\r
391 {\r
392         /* Called by the RegTest tasks if an error is found.  lRegTestStatus is\r
393         inspected by the check task. */\r
394         sRegTestStatus = pdFAIL;\r
395 \r
396         /* Do not return from here as the reg test tasks clobber all registers so\r
397         function calls may not function correctly. */\r
398         for( ;; );\r
399 }\r
400 /*-----------------------------------------------------------*/\r
401 \r
402 void vApplicationMallocFailedHook( void )\r
403 {\r
404         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
405         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
406         internally by FreeRTOS API functions that create tasks, queues, software\r
407         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
408         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
409         taskDISABLE_INTERRUPTS();\r
410         for( ;; );\r
411 }\r
412 /*-----------------------------------------------------------*/\r
413 \r
414 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
415 {\r
416         ( void ) pcTaskName;\r
417         ( void ) pxTask;\r
418 \r
419         /* Run time stack overflow checking is performed if\r
420         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
421         function is called if a stack overflow is detected. */\r
422         taskDISABLE_INTERRUPTS();\r
423         for( ;; );\r
424 }\r
425 /*-----------------------------------------------------------*/\r
426 \r
427 void vApplicationIdleHook( void )\r
428 {\r
429 volatile size_t xFreeHeapSpace;\r
430 \r
431         /* This is just a trivial example of an idle hook.  It is called on each\r
432         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
433         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
434         memory management section on the http://www.FreeRTOS.org web site for memory\r
435         management options.  If there is a lot of heap memory free then the\r
436         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
437         RAM. */\r
438         xFreeHeapSpace = xPortGetFreeHeapSize();        \r
439 }\r
440 \r