I found out that same code produces different results, being placed in different places:
Code: Select all
static uint8_t Buf[2048];
<...>
Rslt = sdcRead(&SDCD1, 0, Buf, 1);
Uart.Printf("%A\r", Buf, 512, ' '); // Prints first 512 bytes of Buf's contents, definitely working
For clarity, I removed obvious things like checking if reading succeded.
To check all this, I placed numbers 00, 01, 02, ..., FF in zero sector of SD card (twice, i.e. ...FE, FF, 00, 01, 02...). I did it with my laptop. Laptop reads this correctly (and Windows says disk is not formatted, hehe).
Back to code.
When placed in main.cpp, it reads bytes correctly: 00, 01, 02 etc.
When placed in other file (MassStorage.cpp in my case), it reads 02 03 04...FE FF 00 00 (shifted left by two bytes). When repeated at same place with other buffer, it reads 01 02 03...FE FF 00 (shifted left by one byte).
It works OK if modified next way:
Code: Select all
static uint32_t Buf[2048]; // I did not bothered to set other size
<...>
Rslt = sdcRead(&SDCD1, 0, (uint8_t*)Buf, 1);
Uart.Printf("%A\r", Buf, 512, ' ');
As far as I can see, correctness depends on aligment of byte buffer in memory. Time after time (more precisely, place after place) it is OK because aligment is correct by chance.
It seems that such a requirement is a result of advanced DMA usage, which writes memory in aligned way.
1) Is there a way to align buffer explicitely? I would like not to cast type here and there.
2) I checked buffers declaration in several places like FatFS etc, there is no explicit aligment. This means that I have working file system just being lucky.