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