mesmariusz
Posts: 57
Joined: Fri Feb 15, 2013 6:07 am

RS485 and Raspbery Pi

Sat Feb 16, 2013 7:24 pm

Hello

I would like to connect my RS485 network to Raspbery Pi.
I know Raspbery Pi has got USART interface working with 3V3 potential.
So I need 3V3 USART to RS485 converter (half duplex).

When I used desktop PC with COM port I used half duplex COM <-> RS485 converter ( http://serwis.avt.pl/manuals/AVT530.pdf )

But this time it is stupid to connect all via two converters:

Raspbery Pi USART 3V3<-> 3V3 level to COM standard <-> COM_RS232_half_duplex_converter <-> RS485 network.

So I need to build 3V3 UART <-> RS485 half duplex converter.

Or maybe something like this already exists?

Do someone use Raspbery Pi as RS485 metwork master / controler / device ?

klricks
Posts: 6550
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: RS485 and Raspbery Pi

Sun Feb 17, 2013 11:25 pm

Go to Ebay and search [3.3 Volt RS485]
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

mesmariusz
Posts: 57
Joined: Fri Feb 15, 2013 6:07 am

Re: RS485 and Raspbery Pi

Mon Feb 18, 2013 12:43 pm

Locally I found something like this:
http://propix.com.pl/pl/p/file/714a1a42 ... 5-Board.7z

But I wondering about bidirectional transmission. It is not supported at this modul. For example in http://serwis.avt.pl/manuals/AVT530.pdf there is realised by 555 IC (it automaticaly switch to sender mode if there are some data on input). In this modul we have to manually schwitch the direction external from one more microcontroler line.


mesmariusz
Posts: 57
Joined: Fri Feb 15, 2013 6:07 am

Re: RS485 and Raspbery Pi

Tue Feb 19, 2013 8:06 am

- First one looks good (only two lines, it means half-duplex, but I'm wondering about how direction of transmission is controlled) - but why could I use one of my only 2 available USB port for it (then pottentialy additional driver difficulties) if I can use USART included in Raspbery?
- Second one is full duplex (two RS485 lines, one transmitter, second receiver) - not usable in my RS485 network.

I think schematic from AVT it the best in this case. But I think I will prepare a new project for EP (using Design Spark from RS of course :-) - additional board for Raspbery, adapter for RS485 including transmission direction driver based on NE555.

mesmariusz
Posts: 57
Joined: Fri Feb 15, 2013 6:07 am

Re: RS485 and Raspbery Pi

Tue Feb 19, 2013 8:15 am

The easiest way is to use IC designed for 3.3V pover supply to connect it directly to Raspbery, but it would be better if it have galvanic separation also.

So only one thing I'm wondering now is how to implement galvanic separation. Are there any electronic designers colleagues?

majenko
Posts: 16
Joined: Fri Jun 01, 2012 9:53 am

Re: RS485 and Raspbery Pi

Fri Mar 22, 2013 3:01 pm

The chip I use for 3.3V RS-485 is the MAX3485 chip - half duplex, takes input from TTL level serial.

It has RE# and DE lines for switching to between receive and transmit - how you drive those is up to you. I have seen schematics with MOSFETs, and with 555 timers. Ideally you would drive it from a GPIO line and control it via software. Easy enough to switch to TX before transmitting - the trick though is to switch back to RS immediately the transmission has finished...

That's the bit I'm struggling with right now if anyone has any clues - how to delay until the last bit has left the UART. I'm guessing that step one is to switch the IO stream to un-buffered mode (simple enough), then do your transmitting. At that point the output data should all either be transmitted or in the UART's hardware buffer. So how can I probe that buffer to see if it's empty?

Is the Pi using the "Mini UART" for the console? If so there is the register AUX_MU_LSR_REG (0x7E21 5054) which has bit 6 "Transmitter idle" - "This bit is set if the transmit FIFO is empty and the
transmitter is idle. (Finished shifting out the last bit)."

If it's not using the mini uart, but using the main UART, then the register UART_FR looks interesting. It has the flag "TXFE " - "Transmit FIFO empty.". However, it is slyghtly complecated:
The meaning of this bit depends on the state of the FEN bit in the Line Control Register, UARTLCR_LCRH.
If the FIFO is disabled, this bit is set when the transmit holding register is empty.
If the FIFO is enabled, the TXFE bit is set when the transmit FIFO is empty. This bit does not indicate if there is data in the transmit shift register.
So you'd have to combine that with BUSY in the same register which indicates that the UART is actively transmitting data. Wait for the FIFO to empty, then wait for the UART to become idle.

So... My question is, given all that lot, what is the best way to delay my program after transmitting data through the UART to ensure that all the data has left the UART before continuing with my program?
Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies

They say computers only do what you tell them to. They're wrong. Mine still hasn't gone to hell.

majenko
Posts: 16
Joined: Fri Jun 01, 2012 9:53 am

Re: RS485 and Raspbery Pi

Fri Mar 22, 2013 3:34 pm

OK, to partially answer my own question, the Pi uses the main UART for the GPIO connector and system console. That makes sense I guess - you'd want to use the best one for the job.

I have knocked together a little program to probe the UART registers, and a combination of TXFE and BUSY looks promising.
Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies

They say computers only do what you tell them to. They're wrong. Mine still hasn't gone to hell.

majenko
Posts: 16
Joined: Fri Jun 01, 2012 9:53 am

Re: RS485 and Raspbery Pi

Fri Mar 22, 2013 4:10 pm

Ok, and now to finish answering my question :)

To use a MAX3485 chip and manually drive the RE#/DE lines (both lines tied to a single GPIO pin), you need to:

1. Output a 1 on the GPIO pin
2. Output your data
3. Do an fsync() or fflush()
4. Wait for TXFE to go true.
5. Wait for BUSY to go false.
6. Output a 0 on GPIO pin

I'm attaching my sample program that uses GPIO 7 and outputs 4 U's in succession over and over again so you can see the timing on a scope.
Attachments
uarttest.zip
(1.14 KiB) Downloaded 1348 times
Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies

They say computers only do what you tell them to. They're wrong. Mine still hasn't gone to hell.

dibakarb
Posts: 3
Joined: Sat Jun 29, 2013 7:25 am

Re: RS485 and Raspbery Pi

Sat Jun 29, 2013 7:31 am

@majenko
Hey so I was having the same doubts as you. I have been writing python codes to access the GPIO. I was wondering if you can tell me if there is anyway to access just the UART Status Bits using the python-serial library. I have been trying to avoid kernel access but I presume there is no way to get past that in this case.

Also can you mention some good resource on the BCM-2708 chip so I can directly talk to the kernel.

Return to “Interfacing (DSI, CSI, I2C, etc.)”