]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_Kinetis_K60_Tower_IAR/main_blinky.c
c53db911053823f5882f25a291749604d55bf482
[freertos] / FreeRTOS / Demo / CORTEX_Kinetis_K60_Tower_IAR / main_blinky.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * main-blinky.c is included when the "Blinky" build configuration is used.\r
30  * main-full.c is included when the "Full" build configuration is used.\r
31  *\r
32  * main-blinky.c (this file) defines a very simple demo that creates two tasks,\r
33  * one queue, and one timer.  It also demonstrates how Cortex-M3 interrupts can\r
34  * interact with FreeRTOS tasks/timers.\r
35  *\r
36  * This simple demo project runs 'stand alone' (without the rest of the tower\r
37  * system) on the TWR-K60N512 tower module, which is populated with a K60N512\r
38  * Cortex-M4 microcontroller.\r
39  *\r
40  * The idle hook function:\r
41  * The idle hook function demonstrates how to query the amount of FreeRTOS heap\r
42  * space that is remaining (see vApplicationIdleHook() defined in this file).\r
43  *\r
44  * The main() Function:\r
45  * main() creates one software timer, one queue, and two tasks.  It then starts\r
46  * the scheduler.\r
47  *\r
48  * The Queue Send Task:\r
49  * The queue send task is implemented by the prvQueueSendTask() function in\r
50  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
51  * block for 200 milliseconds, before sending the value 100 to the queue that\r
52  * was created within main().  Once the value is sent, the task loops back\r
53  * around to block for another 200 milliseconds.\r
54  *\r
55  * The Queue Receive Task:\r
56  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
57  * in this file.  prvQueueReceiveTask() sits in a loop that causes it to\r
58  * repeatedly attempt to read data from the queue that was created within\r
59  * main().  When data is received, the task checks the value of the data, and\r
60  * if the value equals the expected 100, toggles the blue LED.  The 'block\r
61  * time' parameter passed to the queue receive function specifies that the task\r
62  * should be held in the Blocked state indefinitely to wait for data to be\r
63  * available on the queue.  The queue receive task will only leave the Blocked\r
64  * state when the queue send task writes to the queue.  As the queue send task\r
65  * writes to the queue every 200 milliseconds, the queue receive task leaves the\r
66  * Blocked state every 200 milliseconds, and therefore toggles the blue LED\r
67  * every 200 milliseconds.\r
68  *\r
69  * The LED Software Timer and the Button Interrupt:\r
70  * The user button SW2 is configured to generate an interrupt each time it is\r
71  * pressed.  The interrupt service routine switches the green LED on, and\r
72  * resets the LED software timer.  The LED timer has a 5000 millisecond (5\r
73  * second) period, and uses a callback function that is defined to just turn the\r
74  * LED off again.  Therefore, pressing the user button will turn the LED on, and\r
75  * the LED will remain on until a full five seconds pass without the button\r
76  * being pressed.\r
77  */\r
78 \r
79 /* Kernel includes. */\r
80 #include "FreeRTOS.h"\r
81 #include "task.h"\r
82 #include "queue.h"\r
83 #include "timers.h"\r
84 \r
85 /* Freescale includes. */\r
86 #include "common.h"\r
87 \r
88 /* Priorities at which the tasks are created. */\r
89 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
90 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
91 \r
92 /* The rate at which data is sent to the queue, specified in milliseconds, and\r
93 converted to ticks using the portTICK_PERIOD_MS constant. */\r
94 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_PERIOD_MS )\r
95 \r
96 /* The LED will remain on until the button has not been pushed for a full\r
97 5000ms. */\r
98 #define mainBUTTON_LED_TIMER_PERIOD_MS          ( 5000UL / portTICK_PERIOD_MS )\r
99 \r
100 /* The number of items the queue can hold.  This is 1 as the receive task\r
101 will remove items as they are added, meaning the send task should always find\r
102 the queue empty. */\r
103 #define mainQUEUE_LENGTH                                        ( 1 )\r
104 \r
105 /* The LED toggle by the queue receive task (blue). */\r
106 #define mainTASK_CONTROLLED_LED                         ( 1UL << 10UL )\r
107 \r
108 /* The LED turned on by the button interrupt, and turned off by the LED timer\r
109 (green). */\r
110 #define mainTIMER_CONTROLLED_LED                        ( 1UL << 29UL )\r
111 \r
112 /* The vector used by the GPIO port E.  Button SW2 is configured to generate\r
113 an interrupt on this port. */\r
114 #define mainGPIO_E_VECTOR                                       ( 91 )\r
115 \r
116 /* A block time of zero simply means "don't block". */\r
117 #define mainDONT_BLOCK                                          ( 0UL )\r
118 \r
119 /*-----------------------------------------------------------*/\r
120 \r
121 /*\r
122  * Setup the NVIC, LED outputs, and button inputs.\r
123  */\r
124 static void prvSetupHardware( void );\r
125 \r
126 /*\r
127  * The tasks as described in the comments at the top of this file.\r
128  */\r
129 static void prvQueueReceiveTask( void *pvParameters );\r
130 static void prvQueueSendTask( void *pvParameters );\r
131 \r
132 /*\r
133  * The LED timer callback function.  This does nothing but switch off the\r
134  * LED defined by the mainTIMER_CONTROLLED_LED constant.\r
135  */\r
136 static void prvButtonLEDTimerCallback( TimerHandle_t xTimer );\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 \r
140 /* The queue used by both tasks. */\r
141 static QueueHandle_t xQueue = NULL;\r
142 \r
143 /* The LED software timer.  This uses prvButtonLEDTimerCallback() as its callback\r
144 function. */\r
145 static TimerHandle_t xButtonLEDTimer = NULL;\r
146 \r
147 /*-----------------------------------------------------------*/\r
148 \r
149 void main( void )\r
150 {\r
151         /* Configure the NVIC, LED outputs and button inputs. */\r
152         prvSetupHardware();\r
153 \r
154         /* Create the queue. */\r
155         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
156 \r
157         if( xQueue != NULL )\r
158         {\r
159                 /* Start the two tasks as described in the comments at the top of this\r
160                 file. */\r
161                 xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
162                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
163 \r
164                 /* Create the software timer that is responsible for turning off the LED\r
165                 if the button is not pushed within 5000ms, as described at the top of\r
166                 this file. */\r
167                 xButtonLEDTimer = xTimerCreate( "ButtonLEDTimer",                       /* A text name, purely to help debugging. */\r
168                                                                         mainBUTTON_LED_TIMER_PERIOD_MS, /* The timer period, in this case 5000ms (5s). */\r
169                                                                         pdFALSE,                                                /* This is a one shot timer, so xAutoReload is set to pdFALSE. */\r
170                                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
171                                                                         prvButtonLEDTimerCallback               /* The callback function that switches the LED off. */\r
172                                                                 );\r
173 \r
174                 /* Start the tasks and timer running. */\r
175                 vTaskStartScheduler();\r
176         }\r
177 \r
178         /* If all is well, the scheduler will now be running, and the following line\r
179         will never be reached.  If the following line does execute, then there was\r
180         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
181         to be created.  See the memory management section on the FreeRTOS web site\r
182         for more details. */\r
183         for( ;; );\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 static void prvButtonLEDTimerCallback( TimerHandle_t xTimer )\r
188 {\r
189         /* The timer has expired - so no button pushes have occurred in the last\r
190         five seconds - turn the LED off. */\r
191         GPIOA_PSOR = mainTIMER_CONTROLLED_LED;\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 /* The ISR executed when the user button is pushed. */\r
196 void vPort_E_ISRHandler( void )\r
197 {\r
198 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
199 \r
200         /* The button was pushed, so ensure the LED is on before resetting the\r
201         LED timer.  The LED timer will turn the LED off if the button is not\r
202         pushed within 5000ms. */\r
203         GPIOA_PCOR = mainTIMER_CONTROLLED_LED;\r
204 \r
205         /* This interrupt safe FreeRTOS function can be called from this interrupt\r
206         because the interrupt priority is below the\r
207         configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
208         xTimerResetFromISR( xButtonLEDTimer, &xHigherPriorityTaskWoken );\r
209 \r
210         /* Clear the interrupt before leaving. */\r
211         PORTE_ISFR = 0xFFFFFFFFUL;\r
212 \r
213         /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
214         service/daemon task) to unblock, and the unblocked task has a priority\r
215         higher than or equal to the task that was interrupted, then\r
216         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
217         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
218         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 static void prvQueueSendTask( void *pvParameters )\r
223 {\r
224 TickType_t xNextWakeTime;\r
225 const unsigned long ulValueToSend = 100UL;\r
226 \r
227         /* Initialise xNextWakeTime - this only needs to be done once. */\r
228         xNextWakeTime = xTaskGetTickCount();\r
229 \r
230         for( ;; )\r
231         {\r
232                 /* Place this task in the blocked state until it is time to run again.\r
233                 The block time is specified in ticks, the constant used converts ticks\r
234                 to ms.  While in the Blocked state this task will not consume any CPU\r
235                 time. */\r
236                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
237 \r
238                 /* Send to the queue - causing the queue receive task to unblock and\r
239                 toggle an LED.  0 is used as the block time so the sending operation\r
240                 will not block - it shouldn't need to block as the queue should always\r
241                 be empty at this point in the code. */\r
242                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );\r
243         }\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 static void prvQueueReceiveTask( void *pvParameters )\r
248 {\r
249 unsigned long ulReceivedValue;\r
250 \r
251         for( ;; )\r
252         {\r
253                 /* Wait until something arrives in the queue - this task will block\r
254                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
255                 FreeRTOSConfig.h. */\r
256                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
257 \r
258                 /*  To get here something must have been received from the queue, but\r
259                 is it the expected value?  If it is, toggle the LED. */\r
260                 if( ulReceivedValue == 100UL )\r
261                 {\r
262                     GPIOA_PTOR = mainTASK_CONTROLLED_LED;\r
263                 }\r
264         }\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 static void prvSetupHardware( void )\r
269 {\r
270         /* Enable the interrupt on SW1. */\r
271         PORTE_PCR26 = PORT_PCR_MUX( 1 ) | PORT_PCR_IRQC( 0xA ) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;\r
272         enable_irq( mainGPIO_E_VECTOR );\r
273 \r
274         /* The interrupt calls an interrupt safe API function - so its priority must\r
275         be equal to or lower than configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY. */\r
276         set_irq_priority( mainGPIO_E_VECTOR, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
277 \r
278         /* Set PTA10, PTA11, PTA28, and PTA29 (connected to LED's) for GPIO\r
279         functionality. */\r
280         PORTA_PCR10 = ( 0 | PORT_PCR_MUX( 1 ) );\r
281         PORTA_PCR11 = ( 0 | PORT_PCR_MUX( 1 ) );\r
282         PORTA_PCR28 = ( 0 | PORT_PCR_MUX( 1 ) );\r
283         PORTA_PCR29 = ( 0 | PORT_PCR_MUX( 1 ) );\r
284 \r
285         /* Change PTA10, PTA29 to outputs. */\r
286         GPIOA_PDDR=GPIO_PDDR_PDD( mainTASK_CONTROLLED_LED | mainTIMER_CONTROLLED_LED );\r
287 \r
288         /* Start with LEDs off. */\r
289         GPIOA_PTOR = ~0U;\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 void vApplicationMallocFailedHook( void )\r
294 {\r
295         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
296         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
297         internally by FreeRTOS API functions that create tasks, queues, software\r
298         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
299         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
300         taskDISABLE_INTERRUPTS();\r
301         for( ;; );\r
302 }\r
303 /*-----------------------------------------------------------*/\r
304 \r
305 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
306 {\r
307         ( void ) pcTaskName;\r
308         ( void ) pxTask;\r
309 \r
310         /* Run time stack overflow checking is performed if\r
311         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
312         function is called if a stack overflow is detected. */\r
313         taskDISABLE_INTERRUPTS();\r
314         for( ;; );\r
315 }\r
316 /*-----------------------------------------------------------*/\r
317 \r
318 void vApplicationIdleHook( void )\r
319 {\r
320 volatile size_t xFreeHeapSpace;\r
321 \r
322         /* This function is called on each cycle of the idle task.  In this case it\r
323         does nothing useful, other than report the amount of FreeRTOS heap that\r
324         remains unallocated. */\r
325         xFreeHeapSpace = xPortGetFreeHeapSize();\r
326 \r
327         if( xFreeHeapSpace > 100 )\r
328         {\r
329                 /* By now, the kernel has allocated everything it is going to, so\r
330                 if there is a lot of heap remaining unallocated then\r
331                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
332                 reduced accordingly. */\r
333         }\r
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \r
337 /* The Blinky build configuration does not include Ethernet functionality,\r
338 however, the Full and Blinky build configurations share a vectors.h header file.\r
339 Therefore, dummy Ethernet interrupt handers need to be defined to keep the\r
340 linker happy. */\r
341 void vEMAC_TxISRHandler( void ) {}\r
342 void vEMAC_RxISRHandler( void ){}\r
343 void vEMAC_ErrorISRHandler( void ) {}\r
344 \r
345 /* The Blinky build configuration does not include run time stats gathering,\r
346 however, the Full and Blinky build configurations share a FreeRTOSConfig.h\r
347 file.  Therefore, dummy run time stats functions need to be defined to keep the\r
348 linker happy. */\r
349 void vMainConfigureTimerForRunTimeStats( void ) {}\r
350 unsigned long ulMainGetRunTimeCounterValue( void ) { return 0UL; }\r
351 \r
352 /* A tick hook is used by the "Full" build configuration.  The Full and blinky\r
353 build configurations share a FreeRTOSConfig.h header file, so this simple build\r
354 configuration also has to define a tick hook - even though it does not actually\r
355 use it for anything. */\r
356 void vApplicationTickHook( void ) {}\r
357 \r
358 \r
359 \r
360 \r
361 \r
362 \r
363 \r