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