Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: SDFat with multiple SPI devices (Shared SPI Bus)

  1. #1

    SDFat with multiple SPI devices (Shared SPI Bus)

    Hello all,

    This is more of a "tip" to anyone using SDFat ( with their Teensy 3.x devices. Specifically, this tip applies to anyone using SDFat and other devices on the same SPI bus.

    In the project I'm working on, I use SDFat for data-logging to an SD card and the same SPI bus to communicate with some MCP2515 CAN controllers. The baud rates are different, but the Mode (referencing the "4-modes" of SPI) are the same, both Mode 0. My problem was that I could get SD logging to work or the MCP2515 chips working, but not both at the same time. Ultimately, if SD logging was occurring, the MCP2515 SPI data would appear to be corrupted and I couldn't read data from the chips properly. Looking at the data on a scope revealed that the data was being sent to the chips fine and the chips were responding with the expected data, but my SPI routines wouldn't receive the same data.

    As it turns out, I needed to clear the RX FIFO buffer in the Teensy before sending SPI data to/from the MCP2515's. I didn't find a specific function to do this in the SPI.h files, so I made a simple function to do this.

    Here is an example:

    void SpiFlushRxFifo(void)
    	uint32_t u32_SPI0_MCR_copy;
    	/* Flush the RX FIFO */
    	u32_SPI0_MCR_copy = SPI0_MCR;	// Grab a copy of the current register value
    ...Using the flushing function:

    	digitalWrite(CAN2_CS,LOW);		// pull down CS
    	can2.transfer(MCP2515_READ);	// READ command
    	can2.transfer(CANSTAT);		// Start address
    	retVal = (can2.transfer(DUMMY) >> 1u) & 0x07u;
    	digitalWrite(CAN2_CS,HIGH);		// Release the CS
    I hope this helps someone else that's also dealing with similar strangeness while sharing the SPI bus with SDFat.

  2. #2
    Junior Member
    Join Date
    Aug 2015
    @afterhoursengineering - I just wanted to reply and thank you for posting this! Your function worked great for me and got me up and running. In my prior work around I wound up exchanging data with my other SPI device using digitalWrite/digitalRead. Thanks again!

  3. #3
    @codixtech, glad it helped. Here's a link to the project I'm working on. The complete source code is available if you needed further reference.

  4. #4
    Junior Member
    Join Date
    Oct 2015
    Thanks for your code, afterhours! It fixed an issue I've been frustrated with for about a week.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Oh, that looks like SdFat might have a bug in its FIFO handling.

  6. #6
    Junior Member
    Join Date
    Feb 2018

    Thank you

    I just registered on this forum so that I could THANK YOU for taking the time to post that. You have saved me countless hours of frustration.

  7. #7
    Senior Member
    Join Date
    Dec 2016
    Montreal, Canada
    the 3 last 1 post posters comments sound fishy, suspicious...

  8. #8
    Junior Member
    Join Date
    Sep 2019
    Thank you very much for sharing. I am having the same problem. I am using Teensy 3.2+SDFAT+MCP2515(Seeed Library). I can get them working alone but not togother.
    I would like to use your function but i don not know which file i need to change. It would be great if someone can help me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts