]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MicroBlaze_Spartan-6_EthernetLite/KernelAwareBSPRepository/sw_apps/FreeRTOS_Hello_World/src/FreeRTOS-main.c
Prepare for V7.2.0 release.
[freertos] / FreeRTOS / Demo / MicroBlaze_Spartan-6_EthernetLite / KernelAwareBSPRepository / sw_apps / FreeRTOS_Hello_World / src / FreeRTOS-main.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \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
7 \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
11 \r
12     ***************************************************************************\r
13      *                                                                       *\r
14      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
15      *    Complete, revised, and edited pdf reference manuals are also       *\r
16      *    available.                                                         *\r
17      *                                                                       *\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
24      *                                                                       *\r
25      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
26      *                                                                       *\r
27      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
28      *                                                                       *\r
29     ***************************************************************************\r
30 \r
31 \r
32     This file is part of the FreeRTOS distribution.\r
33 \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
47     FreeRTOS WEB site.\r
48 \r
49     1 tab == 4 spaces!\r
50     \r
51     ***************************************************************************\r
52      *                                                                       *\r
53      *    Having a problem?  Start by reading the FAQ "My application does   *\r
54      *    not run, what could be wrong?                                      *\r
55      *                                                                       *\r
56      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
57      *                                                                       *\r
58     ***************************************************************************\r
59 \r
60     \r
61     http://www.FreeRTOS.org - Documentation, training, latest information, \r
62     license and contact details.\r
63     \r
64     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
65     including FreeRTOS+Trace - an indispensable productivity tool.\r
66 \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
72 */\r
73 \r
74 /*\r
75  * FreeRTOS-main.c (this file) defines a very simple demo that creates two tasks,\r
76  * one queue, and one timer.\r
77  *\r
78  * The main() Function:\r
79  * main() creates one software timer, one queue, and two tasks.  It then starts\r
80  * the scheduler.\r
81  *\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
88  *\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
102  *\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
106  */\r
107 \r
108 /* Kernel includes. */\r
109 #include "FreeRTOS.h"\r
110 #include "task.h"\r
111 #include "queue.h"\r
112 #include "timers.h"\r
113 \r
114 /* BSP includes. */\r
115 #include "xtmrctr.h"\r
116 \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
120 \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
124 \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
129 \r
130 /* A block time of 0 simply means, "don't block". */\r
131 #define mainDONT_BLOCK                                          ( portTickType ) 0\r
132 \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
135 in one place. */\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
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 /*\r
143  * The tasks as described in the comments at the top of this file.\r
144  */\r
145 static void prvQueueReceiveTask( void *pvParameters );\r
146 static void prvQueueSendTask( void *pvParameters );\r
147 \r
148 /*\r
149  * The LED timer callback function.  This does nothing but increment the\r
150  * ulCallback variable each time it executes.\r
151  */\r
152 static void vSoftwareTimerCallback( xTimerHandle xTimer );\r
153 \r
154 /*-----------------------------------------------------------*/\r
155 \r
156 /* The queue used by the queue send and queue receive tasks. */\r
157 static xQueueHandle xQueue = NULL;\r
158 \r
159 /* The LED software timer.  This uses vSoftwareTimerCallback() as its callback\r
160 function. */\r
161 static xTimerHandle xExampleSoftwareTimer = NULL;\r
162 \r
163 /*-----------------------------------------------------------*/\r
164 \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
168 \r
169 /* The variable that is incremented each time the receive task receives the\r
170 value 100. */\r
171 static unsigned long ulReceived = 0UL;\r
172 \r
173 /* The variable that is incremented each time the software time callback function\r
174 executes. */\r
175 static unsigned long ulCallback = 0UL;\r
176 \r
177 /*-----------------------------------------------------------*/\r
178 \r
179 int main( void )\r
180 {\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
184 \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
188 \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
192 \r
193         /* Sanity check that the queue was created. */\r
194         configASSERT( xQueue );\r
195 \r
196         /* Start the two tasks as described in the comments at the top of this\r
197         file. */\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
200 \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
207                                                                                 );\r
208 \r
209         /* Start the software timer. */\r
210         xTimerStart( xExampleSoftwareTimer, mainDONT_BLOCK );\r
211 \r
212         /* Start the tasks and timer running. */\r
213         vTaskStartScheduler();\r
214 \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
220         for( ;; );\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 /* The callback is executed when the software timer expires. */\r
225 static void vSoftwareTimerCallback( xTimerHandle xTimer )\r
226 {\r
227         /* Just increment the ulCallbac variable. */\r
228         ulCallback++;\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 static void prvQueueSendTask( void *pvParameters )\r
233 {\r
234 portTickType xNextWakeTime;\r
235 const unsigned long ulValueToSend = 100UL;\r
236 \r
237         /* Initialise xNextWakeTime - this only needs to be done once. */\r
238         xNextWakeTime = xTaskGetTickCount();\r
239 \r
240         for( ;; )\r
241         {\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
245                 time. */\r
246                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
247 \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
253         }\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 static void prvQueueReceiveTask( void *pvParameters )\r
258 {\r
259 unsigned long ulReceivedValue;\r
260 \r
261         for( ;; )\r
262         {\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
267 \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
271                 {\r
272                         ulReceived++;\r
273                 }\r
274         }\r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 void vApplicationMallocFailedHook( void )\r
279 {\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
291         for( ;; );\r
292 }\r
293 /*-----------------------------------------------------------*/\r
294 \r
295 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
296 {\r
297         ( void ) pcTaskName;\r
298         ( void ) pxTask;\r
299 \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
307         for( ;; );\r
308 }\r
309 /*-----------------------------------------------------------*/\r
310 \r
311 void vApplicationIdleHook( void )\r
312 {\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
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 void vApplicationTickHook( void )\r
326 {\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
330 \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
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \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
348 {\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
353 \r
354         /* Initialise the timer/counter. */\r
355         xStatus = XTmrCtr_Initialize( &xTimer0Instance, TIMER_DEVICE_ID );\r
356 \r
357         if( xStatus == XST_SUCCESS )\r
358         {\r
359                 /* Install the tick interrupt handler as the timer ISR.\r
360                 *NOTE* The xPortInstallInterruptHandler() API function must be used for\r
361                 this purpose. */\r
362                 xStatus = xPortInstallInterruptHandler( TIMER_INTR_ID, vPortTickISR, NULL );\r
363         }\r
364 \r
365         if( xStatus == pdPASS )\r
366         {\r
367                 /* Enable the timer interrupt in the interrupt controller.\r
368                 *NOTE* The vPortEnableInterrupt() API function must be used for this\r
369                 purpose. */\r
370                 vPortEnableInterrupt( TIMER_INTR_ID );\r
371 \r
372                 /* Configure the timer interrupt handler. */\r
373                 XTmrCtr_SetHandler( &xTimer0Instance, ( void * ) vPortTickISR, NULL );\r
374 \r
375                 /* Set the correct period for the timer. */\r
376                 XTmrCtr_SetResetValue( &xTimer0Instance, ucTimerCounterNumber, ulCounterValue );\r
377 \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
383 \r
384                 /* Start the timer. */\r
385                 XTmrCtr_Start( &xTimer0Instance, ucTimerCounterNumber );\r
386         }\r
387 \r
388         /* Sanity check that the function executed as expected. */\r
389         configASSERT( ( xStatus == pdPASS ) );\r
390 }\r
391 /*-----------------------------------------------------------*/\r
392 \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
403 {\r
404 unsigned long ulCSR;\r
405 \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
409 }\r
410 /*-----------------------------------------------------------*/\r
411 \r