2 * FreeRTOS Kernel V10.0.1
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\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
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
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
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
44 * The main() Function:
\r
45 * main() creates one software timer, one queue, and two tasks. It then starts
\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
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
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
78 /* Kernel includes. */
\r
79 #include "FreeRTOS.h"
\r
84 /* Microsemi drivers/libraries. */
\r
85 #include "mss_gpio.h"
\r
86 #include "mss_watchdog.h"
\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
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
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
100 #define mainQUEUE_LENGTH ( 1 )
\r
102 /* The LED toggle by the queue receive task. */
\r
103 #define mainTASK_CONTROLLED_LED 0x01UL
\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
108 /*-----------------------------------------------------------*/
\r
111 * Setup the NVIC, LED outputs, and button inputs.
\r
113 static void prvSetupHardware( void );
\r
116 * The tasks as described in the comments at the top of this file.
\r
118 static void prvQueueReceiveTask( void *pvParameters );
\r
119 static void prvQueueSendTask( void *pvParameters );
\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
125 static void vLEDTimerCallback( TimerHandle_t xTimer );
\r
127 /*-----------------------------------------------------------*/
\r
129 /* The queue used by both tasks. */
\r
130 static QueueHandle_t xQueue = NULL;
\r
132 /* The LED software timer. This uses vLEDTimerCallback() as its callback
\r
134 static TimerHandle_t xLEDTimer = NULL;
\r
136 /* Maintains the current LED output state. */
\r
137 static volatile unsigned long ulGPIOState = 0UL;
\r
139 /*-----------------------------------------------------------*/
\r
143 /* Configure the NVIC, LED outputs and button inputs. */
\r
144 prvSetupHardware();
\r
146 /* Create the queue. */
\r
147 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
\r
149 if( xQueue != NULL )
\r
151 /* Start the two tasks as described in the comments at the top of this
\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
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
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
166 /* Start the tasks and timer running. */
\r
167 vTaskStartScheduler();
\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
177 /*-----------------------------------------------------------*/
\r
179 static void vLEDTimerCallback( TimerHandle_t xTimer )
\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
189 /*-----------------------------------------------------------*/
\r
191 /* The ISR executed when the user button is pushed. */
\r
192 void GPIO8_IRQHandler( void )
\r
194 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\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
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
207 /* Clear the interrupt before leaving. */
\r
208 MSS_GPIO_clear_irq( MSS_GPIO_8 );
\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
217 /*-----------------------------------------------------------*/
\r
219 static void prvQueueSendTask( void *pvParameters )
\r
221 TickType_t xNextWakeTime;
\r
222 const unsigned long ulValueToSend = 100UL;
\r
224 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
225 xNextWakeTime = xTaskGetTickCount();
\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
233 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
\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
242 /*-----------------------------------------------------------*/
\r
244 static void prvQueueReceiveTask( void *pvParameters )
\r
246 unsigned long ulReceivedValue;
\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
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
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
263 if( ( ulGPIOState & mainTASK_CONTROLLED_LED ) != 0 )
\r
265 ulGPIOState &= ~mainTASK_CONTROLLED_LED;
\r
269 ulGPIOState |= mainTASK_CONTROLLED_LED;
\r
271 MSS_GPIO_set_outputs( ulGPIOState );
\r
275 /*-----------------------------------------------------------*/
\r
277 static void prvSetupHardware( void )
\r
279 SystemCoreClockUpdate();
\r
281 /* Disable the Watch Dog Timer */
\r
284 /* Initialise the GPIO */
\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
297 /* All LEDs start off. */
\r
298 ulGPIOState = 0xffffffffUL;
\r
299 MSS_GPIO_set_outputs( ulGPIOState );
\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
307 /*-----------------------------------------------------------*/
\r
309 void vApplicationMallocFailedHook( void )
\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
318 /*-----------------------------------------------------------*/
\r
320 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
322 ( void ) pcTaskName;
\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
330 /*-----------------------------------------------------------*/
\r
332 void vApplicationIdleHook( void )
\r
334 volatile size_t xFreeHeapSpace;
\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
341 if( xFreeHeapSpace > 100 )
\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
349 /*-----------------------------------------------------------*/
\r
351 void vMainConfigureTimerForRunTimeStats( void )
\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
357 /*-----------------------------------------------------------*/
\r
359 unsigned long ulGetRunTimeCounterValue( void )
\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