2 FreeRTOS V9.0.1 - Copyright (C) 2017 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /* ****************************************************************************
\r
71 * This project includes a lot of tasks and tests and is therefore complex.
\r
72 * If you would prefer a much simpler project to get started with then select
\r
73 * the 'Blinky' build configuration within the HEW IDE.
\r
74 * ****************************************************************************
\r
76 * Creates all the demo application tasks, then starts the scheduler. The web
\r
77 * documentation provides more details of the standard demo application tasks,
\r
78 * which provide no particular functionality but do provide a good example of
\r
79 * how to use the FreeRTOS API. The tasks defined in flop.c are included in the
\r
80 * set of standard demo tasks to ensure the floating point unit gets some
\r
83 * In addition to the standard demo tasks, the following tasks and tests are
\r
84 * defined and/or created within this file:
\r
86 * Webserver ("uIP") task - This serves a number of dynamically generated WEB
\r
87 * pages to a standard WEB browser. The IP and MAC addresses are configured by
\r
88 * constants defined at the bottom of FreeRTOSConfig.h. Use either a standard
\r
89 * Ethernet cable to connect through a hug, or a cross over (point to point)
\r
90 * cable to connect directly. Ensure the IP address used is compatible with the
\r
91 * IP address of the machine running the browser - the easiest way to achieve
\r
92 * this is to ensure the first three octets of the IP addresses are the same.
\r
94 * "Reg test" tasks - These fill the registers with known values, then check
\r
95 * that each register still contains its expected value. Each task uses
\r
96 * different values. The tasks run with very low priority so get preempted
\r
97 * very frequently. A check variable is incremented on each iteration of the
\r
98 * test loop. A register containing an unexpected value is indicative of an
\r
99 * error in the context switching mechanism and will result in a branch to a
\r
100 * null loop - which in turn will prevent the check variable from incrementing
\r
101 * any further and allow the check task (described below) to determine that an
\r
102 * error has occurred. The nature of the reg test tasks necessitates that they
\r
103 * are written in assembly code.
\r
105 * "Check" task - This only executes every five seconds but has a high priority
\r
106 * to ensure it gets processor time. Its main function is to check that all the
\r
107 * standard demo tasks are still operational. While no errors have been
\r
108 * discovered the check task will toggle LED 5 every 5 seconds - the toggle
\r
109 * rate increasing to 200ms being a visual indication that at least one task has
\r
110 * reported unexpected behaviour.
\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 should 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, should 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. In
\r
119 * optimised builds the count of high frequency ticks is used as the time base
\r
120 * for the run time stats.
\r
122 * *NOTE 1* If LED5 is toggling every 5 seconds then all the demo application
\r
123 * tasks are executing as expected and no errors have been reported in any
\r
124 * tasks. The toggle rate increasing to 200ms indicates that at least one task
\r
125 * has reported unexpected behaviour.
\r
127 * *NOTE 2* vApplicationSetupTimerInterrupt() is called by the kernel to let
\r
128 * the application set up a timer to generate the tick interrupt. In this
\r
129 * example a compare match timer is used for this purpose.
\r
131 * *NOTE 3* The CPU must be in Supervisor mode when the scheduler is started.
\r
132 * The PowerON_Reset_PC() supplied in resetprg.c with this demo has
\r
133 * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.
\r
135 * *NOTE 4* The IntQueue common demo tasks test interrupt nesting and make use
\r
136 * of all the 8bit timers (as two cascaded 16bit units).
\r
139 /* Standard includes. */
\r
140 #include <string.h>
\r
143 /* Hardware specific includes. */
\r
144 #include <iorx62n.h>
\r
146 /* Kernel includes. */
\r
147 #include "FreeRTOS.h"
\r
150 /* Standard demo includes. */
\r
151 #include "partest.h"
\r
153 #include "IntQueue.h"
\r
154 #include "BlockQ.h"
\r
156 #include "integer.h"
\r
157 #include "blocktim.h"
\r
158 #include "semtest.h"
\r
160 #include "GenQTest.h"
\r
162 #include "recmutex.h"
\r
165 /* Values that are passed into the reg test tasks using the task parameter. The
\r
166 tasks check that the values are passed in correctly. */
\r
167 #define mainREG_TEST_1_PARAMETER ( 0x12121212UL )
\r
168 #define mainREG_TEST_2_PARAMETER ( 0x12345678UL )
\r
170 /* Priorities at which the tasks are created. */
\r
171 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
172 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
173 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
174 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
175 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
176 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
177 #define mainuIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
178 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
179 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
180 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
182 /* The WEB server uses string handling functions, which in turn use a bit more
\r
183 stack than most of the other tasks. */
\r
184 #define mainuIP_STACK_SIZE ( configMINIMAL_STACK_SIZE * 3 )
\r
186 /* The LED toggled by the check task. */
\r
187 #define mainCHECK_LED ( 5 )
\r
189 /* The rate at which mainCHECK_LED will toggle when all the tasks are running
\r
190 without error. Controlled by the check task as described at the top of this
\r
192 #define mainNO_ERROR_CYCLE_TIME ( 5000 / portTICK_PERIOD_MS )
\r
194 /* The rate at which mainCHECK_LED will toggle when an error has been reported
\r
195 by at least one task. Controlled by the check task as described at the top of
\r
197 #define mainERROR_CYCLE_TIME ( 200 / portTICK_PERIOD_MS )
\r
199 /* For outputing debug console messages - just maps to printf. */
\r
201 #define xPrintf( x ) printf( x )
\r
203 #define xPrintf( x ) ( void ) x
\r
207 * vApplicationMallocFailedHook() will only be called if
\r
208 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
209 * function that will execute if a call to pvPortMalloc() fails.
\r
210 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
211 * semaphore is created. It is also called by various parts of the demo
\r
214 void vApplicationMallocFailedHook( void );
\r
217 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
218 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
219 * of the idle task. It is essential that code added to this hook function
\r
220 * never attempts to block in any way (for example, call xQueueReceive() with
\r
221 * a block time specified). If the application makes use of the vTaskDelete()
\r
222 * API function (as this demo application does) then it is also important that
\r
223 * vApplicationIdleHook() is permitted to return to its calling function because
\r
224 * it is the responsibility of the idle task to clean up memory allocated by the
\r
225 * kernel to any task that has since been deleted.
\r
227 void vApplicationIdleHook( void );
\r
230 * vApplicationStackOverflowHook() will only be called if
\r
231 * configCHECK_FOR_STACK_OVERFLOW is set to a non-zero value. The handle and
\r
232 * name of the offending task should be passed in the function parameters, but
\r
233 * it is possible that the stack overflow will have corrupted these - in which
\r
234 * case pxCurrentTCB can be inspected to find the same information.
\r
236 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
239 * The reg test tasks as described at the top of this file.
\r
241 static void prvRegTest1Task( void *pvParameters );
\r
242 static void prvRegTest2Task( void *pvParameters );
\r
245 * The actual implementation of the reg test functionality, which, because of
\r
246 * the direct register access, have to be in assembly.
\r
248 extern void prvRegTest1Implementation( void );
\r
249 extern void prvRegTest2Implementation( void );
\r
253 * The check task as described at the top of this file.
\r
255 static void prvCheckTask( void *pvParameters );
\r
258 * Contains the implementation of the WEB server.
\r
260 extern void vuIP_Task( void *pvParameters );
\r
262 /*-----------------------------------------------------------*/
\r
264 /* Variables that are incremented on each iteration of the reg test tasks -
\r
265 provided the tasks have not reported any errors. The check task inspects these
\r
266 variables to ensure they are still incrementing as expected. If a variable
\r
267 stops incrementing then it is likely that its associate task has stalled. */
\r
268 unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
270 /* The status message that is displayed at the bottom of the "task stats" web
\r
271 page, which is served by the uIP task. This will report any errors picked up
\r
272 by the reg test task. */
\r
273 static const char *pcStatusMessage = NULL;
\r
275 /*-----------------------------------------------------------*/
\r
279 extern void HardwareSetup( void );
\r
281 /* Renesas provided CPU configuration routine. The clocks are configured in
\r
285 xPrintf( "http://www.FreeRTOS.org\r\n" );
\r
287 /* Start the reg test tasks which test the context switching mechanism. */
\r
288 xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
289 xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
291 /* The web server task. */
\r
292 xTaskCreate( vuIP_Task, "uIP", mainuIP_STACK_SIZE, NULL, mainuIP_TASK_PRIORITY, NULL );
\r
294 /* Start the check task as described at the top of this file. */
\r
295 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE * 3, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
297 /* Create the standard demo tasks. */
\r
298 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
299 vCreateBlockTimeTasks();
\r
300 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
301 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
302 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
303 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
304 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
305 vStartQueuePeekTasks();
\r
306 vStartRecursiveMutexTasks();
\r
307 vStartInterruptQueueTasks();
\r
308 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
310 /* The suicide tasks must be created last as they need to know how many
\r
311 tasks were running prior to their creation in order to ascertain whether
\r
312 or not the correct/expected number of tasks are running at any given time. */
\r
313 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
315 /* Start the tasks running. */
\r
316 vTaskStartScheduler();
\r
318 /* If all is well we will never reach here as the scheduler will now be
\r
319 running. If we do reach here then it is likely that there was insufficient
\r
320 heap available for the idle task to be created. */
\r
323 /*-----------------------------------------------------------*/
\r
325 static void prvCheckTask( void *pvParameters )
\r
327 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
328 TickType_t xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;
\r
329 extern void vSetupHighFrequencyTimer( void );
\r
331 /* If this is being executed then the kernel has been started. Start the high
\r
332 frequency timer test as described at the top of this file. This is only
\r
333 included in the optimised build configuration - otherwise it takes up too much
\r
335 #ifdef INCLUDE_HIGH_FREQUENCY_TIMER_TEST
\r
336 vSetupHighFrequencyTimer();
\r
339 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
340 xNextWakeTime = xTaskGetTickCount();
\r
344 /* Place this task in the blocked state until it is time to run again. */
\r
345 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
347 /* Check the standard demo tasks are running without error. */
\r
348 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
350 /* Increase the rate at which this task cycles, which will increase the
\r
351 rate at which mainCHECK_LED flashes to give visual feedback that an error
\r
353 pcStatusMessage = "Error: GenQueue";
\r
354 xPrintf( pcStatusMessage );
\r
357 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
359 pcStatusMessage = "Error: QueuePeek\r\n";
\r
360 xPrintf( pcStatusMessage );
\r
363 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
365 pcStatusMessage = "Error: BlockQueue\r\n";
\r
366 xPrintf( pcStatusMessage );
\r
369 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
371 pcStatusMessage = "Error: BlockTime\r\n";
\r
372 xPrintf( pcStatusMessage );
\r
375 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
377 pcStatusMessage = "Error: SemTest\r\n";
\r
378 xPrintf( pcStatusMessage );
\r
381 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
383 pcStatusMessage = "Error: PollQueue\r\n";
\r
384 xPrintf( pcStatusMessage );
\r
387 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
389 pcStatusMessage = "Error: Death\r\n";
\r
390 xPrintf( pcStatusMessage );
\r
393 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
395 pcStatusMessage = "Error: IntMath\r\n";
\r
396 xPrintf( pcStatusMessage );
\r
399 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
401 pcStatusMessage = "Error: RecMutex\r\n";
\r
402 xPrintf( pcStatusMessage );
\r
405 if( xAreIntQueueTasksStillRunning() != pdPASS )
\r
407 pcStatusMessage = "Error: IntQueue\r\n";
\r
408 xPrintf( pcStatusMessage );
\r
411 if( xAreMathsTaskStillRunning() != pdPASS )
\r
413 pcStatusMessage = "Error: Flop\r\n";
\r
414 xPrintf( pcStatusMessage );
\r
417 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
418 their loop counters if they encounter an error. */
\r
419 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
421 pcStatusMessage = "Error: RegTest1\r\n";
\r
422 xPrintf( pcStatusMessage );
\r
425 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
427 pcStatusMessage = "Error: RegTest2\r\n";
\r
428 xPrintf( pcStatusMessage );
\r
431 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
432 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
434 /* Toggle the check LED to give an indication of the system status. If
\r
435 the LED toggles every 5 seconds then everything is ok. A faster toggle
\r
436 indicates an error. */
\r
437 vParTestToggleLED( mainCHECK_LED );
\r
439 /* Ensure the LED toggles at a faster rate if an error has occurred. */
\r
440 if( pcStatusMessage != NULL )
\r
442 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
446 /*-----------------------------------------------------------*/
\r
448 /* The RX port uses this callback function to configure its tick interrupt.
\r
449 This allows the application to choose the tick interrupt source. */
\r
450 void vApplicationSetupTimerInterrupt( void )
\r
452 /* Enable compare match timer 0. */
\r
455 /* Interrupt on compare match. */
\r
456 CMT0.CMCR.BIT.CMIE = 1;
\r
458 /* Set the compare match value. */
\r
459 CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );
\r
461 /* Divide the PCLK by 8. */
\r
462 CMT0.CMCR.BIT.CKS = 0;
\r
464 /* Enable the interrupt... */
\r
465 _IEN( _CMT0_CMI0 ) = 1;
\r
467 /* ...and set its priority to the application defined kernel priority. */
\r
468 _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
\r
470 /* Start the timer. */
\r
471 CMT.CMSTR0.BIT.STR0 = 1;
\r
473 /*-----------------------------------------------------------*/
\r
475 /* This function is explained by the comments above its prototype at the top
\r
477 void vApplicationMallocFailedHook( void )
\r
481 /*-----------------------------------------------------------*/
\r
483 /* This function is explained by the comments above its prototype at the top
\r
485 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
489 /*-----------------------------------------------------------*/
\r
491 /* This function is explained by the comments above its prototype at the top
\r
493 void vApplicationIdleHook( void )
\r
496 /*-----------------------------------------------------------*/
\r
498 /* This function is explained in the comments at the top of this file. */
\r
499 static void prvRegTest1Task( void *pvParameters )
\r
501 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )
\r
503 /* The parameter did not contain the expected value. */
\r
506 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
507 taskDISABLE_INTERRUPTS();
\r
511 /* This is an asm function that never returns. */
\r
512 prvRegTest1Implementation();
\r
514 /*-----------------------------------------------------------*/
\r
516 /* This function is explained in the comments at the top of this file. */
\r
517 static void prvRegTest2Task( void *pvParameters )
\r
519 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )
\r
521 /* The parameter did not contain the expected value. */
\r
524 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
525 taskDISABLE_INTERRUPTS();
\r
529 /* This is an asm function that never returns. */
\r
530 prvRegTest2Implementation();
\r
532 /*-----------------------------------------------------------*/
\r
534 char *pcGetTaskStatusMessage( void )
\r
536 /* Not bothered about a critical section here although technically because of
\r
537 the task priorities the pointer could change it will be atomic if not near
\r
538 atomic and its not critical. */
\r
539 if( pcStatusMessage == NULL )
\r
541 return "All tasks running without error";
\r
545 return ( char * ) pcStatusMessage;
\r
548 /*-----------------------------------------------------------*/
\r