Aligned buffer for SDIO

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
User avatar
kreyl
Posts: 59
Joined: Sun Jan 13, 2013 11:46 pm
Been thanked: 4 times

Aligned buffer for SDIO

Post by kreyl »

My configuration: stm32f205VE, uSD card connected by SDIO; gcc compiler, chibios v.2.6.0.
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.
User avatar
kreyl
Posts: 59
Joined: Sun Jan 13, 2013 11:46 pm
Been thanked: 4 times

Re: Aligned buffer for SDIO

Post by kreyl »

Aligment can be done by declaring buffer next way:

Code: Select all

    union {
        uint32_t Dummy1[512];
        uint8_t Buf1[2048];
    };

It works, though I cannot say I like it.
Last edited by kreyl on Sun Oct 13, 2013 7:23 am, edited 1 time in total.
User avatar
kreyl
Posts: 59
Joined: Sun Jan 13, 2013 11:46 pm
Been thanked: 4 times

Yet another way to align array in boundaries

Post by kreyl »

Code: Select all

struct { uint8_t Buf1[MS_DATABUF_SZ]; } __attribute__((aligned(MS_DATABUF_SZ)));
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Aligned buffer for SDIO

Post by Giovanni »

Hi,

The buffer is required to be aligned unless the support for unaligned buffers is enabled, there is an option for that. Note that operations on unaligned buffers are MUCH slower because the data is first read in an internal buffer then copied in the final buffer.

Giovanni
User avatar
kreyl
Posts: 59
Joined: Sun Jan 13, 2013 11:46 pm
Been thanked: 4 times

Re: Aligned buffer for SDIO

Post by kreyl »

Yes, I have analyzed both options (I have read the code of SDIO).
Of course, I want aligned access.
The question is, how can I declare that whole array must be aligned to 4-byte boundaries?

Code: Select all

uint8_t Buf[2048] __attribute__((aligned(2048))); 

will make me an array of 2048 elements, each 2048 bytes in size.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Aligned buffer for SDIO

Post by Giovanni »

You could make the buffer an array of uint32_t.

Giovanni
colin
Posts: 149
Joined: Thu Dec 22, 2011 7:44 pm

Re: Aligned buffer for SDIO

Post by colin »

Giovanni wrote:You could make the buffer an array of uint32_t.

Giovanni


So it needs only to be aligned on 32-bit word boundary, not on 2048-byte boundary? That is much nicer if so ... the previous code snippets in the thread seemed to imply the buffer needed to be aligned to start on a 2048-byte boundary...
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Aligned buffer for SDIO

Post by Giovanni »

Absolutely not, just 32bits boundary required by the DMA.

Giovanni
Post Reply