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