]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32F100_Atollic/Simple_Demo_Source/main.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_STM32F100_Atollic / Simple_Demo_Source / main.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30 This simple demo project runs on the STM32 Discovery board, which is\r
31 populated with an STM32F100RB Cortex-M3 microcontroller.  The discovery board\r
32 makes an ideal low cost evaluation platform, but the 8K of RAM provided on the\r
33 STM32F100RB does not allow the simple application to demonstrate all of all the\r
34 FreeRTOS kernel features.  Therefore, this simple demo only actively\r
35 demonstrates task, queue, timer and interrupt functionality.  In addition, the\r
36 demo is configured to include malloc failure, idle and stack overflow hook\r
37 functions.\r
38 \r
39 The idle hook function:\r
40 The idle hook function queries the amount of FreeRTOS heap space that is\r
41 remaining (see vApplicationIdleHook() defined in this file).  The demo\r
42 application is configured to use 7K of the available 8K of RAM as the FreeRTOS\r
43 heap.  Memory is only allocated from this heap during initialisation, and this\r
44 demo only actually uses 1.6K bytes of the configured 7K available - leaving 5.4K\r
45 bytes of heap space unallocated.\r
46 \r
47 The main() Function:\r
48 main() creates one software timer, one queue, and two tasks.  It then starts the\r
49 scheduler.\r
50 \r
51 The Queue Send Task:\r
52 The queue send task is implemented by the prvQueueSendTask() function in this\r
53 file.  prvQueueSendTask() sits in a loop that causes it to repeatedly block for\r
54 200 milliseconds, before sending the value 100 to the queue that was created\r
55 within main().  Once the value is sent, the task loops back around to block for\r
56 another 200 milliseconds.\r
57 \r
58 The Queue Receive Task:\r
59 The queue receive task is implemented by the prvQueueReceiveTask() function\r
60 in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly blocks\r
61 on attempts to read data from the queue that was created within main().  When\r
62 data is received, the task checks the value of the data, and if the value equals\r
63 the expected 100, toggles the green LED.  The 'block time' parameter passed to\r
64 the queue receive function specifies that the task should be held in the Blocked\r
65 state indefinitely to wait for data to be available on the queue.  The queue\r
66 receive task will only leave the Blocked state when the queue send task writes\r
67 to the queue.  As the queue send task writes to the queue every 200\r
68 milliseconds, the queue receive task leaves the Blocked state every 200\r
69 milliseconds, and therefore toggles the green LED every 200 milliseconds.\r
70 \r
71 The LED Software Timer and the Button Interrupt:\r
72 The user button B1 is configured to generate an interrupt each time it is\r
73 pressed.  The interrupt service routine switches the red LED on, and resets the\r
74 LED software timer.  The LED timer has a 5000 millisecond (5 second) period, and\r
75 uses a callback function that is defined to just turn the red LED off.\r
76 Therefore, pressing the user button will turn the red LED on, and the LED will\r
77 remain on until a full five seconds pass without the button being pressed.\r
78 */\r
79 \r
80 \r
81 /* Kernel includes. */\r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 #include "queue.h"\r
85 #include "timers.h"\r
86 \r
87 /* STM32 Library includes. */\r
88 #include "stm32f10x.h"\r
89 #include "STM32vldiscovery.h"\r
90 \r
91 /* Priorities at which the tasks are created. */\r
92 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
93 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
94 \r
95 /* The rate at which data is sent to the queue, specified in milliseconds, and\r
96 converted to ticks using the portTICK_PERIOD_MS constant. */\r
97 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_PERIOD_MS )\r
98 \r
99 /* The number of items the queue can hold.  This is 1 as the receive task\r
100 will remove items as they are added, meaning the send task should always find\r
101 the queue empty. */\r
102 #define mainQUEUE_LENGTH                                        ( 1 )\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 /*\r
107  * Setup the NVIC, LED outputs, and button inputs.\r
108  */\r
109 static void prvSetupHardware( void );\r
110 \r
111 /*\r
112  * The tasks as described in the comments at the top of this file.\r
113  */\r
114 static void prvQueueReceiveTask( void *pvParameters );\r
115 static void prvQueueSendTask( void *pvParameters );\r
116 \r
117 /*\r
118  * The LED timer callback function.  This does nothing but switch the red LED\r
119  * off.\r
120  */\r
121 static void vLEDTimerCallback( TimerHandle_t xTimer );\r
122 \r
123 /*-----------------------------------------------------------*/\r
124 \r
125 /* The queue used by both tasks. */\r
126 static QueueHandle_t xQueue = NULL;\r
127 \r
128 /* The LED software timer.  This uses vLEDTimerCallback() as its callback\r
129  * function.\r
130  */\r
131 static TimerHandle_t xLEDTimer = NULL;\r
132 \r
133 /*-----------------------------------------------------------*/\r
134 \r
135 int main(void)\r
136 {\r
137         /* Configure the NVIC, LED outputs and button inputs. */\r
138         prvSetupHardware();\r
139 \r
140         /* Create the queue. */\r
141         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
142 \r
143         if( xQueue != NULL )\r
144         {\r
145                 /* Start the two tasks as described in the comments at the top of this\r
146                 file. */\r
147                 xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
148                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
149 \r
150                 /* Create the software timer that is responsible for turning off the LED\r
151                 if the button is not pushed within 5000ms, as described at the top of\r
152                 this file. */\r
153                 xLEDTimer = xTimerCreate(       "LEDTimer",                             /* A text name, purely to help debugging. */\r
154                                                                         ( 5000 / portTICK_PERIOD_MS ),/* The timer period, in this case 5000ms (5s). */\r
155                                                                         pdFALSE,                                        /* This is a one shot timer, so xAutoReload is set to pdFALSE. */\r
156                                                                         ( void * ) 0,                           /* The ID is not used, so can be set to anything. */\r
157                                                                         vLEDTimerCallback                       /* The callback function that switches the LED off. */\r
158                                                                 );\r
159 \r
160                 /* Start the tasks and timer running. */\r
161                 vTaskStartScheduler();\r
162         }\r
163 \r
164         /* If all is well, the scheduler will now be running, and the following line\r
165         will never be reached.  If the following line does execute, then there was\r
166         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
167         to be created.  See the memory management section on the FreeRTOS web site\r
168         for more details. */\r
169         for( ;; );\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 static void vLEDTimerCallback( TimerHandle_t xTimer )\r
174 {\r
175         /* The timer has expired - so no button pushes have occurred in the last\r
176         five seconds - turn the LED off.  NOTE - accessing the LED port should use\r
177         a critical section because it is accessed from multiple tasks, and the\r
178         button interrupt - in this trivial case, for simplicity, the critical\r
179         section is omitted. */\r
180         STM32vldiscovery_LEDOff( LED4 );\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 /* The ISR executed when the user button is pushed. */\r
185 void EXTI0_IRQHandler( void )\r
186 {\r
187 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
188 \r
189         /* The button was pushed, so ensure the LED is on before resetting the\r
190         LED timer.  The LED timer will turn the LED off if the button is not\r
191         pushed within 5000ms. */\r
192         STM32vldiscovery_LEDOn( LED4 );\r
193 \r
194         /* This interrupt safe FreeRTOS function can be called from this interrupt\r
195         because the interrupt priority is below the\r
196         configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
197         xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );\r
198 \r
199         /* Clear the interrupt before leaving. */\r
200         EXTI_ClearITPendingBit( EXTI_Line0 );\r
201 \r
202         /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
203         service/daemon task) to unblock, and the unblocked task has a priority\r
204         higher than or equal to the task that was interrupted, then\r
205         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
206         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
207         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 static void prvQueueSendTask( void *pvParameters )\r
212 {\r
213 TickType_t xNextWakeTime;\r
214 const unsigned long ulValueToSend = 100UL;\r
215 \r
216         /* Initialise xNextWakeTime - this only needs to be done once. */\r
217         xNextWakeTime = xTaskGetTickCount();\r
218 \r
219         for( ;; )\r
220         {\r
221                 /* Place this task in the blocked state until it is time to run again.\r
222                 The block time is specified in ticks, the constant used converts ticks\r
223                 to ms.  While in the Blocked state this task will not consume any CPU\r
224                 time. */\r
225                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
226 \r
227                 /* Send to the queue - causing the queue receive task to unblock and\r
228                 toggle an LED.  0 is used as the block time so the sending operation\r
229                 will not block - it shouldn't need to block as the queue should always\r
230                 be empty at this point in the code. */\r
231                 xQueueSend( xQueue, &ulValueToSend, 0 );\r
232         }\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 static void prvQueueReceiveTask( void *pvParameters )\r
237 {\r
238 unsigned long ulReceivedValue;\r
239 \r
240         for( ;; )\r
241         {\r
242                 /* Wait until something arrives in the queue - this task will block\r
243                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
244                 FreeRTOSConfig.h. */\r
245                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
246 \r
247                 /*  To get here something must have been received from the queue, but\r
248                 is it the expected value?  If it is, toggle the green LED. */\r
249                 if( ulReceivedValue == 100UL )\r
250                 {\r
251                         /* NOTE - accessing the LED port should use a critical section\r
252                         because it is accessed from multiple tasks, and the button interrupt\r
253                         - in this trivial case, for simplicity, the critical section is\r
254                         omitted. */\r
255                         STM32vldiscovery_LEDToggle( LED3 );\r
256                 }\r
257         }\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 static void prvSetupHardware( void )\r
262 {\r
263         /* Ensure that all 4 interrupt priority bits are used as the pre-emption\r
264         priority. */\r
265         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
266 \r
267         /* Set up the LED outputs and the button inputs. */\r
268         STM32vldiscovery_LEDInit( LED3 );\r
269         STM32vldiscovery_LEDInit( LED4 );\r
270         STM32vldiscovery_PBInit( BUTTON_USER, BUTTON_MODE_EXTI );\r
271 \r
272         /* Start with the LEDs off. */\r
273         STM32vldiscovery_LEDOff( LED3 );\r
274         STM32vldiscovery_LEDOff( LED4 );\r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 void vApplicationMallocFailedHook( void )\r
279 {\r
280         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
281         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
282         internally by FreeRTOS API functions that create tasks, queues, software\r
283         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
284         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
285         for( ;; );\r
286 }\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
290 {\r
291         ( void ) pcTaskName;\r
292         ( void ) pxTask;\r
293 \r
294         /* Run time stack overflow checking is performed if\r
295         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
296         function is called if a stack overflow is detected. */\r
297         for( ;; );\r
298 }\r
299 /*-----------------------------------------------------------*/\r
300 \r
301 void vApplicationIdleHook( void )\r
302 {\r
303 volatile size_t xFreeStackSpace;\r
304 \r
305         /* This function is called on each cycle of the idle task.  In this case it\r
306         does nothing useful, other than report the amout of FreeRTOS heap that\r
307         remains unallocated. */\r
308         xFreeStackSpace = xPortGetFreeHeapSize();\r
309 \r
310         if( xFreeStackSpace > 100 )\r
311         {\r
312                 /* By now, the kernel has allocated everything it is going to, so\r
313                 if there is a lot of heap remaining unallocated then\r
314                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
315                 reduced accordingly. */\r
316         }\r
317 }\r