Ralph Doncaster's picoUART Is a "Better Bit-Bang UART" for 8-Bit AVR Microcontrollers

Supporting a wider range of baud rates, tighter timing, and one-wire operation, picoUART is a great tool for the AVR programmer.

Gareth Halfacree
4 years agoHW101

Ralph Doncaster's small UART for 8-bit AVR microcontrollers proved popular when it was released back in 2014 — but now, armed with six years' experience, it has been replaced with something shinier and newer: picoUART, which comes complete with single-pin operation support and wider baud rate compatibility.

"Over the past years, one of my most popular blog posts has been a soft UART for AVR MCUs," Doncaster explains in a blog post brought to our attention by Dangerous Prototypes. "I've seen variations of my soft UART code used in other projects. When MicroCore recently integrated a modified version of my old bit-bang UART code, it got me thinking about how I could improve it.

"[picoUART] has several improvements over my old code. The transmit timing of my earlier soft UART implementations is accurate to within 3 clock cycles. On an MCU running at 8MHz, that limited the lowest baud rate to around 8000/768 or 10.4kbps. To allow for lower bit rates, picoUART needed to support longer delays. I also wanted to support more accurate timing, so picoUART uses __builtin_avr_delay_cycles during the transmission of each bit. The exact number of cycles to wait is calculated by some inline functions, which is a better way of doing the calculations than the macros I had used before.

"As with the transmit code," Doncaster continues, "picoUART's receive code is accurate to within one cycle. Unlike my earlier UART code, picoUART returns after reading the 8th bit instead of waiting for the stop bit. Because of this change, picoUART begins by waiting for the line to be high before waiting for the start bit.

"Without the initial wait for high, back-to-back calls to purx() could lead an error when the 8th bit of one frame is 0 (low) and gets interpreted as the start bit of the next frame. This change approximately triples the amount of time for the AVR to process each byte in a continuous stream of data."

The new picoUART is available as a release file suitable for loading directly into the Arduino IDE, while a Makefile is included for those who are "an old AVR developer [...] that prefers a keyboard over a mouse." Both are available under the permissive MIT licence on Doncaster's GitHub repository, while his blog post includes details of the UART's operation and exactly how it improves on its predecessor.

Gareth Halfacree
Freelance journalist, technical author, hacker, tinkerer, erstwhile sysadmin. For hire: freelance@halfacree.co.uk.
Latest articles
Sponsored articles
Related articles
Latest articles
Read more
Related articles