2022-08-16 10:36 AM
I am implementing a MODBUS RTU (slave) using STM32F405 custom board and RS485, the hardware is working fine when I test using HAL functions generated with CUBMX, I am writing my own drivers and I do not want to use HAL. I have managed to create my objects and the receive part is working fine, the parser is generating the PDU, but when I am Transmitting the response to master I always receiving a 'NULL' char before my data and another 'NULL' char after my data. I am using interrupts to achieve the goal and here is my code:
StatusType UART::StartTransmit() {
WDG = Sys_Tick;
if (State == STATE::WAIT_TX) {
BytesSent = 0;
State = STATE::BUSY_TX;
UART_DISABLE_IT(USART, UART_IT_PE);
UART_DISABLE_IT(USART, UART_IT_ERR);
UART_DISABLE_IT(USART, UART_IT_RXNE);
UART_ENABLE_IT(USART, UART_IT_TXE);
return SYS_OK;
} else {
return SYS_BUSY;
}
}
The StartTransmit() method disables the receive interrupts and enables the TXE interrupt which will be used to feed the PDU byte by byte to the TX FIFO.
void UART::HandleTX() {
if ((USART->SR & USART_SR_TXE) && (USART->CR1 & USART_CR1_TXEIE)) {
USART->DR = Buff[BytesSent++];
if (BytesSent == MsgLen) {
UART_DISABLE_IT(USART, UART_IT_TXE);
UART_ENABLE_IT(USART, UART_IT_TC);
}
}else if ((USART->SR & USART_SR_TC) && (USART->CR1 & USART_CR1_TCIE)) {
StartReceive();
}
}
The HandleTX() method is called from USART6_IRQ Handler and as you can see it feeds the bytes to USART->DR and enables the TCIE when all bytes have been sent. Once the TC interrupt is received the StartReceive() method is called to listen to any Master request.
All is working fine but there is always two additional bytes added at the start and end of the PDU which causes communication failure.
For Example my PDU is:
0x01 0x03 0x02 0x30 0x00 0x44 0xAC
when sent I get:
0x00 0x01 0x03 0x02 0x30 0x00 0x44 0xAC 0x00
Any Idea what is happening??
2022-08-16 10:18 PM
Hi,
I think so You should disable interrupt TC (transmission complete) before call to the function "StartReceive()".
Best Regards,
Slawek