Low Power Modes For STM32L4

前言

現在MCU都會被要求有節約省電,ST針對省電版本MCU推出L系列與最新的U5系列,此章節主要會介紹各個省電模式下的操作,與Sleep mode為例子會使用到的範例程式,配合客戶目前遇到問題,提點說明要點,與注意事項。(此章節也會介紹L4系列MCU)

STM32L系列核心

這邊可以看到圖表L4的核心處理器已來到Core-M4能對應的運算效能也大大增加

Low-power modes summary

這邊會以L系列中常使用的L4為主作介紹,下表為L4在各狀態下哪些部分會進入休眠哪些是可以使用的(耗電流是可以達成的但控制條件與注意事項較為嚴苛)

Low-power modes transitions

STM32L4 Sleep mode實作

在看完上述介紹後,對於Low pow系列有比較深入認知,這邊我們來實作Low Pow Sleep mode模式

硬體注意事項

  • GPIO漏電問題
  • 焊接問題

GPIO漏電問題

GPIO端口在STM32進入低功耗模式後有電荷流動。这是由於有壓差所造成的,因此在進入低功耗模式前先把各GPIO腳位拉到與外界水位相同
我們可以看以下範例來協助了解

案例1:LCD模組連接MCU在進入STOP mode後功耗整板800多uA非1.6uA(代碼是相同的)
問題分析
  • 摘除LCD模組後功耗下降至500uA
  • 個模組都是以GPIO來控制開關

解法:GPIO在進入STOP mode前先把腳位全部拉到HIGH(LCD電為水平相同)

焊接問題

此問題其實也與GPIO問題原理雷同,主要是焊接接觸不良也會有漏電的現象

Coding撰寫

要能達到極為省電關閉部必要的Clock來源與GPIO腳位這2點是極為重要

void SystemClock_Decrease(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};

  /* MSI is enabled in range 0 (100Khz) */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_0;//100khz
  RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }  
  /* Select MSI as system clock source and keep HCLK, PCLK1 and PCLK2 clocks dividers as before */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI; 
  if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  } 
  /* Disable HSI to reduce power consumption since MSI is used from that point */
  __HAL_RCC_HSI_DISABLE();  
}
static void SystemPower_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  
  /* Enable GPIOs clock */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOE_CLK_ENABLE();
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOG_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);
  HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
  HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
  HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
  HAL_GPIO_Init(GPIOF, &GPIO_InitStructure);
  HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);

  /* Disable GPIOs clock */
  __HAL_RCC_GPIOA_CLK_DISABLE();
  __HAL_RCC_GPIOB_CLK_DISABLE();
  __HAL_RCC_GPIOC_CLK_DISABLE();
  __HAL_RCC_GPIOD_CLK_DISABLE();
  __HAL_RCC_GPIOH_CLK_DISABLE();
  __HAL_RCC_GPIOE_CLK_DISABLE();
  __HAL_RCC_GPIOF_CLK_DISABLE();
  __HAL_RCC_GPIOG_CLK_DISABLE();
}

主程式如下

int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  BSP_LED_Init(LED2);
  printf("start\n\r");
  /* Disable Prefetch Buffer */
   __HAL_FLASH_PREFETCH_BUFFER_DISABLE();

   /* Enable Power Clock */
   __HAL_RCC_PWR_CLK_ENABLE();

   /* Enable Flash power down mode during Sleep mode     */
   /* (uncomment this line if power consumption figures  */
   /*  must be measured with Flash still on in Low Power */
   /*  Sleep mode)                                       */
   __HAL_FLASH_SLEEP_POWERDOWN_ENABLE();

   /* Reset all RCC Sleep and Stop modes register to */
   /* improve power consumption                      */
   RCC->AHB1SMENR  = 0x0;
   RCC->AHB2SMENR  = 0x0;
   RCC->AHB3SMENR  = 0x0;

   RCC->APB1SMENR1 = 0x0;
   RCC->APB1SMENR2 = 0x0;
   RCC->APB2SMENR  = 0x0;
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
	  HAL_Delay(5000);

	  if (BSP_PB_GetState(BUTTON_USER) == SET)
	  {
	  SystemPower_Config();
	  BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
	  SystemClock_Decrease();
	  HAL_SuspendTick();
	  HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);
	  HAL_PWREx_DisableLowPowerRunMode();
	  SystemClock_Config();
	  BSP_LED_Init(LED2);
	  HAL_ResumeTick();
	  }
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart