Multiple hardware SPI

I tried to find information about this in the forum and elsewhere, but I seem to be coming up blank. I’m trying to run a MAX31855 thermocouple chip and an SD card over the SPI interface, and I’m trying to figure out if the hardware SPI interface can support multiple devices with separate chip select pins. Does anyone have any info on this? I’ve gotten the program running using the ADAfruit library and the SD card libraries ported by Technobly, but I’m using the bit-banging interface, and I’d like to speed it up some. Thanks!

It is indeed possible. Your libraries for each device will control the chip select pin you define and it should work well. :wink:

1 Like

@mumblepins, from what I see, you can run the MAX31855 in SPI mode 0 at a clock frequency of 5MHz max (which is about DIV8 on the Spark). The SD library has already been tested with hardware SPI and it also runs in SPI mode 0. That means that you should be able to do everything with hardware SPI. :smile:

1 Like

Yay! Thanks for the help @peekay123 and @kennethlimcp. I managed to get it working with both chips in hardware mode.

If anyone in the future is interested, I forked @BDub’s MAX31855 code (at https://github.com/mumblepins/SparkCore-MAX31855), adding hardware SPI support. I found that it’s important to make sure to set all chipSelect pins high before initializing any of the libraries, or bad things happen.

I also found that I had to have a function in between calls to the SD card and the thermocouple chip, or I’d get weird readings off of the thermocouple. I think the SD card keeps communicating after the library thinks it’s done with it, but didn’t feel like tracking down the problem in depth, so I just came up with this:

void switchChips(void) {
    // not sure why this is necessary, but the temp sensor returns a wrong value on the first read without it.
    // deselect both chips, "receive" one byte and throw it away
    digitalWrite(SD_CS, HIGH);
    digitalWrite(THERMO_CS, HIGH);
    SPI.transfer(0xFF);
}

I also forced the SD card to SPI_half_speed, just in case (and I don’t need full speed anyways, I’m just using it for logging every 5 seconds or so)

Anyways, thanks everyone!

2 Likes