]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_Kinetis_K60_Tower_IAR/main_blinky.c
Update version numbers to V7.4.1.
[freertos] / FreeRTOS / Demo / CORTEX_Kinetis_K60_Tower_IAR / main_blinky.c
1 /*\r
2     FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * main-blinky.c is included when the "Blinky" build configuration is used.\r
77  * main-full.c is included when the "Full" build configuration is used.\r
78  *\r
79  * main-blinky.c (this file) defines a very simple demo that creates two tasks,\r
80  * one queue, and one timer.  It also demonstrates how Cortex-M3 interrupts can\r
81  * interact with FreeRTOS tasks/timers.\r
82  *\r
83  * This simple demo project runs 'stand alone' (without the rest of the tower\r
84  * system) on the TWR-K60N512 tower module, which is populated with a K60N512\r
85  * Cortex-M4 microcontroller.\r
86  *\r
87  * The idle hook function:\r
88  * The idle hook function demonstrates how to query the amount of FreeRTOS heap\r
89  * space that is remaining (see vApplicationIdleHook() defined in this file).\r
90  *\r
91  * The main() Function:\r
92  * main() creates one software timer, one queue, and two tasks.  It then starts\r
93  * the scheduler.\r
94  *\r
95  * The Queue Send Task:\r
96  * The queue send task is implemented by the prvQueueSendTask() function in\r
97  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
98  * block for 200 milliseconds, before sending the value 100 to the queue that\r
99  * was created within main().  Once the value is sent, the task loops back\r
100  * around to block for another 200 milliseconds.\r
101  *\r
102  * The Queue Receive Task:\r
103  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
104  * in this file.  prvQueueReceiveTask() sits in a loop that causes it to\r
105  * repeatedly attempt to read data from the queue that was created within\r
106  * main().  When data is received, the task checks the value of the data, and\r
107  * if the value equals the expected 100, toggles the blue LED.  The 'block \r
108  * time' parameter passed to the queue receive function specifies that the task \r
109  * should be held in the Blocked state indefinitely to wait for data to be \r
110  * available on the queue.  The queue receive task will only leave the Blocked \r
111  * state when the queue send task writes to the queue.  As the queue send task \r
112  * writes to the queue every 200 milliseconds, the queue receive task leaves the \r
113  * Blocked state every 200 milliseconds, and therefore toggles the blue LED \r
114  * every 200 milliseconds.\r
115  *\r
116  * The LED Software Timer and the Button Interrupt:\r
117  * The user button SW2 is configured to generate an interrupt each time it is\r
118  * pressed.  The interrupt service routine switches the green LED on, and \r
119  * resets the LED software timer.  The LED timer has a 5000 millisecond (5 \r
120  * second) period, and uses a callback function that is defined to just turn the \r
121  * LED off again.  Therefore, pressing the user button will turn the LED on, and \r
122  * the LED will remain on until a full five seconds pass without the button \r
123  * being pressed.\r
124  */\r
125 \r
126 /* Kernel includes. */\r
127 #include "FreeRTOS.h"\r
128 #include "task.h"\r
129 #include "queue.h"\r
130 #include "timers.h"\r
131 \r
132 /* Freescale includes. */\r
133 #include "common.h"\r
134 \r
135 /* Priorities at which the tasks are created. */\r
136 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
137 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
138 \r
139 /* The rate at which data is sent to the queue, specified in milliseconds, and\r
140 converted to ticks using the portTICK_RATE_MS constant. */\r
141 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_RATE_MS )\r
142 \r
143 /* The LED will remain on until the button has not been pushed for a full\r
144 5000ms. */\r
145 #define mainBUTTON_LED_TIMER_PERIOD_MS          ( 5000UL / portTICK_RATE_MS )\r
146 \r
147 /* The number of items the queue can hold.  This is 1 as the receive task\r
148 will remove items as they are added, meaning the send task should always find\r
149 the queue empty. */\r
150 #define mainQUEUE_LENGTH                                        ( 1 )\r
151 \r
152 /* The LED toggle by the queue receive task (blue). */\r
153 #define mainTASK_CONTROLLED_LED                         ( 1UL << 10UL )\r
154 \r
155 /* The LED turned on by the button interrupt, and turned off by the LED timer\r
156 (green). */\r
157 #define mainTIMER_CONTROLLED_LED                        ( 1UL << 29UL )\r
158 \r
159 /* The vector used by the GPIO port E.  Button SW2 is configured to generate\r
160 an interrupt on this port. */\r
161 #define mainGPIO_E_VECTOR                                       ( 91 )\r
162 \r
163 /* A block time of zero simply means "don't block". */\r
164 #define mainDONT_BLOCK                                          ( 0UL )\r
165 \r
166 /*-----------------------------------------------------------*/\r
167 \r
168 /*\r
169  * Setup the NVIC, LED outputs, and button inputs.\r
170  */\r
171 static void prvSetupHardware( void );\r
172 \r
173 /*\r
174  * The tasks as described in the comments at the top of this file.\r
175  */\r
176 static void prvQueueReceiveTask( void *pvParameters );\r
177 static void prvQueueSendTask( void *pvParameters );\r
178 \r
179 /*\r
180  * The LED timer callback function.  This does nothing but switch off the\r
181  * LED defined by the mainTIMER_CONTROLLED_LED constant.\r
182  */\r
183 static void prvButtonLEDTimerCallback( xTimerHandle xTimer );\r
184 \r
185 /*-----------------------------------------------------------*/\r
186 \r
187 /* The queue used by both tasks. */\r
188 static xQueueHandle xQueue = NULL;\r
189 \r
190 /* The LED software timer.  This uses prvButtonLEDTimerCallback() as its callback\r
191 function. */\r
192 static xTimerHandle xButtonLEDTimer = NULL;\r
193 \r
194 /*-----------------------------------------------------------*/\r
195 \r
196 void main( void )\r
197 {\r
198         /* Configure the NVIC, LED outputs and button inputs. */\r
199         prvSetupHardware();\r
200 \r
201         /* Create the queue. */\r
202         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
203 \r
204         if( xQueue != NULL )\r
205         {\r
206                 /* Start the two tasks as described in the comments at the top of this\r
207                 file. */\r
208                 xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
209                 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
210 \r
211                 /* Create the software timer that is responsible for turning off the LED\r
212                 if the button is not pushed within 5000ms, as described at the top of\r
213                 this file. */\r
214                 xButtonLEDTimer = xTimerCreate( ( const signed char * ) "ButtonLEDTimer", /* A text name, purely to help debugging. */\r
215                                                                         mainBUTTON_LED_TIMER_PERIOD_MS,                 /* The timer period, in this case 5000ms (5s). */\r
216                                                                         pdFALSE,                                                                /* This is a one shot timer, so xAutoReload is set to pdFALSE. */\r
217                                                                         ( void * ) 0,                                                   /* The ID is not used, so can be set to anything. */\r
218                                                                         prvButtonLEDTimerCallback                               /* The callback function that switches the LED off. */\r
219                                                                 );\r
220 \r
221                 /* Start the tasks and timer running. */\r
222                 vTaskStartScheduler();\r
223         }\r
224 \r
225         /* If all is well, the scheduler will now be running, and the following line\r
226         will never be reached.  If the following line does execute, then there was\r
227         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
228         to be created.  See the memory management section on the FreeRTOS web site\r
229         for more details. */\r
230         for( ;; );\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 static void prvButtonLEDTimerCallback( xTimerHandle xTimer )\r
235 {\r
236         /* The timer has expired - so no button pushes have occurred in the last\r
237         five seconds - turn the LED off. */\r
238         GPIOA_PSOR = mainTIMER_CONTROLLED_LED;\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 /* The ISR executed when the user button is pushed. */\r
243 void vPort_E_ISRHandler( void )\r
244 {\r
245 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
246 \r
247         /* The button was pushed, so ensure the LED is on before resetting the\r
248         LED timer.  The LED timer will turn the LED off if the button is not\r
249         pushed within 5000ms. */\r
250         GPIOA_PCOR = mainTIMER_CONTROLLED_LED;\r
251 \r
252         /* This interrupt safe FreeRTOS function can be called from this interrupt\r
253         because the interrupt priority is below the\r
254         configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
255         xTimerResetFromISR( xButtonLEDTimer, &xHigherPriorityTaskWoken );\r
256 \r
257         /* Clear the interrupt before leaving. */\r
258         PORTE_ISFR = 0xFFFFFFFFUL;\r
259 \r
260         /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
261         service/daemon task) to unblock, and the unblocked task has a priority\r
262         higher than or equal to the task that was interrupted, then\r
263         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
264         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
265         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
266 }\r
267 /*-----------------------------------------------------------*/\r
268 \r
269 static void prvQueueSendTask( void *pvParameters )\r
270 {\r
271 portTickType xNextWakeTime;\r
272 const unsigned long ulValueToSend = 100UL;\r
273 \r
274         /* Initialise xNextWakeTime - this only needs to be done once. */\r
275         xNextWakeTime = xTaskGetTickCount();\r
276 \r
277         for( ;; )\r
278         {\r
279                 /* Place this task in the blocked state until it is time to run again.\r
280                 The block time is specified in ticks, the constant used converts ticks\r
281                 to ms.  While in the Blocked state this task will not consume any CPU\r
282                 time. */\r
283                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
284 \r
285                 /* Send to the queue - causing the queue receive task to unblock and\r
286                 toggle an LED.  0 is used as the block time so the sending operation\r
287                 will not block - it shouldn't need to block as the queue should always\r
288                 be empty at this point in the code. */\r
289                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );\r
290         }\r
291 }\r
292 /*-----------------------------------------------------------*/\r
293 \r
294 static void prvQueueReceiveTask( void *pvParameters )\r
295 {\r
296 unsigned long ulReceivedValue;\r
297 \r
298         for( ;; )\r
299         {\r
300                 /* Wait until something arrives in the queue - this task will block\r
301                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
302                 FreeRTOSConfig.h. */\r
303                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
304 \r
305                 /*  To get here something must have been received from the queue, but\r
306                 is it the expected value?  If it is, toggle the LED. */\r
307                 if( ulReceivedValue == 100UL )\r
308                 {\r
309                     GPIOA_PTOR = mainTASK_CONTROLLED_LED;\r
310                 }\r
311         }\r
312 }\r
313 /*-----------------------------------------------------------*/\r
314 \r
315 static void prvSetupHardware( void )\r
316 {\r
317         /* Enable the interrupt on SW1. */\r
318         PORTE_PCR26 = PORT_PCR_MUX( 1 ) | PORT_PCR_IRQC( 0xA ) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;\r
319         enable_irq( mainGPIO_E_VECTOR );\r
320         \r
321         /* The interrupt calls an interrupt safe API function - so its priority must\r
322         be equal to or lower than configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY. */\r
323         set_irq_priority( mainGPIO_E_VECTOR, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
324         \r
325         /* Set PTA10, PTA11, PTA28, and PTA29 (connected to LED's) for GPIO\r
326         functionality. */\r
327         PORTA_PCR10 = ( 0 | PORT_PCR_MUX( 1 ) );\r
328         PORTA_PCR11 = ( 0 | PORT_PCR_MUX( 1 ) );\r
329         PORTA_PCR28 = ( 0 | PORT_PCR_MUX( 1 ) );\r
330         PORTA_PCR29 = ( 0 | PORT_PCR_MUX( 1 ) );\r
331         \r
332         /* Change PTA10, PTA29 to outputs. */\r
333         GPIOA_PDDR=GPIO_PDDR_PDD( mainTASK_CONTROLLED_LED | mainTIMER_CONTROLLED_LED ); \r
334 \r
335         /* Start with LEDs off. */\r
336         GPIOA_PTOR = ~0U;\r
337 }\r
338 /*-----------------------------------------------------------*/\r
339 \r
340 void vApplicationMallocFailedHook( void )\r
341 {\r
342         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
343         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
344         internally by FreeRTOS API functions that create tasks, queues, software\r
345         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
346         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
347         taskDISABLE_INTERRUPTS();\r
348         for( ;; );\r
349 }\r
350 /*-----------------------------------------------------------*/\r
351 \r
352 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
353 {\r
354         ( void ) pcTaskName;\r
355         ( void ) pxTask;\r
356 \r
357         /* Run time stack overflow checking is performed if\r
358         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
359         function is called if a stack overflow is detected. */\r
360         taskDISABLE_INTERRUPTS();\r
361         for( ;; );\r
362 }\r
363 /*-----------------------------------------------------------*/\r
364 \r
365 void vApplicationIdleHook( void )\r
366 {\r
367 volatile size_t xFreeHeapSpace;\r
368 \r
369         /* This function is called on each cycle of the idle task.  In this case it\r
370         does nothing useful, other than report the amount of FreeRTOS heap that\r
371         remains unallocated. */\r
372         xFreeHeapSpace = xPortGetFreeHeapSize();\r
373 \r
374         if( xFreeHeapSpace > 100 )\r
375         {\r
376                 /* By now, the kernel has allocated everything it is going to, so\r
377                 if there is a lot of heap remaining unallocated then\r
378                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
379                 reduced accordingly. */\r
380         }\r
381 }\r
382 /*-----------------------------------------------------------*/\r
383 \r
384 /* The Blinky build configuration does not include Ethernet functionality,\r
385 however, the Full and Blinky build configurations share a vectors.h header file.\r
386 Therefore, dummy Ethernet interrupt handers need to be defined to keep the\r
387 linker happy. */\r
388 void vEMAC_TxISRHandler( void ) {}\r
389 void vEMAC_RxISRHandler( void ){}\r
390 void vEMAC_ErrorISRHandler( void ) {}\r
391 \r
392 /* The Blinky build configuration does not include run time stats gathering,\r
393 however, the Full and Blinky build configurations share a FreeRTOSConfig.h\r
394 file.  Therefore, dummy run time stats functions need to be defined to keep the\r
395 linker happy. */\r
396 void vMainConfigureTimerForRunTimeStats( void ) {}\r
397 unsigned long ulMainGetRunTimeCounterValue( void ) { return 0UL; }\r
398 \r
399 /* A tick hook is used by the "Full" build configuration.  The Full and blinky \r
400 build configurations share a FreeRTOSConfig.h header file, so this simple build \r
401 configuration also has to define a tick hook - even though it does not actually \r
402 use it for anything. */\r
403 void vApplicationTickHook( void ) {}\r
404 \r
405 \r
406 \r
407 \r
408 \r
409 \r
410 \r