OK, I’m trying to play with some embedded systems once in a while. This time around I was trying to implement a relatively fast (1000000Baud) asynchronous serial connection with an Arduino Micro. The Micro is running at 16MHz, so that connection gets close to what should be possible. I implemented my protocol handler and it seemed to do the right thing for some time, but stop dead at some random point in time:-( I verified that a couple of times on the other side of the connection (a Robotis CM-900) via an simple loop sending a READ command and blinking a LED when it received a valid response from the Micro.

So how do you find out what the Micro is doing when it falls of the rails? A normal serial protocol (over USB or otherwise) would change all the timing and would add more interrupts to the mix. That’s when I got the idea of using some digital output pins and a (cheap) logic analyzer to protocol what was going on. I don’t know, if this is a common technique in the embedded world, but I’m normally working with more powerful systems where I don’t have to look at logic levels;-)

I added some (in this case four) digital output pins to the Micro sketch and created a little macro to “pull a certain pin down, wait 4us, bring the pin up & wait another 4us”. I used the 4us timer because that’s officially the smallest interval the Micro can handle. It seemed to have worked OK for my case, but can easily be changed. I added that macro to some potentially interesting places in the code so that I could distinguish them later. Overall that method did not add much time during the run and more importantly, it did not add any additional interrupts.

FirstIncompleteResponse2LastCompleteResponse2The first screen shot shows a complete cycle. The CM-900 sends the READ request and the Micro responds and finishes the functions as it is supposed to.  Click on the thumbnail to see the larger version with some explanations on the different markers.

The second screen shot shows the markers for the time when things fall apart. The marker `before the HardwareSerial.flush()’ is the last recorded marker from the Micro. So, the HardwareSerial.flush() did not return. That brought me onto the right track;-) With the help of two nice people on #arduino @ freenode IRC, I got pointed to a patch on github, where somebody else was running into the same flush loop. I tried the proposed patch and the setup works fine now;-) Even after extensive testing (something like 2500 rounds), I did not see the hang again. Before it happened usually somewhere between 30 and 100 rounds.

IMG_20131016_191523_1000Now I can actually go back to my initial project for which I need that protocol stack, extra intelligent sensors for my little robot. Right now, it has a lot of extra stuff on it’s back and can’t walk:-(

As always, have fun exploring;-)