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