2 * FreeRTOS Kernel V10.0.1
\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.
\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 * This project includes a lot of tasks and tests and is therefore complex.
\r
30 * If you would prefer a much simpler project to get started with then select
\r
31 * the 'Blinky' build configuration within the HEW IDE.
\r
32 * ****************************************************************************
\r
34 * Creates all the demo application tasks, then starts the scheduler. The web
\r
35 * documentation provides more details of the standard demo application tasks,
\r
36 * which provide no particular functionality but do provide a good example of
\r
37 * how to use the FreeRTOS API. The tasks defined in flop.c are included in the
\r
38 * set of standard demo tasks to ensure the floating point unit gets some
\r
41 * In addition to the standard demo tasks, the following tasks and tests are
\r
42 * defined and/or created within this file:
\r
44 * Webserver ("uIP") task - This serves a number of dynamically generated WEB
\r
45 * pages to a standard WEB browser. The IP and MAC addresses are configured by
\r
46 * constants defined at the bottom of FreeRTOSConfig.h. Use either a standard
\r
47 * Ethernet cable to connect through a hug, or a cross over (point to point)
\r
48 * cable to connect directly. Ensure the IP address used is compatible with the
\r
49 * IP address of the machine running the browser - the easiest way to achieve
\r
50 * this is to ensure the first three octets of the IP addresses are the same.
\r
52 * "Reg test" tasks - These fill the registers with known values, then check
\r
53 * that each register still contains its expected value. Each task uses
\r
54 * different values. The tasks run with very low priority so get preempted
\r
55 * very frequently. A check variable is incremented on each iteration of the
\r
56 * test loop. A register containing an unexpected value is indicative of an
\r
57 * error in the context switching mechanism and will result in a branch to a
\r
58 * null loop - which in turn will prevent the check variable from incrementing
\r
59 * any further and allow the check task (described below) to determine that an
\r
60 * error has occurred. The nature of the reg test tasks necessitates that they
\r
61 * are written in assembly code.
\r
63 * "Check" task - This only executes every five seconds but has a high priority
\r
64 * to ensure it gets processor time. Its main function is to check that all the
\r
65 * standard demo tasks are still operational. While no errors have been
\r
66 * discovered the check task will toggle LED 5 every 5 seconds - the toggle
\r
67 * rate increasing to 200ms being a visual indication that at least one task has
\r
68 * reported unexpected behaviour.
\r
70 * "High frequency timer test" - A high frequency periodic interrupt is
\r
71 * generated using a timer - the interrupt is assigned a priority above
\r
72 * configMAX_SYSCALL_INTERRUPT_PRIORITY so should not be effected by anything
\r
73 * the kernel is doing. The frequency and priority of the interrupt, in
\r
74 * combination with other standard tests executed in this demo, should result
\r
75 * in interrupts nesting at least 3 and probably 4 deep. This test is only
\r
76 * included in build configurations that have the optimiser switched on. In
\r
77 * optimised builds the count of high frequency ticks is used as the time base
\r
78 * for the run time stats.
\r
80 * *NOTE 1* If LED5 is toggling every 5 seconds then all the demo application
\r
81 * tasks are executing as expected and no errors have been reported in any
\r
82 * tasks. The toggle rate increasing to 200ms indicates that at least one task
\r
83 * has reported unexpected behaviour.
\r
85 * *NOTE 2* vApplicationSetupTimerInterrupt() is called by the kernel to let
\r
86 * the application set up a timer to generate the tick interrupt. In this
\r
87 * example a compare match timer is used for this purpose.
\r
89 * *NOTE 3* The CPU must be in Supervisor mode when the scheduler is started.
\r
90 * The PowerON_Reset_PC() supplied in resetprg.c with this demo has
\r
91 * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.
\r
93 * *NOTE 4* The IntQueue common demo tasks test interrupt nesting and make use
\r
94 * of all the 8bit timers (as two cascaded 16bit units).
\r
97 /* Standard includes. */
\r
101 /* Hardware specific includes. */
\r
102 #include <iorx62n.h>
\r
104 /* Kernel includes. */
\r
105 #include "FreeRTOS.h"
\r
108 /* Standard demo includes. */
\r
109 #include "partest.h"
\r
111 #include "IntQueue.h"
\r
112 #include "BlockQ.h"
\r
114 #include "integer.h"
\r
115 #include "blocktim.h"
\r
116 #include "semtest.h"
\r
118 #include "GenQTest.h"
\r
120 #include "recmutex.h"
\r
123 /* Values that are passed into the reg test tasks using the task parameter. The
\r
124 tasks check that the values are passed in correctly. */
\r
125 #define mainREG_TEST_1_PARAMETER ( 0x12121212UL )
\r
126 #define mainREG_TEST_2_PARAMETER ( 0x12345678UL )
\r
128 /* Priorities at which the tasks are created. */
\r
129 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
130 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
131 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
132 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
133 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
134 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
135 #define mainuIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
136 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
137 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
138 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
140 /* The WEB server uses string handling functions, which in turn use a bit more
\r
141 stack than most of the other tasks. */
\r
142 #define mainuIP_STACK_SIZE ( configMINIMAL_STACK_SIZE * 3 )
\r
144 /* The LED toggled by the check task. */
\r
145 #define mainCHECK_LED ( 5 )
\r
147 /* The rate at which mainCHECK_LED will toggle when all the tasks are running
\r
148 without error. Controlled by the check task as described at the top of this
\r
150 #define mainNO_ERROR_CYCLE_TIME ( 5000 / portTICK_PERIOD_MS )
\r
152 /* The rate at which mainCHECK_LED will toggle when an error has been reported
\r
153 by at least one task. Controlled by the check task as described at the top of
\r
155 #define mainERROR_CYCLE_TIME ( 200 / portTICK_PERIOD_MS )
\r
157 /* For outputing debug console messages - just maps to printf. */
\r
159 #define xPrintf( x ) printf( x )
\r
161 #define xPrintf( x ) ( void ) x
\r
165 * vApplicationMallocFailedHook() will only be called if
\r
166 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
167 * function that will execute if a call to pvPortMalloc() fails.
\r
168 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
169 * semaphore is created. It is also called by various parts of the demo
\r
172 void vApplicationMallocFailedHook( void );
\r
175 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
176 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
177 * of the idle task. It is essential that code added to this hook function
\r
178 * never attempts to block in any way (for example, call xQueueReceive() with
\r
179 * a block time specified). If the application makes use of the vTaskDelete()
\r
180 * API function (as this demo application does) then it is also important that
\r
181 * vApplicationIdleHook() is permitted to return to its calling function because
\r
182 * it is the responsibility of the idle task to clean up memory allocated by the
\r
183 * kernel to any task that has since been deleted.
\r
185 void vApplicationIdleHook( void );
\r
188 * vApplicationStackOverflowHook() will only be called if
\r
189 * configCHECK_FOR_STACK_OVERFLOW is set to a non-zero value. The handle and
\r
190 * name of the offending task should be passed in the function parameters, but
\r
191 * it is possible that the stack overflow will have corrupted these - in which
\r
192 * case pxCurrentTCB can be inspected to find the same information.
\r
194 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
197 * The reg test tasks as described at the top of this file.
\r
199 static void prvRegTest1Task( void *pvParameters );
\r
200 static void prvRegTest2Task( void *pvParameters );
\r
203 * The actual implementation of the reg test functionality, which, because of
\r
204 * the direct register access, have to be in assembly.
\r
206 extern void prvRegTest1Implementation( void );
\r
207 extern void prvRegTest2Implementation( void );
\r
211 * The check task as described at the top of this file.
\r
213 static void prvCheckTask( void *pvParameters );
\r
216 * Contains the implementation of the WEB server.
\r
218 extern void vuIP_Task( void *pvParameters );
\r
220 /*-----------------------------------------------------------*/
\r
222 /* Variables that are incremented on each iteration of the reg test tasks -
\r
223 provided the tasks have not reported any errors. The check task inspects these
\r
224 variables to ensure they are still incrementing as expected. If a variable
\r
225 stops incrementing then it is likely that its associate task has stalled. */
\r
226 unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
228 /* The status message that is displayed at the bottom of the "task stats" web
\r
229 page, which is served by the uIP task. This will report any errors picked up
\r
230 by the reg test task. */
\r
231 static const char *pcStatusMessage = NULL;
\r
233 /*-----------------------------------------------------------*/
\r
237 extern void HardwareSetup( void );
\r
239 /* Renesas provided CPU configuration routine. The clocks are configured in
\r
243 xPrintf( "http://www.FreeRTOS.org\r\n" );
\r
245 /* Start the reg test tasks which test the context switching mechanism. */
\r
246 xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
247 xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
249 /* The web server task. */
\r
250 xTaskCreate( vuIP_Task, "uIP", mainuIP_STACK_SIZE, NULL, mainuIP_TASK_PRIORITY, NULL );
\r
252 /* Start the check task as described at the top of this file. */
\r
253 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE * 3, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
255 /* Create the standard demo tasks. */
\r
256 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
257 vCreateBlockTimeTasks();
\r
258 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
259 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
260 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
261 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
262 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
263 vStartQueuePeekTasks();
\r
264 vStartRecursiveMutexTasks();
\r
265 vStartInterruptQueueTasks();
\r
266 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
268 /* The suicide tasks must be created last as they need to know how many
\r
269 tasks were running prior to their creation in order to ascertain whether
\r
270 or not the correct/expected number of tasks are running at any given time. */
\r
271 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
273 /* Start the tasks running. */
\r
274 vTaskStartScheduler();
\r
276 /* If all is well we will never reach here as the scheduler will now be
\r
277 running. If we do reach here then it is likely that there was insufficient
\r
278 heap available for the idle task to be created. */
\r
281 /*-----------------------------------------------------------*/
\r
283 static void prvCheckTask( void *pvParameters )
\r
285 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
286 TickType_t xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;
\r
287 extern void vSetupHighFrequencyTimer( void );
\r
289 /* If this is being executed then the kernel has been started. Start the high
\r
290 frequency timer test as described at the top of this file. This is only
\r
291 included in the optimised build configuration - otherwise it takes up too much
\r
293 #ifdef INCLUDE_HIGH_FREQUENCY_TIMER_TEST
\r
294 vSetupHighFrequencyTimer();
\r
297 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
298 xNextWakeTime = xTaskGetTickCount();
\r
302 /* Place this task in the blocked state until it is time to run again. */
\r
303 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
305 /* Check the standard demo tasks are running without error. */
\r
306 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
308 /* Increase the rate at which this task cycles, which will increase the
\r
309 rate at which mainCHECK_LED flashes to give visual feedback that an error
\r
311 pcStatusMessage = "Error: GenQueue";
\r
312 xPrintf( pcStatusMessage );
\r
315 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
317 pcStatusMessage = "Error: QueuePeek\r\n";
\r
318 xPrintf( pcStatusMessage );
\r
321 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
323 pcStatusMessage = "Error: BlockQueue\r\n";
\r
324 xPrintf( pcStatusMessage );
\r
327 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
329 pcStatusMessage = "Error: BlockTime\r\n";
\r
330 xPrintf( pcStatusMessage );
\r
333 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
335 pcStatusMessage = "Error: SemTest\r\n";
\r
336 xPrintf( pcStatusMessage );
\r
339 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
341 pcStatusMessage = "Error: PollQueue\r\n";
\r
342 xPrintf( pcStatusMessage );
\r
345 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
347 pcStatusMessage = "Error: Death\r\n";
\r
348 xPrintf( pcStatusMessage );
\r
351 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
353 pcStatusMessage = "Error: IntMath\r\n";
\r
354 xPrintf( pcStatusMessage );
\r
357 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
359 pcStatusMessage = "Error: RecMutex\r\n";
\r
360 xPrintf( pcStatusMessage );
\r
363 if( xAreIntQueueTasksStillRunning() != pdPASS )
\r
365 pcStatusMessage = "Error: IntQueue\r\n";
\r
366 xPrintf( pcStatusMessage );
\r
369 if( xAreMathsTaskStillRunning() != pdPASS )
\r
371 pcStatusMessage = "Error: Flop\r\n";
\r
372 xPrintf( pcStatusMessage );
\r
375 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
376 their loop counters if they encounter an error. */
\r
377 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
379 pcStatusMessage = "Error: RegTest1\r\n";
\r
380 xPrintf( pcStatusMessage );
\r
383 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
385 pcStatusMessage = "Error: RegTest2\r\n";
\r
386 xPrintf( pcStatusMessage );
\r
389 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
390 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
392 /* Toggle the check LED to give an indication of the system status. If
\r
393 the LED toggles every 5 seconds then everything is ok. A faster toggle
\r
394 indicates an error. */
\r
395 vParTestToggleLED( mainCHECK_LED );
\r
397 /* Ensure the LED toggles at a faster rate if an error has occurred. */
\r
398 if( pcStatusMessage != NULL )
\r
400 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
404 /*-----------------------------------------------------------*/
\r
406 /* The RX port uses this callback function to configure its tick interrupt.
\r
407 This allows the application to choose the tick interrupt source. */
\r
408 void vApplicationSetupTimerInterrupt( void )
\r
410 /* Enable compare match timer 0. */
\r
413 /* Interrupt on compare match. */
\r
414 CMT0.CMCR.BIT.CMIE = 1;
\r
416 /* Set the compare match value. */
\r
417 CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );
\r
419 /* Divide the PCLK by 8. */
\r
420 CMT0.CMCR.BIT.CKS = 0;
\r
422 /* Enable the interrupt... */
\r
423 _IEN( _CMT0_CMI0 ) = 1;
\r
425 /* ...and set its priority to the application defined kernel priority. */
\r
426 _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
\r
428 /* Start the timer. */
\r
429 CMT.CMSTR0.BIT.STR0 = 1;
\r
431 /*-----------------------------------------------------------*/
\r
433 /* This function is explained by the comments above its prototype at the top
\r
435 void vApplicationMallocFailedHook( void )
\r
439 /*-----------------------------------------------------------*/
\r
441 /* This function is explained by the comments above its prototype at the top
\r
443 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
447 /*-----------------------------------------------------------*/
\r
449 /* This function is explained by the comments above its prototype at the top
\r
451 void vApplicationIdleHook( void )
\r
454 /*-----------------------------------------------------------*/
\r
456 /* This function is explained in the comments at the top of this file. */
\r
457 static void prvRegTest1Task( void *pvParameters )
\r
459 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )
\r
461 /* The parameter did not contain the expected value. */
\r
464 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
465 taskDISABLE_INTERRUPTS();
\r
469 /* This is an asm function that never returns. */
\r
470 prvRegTest1Implementation();
\r
472 /*-----------------------------------------------------------*/
\r
474 /* This function is explained in the comments at the top of this file. */
\r
475 static void prvRegTest2Task( void *pvParameters )
\r
477 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )
\r
479 /* The parameter did not contain the expected value. */
\r
482 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
483 taskDISABLE_INTERRUPTS();
\r
487 /* This is an asm function that never returns. */
\r
488 prvRegTest2Implementation();
\r
490 /*-----------------------------------------------------------*/
\r
492 char *pcGetTaskStatusMessage( void )
\r
494 /* Not bothered about a critical section here although technically because of
\r
495 the task priorities the pointer could change it will be atomic if not near
\r
496 atomic and its not critical. */
\r
497 if( pcStatusMessage == NULL )
\r
499 return "All tasks running without error";
\r
503 return ( char * ) pcStatusMessage;
\r
506 /*-----------------------------------------------------------*/
\r