11 years, 4 months ago.

How can I share SPI pins for different devices (e.g. SD-Card and OLED-display)

Hello,

for my project I am planning to run an SD Card and another device (e.g. OLED display) using the same SPI pins (except CS). Unfortunately, all libraries have their own SPI object. I can not simply define SPI once and assign to different devices, looking like this (Pseudocode):

//First define SPI device
SPI spi1(p5,p6,p7);


//then assign SPI object to the first device
SDFileSystem sd(spi1, p8);

//then assign the same SPI object to the second device
OLEDDisplay OLED(spi1, p15);

Unfortunately, I cannot use the other SPI port, since it will take over different functions.

I am new to c/c++, how would this be done (Handle, where to put asterisk...?)?

Or can different classes define their own SPI devices using the same pins?

Pseudocode:

// Assign first SPI handler, e.g. for SD card
SPI SDCardSPI(p5,p6,p7);


//then assign second SPI object to the same port
SPI OLEDSPI(p5,p6,p7);

Maybe someone knows an answer? Thank you in advance!

OLEDDisplay OLED(spi1, p15);

can you please share your full code thank you

posted by jayendra mishra 20 May 2016

3 Answers

11 years, 4 months ago.

Simply your second one, you can define pretty much as many SPI objects as you want on a single port. An advantage of that is that you can for example easily set one of them at a different frequency than the other one.

Only don't use that SPI inside an interrupt, that will give problems if it can interrupt another function that uses the same SPI.

Accepted Answer
11 years, 4 months ago.

You may find my answer to this question useful.

Is it possible to connect 3 SPI devices to the same mbed at once?

11 years, 3 months ago.

Thank you very much Erik and Stephen! mbed really makes prototype development easy and fun!