1

Is it possible to test a SPI connection by wiring the MISO directly to the MOSI pin? I'm working on a low-level SPI implementation, and I want to check that the library is basically working. It would be nice to be able to test the connection itself without additional hardware.

  • If you short MISO to MOSI, make sure there are no other devices connected to the bus. – Edgar Bonet Jul 8 at 7:37
  • Do you have an oscilloscope? This can be used to check whether SPI is working. – MichaelT Jul 8 at 7:45
  • @EdgarBonet There are no other devices. – Jonah Jul 9 at 4:39
  • @MichaelT I don't unfortunately, might need to get one at some point. – Jonah Jul 9 at 4:39
  • For posterity, the reason my test wasn't working is because the MOSI pin was not set to output mode. – Jonah Jul 11 at 18:51
2

Yes, you can - and it's something I often do when doing low-level electrical testing an SPI interfacce. For every clock that you send out the data presented on MOSI will get reflected on MISO. So if you were to (using the Arduino SPI library) do:

uint8_t x = SPI.transfer(0x38);

then x should equal 0x38.

I find this especially useful on systems with remappable pins to confirm that the pins are configured correctly in the board configuration for proper SPI operation.

  • Great, thanks for confirming. Based on reading the timing diagram it seemed like it should, but it's not for me. Now I can know with more certainty that the problem must lie with my code, not with the concept. – Jonah Jul 9 at 4:43
2

Usually the SPI Master clocks data out while the Slave clocks data back in. So if you were sending and receiving 8 bits per transaction you can clock out 8 bits and at the same time clock in 8 bits for a total of 8 clocks. This is described in this Wikipedia paragraph. That said, often the first 8 clocks are used to transmit a command and an additional 8 clocks are used to retrieve the result.

If you looped MOSI to MISO you would clock in what you clocked out. You would complete the entire transaction in 8 clocks. At a low level I'm thinking this does not matter. But at a higher level the software would have to be re-written to accommodate this weirdness.

  • Cool. It won't matter for the higher level programming, I only care about running a very simple test to check that my library works (it currently doesn't, lol). – Jonah Jul 9 at 4:43
  • 1
    I find that what you say is a good approach. That is, beyond the basics of sending / receiving bits of data, these simple serial interfaces such as SPI are often modified. Some use 8 bit transactions. Others use 12 or even 16 bits. Some ignore the received data during transmission of a command. Others clock in generic information unrelated to the command just to not let the bandwidth go to waste. – st2000 Jul 9 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.