Firmware again, MIDI again
The Piggy’s firmware was updated to version 1.2. Download. It now features Decay time and Sustain level setting via MIDI. For the rest the ADSR quality checking has been improved with additional unit testing.
But speaking of MIDI.. one of the trickier parts in the firmware.. We did our own Software UART for the MIDI implementation. It just was the right thing to do. MIDI over USB was possible, but required fooling around with special drivers on every platform (Mac, Linux and Windows), and quite possibly also depending on the OS version. An unmanageable software thundercloud seemed to loom over our heads. So, the answer would have to reside with the Arduino itself. The Arduino’s hardware UART was already used by USB, and so the idea for a software MIDI UART was born.
The implementation wasn’t straightforward. And that’s typically the case where high speed interrupts (max 31,250 interrupts per second!) are involved. We chose to measure the time passed between rising and falling edges. Divide by 32 usec and you have the number of elapsed bits. For this to work, interrupt code has to be -fast-. And interrupt code is notoriously hard to debug. For that reason the interrupt only encodes runs: {bit-value, nr-of-bits}, {bit-value, nr-of-bits}, etc, etc. That’s the low-level layer. A layer on top of that, which is more complex, decodes the runs, which then gives us the real bits as they were communicated. Another layer on top of that strips away the start/stop bits and reverses bit order (crazy UART stuff). This is a very tidy way of doing things, and you wouldn’t expect it to work at such high interrupt frequencies, and with so little RAM (2k).
Software UART layers:
- interrupt: checking the time between signal edges and encoding runs
- decoding runs: we now have the bits as they were sent
- decoding bytes: strip off start/stop bits and reverse bit order to get the byte values
In the end, the communication took only a few percent CPU and manages to read all MIDI data correctly. It took a good couple of days, but it was worth it.
Recently, a friend asked. Hey.. software UART? Doesn’t that already exist? The blunder of dozens of wasted hours immediately crossed our minds. Checking what was already done on the Arduino:
NewSoftSerial library
This is actually already a follow-up of an original, which was said to make timers unstable and so forth. But still, we quote: “NewSoftSerial requires nearly 100% CPU time while transmitting or receiving data.”.
Ok, the stubborn folks at Tasty Chips apparently didn’t do too bad then.
With all respect towards the developers at Teensy, of course. They were brave enough to make a full-fledged UART including writing, not just reading. But for the Piggy, a dedicated, newly developed software UART was the best choice. 😉
Okay, how about plugging an Atari ST + Cubase, will that work then? 😉
It worked with Maxymiser’s MIDI messages, but that was on a Falcon. 😉 Maybe I’ll try Mastertracks on ST soon 😉