]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A2F200_IAR_and_Keil/main-blinky.c
60800c3d74f7b784af7763b8c72d6d6bd02eef6e
[freertos] / FreeRTOS / Demo / CORTEX_A2F200_IAR_and_Keil / main-blinky.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30  * main-blinky.c is included when the "Blinky" build configuration is used.\r
31  * main-full.c is included when the "Full" build configuration is used.\r
32  *\r
33  * main-blinky.c (this file) defines a very simple demo that creates two tasks,\r
34  * one queue, and one timer.  It also demonstrates how Cortex-M3 interrupts can\r
35  * interact with FreeRTOS tasks/timers.\r
36  *\r
37  * This simple demo project runs on the SmartFusion A2F-EVAL-KIT evaluation\r
38  * board, which is populated with an A2F200M3F SmartFusion mixed signal FPGA.\r
39  * The A2F200M3F incorporates a Cortex-M3 microcontroller.\r
40  *\r
41  * The idle hook function:\r
42  * The idle hook function demonstrates how to query the amount of FreeRTOS heap\r
43  * space that is remaining (see vApplicationIdleHook() defined in this file).\r
44  *\r
45  * The main() Function:\r
46  * main() creates one software timer, one queue, and two tasks.  It then starts\r
47  * the scheduler.\r
48  *\r
49  * The Queue Send Task:\r
50  * The queue send task is implemented by the prvQueueSendTask() function in\r
51  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
52  * block for 200 milliseconds, before sending the value 100 to the queue that\r
53  * was created within main().  Once the value is sent, the task loops back\r
54  * around to block for another 200 milliseconds.\r
55  *\r
56  * The Queue Receive Task:\r
57  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
58  * in this file.  prvQueueReceiveTask() sits in a loop that causes it to\r
59  * repeatedly attempt to read data from the queue that was created within\r
60  * main().  When data is received, the task checks the value of the data, and\r
61  * if the value equals the expected 100, toggles the green LED.  The 'block\r
62  * time' parameter passed to the queue receive function specifies that the task\r
63  * should be held in the Blocked state indefinitely to wait for data to be\r
64  * available on the queue.  The queue receive task will only leave the Blocked\r
65  * state when the queue send task writes to the queue.  As the queue send task\r
66  * writes to the queue every 200 milliseconds, the queue receive task leaves\r
67  * the Blocked state every 200 milliseconds, and therefore toggles the LED\r
68  * every 200 milliseconds.\r
69  *\r
70  * The LED Software Timer and the Button Interrupt:\r
71  * The user button SW1 is configured to generate an interrupt each time it is\r
72  * pressed.  The interrupt service routine switches an LED on, and resets the\r
73  * LED software timer.  The LED timer has a 5000 millisecond (5 second) period,\r
74  * and uses a callback function that is defined to just turn the LED off again.\r
75  * Therefore, pressing the user button will turn the LED on, and the LED will\r
76  * remain on until a full five seconds pass without the button being pressed.\r
77  */\r
78 \r
79 /* Kernel includes. */\r
80 #include "FreeRTOS.h"\r
81 #include "task.h"\r
82 #include "queue.h"\r
83 #include "timers.h"\r
84 \r
85 /* Microsemi drivers/libraries. */\r
86 #include "mss_gpio.h"\r
87 #include "mss_watchdog.h"\r
88 \r
89 \r
90 /* Priorities at which the tasks are created. */\r
91 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
92 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
93 \r
94 /* The rate at which data is sent to the queue, specified in milliseconds, and\r
95 converted to ticks using the portTICK_PERIOD_MS constant. */\r
96 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_PERIOD_MS )\r
97 \r
98 /* The number of items the queue can hold.  This is 1 as the receive task\r
99 will remove items as they are added, meaning the send task should always find\r
100 the queue empty. */\r
101 #define mainQUEUE_LENGTH                                        ( 1 )\r
102 \r
103 /* The LED toggle by the queue receive task. */\r
104 #define mainTASK_CONTROLLED_LED                         0x01UL\r
105 \r
106 /* The LED turned on by the button interrupt, and turned off by the LED timer. */\r
107 #define mainTIMER_CONTROLLED_LED                        0x02UL\r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /*\r
112  * Setup the NVIC, LED outputs, and button inputs.\r
113  */\r
114 static void prvSetupHardware( void );\r
115 \r
116 /*\r
117  * The tasks as described in the comments at the top of this file.\r
118  */\r
119 static void prvQueueReceiveTask( void *pvParameters );\r
120 static void prvQueueSendTask( void *pvParameters );\r
121 \r
122 /*\r
123  * The LED timer callback function.  This does nothing but switch off the\r
124  * LED defined by the mainTIMER_CONTROLLED_LED constant.\r
125  */\r
126 static void vLEDTimerCallback( TimerHandle_t xTimer );\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 /* The queue used by both tasks. */\r
131 static QueueHandle_t xQueue = NULL;\r
132 \r
133 /* The LED software timer.  This uses vLEDTimerCallback() as its callback\r
134 function. */\r
135 static TimerHandle_t xLEDTimer = NULL;\r
136 \r
137 /* Maintains the current LED output state. */\r
138 static volatile unsigned long ulGPIOState = 0UL;\r
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 int main(void)\r
143 {\r
144         /* Configure the NVIC, LED outputs and button inputs. */\r
145         prvSetupHardware();\r
146 \r
147         /* Create the queue. */\r
148         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
149 \r
150         if( xQueue != NULL )\r
151         {\r
152                 /* Start the two tasks as described in the comments at the top of this\r
153                 file. */\r
154                 xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
155                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
156 \r
157                 /* Create the software timer that is responsible for turning off the LED\r
158                 if the button is not pushed within 5000ms, as described at the top of\r
159                 this file. */\r
160                 xLEDTimer = xTimerCreate(       "LEDTimer",                                     /* A text name, purely to help debugging. */\r
161                                                                         ( 5000 / portTICK_PERIOD_MS ),  /* The timer period, in this case 5000ms (5s). */\r
162                                                                         pdFALSE,                                                /* This is a one shot timer, so xAutoReload is set to pdFALSE. */\r
163                                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
164                                                                         vLEDTimerCallback                               /* The callback function that switches the LED off. */\r
165                                                                 );\r
166 \r
167                 /* Start the tasks and timer running. */\r
168                 vTaskStartScheduler();\r
169         }\r
170 \r
171         /* If all is well, the scheduler will now be running, and the following line\r
172         will never be reached.  If the following line does execute, then there was\r
173         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
174         to be created.  See the memory management section on the FreeRTOS web site\r
175         for more details. */\r
176         for( ;; );\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 static void vLEDTimerCallback( TimerHandle_t xTimer )\r
181 {\r
182         /* The timer has expired - so no button pushes have occurred in the last\r
183         five seconds - turn the LED off.  NOTE - accessing the LED port should use\r
184         a critical section because it is accessed from multiple tasks, and the\r
185         button interrupt - in this trivial case, for simplicity, the critical\r
186         section is omitted. */\r
187         ulGPIOState |= mainTIMER_CONTROLLED_LED;\r
188         MSS_GPIO_set_outputs( ulGPIOState );\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 /* The ISR executed when the user button is pushed. */\r
193 void GPIO8_IRQHandler( void )\r
194 {\r
195 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
196 \r
197         /* The button was pushed, so ensure the LED is on before resetting the\r
198         LED timer.  The LED timer will turn the LED off if the button is not\r
199         pushed within 5000ms. */\r
200         ulGPIOState &= ~mainTIMER_CONTROLLED_LED;\r
201         MSS_GPIO_set_outputs( ulGPIOState );\r
202 \r
203         /* This interrupt safe FreeRTOS function can be called from this interrupt\r
204         because the interrupt priority is below the\r
205         configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
206         xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );\r
207 \r
208         /* Clear the interrupt before leaving. */\r
209     MSS_GPIO_clear_irq( MSS_GPIO_8 );\r
210 \r
211         /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
212         service/daemon task) to unblock, and the unblocked task has a priority\r
213         higher than or equal to the task that was interrupted, then\r
214         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
215         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
216         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 static void prvQueueSendTask( void *pvParameters )\r
221 {\r
222 TickType_t xNextWakeTime;\r
223 const unsigned long ulValueToSend = 100UL;\r
224 \r
225         /* Initialise xNextWakeTime - this only needs to be done once. */\r
226         xNextWakeTime = xTaskGetTickCount();\r
227 \r
228         for( ;; )\r
229         {\r
230                 /* Place this task in the blocked state until it is time to run again.\r
231                 The block time is specified in ticks, the constant used converts ticks\r
232                 to ms.  While in the Blocked state this task will not consume any CPU\r
233                 time. */\r
234                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
235 \r
236                 /* Send to the queue - causing the queue receive task to unblock and\r
237                 toggle an LED.  0 is used as the block time so the sending operation\r
238                 will not block - it shouldn't need to block as the queue should always\r
239                 be empty at this point in the code. */\r
240                 xQueueSend( xQueue, &ulValueToSend, 0 );\r
241         }\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 static void prvQueueReceiveTask( void *pvParameters )\r
246 {\r
247 unsigned long ulReceivedValue;\r
248 \r
249         for( ;; )\r
250         {\r
251                 /* Wait until something arrives in the queue - this task will block\r
252                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
253                 FreeRTOSConfig.h. */\r
254                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
255 \r
256                 /*  To get here something must have been received from the queue, but\r
257                 is it the expected value?  If it is, toggle the green LED. */\r
258                 if( ulReceivedValue == 100UL )\r
259                 {\r
260                         /* NOTE - accessing the LED port should use a critical section\r
261                         because it is accessed from multiple tasks, and the button interrupt\r
262                         - in this trivial case, for simplicity, the critical section is\r
263                         omitted. */\r
264                         if( ( ulGPIOState & mainTASK_CONTROLLED_LED ) != 0 )\r
265                         {\r
266                                 ulGPIOState &= ~mainTASK_CONTROLLED_LED;\r
267                         }\r
268                         else\r
269                         {\r
270                                 ulGPIOState |= mainTASK_CONTROLLED_LED;\r
271                         }\r
272                         MSS_GPIO_set_outputs( ulGPIOState );\r
273                 }\r
274         }\r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 static void prvSetupHardware( void )\r
279 {\r
280         SystemCoreClockUpdate();\r
281 \r
282         /* Disable the Watch Dog Timer */\r
283         MSS_WD_disable( );\r
284 \r
285         /* Initialise the GPIO */\r
286         MSS_GPIO_init();\r
287 \r
288         /* Set up GPIO for the LEDs. */\r
289     MSS_GPIO_config( MSS_GPIO_0 , MSS_GPIO_OUTPUT_MODE );\r
290     MSS_GPIO_config( MSS_GPIO_1 , MSS_GPIO_OUTPUT_MODE );\r
291     MSS_GPIO_config( MSS_GPIO_2 , MSS_GPIO_OUTPUT_MODE );\r
292     MSS_GPIO_config( MSS_GPIO_3 , MSS_GPIO_OUTPUT_MODE );\r
293     MSS_GPIO_config( MSS_GPIO_4 , MSS_GPIO_OUTPUT_MODE );\r
294     MSS_GPIO_config( MSS_GPIO_5 , MSS_GPIO_OUTPUT_MODE );\r
295     MSS_GPIO_config( MSS_GPIO_6 , MSS_GPIO_OUTPUT_MODE );\r
296     MSS_GPIO_config( MSS_GPIO_7 , MSS_GPIO_OUTPUT_MODE );\r
297 \r
298     /* All LEDs start off. */\r
299     ulGPIOState = 0xffffffffUL;\r
300     MSS_GPIO_set_outputs( ulGPIOState );\r
301 \r
302         /* Setup the GPIO and the NVIC for the switch used in this simple demo. */\r
303         NVIC_SetPriority( GPIO8_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
304     NVIC_EnableIRQ( GPIO8_IRQn );\r
305     MSS_GPIO_config( MSS_GPIO_8, MSS_GPIO_INPUT_MODE | MSS_GPIO_IRQ_EDGE_NEGATIVE );\r
306     MSS_GPIO_enable_irq( MSS_GPIO_8 );\r
307 }\r
308 /*-----------------------------------------------------------*/\r
309 \r
310 void vApplicationMallocFailedHook( void )\r
311 {\r
312         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
313         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
314         internally by FreeRTOS API functions that create tasks, queues, software\r
315         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
316         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
317         for( ;; );\r
318 }\r
319 /*-----------------------------------------------------------*/\r
320 \r
321 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
322 {\r
323         ( void ) pcTaskName;\r
324         ( void ) pxTask;\r
325 \r
326         /* Run time stack overflow checking is performed if\r
327         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
328         function is called if a stack overflow is detected. */\r
329         for( ;; );\r
330 }\r
331 /*-----------------------------------------------------------*/\r
332 \r
333 void vApplicationIdleHook( void )\r
334 {\r
335 volatile size_t xFreeHeapSpace;\r
336 \r
337         /* This function is called on each cycle of the idle task.  In this case it\r
338         does nothing useful, other than report the amout of FreeRTOS heap that\r
339         remains unallocated. */\r
340         xFreeHeapSpace = xPortGetFreeHeapSize();\r
341 \r
342         if( xFreeHeapSpace > 100 )\r
343         {\r
344                 /* By now, the kernel has allocated everything it is going to, so\r
345                 if there is a lot of heap remaining unallocated then\r
346                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
347                 reduced accordingly. */\r
348         }\r
349 }\r
350 /*-----------------------------------------------------------*/\r
351 \r
352 void vMainConfigureTimerForRunTimeStats( void )\r
353 {\r
354         /* This function is not used by the Blinky build configuration, but needs\r
355         to be defined as the Blinky and Full build configurations share a\r
356         FreeRTOSConfig.h header file. */\r
357 }\r
358 /*-----------------------------------------------------------*/\r
359 \r
360 unsigned long ulGetRunTimeCounterValue( void )\r
361 {\r
362         /* This function is not used by the Blinky build configuration, but needs\r
363         to be defined as the Blinky and Full build configurations share a\r
364         FreeRTOSConfig.h header file. */\r
365         return 0UL;\r
366 }\r
367 \r