Direct Port I/O in NewSoftSerial 9

Posted on May 21st, 2009 at 10:52 pm by


NewSoftSerial 9 is now available.  This is a major new release that replaces all the performance path pin reads and writes with direct Port I/O.

What does that give you?  Well, a couple of things.  Most importantly, it makes NewSoftSerial more compatible with the latest Arduino release.  Changes to timings of digitalRead and digitalWrite in Arduino 0015 broke NewSoftSerial at higher baud rates.  All software serial libraries are timing sensitive, and even a microsecond or two change in a function’s timing can introduce enough error to fatally corrupt serial transmissions.  By using direct port I/O and retuning all the timing tables, we avoid problems caused by the longer digitalReads and digitalWrites.  Furthermore, the library should be more immune to future changes of this kind.

Direct port I/O is amazingly fast — about 10-25 times faster than the equivalent call to digitalRead/Write.  Because of this, NewSoftSerial 9 is able to operate reliably at higher baud rates than in the past.  In my test suite, I am now able to get reliable RX at speeds up to an unprecedented 57.6K baud on a 16MHz processor and 31.25K baud at 8MHz*.  Because the performance paths no longer use digitalRead/Write, they are less sluggish and much easier to tune to the higher speeds.

*These figures are about 70% higher than NewSoftSerial 8 with Arduino 0014.  These were measured using USB-powered Arduinos running a prerelease v. 0016 copy of the software, which optimizes the timer 0 overflow counter — an important contributor to software serial performance.  I/O was tightly controlled to avoid overflow, which can easily occur at high baud rates when you have no hardware flow control.

Please let me know what you think.  The new version is here.

What’s next for NewSoftSerial?  Well, there are a number of things being asked for:

  • Arduino Mega support
  • Signal inversion option
  • RTS/CTS flow control option
  • Configurability, i.e. “E, 7, 1″ style parity, data and stopbit configuration.

What are you most interested in?  Let me know here.