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