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