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