2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /******************************************************************************
\r
29 * NOTE 1: This project provides two demo applications. A simple blinky
\r
30 * style project, and a more comprehensive test and demo application. The
\r
31 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to
\r
32 * select between the two. See the notes on using
\r
33 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY in main.c. This file implements the
\r
34 * comprehensive version.
\r
36 * NOTE 2: This file only contains the source code that is specific to the
\r
37 * full demo. Generic functions, such FreeRTOS hook functions, and functions
\r
38 * required to configure the hardware, are defined in main.c.
\r
40 ******************************************************************************
\r
42 * main_full() creates a set of demo application tasks and software timers, then
\r
43 * starts the scheduler. The web documentation provides more details of the
\r
44 * standard demo application tasks, which provide no particular functionality,
\r
45 * but do provide a good example of how to use the FreeRTOS API.
\r
47 * In addition to the standard demo tasks, the following tasks and tests are
\r
48 * defined and/or created within this file:
\r
50 * "Reg test" tasks - These fill both the core and floating point registers with
\r
51 * known values, then check that each register maintains its expected value for
\r
52 * the lifetime of the task. Each task uses a different set of values. The reg
\r
53 * test tasks execute with a very low priority, so get preempted very
\r
54 * frequently. A register containing an unexpected value is indicative of an
\r
55 * error in the context switching mechanism.
\r
57 * "Check" task - The check task period is initially set to three seconds. The
\r
58 * task checks that all the standard demo tasks are not only still executing,
\r
59 * but are executing without reporting any errors. If the check task discovers
\r
60 * that a task has either stalled, or reported an error, then it changes its own
\r
61 * execution period from the initial three seconds, to just 200ms. The check
\r
62 * task also toggles an LED on each iteration of its loop. This provides a
\r
63 * visual indication of the system status: If the LED toggles every three
\r
64 * seconds, then no issues have been discovered. If the LED toggles every
\r
65 * 200ms, then an issue has been discovered with at least one task.
\r
68 /* Standard includes. */
\r
71 /* Kernel includes. */
\r
72 #include "FreeRTOS.h"
\r
77 /* Standard demo application includes. */
\r
79 #include "semtest.h"
\r
80 #include "dynamic.h"
\r
82 #include "blocktim.h"
\r
83 #include "countsem.h"
\r
84 #include "GenQTest.h"
\r
85 #include "recmutex.h"
\r
87 #include "partest.h"
\r
88 #include "comtest2.h"
\r
90 #include "TimerDemo.h"
\r
91 #include "QueueOverwrite.h"
\r
92 #include "IntQueue.h"
\r
93 #include "EventGroupsDemo.h"
\r
94 #include "TaskNotify.h"
\r
95 #include "IntSemTest.h"
\r
97 /* Renesas includes. */
\r
98 #include "rskrx231def.h"
\r
100 /* Priorities for the demo application tasks. */
\r
101 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1UL )
\r
102 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2UL )
\r
103 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3UL )
\r
104 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
105 #define mainUART_COMMAND_CONSOLE_STACK_SIZE ( configMINIMAL_STACK_SIZE * 3UL )
\r
106 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
107 #define mainQUEUE_OVERWRITE_PRIORITY ( tskIDLE_PRIORITY )
\r
109 /* The period of the check task, in ms, provided no errors have been reported by
\r
110 any of the standard demo tasks. ms are converted to the equivalent in ticks
\r
111 using the portTICK_PERIOD_MS constant. */
\r
112 #define mainNO_ERROR_CHECK_TASK_PERIOD pdMS_TO_TICKS( 3000UL )
\r
114 /* The period of the check task, in ms, if an error has been reported in one of
\r
115 the standard demo tasks. ms are converted to the equivalent in ticks using the
\r
116 portTICK_PERIOD_MS constant. */
\r
117 #define mainERROR_CHECK_TASK_PERIOD pdMS_TO_TICKS( 200UL )
\r
119 /* Parameters that are passed into the register check tasks solely for the
\r
120 purpose of ensuring parameters are passed into tasks correctly. */
\r
121 #define mainREG_TEST_1_PARAMETER ( ( void * ) 0x12121212UL )
\r
122 #define mainREG_TEST_2_PARAMETER ( ( void * ) 0x12345678UL )
\r
124 /* The base period used by the timer test tasks. */
\r
125 #define mainTIMER_TEST_PERIOD ( 50 )
\r
127 /*-----------------------------------------------------------*/
\r
130 * Entry point for the comprehensive demo (as opposed to the simple blinky
\r
133 void main_full( void );
\r
136 * The full demo includes some functionality called from the tick hook.
\r
138 void vFullDemoTickHook( void );
\r
141 * The check task, as described at the top of this file.
\r
143 static void prvCheckTask( void *pvParameters );
\r
146 * Register check tasks, and the tasks used to write over and check the contents
\r
147 * of the registers, as described at the top of this file. The nature of these
\r
148 * files necessitates that they are written in assembly, but the entry points
\r
149 * are kept in the C file for the convenience of checking the task parameter.
\r
151 static void prvRegTest1Task( void *pvParameters );
\r
152 static void prvRegTest2Task( void *pvParameters );
\r
153 void vRegTest1Implementation( void );
\r
154 void vRegTest2Implementation( void );
\r
157 * A high priority task that does nothing other than execute at a pseudo random
\r
158 * time to ensure the other test tasks don't just execute in a repeating
\r
161 static void prvPseudoRandomiser( void *pvParameters );
\r
163 /*-----------------------------------------------------------*/
\r
165 /* The following two variables are used to communicate the status of the
\r
166 register check tasks to the check task. If the variables keep incrementing,
\r
167 then the register check tasks have not discovered any errors. If a variable
\r
168 stops incrementing, then an error has been found. */
\r
169 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
\r
171 /*-----------------------------------------------------------*/
\r
173 void main_full( void )
\r
175 /* Start all the other standard demo/test tasks. They have no particular
\r
176 functionality, but do demonstrate how to use the FreeRTOS API and test the
\r
178 vStartInterruptQueueTasks();
\r
179 vStartDynamicPriorityTasks();
\r
180 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
181 vCreateBlockTimeTasks();
\r
182 vStartCountingSemaphoreTasks();
\r
183 vStartGenericQueueTasks( tskIDLE_PRIORITY );
\r
184 vStartRecursiveMutexTasks();
\r
185 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
186 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
187 vStartTimerDemoTask( mainTIMER_TEST_PERIOD );
\r
188 vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );
\r
189 vStartEventGroupTasks();
\r
190 vStartTaskNotifyTask();
\r
191 vStartInterruptSemaphoreTasks();
\r
193 /* Create the register check tasks, as described at the top of this file */
\r
194 xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
195 xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
197 /* Create the task that just adds a little random behaviour. */
\r
198 xTaskCreate( prvPseudoRandomiser, "Rnd", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 2, NULL );
\r
200 /* Create the task that performs the 'check' functionality, as described at
\r
201 the top of this file. */
\r
202 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
204 /* The set of tasks created by the following function call have to be
\r
205 created last as they keep account of the number of tasks they expect to see
\r
207 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
209 /* Start the scheduler. */
\r
210 vTaskStartScheduler();
\r
212 /* If all is well, the scheduler will now be running, and the following
\r
213 line will never be reached. If the following line does execute, then
\r
214 there was insufficient FreeRTOS heap memory available for the Idle and/or
\r
215 timer tasks to be created. See the memory management section on the
\r
216 FreeRTOS web site for more details on the FreeRTOS heap
\r
217 http://www.freertos.org/a00111.html. */
\r
220 /*-----------------------------------------------------------*/
\r
222 static void prvCheckTask( void *pvParameters )
\r
224 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;
\r
225 TickType_t xLastExecutionTime;
\r
226 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
\r
227 unsigned long ulErrorFound = pdFALSE;
\r
229 /* Just to stop compiler warnings. */
\r
230 ( void ) pvParameters;
\r
232 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
233 works correctly. */
\r
234 xLastExecutionTime = xTaskGetTickCount();
\r
236 /* Cycle for ever, delaying then checking all the other tasks are still
\r
237 operating without error. The onboard LED is toggled on each iteration.
\r
238 If an error is detected then the delay period is decreased from
\r
239 mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD. This has the
\r
240 effect of increasing the rate at which the onboard LED toggles, and in so
\r
241 doing gives visual feedback of the system status. */
\r
244 /* Delay until it is time to execute again. */
\r
245 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );
\r
247 /* Check all the demo tasks (other than the flash tasks) to ensure
\r
248 that they are all still running, and that none have detected an error. */
\r
249 if( xAreIntQueueTasksStillRunning() != pdTRUE )
\r
251 ulErrorFound |= 1UL << 0UL;
\r
254 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
256 ulErrorFound |= 1UL << 1UL;
\r
259 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
261 ulErrorFound |= 1UL << 2UL;
\r
264 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
266 ulErrorFound |= 1UL << 3UL;
\r
269 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
271 ulErrorFound |= 1UL << 4UL;
\r
274 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
276 ulErrorFound |= 1UL << 5UL;
\r
279 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
281 ulErrorFound |= 1UL << 6UL;
\r
284 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
286 ulErrorFound |= 1UL << 7UL;
\r
289 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
291 ulErrorFound |= 1UL << 8UL;
\r
294 if( xAreTimerDemoTasksStillRunning( ( TickType_t ) mainNO_ERROR_CHECK_TASK_PERIOD ) != pdPASS )
\r
296 ulErrorFound |= 1UL << 9UL;
\r
299 if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
301 ulErrorFound |= 1UL << 10UL;
\r
304 if( xIsQueueOverwriteTaskStillRunning() != pdPASS )
\r
306 ulErrorFound |= 1UL << 11UL;
\r
309 if( xAreEventGroupTasksStillRunning() != pdPASS )
\r
311 ulErrorFound |= 1UL << 12UL;
\r
314 if( xAreTaskNotificationTasksStillRunning() != pdTRUE )
\r
316 ulErrorFound |= 1UL << 13UL;
\r
319 if( xAreInterruptSemaphoreTasksStillRunning() != pdTRUE )
\r
321 ulErrorFound |= 1UL << 14UL;
\r
324 /* Check that the register test 1 task is still running. */
\r
325 if( ulLastRegTest1Value == ulRegTest1LoopCounter )
\r
327 ulErrorFound |= 1UL << 15UL;
\r
329 ulLastRegTest1Value = ulRegTest1LoopCounter;
\r
331 /* Check that the register test 2 task is still running. */
\r
332 if( ulLastRegTest2Value == ulRegTest2LoopCounter )
\r
334 ulErrorFound |= 1UL << 16UL;
\r
336 ulLastRegTest2Value = ulRegTest2LoopCounter;
\r
338 /* Toggle the check LED to give an indication of the system status. If
\r
339 the LED toggles every mainNO_ERROR_CHECK_TASK_PERIOD milliseconds then
\r
340 everything is ok. A faster toggle indicates an error. */
\r
343 if( ulErrorFound != pdFALSE )
\r
345 /* An error has been detected in one of the tasks - flash the LED
\r
346 at a higher frequency to give visible feedback that something has
\r
347 gone wrong (it might just be that the loop back connector required
\r
348 by the comtest tasks has not been fitted). */
\r
349 xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;
\r
353 /*-----------------------------------------------------------*/
\r
355 static void prvPseudoRandomiser( void *pvParameters )
\r
357 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL, ulMinDelay = pdMS_TO_TICKS( 35 );
\r
358 volatile uint32_t ulNextRand = ( uint32_t ) &pvParameters, ulValue;
\r
360 /* This task does nothing other than ensure there is a little bit of
\r
361 disruption in the scheduling pattern of the other tasks. Normally this is
\r
362 done by generating interrupts at pseudo random times. */
\r
365 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
\r
366 ulValue = ( ulNextRand >> 16UL ) & 0xffUL;
\r
368 if( ulValue < ulMinDelay )
\r
370 ulValue = ulMinDelay;
\r
373 vTaskDelay( ulValue );
\r
375 while( ulValue > 0 )
\r
377 __asm volatile( "NOP" );
\r
378 __asm volatile( "NOP" );
\r
379 __asm volatile( "NOP" );
\r
380 __asm volatile( "NOP" );
\r
381 __asm volatile( "NOP" );
\r
382 __asm volatile( "NOP" );
\r
383 __asm volatile( "NOP" );
\r
389 /*-----------------------------------------------------------*/
\r
391 void vFullDemoTickHook( void )
\r
393 /* The full demo includes a software timer demo/test that requires
\r
394 prodding periodically from the tick interrupt. */
\r
395 vTimerPeriodicISRTests();
\r
397 /* Call the periodic queue overwrite from ISR demo. */
\r
398 vQueueOverwritePeriodicISRDemo();
\r
400 /* Call the periodic event group from ISR demo. */
\r
401 vPeriodicEventGroupsProcessing();
\r
403 /* Use task notifications from an interrupt. */
\r
404 xNotifyTaskFromISR();
\r
406 /* Use mutexes from interrupts. */
\r
407 vInterruptSemaphorePeriodicTest();
\r
409 /*-----------------------------------------------------------*/
\r
411 /* This function is explained in the comments at the top of this file. */
\r
412 static void prvRegTest1Task( void *pvParameters )
\r
414 if( pvParameters != mainREG_TEST_1_PARAMETER )
\r
416 /* The parameter did not contain the expected value. */
\r
419 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
420 taskDISABLE_INTERRUPTS();
\r
424 /* This is an inline asm function that never returns. */
\r
425 vRegTest1Implementation();
\r
427 /*-----------------------------------------------------------*/
\r
429 /* This function is explained in the comments at the top of this file. */
\r
430 static void prvRegTest2Task( void *pvParameters )
\r
432 if( pvParameters != mainREG_TEST_2_PARAMETER )
\r
434 /* The parameter did not contain the expected value. */
\r
437 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
438 taskDISABLE_INTERRUPTS();
\r
442 /* This is an inline asm function that never returns. */
\r
443 vRegTest2Implementation();
\r
445 /*-----------------------------------------------------------*/
\r