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