]> git.sur5r.net Git - freertos/blob - Demo/MicroBlaze_Spartan-6_EthernetLite/SDKProjects/RTOSDemoSource/main-blinky.c
Add LED output to the new MicroBlaze blinky demo - still a work in progress.
[freertos] / Demo / MicroBlaze_Spartan-6_EthernetLite / SDKProjects / RTOSDemoSource / main-blinky.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3 \r
4 \r
5         FreeRTOS supports many tools and architectures. V7.0.0 is sponsored by:\r
6         Atollic AB - Atollic provides professional embedded systems development\r
7         tools for C/C++ development, code analysis and test automation.\r
8         See http://www.atollic.com\r
9 \r
10 \r
11     ***************************************************************************\r
12      *                                                                       *\r
13      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
14      *    Complete, revised, and edited pdf reference manuals are also       *\r
15      *    available.                                                         *\r
16      *                                                                       *\r
17      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
18      *    ensuring you get running as quickly as possible and with an        *\r
19      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
20      *    the FreeRTOS project to continue with its mission of providing     *\r
21      *    professional grade, cross platform, de facto standard solutions    *\r
22      *    for microcontrollers - completely free of charge!                  *\r
23      *                                                                       *\r
24      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
25      *                                                                       *\r
26      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
27      *                                                                       *\r
28     ***************************************************************************\r
29 \r
30 \r
31     This file is part of the FreeRTOS distribution.\r
32 \r
33     FreeRTOS is free software; you can redistribute it and/or modify it under\r
34     the terms of the GNU General Public License (version 2) as published by the\r
35     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
36     >>>NOTE<<< The modification to the GPL is included to allow you to\r
37     distribute a combined work that includes FreeRTOS without being obliged to\r
38     provide the source code for proprietary components outside of the FreeRTOS\r
39     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
40     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
41     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
42     more details. You should have received a copy of the GNU General Public\r
43     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
44     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
45     by writing to Richard Barry, contact details for whom are available on the\r
46     FreeRTOS WEB site.\r
47 \r
48     1 tab == 4 spaces!\r
49 \r
50     http://www.FreeRTOS.org - Documentation, latest information, license and\r
51     contact details.\r
52 \r
53     http://www.SafeRTOS.com - A version that is certified for use in safety\r
54     critical systems.\r
55 \r
56     http://www.OpenRTOS.com - Commercial support, development, porting,\r
57     licensing and training services.\r
58 */\r
59 \r
60 /*\r
61  * main-blinky.c is included when the "Blinky" build configuration is used.\r
62  * main-full.c is included when the "Full" build configuration is used.\r
63  *\r
64  * main-blinky.c (this file) defines a very simple demo that creates two tasks,\r
65  * one queue, and one timer.  It also demonstrates how MicroBlaze interrupts\r
66  * can interact with FreeRTOS tasks/timers.\r
67  *\r
68  * This simple demo project runs on the Spartan-6 SP605 development board.\r
69  *\r
70  * The idle hook function:\r
71  * The idle hook function demonstrates how to query the amount of FreeRTOS heap\r
72  * space that is remaining (see vApplicationIdleHook() defined in this file).\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, toggles the green LED.  The 'block\r
91  * time' parameter passed to the queue receive function specifies that the task\r
92  * should be held in the Blocked state indefinitely to wait for data to be\r
93  * available on the queue.  The queue receive task will only leave the Blocked\r
94  * state when the queue send task writes to the queue.  As the queue send task\r
95  * writes to the queue every 200 milliseconds, the queue receive task leaves\r
96  * the Blocked state every 200 milliseconds, and therefore toggles the LED\r
97  * every 200 milliseconds.\r
98  *\r
99  * The LED Software Timer and the Button Interrupt:\r
100  * The user button SW1 is configured to generate an interrupt each time it is\r
101  * pressed.  The interrupt service routine switches an LED on, and resets the\r
102  * LED software timer.  The LED timer has a 5000 millisecond (5 second) period,\r
103  * and uses a callback function that is defined to just turn the LED off again.\r
104  * Therefore, pressing the user button will turn the LED on, and the LED will\r
105  * remain on until a full five seconds pass without the button being pressed.\r
106  */\r
107 \r
108 /* Kernel includes. */\r
109 #include "FreeRTOS.h"\r
110 #include "task.h"\r
111 #include "queue.h"\r
112 #include "timers.h"\r
113 \r
114 /* BSP includes. */\r
115 #include "xenv_standalone.h"\r
116 #include "xtmrctr.h"\r
117 #include "xil_exception.h"\r
118 #include "microblaze_exceptions_g.h"\r
119 #include "xgpio.h"\r
120 \r
121 /* Priorities at which the tasks are created. */\r
122 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
123 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
124 \r
125 /* The rate at which data is sent to the queue, specified in milliseconds, and\r
126 converted to ticks using the portTICK_RATE_MS constant. */\r
127 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_RATE_MS )\r
128 \r
129 /* The number of items the queue can hold.  This is 1 as the receive task\r
130 will remove items as they are added, meaning the send task should always find\r
131 the queue empty. */\r
132 #define mainQUEUE_LENGTH                                        ( 1 )\r
133 \r
134 /* The LED toggle by the queue receive task. */\r
135 #define mainTASK_CONTROLLED_LED                         0x01UL\r
136 \r
137 /* The LED turned on by the button interrupt, and turned off by the LED timer. */\r
138 #define mainTIMER_CONTROLLED_LED                        0x02UL\r
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 /*\r
143  * Setup the NVIC, LED outputs, and button inputs.\r
144  */\r
145 static void prvSetupHardware( void );\r
146 \r
147 /*\r
148  * The tasks as described in the comments at the top of this file.\r
149  */\r
150 static void prvQueueReceiveTask( void *pvParameters );\r
151 static void prvQueueSendTask( void *pvParameters );\r
152 \r
153 /*\r
154  * The LED timer callback function.  This does nothing but switch off the\r
155  * LED defined by the mainTIMER_CONTROLLED_LED constant.\r
156  */\r
157 static void vLEDTimerCallback( xTimerHandle xTimer );\r
158 \r
159 /*-----------------------------------------------------------*/\r
160 \r
161 /* The queue used by both tasks. */\r
162 static xQueueHandle xQueue = NULL;\r
163 \r
164 /* The LED software timer.  This uses vLEDTimerCallback() as its callback\r
165 function. */\r
166 static xTimerHandle xLEDTimer = NULL;\r
167 \r
168 /* Maintains the current LED output state. */\r
169 static volatile unsigned char ucGPIOState = 0U;\r
170 \r
171 /*-----------------------------------------------------------*/\r
172 \r
173 static XTmrCtr xTimer0Instance;\r
174 static XGpio xOutputGPIOInstance;\r
175 static const unsigned portBASE_TYPE uxGPIOOutputChannel = 1UL;\r
176 \r
177 /*-----------------------------------------------------------*/\r
178 #define NOT_JUST_TESTING\r
179 #ifdef JUST_TESTING\r
180 volatile unsigned long ul1 = 0, ul2 = 0;\r
181 \r
182 void vTemp1( void *pvParameters )\r
183 {\r
184         for( ;; )\r
185         {\r
186                 ul1++;\r
187                 //taskYIELD();\r
188         }\r
189 }\r
190 \r
191 void vTemp2( void *pvParameters )\r
192 {\r
193         for( ;; )\r
194         {\r
195                 ul2++;\r
196                 //taskYIELD();\r
197         }\r
198 }\r
199 \r
200 void main( void )\r
201 {\r
202         prvSetupHardware();\r
203 \r
204         XGpio_DiscreteWrite( &xOutputGPIOInstance, uxGPIOOutputChannel, 1 << 0 );\r
205         XGpio_DiscreteWrite( &xOutputGPIOInstance, uxGPIOOutputChannel, 1 << 1 );\r
206         XGpio_DiscreteWrite( &xOutputGPIOInstance, uxGPIOOutputChannel, 1 << 2 );\r
207         XGpio_DiscreteWrite( &xOutputGPIOInstance, uxGPIOOutputChannel, 1 << 3 );\r
208 \r
209 \r
210         xTaskCreate( vTemp1, ( signed char * ) "Test1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );\r
211         xTaskCreate( vTemp2, ( signed char * ) "Test2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL );\r
212 \r
213         vTaskStartScheduler();\r
214         for( ;; );\r
215 }\r
216 \r
217 \r
218 #else /* JUST_TESTING */\r
219 \r
220 int main(void)\r
221 {\r
222         /* Configure the interrupt controller, LED outputs and button inputs. */\r
223         prvSetupHardware();\r
224 \r
225         /* Create the queue. */\r
226         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
227 \r
228         if( xQueue != NULL )\r
229         {\r
230                 /* Start the two tasks as described in the comments at the top of this\r
231                 file. */\r
232                 xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
233                 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
234 \r
235                 /* Create the software timer that is responsible for turning off the LED\r
236                 if the button is not pushed within 5000ms, as described at the top of\r
237                 this file. */\r
238                 xLEDTimer = xTimerCreate(       ( const signed char * ) "LEDTimer", /* A text name, purely to help debugging. */\r
239                                                                         ( 5000 / portTICK_RATE_MS ),            /* The timer period, in this case 5000ms (5s). */\r
240                                                                         pdFALSE,                                                        /* This is a one shot timer, so xAutoReload is set to pdFALSE. */\r
241                                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
242                                                                         vLEDTimerCallback                                       /* The callback function that switches the LED off. */\r
243                                                                 );\r
244 \r
245                 /* Start the tasks and timer running. */\r
246                 vTaskStartScheduler();\r
247         }\r
248 \r
249         /* If all is well, the scheduler will now be running, and the following line\r
250         will never be reached.  If the following line does execute, then there was\r
251         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
252         to be created.  See the memory management section on the FreeRTOS web site\r
253         for more details. */\r
254         for( ;; );\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 #endif /* JUST_TESTING */\r
258 static void vLEDTimerCallback( xTimerHandle xTimer )\r
259 {\r
260         /* The timer has expired - so no button pushes have occurred in the last\r
261         five seconds - turn the LED off.  NOTE - accessing the LED port should use\r
262         a critical section because it is accessed from multiple tasks, and the\r
263         button interrupt - in this trivial case, for simplicity, the critical\r
264         section is omitted. */\r
265         ucGPIOState |= mainTIMER_CONTROLLED_LED;\r
266         XGpio_DiscreteWrite( &xOutputGPIOInstance, uxGPIOOutputChannel, ucGPIOState );\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 /* The ISR executed when the user button is pushed. */\r
271 void GPIO8_IRQHandler( void )\r
272 {\r
273 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
274 \r
275         /* The button was pushed, so ensure the LED is on before resetting the\r
276         LED timer.  The LED timer will turn the LED off if the button is not\r
277         pushed within 5000ms. */\r
278         ucGPIOState &= ~mainTIMER_CONTROLLED_LED;\r
279         XGpio_DiscreteWrite( &xOutputGPIOInstance, uxGPIOOutputChannel, ucGPIOState );\r
280 \r
281         /* This interrupt safe FreeRTOS function can be called from this interrupt\r
282         because the interrupt priority is below the\r
283         configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
284         xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );\r
285 \r
286         /* Clear the interrupt before leaving. */\r
287 //_RB_    MSS_GPIO_clear_irq( MSS_GPIO_8 );\r
288 \r
289         /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
290         service/daemon task) to unblock, and the unblocked task has a priority\r
291         higher than or equal to the task that was interrupted, then\r
292         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
293         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
294         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
295 }\r
296 /*-----------------------------------------------------------*/\r
297 \r
298 static void prvQueueSendTask( void *pvParameters )\r
299 {\r
300 portTickType xNextWakeTime;\r
301 const unsigned long ulValueToSend = 100UL;\r
302 \r
303         /* Initialise xNextWakeTime - this only needs to be done once. */\r
304         xNextWakeTime = xTaskGetTickCount();\r
305 \r
306         for( ;; )\r
307         {\r
308                 /* Place this task in the blocked state until it is time to run again.\r
309                 The block time is specified in ticks, the constant used converts ticks\r
310                 to ms.  While in the Blocked state this task will not consume any CPU\r
311                 time. */\r
312                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
313 \r
314                 /* Send to the queue - causing the queue receive task to unblock and\r
315                 toggle an LED.  0 is used as the block time so the sending operation\r
316                 will not block - it shouldn't need to block as the queue should always\r
317                 be empty at this point in the code. */\r
318                 xQueueSend( xQueue, &ulValueToSend, 0 );\r
319         }\r
320 }\r
321 /*-----------------------------------------------------------*/\r
322 \r
323 static void prvQueueReceiveTask( void *pvParameters )\r
324 {\r
325 unsigned long ulReceivedValue;\r
326 \r
327         for( ;; )\r
328         {\r
329                 /* Wait until something arrives in the queue - this task will block\r
330                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
331                 FreeRTOSConfig.h. */\r
332                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
333 \r
334                 /*  To get here something must have been received from the queue, but\r
335                 is it the expected value?  If it is, toggle the green LED. */\r
336                 if( ulReceivedValue == 100UL )\r
337                 {\r
338                         /* NOTE - accessing the LED port should use a critical section\r
339                         because it is accessed from multiple tasks, and the button interrupt\r
340                         - in this trivial case, for simplicity, the critical section is\r
341                         omitted. */\r
342                         if( ( ucGPIOState & mainTASK_CONTROLLED_LED ) != 0 )\r
343                         {\r
344                                 ucGPIOState &= ~mainTASK_CONTROLLED_LED;\r
345                         }\r
346                         else\r
347                         {\r
348                                 ucGPIOState |= mainTASK_CONTROLLED_LED;\r
349                         }\r
350 \r
351                         XGpio_DiscreteWrite( &xOutputGPIOInstance, uxGPIOOutputChannel, ucGPIOState );\r
352                 }\r
353         }\r
354 }\r
355 /*-----------------------------------------------------------*/\r
356 \r
357 static void prvSetupHardware( void )\r
358 {\r
359 portBASE_TYPE xStatus;\r
360 const unsigned char ucSetToOutput = 0U;\r
361 \r
362         /* Initialize the GPIO. */\r
363         xStatus = XGpio_Initialize( &xOutputGPIOInstance, XPAR_LEDS_4BITS_DEVICE_ID );\r
364         if( xStatus == XST_SUCCESS )\r
365         {\r
366                 /* All LEDs on this channel are going to be outputs. */\r
367                 XGpio_SetDataDirection( &xOutputGPIOInstance, uxGPIOOutputChannel, ucSetToOutput );\r
368 \r
369                 /* Start with all LEDs off. */\r
370                 ucGPIOState = 0U;\r
371                 XGpio_DiscreteWrite( &xOutputGPIOInstance, uxGPIOOutputChannel, ucGPIOState );\r
372         }\r
373 \r
374         configASSERT( ( xStatus == XST_SUCCESS ) );\r
375 \r
376         #ifdef MICROBLAZE_EXCEPTIONS_ENABLED\r
377                 microblaze_enable_exceptions();\r
378         #endif\r
379 }\r
380 /*-----------------------------------------------------------*/\r
381 \r
382 void vApplicationMallocFailedHook( void )\r
383 {\r
384         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
385         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
386         internally by FreeRTOS API functions that create tasks, queues, software\r
387         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
388         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
389         for( ;; );\r
390 }\r
391 /*-----------------------------------------------------------*/\r
392 \r
393 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
394 {\r
395         ( void ) pcTaskName;\r
396         ( void ) pxTask;\r
397 \r
398         /* Run time stack overflow checking is performed if\r
399         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
400         function is called if a stack overflow is detected. */\r
401         for( ;; );\r
402 }\r
403 /*-----------------------------------------------------------*/\r
404 \r
405 void vApplicationIdleHook( void )\r
406 {\r
407 volatile size_t xFreeHeapSpace;\r
408 \r
409         /* This function is called on each cycle of the idle task.  In this case it\r
410         does nothing useful, other than report the amout of FreeRTOS heap that\r
411         remains unallocated. */\r
412         xFreeHeapSpace = xPortGetFreeHeapSize();\r
413 \r
414         if( xFreeHeapSpace > 100 )\r
415         {\r
416                 /* By now, the kernel has allocated everything it is going to, so\r
417                 if there is a lot of heap remaining unallocated then\r
418                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
419                 reduced accordingly. */\r
420         }\r
421 }\r
422 /*-----------------------------------------------------------*/\r
423 \r
424 void vMainConfigureTimerForRunTimeStats( void )\r
425 {\r
426         /* This function is not used by the Blinky build configuration, but needs\r
427         to be defined as the Blinky and Full build configurations share a\r
428         FreeRTOSConfig.h header file. */\r
429 }\r
430 /*-----------------------------------------------------------*/\r
431 \r
432 unsigned long ulGetRunTimeCounterValue( void )\r
433 {\r
434         /* This function is not used by the Blinky build configuration, but needs\r
435         to be defined as the Blinky and Full build configurations share a\r
436         FreeRTOSConfig.h header file. */\r
437         return 0UL;\r
438 }\r
439 /*-----------------------------------------------------------*/\r
440 \r
441 void vApplicationSetupTimerInterrupt( void )\r
442 {\r
443 portBASE_TYPE xStatus;\r
444 const unsigned char ucTimerCounterNumber = ( unsigned char ) 0U;\r
445 const unsigned long ulCounterValue = ( ( XPAR_AXI_TIMER_0_CLOCK_FREQ_HZ / configTICK_RATE_HZ ) - 1UL );\r
446 extern void vTickISR( void *pvUnused );\r
447 \r
448         /* Initialise the timer/counter. */\r
449         xStatus = XTmrCtr_Initialize( &xTimer0Instance, XPAR_AXI_TIMER_0_DEVICE_ID );\r
450 \r
451         if( xStatus == XST_SUCCESS )\r
452         {\r
453                 /* Install the tick interrupt handler as the timer ISR. */\r
454                 xStatus = xPortInstallInterruptHandler( XPAR_MICROBLAZE_0_INTC_AXI_TIMER_0_INTERRUPT_INTR, vTickISR, NULL );\r
455         }\r
456 \r
457         if( xStatus == pdPASS )\r
458         {\r
459                 vPortEnableInterrupt( XPAR_MICROBLAZE_0_INTC_AXI_TIMER_0_INTERRUPT_INTR );\r
460 \r
461                 /* Configure the timer interrupt handler. */\r
462                 XTmrCtr_SetHandler( &xTimer0Instance, ( void * ) vTickISR, NULL );\r
463 \r
464                 /* Set the correct period for the timer. */\r
465                 XTmrCtr_SetResetValue( &xTimer0Instance, ucTimerCounterNumber, ulCounterValue );\r
466 \r
467                 /* Enable the interrupts.  Auto-reload mode is used to generate a\r
468                 periodic tick.  Note that interrupts are disabled when this function is\r
469                 called, so interrupts will not start to be processed until the first\r
470                 task has started to run. */\r
471                 XTmrCtr_SetOptions( &xTimer0Instance, ucTimerCounterNumber, ( XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION | XTC_DOWN_COUNT_OPTION ) );\r
472 \r
473                 /* Start the timer. */\r
474                 XTmrCtr_Start( &xTimer0Instance, ucTimerCounterNumber );\r
475         }\r
476 \r
477         configASSERT( ( xStatus == pdPASS ) );\r
478 }\r
479 /*-----------------------------------------------------------*/\r
480 \r
481 void vApplicationClearTimerInterrupt( void )\r
482 {\r
483 unsigned long ulCSR;\r
484 \r
485         /* Increment the RTOS tick - this might cause a task to unblock. */\r
486         vTaskIncrementTick();\r
487 \r
488         /* Clear the timer interrupt */\r
489         ulCSR = XTmrCtr_GetControlStatusReg( XPAR_AXI_TIMER_0_BASEADDR, 0 );\r
490         XTmrCtr_SetControlStatusReg( XPAR_AXI_TIMER_0_BASEADDR, 0, ulCSR );\r
491 }\r
492 \r