[STM32U3] 【STM32U385RG 测评】基础任务2 基于低功耗串口测试

[复制链接]
145|0
lulugl 发表于 2025-9-1 18:17 | 显示全部楼层 |阅读模式
本帖最后由 lulugl 于 2025-9-1 19:40 编辑

根据评测任务,此次测试需要用到低功耗串口,我这里使用lpuart1做为测试口,根据原理图,他接到PA2、PA3

8027168b570cdef25c.png
1、串口初始化:
  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、同时为了显示LED,初始化led2
  1. BSP_LED_Init(LD2);
3、配置低功耗时钟:
  1. void SystemClock_Config_fromSTOP(void)
  2. {

  3.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  4.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  5.   uint32_t pFLatency = 0;

  6.   /* Get the Oscillators configuration from the internal RCC registers */
  7.   HAL_RCC_GetOscConfig(&RCC_OscInitStruct);

  8.   /* Wake up on HSI, re-enable PLL with HSI as source */
  9.   /* Oscillator configuration unchanged */
  10.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  11.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  12.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  13.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  14.   {
  15.     Error_Handler();
  16.   }
  17.   /* Get the clock prescalers configuration from the internal RCC registers */
  18.   HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency);
  19.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  20.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  21.                               |RCC_CLOCKTYPE_PCLK3;
  22.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  23.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  24.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  25.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  26.   RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;
  27.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency) != HAL_OK)
  28.   {
  29.     Error_Handler();
  30.   }
  31. }


4、测试程序设计,在主程序中,首先延时2秒,然后开启低功耗串口的接收中断,然后进入低功耗模式,关闭LD2,当有串口数据接收后,周期闪烁,闪烁10次,如果没有数据再次进入,则再次进入低功耗模式。
  1.   /* LD2 is on till stop mode */
  2.   BSP_LED_On(LD2);

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

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

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

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

  12.       Error_Handler();
  13.   }

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

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

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

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

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



  25. #endif /* !BOARD_IN_STOP_MODE */
  26.   /* USER CODE END 2 */
  27.   /* Infinite loop */
  28.   /* USER CODE BEGIN WHILE */
  29.   while (1)
  30.   {
  31.                 BSP_LED_Off(LD2);
  32.                 HAL_Delay(500);
  33.                 BSP_LED_On(LD2);
  34.                 HAL_Delay(500);
  35.                 cnt++;
  36.                 if(cnt >10)
  37.                 {
  38.                           /* Disable Debug during Stop mode */
  39.                         HAL_DBGMCU_DisableDBGStopMode();

  40.                         /* Enable MCU wake-up by LPUART */
  41.                         HAL_UARTEx_EnableStopMode(&hlpuart1);
  42.                         
  43.                         /*##-1- Put UART peripheral in reception process ###########################*/
  44.                         if (HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
  45.                         {

  46.                                         Error_Handler();
  47.                         }
  48.                         
  49.                         /* about to enter STOP mode: switch off LD2 */
  50.                         BSP_LED_Off(LD2);

  51.                         /* enter STOP mode */
  52.                         HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

  53.                         /* The board receives the message from the other board. Message reception wakes up the board. */
  54.                         /* Switch On LD2 */
  55.                         BSP_LED_On(LD2);

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

  58.                         /* Disable MCU wake-up by LPUART */
  59.                         HAL_UARTEx_DisableStopMode(&hlpuart1);
  60.                         cnt = 0;
  61.                 }
  62.     /* USER CODE END WHILE */

  63.     /* USER CODE BEGIN 3 */
  64.   }
  65.   /* USER CODE END 3 */
【测试】
我使用微安表进行测试,在正常工作时工作电流为1.1MA,进入低功耗时电流为400uA,使用串口发送数据后,可以正常唤醒。


您需要登录后才可以回帖 登录 | 注册

本版积分规则

186

主题

839

帖子

12

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