]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RL78_E2Studio_GCC/src/main.c
For RL78GCC port/demo:
[freertos] / FreeRTOS / Demo / RL78_E2Studio_GCC / src / main.c
1 /*\r
2     FreeRTOS V7.4.0 - Copyright (C) 2013 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 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not itcan be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
67     Integrity Systems, who sell the code with commercial support,\r
68     indemnification and middleware, under the OpenRTOS brand.\r
69 \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
71     engineered and independently SIL3 certified version for use in safety and\r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  *\r
77  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
78  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
79  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
80  *\r
81  *\r
82  * main() creates the demo application tasks and timers, then starts the\r
83  * scheduler.\r
84  *\r
85  * This demo is configured to run on the RL78/G13 Promotion Board, which is\r
86  * fitted with a R5F100LEA microcontroller.  The R5F100LEA contains a little\r
87  * under 4K bytes of usable internal RAM.  The RAM size restricts the number of\r
88  * demo tasks that can be created, and the demo creates 13 tasks, 4 queues and\r
89  * two timers.  The RL78 range does however include parts with up to 32K bytes\r
90  * of RAM (at the time of writing).  Using FreeRTOS on such a part will allow an\r
91  * application to make a more comprehensive use of FreeRTOS tasks, and other\r
92  * FreeRTOS features.\r
93  *\r
94  * In addition to the standard demo tasks, the following tasks, tests and timers\r
95  * are created within this file:\r
96  *\r
97  * "Reg test" tasks - These fill the registers with known values, then check\r
98  * that each register still contains its expected value.  Each task uses a\r
99  * different set of values.  The reg test tasks execute with a very low priority,\r
100  * so get preempted very frequently.  A register containing an unexpected value\r
101  * is indicative of an error in the context switching mechanism.\r
102  *\r
103  * The "Demo" Timer and Callback Function:\r
104  * The demo timer callback function does nothing more than increment a variable.\r
105  * The period of the demo timer is set relative to the period of the check timer\r
106  * (described below).  This allows the check timer to know how many times the\r
107  * demo timer callback function should execute between each execution of the\r
108  * check timer callback function.  The variable incremented in the demo timer\r
109  * callback function is used to determine how many times the callback function\r
110  * has executed.\r
111  *\r
112  * The "Check" Timer and Callback Function:\r
113  * The check timer period is initially set to three seconds.  The check timer\r
114  * callback function checks that all the standard demo tasks, the reg test tasks,\r
115  * and the demo timer are not only still executing, but are executing without\r
116  * reporting any errors.  If the check timer discovers that a task or timer has\r
117  * stalled, or reported an error, then it changes its own period from the\r
118  * initial three seconds, to just 200ms.  The check timer callback function also\r
119  * toggles the user LED each time it is called.  This provides a visual\r
120  * indication of the system status:  If the LED toggles every three seconds,\r
121  * then no issues have been discovered.  If the LED toggles every 200ms, then an\r
122  * issue has been discovered with at least one task.\r
123  *\r
124  */\r
125 \r
126 /* Scheduler include files. */\r
127 #include "FreeRTOS.h"\r
128 #include "task.h"\r
129 #include "timers.h"\r
130 \r
131 /* Standard demo includes. */\r
132 #include "dynamic.h"\r
133 #include "PollQ.h"\r
134 #include "blocktim.h"\r
135 \r
136 /* Hardware includes. */\r
137 #include "port_iodefine.h"\r
138 #include "port_iodefine_ext.h"\r
139 #include "LED.h"\r
140 \r
141 /* The period at which the check timer will expire, in ms, provided no errors\r
142 have been reported by any of the standard demo tasks.  ms are converted to the\r
143 equivalent in ticks using the portTICK_RATE_MS constant. */\r
144 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
145 \r
146 /* The period at which the check timer will expire, in ms, if an error has been\r
147 reported in one of the standard demo tasks, the check tasks, or the demo timer.\r
148 ms are converted to the equivalent in ticks using the portTICK_RATE_MS\r
149 constant. */\r
150 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_RATE_MS )\r
151 \r
152 /* These two definitions are used to set the period of the demo timer.  The demo\r
153 timer period is always relative to the check timer period, so the check timer\r
154 can determine if the demo timer has expired the expected number of times between\r
155 its own executions. */\r
156 #define mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT       ( 100UL )\r
157 #define mainDEMO_TIMER_PERIOD_MS                        ( mainCHECK_TIMER_PERIOD_MS / mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT )\r
158 \r
159 /* A block time of zero simple means "don't block". */\r
160 #define mainDONT_BLOCK                                          ( 0U )\r
161 \r
162 /* Values that are passed as parameters into the reg test tasks (purely to\r
163 ensure task parameters are passed correctly). */\r
164 #define mainREG_TEST_1_PARAMETER                        ( ( void * ) 0x1234 )\r
165 #define mainREG_TEST_2_PARAMETER                        ( ( void * ) 0x5678 )\r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 /*\r
170  * The 'check' timer callback function, as described at the top of this file.\r
171  */\r
172 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
173 \r
174 /*\r
175  * The 'demo' timer callback function, as described at the top of this file.\r
176  */\r
177 static void prvDemoTimerCallback( xTimerHandle xTimer );\r
178 \r
179 /*\r
180  * Functions that define the RegTest tasks, as described at the top of this\r
181  * file.  The RegTest tasks are written (necessarily) in assembler.  Their\r
182  * entry points are written in C to allow for easy checking of the task\r
183  * parameter values.\r
184  */\r
185 extern void vRegTest1Task( void );\r
186 extern void vRegTest2Task( void );\r
187 static void prvRegTest1Entry( void *pvParameters );\r
188 static void prvRegTest2Entry( void *pvParameters );\r
189 \r
190 /*\r
191  * Called if a RegTest task discovers an error as a mechanism to stop the\r
192  * tasks loop counter incrementing (so the check task can detect that an\r
193  * error exists).\r
194  */\r
195 void vRegTestError( void );\r
196 \r
197 /*-----------------------------------------------------------*/\r
198 \r
199 /* Variables that are incremented on each cycle of the two reg tests to allow\r
200 the check timer to know that they are still executing. */\r
201 unsigned short usRegTest1LoopCounter = 0, usRegTest2LoopCounter;\r
202 \r
203 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
204 function. */\r
205 static xTimerHandle xCheckTimer = NULL;\r
206 \r
207 /* The demo timer.  This uses prvDemoTimerCallback() as its callback function. */\r
208 static xTimerHandle xDemoTimer = NULL;\r
209 \r
210 /* This variable is incremented each time the demo timer expires. */\r
211 static volatile unsigned long ulDemoSoftwareTimerCounter = 0UL;\r
212 \r
213 /*-----------------------------------------------------------*/\r
214 volatile unsigned char ucTemp;\r
215 short main( void )\r
216 {\r
217         ucTemp = RESF;\r
218         ucTemp = sizeof( char* );\r
219         ucTemp = sizeof( pdTASK_CODE );\r
220         P1 &= 0xFE; PM1 &= 0xFE;\r
221         P1_bit.no0 = 1;\r
222 \r
223 \r
224         /* Creates all the tasks and timers, then starts the scheduler. */\r
225 \r
226         /* First create the 'standard demo' tasks.  These are used to demonstrate\r
227         API functions being used and also to test the kernel port.  More information\r
228         is provided on the FreeRTOS.org WEB site. */\r
229 //      vStartDynamicPriorityTasks();\r
230 #warning Runs if the debugger is not connected and vStartDynamicPriorityTasks() is commented out.\r
231         vStartPolledQueueTasks( tskIDLE_PRIORITY );\r
232         vCreateBlockTimeTasks();\r
233 \r
234         /* Create the RegTest tasks as described at the top of this file. */\r
235         xTaskCreate( prvRegTest1Entry, "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
236         xTaskCreate( prvRegTest2Entry, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
237 \r
238         /* Create the software timer that performs the 'check' functionality,\r
239         as described at the top of this file. */\r
240         xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
241                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
242                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
243                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
244                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
245                                                           );\r
246 \r
247         /* Create the software timer that just increments a variable for demo\r
248         purposes. */\r
249         xDemoTimer = xTimerCreate( ( const signed char * ) "DemoTimer",/* A text name, purely to help debugging. */\r
250                                                                 ( 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
251                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
252                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
253                                                                 prvDemoTimerCallback                            /* The callback function that inspects the status of all the other tasks. */\r
254                                                           );\r
255 \r
256         /* Start both the check timer and the demo timer.  The timers won't actually\r
257         start until the scheduler is started. */\r
258         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
259         xTimerStart( xDemoTimer, mainDONT_BLOCK );\r
260 \r
261         /* Finally start the scheduler running. */\r
262         vTaskStartScheduler();\r
263 \r
264         /* If this line is reached then vTaskStartScheduler() returned because there\r
265         was insufficient heap memory remaining for the idle task to be created. */\r
266         for( ;; );\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 static void prvDemoTimerCallback( xTimerHandle xTimer )\r
271 {\r
272         /* The demo timer has expired.  All it does is increment a variable.  The\r
273         period of the demo timer is relative to that of the check timer, so the\r
274         check timer knows how many times this variable should have been incremented\r
275         between each execution of the check timer's own callback. */\r
276         ulDemoSoftwareTimerCounter++;\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
281 {\r
282 static portBASE_TYPE xChangedTimerPeriodAlready = pdFALSE, xErrorStatus = pdPASS;\r
283 static unsigned short usLastRegTest1Counter = 0, usLastRegTest2Counter = 0;\r
284 \r
285         /* Inspect the status of the standard demo tasks. */\r
286         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
287         {\r
288                 xErrorStatus = pdFAIL;\r
289         }\r
290 \r
291         if( xArePollingQueuesStillRunning() != pdTRUE )\r
292         {\r
293                 xErrorStatus = pdFAIL;\r
294         }\r
295 \r
296         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
297         {\r
298                 xErrorStatus = pdFAIL;\r
299         }\r
300 \r
301         /* Indicate an error if either of the reg test loop counters have not\r
302         incremented since the last time this function was called. */\r
303         if( usLastRegTest1Counter == usRegTest1LoopCounter )\r
304         {\r
305                 xErrorStatus = pdFAIL;\r
306         }\r
307         else\r
308         {\r
309                 usLastRegTest1Counter = usRegTest1LoopCounter;\r
310         }\r
311 \r
312         if( usLastRegTest2Counter == usRegTest2LoopCounter )\r
313         {\r
314                 xErrorStatus = pdFAIL;\r
315         }\r
316         else\r
317         {\r
318                 usLastRegTest2Counter = usRegTest2LoopCounter;\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         LED_BIT = !LED_BIT;\r
355 \r
356         if( xTaskGetTickCount() > ( ( portTickType ) 10000 / portTICK_RATE_MS ) )\r
357         {\r
358                 /* Turn off the LED used to visualise a reset. */\r
359                 P1_bit.no0 = 0;\r
360         }\r
361 }\r
362 /*-----------------------------------------------------------*/\r
363 \r
364 void vRegTestError( void )\r
365 {\r
366         /* Called by both reg test tasks if an error is found.  There is no way out\r
367         of this function so the loop counter of the calling task will stop\r
368         incrementing, which will result in the check timer signaling an error. */\r
369         for( ;; );\r
370 }\r
371 /*-----------------------------------------------------------*/\r
372 \r
373 void vApplicationMallocFailedHook( void )\r
374 {\r
375         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
376         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
377         internally by FreeRTOS API functions that create tasks, queues, software\r
378         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
379         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
380         taskDISABLE_INTERRUPTS();\r
381         P1_bit.no0 = 0;\r
382         for( ;; );\r
383 }\r
384 /*-----------------------------------------------------------*/\r
385 \r
386 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
387 {\r
388         ( void ) pcTaskName;\r
389         ( void ) pxTask;\r
390 \r
391         /* Run time stack overflow checking is performed if\r
392         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
393         function is called if a stack overflow is detected. */\r
394         taskDISABLE_INTERRUPTS();\r
395         P1_bit.no0 = 0;\r
396         for( ;; );\r
397 }\r
398 /*-----------------------------------------------------------*/\r
399 \r
400 void vApplicationIdleHook( void )\r
401 {\r
402 volatile size_t xFreeHeapSpace;\r
403 \r
404         /* This is just a trivial example of an idle hook.  It is called on each\r
405         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
406         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
407         memory management section on the http://www.FreeRTOS.org web site for memory\r
408         management options.  If there is a lot of heap memory free then the\r
409         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
410         RAM. */\r
411         xFreeHeapSpace = xPortGetFreeHeapSize();\r
412 }\r
413 /*-----------------------------------------------------------*/\r
414 \r
415 static void prvRegTest1Entry( void *pvParameters )\r
416 {\r
417         /* If the parameter has its expected value then start the first reg test\r
418         task (this is only done to test that the RTOS port is correctly handling\r
419         task parameters. */\r
420         if( pvParameters == mainREG_TEST_1_PARAMETER )\r
421         {\r
422                 vRegTest1Task();\r
423         }\r
424         else\r
425         {\r
426                 vRegTestError();\r
427         }\r
428 \r
429         /* It is not possible to get here as neither of the two functions called\r
430         above will ever return. */\r
431 }\r
432 /*-----------------------------------------------------------*/\r
433 \r
434 static void prvRegTest2Entry( void *pvParameters )\r
435 {\r
436         /* If the parameter has its expected value then start the first reg test\r
437         task (this is only done to test that the RTOS port is correctly handling\r
438         task parameters. */\r
439         if( pvParameters == mainREG_TEST_2_PARAMETER )\r
440         {\r
441                 vRegTest2Task();\r
442         }\r
443         else\r
444         {\r
445                 vRegTestError();\r
446         }\r
447 \r
448         /* It is not possible to get here as neither of the two functions called\r
449         above will ever return. */\r
450 }\r
451 /*-----------------------------------------------------------*/\r
452 \r
453 void vAssertCalled( void )\r
454 {\r
455         taskDISABLE_INTERRUPTS();\r
456         P1_bit.no0 = 0;\r
457         for( ;; );\r
458 }\r