SD card corruptions

flok

Active member
Hi,

I think I'm doing something wrong but I can't figure out what. The situation is that if I write a block of data to the SD-card and then read it back again, then parts of the written data are missing (some parts are then all 0x00) or it is all totally different.
Source code: (zip)

Code:
Sleep
Go!
Init SD-card backend...
Init SD-card succeeded
2024-02-13 10:09  30064771072 test.dat
Virtual disk size: 28672MB
begin: 1
Write to block 0, 1 blocks
write to offset 0
2d cf 46 29 04 b4 78 d8 68 a7 ff 3f 2b f1 fc d9 7a 96 09 2c a5 57 74 64 c4 af 15 28 a4 e9 57 db 5e 20 fb 38 a8 4e a6 14 93 25 56 24 44 df 59 8d 43 7b be
...
21 cd 0d d9 46 71 b1 9a 72 2a 25 11 8c 81 1a d4 2d 34 aa 68 ac 76 72 81 87 8b 2b 5a 2f 26 be de ec bf 38 ad b0 df a3 0d 29 c8 6f df 78 06 b8 84 0a 72 a2
 2a 4c ae 77 c7
WRITE: 1
Read from block 0, 1 blocks
read from offset 0
32 54 2d 5a 54 32 37 56 37 4b 2d 54 4f 4a 4f 4d 55 35 2d 47 42 47 42 5a 43 4c 2d 53 50 41 36 4d 42 52 2d 4f 4f 4d 44 4d 51 48 26 6e 65 74 77 6f 72 6b 54
...
4c 36 dd 83 d9 10 d5 9c fc 7f 3a f0 7e 0e 82 e3 10 00 99 84 3f 59 db f8 1a 67 2d a6 7e bc 1f 18 be a2 aa 2c ef 33 aa 8d 7d 43 23 9a 48 53 b7 f0 18 bc bf
 9d ad 88 4d f6
READ: 1
equal: 5

"WRITE 1" and "READ 1" say that the sd class says that write/read succeeded.
Yet the contents is different ("equal" should say 0).

Anyone an idea?
 
If you use new or malloc to allocate the sector buffers they'll be in DMAMEM (aka RAM2) which is cached. The SD library probably doesn't handle flushing/invalidating the memory before it writes/reads it.
 
Hi JMarsh!

Something like?

Code:
                arm_dcache_delete(data, n_bytes_to_read);
                size_t bytes_read = file.read(data, n_bytes_to_read);
                rc = bytes_read == n_bytes_to_read;
                if (rc) {
                        arm_dcache_flush(data, n_bytes_to_read);
                        break;
                }

I also tried the other way around but neither of them resolves the problem.

But a caching problem sounds likely to me because that would explain the partial garbages.
 
Last edited:
Did you add flush before writing too?
Have you checked the contents of the file on a different device, to see what is actually being written?
 
Did you add flush before writing too?
Have you checked the contents of the file on a different device, to see what is actually being written?

Oh indeed. I now do not get an error.
It's not clear to me though which one to use when arm_dcache_delete or arm_dcache_flush.
For write I do now both before the file.write() call.

I verified the contents of the sd card on my pc but the data is not what I expected it to be.

Shouldn't the sdfat library take care of cache invalidating and such?
 
You'd want to flush before writing (push data out of the CPU cache into the memory) and delete before reading (discard whatever is in the cache, to ensure the memory is read).
If it was up to me I would say yes, the SD library should be taking care of this but the cache calls use up a little bit of CPU time which is just wasted when uncacheable memory (DTCM) is used, and that's the majority of use cases.
 
True.

Ok it now works. On the Teensy4.1 and when verified on the pc.
Thanks for your help.
 
Back
Top