rs485 repeater

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

I have a pc connected to a board (xmega64a3) via rs232.
This board is connected to another (xmega32a4) via rs485.
I want to reach the second board from the PC, so I need a sort of "pass-through" between rs232 and rs485.

I started with the usart_driver of app note 1307, adding the support for rs485.
In the USART_TXBuffer_PutByte() function I set the rs485 enable pin.

bool USART_TXBuffer_PutByte(USART_data_t *usart_data, uint8_t data) {
    uint8_t tempCTRLA;
    uint8_t tempTX_Head;
    bool TXBuffer_FreeSpace;
    USART_Buffer_t * TXbufPtr;

    #ifdef USART_USE_RS485
    USART_RS485_Tx();
    #endif
    TXbufPtr = &usart_data->buffer;
    TXBuffer_FreeSpace = USART_TXBuffer_FreeSpace(usart_data);
...

Then, I added the isr for TransmiComplete interrupt:

void USART_TransmitComplete(USART_data_t *usart_data) {
    #ifdef USART_USE_RS485
    uint8_t tempHead = usart_data->buffer.TX_Head;
    uint8_t tempTail = usart_data->buffer.TX_Tail;
    if (tempHead == tempTail) {
        USART_RS485_Rx();
    }	
    #endif
}

In the main loop I do the following:

if (USART_RXBufferData_Available(&BUS_USART_data)) {
    f_ch = USART_RXBuffer_GetByte(&BUS_USART_data);
    USART_TXBuffer_PutByte(&DEBUG_USART_data, f_ch);
} 

It works fine when I send char-by-char (e.g. typing in a terminal) or when I print an entire packet (e.g. with printf).

When chars arrive too fast (e.g. sending a file with realterm) I get some weird data.

It's because the rs485 enable pin will deselect the line while the transmission is still active.

Perhaps the check for empty buffer in txc isr is wrong?

The best option should be to use a timer interrupt to delay a bit the USART_RS485_Rx(), but it's not very straightforward.

Do you have any hint?

Thanks!
Marco

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It seems I solved enabling the txc interrupt when last byte is sent to data register. Furthermore there was another bug: I should not use #ifdef to select when to use the rs485 because on the first board I have two different kind of serials!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
It seems I solved enabling the txc interrupt when last byte is sent to data register.
You must use the TXC bit for RS485 or wait 2 characters time before changing direction or turning off the transmitter.

In fact some people just use TXC and don't bother with UDRE at all with RS485.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly