2 FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
75 /* ****************************************************************************
\r
76 * This project includes a lot of tasks and tests and is therefore complex.
\r
77 * If you would prefer a much simpler project to get started with then select
\r
78 * the 'Blinky' build configuration within the HEW IDE.
\r
79 * ****************************************************************************
\r
81 * Creates all the demo application tasks, then starts the scheduler. The web
\r
82 * documentation provides more details of the standard demo application tasks,
\r
83 * which provide no particular functionality but do provide a good example of
\r
84 * how to use the FreeRTOS API. The tasks defined in flop.c are included in the
\r
85 * set of standard demo tasks to ensure the floating point unit gets some
\r
88 * In addition to the standard demo tasks, the following tasks and tests are
\r
89 * defined and/or created within this file:
\r
91 * Webserver ("uIP") task - This serves a number of dynamically generated WEB
\r
92 * pages to a standard WEB browser. The IP and MAC addresses are configured by
\r
93 * constants defined at the bottom of FreeRTOSConfig.h. Use either a standard
\r
94 * Ethernet cable to connect through a hug, or a cross over (point to point)
\r
95 * cable to connect directly. Ensure the IP address used is compatible with the
\r
96 * IP address of the machine running the browser - the easiest way to achieve
\r
97 * this is to ensure the first three octets of the IP addresses are the same.
\r
99 * "Reg test" tasks - These fill the registers with known values, then check
\r
100 * that each register still contains its expected value. Each task uses
\r
101 * different values. The tasks run with very low priority so get preempted
\r
102 * very frequently. A check variable is incremented on each iteration of the
\r
103 * test loop. A register containing an unexpected value is indicative of an
\r
104 * error in the context switching mechanism and will result in a branch to a
\r
105 * null loop - which in turn will prevent the check variable from incrementing
\r
106 * any further and allow the check task (described below) to determine that an
\r
107 * error has occurred. The nature of the reg test tasks necessitates that they
\r
108 * are written in assembly code.
\r
110 * "Check" task - This only executes every five seconds but has a high priority
\r
111 * to ensure it gets processor time. Its main function is to check that all the
\r
112 * standard demo tasks are still operational. While no errors have been
\r
113 * discovered the check task will toggle LED 5 every 5 seconds - the toggle
\r
114 * rate increasing to 200ms being a visual indication that at least one task has
\r
115 * reported unexpected behaviour.
\r
117 * "High frequency timer test" - A high frequency periodic interrupt is
\r
118 * generated using a timer - the interrupt is assigned a priority above
\r
119 * configMAX_SYSCALL_INTERRUPT_PRIORITY so should not be effected by anything
\r
120 * the kernel is doing. The frequency and priority of the interrupt, in
\r
121 * combination with other standard tests executed in this demo, should result
\r
122 * in interrupts nesting at least 3 and probably 4 deep. This test is only
\r
123 * included in build configurations that have the optimiser switched on. In
\r
124 * optimised builds the count of high frequency ticks is used as the time base
\r
125 * for the run time stats.
\r
127 * *NOTE 1* If LED5 is toggling every 5 seconds then all the demo application
\r
128 * tasks are executing as expected and no errors have been reported in any
\r
129 * tasks. The toggle rate increasing to 200ms indicates that at least one task
\r
130 * has reported unexpected behaviour.
\r
132 * *NOTE 2* vApplicationSetupTimerInterrupt() is called by the kernel to let
\r
133 * the application set up a timer to generate the tick interrupt. In this
\r
134 * example a compare match timer is used for this purpose.
\r
136 * *NOTE 3* The CPU must be in Supervisor mode when the scheduler is started.
\r
137 * The PowerON_Reset_PC() supplied in resetprg.c with this demo has
\r
138 * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.
\r
140 * *NOTE 4* The IntQueue common demo tasks test interrupt nesting and make use
\r
141 * of all the 8bit timers (as two cascaded 16bit units).
\r
144 /* Standard includes. */
\r
145 #include <string.h>
\r
148 /* Hardware specific includes. */
\r
149 #include <iorx62n.h>
\r
151 /* Kernel includes. */
\r
152 #include "FreeRTOS.h"
\r
155 /* Standard demo includes. */
\r
156 #include "partest.h"
\r
158 #include "IntQueue.h"
\r
159 #include "BlockQ.h"
\r
161 #include "integer.h"
\r
162 #include "blocktim.h"
\r
163 #include "semtest.h"
\r
165 #include "GenQTest.h"
\r
167 #include "recmutex.h"
\r
170 /* Values that are passed into the reg test tasks using the task parameter. The
\r
171 tasks check that the values are passed in correctly. */
\r
172 #define mainREG_TEST_1_PARAMETER ( 0x12121212UL )
\r
173 #define mainREG_TEST_2_PARAMETER ( 0x12345678UL )
\r
175 /* Priorities at which the tasks are created. */
\r
176 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
177 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
178 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
179 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
180 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
181 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
182 #define mainuIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
183 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
184 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
185 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
187 /* The WEB server uses string handling functions, which in turn use a bit more
\r
188 stack than most of the other tasks. */
\r
189 #define mainuIP_STACK_SIZE ( configMINIMAL_STACK_SIZE * 3 )
\r
191 /* The LED toggled by the check task. */
\r
192 #define mainCHECK_LED ( 5 )
\r
194 /* The rate at which mainCHECK_LED will toggle when all the tasks are running
\r
195 without error. Controlled by the check task as described at the top of this
\r
197 #define mainNO_ERROR_CYCLE_TIME ( 5000 / portTICK_RATE_MS )
\r
199 /* The rate at which mainCHECK_LED will toggle when an error has been reported
\r
200 by at least one task. Controlled by the check task as described at the top of
\r
202 #define mainERROR_CYCLE_TIME ( 200 / portTICK_RATE_MS )
\r
204 /* For outputing debug console messages - just maps to printf. */
\r
206 #define xPrintf( x ) printf( x )
\r
208 #define xPrintf( x ) ( void ) x
\r
212 * vApplicationMallocFailedHook() will only be called if
\r
213 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
214 * function that will execute if a call to pvPortMalloc() fails.
\r
215 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
216 * semaphore is created. It is also called by various parts of the demo
\r
219 void vApplicationMallocFailedHook( void );
\r
222 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
223 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
224 * of the idle task. It is essential that code added to this hook function
\r
225 * never attempts to block in any way (for example, call xQueueReceive() with
\r
226 * a block time specified). If the application makes use of the vTaskDelete()
\r
227 * API function (as this demo application does) then it is also important that
\r
228 * vApplicationIdleHook() is permitted to return to its calling function because
\r
229 * it is the responsibility of the idle task to clean up memory allocated by the
\r
230 * kernel to any task that has since been deleted.
\r
232 void vApplicationIdleHook( void );
\r
235 * vApplicationStackOverflowHook() will only be called if
\r
236 * configCHECK_FOR_STACK_OVERFLOW is set to a non-zero value. The handle and
\r
237 * name of the offending task should be passed in the function parameters, but
\r
238 * it is possible that the stack overflow will have corrupted these - in which
\r
239 * case pxCurrentTCB can be inspected to find the same information.
\r
241 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName );
\r
244 * The reg test tasks as described at the top of this file.
\r
246 static void prvRegTest1Task( void *pvParameters );
\r
247 static void prvRegTest2Task( void *pvParameters );
\r
250 * The actual implementation of the reg test functionality, which, because of
\r
251 * the direct register access, have to be in assembly.
\r
253 extern void prvRegTest1Implementation( void );
\r
254 extern void prvRegTest2Implementation( void );
\r
258 * The check task as described at the top of this file.
\r
260 static void prvCheckTask( void *pvParameters );
\r
263 * Contains the implementation of the WEB server.
\r
265 extern void vuIP_Task( void *pvParameters );
\r
267 /*-----------------------------------------------------------*/
\r
269 /* Variables that are incremented on each iteration of the reg test tasks -
\r
270 provided the tasks have not reported any errors. The check task inspects these
\r
271 variables to ensure they are still incrementing as expected. If a variable
\r
272 stops incrementing then it is likely that its associate task has stalled. */
\r
273 unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
275 /* The status message that is displayed at the bottom of the "task stats" web
\r
276 page, which is served by the uIP task. This will report any errors picked up
\r
277 by the reg test task. */
\r
278 static const char *pcStatusMessage = NULL;
\r
280 /*-----------------------------------------------------------*/
\r
284 extern void HardwareSetup( void );
\r
286 /* Renesas provided CPU configuration routine. The clocks are configured in
\r
290 xPrintf( "http://www.FreeRTOS.org\r\n" );
\r
292 /* Start the reg test tasks which test the context switching mechanism. */
\r
293 xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
294 xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
296 /* The web server task. */
\r
297 xTaskCreate( vuIP_Task, "uIP", mainuIP_STACK_SIZE, NULL, mainuIP_TASK_PRIORITY, NULL );
\r
299 /* Start the check task as described at the top of this file. */
\r
300 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE * 3, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
302 /* Create the standard demo tasks. */
\r
303 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
304 vCreateBlockTimeTasks();
\r
305 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
306 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
307 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
308 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
309 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
310 vStartQueuePeekTasks();
\r
311 vStartRecursiveMutexTasks();
\r
312 vStartInterruptQueueTasks();
\r
313 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
315 /* The suicide tasks must be created last as they need to know how many
\r
316 tasks were running prior to their creation in order to ascertain whether
\r
317 or not the correct/expected number of tasks are running at any given time. */
\r
318 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
320 /* Start the tasks running. */
\r
321 vTaskStartScheduler();
\r
323 /* If all is well we will never reach here as the scheduler will now be
\r
324 running. If we do reach here then it is likely that there was insufficient
\r
325 heap available for the idle task to be created. */
\r
328 /*-----------------------------------------------------------*/
\r
330 static void prvCheckTask( void *pvParameters )
\r
332 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
333 portTickType xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;
\r
334 extern void vSetupHighFrequencyTimer( void );
\r
336 /* If this is being executed then the kernel has been started. Start the high
\r
337 frequency timer test as described at the top of this file. This is only
\r
338 included in the optimised build configuration - otherwise it takes up too much
\r
340 #ifdef INCLUDE_HIGH_FREQUENCY_TIMER_TEST
\r
341 vSetupHighFrequencyTimer();
\r
344 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
345 xNextWakeTime = xTaskGetTickCount();
\r
349 /* Place this task in the blocked state until it is time to run again. */
\r
350 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
352 /* Check the standard demo tasks are running without error. */
\r
353 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
355 /* Increase the rate at which this task cycles, which will increase the
\r
356 rate at which mainCHECK_LED flashes to give visual feedback that an error
\r
358 pcStatusMessage = "Error: GenQueue";
\r
359 xPrintf( pcStatusMessage );
\r
362 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
364 pcStatusMessage = "Error: QueuePeek\r\n";
\r
365 xPrintf( pcStatusMessage );
\r
368 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
370 pcStatusMessage = "Error: BlockQueue\r\n";
\r
371 xPrintf( pcStatusMessage );
\r
374 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
376 pcStatusMessage = "Error: BlockTime\r\n";
\r
377 xPrintf( pcStatusMessage );
\r
380 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
382 pcStatusMessage = "Error: SemTest\r\n";
\r
383 xPrintf( pcStatusMessage );
\r
386 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
388 pcStatusMessage = "Error: PollQueue\r\n";
\r
389 xPrintf( pcStatusMessage );
\r
392 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
394 pcStatusMessage = "Error: Death\r\n";
\r
395 xPrintf( pcStatusMessage );
\r
398 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
400 pcStatusMessage = "Error: IntMath\r\n";
\r
401 xPrintf( pcStatusMessage );
\r
404 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
406 pcStatusMessage = "Error: RecMutex\r\n";
\r
407 xPrintf( pcStatusMessage );
\r
410 if( xAreIntQueueTasksStillRunning() != pdPASS )
\r
412 pcStatusMessage = "Error: IntQueue\r\n";
\r
413 xPrintf( pcStatusMessage );
\r
416 if( xAreMathsTaskStillRunning() != pdPASS )
\r
418 pcStatusMessage = "Error: Flop\r\n";
\r
419 xPrintf( pcStatusMessage );
\r
422 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
423 their loop counters if they encounter an error. */
\r
424 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
426 pcStatusMessage = "Error: RegTest1\r\n";
\r
427 xPrintf( pcStatusMessage );
\r
430 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
432 pcStatusMessage = "Error: RegTest2\r\n";
\r
433 xPrintf( pcStatusMessage );
\r
436 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
437 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
439 /* Toggle the check LED to give an indication of the system status. If
\r
440 the LED toggles every 5 seconds then everything is ok. A faster toggle
\r
441 indicates an error. */
\r
442 vParTestToggleLED( mainCHECK_LED );
\r
444 /* Ensure the LED toggles at a faster rate if an error has occurred. */
\r
445 if( pcStatusMessage != NULL )
\r
447 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
451 /*-----------------------------------------------------------*/
\r
453 /* The RX port uses this callback function to configure its tick interrupt.
\r
454 This allows the application to choose the tick interrupt source. */
\r
455 void vApplicationSetupTimerInterrupt( void )
\r
457 /* Enable compare match timer 0. */
\r
460 /* Interrupt on compare match. */
\r
461 CMT0.CMCR.BIT.CMIE = 1;
\r
463 /* Set the compare match value. */
\r
464 CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );
\r
466 /* Divide the PCLK by 8. */
\r
467 CMT0.CMCR.BIT.CKS = 0;
\r
469 /* Enable the interrupt... */
\r
470 _IEN( _CMT0_CMI0 ) = 1;
\r
472 /* ...and set its priority to the application defined kernel priority. */
\r
473 _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
\r
475 /* Start the timer. */
\r
476 CMT.CMSTR0.BIT.STR0 = 1;
\r
478 /*-----------------------------------------------------------*/
\r
480 /* This function is explained by the comments above its prototype at the top
\r
482 void vApplicationMallocFailedHook( void )
\r
486 /*-----------------------------------------------------------*/
\r
488 /* This function is explained by the comments above its prototype at the top
\r
490 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
494 /*-----------------------------------------------------------*/
\r
496 /* This function is explained by the comments above its prototype at the top
\r
498 void vApplicationIdleHook( void )
\r
501 /*-----------------------------------------------------------*/
\r
503 /* This function is explained in the comments at the top of this file. */
\r
504 static void prvRegTest1Task( void *pvParameters )
\r
506 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )
\r
508 /* The parameter did not contain the expected value. */
\r
511 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
512 taskDISABLE_INTERRUPTS();
\r
516 /* This is an asm function that never returns. */
\r
517 prvRegTest1Implementation();
\r
519 /*-----------------------------------------------------------*/
\r
521 /* This function is explained in the comments at the top of this file. */
\r
522 static void prvRegTest2Task( void *pvParameters )
\r
524 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )
\r
526 /* The parameter did not contain the expected value. */
\r
529 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
530 taskDISABLE_INTERRUPTS();
\r
534 /* This is an asm function that never returns. */
\r
535 prvRegTest2Implementation();
\r
537 /*-----------------------------------------------------------*/
\r
539 char *pcGetTaskStatusMessage( void )
\r
541 /* Not bothered about a critical section here although technically because of
\r
542 the task priorities the pointer could change it will be atomic if not near
\r
543 atomic and its not critical. */
\r
544 if( pcStatusMessage == NULL )
\r
546 return "All tasks running without error";
\r
550 return ( char * ) pcStatusMessage;
\r
553 /*-----------------------------------------------------------*/
\r