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