STM32F7 RS485 DE Enable Not working
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.
