2 FreeRTOS V7.1.1 - Copyright (C) 2012 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 ***************************************************************************
\r
53 * Having a problem? Start by reading the FAQ "My application does *
\r
54 * not run, what could be wrong? *
\r
56 * http://www.FreeRTOS.org/FAQHelp.html *
\r
58 ***************************************************************************
\r
61 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
62 license and contact details.
\r
64 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
65 including FreeRTOS+Trace - an indispensable productivity tool.
\r
67 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
68 the code with commercial support, indemnification, and middleware, under
\r
69 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
70 provide a safety engineered and independently SIL3 certified version under
\r
71 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
75 * FreeRTOS-main.c (this file) defines a very simple demo that creates two tasks,
\r
76 * one queue, and one timer.
\r
78 * The main() Function:
\r
79 * main() creates one software timer, one queue, and two tasks. It then starts
\r
82 * The Queue Send Task:
\r
83 * The queue send task is implemented by the prvQueueSendTask() function in
\r
84 * this file. prvQueueSendTask() sits in a loop that causes it to repeatedly
\r
85 * block for 200 milliseconds, before sending the value 100 to the queue that
\r
86 * was created within main(). Once the value is sent, the task loops back
\r
87 * around to block for another 200 milliseconds.
\r
89 * The Queue Receive Task:
\r
90 * The queue receive task is implemented by the prvQueueReceiveTask() function
\r
91 * in this file. prvQueueReceiveTask() sits in a loop that causes it to
\r
92 * repeatedly attempt to read data from the queue that was created within
\r
93 * main(). When data is received, the task checks the value of the data, and
\r
94 * if the value equals the expected 100, increments the ulRecieved variable.
\r
95 * The 'block time' parameter passed to the queue receive function specifies
\r
96 * that the task should be held in the Blocked state indefinitely to wait for
\r
97 * data to be available on the queue. The queue receive task will only leave
\r
98 * the Blocked state when the queue send task writes to the queue. As the queue
\r
99 * send task writes to the queue every 200 milliseconds, the queue receive task
\r
100 * leaves the Blocked state every 200 milliseconds, and therefore toggles the LED
\r
101 * every 200 milliseconds.
\r
103 * The Software Timer:
\r
104 * The software timer is configured to be an "auto reset" timer. Its callback
\r
105 * function simply increments the ulCallback variable each time it executes.
\r
108 /* Kernel includes. */
\r
109 #include "FreeRTOS.h"
\r
112 #include "timers.h"
\r
114 /* BSP includes. */
\r
115 #include "xtmrctr.h"
\r
117 /* Priorities at which the tasks are created. */
\r
118 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
119 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
121 /* The rate at which data is sent to the queue, specified in milliseconds, and
\r
122 converted to ticks using the portTICK_RATE_MS constant. */
\r
123 #define mainQUEUE_SEND_FREQUENCY_MS ( 200 / portTICK_RATE_MS )
\r
125 /* The number of items the queue can hold. This is 1 as the receive task
\r
126 will remove items as they are added because it has the higher priority, meaning
\r
127 the send task should always find the queue empty. */
\r
128 #define mainQUEUE_LENGTH ( 1 )
\r
130 /* A block time of 0 simply means, "don't block". */
\r
131 #define mainDONT_BLOCK ( portTickType ) 0
\r
133 /* The following constants describe the timer instance used in this application.
\r
134 They are defined here such that a user can easily change all the needed parameters
\r
136 #define TIMER_DEVICE_ID XPAR_TMRCTR_0_DEVICE_ID
\r
137 #define TIMER_FREQ_HZ XPAR_TMRCTR_0_CLOCK_FREQ_HZ
\r
138 #define TIMER_INTR_ID XPAR_INTC_0_TMRCTR_0_VEC_ID
\r
140 /*-----------------------------------------------------------*/
\r
143 * The tasks as described in the comments at the top of this file.
\r
145 static void prvQueueReceiveTask( void *pvParameters );
\r
146 static void prvQueueSendTask( void *pvParameters );
\r
149 * The LED timer callback function. This does nothing but increment the
\r
150 * ulCallback variable each time it executes.
\r
152 static void vSoftwareTimerCallback( xTimerHandle xTimer );
\r
154 /*-----------------------------------------------------------*/
\r
156 /* The queue used by the queue send and queue receive tasks. */
\r
157 static xQueueHandle xQueue = NULL;
\r
159 /* The LED software timer. This uses vSoftwareTimerCallback() as its callback
\r
161 static xTimerHandle xExampleSoftwareTimer = NULL;
\r
163 /*-----------------------------------------------------------*/
\r
165 /* Structures that hold the state of the various peripherals used by this demo.
\r
166 These are used by the Xilinx peripheral driver API functions. */
\r
167 static XTmrCtr xTimer0Instance;
\r
169 /* The variable that is incremented each time the receive task receives the
\r
171 static unsigned long ulReceived = 0UL;
\r
173 /* The variable that is incremented each time the software time callback function
\r
175 static unsigned long ulCallback = 0UL;
\r
177 /*-----------------------------------------------------------*/
\r
181 /***************************************************************************
\r
182 See http://www.FreeRTOS.org for full information on FreeRTOS, including
\r
183 an API reference, pdf API reference manuals, and FreeRTOS tutorial books.
\r
185 See http://www.freertos.org/Free-RTOS-for-Xilinx-MicroBlaze-on-Spartan-6-FPGA.html
\r
186 for comprehensive standalone FreeRTOS for MicroBlaze demos.
\r
187 ***************************************************************************/
\r
189 /* Create the queue used by the queue send and queue receive tasks as
\r
190 described in the comments at the top of this file. */
\r
191 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
\r
193 /* Sanity check that the queue was created. */
\r
194 configASSERT( xQueue );
\r
196 /* Start the two tasks as described in the comments at the top of this
\r
198 xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );
\r
199 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
\r
201 /* Create the software timer */
\r
202 xExampleSoftwareTimer = xTimerCreate( ( const signed char * ) "SoftwareTimer", /* A text name, purely to help debugging. */
\r
203 ( 5000 / portTICK_RATE_MS ), /* The timer period, in this case 5000ms (5s). */
\r
204 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
205 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
206 vSoftwareTimerCallback /* The callback function that switches the LED off. */
\r
209 /* Start the software timer. */
\r
210 xTimerStart( xExampleSoftwareTimer, mainDONT_BLOCK );
\r
212 /* Start the tasks and timer running. */
\r
213 vTaskStartScheduler();
\r
215 /* If all is well, the scheduler will now be running, and the following line
\r
216 will never be reached. If the following line does execute, then there was
\r
217 insufficient FreeRTOS heap memory available for the idle and/or timer tasks
\r
218 to be created. See the memory management section on the FreeRTOS web site
\r
219 for more details. */
\r
222 /*-----------------------------------------------------------*/
\r
224 /* The callback is executed when the software timer expires. */
\r
225 static void vSoftwareTimerCallback( xTimerHandle xTimer )
\r
227 /* Just increment the ulCallbac variable. */
\r
230 /*-----------------------------------------------------------*/
\r
232 static void prvQueueSendTask( void *pvParameters )
\r
234 portTickType xNextWakeTime;
\r
235 const unsigned long ulValueToSend = 100UL;
\r
237 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
238 xNextWakeTime = xTaskGetTickCount();
\r
242 /* Place this task in the blocked state until it is time to run again.
\r
243 The block time is specified in ticks, the constant used converts ticks
\r
244 to ms. While in the Blocked state this task will not consume any CPU
\r
246 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
\r
248 /* Send to the queue - causing the queue receive task to unblock and
\r
249 toggle an LED. 0 is used as the block time so the sending operation
\r
250 will not block - it shouldn't need to block as the queue should always
\r
251 be empty at this point in the code. */
\r
252 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );
\r
255 /*-----------------------------------------------------------*/
\r
257 static void prvQueueReceiveTask( void *pvParameters )
\r
259 unsigned long ulReceivedValue;
\r
263 /* Wait until something arrives in the queue - this task will block
\r
264 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
\r
265 FreeRTOSConfig.h. */
\r
266 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
\r
268 /* To get here something must have been received from the queue, but
\r
269 is it the expected value? If it is, increment the ulReceived variable. */
\r
270 if( ulReceivedValue == 100UL )
\r
276 /*-----------------------------------------------------------*/
\r
278 void vApplicationMallocFailedHook( void )
\r
280 /* vApplicationMallocFailedHook() will only be called if
\r
281 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
282 function that will get called if a call to pvPortMalloc() fails.
\r
283 pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
284 semaphore is created. It is also called by various parts of the demo
\r
285 application. If heap_1.c or heap_2.c are used, then the size of the heap
\r
286 available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
\r
287 FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
\r
288 to query the size of free heap space that remains (although it does not
\r
289 provide information on how the remaining heap might be fragmented). */
\r
290 taskDISABLE_INTERRUPTS();
\r
293 /*-----------------------------------------------------------*/
\r
295 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
\r
297 ( void ) pcTaskName;
\r
300 /* vApplicationStackOverflowHook() will only be called if
\r
301 configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2. The handle and name
\r
302 of the offending task will be passed into the hook function via its
\r
303 parameters. However, when a stack has overflowed, it is possible that the
\r
304 parameters will have been corrupted, in which case the pxCurrentTCB variable
\r
305 can be inspected directly. */
\r
306 taskDISABLE_INTERRUPTS();
\r
309 /*-----------------------------------------------------------*/
\r
311 void vApplicationIdleHook( void )
\r
313 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
314 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
315 task. It is essential that code added to this hook function never attempts
\r
316 to block in any way (for example, call xQueueReceive() with a block time
\r
317 specified, or call vTaskDelay()). If the application makes use of the
\r
318 vTaskDelete() API function (as this demo application does) then it is also
\r
319 important that vApplicationIdleHook() is permitted to return to its calling
\r
320 function, because it is the responsibility of the idle task to clean up
\r
321 memory allocated by the kernel to any task that has since been deleted. */
\r
323 /*-----------------------------------------------------------*/
\r
325 void vApplicationTickHook( void )
\r
327 /* vApplicationTickHook() will only be called if configUSE_TICK_HOOK is set
\r
328 to 1 in FreeRTOSConfig.h. It executes from an interrupt context so must
\r
329 not use any FreeRTOS API functions that do not end in ...FromISR().
\r
331 This simple blinky demo does not use the tick hook, but a tick hook is
\r
332 required to be defined as the blinky and full demos share a
\r
333 FreeRTOSConfig.h header file. */
\r
335 /*-----------------------------------------------------------*/
\r
337 /* This is an application defined callback function used to install the tick
\r
338 interrupt handler. It is provided as an application callback because the kernel
\r
339 will run on lots of different MicroBlaze and FPGA configurations - there could
\r
340 be multiple timer instances in the hardware platform and the users can chose to
\r
341 use any one of them. This example uses Timer 0. If that is available in your
\r
342 hardware platform then this example callback implementation should not require
\r
343 modification. The definitions for the timer instance used are at the top of this
\r
344 file so that users can change them at one place based on the timer instance they
\r
345 use. The name of the interrupt handler that should be installed is vPortTickISR(),
\r
346 which the function below declares as an extern. */
\r
347 void vApplicationSetupTimerInterrupt( void )
\r
349 portBASE_TYPE xStatus;
\r
350 const unsigned char ucTimerCounterNumber = ( unsigned char ) 0U;
\r
351 const unsigned long ulCounterValue = ( ( TIMER_FREQ_HZ / configTICK_RATE_HZ ) - 1UL );
\r
352 extern void vPortTickISR( void *pvUnused );
\r
354 /* Initialise the timer/counter. */
\r
355 xStatus = XTmrCtr_Initialize( &xTimer0Instance, TIMER_DEVICE_ID );
\r
357 if( xStatus == XST_SUCCESS )
\r
359 /* Install the tick interrupt handler as the timer ISR.
\r
360 *NOTE* The xPortInstallInterruptHandler() API function must be used for
\r
362 xStatus = xPortInstallInterruptHandler( TIMER_INTR_ID, vPortTickISR, NULL );
\r
365 if( xStatus == pdPASS )
\r
367 /* Enable the timer interrupt in the interrupt controller.
\r
368 *NOTE* The vPortEnableInterrupt() API function must be used for this
\r
370 vPortEnableInterrupt( TIMER_INTR_ID );
\r
372 /* Configure the timer interrupt handler. */
\r
373 XTmrCtr_SetHandler( &xTimer0Instance, ( void * ) vPortTickISR, NULL );
\r
375 /* Set the correct period for the timer. */
\r
376 XTmrCtr_SetResetValue( &xTimer0Instance, ucTimerCounterNumber, ulCounterValue );
\r
378 /* Enable the interrupts. Auto-reload mode is used to generate a
\r
379 periodic tick. Note that interrupts are disabled when this function is
\r
380 called, so interrupts will not start to be processed until the first
\r
381 task has started to run. */
\r
382 XTmrCtr_SetOptions( &xTimer0Instance, ucTimerCounterNumber, ( XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION | XTC_DOWN_COUNT_OPTION ) );
\r
384 /* Start the timer. */
\r
385 XTmrCtr_Start( &xTimer0Instance, ucTimerCounterNumber );
\r
388 /* Sanity check that the function executed as expected. */
\r
389 configASSERT( ( xStatus == pdPASS ) );
\r
391 /*-----------------------------------------------------------*/
\r
393 /* This is an application defined callback function used to clear whichever
\r
394 interrupt was installed by the the vApplicationSetupTimerInterrupt() callback
\r
395 function - in this case the interrupt generated by the AXI timer. It is
\r
396 provided as an application callback because the kernel will run on lots of
\r
397 different MicroBlaze and FPGA configurations - not all of which will have the
\r
398 same timer peripherals defined or available. This example uses the AXI Timer 0.
\r
399 If that is available on your hardware platform then this example callback
\r
400 implementation should not require modification provided the example definition
\r
401 of vApplicationSetupTimerInterrupt() is also not modified. */
\r
402 void vApplicationClearTimerInterrupt( void )
\r
404 unsigned long ulCSR;
\r
406 /* Clear the timer interrupt */
\r
407 ulCSR = XTmrCtr_GetControlStatusReg( XPAR_TMRCTR_0_BASEADDR, 0 );
\r
408 XTmrCtr_SetControlStatusReg( XPAR_TMRCTR_0_BASEADDR, 0, ulCSR );
\r
410 /*-----------------------------------------------------------*/
\r