Various strategies to write a low-level serial driver (harware or microcontroller)


http://www.pololu.com/picture/0J1035.600.png

Busy wait

Trigger on the start bit edge, then do busy wait until time of bit/2 to sample the value of the pin

  • Pro: simple ?
  • Con: very difficult to read from multiple ports, no time for other tasks, long latencies

This is the way NewSoftSerial / SoftSerial works on the Arduino.

The lower the baud speed, the higher the CPU charge !

Regular sample

Sample at regular interval (at least baud speed x 2 to respect Nyquist criteria)

  • Pro: can read any number of ports, quite simple (no need to manage a schedule list)
  • Con: may use a lot of CPU, risk of missing an edge

Trigger + clock

Triggered on the start bit edge, then sample at time of start + (time of bit / 2)

(Almost) Pure trigger

Every time you're triggered, compute the time since the last time and deduce the number and type of bits.

  • Problem with this approach: since the stop bit is 1, you may never get an edge at the end of the byte (if it's 0xff for example), so you need a watchdog whatever happen.

This is the way AltSoftSerial works on the Arduino (and Teensy...).