Daisy Chaining multiple AutoDrivers

In a previous post I tested running SPI over a distance of roughly 100 feet using RS-485 over Cat5 with only a single SPI slave device, in this post I will cover the how to have multiple SPI slaves (AutoDriver with RS-485 shield) daisy chained together. The goal here is to communicate with multiple AutoDrivers over a long distance with only a single chip select signal.

The way daisy chaining works with multiple devices on a single SPI bus is similar to shift registers, data from the SPI master is shifted out MSB first and shifted into the SPI slave on the falling edge of the SPI clock. At the same time the data in the slave is shifted out to the next device, when there is only a single slave device this would go back to the MISO pin on the master or with multiple slave devices the MOSI pin on the next slave. The number of devices that can be daisy chained together is only limited by length of the SPI bus and delay caused by each of the SPI devices on the SPI bus (see my SPI over long distances post for more details).

When there are multiple devices on an SPI chain the way data is sent must be altered to correctly send and receive data. Even if you are sending or receiving only from one SPI slave the number of bytes transmitted must equal the number of SPI slaves on the bus. In my case there are going to be three AutoDrivers on the SPI bus so three bytes must be sent each time, one for each device. If you want to send to and receive a byte from the first device (AutoDriver #0) two byte of 0x00 (NOP) must be sent first and then the byte destined for the first device. The first two bytes sent shifts the data in the shift register of the first device (AutoDriver #0) to the third device (AutoDriver #2) and the two bytes of 0x00 into the first and second devices (AutoDriver #0 and #1). The third byte sent will shift the data from the first device (currently in AutoDriver #2) into the Arduino and the byte for the first device into the first device.

AutoDriver daisy chaining data flow

When communicating with only a single device at a time as mentioned above the Arduino must save and return only the byte that was returned from the SPI slave in question and ignore the bytes from the other devices. In the case of the above example, since the third byte transmitted is intended for the first device the third byte received is the data from the first device and the byte to be returned for processing. In general to send and receive a byte from a specific SPI slave device in a chain the position of the data should be number_of devices_in_chain – position_of_device_in_chain, where the position starts from 0. For a better understanding of how daisy chaining multiple device on a SPI bus works I would suggest looking over this Daisy-Chaining SPI Devices app note from Maxim Integrated.

To make life easy I modified the Arduino AutoDriver library to add index and count variable so that each instance of the AutoDriver library is assigned a unique index number and the count represents the total number of instances.

I also modified the SPIXfer function to use this information and send and receive data from the correct device in the chain.

With these modifications the library would now be able transmit and receive data from multiple devices in a SPI chain as long as the number of instances of the library matches the number of devices in the chain. Below is an example Arduino project to show how this would be accomplished.

After modifying the code I proceed to test the setup with two AutoDriver boards and their associated shields. This worked great, after spending two weeks troubleshooting a bad driver chip that made it appear that the first device in the chain was always returning 0xFF, and I then moved on to test three AutoDrivers in the SPI chain. This also worked without any hiccups and I called this part of the project a complete success!!! As always the code can be found on my fork of the AutoDriver library on GitHub. I also have a branch of the AutoDriver library that contains only the daisy chain code that can be used on a regular SPI bus.

One final note, the current implementation only communicates with one device at a time, so if you need to send a command to the devices on the SPI bus an individual command must be sent to each device. It is possible to combine these commands so that all can be sent at the same time but I am saving that modification for a later date as it would require substantial modification of the AutoDriver library.

Share

One thought on “Daisy Chaining multiple AutoDrivers

  1. Wow. Just…wow. That’s an amazing thing. I was looking for answers like this. In my own project, I only need to go four feet, but even that is too much for SPI. Thank you so much for leading the way on this!

Leave a Reply

Your email address will not be published. Required fields are marked *