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