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