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