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 * 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 'stand alone' (without the rest of the tower
\r
75 * system) on the TWR-K60N512 tower module, which is populated with a K60N512
\r
76 * Cortex-M4 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 blue 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 the
\r
104 * Blocked state every 200 milliseconds, and therefore toggles the blue LED
\r
105 * every 200 milliseconds.
\r
107 * The LED Software Timer and the Button Interrupt:
\r
108 * The user button SW2 is configured to generate an interrupt each time it is
\r
109 * pressed. The interrupt service routine switches the green LED on, and
\r
110 * resets the LED software timer. The LED timer has a 5000 millisecond (5
\r
111 * second) period, and uses a callback function that is defined to just turn the
\r
112 * LED off again. Therefore, pressing the user button will turn the LED on, and
\r
113 * the LED will remain on until a full five seconds pass without the button
\r
117 /* Kernel includes. */
\r
118 #include "FreeRTOS.h"
\r
121 #include "timers.h"
\r
123 /* Freescale includes. */
\r
124 #include "common.h"
\r
126 /* Priorities at which the tasks are created. */
\r
127 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
128 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
130 /* The rate at which data is sent to the queue, specified in milliseconds, and
\r
131 converted to ticks using the portTICK_PERIOD_MS constant. */
\r
132 #define mainQUEUE_SEND_FREQUENCY_MS ( 200 / portTICK_PERIOD_MS )
\r
134 /* The LED will remain on until the button has not been pushed for a full
\r
136 #define mainBUTTON_LED_TIMER_PERIOD_MS ( 5000UL / portTICK_PERIOD_MS )
\r
138 /* The number of items the queue can hold. This is 1 as the receive task
\r
139 will remove items as they are added, meaning the send task should always find
\r
140 the queue empty. */
\r
141 #define mainQUEUE_LENGTH ( 1 )
\r
143 /* The LED toggle by the queue receive task (blue). */
\r
144 #define mainTASK_CONTROLLED_LED ( 1UL << 10UL )
\r
146 /* The LED turned on by the button interrupt, and turned off by the LED timer
\r
148 #define mainTIMER_CONTROLLED_LED ( 1UL << 29UL )
\r
150 /* The vector used by the GPIO port E. Button SW2 is configured to generate
\r
151 an interrupt on this port. */
\r
152 #define mainGPIO_E_VECTOR ( 91 )
\r
154 /* A block time of zero simply means "don't block". */
\r
155 #define mainDONT_BLOCK ( 0UL )
\r
157 /*-----------------------------------------------------------*/
\r
160 * Setup the NVIC, LED outputs, and button inputs.
\r
162 static void prvSetupHardware( void );
\r
165 * The tasks as described in the comments at the top of this file.
\r
167 static void prvQueueReceiveTask( void *pvParameters );
\r
168 static void prvQueueSendTask( void *pvParameters );
\r
171 * The LED timer callback function. This does nothing but switch off the
\r
172 * LED defined by the mainTIMER_CONTROLLED_LED constant.
\r
174 static void prvButtonLEDTimerCallback( TimerHandle_t xTimer );
\r
176 /*-----------------------------------------------------------*/
\r
178 /* The queue used by both tasks. */
\r
179 static QueueHandle_t xQueue = NULL;
\r
181 /* The LED software timer. This uses prvButtonLEDTimerCallback() as its callback
\r
183 static TimerHandle_t xButtonLEDTimer = NULL;
\r
185 /*-----------------------------------------------------------*/
\r
189 /* Configure the NVIC, LED outputs and button inputs. */
\r
190 prvSetupHardware();
\r
192 /* Create the queue. */
\r
193 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
\r
195 if( xQueue != NULL )
\r
197 /* Start the two tasks as described in the comments at the top of this
\r
199 xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );
\r
200 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
\r
202 /* Create the software timer that is responsible for turning off the LED
\r
203 if the button is not pushed within 5000ms, as described at the top of
\r
205 xButtonLEDTimer = xTimerCreate( "ButtonLEDTimer", /* A text name, purely to help debugging. */
\r
206 mainBUTTON_LED_TIMER_PERIOD_MS, /* The timer period, in this case 5000ms (5s). */
\r
207 pdFALSE, /* This is a one shot timer, so xAutoReload is set to pdFALSE. */
\r
208 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
209 prvButtonLEDTimerCallback /* The callback function that switches the LED off. */
\r
212 /* Start the tasks and timer running. */
\r
213 vTaskStartScheduler();
\r
216 /* If all is well, the scheduler will now be running, and the following line
\r
217 will never be reached. If the following line does execute, then there was
\r
218 insufficient FreeRTOS heap memory available for the idle and/or timer tasks
\r
219 to be created. See the memory management section on the FreeRTOS web site
\r
220 for more details. */
\r
223 /*-----------------------------------------------------------*/
\r
225 static void prvButtonLEDTimerCallback( TimerHandle_t xTimer )
\r
227 /* The timer has expired - so no button pushes have occurred in the last
\r
228 five seconds - turn the LED off. */
\r
229 GPIOA_PSOR = mainTIMER_CONTROLLED_LED;
\r
231 /*-----------------------------------------------------------*/
\r
233 /* The ISR executed when the user button is pushed. */
\r
234 void vPort_E_ISRHandler( void )
\r
236 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
238 /* The button was pushed, so ensure the LED is on before resetting the
\r
239 LED timer. The LED timer will turn the LED off if the button is not
\r
240 pushed within 5000ms. */
\r
241 GPIOA_PCOR = mainTIMER_CONTROLLED_LED;
\r
243 /* This interrupt safe FreeRTOS function can be called from this interrupt
\r
244 because the interrupt priority is below the
\r
245 configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */
\r
246 xTimerResetFromISR( xButtonLEDTimer, &xHigherPriorityTaskWoken );
\r
248 /* Clear the interrupt before leaving. */
\r
249 PORTE_ISFR = 0xFFFFFFFFUL;
\r
251 /* If calling xTimerResetFromISR() caused a task (in this case the timer
\r
252 service/daemon task) to unblock, and the unblocked task has a priority
\r
253 higher than or equal to the task that was interrupted, then
\r
254 xHigherPriorityTaskWoken will now be set to pdTRUE, and calling
\r
255 portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */
\r
256 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
258 /*-----------------------------------------------------------*/
\r
260 static void prvQueueSendTask( void *pvParameters )
\r
262 TickType_t xNextWakeTime;
\r
263 const unsigned long ulValueToSend = 100UL;
\r
265 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
266 xNextWakeTime = xTaskGetTickCount();
\r
270 /* Place this task in the blocked state until it is time to run again.
\r
271 The block time is specified in ticks, the constant used converts ticks
\r
272 to ms. While in the Blocked state this task will not consume any CPU
\r
274 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
\r
276 /* Send to the queue - causing the queue receive task to unblock and
\r
277 toggle an LED. 0 is used as the block time so the sending operation
\r
278 will not block - it shouldn't need to block as the queue should always
\r
279 be empty at this point in the code. */
\r
280 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );
\r
283 /*-----------------------------------------------------------*/
\r
285 static void prvQueueReceiveTask( void *pvParameters )
\r
287 unsigned long ulReceivedValue;
\r
291 /* Wait until something arrives in the queue - this task will block
\r
292 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
\r
293 FreeRTOSConfig.h. */
\r
294 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
\r
296 /* To get here something must have been received from the queue, but
\r
297 is it the expected value? If it is, toggle the LED. */
\r
298 if( ulReceivedValue == 100UL )
\r
300 GPIOA_PTOR = mainTASK_CONTROLLED_LED;
\r
304 /*-----------------------------------------------------------*/
\r
306 static void prvSetupHardware( void )
\r
308 /* Enable the interrupt on SW1. */
\r
309 PORTE_PCR26 = PORT_PCR_MUX( 1 ) | PORT_PCR_IRQC( 0xA ) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
\r
310 enable_irq( mainGPIO_E_VECTOR );
\r
312 /* The interrupt calls an interrupt safe API function - so its priority must
\r
313 be equal to or lower than configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY. */
\r
314 set_irq_priority( mainGPIO_E_VECTOR, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
\r
316 /* Set PTA10, PTA11, PTA28, and PTA29 (connected to LED's) for GPIO
\r
318 PORTA_PCR10 = ( 0 | PORT_PCR_MUX( 1 ) );
\r
319 PORTA_PCR11 = ( 0 | PORT_PCR_MUX( 1 ) );
\r
320 PORTA_PCR28 = ( 0 | PORT_PCR_MUX( 1 ) );
\r
321 PORTA_PCR29 = ( 0 | PORT_PCR_MUX( 1 ) );
\r
323 /* Change PTA10, PTA29 to outputs. */
\r
324 GPIOA_PDDR=GPIO_PDDR_PDD( mainTASK_CONTROLLED_LED | mainTIMER_CONTROLLED_LED );
\r
326 /* Start with LEDs off. */
\r
329 /*-----------------------------------------------------------*/
\r
331 void vApplicationMallocFailedHook( void )
\r
333 /* Called if a call to pvPortMalloc() fails because there is insufficient
\r
334 free memory available in the FreeRTOS heap. pvPortMalloc() is called
\r
335 internally by FreeRTOS API functions that create tasks, queues, software
\r
336 timers, and semaphores. The size of the FreeRTOS heap is set by the
\r
337 configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
\r
338 taskDISABLE_INTERRUPTS();
\r
341 /*-----------------------------------------------------------*/
\r
343 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
345 ( void ) pcTaskName;
\r
348 /* Run time stack overflow checking is performed if
\r
349 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
350 function is called if a stack overflow is detected. */
\r
351 taskDISABLE_INTERRUPTS();
\r
354 /*-----------------------------------------------------------*/
\r
356 void vApplicationIdleHook( void )
\r
358 volatile size_t xFreeHeapSpace;
\r
360 /* This function is called on each cycle of the idle task. In this case it
\r
361 does nothing useful, other than report the amount of FreeRTOS heap that
\r
362 remains unallocated. */
\r
363 xFreeHeapSpace = xPortGetFreeHeapSize();
\r
365 if( xFreeHeapSpace > 100 )
\r
367 /* By now, the kernel has allocated everything it is going to, so
\r
368 if there is a lot of heap remaining unallocated then
\r
369 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be
\r
370 reduced accordingly. */
\r
373 /*-----------------------------------------------------------*/
\r
375 /* The Blinky build configuration does not include Ethernet functionality,
\r
376 however, the Full and Blinky build configurations share a vectors.h header file.
\r
377 Therefore, dummy Ethernet interrupt handers need to be defined to keep the
\r
379 void vEMAC_TxISRHandler( void ) {}
\r
380 void vEMAC_RxISRHandler( void ){}
\r
381 void vEMAC_ErrorISRHandler( void ) {}
\r
383 /* The Blinky build configuration does not include run time stats gathering,
\r
384 however, the Full and Blinky build configurations share a FreeRTOSConfig.h
\r
385 file. Therefore, dummy run time stats functions need to be defined to keep the
\r
387 void vMainConfigureTimerForRunTimeStats( void ) {}
\r
388 unsigned long ulMainGetRunTimeCounterValue( void ) { return 0UL; }
\r
390 /* A tick hook is used by the "Full" build configuration. The Full and blinky
\r
391 build configurations share a FreeRTOSConfig.h header file, so this simple build
\r
392 configuration also has to define a tick hook - even though it does not actually
\r
393 use it for anything. */
\r
394 void vApplicationTickHook( void ) {}
\r