]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_MB9A310_IAR_Keil/main_blinky.c
Initial commit
[cmsis-freertos] / Demo / CORTEX_MB9A310_IAR_Keil / main_blinky.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70 /*
71  * main-blinky.c is included when the "Blinky" build configuration is used.
72  * main-full.c is included when the "Full" build configuration is used.
73  *
74  * main-blinky.c (this file) defines a very simple demo that creates two tasks,
75  * one queue, and one timer.  It also demonstrates how Cortex-M3 interrupts can
76  * interact with FreeRTOS tasks/timers.
77  *
78  * This simple demo project runs on the SK-FM3-64PMC1 evaluation board, which
79  * is populated with an MB9A314 microcontroller.
80  *
81  * The idle hook function:
82  * The idle hook function demonstrates how to query the amount of FreeRTOS heap
83  * space that is remaining (see vApplicationIdleHook() defined in this file).
84  *
85  * The main() Function:
86  * main() creates one software timer, one queue, and two tasks.  It then starts
87  * the scheduler.
88  *
89  * The Queue Send Task:
90  * The queue send task is implemented by the prvQueueSendTask() function in
91  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly
92  * block for 200 milliseconds, before sending the value 100 to the queue that
93  * was created within main().  Once the value is sent, the task loops back
94  * around to block for another 200 milliseconds.
95  *
96  * The Queue Receive Task:
97  * The queue receive task is implemented by the prvQueueReceiveTask() function
98  * in this file.  prvQueueReceiveTask() sits in a loop that causes it to
99  * repeatedly attempt to read data from the queue that was created within
100  * main().  When data is received, the task checks the value of the data, and
101  * if the value equals the expected 100, toggles an LED on the 7 segment
102  * display.  The 'block time' parameter passed to the queue receive function
103  * specifies that the task should be held in the Blocked state indefinitely to
104  * wait for data to be available on the queue.  The queue receive task will only
105  * leave the Blocked state when the queue send task writes to the queue.  As the
106  * queue send task writes to the queue every 200 milliseconds, the queue receive
107  * task leaves the Blocked state every 200 milliseconds, and therefore toggles
108  * the LED every 200 milliseconds.
109  *
110  * The LED Software Timer and the Button Interrupt:
111  * The user button SW2 is configured to generate an interrupt each time it is
112  * pressed.  The interrupt service routine switches an LED in the 7 segment
113  * display on, and resets the LED software timer.  The LED timer has a 5000
114  * millisecond (5 second) period, and uses a callback function that is defined
115  * to just turn the LED off again.  Therefore, pressing the user button will
116  * turn the LED on, and the LED will remain on until a full five seconds pass
117  * without the button being pressed.
118  */
119
120 /* Kernel includes. */
121 #include "FreeRTOS.h"
122 #include "task.h"
123 #include "queue.h"
124 #include "timers.h"
125
126 /* Fujitsu drivers/libraries. */
127 #include "mcu.h"
128
129 /* Priorities at which the tasks are created. */
130 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )
131 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )
132
133 /* The rate at which data is sent to the queue, specified in milliseconds, and
134 converted to ticks using the portTICK_PERIOD_MS constant. */
135 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_PERIOD_MS )
136
137 /* The number of items the queue can hold.  This is 1 as the receive task
138 will remove items as they are added, meaning the send task should always find
139 the queue empty. */
140 #define mainQUEUE_LENGTH                                        ( 1 )
141
142 /* The LED toggle by the queue receive task. */
143 #define mainTASK_CONTROLLED_LED                         ( 1UL << 3UL )
144
145 /* The LED turned on by the button interrupt, and turned off by the LED timer. */
146 #define mainTIMER_CONTROLLED_LED                        ( 1UL << 2UL )
147
148 /*-----------------------------------------------------------*/
149
150 /*
151  * Setup the NVIC, LED outputs, and button inputs.
152  */
153 static void prvSetupHardware( void );
154
155 /*
156  * The tasks as described in the comments at the top of this file.
157  */
158 static void prvQueueReceiveTask( void *pvParameters );
159 static void prvQueueSendTask( void *pvParameters );
160
161 /*
162  * The LED timer callback function.  This does nothing but switch off the
163  * LED defined by the mainTIMER_CONTROLLED_LED constant.
164  */
165 static void vLEDTimerCallback( TimerHandle_t xTimer );
166
167 /*-----------------------------------------------------------*/
168
169 /* The queue used by both tasks. */
170 static QueueHandle_t xQueue = NULL;
171
172 /* The LED software timer.  This uses vLEDTimerCallback() as its callback
173 function. */
174 static TimerHandle_t xLEDTimer = NULL;
175
176 /*-----------------------------------------------------------*/
177
178 int main(void)
179 {
180         /* Configure the NVIC, LED outputs and button inputs. */
181         prvSetupHardware();
182
183         /* Create the queue. */
184         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
185
186         if( xQueue != NULL )
187         {
188                 /* Start the two tasks as described in the comments at the top of this
189                 file. */
190                 xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );
191                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
192
193                 /* Create the software timer that is responsible for turning off the LED
194                 if the button is not pushed within 5000ms, as described at the top of
195                 this file. */
196                 xLEDTimer = xTimerCreate(       "LEDTimer",                                     /* A text name, purely to help debugging. */
197                                                                         ( 5000 / portTICK_PERIOD_MS ),  /* The timer period, in this case 5000ms (5s). */
198                                                                         pdFALSE,                                                /* This is a one shot timer, so xAutoReload is set to pdFALSE. */
199                                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */
200                                                                         vLEDTimerCallback                               /* The callback function that switches the LED off. */
201                                                                 );
202
203                 /* Start the tasks and timer running. */
204                 vTaskStartScheduler();
205         }
206
207         /* If all is well, the scheduler will now be running, and the following line
208         will never be reached.  If the following line does execute, then there was
209         insufficient FreeRTOS heap memory available for the idle and/or timer tasks
210         to be created.  See the memory management section on the FreeRTOS web site
211         for more details. */
212         for( ;; );
213 }
214 /*-----------------------------------------------------------*/
215
216 static void vLEDTimerCallback( TimerHandle_t xTimer )
217 {
218         /* The timer has expired - so no button pushes have occurred in the last
219         five seconds - turn the LED off.  NOTE - accessing the LED port should use
220         a critical section because it is accessed from multiple tasks, and the
221         button interrupt - in this trivial case, for simplicity, the critical
222         section is omitted. */
223         FM3_GPIO->PDOR3 |= mainTIMER_CONTROLLED_LED;
224 }
225 /*-----------------------------------------------------------*/
226
227 /* The ISR executed when the user button is pushed. */
228 void INT0_7_Handler( void )
229 {
230 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
231
232         /* The button was pushed, so ensure the LED is on before resetting the
233         LED timer.  The LED timer will turn the LED off if the button is not
234         pushed within 5000ms. */
235         FM3_GPIO->PDOR3 &= ~mainTIMER_CONTROLLED_LED;
236
237         /* This interrupt safe FreeRTOS function can be called from this interrupt
238         because the interrupt priority is below the
239         configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */
240         xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );
241
242         /* Clear the interrupt before leaving.  This just clears all the interrupts
243         for simplicity, as only one is actually used in this simple demo anyway. */
244         FM3_EXTI->EICL = 0x0000;
245
246         /* If calling xTimerResetFromISR() caused a task (in this case the timer
247         service/daemon task) to unblock, and the unblocked task has a priority
248         higher than or equal to the task that was interrupted, then
249         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling
250         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */
251         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
252 }
253 /*-----------------------------------------------------------*/
254
255 static void prvQueueSendTask( void *pvParameters )
256 {
257 TickType_t xNextWakeTime;
258 const unsigned long ulValueToSend = 100UL;
259
260         /* Initialise xNextWakeTime - this only needs to be done once. */
261         xNextWakeTime = xTaskGetTickCount();
262
263         for( ;; )
264         {
265                 /* Place this task in the blocked state until it is time to run again.
266                 The block time is specified in ticks, the constant used converts ticks
267                 to ms.  While in the Blocked state this task will not consume any CPU
268                 time. */
269                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
270
271                 /* Send to the queue - causing the queue receive task to unblock and
272                 toggle an LED.  0 is used as the block time so the sending operation
273                 will not block - it shouldn't need to block as the queue should always
274                 be empty at this point in the code. */
275                 xQueueSend( xQueue, &ulValueToSend, 0 );
276         }
277 }
278 /*-----------------------------------------------------------*/
279
280 static void prvQueueReceiveTask( void *pvParameters )
281 {
282 unsigned long ulReceivedValue;
283
284         for( ;; )
285         {
286                 /* Wait until something arrives in the queue - this task will block
287                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
288                 FreeRTOSConfig.h. */
289                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
290
291                 /*  To get here something must have been received from the queue, but
292                 is it the expected value?  If it is, toggle the LED. */
293                 if( ulReceivedValue == 100UL )
294                 {
295                         /* NOTE - accessing the LED port should use a critical section
296                         because it is accessed from multiple tasks, and the button interrupt
297                         - in this trivial case, for simplicity, the critical section is
298                         omitted. */
299                         if( ( FM3_GPIO->PDOR3 & mainTASK_CONTROLLED_LED ) != 0 )
300                         {
301                                 FM3_GPIO->PDOR3 &= ~mainTASK_CONTROLLED_LED;
302                         }
303                         else
304                         {
305                                 FM3_GPIO->PDOR3 |= mainTASK_CONTROLLED_LED;
306                         }
307                 }
308         }
309 }
310 /*-----------------------------------------------------------*/
311
312 static void prvSetupHardware( void )
313 {
314 const unsigned short usButtonInputBit = 0x01U;
315
316         SystemInit();
317         SystemCoreClockUpdate();
318
319         /* Analog inputs are not used on the LED outputs. */
320         FM3_GPIO->ADE  = 0x0000;
321
322         /* Set to output. */
323         FM3_GPIO->DDR1 |= 0xFFFF;
324         FM3_GPIO->DDR3 |= 0xFFFF;
325
326         /* Set as GPIO. */
327         FM3_GPIO->PFR1 &= 0x0000;
328         FM3_GPIO->PFR3 &= 0x0000;
329
330         /* Start with all LEDs off. */
331         FM3_GPIO->PDOR3 = 0xFFFF;
332         FM3_GPIO->PDOR1 = 0xFFFF;
333
334         /* Set the switches to input (P18->P1F). */
335         FM3_GPIO->DDR5 = 0x0000;
336         FM3_GPIO->PFR5 = 0x0000;
337
338         /* Assign the button input as GPIO. */
339         FM3_GPIO->PFR5 |= usButtonInputBit;
340
341         /* Button interrupt on falling edge. */
342         FM3_EXTI->ELVR  = 0x0003;
343
344         /* Clear all external interrupts. */
345         FM3_EXTI->EICL  = 0x0000;
346
347         /* Enable the button interrupt. */
348         FM3_EXTI->ENIR |= usButtonInputBit;
349
350         /* Setup the GPIO and the NVIC for the switch used in this simple demo. */
351         NVIC_SetPriority( EXINT0_7_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
352     NVIC_EnableIRQ( EXINT0_7_IRQn );
353 }
354 /*-----------------------------------------------------------*/
355
356 void vApplicationMallocFailedHook( void )
357 {
358         /* Called if a call to pvPortMalloc() fails because there is insufficient
359         free memory available in the FreeRTOS heap.  pvPortMalloc() is called
360         internally by FreeRTOS API functions that create tasks, queues, software
361         timers, and semaphores.  The size of the FreeRTOS heap is set by the
362         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
363         for( ;; );
364 }
365 /*-----------------------------------------------------------*/
366
367 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
368 {
369         ( void ) pcTaskName;
370         ( void ) pxTask;
371
372         /* Run time stack overflow checking is performed if
373         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook
374         function is called if a stack overflow is detected. */
375         for( ;; );
376 }
377 /*-----------------------------------------------------------*/
378
379 void vApplicationTickHook( void )
380 {
381         /* A tick hook is used by the "Full" build configuration.  The Full and
382         blinky build configurations share a FreeRTOSConfig.h header file, so this
383         simple build configuration also has to define a tick hook - even though it
384         does not actually use it for anything. */
385 }
386 /*-----------------------------------------------------------*/
387
388 void vApplicationIdleHook( void )
389 {
390 volatile size_t xFreeHeapSpace;
391
392         /* This function is called on each cycle of the idle task.  In this case it
393         does nothing useful, other than report the amount of FreeRTOS heap that
394         remains unallocated. */
395         xFreeHeapSpace = xPortGetFreeHeapSize();
396
397         if( xFreeHeapSpace > 100 )
398         {
399                 /* By now, the kernel has allocated everything it is going to, so
400                 if there is a lot of heap remaining unallocated then
401                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be
402                 reduced accordingly. */
403         }
404 }
405 /*-----------------------------------------------------------*/
406
407
408
409