I intend to use my controller in a half duplex system. I have a GPIO20 connected to the direction control of the RS485 interface IC.
At all times the GPIO20 is output 0 indicating RX state. Only when I want to send something over RS485 network, I set the GPIO20 to switch to TX state.
I am unable to understand, how should I ensure completion of transmission before resetting the GPIO20 pin.
I tried polling:
int RS232_Send(char *buf) {
int a = 0;
GpioDataRegs.GPASET.bit.GPIO20 = 1;
while ((buf[a] != 0) && (SciaRegs.SCIRXST.bit.RXERROR != 1)) {
if (SciaRegs.SCIFFTX.bit.TXFFST < 4) {
SciaRegs.SCITXBUF = buf[a++];
}
}
while (SciaRegs.SCIFFTX.bit.TXFFINT == 0);
GpioDataRegs.GPACLEAR.bit.GPIO20 = 1;
if (SciaRegs.SCIRXST.bit.RXERROR == 1) { // if there is an error reset the SCI
RS232_Reset();
GpioDataRegs.GPACLEAR.bit.GPIO20 = 1;
return 1;
}
return 0;
}
I tried using interrupt method
int RS232_Send(char *buf) {
int a = 0;
GpioDataRegs.GPASET.bit.GPIO20 = 1;
while ((buf[a] != 0) && (SciaRegs.SCIRXST.bit.RXERROR != 1)) {
if (SciaRegs.SCIFFTX.bit.TXFFST < 4) {
SciaRegs.SCITXBUF = buf[a++];
}
}
SciaRegs.SCIFFTX.bit.TXFFIENA = 1;
if (SciaRegs.SCIRXST.bit.RXERROR == 1) { // if there is an error reset the SCI
RS232_Reset();
GpioDataRegs.GPACLEAR.bit.GPIO20 = 1;
return 1;
}
return 0;
}
__interrupt void tx_comp(void){
GpioDataRegs.GPACLEAR.bit.GPIO20 = 1;
SciaRegs.SCIFFTX.bit.TXFFINTCLR = 1;
SciaRegs.SCIFFTX.bit.TXFFIENA = 0;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP9;
}
In both the above cases the GPIO20 is cleared well before the last character in the transmit string, as a result last 2 or 3 characters are not received by the RS485 device.
Please let me know any other method which will ensure a total transmission completion and then turn off the GPIO20.