cancel
Showing results for 
Search instead for 
Did you mean: 

UART communication problem between Raspberry Pi 4 and STM32F767ZI

ChristenTen
Associate II

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:

0693W00000JObRJQA1.png 

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.

8 REPLIES 8
Bruno_ST
ST Employee

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.

ChristenTen
Associate II

Thanks for your answer @Bruno_ST​.

The UART on Raspberry pi 4 is 3V3 and not 5V. I checked this with my scope.

Muhammed Güler
Senior III

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/

ChristenTen
Associate II

@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.

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.

Javier1
Principal

I know its a dumb question but is your wiring OK?

Are you wiring your stm32's GND with your raspi GND¿

we dont need to firmware by ourselves, lets talk

I already checked the connection : Tx_Raspberry -> Rx_STM32 and Rx_Raspberry ->Tx_STM32 and also same ground.

i find ftdi's usb-UART modules very usefull to debug this kind of scenarios.

0693W00000JOokrQAD.png 

**extra: i had some problems back in the day with the raspi's internal bluetooth trying to take over the uart i was using .....

we dont need to firmware by ourselves, lets talk