I am interfacing with an SD card and a CAN interface chip (the MCP2515) on an SPI interface. For speed of writing, I am using a FAT library and opening a file to stream to. If I temporarily stop streaming data to the SD card to gather data from the CAN chip, resuming the card writes causes corruption of the file.

I cannot see why the two interact with each other. If at the lowest level there is no overlap in pulling the CS pins low for each device, surely the SD card would effectively be blind to what was happening on the SPI bus. I have confirmed releasing the CS pin of the SD card is not causing any issue, nor pulling the SC pin of the CAN interface chip. It is only with an SPI transfer that the SD card writing becomes corrupted.

Is this a problem of electronics, or is this more likely a firmware/library issue that I am facing?

Further information:

  • Microprocessor: ATmega2560
  • CAN Interface: MCP2515
  • SD Card: Transcend 4GB C10 MicroSD HC
  • SD Card level shifter: 74hc4050

The writes are done in complete 512 byte chunks, aligned with the blocks on the SD card. The symptom of failure is that the next block written returns a failure to write. I have not been able to decipher the meaning of the fault yet, though two codes are returned for each failure. These may be generated by the SD card itself bud I do not know yet:

  1. 0x13
  2. 0xFF, 0xE0, 0x7F or 0x3F

Once the SD write completes, its CS pin is pulled high before the CAN CS pin goes low. Once the CAN operations are complete and the CS pins are swapped. Though this is when the fault occurs.

  • \$\begingroup\$ You should describe your setup in more detail. \$\endgroup\$
    – Eugene Sh.
    Commented Jun 27, 2017 at 19:48
  • \$\begingroup\$ Each SPI command must complete properly, with accordance to the specification of the SPI bus and involved device specification. What you mean I temporarily stop streaming data? And how you resume - at which level, with which command? And you can not just interrupt SD-card transfer, it will (most probably) require whole sector data to write, if you stop in between most probably command will get cancelled. Try analyzing how the target file is being corrupt - has more data? Has missing data? Has garbage instead of good data? \$\endgroup\$
    – Anonymous
    Commented Jun 27, 2017 at 19:55
  • 1
    \$\begingroup\$ How do you know that SD write completes, and what you do just after it? How do you start SD-card write request (just after you set its CS low)? The best if you share timing diagram of CAN/SD-card transfers so that we can see the alternating between devices and their timing. I think I can give answer now, but let's make another troubleshooting iteration. \$\endgroup\$
    – Anonymous
    Commented Jun 27, 2017 at 20:32
  • \$\begingroup\$ I will need to dig into the FAT library to establish whether the write genuinely does finish synchronously, will get back to you with what I learn. \$\endgroup\$
    – J Collins
    Commented Jun 27, 2017 at 21:25

1 Answer 1


There are some anomalies with SD cards releasing data out following de-assertion of chip select as they they require an SPI clock edge.

I suggest you read these notes from this very informative and interesting site Electronic Lives Manufacturing

SD card releasing DO

  • 2
    \$\begingroup\$ I have attempted to use the send byte method after lifting the CS pin, however it didn't have an effect on the issue. I tried several consecutively but still no joy. \$\endgroup\$
    – J Collins
    Commented Jun 29, 2017 at 7:25
  • 1
    \$\begingroup\$ It seems to me that FatFS by ChaN correctly addresses this issue. I had no problem with that library, sharing the same bus with a display. \$\endgroup\$
    – next-hack
    Commented Oct 5, 2017 at 11:49
  • 1
    \$\begingroup\$ I have also used Chan's FatFS with no problems as well where I shared the SPI bus with an MP3 decoder. \$\endgroup\$ Commented Oct 5, 2017 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.