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