Best way to raw read/write SD card with SdFat library?

I want to be able to use the full capacity of my SD card by reading/writing data directly from/to it, not like in any file. It doesn't matter the filesystem will be erased, I could just open the card in a hex editor. So far I've looked at the RawWrite sketch part of SdFat but it's too complex. There have been other questions concerning this on the forum but they don't have good answers.

Is there any good way to do this?

You don't need the library. Just issue SPI commands to the SD card to perform block reads and writes.

An Arduino library SPIFlash examples may be a good start point:

I'm not an expert Arduino programmer and I don't know how you would do this. That's what the library is for. As I had said the SdFat can do raw writes just fine.
Don't assume I'm an expert lol

I know that library is meant for SPI flash chips and not really SD cards. How would that work? :thinking:

The principle the same

Still don't understand

If you already know it, what is your question?

I mentioned in the first post I wanted to use SdFat specifically to perform raw reads/writes. How would I do that?

Also because the RawWrite example is rather complicated.

Do you think that someone will offer you a way to do complex things easily and simply?

Actually I meant like the best way to do it but without all the clutter that the RawWrite example has.

Good luck

:person_shrugging:
I'm just looking for code

It's not in the tradition of the forum - to respond with ready-made code. We can suggest how to write it or what to read. But you write the code yourself.

What you want to do requires considerable expertise.

If you don't want to learn how to do it yourself, post on the Jobs and Paid Collaboration forum section and hire an expert.

I don't see any example sketch named RawWrite.

Looks like you need to create an instance of a "DedicatedSpiCard" and then call the readSectors() and writeSectors() functions. Maybe a search for "DedictedSpiCard" will show an example.

Here is the RawWrite example sketch for the SDFat library: SdFat/RawWrite.ino at master · greiman/SdFat · GitHub

The code is very short and straightforward. Create a large file named RawWrite.txt of so many contiguous blocks, write the blocks out one by one, then close it.

This operates within the framework of the SDFat file system, so it does not use "the full capacity of my SD card", which for some unspecified reason is required by the OP.

2 Likes

This thread had me wondering! The SD card has a controller that does page wear-leveling and presents us with a simulation of a file. Even if I could, why would I reach in and screw with that?

With magnetic disks we might arrange to sort and rewrite a file but with limited writes and large space we write a sorted file or much better is to write a smaller file of sorted offsets into the original data, it works so well that it's faster and smaller on magnetic drives too. You should never have to overwrite SD, that's get a bigger card time.

I wouldn't, but for the fastest possible data logging it might make sense, in some situations.

1 Like

You might look at the Low Latency Logger example in the SdFat library. It creates files, but then writes directly to the sectors.