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