2 FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /* ****************************************************************************
\r
55 * This project includes a lot of tasks and tests and is therefore complex.
\r
56 * If you would prefer a much simpler project to get started with then select
\r
57 * the 'Blinky' build configuration within the Embedded Workbench IDE.
\r
58 * ****************************************************************************
\r
60 * Creates all the demo application tasks, then starts the scheduler. The web
\r
61 * documentation provides more details of the standard demo application tasks,
\r
62 * which provide no particular functionality but do provide a good example of
\r
63 * how to use the FreeRTOS API. The tasks defined in flop.c are included in the
\r
64 * set of standard demo tasks to ensure the floating point unit gets some
\r
67 * In addition to the standard demo tasks, the following tasks and tests are
\r
68 * defined and/or created within this file:
\r
70 * Webserver ("uIP") task - This serves a number of dynamically generated WEB
\r
71 * pages to a standard WEB browser. The IP and MAC addresses are configured by
\r
72 * constants defined at the bottom of FreeRTOSConfig.h. Use either a standard
\r
73 * Ethernet cable to connect through a hug, or a cross over (point to point)
\r
74 * cable to connect directly. Ensure the IP address used is compatible with the
\r
75 * IP address of the machine running the browser - the easiest way to achieve
\r
76 * this is to ensure the first three octets of the IP addresses are the same.
\r
78 * "Reg test" tasks - These fill the registers with known values, then check
\r
79 * that each register still contains its expected value. Each task uses
\r
80 * different values. The tasks run with very low priority so get preempted
\r
81 * very frequently. A check variable is incremented on each iteration of the
\r
82 * test loop. A register containing an unexpected value is indicative of an
\r
83 * error in the context switching mechanism and will result in a branch to a
\r
84 * null loop - which in turn will prevent the check variable from incrementing
\r
85 * any further and allow the check task (described below) to determine that an
\r
86 * error has occurred. The nature of the reg test tasks necessitates that they
\r
87 * are written in assembly code.
\r
89 * "Check" task - This only executes every five seconds but has a high priority
\r
90 * to ensure it gets processor time. Its main function is to check that all the
\r
91 * standard demo tasks are still operational. While no errors have been
\r
92 * discovered the check task will toggle LED 5 every 5 seconds - the toggle
\r
93 * rate increasing to 200ms being a visual indication that at least one task has
\r
94 * reported unexpected behaviour.
\r
96 * "High frequency timer test" - A high frequency periodic interrupt is
\r
97 * generated using a timer - the interrupt is assigned a priority above
\r
98 * configMAX_SYSCALL_INTERRUPT_PRIORITY so should not be effected by anything
\r
99 * the kernel is doing. The frequency and priority of the interrupt, in
\r
100 * combination with other standard tests executed in this demo, should result
\r
101 * in interrupts nesting at least 3 and probably 4 deep. This test is only
\r
102 * included in build configurations that have the optimiser switched on. In
\r
103 * optimised builds the count of high frequency ticks is used as the time base
\r
104 * for the run time stats.
\r
106 * *NOTE 1* If LED5 is toggling every 5 seconds then all the demo application
\r
107 * tasks are executing as expected and no errors have been reported in any
\r
108 * tasks. The toggle rate increasing to 200ms indicates that at least one task
\r
109 * has reported unexpected behaviour.
\r
111 * *NOTE 2* vApplicationSetupTimerInterrupt() is called by the kernel to let
\r
112 * the application set up a timer to generate the tick interrupt. In this
\r
113 * example a compare match timer is used for this purpose.
\r
115 * *NOTE 3* The CPU must be in Supervisor mode when the scheduler is started.
\r
116 * The PowerON_Reset_PC() supplied in resetprg.c with this demo has
\r
117 * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.
\r
119 * *NOTE 4* The IntQueue common demo tasks test interrupt nesting and make use
\r
120 * of all the 8bit timers (as two cascaded 16bit units).
\r
123 /* Standard includes. */
\r
124 #include <string.h>
\r
127 /* BSP includes. */
\r
128 #include "xenv_standalone.h"
\r
129 #include "xtmrctr.h"
\r
130 #include "xil_exception.h"
\r
131 #include "microblaze_exceptions_g.h"
\r
134 /* Kernel includes. */
\r
135 #include "FreeRTOS.h"
\r
137 #include "timers.h"
\r
139 /* Standard demo includes. */
\r
140 #include "partest.h"
\r
142 #include "BlockQ.h"
\r
144 #include "blocktim.h"
\r
145 #include "semtest.h"
\r
147 #include "GenQTest.h"
\r
149 #include "recmutex.h"
\r
151 #include "dynamic.h"
\r
153 #define xPrintf( x )
\r
155 /* Priorities at which the tasks are created. */
\r
156 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
157 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
158 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
159 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
160 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
161 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
162 #define mainuIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
163 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
164 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
165 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
167 /* The WEB server uses string handling functions, which in turn use a bit more
\r
168 stack than most of the other tasks. */
\r
169 #define mainuIP_STACK_SIZE ( configMINIMAL_STACK_SIZE * 3 )
\r
171 /* The LED toggled by the check task. */
\r
172 #define mainCHECK_LED ( 3 )
\r
174 /* The rate at which mainCHECK_LED will toggle when all the tasks are running
\r
175 without error. Controlled by the check task as described at the top of this
\r
177 #define mainNO_ERROR_CHECK_TIMER_PERIOD ( 5000 / portTICK_RATE_MS )
\r
179 /* The rate at which mainCHECK_LED will toggle when an error has been reported
\r
180 by at least one task. Controlled by the check task as described at the top of
\r
182 #define mainERROR_CHECK_TIMER_PERIOD ( 200 / portTICK_RATE_MS )
\r
184 /* A block time of zero means "don't block". */
185 #define mainDONT_BLOCK ( ( portTickType ) 0 )
\r
188 * vApplicationMallocFailedHook() will only be called if
\r
189 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
190 * function that will execute if a call to pvPortMalloc() fails.
\r
191 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
192 * semaphore is created. It is also called by various parts of the demo
\r
195 void vApplicationMallocFailedHook( void );
\r
198 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
199 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
200 * of the idle task. It is essential that code added to this hook function
\r
201 * never attempts to block in any way (for example, call xQueueReceive() with
\r
202 * a block time specified). If the application makes use of the vTaskDelete()
\r
203 * API function (as this demo application does) then it is also important that
\r
204 * vApplicationIdleHook() is permitted to return to its calling function because
\r
205 * it is the responsibility of the idle task to clean up memory allocated by the
\r
206 * kernel to any task that has since been deleted.
\r
208 void vApplicationIdleHook( void );
\r
211 * vApplicationStackOverflowHook() will only be called if
\r
212 * configCHECK_FOR_STACK_OVERFLOW is set to a non-zero value. The handle and
\r
213 * name of the offending task should be passed in the function parameters, but
\r
214 * it is possible that the stack overflow will have corrupted these - in which
\r
215 * case pxCurrentTCB can be inspected to find the same information.
\r
217 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName );
\r
220 * The reg test tasks as described at the top of this file.
\r
222 extern void vRegisterTest1( void *pvParameters );
\r
223 extern void vRegisterTest2( void *pvParameters );
\r
226 * Defines the 'check' functionality as described at the top of this file. This
\r
227 * function is the callback function for the 'check' timer.
\r
229 static void vCheckTimerCallback( xTimerHandle xTimer );
\r
232 static void prvSetupHardware( void );
\r
235 * Contains the implementation of the WEB server.
\r
237 //_RB_extern void vuIP_Task( void *pvParameters );
\r
239 /*-----------------------------------------------------------*/
\r
241 /* The status message that is displayed at the bottom of the "task stats" web
\r
242 page, which is served by the uIP task. This will report any errors picked up
\r
243 by the reg test task. */
\r
244 static const char *pcStatusMessage = NULL;
\r
246 static XTmrCtr xTimer0Instance;
\r
248 /* The 'check' timer, as described at the top of this file. */
\r
249 static xTimerHandle xCheckTimer = NULL;
\r
251 /*-----------------------------------------------------------*/
\r
255 /* Configure the interrupt controller, LED outputs and button inputs. */
\r
256 prvSetupHardware();
\r
258 /* Start the reg test tasks which test the context switching mechanism. */
\r
259 xTaskCreate( vRegisterTest1, ( const signed char * const ) "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) 0, tskIDLE_PRIORITY, NULL );
\r
260 xTaskCreate( vRegisterTest2, ( const signed char * const ) "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) 0, tskIDLE_PRIORITY, NULL );
\r
262 /* The web server task. */
\r
263 //_RB_ xTaskCreate( vuIP_Task, "uIP", mainuIP_STACK_SIZE, NULL, mainuIP_TASK_PRIORITY, NULL );
\r
265 /* Create the standard demo tasks. */
\r
266 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
267 vCreateBlockTimeTasks();
\r
268 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
269 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
270 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
271 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
272 vStartQueuePeekTasks();
\r
273 vStartRecursiveMutexTasks();
\r
274 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
276 /* The suicide tasks must be created last as they need to know how many
\r
277 tasks were running prior to their creation in order to ascertain whether
\r
278 or not the correct/expected number of tasks are running at any given time. */
\r
279 // vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
281 /* Create the 'check' timer - the timer that periodically calls the
\r
282 check function as described at the top of this file. Note that, for
\r
283 the reasons stated in the comments above the call to
\r
284 vStartTimerDemoTask(), that the check timer is not actually started
\r
285 until after the scheduler has been started. */
\r
286 xCheckTimer = xTimerCreate( ( const signed char * ) "Check timer", mainNO_ERROR_CHECK_TIMER_PERIOD, pdTRUE, ( void * ) 0, vCheckTimerCallback );
\r
288 /* Ensure the check timer will start running as soon as the scheduler
\r
289 starts. The block time is set to 0 (mainDONT_BLOCK), but would be
\r
290 ingnored at this point anyway as block times can only be specified when
\r
291 the scheduler is running. */
\r
292 xTimerStart( xCheckTimer, mainDONT_BLOCK );
\r
294 /* Start the tasks running. */
\r
295 vTaskStartScheduler();
\r
297 /* If all is well we will never reach here as the scheduler will now be
\r
298 running. If we do reach here then it is likely that there was insufficient
\r
299 heap available for the idle task to be created. */
\r
302 /*-----------------------------------------------------------*/
\r
304 static void vCheckTimerCallback( xTimerHandle xTimer )
\r
306 extern unsigned long ulRegTest1CycleCount, ulRegTest2CycleCount;
\r
307 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
308 static long lErrorAlreadyLatched = pdFALSE;
\r
310 /* This is the callback function used by the 'check' timer, as described
\r
311 at the top of this file. */
\r
314 /* Check the standard demo tasks are running without error. */
\r
315 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
317 /* Increase the rate at which this task cycles, which will increase the
\r
318 rate at which mainCHECK_LED flashes to give visual feedback that an error
\r
320 pcStatusMessage = "Error: GenQueue";
\r
321 xPrintf( pcStatusMessage );
\r
324 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
326 pcStatusMessage = "Error: QueuePeek\r\n";
\r
327 xPrintf( pcStatusMessage );
\r
330 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
332 pcStatusMessage = "Error: BlockQueue\r\n";
\r
333 xPrintf( pcStatusMessage );
\r
336 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
338 pcStatusMessage = "Error: BlockTime\r\n";
\r
339 xPrintf( pcStatusMessage );
\r
342 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
344 pcStatusMessage = "Error: SemTest\r\n";
\r
345 xPrintf( pcStatusMessage );
\r
348 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
350 pcStatusMessage = "Error: PollQueue\r\n";
\r
351 xPrintf( pcStatusMessage );
\r
354 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
356 pcStatusMessage = "Error: Death\r\n";
\r
357 xPrintf( pcStatusMessage );
\r
360 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
362 pcStatusMessage = "Error: RecMutex\r\n";
\r
363 xPrintf( pcStatusMessage );
\r
366 if( xAreMathsTaskStillRunning() != pdPASS )
\r
368 pcStatusMessage = "Error: Flop\r\n";
\r
369 xPrintf( pcStatusMessage );
\r
372 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
373 their loop counters if they encounter an error. */
\r
374 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
376 pcStatusMessage = "Error: RegTest1\r\n";
\r
377 xPrintf( pcStatusMessage );
\r
380 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
382 pcStatusMessage = "Error: RegTest2\r\n";
\r
383 xPrintf( pcStatusMessage );
\r
386 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
387 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
389 /* Toggle the check LED to give an indication of the system status. If
\r
390 the LED toggles every 5 seconds then everything is ok. A faster toggle
\r
391 indicates an error. */
\r
392 vParTestToggleLED( mainCHECK_LED );
\r
394 if( pcStatusMessage != NULL )
\r
396 if( lErrorAlreadyLatched == pdFALSE )
\r
398 /* Ensure the LED toggles at a faster rate if an error has occurred.
\r
399 This is called from a timer callback so must not attempt to block. */
400 xTimerChangePeriod( xTimer, mainERROR_CHECK_TIMER_PERIOD, mainDONT_BLOCK );
\r
402 /* Just to ensure the timer period is not changed on each execution
\r
404 lErrorAlreadyLatched = pdTRUE;
\r
408 /*-----------------------------------------------------------*/
\r
410 void vApplicationSetupTimerInterrupt( void )
\r
412 portBASE_TYPE xStatus;
\r
413 const unsigned char ucTimerCounterNumber = ( unsigned char ) 0U;
\r
414 //const unsigned long ulCounterValue = ( ( XPAR_AXI_TIMER_0_CLOCK_FREQ_HZ / configTICK_RATE_HZ ) - 1UL );
\r
415 const unsigned long ulCounterValue = ( ( ( XPAR_AXI_TIMER_0_CLOCK_FREQ_HZ / configTICK_RATE_HZ ) - 1UL ) ) * 2UL; //_RB_ there is a clock set up incorrectly somwehre, the *2 should not be required.
\r
416 extern void vTickISR( void *pvUnused );
\r
418 /* Initialise the timer/counter. */
\r
419 xStatus = XTmrCtr_Initialize( &xTimer0Instance, XPAR_AXI_TIMER_0_DEVICE_ID );
\r
421 if( xStatus == XST_SUCCESS )
\r
423 /* Install the tick interrupt handler as the timer ISR. */
\r
424 xStatus = xPortInstallInterruptHandler( XPAR_MICROBLAZE_0_INTC_AXI_TIMER_0_INTERRUPT_INTR, vTickISR, NULL );
\r
427 if( xStatus == pdPASS )
\r
429 vPortEnableInterrupt( XPAR_MICROBLAZE_0_INTC_AXI_TIMER_0_INTERRUPT_INTR );
\r
431 /* Configure the timer interrupt handler. */
\r
432 XTmrCtr_SetHandler( &xTimer0Instance, ( void * ) vTickISR, NULL );
\r
434 /* Set the correct period for the timer. */
\r
435 XTmrCtr_SetResetValue( &xTimer0Instance, ucTimerCounterNumber, ulCounterValue );
\r
437 /* Enable the interrupts. Auto-reload mode is used to generate a
\r
438 periodic tick. Note that interrupts are disabled when this function is
\r
439 called, so interrupts will not start to be processed until the first
\r
440 task has started to run. */
\r
441 XTmrCtr_SetOptions( &xTimer0Instance, ucTimerCounterNumber, ( XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION | XTC_DOWN_COUNT_OPTION ) );
\r
443 /* Start the timer. */
\r
444 XTmrCtr_Start( &xTimer0Instance, ucTimerCounterNumber );
\r
447 configASSERT( ( xStatus == pdPASS ) );
\r
449 /*-----------------------------------------------------------*/
\r
451 void vApplicationClearTimerInterrupt( void )
\r
453 unsigned long ulCSR;
\r
455 /* Increment the RTOS tick - this might cause a task to unblock. */
\r
456 vTaskIncrementTick();
\r
458 /* Clear the timer interrupt */
\r
459 ulCSR = XTmrCtr_GetControlStatusReg( XPAR_AXI_TIMER_0_BASEADDR, 0 );
\r
460 XTmrCtr_SetControlStatusReg( XPAR_AXI_TIMER_0_BASEADDR, 0, ulCSR );
\r
462 /*-----------------------------------------------------------*/
\r
464 /* This function is explained by the comments above its prototype at the top
\r
466 void vApplicationMallocFailedHook( void )
\r
470 /*-----------------------------------------------------------*/
\r
472 /* This function is explained by the comments above its prototype at the top
\r
474 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
\r
478 /*-----------------------------------------------------------*/
\r
480 /* This function is explained by the comments above its prototype at the top
\r
482 void vApplicationIdleHook( void )
\r
485 /*-----------------------------------------------------------*/
\r
487 char *pcGetTaskStatusMessage( void )
\r
489 /* Not bothered about a critical section here although technically because of
\r
490 the task priorities the pointer could change it will be atomic if not near
\r
491 atomic and its not critical. */
\r
492 if( pcStatusMessage == NULL )
\r
494 return "All tasks running without error";
\r
498 return ( char * ) pcStatusMessage;
\r
501 /*-----------------------------------------------------------*/
\r
503 static void prvSetupHardware( void )
\r
506 portBASE_TYPE xStatus;
\r
507 const unsigned char ucSetToOutput = 0U;
\r
509 /* Set up the GPIO port for the LED outputs. */
\r
510 vParTestInitialise();
\r
512 /* Initialise the GPIO for the button inputs. */
\r
513 if( xStatus == XST_SUCCESS )
\r
515 xStatus = XGpio_Initialize( &xInputGPIOInstance, XPAR_PUSH_BUTTONS_4BITS_DEVICE_ID );
\r
518 if( xStatus == XST_SUCCESS )
\r
520 /* Install the handler defined in this task for the button input. */
\r
521 xStatus = xPortInstallInterruptHandler( XPAR_MICROBLAZE_0_INTC_PUSH_BUTTONS_4BITS_IP2INTC_IRPT_INTR, prvButtonInputInterruptHandler, NULL );
\r
523 if( xStatus == pdPASS )
\r
525 /* Set buttons to input. */
\r
526 XGpio_SetDataDirection( &xInputGPIOInstance, uxGPIOInputChannel, ~( ucSetToOutput ) );
\r
529 vPortEnableInterrupt( XPAR_MICROBLAZE_0_INTC_PUSH_BUTTONS_4BITS_IP2INTC_IRPT_INTR );
\r
531 /* Enable GPIO channel interrupts. */
\r
532 XGpio_InterruptEnable( &xInputGPIOInstance, uxGPIOInputChannel ); //_RB_
\r
533 XGpio_InterruptGlobalEnable( &xInputGPIOInstance );
\r
537 configASSERT( ( xStatus == pdPASS ) );
\r
539 taskDISABLE_INTERRUPTS();
\r
540 vParTestInitialise();
\r
543 #ifdef MICROBLAZE_EXCEPTIONS_ENABLED
\r
544 //_RB_ microblaze_enable_exceptions();
\r
547 /*-----------------------------------------------------------*/
\r