Sunday, September 22, 2013

Fix SPI commands being ignored when multiple are sent

My last code had a bug. When two commands were sent from the python script Like so:

  sendByte(ard, leftStop)
 sendByte(ard, rightStop)
The first command would cancel the second because the code on the atmega resets the sent command with readBytes[0] = 0. The SPI receive processing is asynchronous (well, I guess the SPI interrupt function is serial with respect to clocks), but you don't know when the function is run. So the first command is received and while the command processing in the main loop is running, the next command was read into readBytes[0] which is then overwritten.
The fix is to have a ring buffer which saves all the commands and allows the main loop to run asynchronously to the SPI interrupt function. The new blink.ino contains the fixed code.

No comments: