906|1

16

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

【测评STM32L452Nucleo-64】低功耗的使用和代码撰写 [复制链接]

Stm32低功耗模式分为四种分别是运行模式、睡眠模式、停止模式、待机模式

其中运行模式就是我们平时正常工作时所使用的模式,就不做过多赘述。

睡眠模式进入方式使内核寄存器SLEEPDEEP=0,调用WFI(中断方式)或者WFE(事件方式)进入睡眠模式,SLEEPONEXIT=0,立即睡眠,SLEEPONEXIT=1,退出时睡眠,唤醒时无延迟,没有关闭外设,唤醒后执行中断模式,RAM并没有断电。

 

停止模式进入方式是内核寄存器SLEEPDEEP=1,PWR_CR寄存器PDDS=0,调用WFI或者WFE进入停止模式,其中;

PWR_CR寄存器LPDS=0,调压器正常

PWR_CR寄存器LPDS=1,低功耗模式

PWR_CR寄存器FPDS=0,Flash正常

PWR_CR寄存器FPDS=0,Flash掉电

睡眠时会关闭内核时钟、关闭外设时钟、但是保留运行数据,再次唤醒时会有延时首先需要唤醒外部时钟,flash恢复时间,唤醒执行完中断函数后会继续进行睡眠模式。

 

待机模式进入方式是内核寄存器SLEEPDEEP=1,PWR_CR寄存器PDDS=1,PWR_CR寄存器WUF=0,调用WFI或者WFE进入停止模式;使用WKUP上升沿、RTC闹钟、看门狗唤醒,

睡眠是会关闭内核、关闭外设、内存数据丢失,开启时相当于重新开机执行while函数。

在HAL函数库电源函数部分中中对各函数已经进行了定义

其中睡眠模式函数如下:

/**
  * [url=home.php?mod=space&uid=159083]@brief[/url] Enter Sleep or Low-power Sleep mode.
  * @note  In Sleep/Low-power Sleep mode, all I/O pins keep the same state as in Run mode.
  * @param Regulator: Specifies the regulator state in Sleep/Low-power Sleep mode.
  *          This parameter can be one of the following values:
  *            [url=home.php?mod=space&uid=1238002]@arg[/url] [url=home.php?mod=space&uid=1064992]@ref[/url] PWR_MAINREGULATOR_ON Sleep mode (regulator in main mode)
  *            @arg @ref PWR_LOWPOWERREGULATOR_ON Low-power Sleep mode (regulator in low-power mode)
  * @note  Low-power Sleep mode is entered from Low-power Run mode. Therefore, if not yet
  *        in Low-power Run mode before calling HAL_PWR_EnterSLEEPMode() with Regulator set
  *        to PWR_LOWPOWERREGULATOR_ON, the user can optionally configure the
  *        Flash in power-down monde in setting the SLEEP_PD bit in FLASH_ACR register.
  *        Additionally, the clock frequency must be reduced below 2 MHz.
  *        Setting SLEEP_PD in FLASH_ACR then appropriately reducing the clock frequency must
  *        be done before calling HAL_PWR_EnterSLEEPMode() API.
  * @note  When exiting Low-power Sleep mode, the MCU is in Low-power Run mode. To move in
  *        Run mode, the user must resort to HAL_PWREx_DisableLowPowerRunMode() API.
  * @param SLEEPEntry: Specifies if Sleep mode is entered with WFI or WFE instruction.
  *           This parameter can be one of the following values:
  *            @arg @ref PWR_SLEEPENTRY_WFI enter Sleep or Low-power Sleep mode with WFI instruction
  *            @arg @ref PWR_SLEEPENTRY_WFE enter Sleep or Low-power Sleep mode with WFE instruction
  * @note  When WFI entry is used, tick interrupt have to be disabled if not desired as
  *        the interrupt wake up source.
  * @retval None
  */
void HAL_PWR_EnterSLEEPMode(uint32_t Regulator, uint8_t SLEEPEntry)
{
  /* Check the parameters */
  assert_param(IS_PWR_REGULATOR(Regulator));
  assert_param(IS_PWR_SLEEP_ENTRY(SLEEPEntry));

  /* Set Regulator parameter */
  if (Regulator == PWR_MAINREGULATOR_ON)
  {
    /* If in low-power run mode at this point, exit it */
    if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_REGLPF))
    {
      if (HAL_PWREx_DisableLowPowerRunMode() != HAL_OK)
      {
        return ;
      }
    }
    /* Regulator now in main mode. */
  }
  else
  {
    /* If in run mode, first move to low-power run mode.
       The system clock frequency must be below 2 MHz at this point. */
    if (HAL_IS_BIT_SET(PWR->SR2, PWR_SR2_REGLPF) == RESET)
    {
      HAL_PWREx_EnableLowPowerRunMode();
    }
  }

  /* Clear SLEEPDEEP bit of Cortex System Control Register */
  CLEAR_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));

  /* Select SLEEP mode entry -------------------------------------------------*/
  if(SLEEPEntry == PWR_SLEEPENTRY_WFI)
  {
    /* Request Wait For Interrupt */
    __WFI();
  }
  else
  {
    /* Request Wait For Event */
    __SEV();
    __WFE();
    __WFE();
  }

}

函数库对于各变量的定义都有严格的说明

在main函数中调用如下

//睡眠模式
        printf("aaaa\r\n");//a可以打印完全
		HAL_SuspendTick ();
		HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON,PWR_SLEEPENTRY_WFI);//进入睡眠模式
		printf ("aaa\r\n");//b也可以被打印

停止模式定义函数如下:

/**
  * @brief Enter Stop mode
  * @note  This API is named HAL_PWR_EnterSTOPMode to ensure compatibility with legacy code running
  *        on devices where only "Stop mode" is mentioned with main or low power regulator ON.
  * @note  In Stop mode, all I/O pins keep the same state as in Run mode.
  * @note  All clocks in the VCORE domain are stopped; the PLL, the MSI,
  *        the HSI and the HSE oscillators are disabled. Some peripherals with the wakeup capability
  *        (I2Cx, USARTx and LPUART) can switch on the HSI to receive a frame, and switch off the HSI
  *        after receiving the frame if it is not a wakeup frame. In this case, the HSI clock is propagated
  *        only to the peripheral requesting it.
  *        SRAM1, SRAM2 and register contents are preserved.
  *        The BOR is available.
  *        The voltage regulator can be configured either in normal (Stop 0) or low-power mode (Stop 1).
  * @note  When exiting Stop 0 or Stop 1 mode by issuing an interrupt or a wakeup event,
  *         the HSI RC oscillator is selected as system clock if STOPWUCK bit in RCC_CFGR register
  *         is set; the MSI oscillator is selected if STOPWUCK is cleared.
  * @note  When the voltage regulator operates in low power mode (Stop 1), an additional
  *         startup delay is incurred when waking up.
  *         By keeping the internal regulator ON during Stop mode (Stop 0), the consumption
  *         is higher although the startup time is reduced.
  * @param Regulator: Specifies the regulator state in Stop mode.
  *          This parameter can be one of the following values:
  *            @arg @ref PWR_MAINREGULATOR_ON  Stop 0 mode (main regulator ON)
  *            @arg @ref PWR_LOWPOWERREGULATOR_ON  Stop 1 mode (low power regulator ON)
  * @param STOPEntry: Specifies Stop 0 or Stop 1 mode is entered with WFI or WFE instruction.
  *          This parameter can be one of the following values:
  *            @arg @ref PWR_STOPENTRY_WFI  Enter Stop 0 or Stop 1 mode with WFI instruction.
  *            @arg @ref PWR_STOPENTRY_WFE  Enter Stop 0 or Stop 1 mode with WFE instruction.
  * @retval None
  */
void HAL_PWR_EnterSTOPMode(uint32_t Regulator, uint8_t STOPEntry)
{
  /* Check the parameters */
  assert_param(IS_PWR_REGULATOR(Regulator));

  if(Regulator == PWR_LOWPOWERREGULATOR_ON)
  {
    HAL_PWREx_EnterSTOP1Mode(STOPEntry);
  }
  else
  {
    HAL_PWREx_EnterSTOP0Mode(STOPEntry);
  }
}

在主函数中调用如下;

//停止模式对于唤醒时外部时钟设置
  __HAL_RCC_HSE_CONFIG (RCC_HSE_ON );
	__HAL_RCC_PLL_ENABLE ();
	__HAL_RCC_SYSCLK_CONFIG (RCC_SYSCLKSOURCE_PLLCLK );
printf("aaaa\r\n");//不一定能打印完全可以增加延迟
		HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON,PWR_SLEEPENTRY_WFI );
		printf ("aaa\r\n");//恢复时打印完全

其中分为两个部分写在while外是在唤醒停止模式时外部时钟的定义,在while里是进入和唤醒停止模式

待机模式函数如下:

/**
  * @brief Enter Standby mode.
  * @note  In Standby mode, the PLL, the HSI, the MSI and the HSE oscillators are switched
  *        off. The voltage regulator is disabled, except when SRAM2 content is preserved
  *        in which case the regulator is in low-power mode.
  *        SRAM1 and register contents are lost except for registers in the Backup domain and
  *        Standby circuitry. SRAM2 content can be preserved if the bit RRS is set in PWR_CR3 register.
  *        To enable this feature, the user can resort to HAL_PWREx_EnableSRAM2ContentRetention() API
  *        to set RRS bit.
  *        The BOR is available.
  * @note  The I/Os can be configured either with a pull-up or pull-down or can be kept in analog state.
  *        HAL_PWREx_EnableGPIOPullUp() and HAL_PWREx_EnableGPIOPullDown() respectively enable Pull Up and
  *        Pull Down state, HAL_PWREx_DisableGPIOPullUp() and HAL_PWREx_DisableGPIOPullDown() disable the
  *        same.
  *        These states are effective in Standby mode only if APC bit is set through
  *        HAL_PWREx_EnablePullUpPullDownConfig() API.
  * @retval None
  */
void HAL_PWR_EnterSTANDBYMode(void)
{
  /* Set Stand-by mode */
  MODIFY_REG(PWR->CR1, PWR_CR1_LPMS, PWR_CR1_LPMS_STANDBY);

  /* Set SLEEPDEEP bit of Cortex System Control Register */
  SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SLEEPDEEP_Msk));

/* This option is used to ensure that store operations are completed */
#if defined ( __CC_ARM)
  __force_stores();
#endif
  /* Request Wait For Interrupt */
  __WFI();
}

在while中使用待机模式代码如下

printf("aaaa\r\n");
		HAL_PWR_EnableWakeUpPin (PWR_WAKEUP_PIN1_LOW);
		__HAL_PWR_CLEAR_FLAG (PWR_FLAG_WU );
		HAL_PWR_EnterSTANDBYMode();
		printf("aaaa\r\n");//唤醒不执行

 

此帖出自stm32/stm8论坛

最新回复

期待楼主的功耗测试结果,如果能做到纳安级别的待机,那就强了。  详情 回复 发表于 2023-11-2 10:23
点赞 关注
 

回复
举报

6995

帖子

11

TA的资源

版主

沙发
 
期待楼主的功耗测试结果,如果能做到纳安级别的待机,那就强了。
此帖出自stm32/stm8论坛
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
推荐帖子
FPGA——人工智能的未来

FPGA——人工智能的未来 基于大规模数字逻辑的人工智能实现的可行性分析 注:本文为一年前写成,具有一定的科普性,其中有一些 ...

1602的学习总结(菜鸟入门级)

先来叙述一些概念。 LCM和LCD的区别 LCM【Liquid Crystal Module】液晶显示模块,通常包括显示驱动电路,接口电路等等。往 ...

ubuntu下修改内核发生“ncurses libraries“错误(解决方法)

BSEC@bsec-server:~/kernel/Kernel$ make menuconfig HOSTCC scripts/basic/fixdep HOSTCC scripts/basic/docproc HOSTCC script ...

有奖直播:nanoPower技术:延长电池寿命,提升传感器性能 直播资料合集

直播资料合集 nanoPower技术:延长电池寿命,提升传感器性能 直播回放: >>点击观看 直播文档: > ...

【小熊派BearPi-HM Micro】三:烧录编译的固件程序

本帖最后由 数码小叶 于 2022-4-5 13:15 编辑 上一篇已经成功编译了源码,接下来就是把编译的源码结果烧录到小熊派BearPi-HM ...

DC-DC升压到5V异常

我的升压电路图如下图: 608263 我的BAT输入是三节7号干电池,用的DC-DC升压芯片是PT1311,下图是PT1311部分介绍: 608 ...

【花雕动手做】有趣好玩的音乐可视化系列小项目(19)--通体光纤灯

偶然心血来潮,想要做一个音乐可视化的系列专题。这个专题的难度有点高,涉及面也比较广泛,相关的FFT和FHT等算法也相当复杂,不 ...

请问你们平常芯片或者元件都是去哪买的?

请问你们平常芯片或者元件都是去哪买的?我发现某宝现在太病态了,价格乱七八糟,每一个标实价的,只要你拍了,基本都不发货,要 ...

System Identification Methods for (Operational) Modal Analysis Review and Com...

本帖最后由 lihuanyang 于 2022-12-19 09:49 编辑 基于模型分析预览对照的系统辨识方法;全英文原版

【大学生电子竞赛题目分析】——2023年全国赛B题《同轴电缆长度与终端负载检测装置》

没有在论坛内看到这次竞赛B题的讨论,只看到一个问的,还没人回答 其实,今年的竞赛题中,B题是工作量最小的一个。问题是必须 ...

关闭
站长推荐上一条 1/8 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表