2 FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\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
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\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
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\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
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
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
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\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
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
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
75 /* ****************************************************************************
\r
76 * This project includes a lot of tasks and tests and is therefore complex.
\r
77 * If you would prefer a much simpler project to get started with then select
\r
78 * the 'Blinky' build configuration within the HEW IDE. The Blinky build
\r
79 * configuration uses main-blinky.c instead of main-full.c.
\r
80 * ****************************************************************************
\r
82 * Creates all the demo application tasks, then starts the scheduler. The web
\r
83 * documentation provides more details of the standard demo application tasks,
\r
84 * which provide no particular functionality but do provide a good example of
\r
85 * how to use the FreeRTOS API.
\r
87 * In addition to the standard demo tasks, the following tasks and tests are
\r
88 * defined and/or created within this file:
\r
90 * "Reg test" tasks - These fill the registers with known values, then
\r
91 * repeatedly check that each register still contains its expected value for
\r
92 * the lifetime of the tasks. Each task uses different values. The tasks run
\r
93 * with very low priority so get preempted very frequently. A check variable
\r
94 * is incremented on each iteration of the test loop. A register containing an
\r
95 * unexpected value is indicative of an error in the context switching
\r
96 * mechanism and will result in a branch to a null loop - which in turn will
\r
97 * prevent the check variable from incrementing any further and allow the check
\r
98 * timer (described below) to determine that an error has occurred. The nature
\r
99 * of the reg test tasks necessitates that they are written in assembly code.
\r
101 * "Check Timer" and Callback Function - The check timer period is initially
\r
102 * set to five seconds. The check timer callback function checks that all the
\r
103 * standard demo tasks are not only still executing, but are executing without
\r
104 * reporting any errors. If the check timer discovers that a task has either
\r
105 * stalled, or reported an error, then it changes its own period from the
\r
106 * initial five seconds, to just 200ms. The check timer callback function
\r
107 * also toggles LED 3 each time it is called. This provides a visual
\r
108 * indication of the system status: If the LED toggles every five seconds,
\r
109 * then no issues have been discovered. If the LED toggles every 200ms, then
\r
110 * an issue has been discovered with at least one task.
\r
112 * "High frequency timer test" - A high frequency periodic interrupt is
\r
113 * generated using a timer - the interrupt is assigned a priority above
\r
114 * configMAX_SYSCALL_INTERRUPT_PRIORITY, so will not be effected by anything
\r
115 * the kernel is doing. The frequency and priority of the interrupt, in
\r
116 * combination with other standard tests executed in this demo, will result
\r
117 * in interrupts nesting at least 3 and probably 4 deep. This test is only
\r
118 * included in build configurations that have the optimiser switched on.
\r
120 * "Button and LCD test" - This creates two tasks. The first simply scrolls
\r
121 * a message back and forth along the top line of the LCD display. If no
\r
122 * buttons are pushed, the second also scrolls a message back and forth, but
\r
123 * along the bottom line of the display. The automatic scrolling of the second
\r
124 * line of the display can be started and stopped using button SW2. Once
\r
125 * stopped it can then be manually nudged left using button SW3, and manually
\r
126 * nudged right using button SW1. Button pushes generate an interrupt, and the
\r
127 * interrupt communicates with the task using a queue.
\r
129 * *NOTE 1* vApplicationSetupTimerInterrupt() is called by the kernel to let
\r
130 * the application set up a timer to generate the tick interrupt. In this
\r
131 * example a compare match timer is used for this purpose.
\r
133 * *NOTE 2* The CPU must be in Supervisor mode when the scheduler is started.
\r
134 * The PowerON_Reset_PC() supplied in resetprg.c with this demo has
\r
135 * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.
\r
137 * *NOTE 3* The IntQueue common demo tasks test interrupt nesting and make use
\r
138 * of all the 8bit timers (as two cascaded 16bit units).
\r
141 /* Standard includes. */
\r
142 #include <string.h>
\r
144 /* Hardware specific includes. */
\r
145 #include "iodefine.h"
\r
147 /* Kernel includes. */
\r
148 #include "FreeRTOS.h"
\r
150 #include "timers.h"
\r
151 #include "semphr.h"
\r
153 /* Standard demo includes. */
\r
154 #include "partest.h"
\r
156 #include "IntQueue.h"
\r
157 #include "BlockQ.h"
\r
159 #include "integer.h"
\r
160 #include "blocktim.h"
\r
161 #include "semtest.h"
\r
163 #include "GenQTest.h"
\r
165 #include "recmutex.h"
\r
167 /* Demo specific tasks. */
\r
168 #include "ButtonAndLCD.h"
\r
170 /* Peripheral includes. */
\r
173 /* Values that are passed into the reg test tasks using the task parameter.
\r
174 The tasks check that the values are passed in correctly. */
\r
175 #define mainREG_TEST_1_PARAMETER ( 0x12121212UL )
\r
176 #define mainREG_TEST_2_PARAMETER ( 0x12345678UL )
\r
178 /* Priorities at which the tasks are created. */
\r
179 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
180 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
181 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
182 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
183 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
184 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
185 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
186 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
188 /* The LED toggled by the check timer. */
\r
189 #define mainCHECK_LED ( 3 )
\r
191 /* The period at which the check timer will expire, in ms, provided no errors
\r
192 have been reported by any of the standard demo tasks. ms are converted to the
\r
193 equivalent in ticks using the portTICK_RATE_MS constant. */
\r
194 #define mainCHECK_TIMER_PERIOD_MS ( 5000UL / portTICK_RATE_MS )
\r
196 /* The period at which the check timer will expire, in ms, if an error has been
\r
197 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
198 in ticks using the portTICK_RATE_MS constant. */
\r
199 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_RATE_MS )
\r
201 /* A block time of zero simple means "Don't Block". */
\r
202 #define mainDONT_BLOCK ( 0UL )
\r
205 * vApplicationMallocFailedHook() will only be called if
\r
206 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
207 * function that will execute if a call to pvPortMalloc() fails.
\r
208 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
209 * semaphore is created. It is also called by various parts of the demo
\r
212 void vApplicationMallocFailedHook( void );
\r
215 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
216 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
217 * of the idle task. It is essential that code added to this hook function
\r
218 * never attempts to block in any way (for example, call xQueueReceive() with
\r
219 * a block time specified). If the application makes use of the vTaskDelete()
\r
220 * API function (as this demo application does) then it is also important that
\r
221 * vApplicationIdleHook() is permitted to return to its calling function because
\r
222 * it is the responsibility of the idle task to clean up memory allocated by the
\r
223 * kernel to any task that has since been deleted.
\r
225 void vApplicationIdleHook( void );
\r
228 * vApplicationStackOverflowHook() will only be called if
\r
229 * configCHECK_FOR_STACK_OVERFLOW is set to a non-zero value. The handle and
\r
230 * name of the offending task should be passed in the function parameters, but
\r
231 * it is possible that the stack overflow will have corrupted these - in which
\r
232 * case pxCurrentTCB can be inspected to find the same information.
\r
234 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName );
\r
237 * The reg test tasks as described at the top of this file.
\r
239 static void prvRegTest1Task( void *pvParameters );
\r
240 static void prvRegTest2Task( void *pvParameters );
\r
243 * The actual implementation of the reg test functionality, which, because of
\r
244 * the direct register access, have to be in assembly.
\r
246 static void prvRegTest1Implementation( void );
\r
247 static void prvRegTest2Implementation( void );
\r
250 * The check timer callback function, as described at the top of this file.
\r
252 static void prvCheckTimerCallback( xTimerHandle xTimer );
\r
255 /*-----------------------------------------------------------*/
\r
257 /* Variables that are incremented on each iteration of the reg test tasks -
\r
258 provided the tasks have not reported any errors. The check timer inspects these
\r
259 variables to ensure they are still incrementing as expected. If a variable
\r
260 stops incrementing then it is likely that its associate task has stalled. */
\r
261 unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
263 /* The check timer. This uses prvCheckTimerCallback() as its callback
\r
265 static xTimerHandle xCheckTimer = NULL;
\r
267 /*-----------------------------------------------------------*/
\r
271 extern void HardwareSetup( void );
\r
273 /* Renesas provided CPU configuration routine. The clocks are configured in
\r
277 /* Turn all LEDs off. */
\r
278 vParTestInitialise();
\r
280 /* Start the reg test tasks which test the context switching mechanism. */
\r
281 xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
282 xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
284 /* The button and LCD tasks, as described at the top of this file. */
\r
285 vStartButtonAndLCDDemo();
\r
287 /* Create the standard demo tasks. */
\r
288 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
289 vCreateBlockTimeTasks();
\r
290 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
291 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
292 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
293 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
294 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
295 vStartQueuePeekTasks();
\r
296 vStartRecursiveMutexTasks();
\r
297 vStartInterruptQueueTasks();
\r
299 /* The suicide tasks must be created last as they need to know how many
\r
300 tasks were running prior to their creation in order to ascertain whether
\r
301 or not the correct/expected number of tasks are running at any given time. */
\r
302 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
304 /* Create the software timer that performs the 'check' functionality,
\r
305 as described at the top of this file. */
\r
306 xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */
\r
307 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 5000ms (5s). */
\r
308 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
309 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
310 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
313 configASSERT( xCheckTimer );
\r
315 /* Start the check timer. It will actually start when the scheduler is
\r
317 xTimerStart( xCheckTimer, mainDONT_BLOCK );
\r
319 /* Start the tasks running. */
\r
320 vTaskStartScheduler();
\r
322 /* If all is well we will never reach here as the scheduler will now be
\r
323 running. If we do reach here then it is likely that there was insufficient
\r
324 heap available for the idle task to be created. */
\r
327 /*-----------------------------------------------------------*/
\r
329 static void prvCheckTimerCallback( xTimerHandle xTimer )
\r
331 static long lChangedTimerPeriodAlready = pdFALSE, lErrorStatus = pdPASS;
\r
332 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
334 /* Check the standard demo tasks are running without error. */
\r
335 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
337 lErrorStatus = pdFAIL;
\r
339 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
341 lErrorStatus = pdFAIL;
\r
343 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
345 lErrorStatus = pdFAIL;
\r
347 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
349 lErrorStatus = pdFAIL;
\r
351 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
353 lErrorStatus = pdFAIL;
\r
355 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
357 lErrorStatus = pdFAIL;
\r
359 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
361 lErrorStatus = pdFAIL;
\r
363 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
365 lErrorStatus = pdFAIL;
\r
367 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
369 lErrorStatus = pdFAIL;
\r
371 else if( xAreIntQueueTasksStillRunning() != pdPASS )
\r
373 lErrorStatus = pdFAIL;
\r
376 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
377 their loop counters if they encounter an error. */
\r
378 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
380 lErrorStatus = pdFAIL;
\r
383 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
385 lErrorStatus = pdFAIL;
\r
388 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
389 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
391 /* Toggle the check LED to give an indication of the system status. If
\r
392 the LED toggles every 5 seconds then everything is ok. A faster toggle
\r
393 indicates an error. */
\r
394 vParTestToggleLED( mainCHECK_LED );
\r
396 /* Was an error detected this time through the callback execution? */
\r
397 if( lErrorStatus != pdPASS )
\r
399 if( lChangedTimerPeriodAlready == pdFALSE )
\r
401 lChangedTimerPeriodAlready = pdTRUE;
\r
403 /* This call to xTimerChangePeriod() uses a zero block time.
\r
404 Functions called from inside of a timer callback function must
\r
405 *never* attempt to block. */
\r
406 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
410 /*-----------------------------------------------------------*/
\r
412 /* The RX port uses this callback function to configure its tick interrupt.
\r
413 This allows the application to choose the tick interrupt source. */
\r
414 void vApplicationSetupTimerInterrupt( void )
\r
416 /* Enable compare match timer 0. */
\r
419 /* Interrupt on compare match. */
\r
420 CMT0.CMCR.BIT.CMIE = 1;
\r
422 /* Set the compare match value. */
\r
423 CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );
\r
425 /* Divide the PCLK by 8. */
\r
426 CMT0.CMCR.BIT.CKS = 0;
\r
428 /* Enable the interrupt... */
\r
429 _IEN( _CMT0_CMI0 ) = 1;
\r
431 /* ...and set its priority to the application defined kernel priority. */
\r
432 _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
\r
434 /* Start the timer. */
\r
435 CMT.CMSTR0.BIT.STR0 = 1;
\r
437 /*-----------------------------------------------------------*/
\r
439 /* This function is explained by the comments above its prototype at the top
\r
441 void vApplicationMallocFailedHook( void )
\r
445 /*-----------------------------------------------------------*/
\r
447 /* This function is explained by the comments above its prototype at the top
\r
449 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
453 /*-----------------------------------------------------------*/
\r
455 /* This function is explained by the comments above its prototype at the top
\r
457 void vApplicationIdleHook( void )
\r
459 /* If this is being executed then the kernel has been started. Start the high
\r
460 frequency timer test as described at the top of this file. This is only
\r
461 included in the optimised build configuration - otherwise it takes up too much
\r
462 CPU time and can disrupt other tests. */
\r
463 #ifdef INCLUDE_HIGH_FREQUENCY_TIMER_TEST
\r
464 static portBASE_TYPE xTimerTestStarted = pdFALSE;
\r
465 extern void vSetupHighFrequencyTimer( void );
\r
466 if( xTimerTestStarted == pdFALSE )
\r
468 vSetupHighFrequencyTimer();
\r
469 xTimerTestStarted = pdTRUE;
\r
473 /*-----------------------------------------------------------*/
\r
475 /* This function is explained in the comments at the top of this file. */
\r
476 static void prvRegTest1Task( void *pvParameters )
\r
478 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )
\r
480 /* The parameter did not contain the expected value. */
\r
483 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
484 taskDISABLE_INTERRUPTS();
\r
488 /* This is an inline asm function that never returns. */
\r
489 prvRegTest1Implementation();
\r
491 /*-----------------------------------------------------------*/
\r
493 /* This function is explained in the comments at the top of this file. */
\r
494 static void prvRegTest2Task( void *pvParameters )
\r
496 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )
\r
498 /* The parameter did not contain the expected value. */
\r
501 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
502 taskDISABLE_INTERRUPTS();
\r
506 /* This is an inline asm function that never returns. */
\r
507 prvRegTest2Implementation();
\r
509 /*-----------------------------------------------------------*/
\r
511 /* This function is explained in the comments at the top of this file. */
\r
512 #pragma inline_asm prvRegTest1Implementation
\r
513 static void prvRegTest1Implementation( void )
\r
515 ; Put a known value in each register.
\r
532 ; Loop, checking each itteration that each register still contains the
\r
536 ; Push the registers that are going to get clobbered.
\r
539 ; Increment the loop counter to show this task is still getting CPU time.
\r
540 MOV.L #_ulRegTest1CycleCount, R14
\r
545 ; Yield to extend the text coverage. Set the bit in the ITU SWINTR register.
\r
547 MOV.L #0872E0H, R15
\r
552 ; Restore the clobbered registers.
\r
555 ; Now compare each register to ensure it still contains the value that was
\r
556 ; set before this loop was entered.
\r
588 ; All comparisons passed, start a new itteratio of this loop.
\r
592 ; A compare failed, just loop here so the loop counter stops incrementing
\r
593 ; causing the check timer to indicate the error.
\r
596 /*-----------------------------------------------------------*/
\r
598 /* This function is explained in the comments at the top of this file. */
\r
599 #pragma inline_asm prvRegTest2Implementation
\r
600 static void prvRegTest2Implementation( void )
\r
602 ; Put a known value in each register.
\r
619 ; Loop, checking on each itteration that each register still contains the
\r
623 ; Push the registers that are going to get clobbered.
\r
626 ; Increment the loop counter to show this task is still getting CPU time.
\r
627 MOV.L #_ulRegTest2CycleCount, R14
\r
632 ; Restore the clobbered registers.
\r
666 ; All comparisons passed, start a new itteratio of this loop.
\r
670 ; A compare failed, just loop here so the loop counter stops incrementing
\r
671 ; - causing the check timer to indicate the error.
\r
674 /*-----------------------------------------------------------*/
\r