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
29 /******************************************************************************
\r
30 * NOTE 1: This project provides two demo applications. A simple blinky
\r
31 * style project, and a more comprehensive test and demo application. The
\r
32 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to
\r
33 * select between the two. See the notes on using
\r
34 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY in main.c. This file implements the
\r
35 * comprehensive version.
\r
37 * NOTE 2: This file only contains the source code that is specific to the
\r
38 * full demo. Generic functions, such FreeRTOS hook functions, and functions
\r
39 * required to configure the hardware, are defined in main.c.
\r
41 ******************************************************************************
\r
43 * main_full() creates a set of demo application tasks and software timers, then
\r
44 * starts the scheduler. The web documentation provides more details of the
\r
45 * standard demo application tasks, which provide no particular functionality,
\r
46 * but do provide a good example of how to use the FreeRTOS API.
\r
48 * In addition to the standard demo tasks, the following tasks and tests are
\r
49 * defined and/or created within this file:
\r
51 * "FreeRTOS+CLI command console" - The command console uses SCI1 for its
\r
52 * input and output. The baud rate is set to 19200. Type "help" to see a list
\r
53 * of registered commands. The FreeRTOS+CLI license is different to the
\r
54 * FreeRTOS license, see http://www.FreeRTOS.org/cli for license and usage
\r
57 * "Reg test" tasks - These fill both the core and floating point registers with
\r
58 * known values, then check that each register maintains its expected value for
\r
59 * the lifetime of the task. Each task uses a different set of values. The reg
\r
60 * test tasks execute with a very low priority, so get preempted very
\r
61 * frequently. A register containing an unexpected value is indicative of an
\r
62 * error in the context switching mechanism.
\r
64 * "Check" task - The check task period is initially set to three seconds. The
\r
65 * task checks that all the standard demo tasks are not only still executing,
\r
66 * but are executing without reporting any errors. If the check task discovers
\r
67 * that a task has either stalled, or reported an error, then it changes its own
\r
68 * execution period from the initial three seconds, to just 200ms. The check
\r
69 * task also toggles an LED on each iteration of its loop. This provides a
\r
70 * visual indication of the system status: If the LED toggles every three
\r
71 * seconds, then no issues have been discovered. If the LED toggles every
\r
72 * 200ms, then an issue has been discovered with at least one task.
\r
75 /* Standard includes. */
\r
78 /* Kernel includes. */
\r
79 #include "FreeRTOS.h"
\r
84 /* Standard demo application includes. */
\r
86 #include "semtest.h"
\r
87 #include "dynamic.h"
\r
89 #include "blocktim.h"
\r
90 #include "countsem.h"
\r
91 #include "GenQTest.h"
\r
92 #include "recmutex.h"
\r
94 #include "partest.h"
\r
95 #include "comtest2.h"
\r
97 #include "TimerDemo.h"
\r
98 #include "QueueOverwrite.h"
\r
99 #include "IntQueue.h"
\r
100 #include "EventGroupsDemo.h"
\r
101 #include "TaskNotify.h"
\r
102 #include "IntSemTest.h"
\r
104 /* Renesas includes. */
\r
105 #include "r_cg_macrodriver.h"
\r
106 #include "r_cg_userdefine.h"
\r
108 /* Priorities for the demo application tasks. */
\r
109 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1UL )
\r
110 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2UL )
\r
111 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3UL )
\r
112 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
113 #define mainUART_COMMAND_CONSOLE_STACK_SIZE ( configMINIMAL_STACK_SIZE * 3UL )
\r
114 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
115 #define mainQUEUE_OVERWRITE_PRIORITY ( tskIDLE_PRIORITY )
\r
117 /* The priority used by the UART command console task. */
\r
118 #define mainUART_COMMAND_CONSOLE_TASK_PRIORITY ( configMAX_PRIORITIES - 2 )
\r
120 /* The period of the check task, in ms, provided no errors have been reported by
\r
121 any of the standard demo tasks. ms are converted to the equivalent in ticks
\r
122 using the portTICK_PERIOD_MS constant. */
\r
123 #define mainNO_ERROR_CHECK_TASK_PERIOD pdMS_TO_TICKS( 3000UL )
\r
125 /* The period of the check task, in ms, if an error has been reported in one of
\r
126 the standard demo tasks. ms are converted to the equivalent in ticks using the
\r
127 portTICK_PERIOD_MS constant. */
\r
128 #define mainERROR_CHECK_TASK_PERIOD pdMS_TO_TICKS( 200UL )
\r
130 /* Parameters that are passed into the register check tasks solely for the
\r
131 purpose of ensuring parameters are passed into tasks correctly. */
\r
132 #define mainREG_TEST_TASK_1_PARAMETER ( ( void * ) 0x12345678 )
\r
133 #define mainREG_TEST_TASK_2_PARAMETER ( ( void * ) 0x87654321 )
\r
135 /* The base period used by the timer test tasks. */
\r
136 #define mainTIMER_TEST_PERIOD ( 50 )
\r
138 /*-----------------------------------------------------------*/
\r
141 * Entry point for the comprehensive demo (as opposed to the simple blinky
\r
144 void main_full( void );
\r
147 * The full demo includes some functionality called from the tick hook.
\r
149 void vFullDemoTickHook( void );
\r
152 * The check task, as described at the top of this file.
\r
154 static void prvCheckTask( void *pvParameters );
\r
157 * Register check tasks, and the tasks used to write over and check the contents
\r
158 * of the FPU registers, as described at the top of this file. The nature of
\r
159 * these files necessitates that they are written in an assembly file, but the
\r
160 * entry points are kept in the C file for the convenience of checking the task
\r
163 static void prvRegTestTaskEntry1( void *pvParameters );
\r
164 extern void vRegTest1Implementation( void );
\r
165 static void prvRegTestTaskEntry2( void *pvParameters );
\r
166 extern void vRegTest2Implementation( void );
\r
169 * A high priority task that does nothing other than execute at a pseudo random
\r
170 * time to ensure the other test tasks don't just execute in a repeating
\r
173 static void prvPseudoRandomiser( void *pvParameters );
\r
176 * Register commands that can be used with FreeRTOS+CLI. The commands are
\r
177 * defined in CLI-Commands.c and File-Related-CLI-Command.c respectively.
\r
179 extern void vRegisterSampleCLICommands( void );
\r
182 * The task that manages the FreeRTOS+CLI input and output.
\r
184 extern void vUARTCommandConsoleStart( uint16_t usStackSize, UBaseType_t uxPriority );
\r
186 /*-----------------------------------------------------------*/
\r
188 /* The following two variables are used to communicate the status of the
\r
189 register check tasks to the check task. If the variables keep incrementing,
\r
190 then the register check tasks have not discovered any errors. If a variable
\r
191 stops incrementing, then an error has been found. */
\r
192 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
\r
194 /*-----------------------------------------------------------*/
\r
196 void main_full( void )
\r
198 /* Start all the other standard demo/test tasks. They have no particular
\r
199 functionality, but do demonstrate how to use the FreeRTOS API and test the
\r
201 vStartInterruptQueueTasks();
\r
202 vStartDynamicPriorityTasks();
\r
203 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
204 vCreateBlockTimeTasks();
\r
205 vStartCountingSemaphoreTasks();
\r
206 vStartGenericQueueTasks( tskIDLE_PRIORITY );
\r
207 vStartRecursiveMutexTasks();
\r
208 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
209 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
210 vStartTimerDemoTask( mainTIMER_TEST_PERIOD );
\r
211 vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );
\r
212 vStartEventGroupTasks();
\r
213 vStartTaskNotifyTask();
\r
214 vStartInterruptSemaphoreTasks();
\r
216 /* Create the register check tasks, as described at the top of this file */
\r
217 xTaskCreate( prvRegTestTaskEntry1, "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
218 xTaskCreate( prvRegTestTaskEntry2, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
220 /* Create the task that just adds a little random behaviour. */
\r
221 xTaskCreate( prvPseudoRandomiser, "Rnd", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
223 /* Start the tasks that implements the command console on the UART, as
\r
224 described above. */
\r
225 vUARTCommandConsoleStart( mainUART_COMMAND_CONSOLE_STACK_SIZE, mainUART_COMMAND_CONSOLE_TASK_PRIORITY );
\r
227 /* Register the standard CLI commands. */
\r
228 vRegisterSampleCLICommands();
\r
230 /* Create the task that performs the 'check' functionality, as described at
\r
231 the top of this file. */
\r
232 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
234 /* The set of tasks created by the following function call have to be
\r
235 created last as they keep account of the number of tasks they expect to see
\r
237 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
239 /* Start the scheduler. */
\r
240 vTaskStartScheduler();
\r
242 /* If all is well, the scheduler will now be running, and the following
\r
243 line will never be reached. If the following line does execute, then
\r
244 there was either insufficient FreeRTOS heap memory available for the idle
\r
245 and/or timer tasks to be created, or vTaskStartScheduler() was called from
\r
246 User mode. See the memory management section on the FreeRTOS web site for
\r
247 more details on the FreeRTOS heap http://www.freertos.org/a00111.html. The
\r
248 mode from which main() is called is set in the C start up code and must be
\r
249 a privileged mode (not user mode). */
\r
252 /*-----------------------------------------------------------*/
\r
254 static void prvCheckTask( void *pvParameters )
\r
256 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;
\r
257 TickType_t xLastExecutionTime;
\r
258 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
\r
259 unsigned long ulErrorFound = pdFALSE;
\r
261 /* Just to stop compiler warnings. */
\r
262 ( void ) pvParameters;
\r
264 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
265 works correctly. */
\r
266 xLastExecutionTime = xTaskGetTickCount();
\r
268 /* Cycle for ever, delaying then checking all the other tasks are still
\r
269 operating without error. The onboard LED is toggled on each iteration.
\r
270 If an error is detected then the delay period is decreased from
\r
271 mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD. This has the
\r
272 effect of increasing the rate at which the onboard LED toggles, and in so
\r
273 doing gives visual feedback of the system status. */
\r
276 /* Delay until it is time to execute again. */
\r
277 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );
\r
279 /* Check all the demo tasks (other than the flash tasks) to ensure
\r
280 that they are all still running, and that none have detected an error. */
\r
281 if( xAreIntQueueTasksStillRunning() != pdTRUE )
\r
283 ulErrorFound |= 1UL << 0UL;
\r
286 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
288 ulErrorFound |= 1UL << 1UL;
\r
291 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
293 ulErrorFound |= 1UL << 2UL;
\r
296 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
298 ulErrorFound |= 1UL << 3UL;
\r
301 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
303 ulErrorFound |= 1UL << 4UL;
\r
306 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
308 ulErrorFound |= 1UL << 5UL;
\r
311 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
313 ulErrorFound |= 1UL << 6UL;
\r
316 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
318 ulErrorFound |= 1UL << 7UL;
\r
321 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
323 ulErrorFound |= 1UL << 8UL;
\r
326 if( xAreTimerDemoTasksStillRunning( ( TickType_t ) mainNO_ERROR_CHECK_TASK_PERIOD ) != pdPASS )
\r
328 ulErrorFound |= 1UL << 9UL;
\r
331 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
333 ulErrorFound |= 1UL << 10UL;
\r
336 if( xIsQueueOverwriteTaskStillRunning() != pdPASS )
\r
338 ulErrorFound |= 1UL << 11UL;
\r
341 if( xAreEventGroupTasksStillRunning() != pdPASS )
\r
343 ulErrorFound |= 1UL << 12UL;
\r
346 if( xAreTaskNotificationTasksStillRunning() != pdTRUE )
\r
348 ulErrorFound |= 1UL << 13UL;
\r
351 if( xAreInterruptSemaphoreTasksStillRunning() != pdTRUE )
\r
353 ulErrorFound |= 1UL << 14UL;
\r
356 /* Check that the register test 1 task is still running. */
\r
357 if( ulLastRegTest1Value == ulRegTest1LoopCounter )
\r
359 ulErrorFound |= 1UL << 15UL;
\r
361 ulLastRegTest1Value = ulRegTest1LoopCounter;
\r
363 /* Check that the register test 2 task is still running. */
\r
364 if( ulLastRegTest2Value == ulRegTest2LoopCounter )
\r
366 ulErrorFound |= 1UL << 16UL;
\r
368 ulLastRegTest2Value = ulRegTest2LoopCounter;
\r
370 /* Toggle the check LED to give an indication of the system status. If
\r
371 the LED toggles every mainNO_ERROR_CHECK_TASK_PERIOD milliseconds then
\r
372 everything is ok. A faster toggle indicates an error. */
\r
375 if( ulErrorFound != pdFALSE )
\r
377 /* An error has been detected in one of the tasks - flash the LED
\r
378 at a higher frequency to give visible feedback that something has
\r
379 gone wrong (it might just be that the loop back connector required
\r
380 by the comtest tasks has not been fitted). */
\r
381 xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;
\r
385 /*-----------------------------------------------------------*/
\r
387 static void prvRegTestTaskEntry1( void *pvParameters )
\r
389 /* Although the regtest task is written in assembler, its entry point is
\r
390 written in C for convenience of checking the task parameter is being passed
\r
392 if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )
\r
394 /* The reg test task also tests the floating point registers. Tasks
\r
395 that use the floating point unit must call vPortTaskUsesFPU() before
\r
396 any floating point instructions are executed. */
\r
397 vPortTaskUsesFPU();
\r
399 /* Start the part of the test that is written in assembler. */
\r
400 vRegTest1Implementation();
\r
403 /* The following line will only execute if the task parameter is found to
\r
404 be incorrect. The check task will detect that the regtest loop counter is
\r
405 not being incremented and flag an error. */
\r
406 vTaskDelete( NULL );
\r
408 /*-----------------------------------------------------------*/
\r
410 static void prvRegTestTaskEntry2( void *pvParameters )
\r
412 /* Although the regtest task is written in assembler, its entry point is
\r
413 written in C for convenience of checking the task parameter is being passed
\r
415 if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )
\r
417 /* The reg test task also tests the floating point registers. Tasks
\r
418 that use the floating point unit must call vPortTaskUsesFPU() before
\r
419 any floating point instructions are executed. */
\r
420 vPortTaskUsesFPU();
\r
422 /* Start the part of the test that is written in assembler. */
\r
423 vRegTest2Implementation();
\r
426 /* The following line will only execute if the task parameter is found to
\r
427 be incorrect. The check task will detect that the regtest loop counter is
\r
428 not being incremented and flag an error. */
\r
429 vTaskDelete( NULL );
\r
431 /*-----------------------------------------------------------*/
\r
433 static void prvPseudoRandomiser( void *pvParameters )
\r
435 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL, ulMinDelay = pdMS_TO_TICKS( 35 );
\r
436 volatile uint32_t ulNextRand = ( uint32_t ) &pvParameters, ulValue;
\r
438 /* This task does nothing other than ensure there is a little bit of
\r
439 disruption in the scheduling pattern of the other tasks. Normally this is
\r
440 done by generating interrupts at pseudo random times. */
\r
443 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
\r
444 ulValue = ( ulNextRand >> 16UL ) & 0xffUL;
\r
446 if( ulValue < ulMinDelay )
\r
448 ulValue = ulMinDelay;
\r
451 vTaskDelay( ulValue );
\r
453 while( ulValue > 0 )
\r
455 __asm volatile( "NOP" );
\r
456 __asm volatile( "NOP" );
\r
457 __asm volatile( "NOP" );
\r
458 __asm volatile( "NOP" );
\r
464 /*-----------------------------------------------------------*/
\r
466 void vFullDemoTickHook( void )
\r
468 /* The full demo includes a software timer demo/test that requires
\r
469 prodding periodically from the tick interrupt. */
\r
470 vTimerPeriodicISRTests();
\r
472 /* Call the periodic queue overwrite from ISR demo. */
\r
473 vQueueOverwritePeriodicISRDemo();
\r
475 /* Call the periodic event group from ISR demo. */
\r
476 vPeriodicEventGroupsProcessing();
\r
478 /* Use task notifications from an interrupt. */
\r
479 xNotifyTaskFromISR();
\r
481 /* Use mutexes from interrupts. */
\r
482 vInterruptSemaphorePeriodicTest();
\r