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