Arduino SPI Mode

I have been using a lot of SPI devices lately. Specifically working with SD card and the ADS1247.

SPI Library Gotchas

  • http://arduino.cc/en/Reference/SPISetDataMode used to say 0,1,2,3 - but it has now been fixed to say SPI_MODE0, SPI_MODE1, SPI_MODE2, or SPI_MODE3 - thanks team.
  • Must set standard CS pin to output, even if not used (e.g. 10 or 53)

SPI Mode Issues

Working with the Ethernet chip, the SD Cards - they all use SPI Mode 0. This means you only need to use chip select to set the library.

Now start working with devices of different modes. Each may even use interrupts. You MUST set the mode (and any other settings, like Clock Speed etc) each time before chip select.

  • Chip select Low
    • Set Mode, Clock, MSB/LSB etc

ISSUES !

  • Libraries using SPI, do not do this ! E.g. SD Card library does not set mode etc - they should
    • Only real solution is to update the libraries where they use chipselect to set the mode, timing, MSB, LSB etc.
  • Interrupts during this change could cause issues
    • You set Mode
    • Interrupt takes over, sets mode, returns
    • You set Chip select - but your mode is now wrong
    • Only real solution is to disable interrupts during this call. This makes more sense anyway and has solved other bugs.

I don't have resolution to these two issues yet. The first is just a matter of having custom versions. But that can be tricky - although I am going to try inheritance first. The second problem is more difficult.

http://this8bitlife.com/this-8-bit-life/2012/4/16/this-homemade-headphone-amplifier-is-a-work-of-art.html

http://paulgraham.com/todo.html

http://dylanized.com/working-to-code

http://info.timkellogg.me/blog/2012/04/22/why-open-source-is-worth-your-time/

VIM worth learning = Move paragraph = { } Find word marked = *