Pi data to Arduino via SPI
Posted: Sat Feb 10, 2018 2:57 am
Hey all. This has been mentioned a few times so I wanted to post a new thread for it.
The Pi does indeed connect directly to the Arduino via a SPI bus, and as far as I can tell, it works as expected.
We already have a SPI protocol implemented between the two devices that you can latch on to with your own code.
Here's the quick overview...
The Arduino has a SPI buffer length of 11 bytes (this is set in the Comms.h tab of Arduino). This allows for 1 "register address", 8 bytes usable for your data, 1 byte for a checksum (if you want to add it), and 1 trailing byte, which needs to be sent if you're reading the full response string as SPI clocks the outgoing bytes one behind the incoming bytes.
Look at the SPI_Handler function in the Comms tab, and see how we use it to control motors. The first byte sent in the string is intended to "key" the string to a certain behavior or handler on the other end.
We reserve the value 130 to control motors, and you shouldn't use 255 as it's a catch all needed to ignore occasional data errors from teh Pi.
On the Arduino look at SPI_BufferIn and SPI_BufferOut in the Comms tab, and look at all the SPI functions near the bottom of that tab. You should be able to add your own hooks in there for other stuff.
It's hard to process data and pack up the outgoing buffer fast enough to do on the fly, so you may want to send and query back any data in separate packets, so you have time to load up the outgoing buffer on the Arduino side before it's needed by the Pi.
To pack up a packet from the Pi to the Arduino, have a look at the motors function in the spirit_core.py.
Be careful to not flood the SPI bus with too much traffic - I think it'll work fine, but remember the Arduino needs a short time to deal with the incoming data. Maybe add some real short pauses (like maybe 100 to 500 microseconds?) between sequential SPI sends from the Pi. It may work faster, but if you're having dropped data, this may be the problem.
That's not super detailed but hopefully useful to someone.
-Kevin
The Pi does indeed connect directly to the Arduino via a SPI bus, and as far as I can tell, it works as expected.
We already have a SPI protocol implemented between the two devices that you can latch on to with your own code.
Here's the quick overview...
The Arduino has a SPI buffer length of 11 bytes (this is set in the Comms.h tab of Arduino). This allows for 1 "register address", 8 bytes usable for your data, 1 byte for a checksum (if you want to add it), and 1 trailing byte, which needs to be sent if you're reading the full response string as SPI clocks the outgoing bytes one behind the incoming bytes.
Look at the SPI_Handler function in the Comms tab, and see how we use it to control motors. The first byte sent in the string is intended to "key" the string to a certain behavior or handler on the other end.
We reserve the value 130 to control motors, and you shouldn't use 255 as it's a catch all needed to ignore occasional data errors from teh Pi.
On the Arduino look at SPI_BufferIn and SPI_BufferOut in the Comms tab, and look at all the SPI functions near the bottom of that tab. You should be able to add your own hooks in there for other stuff.
It's hard to process data and pack up the outgoing buffer fast enough to do on the fly, so you may want to send and query back any data in separate packets, so you have time to load up the outgoing buffer on the Arduino side before it's needed by the Pi.
To pack up a packet from the Pi to the Arduino, have a look at the motors function in the spirit_core.py.
Be careful to not flood the SPI bus with too much traffic - I think it'll work fine, but remember the Arduino needs a short time to deal with the incoming data. Maybe add some real short pauses (like maybe 100 to 500 microseconds?) between sequential SPI sends from the Pi. It may work faster, but if you're having dropped data, this may be the problem.
That's not super detailed but hopefully useful to someone.
-Kevin