2024-12-09 11:59 PM - edited 2024-12-10 12:02 AM
I have two custom boards, one with a STM32F407 MCU that I use as a USART RS485 master, the other configured as a USART RS485 slave that echoes back everything I send from the master.
I'm using the HAL libaray, and have successfully tested some simple communication between the two devices, i.e., master send a byte to slave, slave echo back, then master will print out the receivd byte.
But when I tried to include DMA in the trasmitting process, the master can send messages as usual, but failed to receive anything even though I have used an oscillascope the check that the slave is still working properly.
This is the code snippet that works:
void rs485_send(uint8_t * buf, uint8_t len)
{
/* Enable output by driving EN pin high */
HAL_GPIO_WritePin(RS485_TX_GPIO_PORT, RS485_EN_PIN, GPIO_PIN_SET);
HAL_UART_Transmit(&UartHandle, buf, len, 0xFF);
/* Enable input by driving EN pin low */
HAL_GPIO_WritePin(RS485_TX_GPIO_PORT, RS485_EN_PIN, GPIO_PIN_RESET);
}
This is the code snippet that does not work:
void rs485_send(uint8_t * buf, uint8_t len)
{
/* Enable output by driving EN pin high */
HAL_GPIO_WritePin(RS485_TX_GPIO_PORT, RS485_EN_PIN, GPIO_PIN_SET);
HAL_UART_Transmit_DMA(&UartHandle, buf, len);
}
void DMA1_Stream4_IRQHandler()
{
HAL_DMA_IRQHandler(&DMAHandle_Tx);
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == RS485_UARTx)
{
/* Enable input by driving EN pin low */
HAL_GPIO_WritePin(RS485_TX_GPIO_PORT, RS485_EN_PIN, GPIO_PIN_RESET);
}
}
My only guess is that the slave echoed back before the Enable Pin is driven low to allow master to start receiving, but have no idea how to properly solve this issue, can anyone help me with this? Much appreicated!
2024-12-10 02:18 AM
> My only guess is that the slave echoed back before the Enable Pin is driven low to allow master to start receiving, but have no idea how to properly solve this issue
Don't use Cube/HAL which is too extensive, write your own functions over which you have full control.
Also make sure the DMA interrupt has sufficiently high priority.
Maybe it would be enough to switch on compiler optimizations even with Cube/HAL. I don't use Cube/HAL.
Also, chosing a newer STM32 family (anything except 'F1/'L1/'F2/'F4) would bring you the luxury of hardware direction (DE) support in their improved UARTs.
JW