2 FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 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 This simple demo project runs on the STM32 Discovery board, which is
\r
68 populated with an STM32F100RB Cortex-M3 microcontroller. The discovery board
\r
69 makes an ideal low cost evaluation platform, but the 8K of RAM provided on the
\r
70 STM32F100RB does not allow the simple application to demonstrate all of all the
\r
71 FreeRTOS kernel features. Therefore, this simple demo only actively
\r
72 demonstrates task, queue, timer and interrupt functionality. In addition, the
\r
73 demo is configured to include malloc failure, idle and stack overflow hook
\r
76 The idle hook function:
\r
77 The idle hook function queries the amount of FreeRTOS heap space that is
\r
78 remaining (see vApplicationIdleHook() defined in this file). The demo
\r
79 application is configured to use 7K of the available 8K of RAM as the FreeRTOS
\r
80 heap. Memory is only allocated from this heap during initialisation, and this
\r
81 demo only actually uses 1.6K bytes of the configured 7K available - leaving 5.4K
\r
82 bytes of heap space unallocated.
\r
84 The main() Function:
\r
85 main() creates one software timer, one queue, and two tasks. It then starts the
\r
88 The Queue Send Task:
\r
89 The queue send task is implemented by the prvQueueSendTask() function in this
\r
90 file. prvQueueSendTask() sits in a loop that causes it to repeatedly block for
\r
91 200 milliseconds, before sending the value 100 to the queue that was created
\r
92 within main(). Once the value is sent, the task loops back around to block for
\r
93 another 200 milliseconds.
\r
95 The Queue Receive Task:
\r
96 The queue receive task is implemented by the prvQueueReceiveTask() function
\r
97 in this file. prvQueueReceiveTask() sits in a loop where it repeatedly blocks
\r
98 on attempts to read data from the queue that was created within main(). When
\r
99 data is received, the task checks the value of the data, and if the value equals
\r
100 the expected 100, toggles the green LED. The 'block time' parameter passed to
\r
101 the queue receive function specifies that the task should be held in the Blocked
\r
102 state indefinitely to wait for data to be available on the queue. The queue
\r
103 receive task will only leave the Blocked state when the queue send task writes
\r
104 to the queue. As the queue send task writes to the queue every 200
\r
105 milliseconds, the queue receive task leaves the Blocked state every 200
\r
106 milliseconds, and therefore toggles the green LED every 200 milliseconds.
\r
108 The LED Software Timer and the Button Interrupt:
\r
109 The user button B1 is configured to generate an interrupt each time it is
\r
110 pressed. The interrupt service routine switches the red LED on, and resets the
\r
111 LED software timer. The LED timer has a 5000 millisecond (5 second) period, and
\r
112 uses a callback function that is defined to just turn the red LED off.
\r
113 Therefore, pressing the user button will turn the red LED on, and the LED will
\r
114 remain on until a full five seconds pass without the button being pressed.
\r
118 /* Kernel includes. */
\r
119 #include "FreeRTOS.h"
\r
122 #include "timers.h"
\r
124 /* STM32 Library includes. */
\r
125 #include "stm32f10x.h"
\r
126 #include "STM32vldiscovery.h"
\r
128 /* Priorities at which the tasks are created. */
\r
129 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
130 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
132 /* The rate at which data is sent to the queue, specified in milliseconds, and
\r
133 converted to ticks using the portTICK_RATE_MS constant. */
\r
134 #define mainQUEUE_SEND_FREQUENCY_MS ( 200 / portTICK_RATE_MS )
\r
136 /* The number of items the queue can hold. This is 1 as the receive task
\r
137 will remove items as they are added, meaning the send task should always find
\r
138 the queue empty. */
\r
139 #define mainQUEUE_LENGTH ( 1 )
\r
141 /*-----------------------------------------------------------*/
\r
144 * Setup the NVIC, LED outputs, and button inputs.
\r
146 static void prvSetupHardware( void );
\r
149 * The tasks as described in the comments at the top of this file.
\r
151 static void prvQueueReceiveTask( void *pvParameters );
\r
152 static void prvQueueSendTask( void *pvParameters );
\r
155 * The LED timer callback function. This does nothing but switch the red LED
\r
158 static void vLEDTimerCallback( xTimerHandle xTimer );
\r
160 /*-----------------------------------------------------------*/
\r
162 /* The queue used by both tasks. */
\r
163 static xQueueHandle xQueue = NULL;
\r
165 /* The LED software timer. This uses vLEDTimerCallback() as its callback
\r
168 static xTimerHandle xLEDTimer = NULL;
\r
170 /*-----------------------------------------------------------*/
\r
174 /* Configure the NVIC, LED outputs and button inputs. */
\r
175 prvSetupHardware();
\r
177 /* Create the queue. */
\r
178 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
\r
180 if( xQueue != NULL )
\r
182 /* Start the two tasks as described in the comments at the top of this
\r
184 xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );
\r
185 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
\r
187 /* Create the software timer that is responsible for turning off the LED
\r
188 if the button is not pushed within 5000ms, as described at the top of
\r
190 xLEDTimer = xTimerCreate( "LEDTimer", /* A text name, purely to help debugging. */
\r
191 ( 5000 / portTICK_RATE_MS ),/* The timer period, in this case 5000ms (5s). */
\r
192 pdFALSE, /* This is a one shot timer, so xAutoReload is set to pdFALSE. */
\r
193 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
194 vLEDTimerCallback /* The callback function that switches the LED off. */
\r
197 /* Start the tasks and timer running. */
\r
198 vTaskStartScheduler();
\r
201 /* If all is well, the scheduler will now be running, and the following line
\r
202 will never be reached. If the following line does execute, then there was
\r
203 insufficient FreeRTOS heap memory available for the idle and/or timer tasks
\r
204 to be created. See the memory management section on the FreeRTOS web site
\r
205 for more details. */
\r
208 /*-----------------------------------------------------------*/
\r
210 static void vLEDTimerCallback( xTimerHandle xTimer )
\r
212 /* The timer has expired - so no button pushes have occurred in the last
\r
213 five seconds - turn the LED off. NOTE - accessing the LED port should use
\r
214 a critical section because it is accessed from multiple tasks, and the
\r
215 button interrupt - in this trivial case, for simplicity, the critical
\r
216 section is omitted. */
\r
217 STM32vldiscovery_LEDOff( LED4 );
\r
219 /*-----------------------------------------------------------*/
\r
221 /* The ISR executed when the user button is pushed. */
\r
222 void EXTI0_IRQHandler( void )
\r
224 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
226 /* The button was pushed, so ensure the LED is on before resetting the
\r
227 LED timer. The LED timer will turn the LED off if the button is not
\r
228 pushed within 5000ms. */
\r
229 STM32vldiscovery_LEDOn( LED4 );
\r
231 /* This interrupt safe FreeRTOS function can be called from this interrupt
\r
232 because the interrupt priority is below the
\r
233 configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */
\r
234 xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );
\r
236 /* Clear the interrupt before leaving. */
\r
237 EXTI_ClearITPendingBit( EXTI_Line0 );
\r
239 /* If calling xTimerResetFromISR() caused a task (in this case the timer
\r
240 service/daemon task) to unblock, and the unblocked task has a priority
\r
241 higher than or equal to the task that was interrupted, then
\r
242 xHigherPriorityTaskWoken will now be set to pdTRUE, and calling
\r
243 portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */
\r
244 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
246 /*-----------------------------------------------------------*/
\r
248 static void prvQueueSendTask( void *pvParameters )
\r
250 portTickType xNextWakeTime;
\r
251 const unsigned long ulValueToSend = 100UL;
\r
253 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
254 xNextWakeTime = xTaskGetTickCount();
\r
258 /* Place this task in the blocked state until it is time to run again.
\r
259 The block time is specified in ticks, the constant used converts ticks
\r
260 to ms. While in the Blocked state this task will not consume any CPU
\r
262 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
\r
264 /* Send to the queue - causing the queue receive task to unblock and
\r
265 toggle an LED. 0 is used as the block time so the sending operation
\r
266 will not block - it shouldn't need to block as the queue should always
\r
267 be empty at this point in the code. */
\r
268 xQueueSend( xQueue, &ulValueToSend, 0 );
\r
271 /*-----------------------------------------------------------*/
\r
273 static void prvQueueReceiveTask( void *pvParameters )
\r
275 unsigned long ulReceivedValue;
\r
279 /* Wait until something arrives in the queue - this task will block
\r
280 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
\r
281 FreeRTOSConfig.h. */
\r
282 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
\r
284 /* To get here something must have been received from the queue, but
\r
285 is it the expected value? If it is, toggle the green LED. */
\r
286 if( ulReceivedValue == 100UL )
\r
288 /* NOTE - accessing the LED port should use a critical section
\r
289 because it is accessed from multiple tasks, and the button interrupt
\r
290 - in this trivial case, for simplicity, the critical section is
\r
292 STM32vldiscovery_LEDToggle( LED3 );
\r
296 /*-----------------------------------------------------------*/
\r
298 static void prvSetupHardware( void )
\r
300 /* Ensure that all 4 interrupt priority bits are used as the pre-emption
\r
302 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
304 /* Set up the LED outputs and the button inputs. */
\r
305 STM32vldiscovery_LEDInit( LED3 );
\r
306 STM32vldiscovery_LEDInit( LED4 );
\r
307 STM32vldiscovery_PBInit( BUTTON_USER, BUTTON_MODE_EXTI );
\r
309 /* Start with the LEDs off. */
\r
310 STM32vldiscovery_LEDOff( LED3 );
\r
311 STM32vldiscovery_LEDOff( LED4 );
\r
313 /*-----------------------------------------------------------*/
\r
315 void vApplicationMallocFailedHook( void )
\r
317 /* Called if a call to pvPortMalloc() fails because there is insufficient
\r
318 free memory available in the FreeRTOS heap. pvPortMalloc() is called
\r
319 internally by FreeRTOS API functions that create tasks, queues, software
\r
320 timers, and semaphores. The size of the FreeRTOS heap is set by the
\r
321 configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
\r
324 /*-----------------------------------------------------------*/
\r
326 void vApplicationStackOverflowHook( xTaskHandle pxTask, char *pcTaskName )
\r
328 ( void ) pcTaskName;
\r
331 /* Run time stack overflow checking is performed if
\r
332 configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
333 function is called if a stack overflow is detected. */
\r
336 /*-----------------------------------------------------------*/
\r
338 void vApplicationIdleHook( void )
\r
340 volatile size_t xFreeStackSpace;
\r
342 /* This function is called on each cycle of the idle task. In this case it
\r
343 does nothing useful, other than report the amout of FreeRTOS heap that
\r
344 remains unallocated. */
\r
345 xFreeStackSpace = xPortGetFreeHeapSize();
\r
347 if( xFreeStackSpace > 100 )
\r
349 /* By now, the kernel has allocated everything it is going to, so
\r
350 if there is a lot of heap remaining unallocated then
\r
351 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be
\r
352 reduced accordingly. */
\r