2025-06-17 7:00 AM
Hi all,
I've been struggling so much to get wake from uart working on this board.
It looks like it should be simple, I have this code which I am pretty sure should be everything needed, but it just doesn't wake up when I send data to the serial port.
Can anyone see what I might be missing?
#include <Arduino.h>
#include <stm32u5xx_hal.h>
#include <stm32u5xx_ll_usart.h>
#include <stm32u5xx_ll_bus.h>
#include "CustomPinsHub.h"
static constexpr uint32_t LED_PIN = LED1_GRN;
uint8_t aRxBuffer[10];
void SystemClock_Config(); // forward
HardwareSerial stlinkSerial(rx_pin2debug, tx_pin2debug);
void setup()
{
stlinkSerial.begin(115200);
HAL_PWREx_EnableUltraLowPowerMode();
pinMode(LED1_GRN, OUTPUT);
digitalWrite(LED1_GRN, LOW);
stlinkSerial.println("→ Setup complete. LED is ON.");
delay(500);
UART_HandleTypeDef *huart = stlinkSerial.getHandle();
UART_WakeUpTypeDef wudata{};
wudata.WakeUpEvent = UART_WAKEUP_ON_READDATA_NONEMPTY;
HAL_UARTEx_StopModeWakeUpSourceConfig(huart, wudata);
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_UART_ENABLE_IT(huart, UART_IT_WUF);
HAL_UARTEx_EnableStopMode(huart);
HAL_UART_Receive_IT(huart, (uint8_t *)aRxBuffer, 10);
stlinkSerial.println("Entering STOP-1, waiting for any RX data to wake me..");
stlinkSerial.flush();
delay(500);
HAL_SuspendTick();
HAL_PWREx_EnterSTOP1Mode(PWR_STOPENTRY_WFI);
HAL_ResumeTick();
SystemClock_Config();
delay(500);
stlinkSerial.println("✔ Woke up from STOP-1! Led off");
delay(500);
digitalWrite(LED_PIN, HIGH);
}
void loop()
{
// nothing
}
2025-06-17 10:15 PM
You skip show clock config and your lines do nothing.
static void MX_USART3_UART_Init(void)
{
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART3;
PeriphClkInit.Usart3ClockSelection = RCC_USART1CLKSOURCE_HSI;
yes you place it in main too, but maybe is overide in MSP init
MX_USART3_UART_Init();
. Better is in MX config right clock and remove this balast.
On F series i use wake with this in mx init usart end.
LL_USART_ConfigAsyncMode(USART1);
LL_USART_Enable(USART1);
/* USER CODE BEGIN USART1_Init 2 */
LL_USART_EnableIT_RXNE(USART1);
LL_USART_SetWKUPType(USART1, LL_USART_WAKEUP_ON_RXNE);
LL_USART_EnableIT_WKUP(USART1);
LL_USART_EnableInStopMode(USART1);
/* USER CODE END USART1_Init 2 */
2025-06-18 12:25 AM - last edited on 2025-06-19 6:29 AM by Andrew Neil
Thanks all, I really appreciate the effort of you all reviewing this :)
I still can't get this working, I think my next step is to use a scope and see what's happening as I don't believe the code is "missing" anything now. At least nothing obvious..
My latest iteration I think is as simple as it can get...
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file UART/UART_WakeUpFromStopUsingFIFO/Src/main.c
* @author MCD Application Team
* @brief This sample code shows how to use UART HAL API (UART instance)
* to wake up the MCU from STOP mode using the UART FIFO level.
* Two boards are used, one which enters STOP mode and the second
* one which sends the wake-up stimuli.
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <CustomPinsHub.h>
#include <stm32u5xx_hal.h>
#include <stm32u5xx_hal_gpio.h>
#include <stm32u5xx_ll_usart.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
#define HAL_TIMEOUT_VALUE 0xFFFFFFFF
#define countof(a) (sizeof(a) / sizeof(*(a)))
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart3;
/* USER CODE BEGIN PV */
uint8_t HeaderTxBuffer[] = "\r\nUSART3 WakeUp from stop mode using FIFO\r\n";
uint8_t Part1TxBuffer[] = "\r\n\t Part 1: RXFIFO threshold interrupt\r\n Waiting for characters reception until RX FIFO threshold is reached\r\n Please send 2 bytes\r\n";
uint8_t WakeupRXFTBuffer[] = "\r\n Proper wakeup based on RXFIFO threshold interrupt detection.\r\n";
uint8_t Part2TxBuffer[] = "\r\n\t Part 2: RXFIFO full interrupt\r\n Waiting for characters reception until RX FIFO is Full \r\n Please send 8 bytes\r\n";
uint8_t WakeupRXFFBuffer[] = "\r\n Proper wakeup based on RXFIFO full interrupt detection.\r\n";
uint8_t FooterTxBuffer[] = "\r\nExample finished successfully\r\n";
uint8_t RxBuffer[8];
/* USER CODE END PV */
static void SystemPower_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART3_UART_Init(void);
/* USER CODE BEGIN PFP */
static void EXTI11_Wakeup_Enable(void);
void SystemClock_Config_fromSTOP(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void HAL_MspInit(void)
{
/* USER CODE BEGIN MspInit 0 */
/* USER CODE END MspInit 0 */
__HAL_RCC_PWR_CLK_ENABLE();
/* System interrupt init*/
/* USER CODE BEGIN MspInit 1 */
/* USER CODE END MspInit 1 */
}
void USART3_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart3);
}
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();
__HAL_RCC_HSI_ENABLE();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* Configure the System Power */
SystemPower_Config();
/* USER CODE BEGIN SysInit */
/* Initialize BSP LED */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART3_UART_Init();
HAL_UARTEx_EnableFifoMode(&huart3);
HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_4); // 2 bytes
HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_8);
/* USER CODE BEGIN 2 */
__HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_HSI);
const char *test = "USART3 alive!\r\n";
HAL_UART_Transmit(&huart3, (uint8_t *)test, strlen(test), HAL_MAX_DELAY);
// const char *echoBanner = "\r\n--- UART Echo Test ---\r\n"
// "Type characters, they will be echoed back.\r\n"
// "Send 'S' (0x53) to skip to STOP-mode test.\r\n";
// HAL_UART_Transmit(&huart3, (uint8_t *)echoBanner, strlen(echoBanner), HAL_MAX_DELAY);
// uint8_t ch;
// while (1)
// {
// if (HAL_UART_Receive(&huart3, &ch, 1, HAL_MAX_DELAY) == HAL_OK)
// {
// HAL_UART_Transmit(&huart3, &ch, 1, HAL_MAX_DELAY);
// if (ch == 'S') // user requests to move on
// break;
// }
// }
UART_WakeUpTypeDef wakeup = {
.WakeUpEvent = UART_WAKEUP_ON_READDATA_NONEMPTY};
if (HAL_UARTEx_StopModeWakeUpSourceConfig(&huart3, wakeup) != HAL_OK)
{
Error_Handler();
}
__HAL_RCC_HSISTOP_ENABLE();
HAL_UART_Transmit(&huart3, (uint8_t *)&HeaderTxBuffer, countof(HeaderTxBuffer) - 1, HAL_TIMEOUT_VALUE);
HAL_UARTEx_EnableStopMode(&huart3);
EXTI11_Wakeup_Enable();
HAL_UART_Transmit(&huart3, (uint8_t *)&Part1TxBuffer, countof(Part1TxBuffer) - 1, HAL_TIMEOUT_VALUE);
HAL_UART_Receive_IT(&huart3, (uint8_t *)&RxBuffer, 1);
HAL_SuspendTick();
HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
HAL_ResumeTick();
SystemClock_Config();
HAL_UARTEx_DisableStopMode(&huart3);
HAL_UART_Transmit(&huart3, (uint8_t *)&WakeupRXFTBuffer, countof(WakeupRXFTBuffer) - 1, HAL_TIMEOUT_VALUE);
const char *testEnd = "USART3 alive again!\r\n";
HAL_UART_Transmit(&huart3, (uint8_t *)testEnd, strlen(testEnd), HAL_MAX_DELAY);
HAL_Delay(100);
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
}
/**
/**
* @brief Power Configuration
* @retval None
*/
static void SystemPower_Config(void)
{
/*
* Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral
*/
HAL_PWREx_DisableUCPDDeadBattery();
/* USER CODE BEGIN PWR */
/* USER CODE END PWR */
}
/**
* @brief USART1 Initialization Function
* None
* @retval None
*/
static void MX_USART3_UART_Init(void)
{
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART3;
PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_HSI;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
__HAL_RCC_USART3_CLK_ENABLE();
/* USER CODE BEGIN USART3_Init 0 */
/* USER CODE END USART3_Init 0 */
/* USER CODE BEGIN USART3_Init 1 */
/* USER CODE END USART3_Init 1 */
huart3.Instance = USART3;
huart3.Init.BaudRate = 115200;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_EnableFifoMode(&huart3) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART3_Init 2 */
/* USER CODE END USART3_Init 2 */
}
/**
* @brief GPIO Initialization Function
* None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
HAL_NVIC_SetPriority(USART3_IRQn, UART_IRQ_PRIO, UART_IRQ_SUBPRIO);
HAL_NVIC_EnableIRQ(USART3_IRQn);
}
/* USER CODE BEGIN 4 */
static void EXTI11_Wakeup_Enable(void)
{
EXTI_HandleTypeDef hexti;
EXTI_ConfigTypeDef exticonfig;
exticonfig.Line = EXTI_LINE_11;
exticonfig.Mode = EXTI_MODE_INTERRUPT;
exticonfig.Trigger = EXTI_TRIGGER_RISING;
exticonfig.GPIOSel = EXTI_GPIOC;
HAL_EXTI_SetConfigLine(&hexti, &exticonfig);
}
2025-06-19 6:20 AM
Last ditch bump as a cry for help :) Myself and a colleague have basically tried everything we can think of now, but aren't any closer to getting this working.
2025-06-19 6:59 AM
2025-06-25 2:24 AM
Hello @Alan01252
Any update for this issue?
2025-06-25 2:26 AM
Nope :(
I/we had no joy getting this to work at all. There's obviously something we're missing, but who knows what!
2025-06-25 2:29 AM
Is your UART receive work fine without stop mode settings?
2025-06-25 3:38 AM - edited 2025-06-25 3:39 AM
Yep,
You can see in the sample logs, I proved it my prompting me to hit "S" before going into stop mode.
So code waits to receive S then moves onto stop mode test, where it then fails to wake up :(
2025-06-25 3:59 AM
Is your clock setting the same as the example that I shared before?
2025-06-25 4:06 AM
Sorry if I am being a bit thick, presumably you're talking about the example project? If so, yes I believe I've mapped that like for like.