本帖最后由 lulugl 于 2025-9-1 19:40 编辑
根据评测任务,此次测试需要用到低功耗串口,我这里使用lpuart1做为测试口,根据原理图,他接到PA2、PA3
1、串口初始化:
- static void MX_LPUART1_UART_Init(void)
- {
- /* USER CODE BEGIN LPUART1_Init 0 */
- /* USER CODE END LPUART1_Init 0 */
- /* USER CODE BEGIN LPUART1_Init 1 */
- /* USER CODE END LPUART1_Init 1 */
- hlpuart1.Instance = LPUART1;
- hlpuart1.Init.BaudRate = 115200;
- hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
- hlpuart1.Init.StopBits = UART_STOPBITS_1;
- hlpuart1.Init.Parity = UART_PARITY_NONE;
- hlpuart1.Init.Mode = UART_MODE_TX_RX;
- hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
- hlpuart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
- hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- hlpuart1.FifoMode = UART_FIFOMODE_DISABLE;
- if (HAL_UART_Init(&hlpuart1) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_SetTxFifoThreshold(&hlpuart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_SetRxFifoThreshold(&hlpuart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_UARTEx_DisableFifoMode(&hlpuart1) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN LPUART1_Init 2 */
- #ifdef BOARD_IN_STOP_MODE
- LPUART1_EXTI_ENABLE_IT();
- #endif /* BOARD_IN_STOP_MODE */
- /* USER CODE END LPUART1_Init 2 */
- }
2、同时为了显示LED,初始化led2
3、配置低功耗时钟:
- void SystemClock_Config_fromSTOP(void)
- {
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- uint32_t pFLatency = 0;
- /* Get the Oscillators configuration from the internal RCC registers */
- HAL_RCC_GetOscConfig(&RCC_OscInitStruct);
- /* Wake up on HSI, re-enable PLL with HSI as source */
- /* Oscillator configuration unchanged */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /* Get the clock prescalers configuration from the internal RCC registers */
- HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency);
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
- |RCC_CLOCKTYPE_PCLK3;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency) != HAL_OK)
- {
- Error_Handler();
- }
- }
4、测试程序设计,在主程序中,首先延时2秒,然后开启低功耗串口的接收中断,然后进入低功耗模式,关闭LD2,当有串口数据接收后,周期闪烁,闪烁10次,如果没有数据再次进入,则再次进入低功耗模式。
- /* LD2 is on till stop mode */
- BSP_LED_On(LD2);
- /* wait for two seconds before test start */
- HAL_Delay(2000);
- /* Disable Debug during Stop mode */
- HAL_DBGMCU_DisableDBGStopMode();
- /* Enable MCU wake-up by LPUART */
- HAL_UARTEx_EnableStopMode(&hlpuart1);
- /*##-1- Put UART peripheral in reception process ###########################*/
- if (HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
- {
- Error_Handler();
- }
- /* about to enter STOP mode: switch off LD2 */
- BSP_LED_Off(LD2);
- /* enter STOP mode */
- HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
- /* The board receives the message from the other board. Message reception wakes up the board. */
- /* Switch On LD2 */
- BSP_LED_On(LD2);
- /* At that point, MCU has been awoken: LD2 has been turned back on ; Wake Up based on RXNE flag successful */
- SystemClock_Config_fromSTOP();
- /* Disable MCU wake-up by LPUART */
- HAL_UARTEx_DisableStopMode(&hlpuart1);
- #endif /* !BOARD_IN_STOP_MODE */
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- BSP_LED_Off(LD2);
- HAL_Delay(500);
- BSP_LED_On(LD2);
- HAL_Delay(500);
- cnt++;
- if(cnt >10)
- {
- /* Disable Debug during Stop mode */
- HAL_DBGMCU_DisableDBGStopMode();
- /* Enable MCU wake-up by LPUART */
- HAL_UARTEx_EnableStopMode(&hlpuart1);
-
- /*##-1- Put UART peripheral in reception process ###########################*/
- if (HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
- {
- Error_Handler();
- }
-
- /* about to enter STOP mode: switch off LD2 */
- BSP_LED_Off(LD2);
- /* enter STOP mode */
- HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
- /* The board receives the message from the other board. Message reception wakes up the board. */
- /* Switch On LD2 */
- BSP_LED_On(LD2);
- /* At that point, MCU has been awoken: LD2 has been turned back on ; Wake Up based on RXNE flag successful */
- SystemClock_Config_fromSTOP();
- /* Disable MCU wake-up by LPUART */
- HAL_UARTEx_DisableStopMode(&hlpuart1);
- cnt = 0;
- }
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
【测试】
我使用微安表进行测试,在正常工作时工作电流为1.1MA,进入低功耗时电流为400uA,使用串口发送数据后,可以正常唤醒。
|