2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * FreeRTOS-main.c (this file) defines a very simple demo that creates two tasks,
\r
67 * one queue, and one timer.
\r
69 * The main() Function:
\r
70 * main() creates one software timer, one queue, and two tasks. It then starts
\r
73 * The Queue Send Task:
\r
74 * The queue send task is implemented by the prvQueueSendTask() function in
\r
75 * this file. prvQueueSendTask() sits in a loop that causes it to repeatedly
\r
76 * block for 200 milliseconds, before sending the value 100 to the queue that
\r
77 * was created within main(). Once the value is sent, the task loops back
\r
78 * around to block for another 200 milliseconds.
\r
80 * The Queue Receive Task:
\r
81 * The queue receive task is implemented by the prvQueueReceiveTask() function
\r
82 * in this file. prvQueueReceiveTask() sits in a loop that causes it to
\r
83 * repeatedly attempt to read data from the queue that was created within
\r
84 * main(). When data is received, the task checks the value of the data, and
\r
85 * if the value equals the expected 100, increments the ulRecieved variable.
\r
86 * The 'block time' parameter passed to the queue receive function specifies
\r
87 * that the task should be held in the Blocked state indefinitely to wait for
\r
88 * data to be available on the queue. The queue receive task will only leave
\r
89 * the Blocked state when the queue send task writes to the queue. As the queue
\r
90 * send task writes to the queue every 200 milliseconds, the queue receive task
\r
91 * leaves the Blocked state every 200 milliseconds, and therefore toggles the LED
\r
92 * every 200 milliseconds.
\r
94 * The Software Timer:
\r
95 * The software timer is configured to be an "auto reset" timer. Its callback
\r
96 * function simply increments the ulCallback variable each time it executes.
\r
99 /* Kernel includes. */
\r
100 #include "FreeRTOS.h"
\r
103 #include "timers.h"
\r
105 /* BSP includes. */
\r
106 #include "xtmrctr.h"
\r
108 /* Priorities at which the tasks are created. */
\r
109 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
110 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
112 /* The rate at which data is sent to the queue, specified in milliseconds, and
\r
113 converted to ticks using the portTICK_RATE_MS constant. */
\r
114 #define mainQUEUE_SEND_FREQUENCY_MS ( 200 / portTICK_RATE_MS )
\r
116 /* The number of items the queue can hold. This is 1 as the receive task
\r
117 will remove items as they are added because it has the higher priority, meaning
\r
118 the send task should always find the queue empty. */
\r
119 #define mainQUEUE_LENGTH ( 1 )
\r
121 /* A block time of 0 simply means, "don't block". */
\r
122 #define mainDONT_BLOCK ( portTickType ) 0
\r
124 /* The following constants describe the timer instance used in this application.
\r
125 They are defined here such that a user can easily change all the needed parameters
\r
127 #define TIMER_DEVICE_ID XPAR_TMRCTR_0_DEVICE_ID
\r
128 #define TIMER_FREQ_HZ XPAR_TMRCTR_0_CLOCK_FREQ_HZ
\r
129 #define TIMER_INTR_ID XPAR_INTC_0_TMRCTR_0_VEC_ID
\r
131 /*-----------------------------------------------------------*/
\r
134 * The tasks as described in the comments at the top of this file.
\r
136 static void prvQueueReceiveTask( void *pvParameters );
\r
137 static void prvQueueSendTask( void *pvParameters );
\r
140 * The LED timer callback function. This does nothing but increment the
\r
141 * ulCallback variable each time it executes.
\r
143 static void vSoftwareTimerCallback( xTimerHandle xTimer );
\r
145 /*-----------------------------------------------------------*/
\r
147 /* The queue used by the queue send and queue receive tasks. */
\r
148 static xQueueHandle xQueue = NULL;
\r
150 /* The LED software timer. This uses vSoftwareTimerCallback() as its callback
\r
152 static xTimerHandle xExampleSoftwareTimer = NULL;
\r
154 /*-----------------------------------------------------------*/
\r
156 /* Structures that hold the state of the various peripherals used by this demo.
\r
157 These are used by the Xilinx peripheral driver API functions. */
\r
158 static XTmrCtr xTimer0Instance;
\r
160 /* The variable that is incremented each time the receive task receives the
\r
162 static unsigned long ulReceived = 0UL;
\r
164 /* The variable that is incremented each time the software time callback function
\r
166 static unsigned long ulCallback = 0UL;
\r
168 /*-----------------------------------------------------------*/
\r
172 /***************************************************************************
\r
173 See http://www.FreeRTOS.org for full information on FreeRTOS, including
\r
174 an API reference, pdf API reference manuals, and FreeRTOS tutorial books.
\r
176 See http://www.freertos.org/Free-RTOS-for-Xilinx-MicroBlaze-on-Spartan-6-FPGA.html
\r
177 for comprehensive standalone FreeRTOS for MicroBlaze demos.
\r
178 ***************************************************************************/
\r
180 /* Create the queue used by the queue send and queue receive tasks as
\r
181 described in the comments at the top of this file. */
\r
182 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
\r
184 /* Sanity check that the queue was created. */
\r
185 configASSERT( xQueue );
\r
187 /* Start the two tasks as described in the comments at the top of this
\r
189 xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );
\r
190 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
\r
192 /* Create the software timer */
\r
193 xExampleSoftwareTimer = xTimerCreate( ( const signed char * ) "SoftwareTimer", /* A text name, purely to help debugging. */
\r
194 ( 5000 / portTICK_RATE_MS ), /* The timer period, in this case 5000ms (5s). */
\r
195 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
196 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
197 vSoftwareTimerCallback /* The callback function that switches the LED off. */
\r
200 /* Start the software timer. */
\r
201 xTimerStart( xExampleSoftwareTimer, mainDONT_BLOCK );
\r
203 /* Start the tasks and timer running. */
\r
204 vTaskStartScheduler();
\r
206 /* If all is well, the scheduler will now be running, and the following line
\r
207 will never be reached. If the following line does execute, then there was
\r
208 insufficient FreeRTOS heap memory available for the idle and/or timer tasks
\r
209 to be created. See the memory management section on the FreeRTOS web site
\r
210 for more details. */
\r
213 /*-----------------------------------------------------------*/
\r
215 /* The callback is executed when the software timer expires. */
\r
216 static void vSoftwareTimerCallback( xTimerHandle xTimer )
\r
218 /* Just increment the ulCallbac variable. */
\r
221 /*-----------------------------------------------------------*/
\r
223 static void prvQueueSendTask( void *pvParameters )
\r
225 portTickType xNextWakeTime;
\r
226 const unsigned long ulValueToSend = 100UL;
\r
228 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
229 xNextWakeTime = xTaskGetTickCount();
\r
233 /* Place this task in the blocked state until it is time to run again.
\r
234 The block time is specified in ticks, the constant used converts ticks
\r
235 to ms. While in the Blocked state this task will not consume any CPU
\r
237 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
\r
239 /* Send to the queue - causing the queue receive task to unblock and
\r
240 toggle an LED. 0 is used as the block time so the sending operation
\r
241 will not block - it shouldn't need to block as the queue should always
\r
242 be empty at this point in the code. */
\r
243 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );
\r
246 /*-----------------------------------------------------------*/
\r
248 static void prvQueueReceiveTask( void *pvParameters )
\r
250 unsigned long ulReceivedValue;
\r
254 /* Wait until something arrives in the queue - this task will block
\r
255 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
\r
256 FreeRTOSConfig.h. */
\r
257 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
\r
259 /* To get here something must have been received from the queue, but
\r
260 is it the expected value? If it is, increment the ulReceived variable. */
\r
261 if( ulReceivedValue == 100UL )
\r
267 /*-----------------------------------------------------------*/
\r
269 void vApplicationMallocFailedHook( void )
\r
271 /* vApplicationMallocFailedHook() will only be called if
\r
272 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
273 function that will get called if a call to pvPortMalloc() fails.
\r
274 pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
275 semaphore is created. It is also called by various parts of the demo
\r
276 application. If heap_1.c or heap_2.c are used, then the size of the heap
\r
277 available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
\r
278 FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
\r
279 to query the size of free heap space that remains (although it does not
\r
280 provide information on how the remaining heap might be fragmented). */
\r
281 taskDISABLE_INTERRUPTS();
\r
284 /*-----------------------------------------------------------*/
\r
286 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
\r
288 ( void ) pcTaskName;
\r
291 /* vApplicationStackOverflowHook() will only be called if
\r
292 configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2. The handle and name
\r
293 of the offending task will be passed into the hook function via its
\r
294 parameters. However, when a stack has overflowed, it is possible that the
\r
295 parameters will have been corrupted, in which case the pxCurrentTCB variable
\r
296 can be inspected directly. */
\r
297 taskDISABLE_INTERRUPTS();
\r
300 /*-----------------------------------------------------------*/
\r
302 void vApplicationIdleHook( void )
\r
304 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
\r
305 to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
\r
306 task. It is essential that code added to this hook function never attempts
\r
307 to block in any way (for example, call xQueueReceive() with a block time
\r
308 specified, or call vTaskDelay()). If the application makes use of the
\r
309 vTaskDelete() API function (as this demo application does) then it is also
\r
310 important that vApplicationIdleHook() is permitted to return to its calling
\r
311 function, because it is the responsibility of the idle task to clean up
\r
312 memory allocated by the kernel to any task that has since been deleted. */
\r
314 /*-----------------------------------------------------------*/
\r
316 void vApplicationTickHook( void )
\r
318 /* vApplicationTickHook() will only be called if configUSE_TICK_HOOK is set
\r
319 to 1 in FreeRTOSConfig.h. It executes from an interrupt context so must
\r
320 not use any FreeRTOS API functions that do not end in ...FromISR().
\r
322 This simple blinky demo does not use the tick hook, but a tick hook is
\r
323 required to be defined as the blinky and full demos share a
\r
324 FreeRTOSConfig.h header file. */
\r
326 /*-----------------------------------------------------------*/
\r
328 /* This is an application defined callback function used to install the tick
\r
329 interrupt handler. It is provided as an application callback because the kernel
\r
330 will run on lots of different MicroBlaze and FPGA configurations - there could
\r
331 be multiple timer instances in the hardware platform and the users can chose to
\r
332 use any one of them. This example uses Timer 0. If that is available in your
\r
333 hardware platform then this example callback implementation should not require
\r
334 modification. The definitions for the timer instance used are at the top of this
\r
335 file so that users can change them at one place based on the timer instance they
\r
336 use. The name of the interrupt handler that should be installed is vPortTickISR(),
\r
337 which the function below declares as an extern. */
\r
338 void vApplicationSetupTimerInterrupt( void )
\r
340 portBASE_TYPE xStatus;
\r
341 const unsigned char ucTimerCounterNumber = ( unsigned char ) 0U;
\r
342 const unsigned long ulCounterValue = ( ( TIMER_FREQ_HZ / configTICK_RATE_HZ ) - 1UL );
\r
343 extern void vPortTickISR( void *pvUnused );
\r
345 /* Initialise the timer/counter. */
\r
346 xStatus = XTmrCtr_Initialize( &xTimer0Instance, TIMER_DEVICE_ID );
\r
348 if( xStatus == XST_SUCCESS )
\r
350 /* Install the tick interrupt handler as the timer ISR.
\r
351 *NOTE* The xPortInstallInterruptHandler() API function must be used for
\r
353 xStatus = xPortInstallInterruptHandler( TIMER_INTR_ID, vPortTickISR, NULL );
\r
356 if( xStatus == pdPASS )
\r
358 /* Enable the timer interrupt in the interrupt controller.
\r
359 *NOTE* The vPortEnableInterrupt() API function must be used for this
\r
361 vPortEnableInterrupt( TIMER_INTR_ID );
\r
363 /* Configure the timer interrupt handler. */
\r
364 XTmrCtr_SetHandler( &xTimer0Instance, ( void * ) vPortTickISR, NULL );
\r
366 /* Set the correct period for the timer. */
\r
367 XTmrCtr_SetResetValue( &xTimer0Instance, ucTimerCounterNumber, ulCounterValue );
\r
369 /* Enable the interrupts. Auto-reload mode is used to generate a
\r
370 periodic tick. Note that interrupts are disabled when this function is
\r
371 called, so interrupts will not start to be processed until the first
\r
372 task has started to run. */
\r
373 XTmrCtr_SetOptions( &xTimer0Instance, ucTimerCounterNumber, ( XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION | XTC_DOWN_COUNT_OPTION ) );
\r
375 /* Start the timer. */
\r
376 XTmrCtr_Start( &xTimer0Instance, ucTimerCounterNumber );
\r
379 /* Sanity check that the function executed as expected. */
\r
380 configASSERT( ( xStatus == pdPASS ) );
\r
382 /*-----------------------------------------------------------*/
\r
384 /* This is an application defined callback function used to clear whichever
\r
385 interrupt was installed by the the vApplicationSetupTimerInterrupt() callback
\r
386 function - in this case the interrupt generated by the AXI timer. It is
\r
387 provided as an application callback because the kernel will run on lots of
\r
388 different MicroBlaze and FPGA configurations - not all of which will have the
\r
389 same timer peripherals defined or available. This example uses the AXI Timer 0.
\r
390 If that is available on your hardware platform then this example callback
\r
391 implementation should not require modification provided the example definition
\r
392 of vApplicationSetupTimerInterrupt() is also not modified. */
\r
393 void vApplicationClearTimerInterrupt( void )
\r
395 unsigned long ulCSR;
\r
397 /* Clear the timer interrupt */
\r
398 ulCSR = XTmrCtr_GetControlStatusReg( XPAR_TMRCTR_0_BASEADDR, 0 );
\r
399 XTmrCtr_SetControlStatusReg( XPAR_TMRCTR_0_BASEADDR, 0, ulCSR );
\r
401 /*-----------------------------------------------------------*/
\r