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