]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32F100_Atollic/Simple_Demo_Source/main.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / CORTEX_STM32F100_Atollic / Simple_Demo_Source / main.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 This simple demo project runs on the STM32 Discovery board, which is\r
69 populated with an STM32F100RB Cortex-M3 microcontroller.  The discovery board \r
70 makes an ideal low cost evaluation platform, but the 8K of RAM provided on the\r
71 STM32F100RB does not allow the simple application to demonstrate all of all the \r
72 FreeRTOS kernel features.  Therefore, this simple demo only actively \r
73 demonstrates task, queue, timer and interrupt functionality.  In addition, the \r
74 demo is configured to include malloc failure, idle and stack overflow hook \r
75 functions.\r
76 \r
77 The idle hook function:\r
78 The idle hook function queries the amount of FreeRTOS heap space that is\r
79 remaining (see vApplicationIdleHook() defined in this file).  The demo \r
80 application is configured to use 7K of the available 8K of RAM as the FreeRTOS \r
81 heap.  Memory is only allocated from this heap during initialisation, and this \r
82 demo only actually uses 1.6K bytes of the configured 7K available - leaving 5.4K \r
83 bytes of heap space unallocated.\r
84 \r
85 The main() Function:\r
86 main() creates one software timer, one queue, and two tasks.  It then starts the\r
87 scheduler.\r
88 \r
89 The Queue Send Task:\r
90 The queue send task is implemented by the prvQueueSendTask() function in this \r
91 file.  prvQueueSendTask() sits in a loop that causes it to repeatedly block for \r
92 200 milliseconds, before sending the value 100 to the queue that was created \r
93 within main().  Once the value is sent, the task loops back around to block for\r
94 another 200 milliseconds.\r
95 \r
96 The Queue Receive Task:\r
97 The queue receive task is implemented by the prvQueueReceiveTask() function\r
98 in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly blocks \r
99 on attempts to read data from the queue that was created within main().  When \r
100 data is received, the task checks the value of the data, and if the value equals \r
101 the expected 100, toggles the green LED.  The 'block time' parameter passed to \r
102 the queue receive function specifies that the task should be held in the Blocked \r
103 state indefinitely to wait for data to be available on the queue.  The queue \r
104 receive task will only leave the Blocked state when the queue send task writes \r
105 to the queue.  As the queue send task writes to the queue every 200 \r
106 milliseconds, the queue receive task leaves the Blocked state every 200 \r
107 milliseconds, and therefore toggles the green LED every 200 milliseconds.\r
108 \r
109 The LED Software Timer and the Button Interrupt:\r
110 The user button B1 is configured to generate an interrupt each time it is\r
111 pressed.  The interrupt service routine switches the red LED on, and resets the \r
112 LED software timer.  The LED timer has a 5000 millisecond (5 second) period, and\r
113 uses a callback function that is defined to just turn the red LED off.  \r
114 Therefore, pressing the user button will turn the red LED on, and the LED will \r
115 remain on until a full five seconds pass without the button being pressed.\r
116 */\r
117 \r
118 \r
119 /* Kernel includes. */\r
120 #include "FreeRTOS.h"\r
121 #include "task.h"\r
122 #include "queue.h"\r
123 #include "timers.h"\r
124 \r
125 /* STM32 Library includes. */\r
126 #include "stm32f10x.h"\r
127 #include "STM32vldiscovery.h"\r
128 \r
129 /* Priorities at which the tasks are created. */\r
130 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
131 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
132 \r
133 /* The rate at which data is sent to the queue, specified in milliseconds, and\r
134 converted to ticks using the portTICK_RATE_MS constant. */\r
135 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_RATE_MS )\r
136 \r
137 /* The number of items the queue can hold.  This is 1 as the receive task\r
138 will remove items as they are added, meaning the send task should always find\r
139 the queue empty. */\r
140 #define mainQUEUE_LENGTH                                        ( 1 )\r
141 \r
142 /*-----------------------------------------------------------*/\r
143 \r
144 /*\r
145  * Setup the NVIC, LED outputs, and button inputs.\r
146  */\r
147 static void prvSetupHardware( void );\r
148 \r
149 /*\r
150  * The tasks as described in the comments at the top of this file.\r
151  */\r
152 static void prvQueueReceiveTask( void *pvParameters );\r
153 static void prvQueueSendTask( void *pvParameters );\r
154 \r
155 /*\r
156  * The LED timer callback function.  This does nothing but switch the red LED \r
157  * off.\r
158  */\r
159 static void vLEDTimerCallback( xTimerHandle xTimer );\r
160 \r
161 /*-----------------------------------------------------------*/\r
162 \r
163 /* The queue used by both tasks. */\r
164 static xQueueHandle xQueue = NULL;\r
165 \r
166 /* The LED software timer.  This uses vLEDTimerCallback() as its callback\r
167  * function. \r
168  */\r
169 static xTimerHandle xLEDTimer = NULL;\r
170 \r
171 /*-----------------------------------------------------------*/\r
172 \r
173 int main(void)\r
174 {\r
175         /* Configure the NVIC, LED outputs and button inputs. */\r
176         prvSetupHardware();\r
177 \r
178         /* Create the queue. */\r
179         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
180 \r
181         if( xQueue != NULL )\r
182         {\r
183                 /* Start the two tasks as described in the comments at the top of this\r
184                 file. */\r
185                 xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
186                 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
187 \r
188                 /* Create the software timer that is responsible for turning off the LED \r
189                 if the button is not pushed within 5000ms, as described at the top of \r
190                 this file. */\r
191                 xLEDTimer = xTimerCreate(       ( const signed char * ) "LEDTimer", /* A text name, purely to help debugging. */\r
192                                                                         ( 5000 / portTICK_RATE_MS ),            /* The timer period, in this case 5000ms (5s). */\r
193                                                                         pdFALSE,                                                        /* This is a one shot timer, so xAutoReload is set to pdFALSE. */\r
194                                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
195                                                                         vLEDTimerCallback                                       /* The callback function that switches the LED off. */\r
196                                                                 );\r
197 \r
198                 /* Start the tasks and timer running. */\r
199                 vTaskStartScheduler();\r
200         }\r
201 \r
202         /* If all is well, the scheduler will now be running, and the following line\r
203         will never be reached.  If the following line does execute, then there was\r
204         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
205         to be created.  See the memory management section on the FreeRTOS web site\r
206         for more details. */\r
207         for( ;; );\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 static void vLEDTimerCallback( xTimerHandle xTimer )\r
212 {\r
213         /* The timer has expired - so no button pushes have occurred in the last\r
214         five seconds - turn the LED off.  NOTE - accessing the LED port should use\r
215         a critical section because it is accessed from multiple tasks, and the\r
216         button interrupt - in this trivial case, for simplicity, the critical\r
217         section is omitted. */\r
218         STM32vldiscovery_LEDOff( LED4 );\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 /* The ISR executed when the user button is pushed. */\r
223 void EXTI0_IRQHandler( void )\r
224 {\r
225 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
226 \r
227         /* The button was pushed, so ensure the LED is on before resetting the\r
228         LED timer.  The LED timer will turn the LED off if the button is not\r
229         pushed within 5000ms. */\r
230         STM32vldiscovery_LEDOn( LED4 );\r
231 \r
232         /* This interrupt safe FreeRTOS function can be called from this interrupt\r
233         because the interrupt priority is below the\r
234         configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
235         xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );\r
236 \r
237         /* Clear the interrupt before leaving. */\r
238         EXTI_ClearITPendingBit( EXTI_Line0 );\r
239 \r
240         /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
241         service/daemon task) to unblock, and the unblocked task has a priority\r
242         higher than or equal to the task that was interrupted, then\r
243         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
244         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
245         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 static void prvQueueSendTask( void *pvParameters )\r
250 {\r
251 portTickType xNextWakeTime;\r
252 const unsigned long ulValueToSend = 100UL;\r
253 \r
254         /* Initialise xNextWakeTime - this only needs to be done once. */\r
255         xNextWakeTime = xTaskGetTickCount();\r
256 \r
257         for( ;; )\r
258         {\r
259                 /* Place this task in the blocked state until it is time to run again.\r
260                 The block time is specified in ticks, the constant used converts ticks\r
261                 to ms.  While in the Blocked state this task will not consume any CPU\r
262                 time. */\r
263                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
264 \r
265                 /* Send to the queue - causing the queue receive task to unblock and\r
266                 toggle an LED.  0 is used as the block time so the sending operation\r
267                 will not block - it shouldn't need to block as the queue should always\r
268                 be empty at this point in the code. */\r
269                 xQueueSend( xQueue, &ulValueToSend, 0 );\r
270         }\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 static void prvQueueReceiveTask( void *pvParameters )\r
275 {\r
276 unsigned long ulReceivedValue;\r
277 \r
278         for( ;; )\r
279         {\r
280                 /* Wait until something arrives in the queue - this task will block\r
281                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
282                 FreeRTOSConfig.h. */\r
283                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
284 \r
285                 /*  To get here something must have been received from the queue, but\r
286                 is it the expected value?  If it is, toggle the green LED. */\r
287                 if( ulReceivedValue == 100UL )\r
288                 {\r
289                         /* NOTE - accessing the LED port should use a critical section\r
290                         because it is accessed from multiple tasks, and the button interrupt \r
291                         - in this trivial case, for simplicity, the critical section is \r
292                         omitted. */\r
293                         STM32vldiscovery_LEDToggle( LED3 );\r
294                 }\r
295         }\r
296 }\r
297 /*-----------------------------------------------------------*/\r
298 \r
299 static void prvSetupHardware( void )\r
300 {\r
301         /* Ensure that all 4 interrupt priority bits are used as the pre-emption\r
302         priority. */\r
303         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
304 \r
305         /* Set up the LED outputs and the button inputs. */\r
306         STM32vldiscovery_LEDInit( LED3 );\r
307         STM32vldiscovery_LEDInit( LED4 );\r
308         STM32vldiscovery_PBInit( BUTTON_USER, BUTTON_MODE_EXTI );\r
309         \r
310         /* Start with the LEDs off. */\r
311         STM32vldiscovery_LEDOff( LED3 );\r
312         STM32vldiscovery_LEDOff( LED4 );\r
313 }\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 void vApplicationMallocFailedHook( void )\r
317 {\r
318         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
319         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
320         internally by FreeRTOS API functions that create tasks, queues, software \r
321         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
322         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
323         for( ;; );\r
324 }\r
325 /*-----------------------------------------------------------*/\r
326 \r
327 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
328 {\r
329         ( void ) pcTaskName;\r
330         ( void ) pxTask;\r
331 \r
332         /* Run time stack overflow checking is performed if\r
333         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
334         function is called if a stack overflow is detected. */\r
335         for( ;; );\r
336 }\r
337 /*-----------------------------------------------------------*/\r
338 \r
339 void vApplicationIdleHook( void )\r
340 {\r
341 volatile size_t xFreeStackSpace;\r
342 \r
343         /* This function is called on each cycle of the idle task.  In this case it\r
344         does nothing useful, other than report the amout of FreeRTOS heap that \r
345         remains unallocated. */\r
346         xFreeStackSpace = xPortGetFreeHeapSize();\r
347 \r
348         if( xFreeStackSpace > 100 )\r
349         {\r
350                 /* By now, the kernel has allocated everything it is going to, so\r
351                 if there is a lot of heap remaining unallocated then\r
352                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
353                 reduced accordingly. */\r
354         }\r
355 }\r