测试STM32 RTOS 低功耗模式

测试低功耗模式

先搭建一个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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值