Skip to main content
Associate III
May 14, 2025
Question

RS485 example for stm32 G4

  • May 14, 2025
  • 5 replies
  • 825 views

Hi,
Is there any RS485 example code available for STM32G497 ?
I referred in community for sample code for enabling RS485, didn't get it.

This topic has been closed for replies.

5 replies

ST Technical Moderator
May 14, 2025

Hello @abhijith_raj 

Unfortunately, there is no example RS485 in our STM32Cube firmware.

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om
Associate III
May 14, 2025

Hi @Saket_Om 
I'm implementing RS485 using USART1 and need to configure the DE (Driver Enable) pin. I came across the HAL_RS485Ex_Init() API in the HAL driver, but I didn't find any specific configuration related to the DE pin within it. Should I call HAL_RS485Ex_Init() instead of HAL_UART_Init() for RS485 functionality, or is it required to use both? Also, how exactly should the DE pin be configured in this context?

Guenael Cadier
ST Employee
May 14, 2025

Hi @abhijith_raj 

In order to use USART1 in RS485 mode, you have to call HAL_RS485Ex_Init() API (instead of HAL_UART_Init()).
You also need to configure your DE pin as Alternate Function (in addition to TX/RX pins).

You could also use STM32CubeMX, which will generate initalisation code for your project, if you prefer :

GuenaelCadier_0-1747228130210.png

GuenaelCadier_1-1747228146052.png

Then you should be able to find generated code in main.c and stm32g4xx_hal_msp.c files.

Regards

 

Associate III
May 15, 2025

Hi @Guenael Cadier ,
In this configuration, the PA12 pin is used for DE control. However, in our schematics, this pin is assigned for a different purpose, so we’ve selected another GPIO for DE. How can I control the DE line in this case? Can I use HAL_GPIO_WritePin to manually control the GPIO for each transaction?

Guenael Cadier
ST Employee
May 15, 2025

Hi @abhijith_raj 

If you can't assign a pin for USART1_DE as Alternate function, then it means it cannot be driven automatically by USART1 peripheral. then, I think it should be feasible to initilialize USART1 in classic Asynchronous mode (HAL_UART_Init()) and drive your DE pin manually in your application using HAL_GPIO_xxx APIs.
Regards