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