Main Content

SingleWireSerial - Arduino Library that Supports Single-Wire Half-Duplex Serial Communication

A new Arduino library has seen the light of day: SingleWireSerial. It supports single-wire, half-duplex serial communication. By using the input capture feature of the AVR MCUs, it is extremely accurate and supports bitrates up to 250 kbps robustly. And contrary to its title, one can even use it in a two-wire setting.

Earlier this year, I worked on a hardware debugger making use of the debugWIRE protocol. But I couldn’t get it to work reliably. One problem seemed to be the serial communication using only one line (the RESET pin of the MCU). People had come up with different solutions, but none of them worked for me reliably. So, recently I set out to program my own solution that led to the SingleWireSerial library. It satisfies the following three requirements:

single-wire serial communication,
extremely accurate and robust up to 125 kbps,
communication speed can be set at runtime.
First, usually asynchronous serial communication is done using two wires. If one wants to restrict the communication to only one wire, this is possible. But then there should be a strict protocol about which party is allowed to transmit data. For instance, if a master is the one who sends requests or queries to which the slave responds, it is clear who is in the role of a sender at each point of time. This can all be solved on the software level. On the hardware level one needs to come up with a solution that permits two parties to send and receive on one wire only. Or one addresses this problem at the software level too.

Second, if you want to transmit strings about the room temperature, then nobody gets annoyed when at some point of time the wrong temperature is displayed. When you are debugging a system and a wrong value is displayed because of a communication error, then one is, of course, upset. debugWIRE does unfortunately not have a any form of error detection, so one has to rely on the fact that there is no communication error at all. Since the communication speed is set to the system clock divided by 128, at 16 MHz system clock the communication speed will be 125 kbps. So, we need error-free serial communication at 125 kbps!

Third, if the communication speed is known ahead at compile time, then picoUART is probably the best alternative. However, with debugWIRE we only learn about the communication speed at runtime. Furthermore, we want to get as close as possible to the communication speed the MCU has that we want to debug, which might diverge from the standard ones because the clock speed is controlled by an internal RC oscillator.

Single-wire: Hardware or software solution?
You can create a single-wire serial solution basically in two ways. First, you can join the TX and RX lines somehow. Second, you have only one line from the beginning.

Joining TX and RX involves some external hardware. The Microchip application note AN2658 describes this in detail, employing two transistors–one inverter and one open collector driver. The same effect can more simply achieved by having a pull-up resistor attached to the RX line and a diode between the RX and TX line with the anode at the RX line, as is sketched in the next picture.”

Link to article