SD card read/write with Arduino

Thanks :slight_smile:

This procedure would likely be the same with whichever library you use. The SPI protocol for writing to the card goes like this:

  1. Send 'write sector command' - 0x58 a b c d 0x00
  2. Await command acceptance - ie you receive 0x00
  3. Send data token - 0xFE
  4. Send 512 bytes
  5. Send dummy checksum - 0x00 0x00
  6. Await 'done' status code - ie you receive 0x00

Mostly these 6 steps are all wrapped up in a function. What I suggest is that 1-3 are split out into one function, 4 in another, and 5-6 in yet another.

StartWrite() - does 1-3
WriteByte - actually would be the existing 'xfer byte' routine
EndWrite() - does 5 & 6

Start the write with steps 1,2,3.

Then write bytes at a time (step 4) until you've sent 512 of the blighters.

Do 5 & 6. If you want to continue, start again at the next sector with steps 1-3.

Let me know how you get on!