2 FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
5 See http://www.FreeRTOS.org for full information on FreeRTOS, including
\r
6 an API reference, pdf API reference manuals, and FreeRTOS tutorial books.
\r
8 See http://www.freertos.org/Free-RTOS-for-Xilinx-MicroBlaze-on-Spartan-6-FPGA.html
\r
9 for comprehensive standalone FreeRTOS for MicroBlaze demos.
\r
10 ***************************************************************************
\r
12 ***************************************************************************
\r
14 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
15 * Complete, revised, and edited pdf reference manuals are also *
\r
18 * Purchasing FreeRTOS documentation will not only help you, by *
\r
19 * ensuring you get running as quickly as possible and with an *
\r
20 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
21 * the FreeRTOS project to continue with its mission of providing *
\r
22 * professional grade, cross platform, de facto standard solutions *
\r
23 * for microcontrollers - completely free of charge! *
\r
25 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
27 * Thank you for using FreeRTOS, and thank you for your support! *
\r
29 ***************************************************************************
\r
32 This file is part of the FreeRTOS distribution.
\r
34 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
35 the terms of the GNU General Public License (version 2) as published by the
\r
36 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
37 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
38 distribute a combined work that includes FreeRTOS without being obliged to
\r
39 provide the source code for proprietary components outside of the FreeRTOS
\r
40 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
41 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
42 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
43 more details. You should have received a copy of the GNU General Public
\r
44 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
45 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
46 by writing to Richard Barry, contact details for whom are available on the
\r
51 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
54 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
57 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
58 licensing and training services.
\r
62 * main-blinky.c (this file) defines a very simple demo that creates two tasks,
\r
63 * one queue, and one timer.
\r
65 * The main() Function:
\r
66 * main() creates one software timer, one queue, and two tasks. It then starts
\r
69 * The Queue Send Task:
\r
70 * The queue send task is implemented by the prvQueueSendTask() function in
\r
71 * this file. prvQueueSendTask() sits in a loop that causes it to repeatedly
\r
72 * block for 200 milliseconds, before sending the value 100 to the queue that
\r
73 * was created within main(). Once the value is sent, the task loops back
\r
74 * around to block for another 200 milliseconds.
\r
76 * The Queue Receive Task:
\r
77 * The queue receive task is implemented by the prvQueueReceiveTask() function
\r
78 * in this file. prvQueueReceiveTask() sits in a loop that causes it to
\r
79 * repeatedly attempt to read data from the queue that was created within
\r
80 * main(). When data is received, the task checks the value of the data, and
\r
81 * if the value equals the expected 100, increments the ulRecieved variable.
\r
82 * The 'block time' parameter passed to the queue receive function specifies
\r
83 * that the task should be held in the Blocked state indefinitely to wait for
\r
84 * data to be available on the queue. The queue receive task will only leave
\r
85 * the Blocked state when the queue send task writes to the queue. As the queue
\r
86 * send task writes to the queue every 200 milliseconds, the queue receive task
\r
87 * leaves the Blocked state every 200 milliseconds, and therefore toggles the LED
\r
88 * every 200 milliseconds.
\r
90 * The Software Timer
\r
91 * The software timer is configured to be an "auto reset" timer. Its callback
\r
92 * function simply increments the ulCallback variable each time it executes.
\r
95 /* Kernel includes. */
\r
96 #include "FreeRTOS.h"
\r
101 /* BSP includes. */
\r
102 #include "xtmrctr.h"
\r
104 /* Priorities at which the tasks are created. */
\r
105 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
106 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
108 /* The rate at which data is sent to the queue, specified in milliseconds, and
\r
109 converted to ticks using the portTICK_RATE_MS constant. */
\r
110 #define mainQUEUE_SEND_FREQUENCY_MS ( 200 / portTICK_RATE_MS )
\r
112 /* The number of items the queue can hold. This is 1 as the receive task
\r
113 will remove items as they are added because it has the higher priority, meaning
\r
114 the send task should always find the queue empty. */
\r
115 #define mainQUEUE_LENGTH ( 1 )
\r
117 /* A block time of 0 simply means, "don't block". */
\r
118 #define mainDONT_BLOCK ( portTickType ) 0
\r
120 /* The following constants describe the timer instance used in this application.
\r
121 They are defined here such that a user can easily change all the needed parameters
\r
123 #define TIMER_DEVICE_ID XPAR_TMRCTR_0_DEVICE_ID
\r
124 #define TIMER_FREQ_HZ XPAR_TMRCTR_0_CLOCK_FREQ_HZ
\r
125 #define TIMER_INTR_ID XPAR_INTC_0_TMRCTR_0_VEC_ID
\r
127 /*-----------------------------------------------------------*/
\r
130 * The tasks as described in the comments at the top of this file.
\r
132 static void prvQueueReceiveTask( void *pvParameters );
\r
133 static void prvQueueSendTask( void *pvParameters );
\r
136 * The LED timer callback function. This does nothing but increment the
\r
137 * ulCallback variable each time it executes.
\r
139 static void vSoftwareTimerCallback( xTimerHandle xTimer );
\r
141 /*-----------------------------------------------------------*/
\r
143 /* The queue used by the queue send and queue receive tasks. */
\r
144 static xQueueHandle xQueue = NULL;
\r
146 /* The LED software timer. This uses vSoftwareTimerCallback() as its callback
\r
148 static xTimerHandle xExampleSoftwareTimer = NULL;
\r
150 /*-----------------------------------------------------------*/
\r
152 /* Structures that hold the state of the various peripherals used by this demo.
\r
153 These are used by the Xilinx peripheral driver API functions. */
\r
154 static XTmrCtr xTimer0Instance;
\r
156 /* The variable that is incremented each time the receive task receives the
\r
158 static unsigned long ulReceived = 0UL;
\r
160 /* The variable that is incremented each time the software time callback function
\r
162 static unsigned long ulCallback = 0UL;
\r
164 /*-----------------------------------------------------------*/
\r
168 /***************************************************************************
\r
169 See http://www.FreeRTOS.org for full information on FreeRTOS, including
\r
170 an API reference, pdf API reference manuals, and FreeRTOS tutorial books.
\r
172 See http://www.freertos.org/Free-RTOS-for-Xilinx-MicroBlaze-on-Spartan-6-FPGA.html
\r
173 for comprehensive standalone FreeRTOS for MicroBlaze demos.
\r
174 ***************************************************************************/
\r
176 /* Create the queue used by the queue send and queue receive tasks as
\r
177 described in the comments at the top of this file. */
\r
178 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
\r
180 /* Sanity check that the queue was created. */
\r
181 configASSERT( xQueue );
\r
183 /* Start the two tasks as described in the comments at the top of this
\r
185 xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );
\r
186 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
\r
188 /* Create the software timer */
\r
189 xExampleSoftwareTimer = xTimerCreate( ( const signed char * ) "SoftwareTimer", /* A text name, purely to help debugging. */
\r
190 ( 5000 / portTICK_RATE_MS ), /* The timer period, in this case 5000ms (5s). */
\r
191 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
192 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
193 vSoftwareTimerCallback /* The callback function that switches the LED off. */
\r
196 /* Start the software timer. */
\r
197 xTimerStart( xExampleSoftwareTimer, mainDONT_BLOCK );
\r
199 /* Start the tasks and timer running. */
\r
200 vTaskStartScheduler();
\r
202 /* If all is well, the scheduler will now be running, and the following line
\r
203 will never be reached. If the following line does execute, then there was
\r
204 insufficient FreeRTOS heap memory available for the idle and/or timer tasks
\r
205 to be created. See the memory management section on the FreeRTOS web site
\r
206 for more details. */
\r
209 /*-----------------------------------------------------------*/
\r
211 /* The callback is executed when the software timer expires. */
\r
212 static void vSoftwareTimerCallback( xTimerHandle xTimer )
\r
214 /* Just increment the ulCallbac variable. */
\r
217 /*-----------------------------------------------------------*/
\r
219 static void prvQueueSendTask( void *pvParameters )
\r
221 portTickType xNextWakeTime;
\r
222 const unsigned long ulValueToSend = 100UL;
\r
224 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
225 xNextWakeTime = xTaskGetTickCount();
\r
229 /* Place this task in the blocked state until it is time to run again.
\r
230 The block time is specified in ticks, the constant used converts ticks
\r
231 to ms. While in the Blocked state this task will not consume any CPU
\r
233 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
\r
235 /* Send to the queue - causing the queue receive task to unblock and
\r
236 toggle an LED. 0 is used as the block time so the sending operation
\r
237 will not block - it shouldn't need to block as the queue should always
\r
238 be empty at this point in the code. */
\r
239 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );
\r
242 /*-----------------------------------------------------------*/
\r
244 static void prvQueueReceiveTask( void *pvParameters )
\r
246 unsigned long ulReceivedValue;
\r
250 /* Wait until something arrives in the queue - this task will block
\r
251 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
\r
252 FreeRTOSConfig.h. */
\r
253 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
\r
255 /* To get here something must have been received from the queue, but
\r
256 is it the expected value? If it is, increment the ulReceived variable. */
\r
257 if( ulReceivedValue == 100UL )
\r
263 /*-----------------------------------------------------------*/
\r
265 void vApplicationMallocFailedHook( void )
\r
267 /* vApplicationMallocFailedHook() will only be called if
\r
268 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
269 function that will get called if a call to pvPortMalloc() fails.
\r
270 pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
271 semaphore is created. It is also called by various parts of the demo
\r
272 application. If heap_1.c or heap_2.c are used, then the size of the heap
\r
273 available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
\r
274 FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
\r
275 to query the size of free heap space that remains (although it does not
\r
276 provide information on how the remaining heap might be fragmented). */
\r
277 taskDISABLE_INTERRUPTS();
\r
280 /*-----------------------------------------------------------*/
\r
282 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
\r
284 ( void ) pcTaskName;
\r
287 /* vApplicationStackOverflowHook() will only be called if
\r
288 configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2. The handle and name
\r
289 of the offending task will be passed into the hook function via its
\r
290 parameters. However, when a stack has overflowed, it is possible that the
\r
291 parameters will have been corrupted, in which case the pxCurrentTCB variable
\r
292 can be inspected directly. */
\r
293 taskDISABLE_INTERRUPTS();
\r
296 /*-----------------------------------------------------------*/
\r
298 void vApplicationIdleHook( void )
\r
300 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
301 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
302 task. It is essential that code added to this hook function never attempts
\r
303 to block in any way (for example, call xQueueReceive() with a block time
\r
304 specified, or call vTaskDelay()). If the application makes use of the
\r
305 vTaskDelete() API function (as this demo application does) then it is also
\r
306 important that vApplicationIdleHook() is permitted to return to its calling
\r
307 function, because it is the responsibility of the idle task to clean up
\r
308 memory allocated by the kernel to any task that has since been deleted. */
\r
310 /*-----------------------------------------------------------*/
\r
312 void vApplicationTickHook( void )
\r
314 /* vApplicationTickHook() will only be called if configUSE_TICK_HOOK is set
\r
315 to 1 in FreeRTOSConfig.h. It executes from an interrupt context so must
\r
316 not use any FreeRTOS API functions that do not end in ...FromISR().
\r
318 This simple blinky demo does not use the tick hook, but a tick hook is
\r
319 required to be defined as the blinky and full demos share a
\r
320 FreeRTOSConfig.h header file. */
\r
322 /*-----------------------------------------------------------*/
\r
324 /* This is an application defined callback function used to install the tick
\r
325 interrupt handler. It is provided as an application callback because the kernel
\r
326 will run on lots of different MicroBlaze and FPGA configurations - there could
\r
327 be multiple timer instances in the hardware platform and the users can chose to
\r
328 use any one of them. This example uses Timer 0. If that is available in your
\r
329 hardware platform then this example callback implementation should not require
\r
330 modification. The definitions for the timer instance used are at the top of this
\r
331 file so that users can change them at one place based on the timer instance they
\r
332 use. The name of the interrupt handler that should be installed is vPortTickISR(),
\r
333 which the function below declares as an extern. */
\r
334 void vApplicationSetupTimerInterrupt( void )
\r
336 portBASE_TYPE xStatus;
\r
337 const unsigned char ucTimerCounterNumber = ( unsigned char ) 0U;
\r
338 const unsigned long ulCounterValue = ( ( TIMER_FREQ_HZ / configTICK_RATE_HZ ) - 1UL );
\r
339 extern void vPortTickISR( void *pvUnused );
\r
341 /* Initialise the timer/counter. */
\r
342 xStatus = XTmrCtr_Initialize( &xTimer0Instance, TIMER_DEVICE_ID );
\r
344 if( xStatus == XST_SUCCESS )
\r
346 /* Install the tick interrupt handler as the timer ISR.
\r
347 *NOTE* The xPortInstallInterruptHandler() API function must be used for
\r
349 xStatus = xPortInstallInterruptHandler( TIMER_INTR_ID, vPortTickISR, NULL );
\r
352 if( xStatus == pdPASS )
\r
354 /* Enable the timer interrupt in the interrupt controller.
\r
355 *NOTE* The vPortEnableInterrupt() API function must be used for this
\r
357 vPortEnableInterrupt( TIMER_INTR_ID );
\r
359 /* Configure the timer interrupt handler. */
\r
360 XTmrCtr_SetHandler( &xTimer0Instance, ( void * ) vPortTickISR, NULL );
\r
362 /* Set the correct period for the timer. */
\r
363 XTmrCtr_SetResetValue( &xTimer0Instance, ucTimerCounterNumber, ulCounterValue );
\r
365 /* Enable the interrupts. Auto-reload mode is used to generate a
\r
366 periodic tick. Note that interrupts are disabled when this function is
\r
367 called, so interrupts will not start to be processed until the first
\r
368 task has started to run. */
\r
369 XTmrCtr_SetOptions( &xTimer0Instance, ucTimerCounterNumber, ( XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION | XTC_DOWN_COUNT_OPTION ) );
\r
371 /* Start the timer. */
\r
372 XTmrCtr_Start( &xTimer0Instance, ucTimerCounterNumber );
\r
375 /* Sanity check that the function executed as expected. */
\r
376 configASSERT( ( xStatus == pdPASS ) );
\r
378 /*-----------------------------------------------------------*/
\r
380 /* This is an application defined callback function used to clear whichever
\r
381 interrupt was installed by the the vApplicationSetupTimerInterrupt() callback
\r
382 function - in this case the interrupt generated by the AXI timer. It is
\r
383 provided as an application callback because the kernel will run on lots of
\r
384 different MicroBlaze and FPGA configurations - not all of which will have the
\r
385 same timer peripherals defined or available. This example uses the AXI Timer 0.
\r
386 If that is available on your hardware platform then this example callback
\r
387 implementation should not require modification provided the example definition
\r
388 of vApplicationSetupTimerInterrupt() is also not modified. */
\r
389 void vApplicationClearTimerInterrupt( void )
\r
391 unsigned long ulCSR;
\r
393 /* Clear the timer interrupt */
\r
394 ulCSR = XTmrCtr_GetControlStatusReg( XPAR_TMRCTR_0_BASEADDR, 0 );
\r
395 XTmrCtr_SetControlStatusReg( XPAR_TMRCTR_0_BASEADDR, 0, ulCSR );
\r
397 /*-----------------------------------------------------------*/
\r