]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MB9A310_IAR_Keil/main_blinky.c
Ensure both one-shot and auto-reload are written consistently with a hyphen in comments.
[freertos] / FreeRTOS / Demo / CORTEX_MB9A310_IAR_Keil / 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 on the SK-FM3-64PMC1 evaluation board, which\r
37  * is populated with an MB9A314 microcontroller.\r
38  *\r
39  * The idle hook function:\r
40  * The idle hook function demonstrates how to query the amount of FreeRTOS heap\r
41  * space that is remaining (see vApplicationIdleHook() defined in this file).\r
42  *\r
43  * The main() Function:\r
44  * main() creates one software timer, one queue, and two tasks.  It then starts\r
45  * the scheduler.\r
46  *\r
47  * The Queue Send Task:\r
48  * The queue send task is implemented by the prvQueueSendTask() function in\r
49  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly\r
50  * block for 200 milliseconds, before sending the value 100 to the queue that\r
51  * was created within main().  Once the value is sent, the task loops back\r
52  * around to block for another 200 milliseconds.\r
53  *\r
54  * The Queue Receive Task:\r
55  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
56  * in this file.  prvQueueReceiveTask() sits in a loop that causes it to\r
57  * repeatedly attempt to read data from the queue that was created within\r
58  * main().  When data is received, the task checks the value of the data, and\r
59  * if the value equals the expected 100, toggles an LED on the 7 segment\r
60  * display.  The 'block time' parameter passed to the queue receive function\r
61  * specifies that the task should be held in the Blocked state indefinitely to\r
62  * wait for data to be available on the queue.  The queue receive task will only\r
63  * leave the Blocked state when the queue send task writes to the queue.  As the\r
64  * queue send task writes to the queue every 200 milliseconds, the queue receive\r
65  * task leaves the Blocked state every 200 milliseconds, and therefore toggles\r
66  * the LED every 200 milliseconds.\r
67  *\r
68  * The LED Software Timer and the Button Interrupt:\r
69  * The user button SW2 is configured to generate an interrupt each time it is\r
70  * pressed.  The interrupt service routine switches an LED in the 7 segment\r
71  * display on, and resets the LED software timer.  The LED timer has a 5000\r
72  * millisecond (5 second) period, and uses a callback function that is defined\r
73  * to just turn the LED off again.  Therefore, pressing the user button will\r
74  * turn the LED on, and the LED will remain on until a full five seconds pass\r
75  * without the button being pressed.\r
76  */\r
77 \r
78 /* Kernel includes. */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 #include "queue.h"\r
82 #include "timers.h"\r
83 \r
84 /* Fujitsu drivers/libraries. */\r
85 #include "mcu.h"\r
86 \r
87 /* Priorities at which the tasks are created. */\r
88 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
89 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
90 \r
91 /* The rate at which data is sent to the queue, specified in milliseconds, and\r
92 converted to ticks using the portTICK_PERIOD_MS constant. */\r
93 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_PERIOD_MS )\r
94 \r
95 /* The number of items the queue can hold.  This is 1 as the receive task\r
96 will remove items as they are added, meaning the send task should always find\r
97 the queue empty. */\r
98 #define mainQUEUE_LENGTH                                        ( 1 )\r
99 \r
100 /* The LED toggle by the queue receive task. */\r
101 #define mainTASK_CONTROLLED_LED                         ( 1UL << 3UL )\r
102 \r
103 /* The LED turned on by the button interrupt, and turned off by the LED timer. */\r
104 #define mainTIMER_CONTROLLED_LED                        ( 1UL << 2UL )\r
105 \r
106 /*-----------------------------------------------------------*/\r
107 \r
108 /*\r
109  * Setup the NVIC, LED outputs, and button inputs.\r
110  */\r
111 static void prvSetupHardware( void );\r
112 \r
113 /*\r
114  * The tasks as described in the comments at the top of this file.\r
115  */\r
116 static void prvQueueReceiveTask( void *pvParameters );\r
117 static void prvQueueSendTask( void *pvParameters );\r
118 \r
119 /*\r
120  * The LED timer callback function.  This does nothing but switch off the\r
121  * LED defined by the mainTIMER_CONTROLLED_LED constant.\r
122  */\r
123 static void vLEDTimerCallback( TimerHandle_t xTimer );\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 /* The queue used by both tasks. */\r
128 static QueueHandle_t xQueue = NULL;\r
129 \r
130 /* The LED software timer.  This uses vLEDTimerCallback() as its callback\r
131 function. */\r
132 static TimerHandle_t xLEDTimer = NULL;\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 int main(void)\r
137 {\r
138         /* Configure the NVIC, LED outputs and button inputs. */\r
139         prvSetupHardware();\r
140 \r
141         /* Create the queue. */\r
142         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
143 \r
144         if( xQueue != NULL )\r
145         {\r
146                 /* Start the two tasks as described in the comments at the top of this\r
147                 file. */\r
148                 xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
149                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
150 \r
151                 /* Create the software timer that is responsible for turning off the LED\r
152                 if the button is not pushed within 5000ms, as described at the top of\r
153                 this file. */\r
154                 xLEDTimer = xTimerCreate(       "LEDTimer",                                     /* A text name, purely to help debugging. */\r
155                                                                         ( 5000 / portTICK_PERIOD_MS ),  /* The timer period, in this case 5000ms (5s). */\r
156                                                                         pdFALSE,                                                /* This is a one-shot timer, so xAutoReload is set to pdFALSE. */\r
157                                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
158                                                                         vLEDTimerCallback                               /* The callback function that switches the LED off. */\r
159                                                                 );\r
160 \r
161                 /* Start the tasks and timer running. */\r
162                 vTaskStartScheduler();\r
163         }\r
164 \r
165         /* If all is well, the scheduler will now be running, and the following line\r
166         will never be reached.  If the following line does execute, then there was\r
167         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
168         to be created.  See the memory management section on the FreeRTOS web site\r
169         for more details. */\r
170         for( ;; );\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 static void vLEDTimerCallback( TimerHandle_t xTimer )\r
175 {\r
176         /* The timer has expired - so no button pushes have occurred in the last\r
177         five seconds - turn the LED off.  NOTE - accessing the LED port should use\r
178         a critical section because it is accessed from multiple tasks, and the\r
179         button interrupt - in this trivial case, for simplicity, the critical\r
180         section is omitted. */\r
181         FM3_GPIO->PDOR3 |= mainTIMER_CONTROLLED_LED;\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 /* The ISR executed when the user button is pushed. */\r
186 void INT0_7_Handler( void )\r
187 {\r
188 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
189 \r
190         /* The button was pushed, so ensure the LED is on before resetting the\r
191         LED timer.  The LED timer will turn the LED off if the button is not\r
192         pushed within 5000ms. */\r
193         FM3_GPIO->PDOR3 &= ~mainTIMER_CONTROLLED_LED;\r
194 \r
195         /* This interrupt safe FreeRTOS function can be called from this interrupt\r
196         because the interrupt priority is below the\r
197         configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
198         xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );\r
199 \r
200         /* Clear the interrupt before leaving.  This just clears all the interrupts\r
201         for simplicity, as only one is actually used in this simple demo anyway. */\r
202         FM3_EXTI->EICL = 0x0000;\r
203 \r
204         /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
205         service/daemon task) to unblock, and the unblocked task has a priority\r
206         higher than or equal to the task that was interrupted, then\r
207         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
208         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
209         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 static void prvQueueSendTask( void *pvParameters )\r
214 {\r
215 TickType_t xNextWakeTime;\r
216 const unsigned long ulValueToSend = 100UL;\r
217 \r
218         /* Initialise xNextWakeTime - this only needs to be done once. */\r
219         xNextWakeTime = xTaskGetTickCount();\r
220 \r
221         for( ;; )\r
222         {\r
223                 /* Place this task in the blocked state until it is time to run again.\r
224                 The block time is specified in ticks, the constant used converts ticks\r
225                 to ms.  While in the Blocked state this task will not consume any CPU\r
226                 time. */\r
227                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
228 \r
229                 /* Send to the queue - causing the queue receive task to unblock and\r
230                 toggle an LED.  0 is used as the block time so the sending operation\r
231                 will not block - it shouldn't need to block as the queue should always\r
232                 be empty at this point in the code. */\r
233                 xQueueSend( xQueue, &ulValueToSend, 0 );\r
234         }\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 static void prvQueueReceiveTask( void *pvParameters )\r
239 {\r
240 unsigned long ulReceivedValue;\r
241 \r
242         for( ;; )\r
243         {\r
244                 /* Wait until something arrives in the queue - this task will block\r
245                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
246                 FreeRTOSConfig.h. */\r
247                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
248 \r
249                 /*  To get here something must have been received from the queue, but\r
250                 is it the expected value?  If it is, toggle the LED. */\r
251                 if( ulReceivedValue == 100UL )\r
252                 {\r
253                         /* NOTE - accessing the LED port should use a critical section\r
254                         because it is accessed from multiple tasks, and the button interrupt\r
255                         - in this trivial case, for simplicity, the critical section is\r
256                         omitted. */\r
257                         if( ( FM3_GPIO->PDOR3 & mainTASK_CONTROLLED_LED ) != 0 )\r
258                         {\r
259                                 FM3_GPIO->PDOR3 &= ~mainTASK_CONTROLLED_LED;\r
260                         }\r
261                         else\r
262                         {\r
263                                 FM3_GPIO->PDOR3 |= mainTASK_CONTROLLED_LED;\r
264                         }\r
265                 }\r
266         }\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 static void prvSetupHardware( void )\r
271 {\r
272 const unsigned short usButtonInputBit = 0x01U;\r
273 \r
274         SystemInit();\r
275         SystemCoreClockUpdate();\r
276 \r
277         /* Analog inputs are not used on the LED outputs. */\r
278         FM3_GPIO->ADE  = 0x0000;\r
279 \r
280         /* Set to output. */\r
281         FM3_GPIO->DDR1 |= 0xFFFF;\r
282         FM3_GPIO->DDR3 |= 0xFFFF;\r
283 \r
284         /* Set as GPIO. */\r
285         FM3_GPIO->PFR1 &= 0x0000;\r
286         FM3_GPIO->PFR3 &= 0x0000;\r
287 \r
288         /* Start with all LEDs off. */\r
289         FM3_GPIO->PDOR3 = 0xFFFF;\r
290         FM3_GPIO->PDOR1 = 0xFFFF;\r
291 \r
292         /* Set the switches to input (P18->P1F). */\r
293         FM3_GPIO->DDR5 = 0x0000;\r
294         FM3_GPIO->PFR5 = 0x0000;\r
295 \r
296         /* Assign the button input as GPIO. */\r
297         FM3_GPIO->PFR5 |= usButtonInputBit;\r
298 \r
299         /* Button interrupt on falling edge. */\r
300         FM3_EXTI->ELVR  = 0x0003;\r
301 \r
302         /* Clear all external interrupts. */\r
303         FM3_EXTI->EICL  = 0x0000;\r
304 \r
305         /* Enable the button interrupt. */\r
306         FM3_EXTI->ENIR |= usButtonInputBit;\r
307 \r
308         /* Setup the GPIO and the NVIC for the switch used in this simple demo. */\r
309         NVIC_SetPriority( EXINT0_7_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
310     NVIC_EnableIRQ( EXINT0_7_IRQn );\r
311 }\r
312 /*-----------------------------------------------------------*/\r
313 \r
314 void vApplicationMallocFailedHook( void )\r
315 {\r
316         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
317         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
318         internally by FreeRTOS API functions that create tasks, queues, software\r
319         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
320         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
321         for( ;; );\r
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
326 {\r
327         ( void ) pcTaskName;\r
328         ( void ) pxTask;\r
329 \r
330         /* Run time stack overflow checking is performed if\r
331         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
332         function is called if a stack overflow is detected. */\r
333         for( ;; );\r
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \r
337 void vApplicationTickHook( void )\r
338 {\r
339         /* A tick hook is used by the "Full" build configuration.  The Full and\r
340         blinky build configurations share a FreeRTOSConfig.h header file, so this\r
341         simple build configuration also has to define a tick hook - even though it\r
342         does not actually use it for anything. */\r
343 }\r
344 /*-----------------------------------------------------------*/\r
345 \r
346 void vApplicationIdleHook( void )\r
347 {\r
348 volatile size_t xFreeHeapSpace;\r
349 \r
350         /* This function is called on each cycle of the idle task.  In this case it\r
351         does nothing useful, other than report the amount of FreeRTOS heap that\r
352         remains unallocated. */\r
353         xFreeHeapSpace = xPortGetFreeHeapSize();\r
354 \r
355         if( xFreeHeapSpace > 100 )\r
356         {\r
357                 /* By now, the kernel has allocated everything it is going to, so\r
358                 if there is a lot of heap remaining unallocated then\r
359                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
360                 reduced accordingly. */\r
361         }\r
362 }\r
363 /*-----------------------------------------------------------*/\r
364 \r
365 \r
366 \r
367 \r