测试低功耗模式
先搭建一个5s翻转一次的LED灯线程
执行一次翻转、IDLE5秒
void StartLEDTask(void const * argument)
{
/* USER CODE BEGIN 5 */
/* Infinite loop */
for(;;)
{
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
osDelay(5000);
}
/* USER CODE END 5 */
}
测试LED亮时 4.9ma
LED灭时 4.3ma
加入休眠程序
-
配置加入休眠
-
找例程
C:\Users\frank\STM32Cube\Repository\STM32Cube_FW_F4_V1.26.2\Projects\STM32469I_EVAL\Applications\FreeRTOS\FreeRTOS_LowPower\Src
在FREERTOS 例程里找到 lowpower例程
在其main.c文件中 找到:
void PreSleepProcessing(uint32_t ulExpectedIdleTime) { /* Called by the kernel before it places the MCU into a sleep mode because configPRE_SLEEP_PROCESSING() is #defined to PreSleepProcessing(). NOTE: Additional actions can be taken here to get the power consumption even lower. For example, peripherals can be turned off here, and then back on again in the post sleep processing function. For maximum power saving ensure all unused pins are in their lowest power state. */ /* Disable the peripheral clock during Low Power (Sleep) mode.*/ __HAL_RCC_GPIOG_CLK_SLEEP_DISABLE(); /* (ulExpectedIdleTime) is set to 0 to indicate that PreSleepProcessing contains its own wait for interrupt or wait for event instruction and so the kernel vPortSuppressTicksAndSleep function does not need to execute the wfi instruction */ (void) ulExpectedIdleTime; /*Enter to sleep Mode using the HAL function HAL_PWR_EnterSLEEPMode with WFI instruction*/ HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); } /** * @brief Post Sleep Processing * @param ulExpectedIdleTime : Not used * @retval None */ void PostSleepProcessing(uint32_t ulExpectedIdleTime) { /* Called by the kernel when the MCU exits a sleep mode because configPOST_SLEEP_PROCESSING is #defined to PostSleepProcessing(). */ /* Avoid compiler warnings about the unused parameter. */ (void) ulExpectedIdleTime; }
其中
void PreSleepProcessing(uint32_t ulExpectedIdleTime)
和void PostSleepProcessing(uint32_t ulExpectedIdleTime)
是两个主要函数 -
改代码
直接运行会报错
__HAL_RCC_GPIOG_CLK_SLEEP_DISABLE();
这里GPIOG
不一定是自己MCU有的IO,改成GPIOA,并ctrl点入,看下自己MCU都有哪些IO,全都给禁了,例如我是F411,改为:
__weak void PreSleepProcessing(uint32_t ulExpectedIdleTime)
{
/* place for user code */
/* Called by the kernel before it places the MCU into a sleep mode because
configPRE_SLEEP_PROCESSING() is #defined to PreSleepProcessing().
NOTE: Additional actions can be taken here to get the power consumption
even lower. For example, peripherals can be turned off here, and then back
on again in the post sleep processing function. For maximum power saving
ensure all unused pins are in their lowest power state. */
/* Disable the peripheral clock during Low Power (Sleep) mode.*/
__HAL_RCC_GPIOA_CLK_SLEEP_DISABLE();
__HAL_RCC_GPIOB_CLK_SLEEP_DISABLE();
__HAL_RCC_GPIOC_CLK_SLEEP_DISABLE();
__HAL_RCC_GPIOH_CLK_SLEEP_DISABLE();
/*
(ulExpectedIdleTime) is set to 0 to indicate that PreSleepProcessing contains
its own wait for interrupt or wait for event instruction and so the kernel vPortSuppressTicksAndSleep
function does not need to execute the wfi instruction
*/
(void) ulExpectedIdleTime;
/*Enter to sleep Mode using the HAL function HAL_PWR_EnterSLEEPMode with WFI instruction*/
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
}
__weak void PostSleepProcessing(uint32_t ulExpectedIdleTime)
{
/* place for user code */
/* Called by the kernel when the MCU exits a sleep mode because
configPOST_SLEEP_PROCESSING is #defined to PostSleepProcessing(). */
/* Avoid compiler warnings about the unused parameter. */
(void) ulExpectedIdleTime;
}
以上函数在freertos.c中
测试 灭时1.6ma
亮时2.1ma