[STM32U3] 【STM32U385RG 测评】4、LPUART唤醒STOP低功耗模式

[复制链接]
 楼主| sujingliang 发表于 2025-7-16 12:56 | 显示全部楼层 |阅读模式
STM32U385RG的SDK中有LPUART唤醒STOP低功耗模式的例程位于:
STM32Cube_FW_U3_V1.2.0\Projects\NUCLEO-U385RG-Q\Examples\UART\LPUART_WakeUpFromStop
所以简单地修改下就可以实现本实验的功能

一、基本配置
1、引脚使用
15.png
2、PWR配置

16.png

3、LPUART1配置
17.png

4、中断配置
18.png

二、软件部分

1、LPUART1初始化
使能中断
  1. static void MX_LPUART1_UART_Init(void)
  2. {

  3.   /* USER CODE BEGIN LPUART1_Init 0 */

  4.   /* USER CODE END LPUART1_Init 0 */

  5.   /* USER CODE BEGIN LPUART1_Init 1 */

  6.   /* USER CODE END LPUART1_Init 1 */
  7.   hlpuart1.Instance = LPUART1;
  8.   hlpuart1.Init.BaudRate = 115200;
  9.   hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
  10.   hlpuart1.Init.StopBits = UART_STOPBITS_1;
  11.   hlpuart1.Init.Parity = UART_PARITY_NONE;
  12.   hlpuart1.Init.Mode = UART_MODE_TX_RX;
  13.   hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  14.   hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  15.   hlpuart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  16.   hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  17.   hlpuart1.FifoMode = UART_FIFOMODE_DISABLE;
  18.   if (HAL_UART_Init(&hlpuart1) != HAL_OK)
  19.   {
  20.     Error_Handler();
  21.   }
  22.   if (HAL_UARTEx_SetTxFifoThreshold(&hlpuart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  23.   {
  24.     Error_Handler();
  25.   }
  26.   if (HAL_UARTEx_SetRxFifoThreshold(&hlpuart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  27.   {
  28.     Error_Handler();
  29.   }
  30.   if (HAL_UARTEx_DisableFifoMode(&hlpuart1) != HAL_OK)
  31.   {
  32.     Error_Handler();
  33.   }
  34.   /* USER CODE BEGIN LPUART1_Init 2 */
  35. #ifdef BOARD_IN_STOP_MODE
  36.   LPUART1_EXTI_ENABLE_IT();
  37. #endif /* BOARD_IN_STOP_MODE */
  38.   /* USER CODE END LPUART1_Init 2 */

  39. }
2、LPUART_RX中断处理

  1. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  2. {
  3.   UserReceptionComplete = 1;
  4. }
RX收到数据,设置标志UserReceptionComplete = 1。

3、main函数

  1. BSP_LED_On(LD2);

  2.   /* wait for two seconds before test start */
  3.   HAL_Delay(2000);

  4.   /* Disable Debug during Stop mode */
  5.   HAL_DBGMCU_DisableDBGStopMode();

  6.   /* Enable MCU wake-up by LPUART */
  7.   HAL_UARTEx_EnableStopMode(&hlpuart1);

  8.   /*##-1- Put UART peripheral in reception process ###########################*/
  9.   if (HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
  10.   {

  11.       Error_Handler();
  12.   }

  13.   /* about to enter STOP mode: switch off LD2 */
  14.   BSP_LED_Off(LD2);

  15.   /* enter STOP mode */
  16.   HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  17.   /* The board receives the message from the other board. Message reception wakes up the board. */
  18.   /* Switch On LD2 */
  19.   BSP_LED_On(LD2);

  20.   /* At that point, MCU has been awoken: LD2 has been turned back on ; Wake Up based on RXNE flag successful */
  21.   SystemClock_Config_fromSTOP();

  22.   /* Disable MCU wake-up by LPUART */
  23.   HAL_UARTEx_DisableStopMode(&hlpuart1);

  24.   /* Wait for reception complete callback */
  25.   while (UserReceptionComplete == 0)
  26.   {
  27.   }

  28.   /*##-2- Start the transmission process #####################################*/
  29.   /* Send back received buffer */
  30.   if (HAL_UART_Transmit_IT(&hlpuart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
  31.   {

  32.     Error_Handler();

  33.   }
进入STOP模式:HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
设置LPUART1结束STOP模式使能:HAL_UARTEx_EnableStopMode(&hlpuart1);

4、while(1)
  1.   while (1)
  2.   {
  3.                 BSP_LED_Toggle(LD2);
  4.                 HAL_Delay(1000);
  5.     /* USER CODE END WHILE */

  6.     /* USER CODE BEGIN 3 */
  7.   }
  8.   /* USER CODE END 3 */
  9. }
退出stop后,进入while(1),没隔1秒翻转LD2。


三、运行

LPUART(PA2、PA3)和串口工具连接

tutieshi_480x271_24s.gif

1、按reset开机,几秒后开发板进入Stop模式,电流0.3mA左右
2、PC机通过串口工具向LPUART1发送"hello world",MCU被唤醒,PC机接收返回的"hello world",此时电流1.0mA左右
3、板载LD2每1秒翻转1次





小小蚂蚁举千斤 发表于 2025-7-28 23:49 | 显示全部楼层
了解LPUART唤醒STOP低功耗模式
您需要登录后才可以回帖 登录 | 注册

本版积分规则

84

主题

146

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部