2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON
\r
70 * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO
\r
71 * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!
\r
74 * main() creates the demo application tasks and timers, then starts the
\r
77 * This demo is configured to run on the RL78/G13 Promotion Board, which is
\r
78 * fitted with a R5F100LEA microcontroller. The R5F100LEA contains a little
\r
79 * under 4K bytes of usable internal RAM. The RAM size restricts the number of
\r
80 * demo tasks that can be created, and the demo creates 13 tasks, 4 queues and
\r
81 * two timers. The RL78 range does however include parts with up to 32K bytes
\r
82 * of RAM (at the time of writing). Using FreeRTOS on such a part will allow an
\r
83 * application to make a more comprehensive use of FreeRTOS tasks, and other
\r
84 * FreeRTOS features.
\r
86 * In addition to the standard demo tasks, the following tasks, tests and timers
\r
87 * are created within this file:
\r
89 * "Reg test" tasks - These fill the registers with known values, then check
\r
90 * that each register still contains its expected value. Each task uses a
\r
91 * different set of values. The reg test tasks execute with a very low priority,
\r
92 * so get preempted very frequently. A register containing an unexpected value
\r
93 * is indicative of an error in the context switching mechanism.
\r
95 * The "Demo" Timer and Callback Function:
\r
96 * The demo timer callback function does nothing more than increment a variable.
\r
97 * The period of the demo timer is set relative to the period of the check timer
\r
98 * (described below). This allows the check timer to know how many times the
\r
99 * demo timer callback function should execute between each execution of the
\r
100 * check timer callback function. The variable incremented in the demo timer
\r
101 * callback function is used to determine how many times the callback function
\r
104 * The "Check" Timer and Callback Function:
\r
105 * The check timer period is initially set to three seconds. The check timer
\r
106 * callback function checks that all the standard demo tasks, the reg test tasks,
\r
107 * and the demo timer are not only still executing, but are executing without
\r
108 * reporting any errors. If the check timer discovers that a task or timer has
\r
109 * stalled, or reported an error, then it changes its own period from the
\r
110 * initial three seconds, to just 200ms. The check timer callback function also
\r
111 * toggles the user LED each time it is called. This provides a visual
\r
112 * indication of the system status: If the LED toggles every three seconds,
\r
113 * then no issues have been discovered. If the LED toggles every 200ms, then an
\r
114 * issue has been discovered with at least one task.
\r
118 /* Scheduler include files. */
\r
119 #include "FreeRTOS.h"
\r
121 #include "timers.h"
\r
123 /* Standard demo includes. */
\r
124 #include "dynamic.h"
\r
126 #include "blocktim.h"
\r
128 /* The period at which the check timer will expire, in ms, provided no errors
\r
129 have been reported by any of the standard demo tasks. ms are converted to the
\r
130 equivalent in ticks using the portTICK_RATE_MS constant. */
\r
131 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_RATE_MS )
\r
133 /* The period at which the check timer will expire, in ms, if an error has been
\r
134 reported in one of the standard demo tasks, the check tasks, or the demo timer.
\r
135 ms are converted to the equivalent in ticks using the portTICK_RATE_MS
\r
137 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_RATE_MS )
\r
139 /* These two definitions are used to set the period of the demo timer. The demo
\r
140 timer period is always relative to the check timer period, so the check timer
\r
141 can determine if the demo timer has expired the expected number of times between
\r
142 its own executions. */
\r
143 #define mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT ( 100UL )
\r
144 #define mainDEMO_TIMER_PERIOD_MS ( mainCHECK_TIMER_PERIOD_MS / mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT )
\r
146 /* The LED toggled by the check timer. */
\r
147 #define mainLED_0 P7_bit.no7
\r
149 /* A block time of zero simple means "don't block". */
\r
150 #define mainDONT_BLOCK ( 0U )
\r
152 /*-----------------------------------------------------------*/
\r
155 * The 'check' timer callback function, as described at the top of this file.
\r
157 static void prvCheckTimerCallback( xTimerHandle xTimer );
\r
160 * The 'demo' timer callback function, as described at the top of this file.
\r
162 static void prvDemoTimerCallback( xTimerHandle xTimer );
\r
165 * This function is called from the C startup routine to setup the processor -
\r
166 * in particular the clock source.
\r
168 int __low_level_init(void);
\r
171 * Functions that define the RegTest tasks, as described at the top of this file.
\r
173 extern void vRegTest1( void *pvParameters );
\r
174 extern void vRegTest2( void *pvParameters );
\r
177 /*-----------------------------------------------------------*/
\r
179 /* If an error is discovered by one of the RegTest tasks then this flag is set
\r
180 to pdFAIL. The 'check' timer then inspects this flag to detect errors within
\r
181 the RegTest tasks. */
\r
182 static short sRegTestStatus = pdPASS;
\r
184 /* The check timer. This uses prvCheckTimerCallback() as its callback
\r
186 static xTimerHandle xCheckTimer = NULL;
\r
188 /* The demo timer. This uses prvDemoTimerCallback() as its callback function. */
\r
189 static xTimerHandle xDemoTimer = NULL;
\r
191 /* This variable is incremented each time the demo timer expires. */
\r
192 static volatile unsigned long ulDemoSoftwareTimerCounter = 0UL;
\r
194 /* RL78/G13 Option Byte Definition. Watchdog disabled, LVI enabled, OCD interface
\r
196 __root __far const unsigned char OptionByte[] @ 0x00C0 =
\r
198 WATCHDOG_DISABLED, LVI_ENABLED, RESERVED_FF, OCD_ENABLED
\r
201 /* Security byte definition */
\r
202 __root __far const unsigned char SecuIDCode[] @ 0x00C4 =
\r
204 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x54
\r
207 /*-----------------------------------------------------------*/
\r
211 /* Creates all the tasks and timers, then starts the scheduler. */
\r
213 /* First create the 'standard demo' tasks. These are used to demonstrate
\r
214 API functions being used and also to test the kernel port. More information
\r
215 is provided on the FreeRTOS.org WEB site. */
\r
216 vStartDynamicPriorityTasks();
\r
217 vStartPolledQueueTasks( tskIDLE_PRIORITY );
\r
218 vCreateBlockTimeTasks();
\r
220 /* Create the RegTest tasks as described at the top of this file. */
\r
221 xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
\r
222 xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
\r
224 /* Create the software timer that performs the 'check' functionality,
\r
225 as described at the top of this file. */
\r
226 xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */
\r
227 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
228 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
229 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
230 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
233 /* Create the software timer that just increments a variable for demo
\r
235 xDemoTimer = xTimerCreate( ( const signed char * ) "DemoTimer",/* A text name, purely to help debugging. */
\r
236 ( 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
237 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
238 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
239 prvDemoTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
242 /* Start both the check timer and the demo timer. The timers won't actually
\r
243 start until the scheduler is started. */
\r
244 xTimerStart( xCheckTimer, mainDONT_BLOCK );
\r
245 xTimerStart( xDemoTimer, mainDONT_BLOCK );
\r
247 /* Finally start the scheduler running. */
\r
248 vTaskStartScheduler();
\r
250 /* If this line is reached then vTaskStartScheduler() returned because there
\r
251 was insufficient heap memory remaining for the idle task to be created. */
\r
254 /*-----------------------------------------------------------*/
\r
256 static void prvDemoTimerCallback( xTimerHandle xTimer )
\r
258 /* The demo timer has expired. All it does is increment a variable. The
\r
259 period of the demo timer is relative to that of the check timer, so the
\r
260 check timer knows how many times this variable should have been incremented
\r
261 between each execution of the check timer's own callback. */
\r
262 ulDemoSoftwareTimerCounter++;
\r
264 /*-----------------------------------------------------------*/
\r
266 static void prvCheckTimerCallback( xTimerHandle xTimer )
\r
268 static portBASE_TYPE xChangedTimerPeriodAlready = pdFALSE, xErrorStatus = pdPASS;
\r
270 /* Inspect the status of the standard demo tasks. */
\r
271 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
273 xErrorStatus = pdFAIL;
\r
276 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
278 xErrorStatus = pdFAIL;
\r
281 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
283 xErrorStatus = pdFAIL;
\r
286 /* Inspect the status of the reg test tasks. */
\r
287 if( sRegTestStatus != pdPASS )
\r
289 xErrorStatus = pdFAIL;
\r
292 /* Ensure that the demo software timer has expired
\r
293 mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT times in between
\r
294 each call of this function. A critical section is not required to access
\r
295 ulDemoSoftwareTimerCounter as the variable is only accessed from another
\r
296 software timer callback, and only one software timer callback can be
\r
297 executing at any time. */
\r
298 if( ( ulDemoSoftwareTimerCounter < ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT - 1 ) ) ||
\r
299 ( ulDemoSoftwareTimerCounter > ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT + 1 ) )
\r
302 xErrorStatus = pdFAIL;
\r
306 ulDemoSoftwareTimerCounter = 0UL;
\r
309 if( ( xErrorStatus == pdFAIL ) && ( xChangedTimerPeriodAlready == pdFALSE ) )
\r
311 /* An error has occurred, but the timer's period has not yet been changed,
\r
312 change it now, and remember that it has been changed. Shortening the
\r
313 timer's period means the LED will toggle at a faster rate, giving a
\r
314 visible indication that something has gone wrong. */
\r
315 xChangedTimerPeriodAlready = pdTRUE;
\r
317 /* This call to xTimerChangePeriod() uses a zero block time. Functions
\r
318 called from inside of a timer callback function must *never* attempt to
\r
320 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
323 /* Toggle the LED. The toggle rate will depend on whether or not an error
\r
324 has been found in any tasks. */
\r
325 mainLED_0 = !mainLED_0;
\r
327 /*-----------------------------------------------------------*/
\r
329 int __low_level_init(void)
\r
331 unsigned portCHAR ucResetFlag = RESF;
\r
333 portDISABLE_INTERRUPTS();
\r
335 /* Clock Configuration:
\r
336 In this port, to use the internal high speed clock source of the
\r
337 microcontroller, define the configCLOCK_SOURCE as 1 in FreeRTOSConfig.h. To
\r
338 use an external clock define configCLOCK_SOURCE as 0. */
\r
339 #if configCLOCK_SOURCE == 1
\r
360 unsigned char ucTempStabset, ucTempStabWait;
\r
366 ucTempStabset = 0xFF;
\r
370 ucTempStabWait = OSTC;
\r
371 ucTempStabWait &= ucTempStabset;
\r
373 while( ucTempStabWait != ucTempStabset );
\r
388 #endif /* configCLOCK_SOURCE == 1 */
\r
390 /* LED port initialization - set port register. */
\r
393 /* Set port mode register. */
\r
396 /* Switch pin initialization - enable pull-up resistor. */
\r
401 /*-----------------------------------------------------------*/
\r
403 void vRegTestError( void )
\r
405 /* Called by the RegTest tasks if an error is found. lRegTestStatus is
\r
406 inspected by the check task. */
\r
407 sRegTestStatus = pdFAIL;
\r
409 /* Do not return from here as the reg test tasks clobber all registers so
\r
410 function calls may not function correctly. */
\r
413 /*-----------------------------------------------------------*/
\r
415 void vApplicationMallocFailedHook( void )
\r
417 /* Called if a call to pvPortMalloc() fails because there is insufficient
\r
418 free memory available in the FreeRTOS heap. pvPortMalloc() is called
\r
419 internally by FreeRTOS API functions that create tasks, queues, software
\r
420 timers, and semaphores. The size of the FreeRTOS heap is set by the
\r
421 configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
\r
422 taskDISABLE_INTERRUPTS();
\r
425 /*-----------------------------------------------------------*/
\r
427 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
429 ( void ) pcTaskName;
\r
432 /* Run time stack overflow checking is performed if
\r
433 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
434 function is called if a stack overflow is detected. */
\r
435 taskDISABLE_INTERRUPTS();
\r
438 /*-----------------------------------------------------------*/
\r
440 void vApplicationIdleHook( void )
\r
442 volatile size_t xFreeHeapSpace;
\r
444 /* This is just a trivial example of an idle hook. It is called on each
\r
445 cycle of the idle task. It must *NOT* attempt to block. In this case the
\r
446 idle task just queries the amount of FreeRTOS heap that remains. See the
\r
447 memory management section on the http://www.FreeRTOS.org web site for memory
\r
448 management options. If there is a lot of heap memory free then the
\r
449 configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up
\r
451 xFreeHeapSpace = xPortGetFreeHeapSize();
\r