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