]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RL78_RL78G13_Promo_Board_IAR/main.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / RL78_RL78G13_Promo_Board_IAR / main.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  *\r
98  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
99  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
100  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
101  *\r
102  *\r
103  * main() creates the demo application tasks and timers, then starts the\r
104  * scheduler.\r
105  *\r
106  * This demo is configured to run on the RL78/G13 Promotion Board, which is\r
107  * fitted with a R5F100LEA microcontroller.  The R5F100LEA contains a little\r
108  * under 4K bytes of usable internal RAM.  The RAM size restricts the number of\r
109  * demo tasks that can be created, and the demo creates 13 tasks, 4 queues and\r
110  * two timers.  The RL78 range does however include parts with up to 32K bytes\r
111  * of RAM (at the time of writing).  Using FreeRTOS on such a part will allow an\r
112  * application to make a more comprehensive use of FreeRTOS tasks, and other\r
113  * FreeRTOS features.\r
114  *\r
115  * In addition to the standard demo tasks, the following tasks, tests and timers\r
116  * are created within this file:\r
117  *\r
118  * "Reg test" tasks - These fill the registers with known values, then check\r
119  * that each register still contains its expected value.  Each task uses a\r
120  * different set of values.  The reg test tasks execute with a very low priority,\r
121  * so get preempted very frequently.  A register containing an unexpected value\r
122  * is indicative of an error in the context switching mechanism.\r
123  *\r
124  * The "Demo" Timer and Callback Function:\r
125  * The demo timer callback function does nothing more than increment a variable.\r
126  * The period of the demo timer is set relative to the period of the check timer\r
127  * (described below).  This allows the check timer to know how many times the\r
128  * demo timer callback function should execute between each execution of the\r
129  * check timer callback function.  The variable incremented in the demo timer\r
130  * callback function is used to determine how many times the callback function\r
131  * has executed.\r
132  *\r
133  * The "Check" Timer and Callback Function:\r
134  * The check timer period is initially set to three seconds.  The check timer\r
135  * callback function checks that all the standard demo tasks, the reg test tasks,\r
136  * and the demo timer are not only still executing, but are executing without\r
137  * reporting any errors.  If the check timer discovers that a task or timer has\r
138  * stalled, or reported an error, then it changes its own period from the\r
139  * initial three seconds, to just 200ms.  The check timer callback function also\r
140  * toggles the user LED each time it is called.  This provides a visual\r
141  * indication of the system status:  If the LED toggles every three seconds,\r
142  * then no issues have been discovered.  If the LED toggles every 200ms, then an\r
143  * issue has been discovered with at least one task.\r
144  *\r
145  */\r
146 \r
147 /* Scheduler include files. */\r
148 #include "FreeRTOS.h"\r
149 #include "task.h"\r
150 #include "timers.h"\r
151 \r
152 /* Standard demo includes. */\r
153 #include "dynamic.h"\r
154 #include "PollQ.h"\r
155 #include "blocktim.h"\r
156 \r
157 /* The period at which the check timer will expire, in ms, provided no errors\r
158 have been reported by any of the standard demo tasks.  ms are converted to the\r
159 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
160 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
161 \r
162 /* The period at which the check timer will expire, in ms, if an error has been\r
163 reported in one of the standard demo tasks, the check tasks, or the demo timer.\r
164 ms are converted to the equivalent in ticks using the portTICK_PERIOD_MS\r
165 constant. */\r
166 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
167 \r
168 /* These two definitions are used to set the period of the demo timer.  The demo\r
169 timer period is always relative to the check timer period, so the check timer\r
170 can determine if the demo timer has expired the expected number of times between\r
171 its own executions. */\r
172 #define mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT       ( 100UL )\r
173 #define mainDEMO_TIMER_PERIOD_MS                        ( mainCHECK_TIMER_PERIOD_MS / mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT )\r
174 \r
175 /* The LED toggled by the check timer. */\r
176 #define mainLED_0                                               P7_bit.no7\r
177 \r
178 /* A block time of zero simple means "don't block". */\r
179 #define mainDONT_BLOCK                                          ( 0U )\r
180 \r
181 /*-----------------------------------------------------------*/\r
182 \r
183 /*\r
184  * The 'check' timer callback function, as described at the top of this file.\r
185  */\r
186 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
187 \r
188 /*\r
189  * The 'demo' timer callback function, as described at the top of this file.\r
190  */\r
191 static void prvDemoTimerCallback( TimerHandle_t xTimer );\r
192 \r
193 /*\r
194  * This function is called from the C startup routine to setup the processor -\r
195  * in particular the clock source.\r
196  */\r
197 int __low_level_init(void);\r
198 \r
199 /*\r
200  * Functions that define the RegTest tasks, as described at the top of this file.\r
201  */\r
202 extern void vRegTest1( void *pvParameters );\r
203 extern void vRegTest2( void *pvParameters );\r
204 \r
205 \r
206 /*-----------------------------------------------------------*/\r
207 \r
208 /* If an error is discovered by one of the RegTest tasks then this flag is set\r
209 to pdFAIL.  The 'check' timer then inspects this flag to detect errors within\r
210 the RegTest tasks. */\r
211 static short sRegTestStatus = pdPASS;\r
212 \r
213 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
214 function. */\r
215 static TimerHandle_t xCheckTimer = NULL;\r
216 \r
217 /* The demo timer.  This uses prvDemoTimerCallback() as its callback function. */\r
218 static TimerHandle_t xDemoTimer = NULL;\r
219 \r
220 /* This variable is incremented each time the demo timer expires. */\r
221 static volatile unsigned long ulDemoSoftwareTimerCounter = 0UL;\r
222 \r
223 /* RL78 Option Byte Definition. Watchdog disabled, LVI enabled, OCD interface\r
224 enabled. */\r
225 __root __far const unsigned char OptionByte[] @ 0x00C0 =\r
226 {\r
227         0x6eU, 0xffU, 0xe8U, 0x85U\r
228 };\r
229 \r
230 /* Security byte definition */\r
231 __root __far const unsigned char SecuIDCode[]  @ 0x00C4 =\r
232 {\r
233         0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x54\r
234 };\r
235 \r
236 /*-----------------------------------------------------------*/\r
237 \r
238 short main( void )\r
239 {\r
240         /* Creates all the tasks and timers, then starts the scheduler. */\r
241 \r
242         /* First create the 'standard demo' tasks.  These are used to demonstrate\r
243         API functions being used and also to test the kernel port.  More information\r
244         is provided on the FreeRTOS.org WEB site. */\r
245         vStartDynamicPriorityTasks();\r
246         vStartPolledQueueTasks( tskIDLE_PRIORITY );\r
247         vCreateBlockTimeTasks();\r
248 \r
249         /* Create the RegTest tasks as described at the top of this file. */\r
250         xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
251         xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
252 \r
253         /* Create the software timer that performs the 'check' functionality,\r
254         as described at the top of this file. */\r
255         xCheckTimer = xTimerCreate( "CheckTimer",/* A text name, purely to help debugging. */\r
256                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
257                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
258                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
259                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
260                                                           );\r
261 \r
262         /* Create the software timer that just increments a variable for demo\r
263         purposes. */\r
264         xDemoTimer = xTimerCreate( "DemoTimer",/* A text name, purely to help debugging. */\r
265                                                                 ( 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
266                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
267                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
268                                                                 prvDemoTimerCallback                            /* The callback function that inspects the status of all the other tasks. */\r
269                                                           );\r
270 \r
271         /* Start both the check timer and the demo timer.  The timers won't actually\r
272         start until the scheduler is started. */\r
273         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
274         xTimerStart( xDemoTimer, mainDONT_BLOCK );\r
275 \r
276         /* Finally start the scheduler running. */\r
277         vTaskStartScheduler();\r
278 \r
279         /* If this line is reached then vTaskStartScheduler() returned because there\r
280         was insufficient heap memory remaining for the idle task to be created. */\r
281         for( ;; );\r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 static void prvDemoTimerCallback( TimerHandle_t xTimer )\r
286 {\r
287         /* The demo timer has expired.  All it does is increment a variable.  The\r
288         period of the demo timer is relative to that of the check timer, so the\r
289         check timer knows how many times this variable should have been incremented\r
290         between each execution of the check timer's own callback. */\r
291         ulDemoSoftwareTimerCounter++;\r
292 }\r
293 /*-----------------------------------------------------------*/\r
294 \r
295 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
296 {\r
297 static portBASE_TYPE xChangedTimerPeriodAlready = pdFALSE, xErrorStatus = pdPASS;\r
298 \r
299         /* Inspect the status of the standard demo tasks. */\r
300         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
301         {\r
302                 xErrorStatus = pdFAIL;\r
303         }\r
304 \r
305         if( xArePollingQueuesStillRunning() != pdTRUE )\r
306         {\r
307                 xErrorStatus = pdFAIL;\r
308         }\r
309 \r
310         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
311         {\r
312                 xErrorStatus = pdFAIL;\r
313         }\r
314 \r
315         /* Inspect the status of the reg test tasks. */\r
316         if( sRegTestStatus != pdPASS )\r
317         {\r
318                 xErrorStatus = pdFAIL;\r
319         }\r
320 \r
321         /* Ensure that the demo software timer has expired\r
322         mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT times in between\r
323         each call of this function.  A critical section is not required to access\r
324         ulDemoSoftwareTimerCounter as the variable is only accessed from another\r
325         software timer callback, and only one software timer callback can be\r
326         executing at any time. */\r
327         if( ( ulDemoSoftwareTimerCounter < ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT - 1 ) ) ||\r
328             ( ulDemoSoftwareTimerCounter > ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT + 1 ) )\r
329           )\r
330         {\r
331                 xErrorStatus = pdFAIL;\r
332         }\r
333         else\r
334         {\r
335                 ulDemoSoftwareTimerCounter = 0UL;\r
336         }\r
337 \r
338         if( ( xErrorStatus == pdFAIL ) && ( xChangedTimerPeriodAlready == pdFALSE ) )\r
339         {\r
340                 /* An error has occurred, but the timer's period has not yet been changed,\r
341                 change it now, and remember that it has been changed.  Shortening the\r
342                 timer's period means the LED will toggle at a faster rate, giving a\r
343                 visible indication that something has gone wrong. */\r
344                 xChangedTimerPeriodAlready = pdTRUE;\r
345 \r
346                 /* This call to xTimerChangePeriod() uses a zero block time.  Functions\r
347                 called from inside of a timer callback function must *never* attempt to\r
348                 block. */\r
349                 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
350         }\r
351 \r
352         /* Toggle the LED.  The toggle rate will depend on whether or not an error\r
353         has been found in any tasks. */\r
354         mainLED_0 = !mainLED_0;\r
355 }\r
356 /*-----------------------------------------------------------*/\r
357 \r
358 int __low_level_init(void)\r
359 {\r
360 unsigned char ucResetFlag = RESF;\r
361 \r
362         portDISABLE_INTERRUPTS();\r
363 \r
364         /* Clock Configuration:\r
365         In this port, to use the internal high speed clock source of the\r
366         microcontroller, define the configCLOCK_SOURCE as 1 in FreeRTOSConfig.h.  To\r
367         use an external clock define configCLOCK_SOURCE as 0. */\r
368         #if configCLOCK_SOURCE == 1\r
369         {\r
370                 /* Set fMX */\r
371                 CMC = 0x00;\r
372                 MSTOP = 1U;\r
373 \r
374                 /* Set fMAIN */\r
375                 MCM0 = 0U;\r
376 \r
377                 /* Set fSUB */\r
378                 XTSTOP = 1U;\r
379                 OSMC = 0x10;\r
380 \r
381                 /* Set fCLK */\r
382                 CSS = 0U;\r
383 \r
384                 /* Set fIH */\r
385                 HIOSTOP = 0U;\r
386         }\r
387         #else\r
388         {\r
389                 unsigned char ucTempStabset, ucTempStabWait;\r
390 \r
391                 /* Set fMX */\r
392                 CMC = 0x41;\r
393                 OSTS = 0x07;\r
394                 MSTOP = 0U;\r
395                 ucTempStabset = 0xFF;\r
396 \r
397                 do\r
398                 {\r
399                         ucTempStabWait = OSTC;\r
400                         ucTempStabWait &= ucTempStabset;\r
401                 }\r
402                 while( ucTempStabWait != ucTempStabset );\r
403 \r
404                 /* Set fMAIN */\r
405                 MCM0 = 1U;\r
406 \r
407                 /* Set fSUB */\r
408                 XTSTOP = 1U;\r
409                 OSMC = 0x10;\r
410 \r
411                 /* Set fCLK */\r
412                 CSS = 0U;\r
413 \r
414                 /* Set fIH */\r
415                 HIOSTOP = 0U;\r
416         }\r
417         #endif /* configCLOCK_SOURCE == 1 */\r
418 \r
419         /* LED port initialization - set port register. */\r
420         P7 &= 0x7F;\r
421 \r
422         /* Set port mode register. */\r
423         PM7 &= 0x7F;\r
424 \r
425         /* Switch pin initialization - enable pull-up resistor. */\r
426         PU12_bit.no0  = 1;\r
427 \r
428         return pdTRUE;\r
429 }\r
430 /*-----------------------------------------------------------*/\r
431 \r
432 void vRegTestError( void )\r
433 {\r
434         /* Called by the RegTest tasks if an error is found.  lRegTestStatus is\r
435         inspected by the check task. */\r
436         sRegTestStatus = pdFAIL;\r
437 \r
438         /* Do not return from here as the reg test tasks clobber all registers so\r
439         function calls may not function correctly. */\r
440         for( ;; );\r
441 }\r
442 /*-----------------------------------------------------------*/\r
443 \r
444 void vApplicationMallocFailedHook( void )\r
445 {\r
446         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
447         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
448         internally by FreeRTOS API functions that create tasks, queues, software\r
449         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
450         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
451         taskDISABLE_INTERRUPTS();\r
452         for( ;; );\r
453 }\r
454 /*-----------------------------------------------------------*/\r
455 \r
456 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
457 {\r
458         ( void ) pcTaskName;\r
459         ( void ) pxTask;\r
460 \r
461         /* Run time stack overflow checking is performed if\r
462         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
463         function is called if a stack overflow is detected. */\r
464         taskDISABLE_INTERRUPTS();\r
465         for( ;; );\r
466 }\r
467 /*-----------------------------------------------------------*/\r
468 \r
469 void vApplicationIdleHook( void )\r
470 {\r
471 volatile size_t xFreeHeapSpace;\r
472 \r
473         /* This is just a trivial example of an idle hook.  It is called on each\r
474         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
475         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
476         memory management section on the http://www.FreeRTOS.org web site for memory\r
477         management options.  If there is a lot of heap memory free then the\r
478         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
479         RAM. */\r
480         xFreeHeapSpace = xPortGetFreeHeapSize();\r
481 }\r
482 \r