2025-02-11 1:41 AM - last edited on 2025-02-11 6:17 AM by Andrew Neil
Hi there, I have an B-WL5M-SUBG1 evaluation board which uses the STM32WL5M module.
I am having problems with the STM32 receiving a message I am sending it from my laptop via python.
I connect between the stm32 and the laptop via an RS422/485 converter. I've used the converter many times before to read print statements from the stm32.
Here is the stm32 code I am using:
#include "main.h"
#include "stm32wlxx_hal.h"
#include <string.h>
#include <stdio.h>
/* Private variables */
UART_HandleTypeDef huart2;
uint8_t rx_buffer[4]; // Buffer for received data
volatile uint8_t data_received = 0; // Flag for data reception
/* Function prototypes */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
void Error_Handler(void);
void check_uart_errors(void);
int main(void)
{
uint8_t tx_ready[] = "READY\r\n";
/* Initialize system */
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
/* Reset UART2 before use */
__HAL_RCC_USART2_FORCE_RESET();
HAL_Delay(10);
__HAL_RCC_USART2_RELEASE_RESET();
MX_USART2_UART_Init();
/* Transmit READY message */
if (HAL_UART_Transmit(&huart2, tx_ready, sizeof(tx_ready) - 1, 300) != HAL_OK)
{
Error_Handler();
}
HAL_Delay(10); // Prevent issues
/* Clear UART RX buffer and start receiving */
__HAL_UART_FLUSH_DRREGISTER(&huart2);
memset(rx_buffer, 0, sizeof(rx_buffer));
if (HAL_UART_Receive_IT(&huart2, rx_buffer, sizeof(rx_buffer)) != HAL_OK)
{
Error_Handler();
}
while (1)
{
if (data_received)
{
data_received = 0; // Reset flag
/* Debug: Print received bytes */
char debug_msg[50];
sprintf(debug_msg, "RX CALLBACK: %02X %02X %02X %02X\r\n",
rx_buffer[0], rx_buffer[1], rx_buffer[2], rx_buffer[3]);
HAL_UART_Transmit(&huart2, (uint8_t *)debug_msg, strlen(debug_msg), 300);
HAL_Delay(10);
/* Echo back the received 4 bytes */
if (HAL_UART_Transmit(&huart2, rx_buffer, 4, 300) != HAL_OK)
{
Error_Handler();
}
/* Restart reception */
memset(rx_buffer, 0, sizeof(rx_buffer));
if (HAL_UART_Receive_IT(&huart2, rx_buffer, sizeof(rx_buffer)) != HAL_OK)
{
Error_Handler();
}
check_uart_errors(); // Check for errors after transmission
}
}
}
/**
* @brief System Clock Configuration
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6; // 4 MHz
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief USART2 Initialization Function
*/
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief UART RX Complete Callback
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART2)
{
data_received = 1; // Set flag to process received data
}
}
/**
* @brief Check UART Errors
*/
void check_uart_errors(void)
{
uint32_t error = HAL_UART_GetError(&huart2);
if (error != HAL_UART_ERROR_NONE)
{
char error_msg[50];
sprintf(error_msg, "UART ERROR: 0x%lX\r\n", error);
HAL_UART_Transmit(&huart2, (uint8_t *)error_msg, strlen(error_msg), 300);
}
}
/**
* @brief GPIO Initialization Function
*/
static void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Configure PA2 and PA3 as Alternate Function (AF7) for USART2 */
GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/**
* @brief Error Handler.
*/
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
My python code is this:
#!/usr/bin/env python3
import serial
import time
def main():
ser = serial.Serial(
port="COM4",
baudrate=115200,
bytesize=8,
parity='N',
stopbits=1,
timeout=2,
xonxoff=False,
rtscts=False,
dsrdtr=False
)
print(f"Opened COM4 at {ser.baudrate} baud.")
# Wait for READY message
start_time = time.time()
ready_received = False
while time.time() - start_time < 5:
line = ser.readline()
if line:
decoded = line.decode(errors="replace")
print("MCU says:", decoded, end='')
if "READY" in decoded:
ready_received = True
break
if not ready_received:
print("No READY message received from MCU.")
ser.close()
return
# Clear UART buffer before sending
ser.reset_input_buffer()
time.sleep(0.1)
# Send 4 bytes
test_msg = b"DATA"
print("\nSending:", test_msg)
ser.write(test_msg)
ser.flush()
# Wait for response with timeout
timeout = time.time() + 5
while time.time() < timeout:
line = ser.readline()
if line.startswith(b"RX CALLBACK: "):
print("MCU Debug:", line.decode(errors="replace").strip())
continue
elif len(line) == 4:
print("Received:", line)
break
else:
print("Error: No response received from STM32.")
ser.close()
print("Done.")
if __name__ == "__main__":
main()
and my output from python is:
Opened COM4 at 115200 baud.
MCU says: READY
Sending: b'DATA'
Error: No response received from STM32.
Done.
Does anybody have any ideas on what the problem is here and how I could fix it. Any help would be greatly appreciated.
Thank you.
2025-02-24 11:40 PM
You can't connect the USB<>RS422/485 signals directly to the STM32 as those are differential signals.
You need a USB<>TTL adapter like this one https://amzn.to/3EUs8BN
2025-02-24 11:47 PM
Thank you very much for your reply Karl.
Out of interest then.
1) How come I can send and receive messages connected this way? (Just get some garbage now and again).
2) I was playing around with my existing setup and the LoRa examples code on the Cube IDE which has a LPUSART example and the I got the code working (but forgot to save *** me but I was just playing around).
I thought I was getting somewhere with some existing examples so will try and probe around there a bit more and let you know.
2025-02-25 12:12 AM
You are just getting lucky to get data.
2025-02-25 12:14 AM
Ah I see, thanks for the info.
So with my current setup then, are you saying I shouldn’t be able to receive data from the STM32 or send data to it, because in isolation, I do without fail and without error.
Also I can see there is an option in the ioc to select RS485 as an option for my USART.
2025-03-03 1:39 AM
2025-03-03 2:06 AM
So why are you connecting the ST-LinkV3 UART to the USB<>TTL adapter? I thought you were trying to communicate with the B-WL5M-SUBG1? Had you mentioned you had the ST-LinkV3 which has a built in VCP, then i would not have suggested the USB<>TTL adapter. You could connect the ST-LinkV3 VCP to the B-WL5M-SUBG1.
Also, I noticed in your images, you have Rx to Rx and Tx to Tx, which incorrect. With UART, first device's Rx connects to the 2nd device's Tx. Then first device's Tx connects to the 2nd device's Rx. So hopefully that was just a mistake and you didn't do that with the B-WL5M-SUBG1 previously?
2025-03-03 2:46 AM
Hi Karl,
Thank you for your reply.
I was trying to communicate with the B-WL5M-SUBG1 module from my Laptop. My previous messages included photographs of all the equipment I am using, debugger included.
I don't want to use a built in VCP> I wan't to use it as if it were a deployed piece of kit, the board communicating with a PC.
I tried swapping the Tx and Rx lines and I got no response.