2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 * main-blinky.c is included when the "Blinky" build configuration is used.
\r
69 * main-full.c is included when the "Full" build configuration is used.
\r
71 * main-blinky.c (this file) defines a very simple demo that creates two tasks,
\r
72 * one queue, and one timer. It also demonstrates how Cortex-M3 interrupts can
\r
73 * interact with FreeRTOS tasks/timers.
\r
75 * This simple demo project runs 'stand alone' (without the rest of the tower
\r
76 * system) on the TWR-K60N512 tower module, which is populated with a K60N512
\r
77 * Cortex-M4 microcontroller.
\r
79 * The idle hook function:
\r
80 * The idle hook function demonstrates how to query the amount of FreeRTOS heap
\r
81 * space that is remaining (see vApplicationIdleHook() defined in this file).
\r
83 * The main() Function:
\r
84 * main() creates one software timer, one queue, and two tasks. It then starts
\r
87 * The Queue Send Task:
\r
88 * The queue send task is implemented by the prvQueueSendTask() function in
\r
89 * this file. prvQueueSendTask() sits in a loop that causes it to repeatedly
\r
90 * block for 200 milliseconds, before sending the value 100 to the queue that
\r
91 * was created within main(). Once the value is sent, the task loops back
\r
92 * around to block for another 200 milliseconds.
\r
94 * The Queue Receive Task:
\r
95 * The queue receive task is implemented by the prvQueueReceiveTask() function
\r
96 * in this file. prvQueueReceiveTask() sits in a loop that causes it to
\r
97 * repeatedly attempt to read data from the queue that was created within
\r
98 * main(). When data is received, the task checks the value of the data, and
\r
99 * if the value equals the expected 100, toggles the blue LED. The 'block
\r
100 * time' parameter passed to the queue receive function specifies that the task
\r
101 * should be held in the Blocked state indefinitely to wait for data to be
\r
102 * available on the queue. The queue receive task will only leave the Blocked
\r
103 * state when the queue send task writes to the queue. As the queue send task
\r
104 * writes to the queue every 200 milliseconds, the queue receive task leaves the
\r
105 * Blocked state every 200 milliseconds, and therefore toggles the blue LED
\r
106 * every 200 milliseconds.
\r
108 * The LED Software Timer and the Button Interrupt:
\r
109 * The user button SW2 is configured to generate an interrupt each time it is
\r
110 * pressed. The interrupt service routine switches the green LED on, and
\r
111 * resets the LED software timer. The LED timer has a 5000 millisecond (5
\r
112 * second) period, and uses a callback function that is defined to just turn the
\r
113 * LED off again. Therefore, pressing the user button will turn the LED on, and
\r
114 * the LED will remain on until a full five seconds pass without the button
\r
118 /* Kernel includes. */
\r
119 #include "FreeRTOS.h"
\r
122 #include "timers.h"
\r
124 /* Freescale includes. */
\r
125 #include "common.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 LED will remain on until the button has not been pushed for a full
\r
137 #define mainBUTTON_LED_TIMER_PERIOD_MS ( 5000UL / portTICK_RATE_MS )
\r
139 /* The number of items the queue can hold. This is 1 as the receive task
\r
140 will remove items as they are added, meaning the send task should always find
\r
141 the queue empty. */
\r
142 #define mainQUEUE_LENGTH ( 1 )
\r
144 /* The LED toggle by the queue receive task (blue). */
\r
145 #define mainTASK_CONTROLLED_LED ( 1UL << 10UL )
\r
147 /* The LED turned on by the button interrupt, and turned off by the LED timer
\r
149 #define mainTIMER_CONTROLLED_LED ( 1UL << 29UL )
\r
151 /* The vector used by the GPIO port E. Button SW2 is configured to generate
\r
152 an interrupt on this port. */
\r
153 #define mainGPIO_E_VECTOR ( 91 )
\r
155 /* A block time of zero simply means "don't block". */
\r
156 #define mainDONT_BLOCK ( 0UL )
\r
158 /*-----------------------------------------------------------*/
\r
161 * Setup the NVIC, LED outputs, and button inputs.
\r
163 static void prvSetupHardware( void );
\r
166 * The tasks as described in the comments at the top of this file.
\r
168 static void prvQueueReceiveTask( void *pvParameters );
\r
169 static void prvQueueSendTask( void *pvParameters );
\r
172 * The LED timer callback function. This does nothing but switch off the
\r
173 * LED defined by the mainTIMER_CONTROLLED_LED constant.
\r
175 static void prvButtonLEDTimerCallback( xTimerHandle xTimer );
\r
177 /*-----------------------------------------------------------*/
\r
179 /* The queue used by both tasks. */
\r
180 static xQueueHandle xQueue = NULL;
\r
182 /* The LED software timer. This uses prvButtonLEDTimerCallback() as its callback
\r
184 static xTimerHandle xButtonLEDTimer = NULL;
\r
186 /*-----------------------------------------------------------*/
\r
190 /* Configure the NVIC, LED outputs and button inputs. */
\r
191 prvSetupHardware();
\r
193 /* Create the queue. */
\r
194 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
\r
196 if( xQueue != NULL )
\r
198 /* Start the two tasks as described in the comments at the top of this
\r
200 xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );
\r
201 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
\r
203 /* Create the software timer that is responsible for turning off the LED
\r
204 if the button is not pushed within 5000ms, as described at the top of
\r
206 xButtonLEDTimer = xTimerCreate( ( const signed char * ) "ButtonLEDTimer", /* A text name, purely to help debugging. */
\r
207 mainBUTTON_LED_TIMER_PERIOD_MS, /* The timer period, in this case 5000ms (5s). */
\r
208 pdFALSE, /* This is a one shot timer, so xAutoReload is set to pdFALSE. */
\r
209 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
210 prvButtonLEDTimerCallback /* The callback function that switches the LED off. */
\r
213 /* Start the tasks and timer running. */
\r
214 vTaskStartScheduler();
\r
217 /* If all is well, the scheduler will now be running, and the following line
\r
218 will never be reached. If the following line does execute, then there was
\r
219 insufficient FreeRTOS heap memory available for the idle and/or timer tasks
\r
220 to be created. See the memory management section on the FreeRTOS web site
\r
221 for more details. */
\r
224 /*-----------------------------------------------------------*/
\r
226 static void prvButtonLEDTimerCallback( xTimerHandle xTimer )
\r
228 /* The timer has expired - so no button pushes have occurred in the last
\r
229 five seconds - turn the LED off. */
\r
230 GPIOA_PSOR = mainTIMER_CONTROLLED_LED;
\r
232 /*-----------------------------------------------------------*/
\r
234 /* The ISR executed when the user button is pushed. */
\r
235 void vPort_E_ISRHandler( void )
\r
237 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
239 /* The button was pushed, so ensure the LED is on before resetting the
\r
240 LED timer. The LED timer will turn the LED off if the button is not
\r
241 pushed within 5000ms. */
\r
242 GPIOA_PCOR = mainTIMER_CONTROLLED_LED;
\r
244 /* This interrupt safe FreeRTOS function can be called from this interrupt
\r
245 because the interrupt priority is below the
\r
246 configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */
\r
247 xTimerResetFromISR( xButtonLEDTimer, &xHigherPriorityTaskWoken );
\r
249 /* Clear the interrupt before leaving. */
\r
250 PORTE_ISFR = 0xFFFFFFFFUL;
\r
252 /* If calling xTimerResetFromISR() caused a task (in this case the timer
\r
253 service/daemon task) to unblock, and the unblocked task has a priority
\r
254 higher than or equal to the task that was interrupted, then
\r
255 xHigherPriorityTaskWoken will now be set to pdTRUE, and calling
\r
256 portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */
\r
257 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
259 /*-----------------------------------------------------------*/
\r
261 static void prvQueueSendTask( void *pvParameters )
\r
263 portTickType xNextWakeTime;
\r
264 const unsigned long ulValueToSend = 100UL;
\r
266 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
267 xNextWakeTime = xTaskGetTickCount();
\r
271 /* Place this task in the blocked state until it is time to run again.
\r
272 The block time is specified in ticks, the constant used converts ticks
\r
273 to ms. While in the Blocked state this task will not consume any CPU
\r
275 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
\r
277 /* Send to the queue - causing the queue receive task to unblock and
\r
278 toggle an LED. 0 is used as the block time so the sending operation
\r
279 will not block - it shouldn't need to block as the queue should always
\r
280 be empty at this point in the code. */
\r
281 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );
\r
284 /*-----------------------------------------------------------*/
\r
286 static void prvQueueReceiveTask( void *pvParameters )
\r
288 unsigned long ulReceivedValue;
\r
292 /* Wait until something arrives in the queue - this task will block
\r
293 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
\r
294 FreeRTOSConfig.h. */
\r
295 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
\r
297 /* To get here something must have been received from the queue, but
\r
298 is it the expected value? If it is, toggle the LED. */
\r
299 if( ulReceivedValue == 100UL )
\r
301 GPIOA_PTOR = mainTASK_CONTROLLED_LED;
\r
305 /*-----------------------------------------------------------*/
\r
307 static void prvSetupHardware( void )
\r
309 /* Enable the interrupt on SW1. */
\r
310 PORTE_PCR26 = PORT_PCR_MUX( 1 ) | PORT_PCR_IRQC( 0xA ) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
\r
311 enable_irq( mainGPIO_E_VECTOR );
\r
313 /* The interrupt calls an interrupt safe API function - so its priority must
\r
314 be equal to or lower than configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY. */
\r
315 set_irq_priority( mainGPIO_E_VECTOR, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
317 /* Set PTA10, PTA11, PTA28, and PTA29 (connected to LED's) for GPIO
\r
319 PORTA_PCR10 = ( 0 | PORT_PCR_MUX( 1 ) );
\r
320 PORTA_PCR11 = ( 0 | PORT_PCR_MUX( 1 ) );
\r
321 PORTA_PCR28 = ( 0 | PORT_PCR_MUX( 1 ) );
\r
322 PORTA_PCR29 = ( 0 | PORT_PCR_MUX( 1 ) );
\r
324 /* Change PTA10, PTA29 to outputs. */
\r
325 GPIOA_PDDR=GPIO_PDDR_PDD( mainTASK_CONTROLLED_LED | mainTIMER_CONTROLLED_LED );
\r
327 /* Start with LEDs off. */
\r
330 /*-----------------------------------------------------------*/
\r
332 void vApplicationMallocFailedHook( void )
\r
334 /* Called if a call to pvPortMalloc() fails because there is insufficient
\r
335 free memory available in the FreeRTOS heap. pvPortMalloc() is called
\r
336 internally by FreeRTOS API functions that create tasks, queues, software
\r
337 timers, and semaphores. The size of the FreeRTOS heap is set by the
\r
338 configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
\r
339 taskDISABLE_INTERRUPTS();
\r
342 /*-----------------------------------------------------------*/
\r
344 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
346 ( void ) pcTaskName;
\r
349 /* Run time stack overflow checking is performed if
\r
350 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
351 function is called if a stack overflow is detected. */
\r
352 taskDISABLE_INTERRUPTS();
\r
355 /*-----------------------------------------------------------*/
\r
357 void vApplicationIdleHook( void )
\r
359 volatile size_t xFreeHeapSpace;
\r
361 /* This function is called on each cycle of the idle task. In this case it
\r
362 does nothing useful, other than report the amount of FreeRTOS heap that
\r
363 remains unallocated. */
\r
364 xFreeHeapSpace = xPortGetFreeHeapSize();
\r
366 if( xFreeHeapSpace > 100 )
\r
368 /* By now, the kernel has allocated everything it is going to, so
\r
369 if there is a lot of heap remaining unallocated then
\r
370 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be
\r
371 reduced accordingly. */
\r
374 /*-----------------------------------------------------------*/
\r
376 /* The Blinky build configuration does not include Ethernet functionality,
\r
377 however, the Full and Blinky build configurations share a vectors.h header file.
\r
378 Therefore, dummy Ethernet interrupt handers need to be defined to keep the
\r
380 void vEMAC_TxISRHandler( void ) {}
\r
381 void vEMAC_RxISRHandler( void ){}
\r
382 void vEMAC_ErrorISRHandler( void ) {}
\r
384 /* The Blinky build configuration does not include run time stats gathering,
\r
385 however, the Full and Blinky build configurations share a FreeRTOSConfig.h
\r
386 file. Therefore, dummy run time stats functions need to be defined to keep the
\r
388 void vMainConfigureTimerForRunTimeStats( void ) {}
\r
389 unsigned long ulMainGetRunTimeCounterValue( void ) { return 0UL; }
\r
391 /* A tick hook is used by the "Full" build configuration. The Full and blinky
\r
392 build configurations share a FreeRTOSConfig.h header file, so this simple build
\r
393 configuration also has to define a tick hook - even though it does not actually
\r
394 use it for anything. */
\r
395 void vApplicationTickHook( void ) {}
\r