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