Open3

CH32シリーズのStandby mode

Koki MizumotoKoki Mizumoto

CH32L103でstandby modeに入るための関数メモ

#include <ch32l103_pwr.h>

void standby()
{
  GPIO_InitTypeDef GPIO_InitStructure = {0};

  /* To reduce power consumption, unused GPIOs need to be set as pull-down inputs */
  RCC_PB2PeriphClockCmd(RCC_PB2Periph_GPIOA | RCC_PB2Periph_GPIOB |
                            RCC_PB2Periph_GPIOC | RCC_PB2Periph_GPIOD,
                        ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;

  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  GPIO_Init(GPIOD, &GPIO_InitStructure);

  RCC_PB1PeriphClockCmd(RCC_PB1Periph_PWR, ENABLE);
  PWR_WakeUpPinCmd(ENABLE);

#if defined(STANDBY_RTC)
  // LSI & RTC Alarm
  RCC_PB1PeriphClockCmd(RCC_PB1Periph_BKP, ENABLE);
  PWR_BackupAccessCmd(ENABLE);
  BKP_DeInit();

  RCC_LSICmd(ENABLE);
  // Wait till LSI is stable
  while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
    ;
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
  RCC_RTCCLKCmd(ENABLE);

  RTC_SetCounter(0);
  RTC_WaitForLastTask();
  RTC_SetAlarm(5); // 5 second
  RTC_WaitForLastTask();
#endif

  // PWR->CTLR |= (0x1 << 16); // RAM 2K
  // PWR->CTLR |= (0x1 << 17); // RAM 18K
  // PWR->CTLR |= (0x1 << 20); // RAM LV

  PWR_EnterSTANDBYMode();
}
Koki MizumotoKoki Mizumoto

CH32V203の場合も大体同じ(未検証)

#include <ch32v20x_pwr.h>

void standby()
{
  GPIO_InitTypeDef GPIO_InitStructure = {0};

  /* To reduce power consumption, unused GPIOs need to be set as pull-down inputs */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                            RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD,
                        ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;

  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  GPIO_Init(GPIOD, &GPIO_InitStructure);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  PWR_WakeUpPinCmd(ENABLE);

#if defined(STANDBY_RTC)
  // LSI & RTC Alarm
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP, ENABLE);
  PWR_BackupAccessCmd(ENABLE);
  BKP_DeInit();

  RCC_LSICmd(ENABLE);
  // Wait till LSI is stable
  while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
    ;
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
  RCC_RTCCLKCmd(ENABLE);

  RTC_SetCounter(0);
  RTC_WaitForLastTask();
  RTC_SetAlarm(5); // 5 second
  RTC_WaitForLastTask();
#endif

  // PWR->CTLR |= (0x1 << 16); // RAM 2K
  // PWR->CTLR |= (0x1 << 17); // RAM 18K
  // PWR->CTLR |= (0x1 << 20); // RAM LV

  PWR_EnterSTANDBYMode();
}