]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32L152_Discovery_IAR/main_low_power.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / CORTEX_STM32L152_Discovery_IAR / main_low_power.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* ****************************************************************************\r
67  * When configCREATE_LOW_POWER_DEMO is set to 1 in FreeRTOSConfig.h main() will\r
68  * call main_low_power(), which is defined in this file.  main_low_power()\r
69  * demonstrates FreeRTOS tick suppression being used to allow the MCU to be\r
70  * placed into the Sleep, Low Power Sleep and Stop low power modes.  When\r
71  * configCREATE_LOW_POWER_DEMO is set to 0 main will instead call main_full(),\r
72  * which is a more comprehensive RTOS demonstration.\r
73  * ****************************************************************************\r
74  *\r
75  * This application demonstrates the FreeRTOS tickless idle mode (tick\r
76  * suppression) being used to allow the STM32L to enter various low power modes\r
77  * during extended idle periods.  See\r
78  * http://www.freertos.org/low-power-tickless-rtos.html for information on\r
79  * tickless operation.\r
80  *\r
81  * Deeper low power modes have longer wake up periods that lighter low power\r
82  * modes, and power is also used simply entering and especially exiting the low\r
83  * power modes.  How the low power modes are used therefore requires careful\r
84  * consideration to ensure power consumption is truly minimised and that the\r
85  * embedded device meets its real time requirements.  This demo is configured to\r
86  * select between four different modes depending on the anticipated idle period.\r
87  * Note the time thresholds used to decide which low power mode to enter are\r
88  * purely for convenience of demonstration and are not intended to represent\r
89  * optimal values for any particular application.\r
90  *\r
91  * The STM32L specific part of the tickless operation is implemented in\r
92  * STM32L_low_power_tick_management.c.\r
93  *\r
94  * The demo is configured to execute on the STM32L Discovery board.\r
95  *\r
96  * Functionality:\r
97  *\r
98  *  + Two tasks are created, an Rx task and a Tx task.  A queue is created to\r
99  *        pass a message from the Tx task to the Rx task.\r
100  *\r
101  *  + The Rx task blocks on a queue to wait for data, blipping an LED each time\r
102  *    data is received (turning it on and then off again) before returning to\r
103  *    block on the queue once more.\r
104  *\r
105  *  + The Tx task repeatedly blocks on an attempt to obtain a semaphore, and\r
106  *        unblocks if either the semaphore is received or its block time expires.\r
107  *        After leaving the blocked state the Tx task uses the queue to send a\r
108  *        value to the Rx task, which in turn causes the Rx task to exit the\r
109  *        Blocked state and blip the LED.  The rate at which the LED is seen to blip\r
110  *        is therefore dependent on the block time.\r
111  *\r
112  *  + The Tx task's block time is changed by the interrupt service routine that\r
113  *        executes when the USER button is pressed.  The low power mode entered\r
114  *        depends on the block time (as described in the Observed Behaviour section\r
115  *        below).  Four block times are used: short, medium, long and infinite.\r
116  *\r
117  * Observed behaviour:\r
118  *\r
119  * 1) The block time used by the Tx task is initialised to its 'short' value,\r
120  * so when the Tx task blocks on the semaphore it times-out quickly, resulting\r
121  * in the LED toggling rapidly.  The timeout period is less than the value of\r
122  * configEXPECTED_IDLE_TIME_BEFORE_SLEEP (set in FreeRTOSConfig.h), so the\r
123  * initial state does not suppress the tick interrupt or enter a low power mode.\r
124  *\r
125  * 2) When the button is pressed the block time used by the Tx task is increased\r
126  * to its 'medium' value.  The longer block time results in a slowing of the\r
127  * rate at which the LED toggles.  The time the Tx task spends in the blocked\r
128  * state is now greater than configEXPECTED_IDLE_TIME_BEFORE_SLEEP, so the tick\r
129  * is suppressed.  The MCU is placed into the 'Sleep' low power state while the\r
130  * tick is suppressed.\r
131  *\r
132  * 3) When the button is pressed again the block time used by the Tx task is\r
133  * increased to its 'long' value, so the rate at which the LED is observed to\r
134  * blip gets even slow.  When the 'long' block time is used the MCU is placed\r
135  * into its 'Low Power Sleep' low power state.\r
136  *\r
137  * 4) The next time the button is pressed the block time used by the Tx task is\r
138  * set to infinite, so the Tx task does not time out when it attempts to obtain\r
139  * the semaphore, and therefore the LED stops blipping completely.  Both tasks\r
140  * are now blocked indefinitely and the MCU is placed into its 'Stop' low power\r
141  * state.\r
142  *\r
143  * 5) Pressing the button one final time results in the semaphore being 'given'\r
144  * to unblock the Tx task, the CPU clocks being returned to their pre-stop\r
145  * state, and the block time being reset to its 'short' time.  The system is\r
146  * then back to its initial condition with the LED blipping rapidly.\r
147  *\r
148  */\r
149 \r
150 /* Kernel includes. */\r
151 #include "FreeRTOS.h"\r
152 #include "task.h"\r
153 #include "queue.h"\r
154 #include "semphr.h"\r
155 \r
156 /* ST library functions. */\r
157 #include "stm32l1xx.h"\r
158 #include "discover_board.h"\r
159 #include "stm32l_discovery_lcd.h"\r
160 \r
161 /* Priorities at which the Rx and Tx tasks are created. */\r
162 #define configQUEUE_RECEIVE_TASK_PRIORITY       ( tskIDLE_PRIORITY + 1 )\r
163 #define configQUEUE_SEND_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
164 \r
165 /* The number of items the queue can hold.  This is 1 as the Rx task will\r
166 remove items as they are added so the Tx task should always find the queue\r
167 empty. */\r
168 #define mainQUEUE_LENGTH                                        ( 1 )\r
169 \r
170 /* A block time of zero simply means "don't block". */\r
171 #define mainDONT_BLOCK                                          ( 0 )\r
172 \r
173 /* The value that is sent from the Tx task to the Rx task on the queue. */\r
174 #define mainQUEUED_VALUE                                        ( 100UL )\r
175 \r
176 /* The length of time the LED will remain on for. */\r
177 #define mainLED_TOGGLE_DELAY                            ( 10 / portTICK_PERIOD_MS )\r
178 \r
179 /*-----------------------------------------------------------*/\r
180 \r
181 /*\r
182  * The Rx and Tx tasks as described at the top of this file.\r
183  */\r
184 static void prvQueueReceiveTask( void *pvParameters );\r
185 static void prvQueueSendTask( void *pvParameters );\r
186 \r
187 /*-----------------------------------------------------------*/\r
188 \r
189 /* The queue used to pass data from the Tx task to the Rx task. */\r
190 static QueueHandle_t xQueue = NULL;\r
191 \r
192 /*-----------------------------------------------------------*/\r
193 \r
194 /* Holds the block time used by the Tx task. */\r
195 TickType_t xSendBlockTime = ( 100UL / portTICK_PERIOD_MS );\r
196 \r
197 /* The lower an upper limits of the block time.  An infinite block time is used\r
198 if xSendBlockTime is incremented past xMaxBlockTime. */\r
199 static const TickType_t xMaxBlockTime = ( 500L / portTICK_PERIOD_MS ), xMinBlockTime = ( 100L / portTICK_PERIOD_MS );\r
200 \r
201 /* The semaphore on which the Tx task blocks. */\r
202 static SemaphoreHandle_t xTxSemaphore = NULL;\r
203 \r
204 /*-----------------------------------------------------------*/\r
205 \r
206 /* See the comments at the top of the file. */\r
207 void main_low_power( void )\r
208 {\r
209         /* Create the semaphore as described at the top of this file. */\r
210         xTxSemaphore = xSemaphoreCreateBinary();\r
211         configASSERT( xTxSemaphore );\r
212 \r
213         /* Create the queue as described at the top of this file. */\r
214         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
215         configASSERT( xQueue );\r
216 \r
217         /* Start the two tasks as described at the top of this file. */\r
218         xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, configQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
219         xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, configQUEUE_SEND_TASK_PRIORITY, NULL );\r
220 \r
221         /* Start the scheduler running running. */\r
222         vTaskStartScheduler();\r
223 \r
224         /* If all is well the next line of code will not be reached as the\r
225         scheduler will be running.  If the next line is reached then it is likely\r
226         there was insufficient FreeRTOS heap available for the idle task and/or\r
227         timer task to be created.  See http://www.freertos.org/a00111.html. */\r
228         for( ;; );\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 static void prvQueueSendTask( void *pvParameters )\r
233 {\r
234 const unsigned long ulValueToSend = mainQUEUED_VALUE;\r
235 \r
236         /* Remove compiler warning about unused parameter. */\r
237         ( void ) pvParameters;\r
238 \r
239         for( ;; )\r
240         {\r
241                 /* Enter the Blocked state to wait for the semaphore.  The task will\r
242                 leave the Blocked state if either the semaphore is received or\r
243                 xSendBlockTime ticks pass without the semaphore being received. */\r
244                 xSemaphoreTake( xTxSemaphore, xSendBlockTime );\r
245 \r
246                 /* Send to the queue - causing the Tx task to flash its LED. */\r
247                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );\r
248         }\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r
252 static void prvQueueReceiveTask( void *pvParameters )\r
253 {\r
254 unsigned long ulReceivedValue;\r
255 \r
256         /* Remove compiler warning about unused parameter. */\r
257         ( void ) pvParameters;\r
258 \r
259         for( ;; )\r
260         {\r
261                 /* Wait until something arrives in the queue. */\r
262                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
263 \r
264                 /*  To get here something must have arrived, but is it the expected\r
265                 value?  If it is, turn the LED on for a short while. */\r
266                 if( ulReceivedValue == mainQUEUED_VALUE )\r
267                 {\r
268                         /* LED on... */\r
269                         GPIO_HIGH( LD_GPIO_PORT, LD_GREEN_GPIO_PIN );\r
270 \r
271                         /* ... short delay ... */\r
272                         vTaskDelay( mainLED_TOGGLE_DELAY );\r
273 \r
274                         /* ... LED off again. */\r
275                         GPIO_LOW( LD_GPIO_PORT, LD_GREEN_GPIO_PIN );\r
276                 }\r
277         }\r
278 }\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 /* Handles interrupts generated by pressing the USER button. */\r
282 void EXTI0_IRQHandler(void)\r
283 {\r
284 static const TickType_t xIncrement = 200UL / portTICK_PERIOD_MS;\r
285 \r
286         /* If xSendBlockTime is already portMAX_DELAY then the Tx task was blocked\r
287         indefinitely, and this interrupt is bringing the MCU out of STOP low power\r
288         mode. */\r
289         if( xSendBlockTime == portMAX_DELAY )\r
290         {\r
291                 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
292 \r
293                 /* Unblock the Tx task. */\r
294                 xSemaphoreGiveFromISR( xTxSemaphore, &xHigherPriorityTaskWoken );\r
295 \r
296                 /* Start over with the 'short' block time as described at the top of\r
297                 this file. */\r
298                 xSendBlockTime = xMinBlockTime;\r
299 \r
300                 /* Request a yield if calling xSemaphoreGiveFromISR() caused a task to\r
301                 leave the Blocked state (which it will have done) and the task that left\r
302                 the Blocked state has a priority higher than the currently running task\r
303                 (which it will have). */\r
304                 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
305         }\r
306         else\r
307         {\r
308                 /* Increase the block time used by the Tx task, as described at the top\r
309                 of this file. */\r
310                 xSendBlockTime += xIncrement;\r
311 \r
312                 /* If the block time has gone over the configured maximum then set it to\r
313                 an infinite block time to allow the MCU to go into its STOP low power\r
314                 mode. */\r
315                 if( xSendBlockTime > xMaxBlockTime )\r
316                 {\r
317                         xSendBlockTime = portMAX_DELAY;\r
318                 }\r
319         }\r
320 \r
321         EXTI_ClearITPendingBit( EXTI_Line0 );\r
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 /* The configPOST_STOP_PROCESSING() macro is called when the MCU leaves its\r
326 STOP low power mode.  The macro is set in FreeRTOSConfig.h to call\r
327 vMainPostStopProcessing(). */\r
328 void vMainPostStopProcessing( void )\r
329 {\r
330 extern void SetSysClock( void );\r
331 \r
332         /* The STOP low power mode has been exited.  Reconfigure the system clocks\r
333         ready for normally running again. */\r
334         SetSysClock();\r
335 }\r
336 /*-----------------------------------------------------------*/\r