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