]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32F100_Atollic/Simple_Demo_Source/main.c
Minor updates and change version number for V7.5.0 release.
[freertos] / FreeRTOS / Demo / CORTEX_STM32F100_Atollic / Simple_Demo_Source / main.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*\r
66 This simple demo project runs on the STM32 Discovery board, which is\r
67 populated with an STM32F100RB Cortex-M3 microcontroller.  The discovery board \r
68 makes an ideal low cost evaluation platform, but the 8K of RAM provided on the\r
69 STM32F100RB does not allow the simple application to demonstrate all of all the \r
70 FreeRTOS kernel features.  Therefore, this simple demo only actively \r
71 demonstrates task, queue, timer and interrupt functionality.  In addition, the \r
72 demo is configured to include malloc failure, idle and stack overflow hook \r
73 functions.\r
74 \r
75 The idle hook function:\r
76 The idle hook function queries the amount of FreeRTOS heap space that is\r
77 remaining (see vApplicationIdleHook() defined in this file).  The demo \r
78 application is configured to use 7K of the available 8K of RAM as the FreeRTOS \r
79 heap.  Memory is only allocated from this heap during initialisation, and this \r
80 demo only actually uses 1.6K bytes of the configured 7K available - leaving 5.4K \r
81 bytes of heap space unallocated.\r
82 \r
83 The main() Function:\r
84 main() creates one software timer, one queue, and two tasks.  It then starts the\r
85 scheduler.\r
86 \r
87 The Queue Send Task:\r
88 The queue send task is implemented by the prvQueueSendTask() function in this \r
89 file.  prvQueueSendTask() sits in a loop that causes it to repeatedly block for \r
90 200 milliseconds, before sending the value 100 to the queue that was created \r
91 within main().  Once the value is sent, the task loops back around to block for\r
92 another 200 milliseconds.\r
93 \r
94 The Queue Receive Task:\r
95 The queue receive task is implemented by the prvQueueReceiveTask() function\r
96 in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly blocks \r
97 on attempts to read data from the queue that was created within main().  When \r
98 data is received, the task checks the value of the data, and if the value equals \r
99 the expected 100, toggles the green LED.  The 'block time' parameter passed to \r
100 the queue receive function specifies that the task should be held in the Blocked \r
101 state indefinitely to wait for data to be available on the queue.  The queue \r
102 receive task will only leave the Blocked state when the queue send task writes \r
103 to the queue.  As the queue send task writes to the queue every 200 \r
104 milliseconds, the queue receive task leaves the Blocked state every 200 \r
105 milliseconds, and therefore toggles the green LED every 200 milliseconds.\r
106 \r
107 The LED Software Timer and the Button Interrupt:\r
108 The user button B1 is configured to generate an interrupt each time it is\r
109 pressed.  The interrupt service routine switches the red LED on, and resets the \r
110 LED software timer.  The LED timer has a 5000 millisecond (5 second) period, and\r
111 uses a callback function that is defined to just turn the red LED off.  \r
112 Therefore, pressing the user button will turn the red LED on, and the LED will \r
113 remain on until a full five seconds pass without the button being pressed.\r
114 */\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 /* STM32 Library includes. */\r
124 #include "stm32f10x.h"\r
125 #include "STM32vldiscovery.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 /*-----------------------------------------------------------*/\r
141 \r
142 /*\r
143  * Setup the NVIC, LED outputs, and button inputs.\r
144  */\r
145 static void prvSetupHardware( void );\r
146 \r
147 /*\r
148  * The tasks as described in the comments at the top of this file.\r
149  */\r
150 static void prvQueueReceiveTask( void *pvParameters );\r
151 static void prvQueueSendTask( void *pvParameters );\r
152 \r
153 /*\r
154  * The LED timer callback function.  This does nothing but switch the red LED \r
155  * off.\r
156  */\r
157 static void vLEDTimerCallback( xTimerHandle xTimer );\r
158 \r
159 /*-----------------------------------------------------------*/\r
160 \r
161 /* The queue used by both tasks. */\r
162 static xQueueHandle xQueue = NULL;\r
163 \r
164 /* The LED software timer.  This uses vLEDTimerCallback() as its callback\r
165  * function. \r
166  */\r
167 static xTimerHandle xLEDTimer = NULL;\r
168 \r
169 /*-----------------------------------------------------------*/\r
170 \r
171 int main(void)\r
172 {\r
173         /* Configure the NVIC, LED outputs and button inputs. */\r
174         prvSetupHardware();\r
175 \r
176         /* Create the queue. */\r
177         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
178 \r
179         if( xQueue != NULL )\r
180         {\r
181                 /* Start the two tasks as described in the comments at the top of this\r
182                 file. */\r
183                 xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
184                 xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
185 \r
186                 /* Create the software timer that is responsible for turning off the LED \r
187                 if the button is not pushed within 5000ms, as described at the top of \r
188                 this file. */\r
189                 xLEDTimer = xTimerCreate(       ( const signed char * ) "LEDTimer", /* A text name, purely to help debugging. */\r
190                                                                         ( 5000 / portTICK_RATE_MS ),            /* The timer period, in this case 5000ms (5s). */\r
191                                                                         pdFALSE,                                                        /* This is a one shot timer, so xAutoReload is set to pdFALSE. */\r
192                                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
193                                                                         vLEDTimerCallback                                       /* The callback function that switches the LED off. */\r
194                                                                 );\r
195 \r
196                 /* Start the tasks and timer running. */\r
197                 vTaskStartScheduler();\r
198         }\r
199 \r
200         /* If all is well, the scheduler will now be running, and the following line\r
201         will never be reached.  If the following line does execute, then there was\r
202         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
203         to be created.  See the memory management section on the FreeRTOS web site\r
204         for more details. */\r
205         for( ;; );\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 static void vLEDTimerCallback( xTimerHandle xTimer )\r
210 {\r
211         /* The timer has expired - so no button pushes have occurred in the last\r
212         five seconds - turn the LED off.  NOTE - accessing the LED port should use\r
213         a critical section because it is accessed from multiple tasks, and the\r
214         button interrupt - in this trivial case, for simplicity, the critical\r
215         section is omitted. */\r
216         STM32vldiscovery_LEDOff( LED4 );\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 /* The ISR executed when the user button is pushed. */\r
221 void EXTI0_IRQHandler( void )\r
222 {\r
223 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
224 \r
225         /* The button was pushed, so ensure the LED is on before resetting the\r
226         LED timer.  The LED timer will turn the LED off if the button is not\r
227         pushed within 5000ms. */\r
228         STM32vldiscovery_LEDOn( LED4 );\r
229 \r
230         /* This interrupt safe FreeRTOS function can be called from this interrupt\r
231         because the interrupt priority is below the\r
232         configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
233         xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );\r
234 \r
235         /* Clear the interrupt before leaving. */\r
236         EXTI_ClearITPendingBit( EXTI_Line0 );\r
237 \r
238         /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
239         service/daemon task) to unblock, and the unblocked task has a priority\r
240         higher than or equal to the task that was interrupted, then\r
241         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
242         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
243         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 static void prvQueueSendTask( void *pvParameters )\r
248 {\r
249 portTickType xNextWakeTime;\r
250 const unsigned long ulValueToSend = 100UL;\r
251 \r
252         /* Initialise xNextWakeTime - this only needs to be done once. */\r
253         xNextWakeTime = xTaskGetTickCount();\r
254 \r
255         for( ;; )\r
256         {\r
257                 /* Place this task in the blocked state until it is time to run again.\r
258                 The block time is specified in ticks, the constant used converts ticks\r
259                 to ms.  While in the Blocked state this task will not consume any CPU\r
260                 time. */\r
261                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
262 \r
263                 /* Send to the queue - causing the queue receive task to unblock and\r
264                 toggle an LED.  0 is used as the block time so the sending operation\r
265                 will not block - it shouldn't need to block as the queue should always\r
266                 be empty at this point in the code. */\r
267                 xQueueSend( xQueue, &ulValueToSend, 0 );\r
268         }\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 static void prvQueueReceiveTask( void *pvParameters )\r
273 {\r
274 unsigned long ulReceivedValue;\r
275 \r
276         for( ;; )\r
277         {\r
278                 /* Wait until something arrives in the queue - this task will block\r
279                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
280                 FreeRTOSConfig.h. */\r
281                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
282 \r
283                 /*  To get here something must have been received from the queue, but\r
284                 is it the expected value?  If it is, toggle the green LED. */\r
285                 if( ulReceivedValue == 100UL )\r
286                 {\r
287                         /* NOTE - accessing the LED port should use a critical section\r
288                         because it is accessed from multiple tasks, and the button interrupt \r
289                         - in this trivial case, for simplicity, the critical section is \r
290                         omitted. */\r
291                         STM32vldiscovery_LEDToggle( LED3 );\r
292                 }\r
293         }\r
294 }\r
295 /*-----------------------------------------------------------*/\r
296 \r
297 static void prvSetupHardware( void )\r
298 {\r
299         /* Ensure that all 4 interrupt priority bits are used as the pre-emption\r
300         priority. */\r
301         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
302 \r
303         /* Set up the LED outputs and the button inputs. */\r
304         STM32vldiscovery_LEDInit( LED3 );\r
305         STM32vldiscovery_LEDInit( LED4 );\r
306         STM32vldiscovery_PBInit( BUTTON_USER, BUTTON_MODE_EXTI );\r
307         \r
308         /* Start with the LEDs off. */\r
309         STM32vldiscovery_LEDOff( LED3 );\r
310         STM32vldiscovery_LEDOff( LED4 );\r
311 }\r
312 /*-----------------------------------------------------------*/\r
313 \r
314 void vApplicationMallocFailedHook( void )\r
315 {\r
316         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
317         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
318         internally by FreeRTOS API functions that create tasks, queues, software \r
319         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
320         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
321         for( ;; );\r
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
326 {\r
327         ( void ) pcTaskName;\r
328         ( void ) pxTask;\r
329 \r
330         /* Run time stack overflow checking is performed if\r
331         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
332         function is called if a stack overflow is detected. */\r
333         for( ;; );\r
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \r
337 void vApplicationIdleHook( void )\r
338 {\r
339 volatile size_t xFreeStackSpace;\r
340 \r
341         /* This function is called on each cycle of the idle task.  In this case it\r
342         does nothing useful, other than report the amout of FreeRTOS heap that \r
343         remains unallocated. */\r
344         xFreeStackSpace = xPortGetFreeHeapSize();\r
345 \r
346         if( xFreeStackSpace > 100 )\r
347         {\r
348                 /* By now, the kernel has allocated everything it is going to, so\r
349                 if there is a lot of heap remaining unallocated then\r
350                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
351                 reduced accordingly. */\r
352         }\r
353 }\r