美文网首页STM32
STM32L051低功耗STOP模式下RTC唤醒

STM32L051低功耗STOP模式下RTC唤醒

作者: TonyCode | 来源:发表于2019-08-15 14:11 被阅读0次

    很多MCU都有低功耗模式,以满足对功耗要求苛刻的应用场景。STM32系列MCU的低功耗模式根据其内核与类型的不同也各不相同,网上有很多文章介绍。本文总结下STM32L051的STOP模式,通过RTC唤醒。

    STM32L051在低功耗方面性能优异,ST官方数据手册中说明,在stop without rtc模式下电流消耗能达到0.4uA,在stop with rtc模式下电流消耗能达到0.8uA。

    功耗指标
    1. 在进入STOP模式前对外设进行设置,GPIO要设置成模拟输入状态。
    static void system_config_before_stop(void)
    {
        HAL_StatusTypeDef ret = HAL_OK;
        GPIO_InitTypeDef GPIO_InitStructure;
    
        /* Enable Ultra low power mode */
        HAL_PWREx_EnableUltraLowPower();
    
        /* Enable the fast wake up from Ultra low power mode */
        HAL_PWREx_EnableFastWakeUp();
    
        /* Select HSI as system clock source after Wake Up from Stop mode */
        __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_HSI);
    
        /* Enable GPIOs clock */
        __HAL_RCC_GPIOA_CLK_ENABLE();
        __HAL_RCC_GPIOB_CLK_ENABLE();
        __HAL_RCC_GPIOC_CLK_ENABLE();
        
        /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
        GPIO_InitStructure.Pin = GPIO_PIN_All;
        GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
        GPIO_InitStructure.Pull = GPIO_NOPULL;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
        HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
        HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
    
        /* Disable GPIOs clock */
        __HAL_RCC_GPIOA_CLK_DISABLE();
        __HAL_RCC_GPIOB_CLK_DISABLE();
        __HAL_RCC_GPIOC_CLK_DISABLE();
    
        /* Configure RTC */
        RTCHandle.Instance = RTC;
        RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
        RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
        RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
        RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
        RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
        RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
        ret = HAL_RTC_Init(&RTCHandle);
        if (ret != HAL_OK) while (1);
    }
    
    1. 设置RTC的唤醒时间,函数参数单位为秒。
    static void stop_rtc_config(uint32_t sleep)
    {
        /* Disable Wakeup Counter */
        HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
    
        /*## Setting the Wake up time */
        HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, sleep, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
    }
    
    1. 调用库函数进入STOP模式。
    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
    
    1. 退出STOP模式后要恢复时钟。特别注意在进入STOP模式后系统时钟变成了MSI。
    static void clock_config_after_stop(void)
    {
        HAL_StatusTypeDef ret = HAL_OK;
        RCC_OscInitTypeDef RCC_OscInitStructure = {0};
        RCC_ClkInitTypeDef RCC_ClkInitStructure = {0};
    
        /* Enable Power Control clock */
        __HAL_RCC_PWR_CLK_ENABLE();
    
        /* The voltage scaling allows optimizing the power consumption when the device is
           clocked below the maximum system frequency, to update the voltage scaling value
           regarding system frequency refer to product datasheet.  */
        __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
    
        /* Get the Oscillators configuration according to the internal RCC registers */
        HAL_RCC_GetOscConfig(&RCC_OscInitStructure);
    
        /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */
        RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI;
        RCC_OscInitStructure.HSEState = RCC_HSE_OFF;
        RCC_OscInitStructure.HSIState = RCC_HSI_ON;
        RCC_OscInitStructure.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
        RCC_OscInitStructure.PLL.PLLState = RCC_PLL_ON;
        RCC_OscInitStructure.PLL.PLLSource = RCC_PLLSOURCE_HSI;
        RCC_OscInitStructure.PLL.PLLMUL = RCC_PLLMUL_4;
        RCC_OscInitStructure.PLL.PLLDIV = RCC_PLLDIV_2;
        ret = HAL_RCC_OscConfig(&RCC_OscInitStructure);
        if (ret != HAL_OK) while (1);
    
        /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
           clocks dividers */
        RCC_ClkInitStructure.ClockType = RCC_CLOCKTYPE_SYSCLK;
        RCC_ClkInitStructure.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
        ret = HAL_RCC_ClockConfig(&RCC_ClkInitStructure, FLASH_LATENCY_1);
        if (ret != HAL_OK) while (1);
    }
    
    1. 恢复各外设状态,比如GPIO、串口等初始化。
    static void gpioInit(void)
    {
        GPIO_InitTypeDef GPIO_InitStruct = {0};
    
        /* GPIO Ports Clock Enable */
        __HAL_RCC_GPIOA_CLK_ENABLE();
    
        /*Configure GPIO pin : PA1 */
        GPIO_InitStruct.Pin = LED_PIN;
        GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
    
    }
    
    1. 在需要进入STOP模式的地方直接调用函数。
    static void enter_stop_rtc_mode(void)
    {
        //1. 配置stop之前各外设
        system_config_before_stop();
    
        //2.设置rtc唤醒时间20s
        stop_rtc_config(20);
    
        //3. 进入stop
        HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
    
        //3.退出stop后恢复时钟
        clock_config_after_stop();
    
        //4.恢复各外设
        gpioInit();
    }
    

    对于低功耗产品开发,CPU的低功耗只是一部份,外围电路设计上也要综合考虑。目前测试到消耗电流比数据手册高。

    stop with rtc

    相关文章

      网友评论

        本文标题:STM32L051低功耗STOP模式下RTC唤醒

        本文链接:https://www.haomeiwen.com/subject/xolzjctx.html