Reading from SPI?

I’ve been trying to read some data from SPI, but the SPI on the core only seems to be clocking through the first three bits, instead of a full byte. I’ve tried different SPI modes and clock dividers, but I can’t seem to get it to read a full byte. Am I doing something wrong here?

void setup() {
  SPI.setClockDivider(SPI_CLOCK_DIV64);
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);
  SPI.begin();

  pinMode(D7, OUTPUT);
  digitalWrite(D7, HIGH);
}

void loop() {
    digitalWrite(D7, LOW);
    
    unsigned char b = SPI.transfer(0xFF);
   
    digitalWrite(D7, HIGH);    
    delay(100);
}

Hi @mattdot, Is this all of the activity you ever see on SCK? What is ENABLE hooked to, because your code doesn’t attempt to control the SS (latch) pin A2.

SPI Source here:
https://github.com/spark/core-firmware/blob/master/src/spark_wiring_spi.cpp

1 Like

I was using D7 to enable the slave and not SS. Changing to using SS seems to have fixed the issue. While I see that I should be using SS, I still don’t understand why not controlling SS would change the clock to only 3 cycles. Thanks for your help!

Hmm, I should have guessed that you were using D7 :wink:

Honestly I don’t see any reason it shouldn’t work with D7. The SPI routine does set SS to an output and set it HIGH, but doesn’t do anything else with it from what I could tell.

It really shouldn’t matter, but maybe try D6 just in case the LED is messing things up. I doubt it though.

Do you expect 0x03 on MISO? Looks kind of strange. I think your Logic Analyzer is decoding it wrong, as MODE0 should be clocking on the rising edge, so should be 0x01.