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