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