Switching off SPI begin/endTransaction for high-speed SPI transfer?

Status
Not open for further replies.

WMXZ

Well-known member
I realized that transfer speed to microSD SPI cards could be improved by switching off begin/endTransactions.
I understand the use of transactions allows flexibility for using different SPI devices in the same Bus.
But if I need fasted speed (e.g. data logging) then every little bit counts.

Sure there is an option within SdFat to interface with custom SPI driver, but it would be nice if this would be a standard feature.

At the moment I do the following
Code:
class MySpiClass : public SdSpiBaseClass {
    public:
        // Initialize the SPI bus.
        void begin(SdSpiConfig config) {  (void) config;   SPI.begin(); }

        // Activate SPI hardware with correct speed and mode.
        void activate() { if(doTransactions) SPI.beginTransaction(m_spiSettings); }
        // Deactivate SPI hardware.
        void deactivate() { if(doTransactions) SPI.endTransaction(); }

        // Receive a byte.
        uint8_t receive() { return SPI.transfer(0XFF); }
        // Send a byte.
        void send(uint8_t data) { SPI.transfer(data); }

        // Receive multiple bytes.  
        // Replace this function if your board has multiple byte receive.
        uint8_t receive(uint8_t* buf, size_t count) { memset(buf, 0XFF, count); SPI.transfer(buf, count); return 0; }
        // Send multiple bytes.
        // Replace this function if your board has multiple byte send.
        void send(const uint8_t* buf, size_t count) {  SPI.transfer((void *)buf, (void *) dummy_buffer, count); }

        // Save SPISettings for new max SCK frequency
        void setSckSpeed(uint32_t maxSck) {  m_spiSettings = SPISettings(maxSck, MSBFIRST, SPI_MODE0); }

    private:
        SPISettings m_spiSettings;

    public:
        bool doTransactions=true;

} mySpi;
and call
Code:
SD.sdfs.begin(SdSpiConfig(cs, SHARED_SPI, SD_SCK_MHZ(33), &mySpi))
( I need SHARED_SPI and cannot use DEDICATED_SPI, as I still have multiple SPI devices on the Bus, but I'm using them sequentially)
and in SdFatConfig.h I set
Code:
   #define SPI_DRIVER_SELECT 3
and if needed I switch use of transaction by
Code:
mySpi.doTransaction = true;
....
mySpi.doTransaction = false;
 
Status
Not open for further replies.
Back
Top