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