2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 * main-blinky.c is included when the "Blinky" build configuration is used.
\r
68 * main-full.c is included when the "Full" build configuration is used.
\r
70 * main-blinky.c (this file) defines a very simple demo that creates two tasks,
\r
71 * one queue, and one timer. It also demonstrates how Cortex-M3 interrupts can
\r
72 * interact with FreeRTOS tasks/timers.
\r
74 * This simple demo project runs on the SmartFusion A2F-EVAL-KIT evaluation
\r
75 * board, which is populated with an A2F200M3F SmartFusion mixed signal FPGA.
\r
76 * The A2F200M3F incorporates a Cortex-M3 microcontroller.
\r
78 * The idle hook function:
\r
79 * The idle hook function demonstrates how to query the amount of FreeRTOS heap
\r
80 * space that is remaining (see vApplicationIdleHook() defined in this file).
\r
82 * The main() Function:
\r
83 * main() creates one software timer, one queue, and two tasks. It then starts
\r
86 * The Queue Send Task:
\r
87 * The queue send task is implemented by the prvQueueSendTask() function in
\r
88 * this file. prvQueueSendTask() sits in a loop that causes it to repeatedly
\r
89 * block for 200 milliseconds, before sending the value 100 to the queue that
\r
90 * was created within main(). Once the value is sent, the task loops back
\r
91 * around to block for another 200 milliseconds.
\r
93 * The Queue Receive Task:
\r
94 * The queue receive task is implemented by the prvQueueReceiveTask() function
\r
95 * in this file. prvQueueReceiveTask() sits in a loop that causes it to
\r
96 * repeatedly attempt to read data from the queue that was created within
\r
97 * main(). When data is received, the task checks the value of the data, and
\r
98 * if the value equals the expected 100, toggles the green LED. The 'block
\r
99 * time' parameter passed to the queue receive function specifies that the task
\r
100 * should be held in the Blocked state indefinitely to wait for data to be
\r
101 * available on the queue. The queue receive task will only leave the Blocked
\r
102 * state when the queue send task writes to the queue. As the queue send task
\r
103 * writes to the queue every 200 milliseconds, the queue receive task leaves
\r
104 * the Blocked state every 200 milliseconds, and therefore toggles the LED
\r
105 * every 200 milliseconds.
\r
107 * The LED Software Timer and the Button Interrupt:
\r
108 * The user button SW1 is configured to generate an interrupt each time it is
\r
109 * pressed. The interrupt service routine switches an LED on, and resets the
\r
110 * LED software timer. The LED timer has a 5000 millisecond (5 second) period,
\r
111 * and uses a callback function that is defined to just turn the LED off again.
\r
112 * Therefore, pressing the user button will turn the LED on, and the LED will
\r
113 * remain on until a full five seconds pass without the button being pressed.
\r
116 /* Kernel includes. */
\r
117 #include "FreeRTOS.h"
\r
120 #include "timers.h"
\r
122 /* Microsemi drivers/libraries. */
\r
123 #include "mss_gpio.h"
\r
124 #include "mss_watchdog.h"
\r
127 /* Priorities at which the tasks are created. */
\r
128 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
129 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
131 /* The rate at which data is sent to the queue, specified in milliseconds, and
\r
132 converted to ticks using the portTICK_RATE_MS constant. */
\r
133 #define mainQUEUE_SEND_FREQUENCY_MS ( 200 / portTICK_RATE_MS )
\r
135 /* The number of items the queue can hold. This is 1 as the receive task
\r
136 will remove items as they are added, meaning the send task should always find
\r
137 the queue empty. */
\r
138 #define mainQUEUE_LENGTH ( 1 )
\r
140 /* The LED toggle by the queue receive task. */
\r
141 #define mainTASK_CONTROLLED_LED 0x01UL
\r
143 /* The LED turned on by the button interrupt, and turned off by the LED timer. */
\r
144 #define mainTIMER_CONTROLLED_LED 0x02UL
\r
146 /*-----------------------------------------------------------*/
\r
149 * Setup the NVIC, LED outputs, and button inputs.
\r
151 static void prvSetupHardware( void );
\r
154 * The tasks as described in the comments at the top of this file.
\r
156 static void prvQueueReceiveTask( void *pvParameters );
\r
157 static void prvQueueSendTask( void *pvParameters );
\r
160 * The LED timer callback function. This does nothing but switch off the
\r
161 * LED defined by the mainTIMER_CONTROLLED_LED constant.
\r
163 static void vLEDTimerCallback( xTimerHandle xTimer );
\r
165 /*-----------------------------------------------------------*/
\r
167 /* The queue used by both tasks. */
\r
168 static xQueueHandle xQueue = NULL;
\r
170 /* The LED software timer. This uses vLEDTimerCallback() as its callback
\r
172 static xTimerHandle xLEDTimer = NULL;
\r
174 /* Maintains the current LED output state. */
\r
175 static volatile unsigned long ulGPIOState = 0UL;
\r
177 /*-----------------------------------------------------------*/
\r
181 /* Configure the NVIC, LED outputs and button inputs. */
\r
182 prvSetupHardware();
\r
184 /* Create the queue. */
\r
185 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
\r
187 if( xQueue != NULL )
\r
189 /* Start the two tasks as described in the comments at the top of this
\r
191 xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );
\r
192 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
\r
194 /* Create the software timer that is responsible for turning off the LED
\r
195 if the button is not pushed within 5000ms, as described at the top of
\r
197 xLEDTimer = xTimerCreate( "LEDTimer", /* A text name, purely to help debugging. */
\r
198 ( 5000 / portTICK_RATE_MS ), /* The timer period, in this case 5000ms (5s). */
\r
199 pdFALSE, /* This is a one shot timer, so xAutoReload is set to pdFALSE. */
\r
200 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
201 vLEDTimerCallback /* The callback function that switches the LED off. */
\r
204 /* Start the tasks and timer running. */
\r
205 vTaskStartScheduler();
\r
208 /* If all is well, the scheduler will now be running, and the following line
\r
209 will never be reached. If the following line does execute, then there was
\r
210 insufficient FreeRTOS heap memory available for the idle and/or timer tasks
\r
211 to be created. See the memory management section on the FreeRTOS web site
\r
212 for more details. */
\r
215 /*-----------------------------------------------------------*/
\r
217 static void vLEDTimerCallback( xTimerHandle xTimer )
\r
219 /* The timer has expired - so no button pushes have occurred in the last
\r
220 five seconds - turn the LED off. NOTE - accessing the LED port should use
\r
221 a critical section because it is accessed from multiple tasks, and the
\r
222 button interrupt - in this trivial case, for simplicity, the critical
\r
223 section is omitted. */
\r
224 ulGPIOState |= mainTIMER_CONTROLLED_LED;
\r
225 MSS_GPIO_set_outputs( ulGPIOState );
\r
227 /*-----------------------------------------------------------*/
\r
229 /* The ISR executed when the user button is pushed. */
\r
230 void GPIO8_IRQHandler( void )
\r
232 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
234 /* The button was pushed, so ensure the LED is on before resetting the
\r
235 LED timer. The LED timer will turn the LED off if the button is not
\r
236 pushed within 5000ms. */
\r
237 ulGPIOState &= ~mainTIMER_CONTROLLED_LED;
\r
238 MSS_GPIO_set_outputs( ulGPIOState );
\r
240 /* This interrupt safe FreeRTOS function can be called from this interrupt
\r
241 because the interrupt priority is below the
\r
242 configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */
\r
243 xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );
\r
245 /* Clear the interrupt before leaving. */
\r
246 MSS_GPIO_clear_irq( MSS_GPIO_8 );
\r
248 /* If calling xTimerResetFromISR() caused a task (in this case the timer
\r
249 service/daemon task) to unblock, and the unblocked task has a priority
\r
250 higher than or equal to the task that was interrupted, then
\r
251 xHigherPriorityTaskWoken will now be set to pdTRUE, and calling
\r
252 portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */
\r
253 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
255 /*-----------------------------------------------------------*/
\r
257 static void prvQueueSendTask( void *pvParameters )
\r
259 portTickType xNextWakeTime;
\r
260 const unsigned long ulValueToSend = 100UL;
\r
262 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
263 xNextWakeTime = xTaskGetTickCount();
\r
267 /* Place this task in the blocked state until it is time to run again.
\r
268 The block time is specified in ticks, the constant used converts ticks
\r
269 to ms. While in the Blocked state this task will not consume any CPU
\r
271 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
\r
273 /* Send to the queue - causing the queue receive task to unblock and
\r
274 toggle an LED. 0 is used as the block time so the sending operation
\r
275 will not block - it shouldn't need to block as the queue should always
\r
276 be empty at this point in the code. */
\r
277 xQueueSend( xQueue, &ulValueToSend, 0 );
\r
280 /*-----------------------------------------------------------*/
\r
282 static void prvQueueReceiveTask( void *pvParameters )
\r
284 unsigned long ulReceivedValue;
\r
288 /* Wait until something arrives in the queue - this task will block
\r
289 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
\r
290 FreeRTOSConfig.h. */
\r
291 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
\r
293 /* To get here something must have been received from the queue, but
\r
294 is it the expected value? If it is, toggle the green LED. */
\r
295 if( ulReceivedValue == 100UL )
\r
297 /* NOTE - accessing the LED port should use a critical section
\r
298 because it is accessed from multiple tasks, and the button interrupt
\r
299 - in this trivial case, for simplicity, the critical section is
\r
301 if( ( ulGPIOState & mainTASK_CONTROLLED_LED ) != 0 )
\r
303 ulGPIOState &= ~mainTASK_CONTROLLED_LED;
\r
307 ulGPIOState |= mainTASK_CONTROLLED_LED;
\r
309 MSS_GPIO_set_outputs( ulGPIOState );
\r
313 /*-----------------------------------------------------------*/
\r
315 static void prvSetupHardware( void )
\r
317 SystemCoreClockUpdate();
\r
319 /* Disable the Watch Dog Timer */
\r
322 /* Initialise the GPIO */
\r
325 /* Set up GPIO for the LEDs. */
\r
326 MSS_GPIO_config( MSS_GPIO_0 , MSS_GPIO_OUTPUT_MODE );
\r
327 MSS_GPIO_config( MSS_GPIO_1 , MSS_GPIO_OUTPUT_MODE );
\r
328 MSS_GPIO_config( MSS_GPIO_2 , MSS_GPIO_OUTPUT_MODE );
\r
329 MSS_GPIO_config( MSS_GPIO_3 , MSS_GPIO_OUTPUT_MODE );
\r
330 MSS_GPIO_config( MSS_GPIO_4 , MSS_GPIO_OUTPUT_MODE );
\r
331 MSS_GPIO_config( MSS_GPIO_5 , MSS_GPIO_OUTPUT_MODE );
\r
332 MSS_GPIO_config( MSS_GPIO_6 , MSS_GPIO_OUTPUT_MODE );
\r
333 MSS_GPIO_config( MSS_GPIO_7 , MSS_GPIO_OUTPUT_MODE );
\r
335 /* All LEDs start off. */
\r
336 ulGPIOState = 0xffffffffUL;
\r
337 MSS_GPIO_set_outputs( ulGPIOState );
\r
339 /* Setup the GPIO and the NVIC for the switch used in this simple demo. */
\r
340 NVIC_SetPriority( GPIO8_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
341 NVIC_EnableIRQ( GPIO8_IRQn );
\r
342 MSS_GPIO_config( MSS_GPIO_8, MSS_GPIO_INPUT_MODE | MSS_GPIO_IRQ_EDGE_NEGATIVE );
\r
343 MSS_GPIO_enable_irq( MSS_GPIO_8 );
\r
345 /*-----------------------------------------------------------*/
\r
347 void vApplicationMallocFailedHook( void )
\r
349 /* Called if a call to pvPortMalloc() fails because there is insufficient
\r
350 free memory available in the FreeRTOS heap. pvPortMalloc() is called
\r
351 internally by FreeRTOS API functions that create tasks, queues, software
\r
352 timers, and semaphores. The size of the FreeRTOS heap is set by the
\r
353 configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
\r
356 /*-----------------------------------------------------------*/
\r
358 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
360 ( void ) pcTaskName;
\r
363 /* Run time stack overflow checking is performed if
\r
364 configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
365 function is called if a stack overflow is detected. */
\r
368 /*-----------------------------------------------------------*/
\r
370 void vApplicationIdleHook( void )
\r
372 volatile size_t xFreeHeapSpace;
\r
374 /* This function is called on each cycle of the idle task. In this case it
\r
375 does nothing useful, other than report the amout of FreeRTOS heap that
\r
376 remains unallocated. */
\r
377 xFreeHeapSpace = xPortGetFreeHeapSize();
\r
379 if( xFreeHeapSpace > 100 )
\r
381 /* By now, the kernel has allocated everything it is going to, so
\r
382 if there is a lot of heap remaining unallocated then
\r
383 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be
\r
384 reduced accordingly. */
\r
387 /*-----------------------------------------------------------*/
\r
389 void vMainConfigureTimerForRunTimeStats( void )
\r
391 /* This function is not used by the Blinky build configuration, but needs
\r
392 to be defined as the Blinky and Full build configurations share a
\r
393 FreeRTOSConfig.h header file. */
\r
395 /*-----------------------------------------------------------*/
\r
397 unsigned long ulGetRunTimeCounterValue( void )
\r
399 /* This function is not used by the Blinky build configuration, but needs
\r
400 to be defined as the Blinky and Full build configurations share a
\r
401 FreeRTOSConfig.h header file. */
\r