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