[单片机芯片] CH32L103低功耗模式

[复制链接]
9477|7
 楼主| L-MCU 发表于 2024-6-17 16:16 | 显示全部楼层 |阅读模式
本帖最后由 L-MCU 于 2024-6-17 16:18 编辑

CH32L103具有3种低功耗模式,分别是睡眠模式、停止模式、待机模式。这3种模式下功耗由低到高依次是:待机模式 < 停止模式 < 睡眠模式。该3种模式均可以通过WFI或WFE进入,注意停止模式和待机模式在使用WFI或WFE之前,需要将SLEEPDEEP置1,PDDS清0或置1。

关于SLEEPDEEP位,属于PFIC_SCTLR寄存器位2,如下图,默认值为0。在进入停止模式和待机模式之前需要将该位置1。
88385666fedf291eab.png

关于PDDS位,属于电源控制寄存器(PWR_CTLR)位1 ,如下图,配置1进入待机模式,配置0进入停机模式。注意在配置进入停机模式时,要配置该寄存器位0为1,即电压调节器工作在低功耗模式。
57342666fee2172119.png

关于WFI与WFE
WFI:
使用WFI进入低功耗模式时,一般采用中断唤醒方式,唤醒后将优先执行中断服务函数。如CH32L103 EVT Sleep_Mode例程,使用WFI进入睡眠模式,采用EXTI0中断唤醒,唤醒后先执行EXTI0中断服务函数再执行接下来函数,打印如下图。在EXTI0唤醒后,先执行中断服务函数打印EXTI0 Wake_up,然后执行main函数while循环中打印。
85294666fee62ebdd6.png
WFE:
使用WFE进入低功耗模式时,一般采用事件唤醒方式。唤醒事件包括以下3种:
(1)配置一个外部或内部的EXTI线为事件模式,此时无需配置中断控制器;
(2)配置一个中断源,等效为WFI 唤醒,系统优先执行中断服务函数;  
(3)配置SLEEPONPEN位,开启外设中断使能,但不开启中断控制器中的中断使能,系统唤醒后需要清除中断挂起位。

关于SLEEPONEXIT与SEVONPEND
SLEEPONEXIT
启用:执行WFI或WFE指令后,微控制器确保所有待处理的中断服务退出后进入低功耗模式。
不启用:执行WFI或WFE指令后,微控制器立即进入低功耗模式。
SEVONPEND
启用:所有中断或者唤醒事件都可以唤醒通过执行WFE进入的低功耗。
不启用:执行WFI或WFE指令后,微控制器立即进入低功耗模式。

1、睡眠模式
关于睡眠模式具体介绍,可参考CH32L103应用书册。
睡眠模式下,内核停止运行,IO引脚保持运行模式下的状态,即进入睡眠模式时,该引脚是高电平就保持高电平,是低电平就保持低电平。
睡眠模式下,外设时钟正常,因为为降低功耗,进入睡眠模式前需要关闭不用的外设时钟。
睡眠模式下,唤醒时间最短,如下图,具体可看CH32L103数据手册。
32516666feebb8508b.png
CH32L103 EVT提供了睡眠模式下使用WFI进入睡眠模式并使用EXTI中断唤醒的例程。在此介绍一下使用WFE进入睡眠并使用3种唤醒事件唤醒的应用,包含对SLEEPONEXIT与SEVONPEND的使用配置。具体程序如下:
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name          : main.c
  3. * Author             : WCH
  4. * Version            : V1.0.0
  5. * Date               : 2023/12/26
  6. * Description        : Main program body.
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/

  12. /*
  13. *@Note
  14. *USART Print debugging routine:
  15. *USART1_Tx(PA9).
  16. *This example demonstrates using USART1(PA9) as a print debug port output.
  17. *
  18. */

  19. #include "debug.h"

  20. /* Global typedef */

  21. /* Global define */

  22. /* Global Variable */

  23. #define  Wake_up_event_1   1
  24. #define  Wake_up_event_2   2
  25. #define  Wake_up_event_3   3

  26. #define  Wake_up_event     Wake_up_event_3


  27. void ALL_GPIO_Init(void)
  28. {
  29.     GPIO_InitTypeDef GPIO_InitStructure = {0};

  30.     /* To reduce power consumption, unused GPIOs need to be set as pull-down inputs */
  31.     RCC_PB2PeriphClockCmd(RCC_PB2Periph_GPIOA|RCC_PB2Periph_GPIOB|RCC_PB2Periph_GPIOC|RCC_PB2Periph_GPIOD, ENABLE);
  32.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  33.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  34.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  35.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  36.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  37.     GPIO_Init(GPIOD, &GPIO_InitStructure);
  38. }
  39. /*********************************************************************
  40. * @fn      EXTI1_EVT_INIT
  41. *
  42. * [url=home.php?mod=space&uid=247401]@brief[/url]   Initializes EXTI0 collection.
  43. *
  44. * [url=home.php?mod=space&uid=266161]@return[/url]  none
  45. */
  46. void EXTI1_EVT_INIT(void)
  47. {
  48.     GPIO_InitTypeDef GPIO_InitStructure = {0};
  49.     EXTI_InitTypeDef EXTI_InitStructure = {0};

  50.     RCC_PB2PeriphClockCmd(RCC_PB2Periph_AFIO | RCC_PB2Periph_GPIOA, ENABLE);

  51.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  52.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  53.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  54.     GPIO_Init(GPIOA, &GPIO_InitStructure);

  55. #if (Wake_up_event == Wake_up_event_1)
  56.     /* GPIOA.1 ----> EXTI_Line1 */
  57.     GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
  58.     EXTI_InitStructure.EXTI_Line = EXTI_Line1;
  59.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Event;
  60.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  61.     EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  62.     EXTI_Init(&EXTI_InitStructure);

  63. #elif (Wake_up_event == Wake_up_event_2)
  64.     /* GPIOA.1 ----> EXTI_Line1 */
  65.     GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
  66.     EXTI_InitStructure.EXTI_Line = EXTI_Line1;
  67.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  68.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  69.     EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  70.     EXTI_Init(&EXTI_InitStructure);

  71.     NVIC_InitTypeDef NVIC_InitStructure = {0};

  72.     NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
  73.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  74.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  75.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  76.     NVIC_Init(&NVIC_InitStructure);

  77. #elif (Wake_up_event == Wake_up_event_3)


  78.     /* GPIOA.1 ----> EXTI_Line1 */
  79.     GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
  80.     EXTI_InitStructure.EXTI_Line = EXTI_Line1;
  81.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  82.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  83.     EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  84.     EXTI_Init(&EXTI_InitStructure);

  85. #endif
  86. }

  87. /*********************************************************************
  88. * @fn      main
  89. *
  90. * [url=home.php?mod=space&uid=247401]@brief[/url]   Main program.
  91. *
  92. * [url=home.php?mod=space&uid=266161]@return[/url]  none
  93. */
  94. int main(void)
  95. {
  96.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  97.     SystemCoreClockUpdate();
  98.     ALL_GPIO_Init();
  99.     Delay_Init();
  100.     USART_Printf_Init(115200);
  101.     printf("SystemClk:%d\r\n", SystemCoreClock);
  102.     printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
  103.     printf("This is printf example\r\n");

  104.     EXTI1_EVT_INIT();

  105.     printf("Enter Sleep Mode\r\n");

  106.     RCC_PB1PeriphClockCmd(RCC_PB1Periph_PWR, ENABLE);

  107. #if (Wake_up_event == Wake_up_event_3)
  108.     //配置PFIC系统控制寄存器SEVONPEND位
  109.     NVIC->SCTLR |= (1 << 4);//配置为1,启用的事件和所有中断都能唤醒
  110. #endif

  111.     __WFE();

  112. #if (Wake_up_event == Wake_up_event_3)
  113.     //唤醒后清除中断标志位
  114.     EXTI_ClearITPendingBit(EXTI_Line1);     /* Clear Flag */
  115. #endif

  116.     printf("\r\n ########## \r\n");

  117.     while(1)
  118.     {
  119.         Delay_Ms(1000);
  120.         printf("Run in main\r\n");
  121.     }
  122. }

  123. #if (Wake_up_event == Wake_up_event_2)
  124. void EXTI1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  125. /*********************************************************************
  126. * @fn      EXTI0_IRQHandler
  127. *
  128. * @brief   This function handles EXTI0 Handler.
  129. *
  130. * @return  none
  131. */
  132. void EXTI1_IRQHandler(void)
  133. {
  134.   if(EXTI_GetITStatus(EXTI_Line1)!=RESET)
  135.   {
  136.     printf("EXTI1 Wake_up\r\n");
  137.     EXTI_ClearITPendingBit(EXTI_Line1);     /* Clear Flag */
  138.   }
  139. }
  140. #endif
Wake_up_event_1对应第1种唤醒事件,配置EXTI1为事件模式进行唤醒,打印信息如下:
84977666fef377225a.png
当PA1引脚接GND,检测到下降沿事件,唤醒睡眠模式。
Wake_up_event_2对应第2种唤醒事件,配置EXTI1为中断模式进行唤醒,打印信息如下:
49957666fef4c6dbf9.png
当PA1引脚接GND,触发下降沿中断,唤醒睡眠模式。
Wake_up_event_3对应第3种唤醒事件,配置SEVONPEND位,配置EXTI1中断使能,打印信息如下:
3132666fef6281621.png
当PA1引脚接GND,触发下降沿中断,唤醒睡眠模式,注意唤醒后要清除对应的中断标志位。

2、停止模式
关于停止模式具体介绍,可参考CH32L103应用手册。
通过不同的配置,可选择不同等级的停止模式,具体如下:
停止模式1:LPDS=0;
停止模式2:LPDS=0,AUTO_LDO_EC=1或LPDS=0,LDO_EC=1;
停止模式3:LPDS=1;
停止模式4:LPDS=1,RAMLV=1;  
以上配置均基于配置进入停止模式的基础上进行配置。
停止模式进入、4个等级配置以及退出具体程序如下,唤醒方式选择PVD输出唤醒。
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name          : main.c
  3. * Author             : WCH
  4. * Version            : V1.0.0
  5. * Date               : 2023/12/26
  6. * Description        : Main program body.
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/

  12. /*
  13. *@Note
  14. *USART Print debugging routine:
  15. *USART1_Tx(PA9).
  16. *This example demonstrates using USART1(PA9) as a print debug port output.
  17. *
  18. */

  19. #include "debug.h"

  20. /* Global typedef */

  21. /* Global define */

  22. /* Global Variable */

  23. #define  Stop_mode_1   1
  24. #define  Stop_mode_2   2
  25. #define  Stop_mode_3   3
  26. #define  Stop_mode_4   4

  27. #define  Stop_mode     Stop_mode_4


  28. void ALL_GPIO_Init(void)
  29. {
  30.     GPIO_InitTypeDef GPIO_InitStructure = {0};

  31.     /* To reduce power consumption, unused GPIOs need to be set as pull-down inputs */
  32.     RCC_PB2PeriphClockCmd(RCC_PB2Periph_GPIOA|RCC_PB2Periph_GPIOB|RCC_PB2Periph_GPIOC|RCC_PB2Periph_GPIOD, ENABLE);
  33.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  34.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  35.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  36.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  37.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  38.     GPIO_Init(GPIOD, &GPIO_InitStructure);
  39. }

  40. void PVD_Config(void)
  41. {
  42.     NVIC_InitTypeDef NVIC_InitStructure={0};
  43.     EXTI_InitTypeDef EXTI_InitStructure={0};

  44.     /* Enable PWR and BKP clock */
  45.     RCC_PB1PeriphClockCmd(RCC_PB1Periph_PWR | RCC_PB1Periph_BKP, ENABLE);

  46.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  47.     /* 使能 PVD 中断 */
  48.     NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn;
  49.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  50.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  51.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  52.     NVIC_Init(&NVIC_InitStructure);

  53.     /* 配置 EXTI16线(PVD 输出) 来产生上升下降沿中断*/
  54.     EXTI_ClearITPendingBit(EXTI_Line16);
  55.     EXTI_InitStructure.EXTI_Line = EXTI_Line16;
  56.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  57.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
  58.     EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  59.     EXTI_Init(&EXTI_InitStructure);

  60.     /* PVD电压监视阈值设置 */
  61.     /*具体级别根据自己的实际应用要求配置*/
  62.     PWR_PVDLevelConfig(PWR_PVDLevel_7);

  63.     /* 使能PVD输出 */
  64.     PWR_PVDCmd(ENABLE);
  65. }

  66. /*********************************************************************
  67. * @fn      main
  68. *
  69. * @brief   Main program.
  70. *
  71. * @return  none
  72. */
  73. int main(void)
  74. {
  75.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  76.     SystemCoreClockUpdate();
  77.     ALL_GPIO_Init();
  78.     Delay_Init();
  79.     USART_Printf_Init(115200);
  80.     printf("SystemClk:%d\r\n", SystemCoreClock);
  81.     printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
  82.     printf("This is printf example\r\n");

  83.     printf("Enter Stop Mode\r\n");
  84.     PVD_Config();

  85.     RCC_PB1PeriphClockCmd(RCC_PB1Periph_PWR, ENABLE);

  86. #if(Stop_mode==Stop_mode_1)
  87.     PWR_EnterSTOPMode(PWR_Regulator_ON, PWR_STOPEntry_WFE);
  88. #elif(Stop_mode==Stop_mode_2)
  89.     //配置当进入停止模式后自动节能
  90.     PWR->CTLR |= (1 <<12); //或PWR->CTLR |= (1 <<13);
  91.     PWR_EnterSTOPMode(PWR_Regulator_ON, PWR_STOPEntry_WFE);
  92. #elif(Stop_mode==Stop_mode_3)
  93.     PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFE);
  94. #elif(Stop_mode==Stop_mode_4)
  95.     //使能RAM工作在低电压模式
  96.     PWR->CTLR |= (1 <<20);
  97.     PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFE);
  98. #endif
  99.     SystemInit();

  100.     while(1)
  101.     {
  102.         Delay_Ms(1000);
  103.         printf("Run in main\r\n");
  104.     }
  105. }

  106. void PVD_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  107. /**
  108.   * @brief  This function handles the PVD Output interrupt request.
  109.   * @param  None
  110.   * @retval None
  111.   */
  112. void PVD_IRQHandler(void)
  113. {
  114.     if(EXTI_GetITStatus(EXTI_Line16) != RESET)
  115.     {
  116.         printf("ENter\r\n");
  117.         /* Clear the Key Button EXTI line pending bit */
  118.         EXTI_ClearITPendingBit(EXTI_Line16);
  119.     }
  120. }
程序中,通过调用PWR_EnterSTOPMode函数可以直接进入停止模式,关于该函数,具有两个入口参数(PWR_Regulator和PWR_STOPEntry):
PWR_Regulator是对电源控制寄存器(PWR_CTLR)位0进行配置,配置选择停机模式下电压调节器是工作在低功耗模式还是正常模式,一般选择低功耗模式,对该位置1;
PWR_STOPEntry是选择WFI进入或WFE进入,此处配置选择WFE进入,并选择通过第2种唤醒事件进行唤醒,通过PVD中断唤醒。也可配置WFI进入。
进入停止模式时,要对SLEEPDEEP置1,PDDS清0,其中PDDS位为电源控制寄存器(PWR_CTLR)位1,默认是0,PWR_EnterSTOPMode函数中有对PFIC 系统控制寄存器(PFIC_SCTLR)位2置1,即对SLEEPDEEP位置1 ,如下图:
40773666fefcb42bd7.png
关于停止模式1,即LPDS=0,配置电压调节器工作在正常模式,具体程序如下:
31501666fefde157ca.png
关于停止模式2,即LPDS=0,AUTO_LDO_EC=1,配置电压调节器工作在正常模式,且当进入停止模式后自动节能,具体程序如下:
64464666fefeea28bb.png
关于停止模式3,即LPDS=1,配置电压调节器工作在低功耗模式,具体程序如下:
5113666ff00471820.png
关于停止模式4,即LPDS=1,RAMLV=1,配置电压调节器工作在低功耗模式,且当进入停止模式使能开启RAM工作在低电压模式,具体程序如下:
64455666ff01848005.png
以上4种模式,其中模式4的功耗最低,相关参数如下图,具体可看CH32L103数据手册。此外注意停止模式下唤醒后系统可以继续运行,但是此时HSI默认为系统时钟,如果配置了其他时钟,需要重新进行系统时钟进行初始化,因此程序中在唤醒后调用SystemInit()函数重新进行了初始化。
80959666ff02dcb79d.png

3、待机模式
关于待机模式具体介绍,可参考CH32L103应用手册。
相较于停止模式,待机模式下唤醒后,MCU将执行电源复位。CH32L103 EVT提供了待机模式例程以及待机模式下RAM数据保持例程,相对较为全面,唤醒方式等配置基本和睡眠模式、停止模式差不多,在此不再举例赘述,可直接参考EVT例程。

附件例程为文中测试所用例程,可下载参考。


CH32L103低功耗.zip

1.32 MB, 下载次数: 12

tpgf 发表于 2024-9-3 11:59 | 显示全部楼层
在睡眠模式下,CH32L103的功耗相对较高。此模式下,CPU停止运行,但系统时钟仍然运行,外设功能可以得到保持并根据需要被唤醒
wakayi 发表于 2024-9-5 10:10 | 显示全部楼层
为了进一步降低功耗,建议将未使用的IO口配置为下拉输入模式,并确保内部调压器处于低功耗模式
wowu 发表于 2024-9-5 20:07 | 显示全部楼层
待机模式是三种模式中功耗最低的模式,此模式下,除了部分具有保持功能的外设,大部分硬件模块的电源都会被切断,包括CPU和绝大多数外设
xiaoqizi 发表于 2024-9-10 15:21 | 显示全部楼层
在待机模式下,当RTC和LSE处于运行状态时,电流可低至1.7uA,这为长时间运行且对功耗要求极其严格的应用提供了可能
paotangsan 发表于 2024-9-21 15:11 | 显示全部楼层
睡眠模式适用于需要快速唤醒和短时间进入低能耗状态的应用场景
renzheshengui 发表于 2024-9-22 20:09 | 显示全部楼层
停止模式是中度低功耗模式,此模式下,CPU和系统时钟均停止,但可以在特定外部刺激或中断发生时被唤醒
wsnsyy 发表于 2024-11-3 01:48 | 显示全部楼层
低功耗模式下,PD端口需要是DRP模式,应该怎么配置呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

17

主题

39

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部