This is a compact code-size C++17 header-only implementation of a bit-banged UART solution. It is designed to handle speeds greater than or equal to 8 CPU cycles per bit, which means, for example, 1 Mbps @ 8 MHz. The data frame configuration supported is 8-N-1, with 8 bits of data, no parity, and 1 stop bit. The implementation is based on the AVR305 application note, and inline assembly is used to guarantee the precise time-sensitive code needed for implementing an UART software solution. The C++ layer is just a stub for the assembly implementation. The only dependency is avrIO, which is used to receive the Tx
and Rx
pins as arguments, and the application program must be compiled with -Os
optimization.
#include <avr/uart.hpp>
using namespace avr::io;
using namespace avr::uart::literals;
int main() {
avr::uart::soft<Pb0/*tx*/, Pb1/*rx*/, 38400_bps, 1_MHz> uart;
while(true)
uart.put(uart.get());
}
Receives 1 byte through the Pb1
pin (Rx
) and echoes it through the Pb0
pin (Tx
). Transmission and reception operates at a baud rate of 38,400 bps, and the AVR is operating with a CPU frequency of 1 MHz.
106 bytes @ ATtiny13A using avr-g++ 13.2.0 with -Os
while(true) {
/** Wait for a reception of 10 bytes. */
auto bytes = uart.get_bytes<10>();
for(auto c : bytes)
uart.put(c);
}
This is a header-only library, so nothing needs to be compiled:
- Check the requirements and dependencies section.
- Add the
include
directory to your include path, as well as the path to include directory related to avrIO. - Choose a baud rate and a clock frequency. Keep in mind that the
ratio
clock_frequency/baud_rate
represents the bit length in CPU cycles. This number must be greater than or equal than 8, and the library we will use an integer approximation of it. It’s important to select a pair with minimum deviation between the real number and the integer that will be used. You can refer to helper/clk-freq_baud-rate.cpp for a better understanding. - [If using the RC oscillator], it’s important to calibrate it
through the register
OSCCAL
. - Include the header
avr/uart.hpp
(#include <avr/uart.hpp>
) in your source.
avr-gcc
with-std=c++17
and-Os
.- The header-only llibrary avrIO to define
Tx
andRx
pins.
4 bytes
are required to set up the UART through the constructor soft::soft()
. The number of bytes required to send or receive a byte depends on the bit length in clock cycles. As an example, a speed of 38,400 bps @ 1 MHz, requires 28 bytes
of code size to assemble the put()
function, and 30 bytes
of code size to assemble the get()
function.
Tested with the following pairs of baud rate and CPU clock frequency using ATtiny13A or ATtiny85 with a calibrated RC oscillator:
- 1 Mbps @ 8 MHz
- 921.6 kbps @ 8 MHz [*]
- 576.0 kbps @ 8 MHz
- 500.0 kbps @ 8 MHz, 8.5 MHz
- 230.4 kbps @ 8 MHz
- 115.2 kbps @ 8 MHz
- 57,600 bps @ 1 Mhz
- 38,400 bps @ 1 MHz, 1.2 MHz
- 19,200 bps @ 1 MHz
- 9,600 bps @ 1MHz
[*] This isn’t a good pair, and it was tested only to transmit data to support tests for 1 Mbps @ 8 MHz.
avrUART is released under MIT License.