]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_Kinetis_K60_Tower_IAR/main_blinky.c
Demo code only:
[freertos] / FreeRTOS / Demo / CORTEX_Kinetis_K60_Tower_IAR / main_blinky.c
1 /*\r
2     FreeRTOS V8.1.1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\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
12      *                                                                       *\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
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \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
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \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
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \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
54 \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
58 \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
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\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
69  *\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
73  *\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
77  *\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
81  *\r
82  * The main() Function:\r
83  * main() creates one software timer, one queue, and two tasks.  It then starts\r
84  * the scheduler.\r
85  *\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
92  *\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
106  *\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
114  * being pressed.\r
115  */\r
116 \r
117 /* Kernel includes. */\r
118 #include "FreeRTOS.h"\r
119 #include "task.h"\r
120 #include "queue.h"\r
121 #include "timers.h"\r
122 \r
123 /* Freescale includes. */\r
124 #include "common.h"\r
125 \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
129 \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
133 \r
134 /* The LED will remain on until the button has not been pushed for a full\r
135 5000ms. */\r
136 #define mainBUTTON_LED_TIMER_PERIOD_MS          ( 5000UL / portTICK_PERIOD_MS )\r
137 \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
142 \r
143 /* The LED toggle by the queue receive task (blue). */\r
144 #define mainTASK_CONTROLLED_LED                         ( 1UL << 10UL )\r
145 \r
146 /* The LED turned on by the button interrupt, and turned off by the LED timer\r
147 (green). */\r
148 #define mainTIMER_CONTROLLED_LED                        ( 1UL << 29UL )\r
149 \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
153 \r
154 /* A block time of zero simply means "don't block". */\r
155 #define mainDONT_BLOCK                                          ( 0UL )\r
156 \r
157 /*-----------------------------------------------------------*/\r
158 \r
159 /*\r
160  * Setup the NVIC, LED outputs, and button inputs.\r
161  */\r
162 static void prvSetupHardware( void );\r
163 \r
164 /*\r
165  * The tasks as described in the comments at the top of this file.\r
166  */\r
167 static void prvQueueReceiveTask( void *pvParameters );\r
168 static void prvQueueSendTask( void *pvParameters );\r
169 \r
170 /*\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
173  */\r
174 static void prvButtonLEDTimerCallback( TimerHandle_t xTimer );\r
175 \r
176 /*-----------------------------------------------------------*/\r
177 \r
178 /* The queue used by both tasks. */\r
179 static QueueHandle_t xQueue = NULL;\r
180 \r
181 /* The LED software timer.  This uses prvButtonLEDTimerCallback() as its callback\r
182 function. */\r
183 static TimerHandle_t xButtonLEDTimer = NULL;\r
184 \r
185 /*-----------------------------------------------------------*/\r
186 \r
187 void main( void )\r
188 {\r
189         /* Configure the NVIC, LED outputs and button inputs. */\r
190         prvSetupHardware();\r
191 \r
192         /* Create the queue. */\r
193         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
194 \r
195         if( xQueue != NULL )\r
196         {\r
197                 /* Start the two tasks as described in the comments at the top of this\r
198                 file. */\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
201 \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
204                 this file. */\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
210                                                                 );\r
211 \r
212                 /* Start the tasks and timer running. */\r
213                 vTaskStartScheduler();\r
214         }\r
215 \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
221         for( ;; );\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 static void prvButtonLEDTimerCallback( TimerHandle_t xTimer )\r
226 {\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
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 /* The ISR executed when the user button is pushed. */\r
234 void vPort_E_ISRHandler( void )\r
235 {\r
236 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
237 \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
242 \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
247 \r
248         /* Clear the interrupt before leaving. */\r
249         PORTE_ISFR = 0xFFFFFFFFUL;\r
250 \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
257 }\r
258 /*-----------------------------------------------------------*/\r
259 \r
260 static void prvQueueSendTask( void *pvParameters )\r
261 {\r
262 TickType_t xNextWakeTime;\r
263 const unsigned long ulValueToSend = 100UL;\r
264 \r
265         /* Initialise xNextWakeTime - this only needs to be done once. */\r
266         xNextWakeTime = xTaskGetTickCount();\r
267 \r
268         for( ;; )\r
269         {\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
273                 time. */\r
274                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
275 \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
281         }\r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 static void prvQueueReceiveTask( void *pvParameters )\r
286 {\r
287 unsigned long ulReceivedValue;\r
288 \r
289         for( ;; )\r
290         {\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
295 \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
299                 {\r
300                     GPIOA_PTOR = mainTASK_CONTROLLED_LED;\r
301                 }\r
302         }\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 static void prvSetupHardware( void )\r
307 {\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
311 \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
315 \r
316         /* Set PTA10, PTA11, PTA28, and PTA29 (connected to LED's) for GPIO\r
317         functionality. */\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
322 \r
323         /* Change PTA10, PTA29 to outputs. */\r
324         GPIOA_PDDR=GPIO_PDDR_PDD( mainTASK_CONTROLLED_LED | mainTIMER_CONTROLLED_LED );\r
325 \r
326         /* Start with LEDs off. */\r
327         GPIOA_PTOR = ~0U;\r
328 }\r
329 /*-----------------------------------------------------------*/\r
330 \r
331 void vApplicationMallocFailedHook( void )\r
332 {\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
339         for( ;; );\r
340 }\r
341 /*-----------------------------------------------------------*/\r
342 \r
343 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
344 {\r
345         ( void ) pcTaskName;\r
346         ( void ) pxTask;\r
347 \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
352         for( ;; );\r
353 }\r
354 /*-----------------------------------------------------------*/\r
355 \r
356 void vApplicationIdleHook( void )\r
357 {\r
358 volatile size_t xFreeHeapSpace;\r
359 \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
364 \r
365         if( xFreeHeapSpace > 100 )\r
366         {\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
371         }\r
372 }\r
373 /*-----------------------------------------------------------*/\r
374 \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
378 linker happy. */\r
379 void vEMAC_TxISRHandler( void ) {}\r
380 void vEMAC_RxISRHandler( void ){}\r
381 void vEMAC_ErrorISRHandler( void ) {}\r
382 \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
386 linker happy. */\r
387 void vMainConfigureTimerForRunTimeStats( void ) {}\r
388 unsigned long ulMainGetRunTimeCounterValue( void ) { return 0UL; }\r
389 \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
395 \r
396 \r
397 \r
398 \r
399 \r
400 \r
401 \r