2025-01-13 09:43 AM - edited 2025-01-13 09:51 AM
Hi
I have been trying to configure the RS485 and trying to enable DE line. Though I have configured the DE line correctly, but still when HAL_UART_Transmit() is called, it just stays low and never get enabled for D to tranmit the data to the destination. Here is the code:
void UART6_Init(void) {
__HAL_RCC_USART6_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct;
// UART TX (PG14) and RX (PG9) pin configuration
GPIO_InitStruct.Pin = GPIO_PIN_14 | GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
// DE (PB0) pin configuration for alternate function
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // Alternate function for DE
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// UART initialization
huart6.Instance = USART6;
huart6.Init.BaudRate = 9600;
huart6.Init.WordLength = UART_WORDLENGTH_8B;
huart6.Init.StopBits = UART_STOPBITS_1;
huart6.Init.Parity = UART_PARITY_NONE;
huart6.Init.Mode = UART_MODE_TX_RX;
huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart6.Init.OverSampling = UART_OVERSAMPLING_16;
// Enable RS-485 DE mode with 1-bit assertion and de-assertion time
if (HAL_RS485Ex_Init(&huart6, UART_DE_POLARITY_HIGH, 2, 2) != HAL_OK) {
printf("Failed to enable RS-485 DE mode!\n\r");
while (1); // Stay here on error
}
printf("USART_CR3:==> 0x%08lx\r\n", USART6->CR3); // Should have DEM bit set
printf("USART_CR1==>: 0x%08lx\r\n", USART6->CR1); // Should have DEAT and DEDT bits set
// Enable USART6 interrupt with priority 1
HAL_NVIC_SetPriority(USART6_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(USART6_IRQn);
//output for the above printf:
//USART_CR3:==> 0x00004000
//USART_CR1==>: 0x0084000d
}
void MinimalTest(void) {
uint8_t testMsg[] = "RS-485 Test\n";
while (1) {
HAL_UART_Transmit(&huart6, testMsg, sizeof(testMsg) - 1, HAL_MAX_DELAY);
HAL_Delay(10000);
}
}
int main(void)
{
/* Configure the MPU attributes */
MPU_Config();
/* Enable the CPU Cache */
CPU_CACHE_Enable();
HAL_Init();
/* Configure the system clock to 216 MHz */
SystemClock_Config();
UART6_Init();
MinimalTest();
}
DE line never gets enabled.
I have tried using the GPIO way of toggling the PB0, as that works perfectly fine,
so it is confirmed that there can be any issue with connections or the hardware settings.
I just wanted to remove GPIO manual toggling and wanted to use DE Enable line to go to high
when USART is trying to send the data on the RS485.
The board i am using is:
Nucleo-144 (STM32f767ZI)
Any comments what can be the issue, will be really helpful.
Solved! Go to Solution.
2025-01-13 10:28 AM
DS11532 Rev 8 Table 13. STM32F765xx, STM32F767xx, STM32F768Ax, and STM32F769xx alternate
function mapping says UART4_CTS for PB0 AF8, not USART6.
2025-01-13 10:20 AM
The STM32CubeMX generated code for my STM32L432KC looks the same and works here.
hth
KnarfB
2025-01-13 10:26 AM
Now that is interesting, I checked the datasheet of STM32L432KC , it specifically mentions in:
Section 3.24:
These interfaces provide asynchronous communication, IrDA SIR ENDEC support,
multiprocessor communication mode, single-wire half-duplex communication mode and
have LIN Master/Slave capability. They provide hardware management of the CTS and RTS
signals, and RS485 Driver Enable. They are able to communicate at speeds of up to
10Mbit/s.
But in the datasheet for STM32F767ZI, nothing of that sort has been mentioned, apart in the Table in it. Now, I doubt whether STM32F7 really support DE Line automatically or not.
2025-01-13 10:28 AM
DS11532 Rev 8 Table 13. STM32F765xx, STM32F767xx, STM32F768Ax, and STM32F769xx alternate
function mapping says UART4_CTS for PB0 AF8, not USART6.
2025-01-13 10:39 AM
This is from STM32f7xx:
/**
* @brief AF 8 selection
*/
#define GPIO_AF8_UART4 ((uint8_t)0x08U) /* UART4 Alternate Function mapping */
#define GPIO_AF8_UART5 ((uint8_t)0x08U) /* UART5 Alternate Function mapping */
#define GPIO_AF8_USART6 ((uint8_t)0x08U) /* USART6 Alternate Function mapping */
#define GPIO_AF8_UART7 ((uint8_t)0x08U) /* UART7 Alternate Function mapping */
#define GPIO_AF8_UART8 ((uint8_t)0x08U) /* UART8 Alternate Function mapping */
#define GPIO_AF8_SPDIFRX ((uint8_t)0x08U) /* SPIDIF-RX Alternate Function mapping */
#define GPIO_AF8_SAI2 ((uint8_t)0x08U) /* SAI2 Alternate Function mapping */
#if defined (STM32F765xx) || defined(STM32F767xx) || defined(STM32F769xx) || defined(STM32F777xx) || defined(STM32F779xx)
#define GPIO_AF8_SPI6 ((uint8_t)0x08U) /* SPI6 Alternate Function mapping */
#endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
And i am using USART6
2025-01-13 10:48 AM
You are right.. i was on wrong Pin. I set it to PG12, bingo DE is toggling.... it is working now.. Thanks KnarfB