Pages: [1]   Go Down
Author Topic: Software Serial with Half Duplex - For a Shared Serial Bus  (Read 1324 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey Everyone,
I needed to make use of Half Duplex Serial for a project, so I adapted the Software Serial Library to allow for it. You can find it on github:

https://github.com/nickstedman/SoftwareSerialWithHalfDuplex

The value of Half Duplex is that you can daisy-chain a bunch of devices to the same serial line, and communicate with them all. It doesn't however do any error checking or flow control like i2c, so you have to handle that yourself. The robotis AX-12 motors make use of Half-Duplex Serial for example, and it is often used with RS485.

With the standard SoftwareSerial library the TX line is always set as an Output, as you'd expect. With the SoftwareSerialWithHalfDuplex library (sorry for the long name!), by default the TX line works exactly the same as the original, but by adding a couple of optional arguments at instantiation, the TX line can instead be set as an Input, pulled-high. Only when a write/print command is called, does the TX line flip to an Output until the byte is sent, and then resets to an Input. As such, you can make the TX and RX line the same, because transmission will be disallowed while receiving and vice-versa.

Only a few modifications of the SoftwareSerial library were necessary. I'm sure it could be written more efficiently, but I wanted it to be as close to the original as possible to reduce maintenance (and hopefully to be considered for merging... smiley-grin ).

This is my first attempt at it, so any feedback or help is appreciated. Also it's my first time using github so let me know if something's amiss. It's been tested a little bit and seems to work pretty well up to 57600. I've used it to communicate with several Arduinos at once, and also with an AX-12 motor. The Half-Duplex option probably won't work As Is if you need to invert the data, which means that more work is required if you want RS485 communication.

Cheers,
Nick
« Last Edit: July 16, 2012, 05:38:04 pm by steddyrobots » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Nick,
Nice work.  I need exactly this functionality.
Atmel is using what they call "single-wire interface" on several new products now:

Atmel's "Single-wire Software UART" (bitbang)
www.atmel.com/Images/avr274.pdf‎

ATAVRSB201 (ATmega16HVA)
http://www.atmel.com/tools/ATAVRSB201.aspx

Atmel ATSHA204
http://www.atmel.com/devices/atsha204.aspx

Atmel AVR947: Single-Wire Bootloader
www.atmel.com/Images/doc42034.pdf‎

Another option is just tie TX and RX together with a 4.7k resistor (and use existing Hardware or Software serial library)
Code:
MCU
-----     4.7k
TX  |----VVVV---|
RX  |-----------+-----> 1-wire UART bus
-----

(the other devices on the bus must have open-drain outputs, and no additional pull-up)

It works for me from a PC with a USB-to-serial (FTDI 232R) ic.  I was going to try it on the Arduino, but came across this post.


A  potential issue may be that the UART will recieve the same data that is transmitted.  It can be discarded, but I would prefer to force the reciever off while transmitting (half duplex).  I'm trying to figure out if the existing SoftwareSerial library is half-duplex.

« Last Edit: September 04, 2013, 04:05:30 pm by phatpaul » Logged

Pages: [1]   Go Up
Jump to: