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