Code Monkey home page Code Monkey logo

avruart's Introduction

avrUART

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.

Echo [demo|fullcode]

#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

Echo sequences of 10 bytes [demo]

while(true) {
  /** Wait for a reception of 10 bytes. */
  auto bytes = uart.get_bytes<10>();
  
  for(auto c : bytes)
    uart.put(c);
}

How to use it

This is a header-only library, so nothing needs to be compiled:

  1. Check the requirements and dependencies section.
  2. Add the include directory to your include path, as well as the path to include directory related to avrIO.
  3. 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.
  4. [If using the RC oscillator], it’s important to calibrate it through the register OSCCAL.
  5. Include the header avr/uart.hpp (#include <avr/uart.hpp>) in your source.

Requirements and dependencies

  1. avr-gcc with -std=c++17 and -Os.
  2. The header-only llibrary avrIO to define Tx and Rx pins.

Space performance [code size]

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.

Tests

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.

License

avrUART is released under MIT License.

avruart's People

Contributors

ricardocosme avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.