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 * This project includes a lot of tasks and tests and is therefore complex.
\r
31 * If you would prefer a much simpler project to get started with then select
\r
32 * the 'Blinky' build configuration within the HEW IDE.
\r
33 * ****************************************************************************
\r
35 * Creates all the demo application tasks, then starts the scheduler. The web
\r
36 * documentation provides more details of the standard demo application tasks,
\r
37 * which provide no particular functionality but do provide a good example of
\r
38 * how to use the FreeRTOS API. The tasks defined in flop.c are included in the
\r
39 * set of standard demo tasks to ensure the floating point unit gets some
\r
42 * In addition to the standard demo tasks, the following tasks and tests are
\r
43 * defined and/or created within this file:
\r
45 * Webserver ("uIP") task - This serves a number of dynamically generated WEB
\r
46 * pages to a standard WEB browser. The IP and MAC addresses are configured by
\r
47 * constants defined at the bottom of FreeRTOSConfig.h. Use either a standard
\r
48 * Ethernet cable to connect through a hug, or a cross over (point to point)
\r
49 * cable to connect directly. Ensure the IP address used is compatible with the
\r
50 * IP address of the machine running the browser - the easiest way to achieve
\r
51 * this is to ensure the first three octets of the IP addresses are the same.
\r
53 * "Reg test" tasks - These fill the registers with known values, then check
\r
54 * that each register still contains its expected value. Each task uses
\r
55 * different values. The tasks run with very low priority so get preempted
\r
56 * very frequently. A check variable is incremented on each iteration of the
\r
57 * test loop. A register containing an unexpected value is indicative of an
\r
58 * error in the context switching mechanism and will result in a branch to a
\r
59 * null loop - which in turn will prevent the check variable from incrementing
\r
60 * any further and allow the check task (described below) to determine that an
\r
61 * error has occurred. The nature of the reg test tasks necessitates that they
\r
62 * are written in assembly code.
\r
64 * "Check" task - This only executes every five seconds but has a high priority
\r
65 * to ensure it gets processor time. Its main function is to check that all the
\r
66 * standard demo tasks are still operational. While no errors have been
\r
67 * discovered the check task will toggle LED 5 every 5 seconds - the toggle
\r
68 * rate increasing to 200ms being a visual indication that at least one task has
\r
69 * reported unexpected behaviour.
\r
71 * "High frequency timer test" - A high frequency periodic interrupt is
\r
72 * generated using a timer - the interrupt is assigned a priority above
\r
73 * configMAX_SYSCALL_INTERRUPT_PRIORITY so should not be effected by anything
\r
74 * the kernel is doing. The frequency and priority of the interrupt, in
\r
75 * combination with other standard tests executed in this demo, should result
\r
76 * in interrupts nesting at least 3 and probably 4 deep. This test is only
\r
77 * included in build configurations that have the optimiser switched on. In
\r
78 * optimised builds the count of high frequency ticks is used as the time base
\r
79 * for the run time stats.
\r
81 * *NOTE 1* If LED5 is toggling every 5 seconds then all the demo application
\r
82 * tasks are executing as expected and no errors have been reported in any
\r
83 * tasks. The toggle rate increasing to 200ms indicates that at least one task
\r
84 * has reported unexpected behaviour.
\r
86 * *NOTE 2* vApplicationSetupTimerInterrupt() is called by the kernel to let
\r
87 * the application set up a timer to generate the tick interrupt. In this
\r
88 * example a compare match timer is used for this purpose.
\r
90 * *NOTE 3* The CPU must be in Supervisor mode when the scheduler is started.
\r
91 * The PowerON_Reset_PC() supplied in resetprg.c with this demo has
\r
92 * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.
\r
94 * *NOTE 4* The IntQueue common demo tasks test interrupt nesting and make use
\r
95 * of all the 8bit timers (as two cascaded 16bit units).
\r
98 /* Standard includes. */
\r
102 /* Hardware specific includes. */
\r
103 #include <iorx62n.h>
\r
105 /* Kernel includes. */
\r
106 #include "FreeRTOS.h"
\r
109 /* Standard demo includes. */
\r
110 #include "partest.h"
\r
112 #include "IntQueue.h"
\r
113 #include "BlockQ.h"
\r
115 #include "integer.h"
\r
116 #include "blocktim.h"
\r
117 #include "semtest.h"
\r
119 #include "GenQTest.h"
\r
121 #include "recmutex.h"
\r
124 /* Values that are passed into the reg test tasks using the task parameter. The
\r
125 tasks check that the values are passed in correctly. */
\r
126 #define mainREG_TEST_1_PARAMETER ( 0x12121212UL )
\r
127 #define mainREG_TEST_2_PARAMETER ( 0x12345678UL )
\r
129 /* Priorities at which the tasks are created. */
\r
130 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
131 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
132 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
133 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
134 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
135 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
136 #define mainuIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
137 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
138 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
139 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
141 /* The WEB server uses string handling functions, which in turn use a bit more
\r
142 stack than most of the other tasks. */
\r
143 #define mainuIP_STACK_SIZE ( configMINIMAL_STACK_SIZE * 3 )
\r
145 /* The LED toggled by the check task. */
\r
146 #define mainCHECK_LED ( 5 )
\r
148 /* The rate at which mainCHECK_LED will toggle when all the tasks are running
\r
149 without error. Controlled by the check task as described at the top of this
\r
151 #define mainNO_ERROR_CYCLE_TIME ( 5000 / portTICK_PERIOD_MS )
\r
153 /* The rate at which mainCHECK_LED will toggle when an error has been reported
\r
154 by at least one task. Controlled by the check task as described at the top of
\r
156 #define mainERROR_CYCLE_TIME ( 200 / portTICK_PERIOD_MS )
\r
158 /* For outputing debug console messages - just maps to printf. */
\r
160 #define xPrintf( x ) printf( x )
\r
162 #define xPrintf( x ) ( void ) x
\r
166 * vApplicationMallocFailedHook() will only be called if
\r
167 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
168 * function that will execute if a call to pvPortMalloc() fails.
\r
169 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
170 * semaphore is created. It is also called by various parts of the demo
\r
173 void vApplicationMallocFailedHook( void );
\r
176 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
177 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
178 * of the idle task. It is essential that code added to this hook function
\r
179 * never attempts to block in any way (for example, call xQueueReceive() with
\r
180 * a block time specified). If the application makes use of the vTaskDelete()
\r
181 * API function (as this demo application does) then it is also important that
\r
182 * vApplicationIdleHook() is permitted to return to its calling function because
\r
183 * it is the responsibility of the idle task to clean up memory allocated by the
\r
184 * kernel to any task that has since been deleted.
\r
186 void vApplicationIdleHook( void );
\r
189 * vApplicationStackOverflowHook() will only be called if
\r
190 * configCHECK_FOR_STACK_OVERFLOW is set to a non-zero value. The handle and
\r
191 * name of the offending task should be passed in the function parameters, but
\r
192 * it is possible that the stack overflow will have corrupted these - in which
\r
193 * case pxCurrentTCB can be inspected to find the same information.
\r
195 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
198 * The reg test tasks as described at the top of this file.
\r
200 static void prvRegTest1Task( void *pvParameters );
\r
201 static void prvRegTest2Task( void *pvParameters );
\r
204 * The actual implementation of the reg test functionality, which, because of
\r
205 * the direct register access, have to be in assembly.
\r
207 extern void prvRegTest1Implementation( void );
\r
208 extern void prvRegTest2Implementation( void );
\r
212 * The check task as described at the top of this file.
\r
214 static void prvCheckTask( void *pvParameters );
\r
217 * Contains the implementation of the WEB server.
\r
219 extern void vuIP_Task( void *pvParameters );
\r
221 /*-----------------------------------------------------------*/
\r
223 /* Variables that are incremented on each iteration of the reg test tasks -
\r
224 provided the tasks have not reported any errors. The check task inspects these
\r
225 variables to ensure they are still incrementing as expected. If a variable
\r
226 stops incrementing then it is likely that its associate task has stalled. */
\r
227 unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
229 /* The status message that is displayed at the bottom of the "task stats" web
\r
230 page, which is served by the uIP task. This will report any errors picked up
\r
231 by the reg test task. */
\r
232 static const char *pcStatusMessage = NULL;
\r
234 /*-----------------------------------------------------------*/
\r
238 extern void HardwareSetup( void );
\r
240 /* Renesas provided CPU configuration routine. The clocks are configured in
\r
244 xPrintf( "http://www.FreeRTOS.org\r\n" );
\r
246 /* Start the reg test tasks which test the context switching mechanism. */
\r
247 xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
248 xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
250 /* The web server task. */
\r
251 xTaskCreate( vuIP_Task, "uIP", mainuIP_STACK_SIZE, NULL, mainuIP_TASK_PRIORITY, NULL );
\r
253 /* Start the check task as described at the top of this file. */
\r
254 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE * 3, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
256 /* Create the standard demo tasks. */
\r
257 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
258 vCreateBlockTimeTasks();
\r
259 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
260 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
261 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
262 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
263 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
264 vStartQueuePeekTasks();
\r
265 vStartRecursiveMutexTasks();
\r
266 vStartInterruptQueueTasks();
\r
267 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
269 /* The suicide tasks must be created last as they need to know how many
\r
270 tasks were running prior to their creation in order to ascertain whether
\r
271 or not the correct/expected number of tasks are running at any given time. */
\r
272 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
274 /* Start the tasks running. */
\r
275 vTaskStartScheduler();
\r
277 /* If all is well we will never reach here as the scheduler will now be
\r
278 running. If we do reach here then it is likely that there was insufficient
\r
279 heap available for the idle task to be created. */
\r
282 /*-----------------------------------------------------------*/
\r
284 static void prvCheckTask( void *pvParameters )
\r
286 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
287 TickType_t xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;
\r
288 extern void vSetupHighFrequencyTimer( void );
\r
290 /* If this is being executed then the kernel has been started. Start the high
\r
291 frequency timer test as described at the top of this file. This is only
\r
292 included in the optimised build configuration - otherwise it takes up too much
\r
294 #ifdef INCLUDE_HIGH_FREQUENCY_TIMER_TEST
\r
295 vSetupHighFrequencyTimer();
\r
298 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
299 xNextWakeTime = xTaskGetTickCount();
\r
303 /* Place this task in the blocked state until it is time to run again. */
\r
304 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
306 /* Check the standard demo tasks are running without error. */
\r
307 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
309 /* Increase the rate at which this task cycles, which will increase the
\r
310 rate at which mainCHECK_LED flashes to give visual feedback that an error
\r
312 pcStatusMessage = "Error: GenQueue";
\r
313 xPrintf( pcStatusMessage );
\r
316 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
318 pcStatusMessage = "Error: QueuePeek\r\n";
\r
319 xPrintf( pcStatusMessage );
\r
322 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
324 pcStatusMessage = "Error: BlockQueue\r\n";
\r
325 xPrintf( pcStatusMessage );
\r
328 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
330 pcStatusMessage = "Error: BlockTime\r\n";
\r
331 xPrintf( pcStatusMessage );
\r
334 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
336 pcStatusMessage = "Error: SemTest\r\n";
\r
337 xPrintf( pcStatusMessage );
\r
340 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
342 pcStatusMessage = "Error: PollQueue\r\n";
\r
343 xPrintf( pcStatusMessage );
\r
346 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
348 pcStatusMessage = "Error: Death\r\n";
\r
349 xPrintf( pcStatusMessage );
\r
352 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
354 pcStatusMessage = "Error: IntMath\r\n";
\r
355 xPrintf( pcStatusMessage );
\r
358 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
360 pcStatusMessage = "Error: RecMutex\r\n";
\r
361 xPrintf( pcStatusMessage );
\r
364 if( xAreIntQueueTasksStillRunning() != pdPASS )
\r
366 pcStatusMessage = "Error: IntQueue\r\n";
\r
367 xPrintf( pcStatusMessage );
\r
370 if( xAreMathsTaskStillRunning() != pdPASS )
\r
372 pcStatusMessage = "Error: Flop\r\n";
\r
373 xPrintf( pcStatusMessage );
\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 pcStatusMessage = "Error: RegTest1\r\n";
\r
381 xPrintf( pcStatusMessage );
\r
384 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
386 pcStatusMessage = "Error: RegTest2\r\n";
\r
387 xPrintf( pcStatusMessage );
\r
390 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
391 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
393 /* Toggle the check LED to give an indication of the system status. If
\r
394 the LED toggles every 5 seconds then everything is ok. A faster toggle
\r
395 indicates an error. */
\r
396 vParTestToggleLED( mainCHECK_LED );
\r
398 /* Ensure the LED toggles at a faster rate if an error has occurred. */
\r
399 if( pcStatusMessage != NULL )
\r
401 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
405 /*-----------------------------------------------------------*/
\r
407 /* The RX port uses this callback function to configure its tick interrupt.
\r
408 This allows the application to choose the tick interrupt source. */
\r
409 void vApplicationSetupTimerInterrupt( void )
\r
411 /* Enable compare match timer 0. */
\r
414 /* Interrupt on compare match. */
\r
415 CMT0.CMCR.BIT.CMIE = 1;
\r
417 /* Set the compare match value. */
\r
418 CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );
\r
420 /* Divide the PCLK by 8. */
\r
421 CMT0.CMCR.BIT.CKS = 0;
\r
423 /* Enable the interrupt... */
\r
424 _IEN( _CMT0_CMI0 ) = 1;
\r
426 /* ...and set its priority to the application defined kernel priority. */
\r
427 _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
\r
429 /* Start the timer. */
\r
430 CMT.CMSTR0.BIT.STR0 = 1;
\r
432 /*-----------------------------------------------------------*/
\r
434 /* This function is explained by the comments above its prototype at the top
\r
436 void vApplicationMallocFailedHook( void )
\r
440 /*-----------------------------------------------------------*/
\r
442 /* This function is explained by the comments above its prototype at the top
\r
444 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
448 /*-----------------------------------------------------------*/
\r
450 /* This function is explained by the comments above its prototype at the top
\r
452 void vApplicationIdleHook( void )
\r
455 /*-----------------------------------------------------------*/
\r
457 /* This function is explained in the comments at the top of this file. */
\r
458 static void prvRegTest1Task( void *pvParameters )
\r
460 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )
\r
462 /* The parameter did not contain the expected value. */
\r
465 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
466 taskDISABLE_INTERRUPTS();
\r
470 /* This is an asm function that never returns. */
\r
471 prvRegTest1Implementation();
\r
473 /*-----------------------------------------------------------*/
\r
475 /* This function is explained in the comments at the top of this file. */
\r
476 static void prvRegTest2Task( void *pvParameters )
\r
478 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_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 asm function that never returns. */
\r
489 prvRegTest2Implementation();
\r
491 /*-----------------------------------------------------------*/
\r
493 char *pcGetTaskStatusMessage( void )
\r
495 /* Not bothered about a critical section here although technically because of
\r
496 the task priorities the pointer could change it will be atomic if not near
\r
497 atomic and its not critical. */
\r
498 if( pcStatusMessage == NULL )
\r
500 return "All tasks running without error";
\r
504 return ( char * ) pcStatusMessage;
\r
507 /*-----------------------------------------------------------*/
\r