Should SPI and SD libraries cohabit ok?

I'm trying to combine access to both an SD card through the SD library, and a Digi-ole colour OLED via the SPI library - first time trying to use either

The s/w supplied with the OLED used a s/w SPI implementation, which worked, but was very slow when drawing bitmaps so I was trying to convert it to hardware SPI though the standard lib.

Where I stand now that my converted code will drive the OLED ok, and I can read the SD ok, but I can't do both - I can initialise the display and write to it up to the point I call SD.begin() at which point the display no longer responds to further commands.

Before I go into finer detail, I just wanted to check whether this is theoretically feasible or whether there is something in the SD library that prevents the simultaneous use of the SPI library?

Cheers

[quote author=Marvin Martian link=topic=272811.msg1922525#msg1922525 date=1413402587] is theoretically feasible

[/quote]

It is not just theoretically feasible, it is thoroughly practical, and people do it ll the time.

Nick is right, but there are things you should check. There are settings for the SPI bus that may be different. Bit order, bus speed, and data mode are the things that will affect cohabitation. http://arduino.cc/en/Reference/SPISetBitOrder http://arduino.cc/en/Reference/SPISetClockDivider http://arduino.cc/en/Reference/SPISetDataMode

The SD card is MSBFIRST, SPI_CLOCK_DIV4 (I think), and MODE0. If your OLED doesn't use those settings, you may have to change those settings "on the fly" when you want to use your OLED, then change them back when you want to use the SD card.

Thanks Tim & Nick - good to know it's worth persevering.

Also - yes - I'd forgotten I had to set MODE1 to get the OLED to work!

Having a quick fiddle over breakfast I managed to get the OLED working and the SD initialised - but still failed opening any of the files on the SD. Could still be down to all the speculative changes I was making so will roll everything back and start again tonight.

Thanks for the help!

You must change the data mode when using the two together. I try to leave the SPI bus in default settings (MSBFIRST and SPI_MODE0), and change them to the different settings, then use that device, and change them back when done.

I would use something like this using the SD (mode 0) and the OLED (mode 1). Change to mode 1 when using the OLED, then change back to mode 0.

SPI.setDataMode(SPI_MODE1);
// do your OLED interface stuff here, then change back to mode 0
SPI.setDataMode(SPI_MODE0);

Not a rule, just my way of keeping things orderly.

edit: Insure when you change the mode or bit order, you have all SPI slave selects HIGH (disabled). Change them before enabling the SPI slave select for the device or it causes problems with the device.

That was it! I wrapped the OLED commands in setDataModes and everything worked!

Many thanks!