【stm32】G0系列串口低功耗模式介绍和应用(地址匹配模式)

一、串口低功耗模式介绍

1.1 介绍

stm32可以通过串口来从stop模式唤醒,如果基于hal库,只要进行一些配置就能使用,这种通过串口唤醒的低功耗模式可以用在带电池的设备上,在需要设备工作的时候才将其唤醒处理事务,处理完后进入stop模式降低功耗。

1.2 三种模式和相关的寄存器

1.2.1 寄存器

1.2.1.1 CR3[21:20] WUS[1:0]

功能:选择从低功耗模式中断唤醒的标志
配置说明:
00:地址匹配模式
01:保留
10:检测起始位
11:RXNE/RXFNE中断

1.2.1.2 CR3[22] WUFIE

功能:使能从低功耗模式唤醒
配置说明:
0:关闭中断
1:使能WUF中断,即当ISR的WUF为1时产生一个中断

1.2.1.3 CR1[1] UESM

功能:使能串口的低功耗模式
配置说明:
0:串口不能唤醒低功耗模式
1:串口能唤醒低功耗模式

1.2.1.4 PWR_CR1[2:0] LPMS[2:0]

功能:低功耗模式选择
配置说明:
000:stop 0模式
001:stop1模式
010:保留
011:stanby模式

1.2.1.5 SCB_SCR[2] SLEEPDEEP

功能:决定处理器在低功耗模式时选择睡眠模式还是深度睡眠模式,SCB寄存器的内容在m0+编程手册里。
配置说明:
0:睡眠模式
1:深度睡眠模式

1.2.2 模式

地址匹配模式
检测起始位
RXNE/RXFNE中断模式

二、用法

2.1 地址匹配模式

地址配置成0x29(0010 1001),串口需要发送0xa9来唤醒,唤醒后需要关闭唤醒中断使能,重新初始化时钟。

/* make sure that no LPUART transfer is on-going */
  while (__HAL_UART_GET_FLAG(&hlpuart1, USART_ISR_BUSY) == SET);

  /* make sure that LPUART is ready to receive
   * (test carried out again later in HAL_UARTEx_StopModeWakeUpSourceConfig) */
  while (__HAL_UART_GET_FLAG(&hlpuart1, USART_ISR_REACK) == RESET);

  /* set the wake-up event:
   * specify address-to-match type.
   * The address is 0x29, meaning the character triggering the
   * address match is 0xA9 */
  WakeUpSelection.WakeUpEvent   = UART_WAKEUP_ON_ADDRESS;
  WakeUpSelection.AddressLength = UART_ADDRESS_DETECT_7B;
  WakeUpSelection.Address       = 0x29;
  if (HAL_UARTEx_StopModeWakeUpSourceConfig(&hlpuart1, WakeUpSelection) != HAL_OK)
  {
    
    
    Error_Handler();
  }

  /* Enable the LPUART Wake UP from stop mode Interrupt */
  __HAL_UART_ENABLE_IT(&hlpuart1, UART_IT_WUF);

  /* about to enter stop mode: switch off LED1 */
  BSP_LED_Off(LED1);
  /* enable MCU wake-up by LPUART */
  HAL_UARTEx_EnableStopMode(&hlpuart1);
  /* enter STOP mode */
  HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  /* ... STOP mode ... */


  /* at that point, MCU has been awoken: LED1 has been turned back on */
  SystemClock_Config_fromSTOP();

  /* Wake Up on 7-bit address detection successful */
  HAL_UARTEx_DisableStopMode(&hlpuart1);

2.2 起始位模式

串口上产生一个起始位可以唤醒。

/* make sure that no LPUART transfer is on-going */
  while (__HAL_UART_GET_FLAG(&hlpuart1, USART_ISR_BUSY) == SET);

  /* make sure that LPUART is ready to receive
   * (test carried out again later in HAL_UARTEx_StopModeWakeUpSourceConfig) */
  while (__HAL_UART_GET_FLAG(&hlpuart1, USART_ISR_REACK) == RESET);

  /* set the wake-up event:
   * specify wake-up on start-bit detection */
  WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_STARTBIT;
  if (HAL_UARTEx_StopModeWakeUpSourceConfig(&hlpuart1, WakeUpSelection) != HAL_OK)
  {
    
    
    Error_Handler();
  }

  /* Enable the LPUART Wake UP from STOP mode Interrupt */
  __HAL_UART_ENABLE_IT(&hlpuart1, UART_IT_WUF);

  /* about to enter STOP mode: switch off LED1 */
  BSP_LED_Off(LED1);
  /* enable MCU wake-up by LPUART */
  HAL_UARTEx_EnableStopMode(&hlpuart1);
  /* enter STOP mode */
  HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  /* ... STOP mode ... */


  /* at that point, MCU has been awoken: LED1 has been turned back on */
  SystemClock_Config_fromSTOP();

  /* Wake Up on start bit detection successful */
  HAL_UARTEx_DisableStopMode(&hlpuart1);

2.3 RXNE/RXFNE中断模式

接收到一个字符可以唤醒,接收的字节是完整的。

/* make sure that no LPUART transfer is on-going */
  while (__HAL_UART_GET_FLAG(&hlpuart1, USART_ISR_BUSY) == SET);
  /* make sure that LPUART is ready to receive
   * (test carried out again later in HAL_UARTEx_StopModeWakeUpSourceConfig) */
  while (__HAL_UART_GET_FLAG(&hlpuart1, USART_ISR_REACK) == RESET);

  /* set the wake-up event:
   * specify wake-up on RXNE flag */
  WakeUpSelection.WakeUpEvent = UART_WAKEUP_ON_READDATA_NONEMPTY;
  if (HAL_UARTEx_StopModeWakeUpSourceConfig(&hlpuart1, WakeUpSelection) != HAL_OK)
  {
    
    
    Error_Handler();
  }

  /* Enable the UART Wake UP from STOP mode Interrupt */
  __HAL_UART_ENABLE_IT(&hlpuart1, UART_IT_WUF);

  /* about to enter STOP mode: switch off LED1 */
  BSP_LED_Off(LED1);
  /* enable MCU wake-up by LPUART */
  HAL_UARTEx_EnableStopMode(&hlpuart1);
  /* enter STOP mode */
  HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  /* ... STOP mode ... */


  /* at that point, MCU has been awoken: LED1 has been turned back on */
  SystemClock_Config_fromSTOP();

  /* Wake Up based on RXNE flag successful */
  HAL_UARTEx_DisableStopMode(&hlpuart1);

三、注意事项

如果使用地址匹配模式,需要关闭RXNE的中断。

__HAL_UART_DISABLE_IT(&huart1,UART_IT_RXNE);

おすすめ

転載: blog.csdn.net/weixin_43810563/article/details/118969363