]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_A2F200_SoftConsole/main-blinky.c
7cd37fca6d3e75fb1a0262c219e0634dcf364361
[freertos] / Demo / CORTEX_A2F200_SoftConsole / main-blinky.c
1 /*\r
2     FreeRTOS V7.0.0 - 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 Cortex-M3 interrupts can\r
66  * interact with FreeRTOS tasks/timers.\r
67  *\r
68  * This simple demo project runs on the SmartFusion A2F-EVAL-KIT evaluation\r
69  * board, which is populated with an A2F200M3F SmartFusion mixed signal FPGA.\r
70  * The A2F200M3F incorporates a Cortex-M3 microcontroller.\r
71  *\r
72  * The idle hook function:\r
73  * The idle hook function demonstrates how to query the amount of FreeRTOS heap\r
74  * space that is remaining (see vApplicationIdleHook() defined in this file).\r
75  *\r
76  * The main() Function:\r
77  * main() creates one software timer, one queue, and two tasks.  It then starts\r
78  * the scheduler.\r
79  *\r
80  * The Queue Send Task:\r
81  * The queue send task is implemented by the prvQueueSendTask() function in\r
82  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
83  * block for 200 milliseconds, before sending the value 100 to the queue that\r
84  * was created within main().  Once the value is sent, the task loops back\r
85  * around to block for another 200 milliseconds.\r
86  *\r
87  * The Queue Receive Task:\r
88  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
89  * in this file.  prvQueueReceiveTask() sits in a loop that causes it to\r
90  * repeatedly attempt to read data from the queue that was created within\r
91  * main().  When data is received, the task checks the value of the data, and\r
92  * if the value equals the expected 100, toggles the green LED.  The 'block\r
93  * time' parameter passed to the queue receive function specifies that the task\r
94  * should be held in the Blocked state indefinitely to wait for data to be\r
95  * available on the queue.  The queue receive task will only leave the Blocked\r
96  * state when the queue send task writes to the queue.  As the queue send task\r
97  * writes to the queue every 200 milliseconds, the queue receive task leaves\r
98  * the Blocked state every 200 milliseconds, and therefore toggles the LED\r
99  * every 200 milliseconds.\r
100  *\r
101  * The LED Software Timer and the Button Interrupt:\r
102  * The user button SW1 is configured to generate an interrupt each time it is\r
103  * pressed.  The interrupt service routine switches an LED on, and resets the\r
104  * LED software timer.  The LED timer has a 5000 millisecond (5 second) period,\r
105  * and uses a callback function that is defined to just turn the LED off again.\r
106  * Therefore, pressing the user button will turn the LED on, and the LED will\r
107  * remain on until a full five seconds pass without the button being pressed.\r
108  */\r
109 \r
110 /* Kernel includes. */\r
111 #include "FreeRTOS.h"\r
112 #include "task.h"\r
113 #include "queue.h"\r
114 #include "timers.h"\r
115 \r
116 /* Microsemi drivers/libraries. */\r
117 #include "mss_gpio.h"\r
118 #include "mss_watchdog.h"\r
119 \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 long ulGPIOState = 0UL;\r
170 \r
171 /*-----------------------------------------------------------*/\r
172 \r
173 int main(void)\r
174 {\r
175         /* Configure the NVIC, LED outputs and button inputs. */\r
176         prvSetupHardware();\r
177 \r
178         /* Create the queue. */\r
179         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
180 \r
181         if( xQueue != NULL )\r
182         {\r
183                 /* Start the two tasks as described in the comments at the top of this\r
184                 file. */\r
185                 xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
186                 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
187 \r
188                 /* Create the software timer that is responsible for turning off the LED\r
189                 if the button is not pushed within 5000ms, as described at the top of\r
190                 this file. */\r
191                 xLEDTimer = xTimerCreate(       ( const signed char * ) "LEDTimer", /* A text name, purely to help debugging. */\r
192                                                                         ( 5000 / portTICK_RATE_MS ),            /* The timer period, in this case 5000ms (5s). */\r
193                                                                         pdFALSE,                                                        /* This is a one shot timer, so xAutoReload is set to pdFALSE. */\r
194                                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
195                                                                         vLEDTimerCallback                                       /* The callback function that switches the LED off. */\r
196                                                                 );\r
197 \r
198                 /* Start the tasks and timer running. */\r
199                 vTaskStartScheduler();\r
200         }\r
201 \r
202         /* If all is well, the scheduler will now be running, and the following line\r
203         will never be reached.  If the following line does execute, then there was\r
204         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
205         to be created.  See the memory management section on the FreeRTOS web site\r
206         for more details. */\r
207         for( ;; );\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 static void vLEDTimerCallback( xTimerHandle xTimer )\r
212 {\r
213         /* The timer has expired - so no button pushes have occurred in the last\r
214         five seconds - turn the LED off.  NOTE - accessing the LED port should use\r
215         a critical section because it is accessed from multiple tasks, and the\r
216         button interrupt - in this trivial case, for simplicity, the critical\r
217         section is omitted. */\r
218         ulGPIOState |= mainTIMER_CONTROLLED_LED;\r
219         MSS_GPIO_set_outputs( ulGPIOState );\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 /* The ISR executed when the user button is pushed. */\r
224 void GPIO8_IRQHandler( void )\r
225 {\r
226 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
227 \r
228         /* The button was pushed, so ensure the LED is on before resetting the\r
229         LED timer.  The LED timer will turn the LED off if the button is not\r
230         pushed within 5000ms. */\r
231         ulGPIOState &= ~mainTIMER_CONTROLLED_LED;\r
232         MSS_GPIO_set_outputs( ulGPIOState );\r
233 \r
234         /* This interrupt safe FreeRTOS function can be called from this interrupt\r
235         because the interrupt priority is below the\r
236         configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
237         xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );\r
238 \r
239         /* Clear the interrupt before leaving. */\r
240     MSS_GPIO_clear_irq( MSS_GPIO_8 );\r
241 \r
242         /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
243         service/daemon task) to unblock, and the unblocked task has a priority\r
244         higher than or equal to the task that was interrupted, then\r
245         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
246         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
247         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 static void prvQueueSendTask( void *pvParameters )\r
252 {\r
253 portTickType xNextWakeTime;\r
254 const unsigned long ulValueToSend = 100UL;\r
255 \r
256         /* Initialise xNextWakeTime - this only needs to be done once. */\r
257         xNextWakeTime = xTaskGetTickCount();\r
258 \r
259         for( ;; )\r
260         {\r
261                 /* Place this task in the blocked state until it is time to run again.\r
262                 The block time is specified in ticks, the constant used converts ticks\r
263                 to ms.  While in the Blocked state this task will not consume any CPU\r
264                 time. */\r
265                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
266 \r
267                 /* Send to the queue - causing the queue receive task to unblock and\r
268                 toggle an LED.  0 is used as the block time so the sending operation\r
269                 will not block - it shouldn't need to block as the queue should always\r
270                 be empty at this point in the code. */\r
271                 xQueueSend( xQueue, &ulValueToSend, 0 );\r
272         }\r
273 }\r
274 /*-----------------------------------------------------------*/\r
275 \r
276 static void prvQueueReceiveTask( void *pvParameters )\r
277 {\r
278 unsigned long ulReceivedValue;\r
279 \r
280         for( ;; )\r
281         {\r
282                 /* Wait until something arrives in the queue - this task will block\r
283                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
284                 FreeRTOSConfig.h. */\r
285                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
286 \r
287                 /*  To get here something must have been received from the queue, but\r
288                 is it the expected value?  If it is, toggle the green LED. */\r
289                 if( ulReceivedValue == 100UL )\r
290                 {\r
291                         /* NOTE - accessing the LED port should use a critical section\r
292                         because it is accessed from multiple tasks, and the button interrupt\r
293                         - in this trivial case, for simplicity, the critical section is\r
294                         omitted. */\r
295                         if( ( ulGPIOState & mainTASK_CONTROLLED_LED ) != 0 )\r
296                         {\r
297                                 ulGPIOState &= ~mainTASK_CONTROLLED_LED;\r
298                         }\r
299                         else\r
300                         {\r
301                                 ulGPIOState |= mainTASK_CONTROLLED_LED;\r
302                         }\r
303                         MSS_GPIO_set_outputs( ulGPIOState );\r
304                 }\r
305         }\r
306 }\r
307 /*-----------------------------------------------------------*/\r
308 \r
309 static void prvSetupHardware( void )\r
310 {\r
311         SystemCoreClockUpdate();\r
312 \r
313         /* Disable the Watch Dog Timer */\r
314         MSS_WD_disable( );\r
315 \r
316         /* Initialise the GPIO */\r
317         MSS_GPIO_init();\r
318 \r
319         /* Set up GPIO for the LEDs. */\r
320     MSS_GPIO_config( MSS_GPIO_0 , MSS_GPIO_OUTPUT_MODE );\r
321     MSS_GPIO_config( MSS_GPIO_1 , MSS_GPIO_OUTPUT_MODE );\r
322     MSS_GPIO_config( MSS_GPIO_2 , MSS_GPIO_OUTPUT_MODE );\r
323     MSS_GPIO_config( MSS_GPIO_3 , MSS_GPIO_OUTPUT_MODE );\r
324     MSS_GPIO_config( MSS_GPIO_4 , MSS_GPIO_OUTPUT_MODE );\r
325     MSS_GPIO_config( MSS_GPIO_5 , MSS_GPIO_OUTPUT_MODE );\r
326     MSS_GPIO_config( MSS_GPIO_6 , MSS_GPIO_OUTPUT_MODE );\r
327     MSS_GPIO_config( MSS_GPIO_7 , MSS_GPIO_OUTPUT_MODE );\r
328 \r
329     /* All LEDs start off. */\r
330     ulGPIOState = 0xffffffffUL;\r
331     MSS_GPIO_set_outputs( ulGPIOState );\r
332 \r
333         /* Setup the GPIO and the NVIC for the switch used in this simple demo. */\r
334         NVIC_SetPriority( GPIO8_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
335     NVIC_EnableIRQ( GPIO8_IRQn );\r
336     MSS_GPIO_config( MSS_GPIO_8, MSS_GPIO_INPUT_MODE | MSS_GPIO_IRQ_EDGE_NEGATIVE );\r
337     MSS_GPIO_enable_irq( MSS_GPIO_8 );\r
338 }\r
339 /*-----------------------------------------------------------*/\r
340 \r
341 void vApplicationMallocFailedHook( void )\r
342 {\r
343         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
344         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
345         internally by FreeRTOS API functions that create tasks, queues, software\r
346         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
347         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
348         for( ;; );\r
349 }\r
350 /*-----------------------------------------------------------*/\r
351 \r
352 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
353 {\r
354         ( void ) pcTaskName;\r
355         ( void ) pxTask;\r
356 \r
357         /* Run time stack overflow checking is performed if\r
358         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
359         function is called if a stack overflow is detected. */\r
360         for( ;; );\r
361 }\r
362 /*-----------------------------------------------------------*/\r
363 \r
364 void vApplicationIdleHook( void )\r
365 {\r
366 volatile size_t xFreeHeapSpace;\r
367 \r
368         /* This function is called on each cycle of the idle task.  In this case it\r
369         does nothing useful, other than report the amout of FreeRTOS heap that\r
370         remains unallocated. */\r
371         xFreeHeapSpace = xPortGetFreeHeapSize();\r
372 \r
373         if( xFreeHeapSpace > 100 )\r
374         {\r
375                 /* By now, the kernel has allocated everything it is going to, so\r
376                 if there is a lot of heap remaining unallocated then\r
377                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
378                 reduced accordingly. */\r
379         }\r
380 }\r
381 /*-----------------------------------------------------------*/\r
382 \r
383 void vMainConfigureTimerForRunTimeStats( void )\r
384 {\r
385         /* This function is not used by the Blinky build configuration, but needs\r
386         to be defined as the Blinky and Full build configurations share a\r
387         FreeRTOSConfig.h header file. */\r
388 }\r
389 /*-----------------------------------------------------------*/\r
390 \r
391 unsigned long ulGetRunTimeCounterValue( void )\r
392 {\r
393         /* This function is not used by the Blinky build configuration, but needs\r
394         to be defined as the Blinky and Full build configurations share a\r
395         FreeRTOSConfig.h header file. */\r
396         return 0UL;\r
397 }\r
398 \r