]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MicroBlaze_Spartan-6_EthernetLite/KernelAwareBSPRepository/sw_apps/FreeRTOS_Hello_World/src/FreeRTOS-main.c
Update version numbers to V7.4.1.
[freertos] / FreeRTOS / Demo / MicroBlaze_Spartan-6_EthernetLite / KernelAwareBSPRepository / sw_apps / FreeRTOS_Hello_World / src / FreeRTOS-main.c
1 /*\r
2     FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * FreeRTOS-main.c (this file) defines a very simple demo that creates two tasks,\r
77  * one queue, and one timer.\r
78  *\r
79  * The main() Function:\r
80  * main() creates one software timer, one queue, and two tasks.  It then starts\r
81  * the scheduler.\r
82  *\r
83  * The Queue Send Task:\r
84  * The queue send task is implemented by the prvQueueSendTask() function in\r
85  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
86  * block for 200 milliseconds, before sending the value 100 to the queue that\r
87  * was created within main().  Once the value is sent, the task loops back\r
88  * around to block for another 200 milliseconds.\r
89  *\r
90  * The Queue Receive Task:\r
91  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
92  * in this file.  prvQueueReceiveTask() sits in a loop that causes it to\r
93  * repeatedly attempt to read data from the queue that was created within\r
94  * main().  When data is received, the task checks the value of the data, and\r
95  * if the value equals the expected 100, increments the ulRecieved variable.\r
96  * The 'block time' parameter passed to the queue receive function specifies\r
97  * that the task should be held in the Blocked state indefinitely to wait for\r
98  * data to be available on the queue.  The queue receive task will only leave\r
99  * the Blocked state when the queue send task writes to the queue.  As the queue\r
100  * send task writes to the queue every 200 milliseconds, the queue receive task\r
101  * leaves the Blocked state every 200 milliseconds, and therefore toggles the LED\r
102  * every 200 milliseconds.\r
103  *\r
104  * The Software Timer:\r
105  * The software timer is configured to be an "auto reset" timer.  Its callback\r
106  * function simply increments the ulCallback variable each time it executes.\r
107  */\r
108 \r
109 /* Kernel includes. */\r
110 #include "FreeRTOS.h"\r
111 #include "task.h"\r
112 #include "queue.h"\r
113 #include "timers.h"\r
114 \r
115 /* BSP includes. */\r
116 #include "xtmrctr.h"\r
117 \r
118 /* Priorities at which the tasks are created. */\r
119 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
120 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
121 \r
122 /* The rate at which data is sent to the queue, specified in milliseconds, and\r
123 converted to ticks using the portTICK_RATE_MS constant. */\r
124 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_RATE_MS )\r
125 \r
126 /* The number of items the queue can hold.  This is 1 as the receive task\r
127 will remove items as they are added because it has the higher priority, meaning\r
128 the send task should always find the queue empty. */\r
129 #define mainQUEUE_LENGTH                                        ( 1 )\r
130 \r
131 /* A block time of 0 simply means, "don't block". */\r
132 #define mainDONT_BLOCK                                          ( portTickType ) 0\r
133 \r
134 /* The following constants describe the timer instance used in this application.\r
135 They are defined here such that a user can easily change all the needed parameters\r
136 in one place. */\r
137 #define TIMER_DEVICE_ID                                         XPAR_TMRCTR_0_DEVICE_ID\r
138 #define TIMER_FREQ_HZ                                           XPAR_TMRCTR_0_CLOCK_FREQ_HZ\r
139 #define TIMER_INTR_ID                                           XPAR_INTC_0_TMRCTR_0_VEC_ID\r
140 \r
141 /*-----------------------------------------------------------*/\r
142 \r
143 /*\r
144  * The tasks as described in the comments at the top of this file.\r
145  */\r
146 static void prvQueueReceiveTask( void *pvParameters );\r
147 static void prvQueueSendTask( void *pvParameters );\r
148 \r
149 /*\r
150  * The LED timer callback function.  This does nothing but increment the\r
151  * ulCallback variable each time it executes.\r
152  */\r
153 static void vSoftwareTimerCallback( xTimerHandle xTimer );\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 \r
157 /* The queue used by the queue send and queue receive tasks. */\r
158 static xQueueHandle xQueue = NULL;\r
159 \r
160 /* The LED software timer.  This uses vSoftwareTimerCallback() as its callback\r
161 function. */\r
162 static xTimerHandle xExampleSoftwareTimer = NULL;\r
163 \r
164 /*-----------------------------------------------------------*/\r
165 \r
166 /* Structures that hold the state of the various peripherals used by this demo.\r
167 These are used by the Xilinx peripheral driver API functions. */\r
168 static XTmrCtr xTimer0Instance;\r
169 \r
170 /* The variable that is incremented each time the receive task receives the\r
171 value 100. */\r
172 static unsigned long ulReceived = 0UL;\r
173 \r
174 /* The variable that is incremented each time the software time callback function\r
175 executes. */\r
176 static unsigned long ulCallback = 0UL;\r
177 \r
178 /*-----------------------------------------------------------*/\r
179 \r
180 int main( void )\r
181 {\r
182         /***************************************************************************\r
183         See http://www.FreeRTOS.org for full information on FreeRTOS, including\r
184         an API reference, pdf API reference manuals, and FreeRTOS tutorial books.\r
185 \r
186         See http://www.freertos.org/Free-RTOS-for-Xilinx-MicroBlaze-on-Spartan-6-FPGA.html\r
187         for comprehensive standalone FreeRTOS for MicroBlaze demos.\r
188         ***************************************************************************/\r
189 \r
190         /* Create the queue used by the queue send and queue receive tasks as\r
191         described in the comments at the top of this file. */\r
192         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
193 \r
194         /* Sanity check that the queue was created. */\r
195         configASSERT( xQueue );\r
196 \r
197         /* Start the two tasks as described in the comments at the top of this\r
198         file. */\r
199         xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
200         xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
201 \r
202         /* Create the software timer */\r
203         xExampleSoftwareTimer = xTimerCreate(   ( const signed char * ) "SoftwareTimer", /* A text name, purely to help debugging. */\r
204                                                                                         ( 5000 / portTICK_RATE_MS ),            /* The timer period, in this case 5000ms (5s). */\r
205                                                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
206                                                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
207                                                                                         vSoftwareTimerCallback                          /* The callback function that switches the LED off. */\r
208                                                                                 );\r
209 \r
210         /* Start the software timer. */\r
211         xTimerStart( xExampleSoftwareTimer, mainDONT_BLOCK );\r
212 \r
213         /* Start the tasks and timer running. */\r
214         vTaskStartScheduler();\r
215 \r
216         /* If all is well, the scheduler will now be running, and the following line\r
217         will never be reached.  If the following line does execute, then there was\r
218         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
219         to be created.  See the memory management section on the FreeRTOS web site\r
220         for more details. */\r
221         for( ;; );\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 /* The callback is executed when the software timer expires. */\r
226 static void vSoftwareTimerCallback( xTimerHandle xTimer )\r
227 {\r
228         /* Just increment the ulCallbac variable. */\r
229         ulCallback++;\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 static void prvQueueSendTask( void *pvParameters )\r
234 {\r
235 portTickType xNextWakeTime;\r
236 const unsigned long ulValueToSend = 100UL;\r
237 \r
238         /* Initialise xNextWakeTime - this only needs to be done once. */\r
239         xNextWakeTime = xTaskGetTickCount();\r
240 \r
241         for( ;; )\r
242         {\r
243                 /* Place this task in the blocked state until it is time to run again.\r
244                 The block time is specified in ticks, the constant used converts ticks\r
245                 to ms.  While in the Blocked state this task will not consume any CPU\r
246                 time. */\r
247                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
248 \r
249                 /* Send to the queue - causing the queue receive task to unblock and\r
250                 toggle an LED.  0 is used as the block time so the sending operation\r
251                 will not block - it shouldn't need to block as the queue should always\r
252                 be empty at this point in the code. */\r
253                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );\r
254         }\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 static void prvQueueReceiveTask( void *pvParameters )\r
259 {\r
260 unsigned long ulReceivedValue;\r
261 \r
262         for( ;; )\r
263         {\r
264                 /* Wait until something arrives in the queue - this task will block\r
265                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
266                 FreeRTOSConfig.h. */\r
267                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
268 \r
269                 /*  To get here something must have been received from the queue, but\r
270                 is it the expected value?  If it is, increment the ulReceived variable. */\r
271                 if( ulReceivedValue == 100UL )\r
272                 {\r
273                         ulReceived++;\r
274                 }\r
275         }\r
276 }\r
277 /*-----------------------------------------------------------*/\r
278 \r
279 void vApplicationMallocFailedHook( void )\r
280 {\r
281         /* vApplicationMallocFailedHook() will only be called if\r
282         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
283         function that will get called if a call to pvPortMalloc() fails.\r
284         pvPortMalloc() is called internally by the kernel whenever a task, queue or\r
285         semaphore is created.  It is also called by various parts of the demo\r
286         application.  If heap_1.c or heap_2.c are used, then the size of the heap\r
287         available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
288         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
289         to query the size of free heap space that remains (although it does not\r
290         provide information on how the remaining heap might be fragmented). */\r
291         taskDISABLE_INTERRUPTS();\r
292         for( ;; );\r
293 }\r
294 /*-----------------------------------------------------------*/\r
295 \r
296 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
297 {\r
298         ( void ) pcTaskName;\r
299         ( void ) pxTask;\r
300 \r
301         /* vApplicationStackOverflowHook() will only be called if\r
302         configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2.  The handle and name\r
303         of the offending task will be passed into the hook function via its\r
304         parameters.  However, when a stack has overflowed, it is possible that the\r
305         parameters will have been corrupted, in which case the pxCurrentTCB variable\r
306         can be inspected directly. */\r
307         taskDISABLE_INTERRUPTS();\r
308         for( ;; );\r
309 }\r
310 /*-----------------------------------------------------------*/\r
311 \r
312 void vApplicationIdleHook( void )\r
313 {\r
314         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
315         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
316         task.  It is essential that code added to this hook function never attempts\r
317         to block in any way (for example, call xQueueReceive() with a block time\r
318         specified, or call vTaskDelay()).  If the application makes use of the\r
319         vTaskDelete() API function (as this demo application does) then it is also\r
320         important that vApplicationIdleHook() is permitted to return to its calling\r
321         function, because it is the responsibility of the idle task to clean up\r
322         memory allocated by the kernel to any task that has since been deleted. */\r
323 }\r
324 /*-----------------------------------------------------------*/\r
325 \r
326 void vApplicationTickHook( void )\r
327 {\r
328         /* vApplicationTickHook() will only be called if configUSE_TICK_HOOK is set\r
329         to 1 in FreeRTOSConfig.h.  It executes from an interrupt context so must\r
330         not use any FreeRTOS API functions that do not end in ...FromISR().\r
331 \r
332         This simple blinky demo does not use the tick hook, but a tick hook is\r
333         required to be defined as the blinky and full demos share a\r
334         FreeRTOSConfig.h header file. */\r
335 }\r
336 /*-----------------------------------------------------------*/\r
337 \r
338 /* This is an application defined callback function used to install the tick\r
339 interrupt handler.  It is provided as an application callback because the kernel\r
340 will run on lots of different MicroBlaze and FPGA configurations - there could\r
341 be multiple timer instances in the hardware platform and the users can chose to\r
342 use any one of them. This example uses Timer 0. If that is available in  your\r
343 hardware platform then this example callback implementation should not require\r
344 modification. The definitions for the timer instance used are at the top of this\r
345 file so that users can change them at one place based on the timer instance they\r
346 use. The name of the interrupt handler that should be installed is vPortTickISR(),\r
347 which the function below declares as an extern. */\r
348 void vApplicationSetupTimerInterrupt( void )\r
349 {\r
350 portBASE_TYPE xStatus;\r
351 const unsigned char ucTimerCounterNumber = ( unsigned char ) 0U;\r
352 const unsigned long ulCounterValue = ( ( TIMER_FREQ_HZ / configTICK_RATE_HZ ) - 1UL );\r
353 extern void vPortTickISR( void *pvUnused );\r
354 \r
355         /* Initialise the timer/counter. */\r
356         xStatus = XTmrCtr_Initialize( &xTimer0Instance, TIMER_DEVICE_ID );\r
357 \r
358         if( xStatus == XST_SUCCESS )\r
359         {\r
360                 /* Install the tick interrupt handler as the timer ISR.\r
361                 *NOTE* The xPortInstallInterruptHandler() API function must be used for\r
362                 this purpose. */\r
363                 xStatus = xPortInstallInterruptHandler( TIMER_INTR_ID, vPortTickISR, NULL );\r
364         }\r
365 \r
366         if( xStatus == pdPASS )\r
367         {\r
368                 /* Enable the timer interrupt in the interrupt controller.\r
369                 *NOTE* The vPortEnableInterrupt() API function must be used for this\r
370                 purpose. */\r
371                 vPortEnableInterrupt( TIMER_INTR_ID );\r
372 \r
373                 /* Configure the timer interrupt handler. */\r
374                 XTmrCtr_SetHandler( &xTimer0Instance, ( void * ) vPortTickISR, NULL );\r
375 \r
376                 /* Set the correct period for the timer. */\r
377                 XTmrCtr_SetResetValue( &xTimer0Instance, ucTimerCounterNumber, ulCounterValue );\r
378 \r
379                 /* Enable the interrupts.  Auto-reload mode is used to generate a\r
380                 periodic tick.  Note that interrupts are disabled when this function is\r
381                 called, so interrupts will not start to be processed until the first\r
382                 task has started to run. */\r
383                 XTmrCtr_SetOptions( &xTimer0Instance, ucTimerCounterNumber, ( XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION | XTC_DOWN_COUNT_OPTION ) );\r
384 \r
385                 /* Start the timer. */\r
386                 XTmrCtr_Start( &xTimer0Instance, ucTimerCounterNumber );\r
387         }\r
388 \r
389         /* Sanity check that the function executed as expected. */\r
390         configASSERT( ( xStatus == pdPASS ) );\r
391 }\r
392 /*-----------------------------------------------------------*/\r
393 \r
394 /* This is an application defined callback function used to clear whichever\r
395 interrupt was installed by the the vApplicationSetupTimerInterrupt() callback\r
396 function - in this case the interrupt generated by the AXI timer.  It is\r
397 provided as an application callback because the kernel will run on lots of\r
398 different MicroBlaze and FPGA configurations - not all of which will have the\r
399 same timer peripherals defined or available.  This example uses the AXI Timer 0.\r
400 If that is available on your hardware platform then this example callback\r
401 implementation should not require modification provided the example definition\r
402 of vApplicationSetupTimerInterrupt() is also not modified. */\r
403 void vApplicationClearTimerInterrupt( void )\r
404 {\r
405 unsigned long ulCSR;\r
406 \r
407         /* Clear the timer interrupt */\r
408         ulCSR = XTmrCtr_GetControlStatusReg( XPAR_TMRCTR_0_BASEADDR, 0 );\r
409         XTmrCtr_SetControlStatusReg( XPAR_TMRCTR_0_BASEADDR, 0, ulCSR );\r
410 }\r
411 /*-----------------------------------------------------------*/\r
412 \r