2022-02-04 07:02 AM
Hello everyone,
I'm trying to debug a problem with the UART communication between the raspberry pi 4 and an STM32.
Indeed, I try to send the number 1 from the raspberry pi to the stm32 but I can't get the right result.
Here is the code I implemented on the raspberry pi 4 :
import serial
from time import sleep
ser = serial.Serial(
port = "/dev/ttyS0",
baudrate = 9600,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1
)
data = 1
while True:
data_str_list = [str(data)]
send_string = ','.join(data_str_list)
ser.write(send_string.encode())
sleep(1)
Here is the STM32 code :
char data_sign[1];
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_USART3_UART_Init();
MX_DAC_Init();
MX_UART4_Init();
MX_TIM6_Init();
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_UART_Receive(&huart4, data_sign, sizeof(data_sign), 1000);
}
/* USER CODE END 3 */
}
static void MX_UART4_Init(void)
{
huart4.Instance = UART4;
huart4.Init.BaudRate = 9600;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart4) != HAL_OK)
{
Error_Handler();
}
}
When I send 1, I get this with the STM32 debug:
Sometimes, the value is 241 but I don't know how to interpret this data. Can you please help me please? I think it's a problem with the data types but I tried to change them but I still can't find the right result.
I also changed the clock frequency of the STM32 but nothing changes. The function HAL_UART returns 0.
2022-02-04 07:43 AM
Hello @ChristenTen ,
Raspberry PI 4 UART Levels for RX/TX are 0V to 5V and STM32 levels are from 0V to 3.3V.
You need a level shifter to secure communication from STM32 to rasberry PI4. Make sure that you use PIO that are 5V tolerent also.
When PI4 doinx a TX the characters received by STM32 should be undersood.
Try to perform a loopback (shortcut between TX & RX) on STM32 first to be sure the UART is working fine.
BR,
Bruno
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2022-02-04 09:07 AM
Thanks for your answer @Bruno_ST.
The UART on Raspberry pi 4 is 3V3 and not 5V. I checked this with my scope.
2022-02-04 09:25 AM
Your code on the STM32 side can only read 1 byte. The 1bytes of data you read looked like part of a UTF-8 encoded character.
If you don't know how many bytes the incoming data will be, below is an example I randomly found on google. it will solve your problem.
http://www.bepat.de/2020/12/02/stm32f103c8-uart-with-dma-buffer-and-idle-detection/
2022-02-05 12:50 AM
@Muhammed G�ler
What I wanted is to receive just 1 byte (0x01 or 0x00). Nevertheless, I get 240 but I can't see the link between 240 and 1 even with the UTF-8 encoding.
2022-02-07 12:17 AM
Sorry, you are right !! I mixed-up myself...
Bruno
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2022-02-07 12:24 AM
I know its a dumb question but is your wiring OK?
Are you wiring your stm32's GND with your raspi GND¿
2022-02-07 02:13 AM
I already checked the connection : Tx_Raspberry -> Rx_STM32 and Rx_Raspberry ->Tx_STM32 and also same ground.
2022-02-07 03:38 AM
i find ftdi's usb-UART modules very usefull to debug this kind of scenarios.
**extra: i had some problems back in the day with the raspi's internal bluetooth trying to take over the uart i was using .....