2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\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
36 * main() creates the demo application tasks and timers, then starts the
\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
48 * In addition to the standard demo tasks, the following tasks, tests and timers
\r
49 * are created within this file:
\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
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
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
80 /* Scheduler include files. */
\r
81 #include "FreeRTOS.h"
\r
85 /* Standard demo includes. */
\r
86 #include "dynamic.h"
\r
88 #include "blocktim.h"
\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
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
99 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_PERIOD_MS )
\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
108 /* The LED toggled by the check timer. */
\r
109 #define mainLED_0 P7_bit.no7
\r
111 /* A block time of zero simple means "don't block". */
\r
112 #define mainDONT_BLOCK ( 0U )
\r
114 /*-----------------------------------------------------------*/
\r
117 * The 'check' timer callback function, as described at the top of this file.
\r
119 static void prvCheckTimerCallback( TimerHandle_t xTimer );
\r
122 * The 'demo' timer callback function, as described at the top of this file.
\r
124 static void prvDemoTimerCallback( TimerHandle_t xTimer );
\r
127 * This function is called from the C startup routine to setup the processor -
\r
128 * in particular the clock source.
\r
130 int __low_level_init(void);
\r
133 * Functions that define the RegTest tasks, as described at the top of this file.
\r
135 extern void vRegTest1( void *pvParameters );
\r
136 extern void vRegTest2( void *pvParameters );
\r
139 /*-----------------------------------------------------------*/
\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
146 /* The check timer. This uses prvCheckTimerCallback() as its callback
\r
148 static TimerHandle_t xCheckTimer = NULL;
\r
150 /* The demo timer. This uses prvDemoTimerCallback() as its callback function. */
\r
151 static TimerHandle_t xDemoTimer = NULL;
\r
153 /* This variable is incremented each time the demo timer expires. */
\r
154 static volatile unsigned long ulDemoSoftwareTimerCounter = 0UL;
\r
156 /* RL78 Option Byte Definition. Watchdog disabled, LVI enabled, OCD interface
\r
158 __root __far const unsigned char OptionByte[] @ 0x00C0 =
\r
160 0x6eU, 0xffU, 0xe8U, 0x85U
\r
163 /* Security byte definition */
\r
164 __root __far const unsigned char SecuIDCode[] @ 0x00C4 =
\r
166 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x54
\r
169 /*-----------------------------------------------------------*/
\r
173 /* Creates all the tasks and timers, then starts the scheduler. */
\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
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
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
195 /* Create the software timer that just increments a variable for demo
\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
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
209 /* Finally start the scheduler running. */
\r
210 vTaskStartScheduler();
\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
216 /*-----------------------------------------------------------*/
\r
218 static void prvDemoTimerCallback( TimerHandle_t xTimer )
\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
226 /*-----------------------------------------------------------*/
\r
228 static void prvCheckTimerCallback( TimerHandle_t xTimer )
\r
230 static portBASE_TYPE xChangedTimerPeriodAlready = pdFALSE, xErrorStatus = pdPASS;
\r
232 /* Inspect the status of the standard demo tasks. */
\r
233 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
235 xErrorStatus = pdFAIL;
\r
238 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
240 xErrorStatus = pdFAIL;
\r
243 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
245 xErrorStatus = pdFAIL;
\r
248 /* Inspect the status of the reg test tasks. */
\r
249 if( sRegTestStatus != pdPASS )
\r
251 xErrorStatus = pdFAIL;
\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
264 xErrorStatus = pdFAIL;
\r
268 ulDemoSoftwareTimerCounter = 0UL;
\r
271 if( ( xErrorStatus == pdFAIL ) && ( xChangedTimerPeriodAlready == pdFALSE ) )
\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
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
282 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\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
289 /*-----------------------------------------------------------*/
\r
291 int __low_level_init(void)
\r
293 unsigned char ucResetFlag = RESF;
\r
295 portDISABLE_INTERRUPTS();
\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
322 unsigned char ucTempStabset, ucTempStabWait;
\r
328 ucTempStabset = 0xFF;
\r
332 ucTempStabWait = OSTC;
\r
333 ucTempStabWait &= ucTempStabset;
\r
335 while( ucTempStabWait != ucTempStabset );
\r
350 #endif /* configCLOCK_SOURCE == 1 */
\r
352 /* LED port initialization - set port register. */
\r
355 /* Set port mode register. */
\r
358 /* Switch pin initialization - enable pull-up resistor. */
\r
363 /*-----------------------------------------------------------*/
\r
365 void vRegTestError( void )
\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
371 /* Do not return from here as the reg test tasks clobber all registers so
\r
372 function calls may not function correctly. */
\r
375 /*-----------------------------------------------------------*/
\r
377 void vApplicationMallocFailedHook( void )
\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
387 /*-----------------------------------------------------------*/
\r
389 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
391 ( void ) pcTaskName;
\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
400 /*-----------------------------------------------------------*/
\r
402 void vApplicationIdleHook( void )
\r
404 volatile size_t xFreeHeapSpace;
\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
413 xFreeHeapSpace = xPortGetFreeHeapSize();
\r