Like other conventional 32-bit MCUs, the APM32F402 features three low-power modes: Sleep, Stop, and Standby. This post will detail the testing of the APM32F402's power consumption in Stop mode and its wakeup functionality using a button.
The test is conducted on an APM32F402 TinyBoard using the official APM32F402 SDK.
Software Logic:The device enters Stop mode when KEY1 (connected to PA1) is pressed. It wakes up from Stop mode when KEY2 (connected to PA0) is pressed.
int main(void)
{
RCM_EnableAPB1PeriphClock((RCM_APB1_PERIPH_T)(RCM_APB1_PERIPH_PMU | RCM_APB1_PERIPH_BAKR));
BOARD_LED_Config(LED2);
BOARD_LED_Config(LED3);
Usart_Init();
printf("run start\r\n");
/* KEY1 \ KEY2 Set */
BOARD_BUTTON_Config(BUTTON_KEY1, BUTTON_MODE_EINT);
BOARD_BUTTON_Config(BUTTON_KEY2, BUTTON_MODE_EINT);
/* NVIC Priority Set */
NVIC_ConfigPriorityGroup(NVIC_PRIORITY_GROUP_1);
NVIC_EnableIRQRequest(EINT0_IRQn, 0, 1);
NVIC_EnableIRQRequest(EINT1_IRQn, 1, 1);
BOARD_LED_On(LED2);
BOARD_LED_Off(LED3);
// Delay(0x7FFFFF);//上电进入休眠测试功耗
// stop_flag=1;//上电进入休眠测试功耗
printf("Press KEY1 to Enter STOP Mode\r\n");
while (1)
{
Delay(0x7FFFFF);
BOARD_LED_Toggle(LED2);
if(stop_flag==1)
{
printf("\r\nIN STOP mode\r\n");
stop_flag=0;
GPIO_ALL_init();
PMU_EnterSTOPMode(PMU_REGULATOR_LOWPOWER, PMU_STOP_ENTRY_WFI);//Enter stop mode
SystemInit();
Usart_Init();
BOARD_LED_Config(LED2);
printf("\r\nOUT STOP mode\r\n");
if(keywkup_flag==1)
{
keywkup_flag=0;
printf("\r\nkey WKUP STOP mode\r\n");
}
}
}
}
void Eint0_Isr(void)
{
if (EINT_ReadIntFlag(KEY2_BUTTON_EINT_LINE) != RESET)
{
keywkup_flag=1;
// SystemInit();
BOARD_LED_Off(LED3);
/* Wait for system init */
Delay(0xfffff);
printf("Wake up from STOP Mode\r\n");
EINT_ClearIntFlag(KEY2_BUTTON_EINT_LINE);
}
}
void Eint1_Isr(void)
{
if (EINT_ReadIntFlag(KEY1_BUTTON_EINT_LINE) != RESET)
{
BOARD_LED_On(LED3);
BOARD_LED_Off(LED2);
printf("\r\nSystem have entered STOP mode\r\n");
printf("now, LED2 remains in the previous state\r\n");
printf("please press KEY2 or reset System to exit STOP mode\r\n");
stop_flag=1;
EINT_ClearIntFlag(KEY1_BUTTON_EINT_LINE);
}
}
In the interrupt service routines (ISRs) for KEY1 and KEY2, only flag variables are modified. This simplifies the test logic within the main
loop.
It is important to note that the state of I/O pins is retained when entering stop mode. To achieve the lowest power consumption, unused pins should be configured in analog input mode. After waking up, peripherals such as the system clock, UART, and LED pins need to be re-initialized.
void GPIO_ALL_init(void)
{
GPIO_Config_T gpioConfig;
/** Enable the GPIO Clocks */
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_GPIOB | RCM_APB2_PERIPH_GPIOC | RCM_APB2_PERIPH_GPIOD);
/** Configure all pins of GPIOB, GPIOC, and GPIOD to analog mode */
gpioConfig.pin = GPIO_PIN_ALL;
gpioConfig.mode = GPIO_MODE_ANALOG;
gpioConfig.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOB, &gpioConfig);
GPIO_Config(GPIOC, &gpioConfig);
GPIO_Config(GPIOD, &gpioConfig);
/** Configure all pins of GPIOA except PA0 and PA1 to analog mode */
gpioConfig.pin = ((uint32_t)0XFFFC); // All pins except PA0 and PA1
gpioConfig.mode = GPIO_MODE_ANALOG;
gpioConfig.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOA, &gpioConfig);
}
Measurement Method:A multimeter is connected in series between the board's 3.3V supply and the VDD pin. VDDA is connected to VDD, so the measurement reflects the MCU's total power consumption.
After connecting the multimeter and flashing the code, you can observe LED2 blinking. The chip's operating power consumption is just under 10mA.
- Pressing KEY1 causes LED2 to turn off, and the MCU enters Stop mode.
- The serial terminal displays status messages before entering and after waking from Stop mode.
If you are not using a development board and lack physical buttons, you can uncomment the lines in main()
to set the stop_flag
after a short delay, forcing the MCU to enter Stop mode automatically on power-up.
Comments