9 years, 3 months ago.

Using the library with other devices attached on the SPI bus

Hi, You have done very nice job with this library. I have a question or maybe a suggestion for this library. Is it possible to optimize a little the library with passing an already created object of SPI? In this way can be done the communication with several devices attached to the same SPI bus. Suggestions for the library code:

/* Member variables */

SPI * m_Spi;

/* class definition */

SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, PinName cd, SwitchType cdtype, int hz = 1000000);

SDFileSystem(SPI * spi, PinName cs, const char* name, PinName cd, SwitchType cdtype, int hz = 1000000);

/* initialization */

SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, PinName cd, SwitchType cdtype, int hz) : FATFileSystem(name), m_Cs(cs, 1), m_Cd(cd), m_FREQ(hz) {

...

/* Configure the SPI bus */

this -> m_Spi = new SPI(mosi, miso, sclk);

this -> m_Spi -> format(8, 0);

...

}

SDFileSystem::SDFileSystem(SPI * spi, const char* name, PinName cd, SwitchType cdtype, int hz) : FATFileSystem(name), m_Cs(cs, 1), m_Cd(cd), m_FREQ(hz) {

...

/* Configure the SPI bus */

this -> m_Spi = spi;

this -> m_Spi -> format(8, 0);

...

}

Question relating to:

A re-written SDFileSystem library with improved compatibility, CRC support, and card removal/replacement support. fatfilesystem, MMC, SD, SD Card, SDHC, SDXC

1 Answer

9 years, 3 months ago.

There is no reason to give an external SPI object for this use case, and actually it has significant downsides. You can just make seperate SPI objects on one SPI bus. The advantage of this is that each object has its own format and speed: If you have a shared object then if SD file system changes the speed to 20MHz, also it will be changed for your acceleration sensor for example. It is generally better to use a seperate SPI object for each library.

Accepted Answer