]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32L152_Discovery_IAR/main_low_power.c
b716d745fd12502e5e88c8848465ac2c9b2cffed
[freertos] / FreeRTOS / Demo / CORTEX_STM32L152_Discovery_IAR / main_low_power.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  * When configCREATE_LOW_POWER_DEMO is set to 1 in FreeRTOSConfig.h main() will\r
30  * call main_low_power(), which is defined in this file.  main_low_power()\r
31  * demonstrates FreeRTOS tick suppression being used to allow the MCU to be\r
32  * placed into the Sleep, Low Power Sleep and Stop low power modes.  When\r
33  * configCREATE_LOW_POWER_DEMO is set to 0 main will instead call main_full(),\r
34  * which is a more comprehensive RTOS demonstration.\r
35  * ****************************************************************************\r
36  *\r
37  * This application demonstrates the FreeRTOS tickless idle mode (tick\r
38  * suppression) being used to allow the STM32L to enter various low power modes\r
39  * during extended idle periods.  See\r
40  * http://www.freertos.org/low-power-tickless-rtos.html for information on\r
41  * tickless operation.\r
42  *\r
43  * Deeper low power modes have longer wake up periods that lighter low power\r
44  * modes, and power is also used simply entering and especially exiting the low\r
45  * power modes.  How the low power modes are used therefore requires careful\r
46  * consideration to ensure power consumption is truly minimised and that the\r
47  * embedded device meets its real time requirements.  This demo is configured to\r
48  * select between four different modes depending on the anticipated idle period.\r
49  * Note the time thresholds used to decide which low power mode to enter are\r
50  * purely for convenience of demonstration and are not intended to represent\r
51  * optimal values for any particular application.\r
52  *\r
53  * The STM32L specific part of the tickless operation is implemented in\r
54  * STM32L_low_power_tick_management.c.\r
55  *\r
56  * The demo is configured to execute on the STM32L Discovery board.\r
57  *\r
58  * Functionality:\r
59  *\r
60  *  + Two tasks are created, an Rx task and a Tx task.  A queue is created to\r
61  *        pass a message from the Tx task to the Rx task.\r
62  *\r
63  *  + The Rx task blocks on a queue to wait for data, blipping an LED each time\r
64  *    data is received (turning it on and then off again) before returning to\r
65  *    block on the queue once more.\r
66  *\r
67  *  + The Tx task repeatedly blocks on an attempt to obtain a semaphore, and\r
68  *        unblocks if either the semaphore is received or its block time expires.\r
69  *        After leaving the blocked state the Tx task uses the queue to send a\r
70  *        value to the Rx task, which in turn causes the Rx task to exit the\r
71  *        Blocked state and blip the LED.  The rate at which the LED is seen to blip\r
72  *        is therefore dependent on the block time.\r
73  *\r
74  *  + The Tx task's block time is changed by the interrupt service routine that\r
75  *        executes when the USER button is pressed.  The low power mode entered\r
76  *        depends on the block time (as described in the Observed Behaviour section\r
77  *        below).  Four block times are used: short, medium, long and infinite.\r
78  *\r
79  * Observed behaviour:\r
80  *\r
81  * 1) The block time used by the Tx task is initialised to its 'short' value,\r
82  * so when the Tx task blocks on the semaphore it times-out quickly, resulting\r
83  * in the LED toggling rapidly.  The timeout period is less than the value of\r
84  * configEXPECTED_IDLE_TIME_BEFORE_SLEEP (set in FreeRTOSConfig.h), so the\r
85  * initial state does not suppress the tick interrupt or enter a low power mode.\r
86  *\r
87  * 2) When the button is pressed the block time used by the Tx task is increased\r
88  * to its 'medium' value.  The longer block time results in a slowing of the\r
89  * rate at which the LED toggles.  The time the Tx task spends in the blocked\r
90  * state is now greater than configEXPECTED_IDLE_TIME_BEFORE_SLEEP, so the tick\r
91  * is suppressed.  The MCU is placed into the 'Sleep' low power state while the\r
92  * tick is suppressed.\r
93  *\r
94  * 3) When the button is pressed again the block time used by the Tx task is\r
95  * increased to its 'long' value, so the rate at which the LED is observed to\r
96  * blip gets even slow.  When the 'long' block time is used the MCU is placed\r
97  * into its 'Low Power Sleep' low power state.\r
98  *\r
99  * 4) The next time the button is pressed the block time used by the Tx task is\r
100  * set to infinite, so the Tx task does not time out when it attempts to obtain\r
101  * the semaphore, and therefore the LED stops blipping completely.  Both tasks\r
102  * are now blocked indefinitely and the MCU is placed into its 'Stop' low power\r
103  * state.\r
104  *\r
105  * 5) Pressing the button one final time results in the semaphore being 'given'\r
106  * to unblock the Tx task, the CPU clocks being returned to their pre-stop\r
107  * state, and the block time being reset to its 'short' time.  The system is\r
108  * then back to its initial condition with the LED blipping rapidly.\r
109  *\r
110  */\r
111 \r
112 /* Kernel includes. */\r
113 #include "FreeRTOS.h"\r
114 #include "task.h"\r
115 #include "queue.h"\r
116 #include "semphr.h"\r
117 \r
118 /* ST library functions. */\r
119 #include "stm32l1xx.h"\r
120 #include "discover_board.h"\r
121 #include "stm32l_discovery_lcd.h"\r
122 \r
123 /* Priorities at which the Rx and Tx tasks are created. */\r
124 #define configQUEUE_RECEIVE_TASK_PRIORITY       ( tskIDLE_PRIORITY + 1 )\r
125 #define configQUEUE_SEND_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
126 \r
127 /* The number of items the queue can hold.  This is 1 as the Rx task will\r
128 remove items as they are added so the Tx task should always find the queue\r
129 empty. */\r
130 #define mainQUEUE_LENGTH                                        ( 1 )\r
131 \r
132 /* A block time of zero simply means "don't block". */\r
133 #define mainDONT_BLOCK                                          ( 0 )\r
134 \r
135 /* The value that is sent from the Tx task to the Rx task on the queue. */\r
136 #define mainQUEUED_VALUE                                        ( 100UL )\r
137 \r
138 /* The length of time the LED will remain on for. */\r
139 #define mainLED_TOGGLE_DELAY                            ( 10 / portTICK_PERIOD_MS )\r
140 \r
141 /*-----------------------------------------------------------*/\r
142 \r
143 /*\r
144  * The Rx and Tx tasks as described at the top of this file.\r
145  */\r
146 static void prvQueueReceiveTask( void *pvParameters );\r
147 static void prvQueueSendTask( void *pvParameters );\r
148 \r
149 /*-----------------------------------------------------------*/\r
150 \r
151 /* The queue used to pass data from the Tx task to the Rx task. */\r
152 static QueueHandle_t xQueue = NULL;\r
153 \r
154 /*-----------------------------------------------------------*/\r
155 \r
156 /* Holds the block time used by the Tx task. */\r
157 TickType_t xSendBlockTime = ( 100UL / portTICK_PERIOD_MS );\r
158 \r
159 /* The lower an upper limits of the block time.  An infinite block time is used\r
160 if xSendBlockTime is incremented past xMaxBlockTime. */\r
161 static const TickType_t xMaxBlockTime = ( 500L / portTICK_PERIOD_MS ), xMinBlockTime = ( 100L / portTICK_PERIOD_MS );\r
162 \r
163 /* The semaphore on which the Tx task blocks. */\r
164 static SemaphoreHandle_t xTxSemaphore = NULL;\r
165 \r
166 /*-----------------------------------------------------------*/\r
167 \r
168 /* See the comments at the top of the file. */\r
169 void main_low_power( void )\r
170 {\r
171         /* Create the semaphore as described at the top of this file. */\r
172         xTxSemaphore = xSemaphoreCreateBinary();\r
173         configASSERT( xTxSemaphore );\r
174 \r
175         /* Create the queue as described at the top of this file. */\r
176         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
177         configASSERT( xQueue );\r
178 \r
179         /* Start the two tasks as described at the top of this file. */\r
180         xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, configQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
181         xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, configQUEUE_SEND_TASK_PRIORITY, NULL );\r
182 \r
183         /* Start the scheduler running running. */\r
184         vTaskStartScheduler();\r
185 \r
186         /* If all is well the next line of code will not be reached as the\r
187         scheduler will be running.  If the next line is reached then it is likely\r
188         there was insufficient FreeRTOS heap available for the idle task and/or\r
189         timer task to be created.  See http://www.freertos.org/a00111.html. */\r
190         for( ;; );\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 static void prvQueueSendTask( void *pvParameters )\r
195 {\r
196 const unsigned long ulValueToSend = mainQUEUED_VALUE;\r
197 \r
198         /* Remove compiler warning about unused parameter. */\r
199         ( void ) pvParameters;\r
200 \r
201         for( ;; )\r
202         {\r
203                 /* Enter the Blocked state to wait for the semaphore.  The task will\r
204                 leave the Blocked state if either the semaphore is received or\r
205                 xSendBlockTime ticks pass without the semaphore being received. */\r
206                 xSemaphoreTake( xTxSemaphore, xSendBlockTime );\r
207 \r
208                 /* Send to the queue - causing the Tx task to flash its LED. */\r
209                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );\r
210         }\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 static void prvQueueReceiveTask( void *pvParameters )\r
215 {\r
216 unsigned long ulReceivedValue;\r
217 \r
218         /* Remove compiler warning about unused parameter. */\r
219         ( void ) pvParameters;\r
220 \r
221         for( ;; )\r
222         {\r
223                 /* Wait until something arrives in the queue. */\r
224                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
225 \r
226                 /*  To get here something must have arrived, but is it the expected\r
227                 value?  If it is, turn the LED on for a short while. */\r
228                 if( ulReceivedValue == mainQUEUED_VALUE )\r
229                 {\r
230                         /* LED on... */\r
231                         GPIO_HIGH( LD_GPIO_PORT, LD_GREEN_GPIO_PIN );\r
232 \r
233                         /* ... short delay ... */\r
234                         vTaskDelay( mainLED_TOGGLE_DELAY );\r
235 \r
236                         /* ... LED off again. */\r
237                         GPIO_LOW( LD_GPIO_PORT, LD_GREEN_GPIO_PIN );\r
238                 }\r
239         }\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 /* Handles interrupts generated by pressing the USER button. */\r
244 void EXTI0_IRQHandler(void)\r
245 {\r
246 static const TickType_t xIncrement = 200UL / portTICK_PERIOD_MS;\r
247 \r
248         /* If xSendBlockTime is already portMAX_DELAY then the Tx task was blocked\r
249         indefinitely, and this interrupt is bringing the MCU out of STOP low power\r
250         mode. */\r
251         if( xSendBlockTime == portMAX_DELAY )\r
252         {\r
253                 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
254 \r
255                 /* Unblock the Tx task. */\r
256                 xSemaphoreGiveFromISR( xTxSemaphore, &xHigherPriorityTaskWoken );\r
257 \r
258                 /* Start over with the 'short' block time as described at the top of\r
259                 this file. */\r
260                 xSendBlockTime = xMinBlockTime;\r
261 \r
262                 /* Request a yield if calling xSemaphoreGiveFromISR() caused a task to\r
263                 leave the Blocked state (which it will have done) and the task that left\r
264                 the Blocked state has a priority higher than the currently running task\r
265                 (which it will have). */\r
266                 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
267         }\r
268         else\r
269         {\r
270                 /* Increase the block time used by the Tx task, as described at the top\r
271                 of this file. */\r
272                 xSendBlockTime += xIncrement;\r
273 \r
274                 /* If the block time has gone over the configured maximum then set it to\r
275                 an infinite block time to allow the MCU to go into its STOP low power\r
276                 mode. */\r
277                 if( xSendBlockTime > xMaxBlockTime )\r
278                 {\r
279                         xSendBlockTime = portMAX_DELAY;\r
280                 }\r
281         }\r
282 \r
283         EXTI_ClearITPendingBit( EXTI_Line0 );\r
284 }\r
285 /*-----------------------------------------------------------*/\r
286 \r
287 /* The configPOST_STOP_PROCESSING() macro is called when the MCU leaves its\r
288 STOP low power mode.  The macro is set in FreeRTOSConfig.h to call\r
289 vMainPostStopProcessing(). */\r
290 void vMainPostStopProcessing( void )\r
291 {\r
292 extern void SetSysClock( void );\r
293 \r
294         /* The STOP low power mode has been exited.  Reconfigure the system clocks\r
295         ready for normally running again. */\r
296         SetSysClock();\r
297 }\r
298 /*-----------------------------------------------------------*/\r