]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A2F200_SoftConsole/main-blinky.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / CORTEX_A2F200_SoftConsole / main-blinky.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*\r
70  * main-blinky.c is included when the "Blinky" build configuration is used.\r
71  * main-full.c is included when the "Full" build configuration is used.\r
72  *\r
73  * main-blinky.c (this file) defines a very simple demo that creates two tasks,\r
74  * one queue, and one timer.  It also demonstrates how Cortex-M3 interrupts can\r
75  * interact with FreeRTOS tasks/timers.\r
76  *\r
77  * This simple demo project runs on the SmartFusion A2F-EVAL-KIT evaluation\r
78  * board, which is populated with an A2F200M3F SmartFusion mixed signal FPGA.\r
79  * The A2F200M3F incorporates a Cortex-M3 microcontroller.\r
80  *\r
81  * The idle hook function:\r
82  * The idle hook function demonstrates how to query the amount of FreeRTOS heap\r
83  * space that is remaining (see vApplicationIdleHook() defined in this file).\r
84  *\r
85  * The main() Function:\r
86  * main() creates one software timer, one queue, and two tasks.  It then starts\r
87  * the scheduler.\r
88  *\r
89  * The Queue Send Task:\r
90  * The queue send task is implemented by the prvQueueSendTask() function in\r
91  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
92  * block for 200 milliseconds, before sending the value 100 to the queue that\r
93  * was created within main().  Once the value is sent, the task loops back\r
94  * around to block for another 200 milliseconds.\r
95  *\r
96  * The Queue Receive Task:\r
97  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
98  * in this file.  prvQueueReceiveTask() sits in a loop that causes it to\r
99  * repeatedly attempt to read data from the queue that was created within\r
100  * main().  When data is received, the task checks the value of the data, and\r
101  * if the value equals the expected 100, toggles the green LED.  The 'block\r
102  * time' parameter passed to the queue receive function specifies that the task\r
103  * should be held in the Blocked state indefinitely to wait for data to be\r
104  * available on the queue.  The queue receive task will only leave the Blocked\r
105  * state when the queue send task writes to the queue.  As the queue send task\r
106  * writes to the queue every 200 milliseconds, the queue receive task leaves\r
107  * the Blocked state every 200 milliseconds, and therefore toggles the LED\r
108  * every 200 milliseconds.\r
109  *\r
110  * The LED Software Timer and the Button Interrupt:\r
111  * The user button SW1 is configured to generate an interrupt each time it is\r
112  * pressed.  The interrupt service routine switches an LED on, and resets the\r
113  * LED software timer.  The LED timer has a 5000 millisecond (5 second) period,\r
114  * and uses a callback function that is defined to just turn the LED off again.\r
115  * Therefore, pressing the user button will turn the LED on, and the LED will\r
116  * remain on until a full five seconds pass without the button being pressed.\r
117  */\r
118 \r
119 /* Kernel includes. */\r
120 #include "FreeRTOS.h"\r
121 #include "task.h"\r
122 #include "queue.h"\r
123 #include "timers.h"\r
124 \r
125 /* Microsemi drivers/libraries. */\r
126 #include "mss_gpio.h"\r
127 #include "mss_watchdog.h"\r
128 \r
129 \r
130 /* Priorities at which the tasks are created. */\r
131 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
132 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
133 \r
134 /* The rate at which data is sent to the queue, specified in milliseconds, and\r
135 converted to ticks using the portTICK_RATE_MS constant. */\r
136 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_RATE_MS )\r
137 \r
138 /* The number of items the queue can hold.  This is 1 as the receive task\r
139 will remove items as they are added, meaning the send task should always find\r
140 the queue empty. */\r
141 #define mainQUEUE_LENGTH                                        ( 1 )\r
142 \r
143 /* The LED toggle by the queue receive task. */\r
144 #define mainTASK_CONTROLLED_LED                         0x01UL\r
145 \r
146 /* The LED turned on by the button interrupt, and turned off by the LED timer. */\r
147 #define mainTIMER_CONTROLLED_LED                        0x02UL\r
148 \r
149 /*-----------------------------------------------------------*/\r
150 \r
151 /*\r
152  * Setup the NVIC, LED outputs, and button inputs.\r
153  */\r
154 static void prvSetupHardware( void );\r
155 \r
156 /*\r
157  * The tasks as described in the comments at the top of this file.\r
158  */\r
159 static void prvQueueReceiveTask( void *pvParameters );\r
160 static void prvQueueSendTask( void *pvParameters );\r
161 \r
162 /*\r
163  * The LED timer callback function.  This does nothing but switch off the\r
164  * LED defined by the mainTIMER_CONTROLLED_LED constant.\r
165  */\r
166 static void vLEDTimerCallback( xTimerHandle xTimer );\r
167 \r
168 /*-----------------------------------------------------------*/\r
169 \r
170 /* The queue used by both tasks. */\r
171 static xQueueHandle xQueue = NULL;\r
172 \r
173 /* The LED software timer.  This uses vLEDTimerCallback() as its callback\r
174 function. */\r
175 static xTimerHandle xLEDTimer = NULL;\r
176 \r
177 /* Maintains the current LED output state. */\r
178 static volatile unsigned long ulGPIOState = 0UL;\r
179 \r
180 /*-----------------------------------------------------------*/\r
181 \r
182 int main(void)\r
183 {\r
184         /* Configure the NVIC, LED outputs and button inputs. */\r
185         prvSetupHardware();\r
186 \r
187         /* Create the queue. */\r
188         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
189 \r
190         if( xQueue != NULL )\r
191         {\r
192                 /* Start the two tasks as described in the comments at the top of this\r
193                 file. */\r
194                 xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
195                 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
196 \r
197                 /* Create the software timer that is responsible for turning off the LED\r
198                 if the button is not pushed within 5000ms, as described at the top of\r
199                 this file. */\r
200                 xLEDTimer = xTimerCreate(       ( const signed char * ) "LEDTimer", /* A text name, purely to help debugging. */\r
201                                                                         ( 5000 / portTICK_RATE_MS ),            /* The timer period, in this case 5000ms (5s). */\r
202                                                                         pdFALSE,                                                        /* This is a one shot timer, so xAutoReload is set to pdFALSE. */\r
203                                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
204                                                                         vLEDTimerCallback                                       /* The callback function that switches the LED off. */\r
205                                                                 );\r
206 \r
207                 /* Start the tasks and timer running. */\r
208                 vTaskStartScheduler();\r
209         }\r
210 \r
211         /* If all is well, the scheduler will now be running, and the following line\r
212         will never be reached.  If the following line does execute, then there was\r
213         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
214         to be created.  See the memory management section on the FreeRTOS web site\r
215         for more details. */\r
216         for( ;; );\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 static void vLEDTimerCallback( xTimerHandle xTimer )\r
221 {\r
222         /* The timer has expired - so no button pushes have occurred in the last\r
223         five seconds - turn the LED off.  NOTE - accessing the LED port should use\r
224         a critical section because it is accessed from multiple tasks, and the\r
225         button interrupt - in this trivial case, for simplicity, the critical\r
226         section is omitted. */\r
227         ulGPIOState |= mainTIMER_CONTROLLED_LED;\r
228         MSS_GPIO_set_outputs( ulGPIOState );\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 /* The ISR executed when the user button is pushed. */\r
233 void GPIO8_IRQHandler( void )\r
234 {\r
235 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
236 \r
237         /* The button was pushed, so ensure the LED is on before resetting the\r
238         LED timer.  The LED timer will turn the LED off if the button is not\r
239         pushed within 5000ms. */\r
240         ulGPIOState &= ~mainTIMER_CONTROLLED_LED;\r
241         MSS_GPIO_set_outputs( ulGPIOState );\r
242 \r
243         /* This interrupt safe FreeRTOS function can be called from this interrupt\r
244         because the interrupt priority is below the\r
245         configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
246         xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );\r
247 \r
248         /* Clear the interrupt before leaving. */\r
249     MSS_GPIO_clear_irq( MSS_GPIO_8 );\r
250 \r
251         /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
252         service/daemon task) to unblock, and the unblocked task has a priority\r
253         higher than or equal to the task that was interrupted, then\r
254         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
255         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
256         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
257 }\r
258 /*-----------------------------------------------------------*/\r
259 \r
260 static void prvQueueSendTask( void *pvParameters )\r
261 {\r
262 portTickType xNextWakeTime;\r
263 const unsigned long ulValueToSend = 100UL;\r
264 \r
265         /* Initialise xNextWakeTime - this only needs to be done once. */\r
266         xNextWakeTime = xTaskGetTickCount();\r
267 \r
268         for( ;; )\r
269         {\r
270                 /* Place this task in the blocked state until it is time to run again.\r
271                 The block time is specified in ticks, the constant used converts ticks\r
272                 to ms.  While in the Blocked state this task will not consume any CPU\r
273                 time. */\r
274                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
275 \r
276                 /* Send to the queue - causing the queue receive task to unblock and\r
277                 toggle an LED.  0 is used as the block time so the sending operation\r
278                 will not block - it shouldn't need to block as the queue should always\r
279                 be empty at this point in the code. */\r
280                 xQueueSend( xQueue, &ulValueToSend, 0 );\r
281         }\r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 static void prvQueueReceiveTask( void *pvParameters )\r
286 {\r
287 unsigned long ulReceivedValue;\r
288 \r
289         for( ;; )\r
290         {\r
291                 /* Wait until something arrives in the queue - this task will block\r
292                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
293                 FreeRTOSConfig.h. */\r
294                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
295 \r
296                 /*  To get here something must have been received from the queue, but\r
297                 is it the expected value?  If it is, toggle the green LED. */\r
298                 if( ulReceivedValue == 100UL )\r
299                 {\r
300                         /* NOTE - accessing the LED port should use a critical section\r
301                         because it is accessed from multiple tasks, and the button interrupt\r
302                         - in this trivial case, for simplicity, the critical section is\r
303                         omitted. */\r
304                         if( ( ulGPIOState & mainTASK_CONTROLLED_LED ) != 0 )\r
305                         {\r
306                                 ulGPIOState &= ~mainTASK_CONTROLLED_LED;\r
307                         }\r
308                         else\r
309                         {\r
310                                 ulGPIOState |= mainTASK_CONTROLLED_LED;\r
311                         }\r
312                         MSS_GPIO_set_outputs( ulGPIOState );\r
313                 }\r
314         }\r
315 }\r
316 /*-----------------------------------------------------------*/\r
317 \r
318 static void prvSetupHardware( void )\r
319 {\r
320         SystemCoreClockUpdate();\r
321 \r
322         /* Disable the Watch Dog Timer */\r
323         MSS_WD_disable( );\r
324 \r
325         /* Initialise the GPIO */\r
326         MSS_GPIO_init();\r
327 \r
328         /* Set up GPIO for the LEDs. */\r
329     MSS_GPIO_config( MSS_GPIO_0 , MSS_GPIO_OUTPUT_MODE );\r
330     MSS_GPIO_config( MSS_GPIO_1 , MSS_GPIO_OUTPUT_MODE );\r
331     MSS_GPIO_config( MSS_GPIO_2 , MSS_GPIO_OUTPUT_MODE );\r
332     MSS_GPIO_config( MSS_GPIO_3 , MSS_GPIO_OUTPUT_MODE );\r
333     MSS_GPIO_config( MSS_GPIO_4 , MSS_GPIO_OUTPUT_MODE );\r
334     MSS_GPIO_config( MSS_GPIO_5 , MSS_GPIO_OUTPUT_MODE );\r
335     MSS_GPIO_config( MSS_GPIO_6 , MSS_GPIO_OUTPUT_MODE );\r
336     MSS_GPIO_config( MSS_GPIO_7 , MSS_GPIO_OUTPUT_MODE );\r
337 \r
338     /* All LEDs start off. */\r
339     ulGPIOState = 0xffffffffUL;\r
340     MSS_GPIO_set_outputs( ulGPIOState );\r
341 \r
342         /* Setup the GPIO and the NVIC for the switch used in this simple demo. */\r
343         NVIC_SetPriority( GPIO8_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
344     NVIC_EnableIRQ( GPIO8_IRQn );\r
345     MSS_GPIO_config( MSS_GPIO_8, MSS_GPIO_INPUT_MODE | MSS_GPIO_IRQ_EDGE_NEGATIVE );\r
346     MSS_GPIO_enable_irq( MSS_GPIO_8 );\r
347 }\r
348 /*-----------------------------------------------------------*/\r
349 \r
350 void vApplicationMallocFailedHook( void )\r
351 {\r
352         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
353         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
354         internally by FreeRTOS API functions that create tasks, queues, software\r
355         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
356         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
357         for( ;; );\r
358 }\r
359 /*-----------------------------------------------------------*/\r
360 \r
361 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
362 {\r
363         ( void ) pcTaskName;\r
364         ( void ) pxTask;\r
365 \r
366         /* Run time stack overflow checking is performed if\r
367         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
368         function is called if a stack overflow is detected. */\r
369         for( ;; );\r
370 }\r
371 /*-----------------------------------------------------------*/\r
372 \r
373 void vApplicationIdleHook( void )\r
374 {\r
375 volatile size_t xFreeHeapSpace;\r
376 \r
377         /* This function is called on each cycle of the idle task.  In this case it\r
378         does nothing useful, other than report the amout of FreeRTOS heap that\r
379         remains unallocated. */\r
380         xFreeHeapSpace = xPortGetFreeHeapSize();\r
381 \r
382         if( xFreeHeapSpace > 100 )\r
383         {\r
384                 /* By now, the kernel has allocated everything it is going to, so\r
385                 if there is a lot of heap remaining unallocated then\r
386                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
387                 reduced accordingly. */\r
388         }\r
389 }\r
390 /*-----------------------------------------------------------*/\r
391 \r
392 void vMainConfigureTimerForRunTimeStats( void )\r
393 {\r
394         /* This function is not used by the Blinky build configuration, but needs\r
395         to be defined as the Blinky and Full build configurations share a\r
396         FreeRTOSConfig.h header file. */\r
397 }\r
398 /*-----------------------------------------------------------*/\r
399 \r
400 unsigned long ulGetRunTimeCounterValue( void )\r
401 {\r
402         /* This function is not used by the Blinky build configuration, but needs\r
403         to be defined as the Blinky and Full build configurations share a\r
404         FreeRTOSConfig.h header file. */\r
405         return 0UL;\r
406 }\r
407 \r