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