-->
Page 1 of 2

How I do to align bits in transfer uint8_t array in SPI?

PostPosted: Thu Dec 08, 2016 9:29 am
by Leandro Zimmer
Hi,

https://github.com/esp8266/Arduino/blob ... PI/SPI.cpp

Code: Select all/**
 * Note:
 *  in and out need to be aligned to 32Bit
 *  or you get an Fatal exception (9)
 * @param out uint8_t *
 * @param in  uint8_t *
 * @param size uint32_t
 */
void SPIClass::transferBytes(uint8_t * out, uint8_t * in, uint32_t size) {
    while(size) {
        if(size > 64) {
            transferBytes_(out, in, 64);
            size -= 64;
            if(out) out += 64;
            if(in) in += 64;
        } else {
            transferBytes_(out, in, size);
            size = 0;
        }
    }
}


"in and out need to be aligned to 32Bit"

How I do to align bits?

Re: How I do to align bits in transfer uint8_t array in SPI?

PostPosted: Thu Dec 08, 2016 10:41 am
by martinayotte
This means that pointer should be with 32 bits addresses, so, divisible by 4.

Here is some explanations which can help understanding :

viewtopic.php?p=55810#p55810

Re: How I do to align bits in transfer uint8_t array in SPI?

PostPosted: Thu Dec 08, 2016 1:18 pm
by Leandro Zimmer
How I create one array de uint8_t with bits aligned to call transferBytes(..?

uint8_t in[3];
uint8_t on[3];

in[0] = 0x01;
in[1] = 0x02;
in[2] = 0x03;

Re: How I do to align bits in transfer uint8_t array in SPI?

PostPosted: Thu Dec 08, 2016 2:50 pm
by martinayotte
Are those arrays been local variables ?
Because I don't think you even need to bother about alignment, since variables created on stack are supposed to be aligned already to 32 bits.
You can verify that by printing their starting address using printf("%p", &in[0]);
If address are not rounded at 4, then you can try :
uint8_t in[3] __attribute__((aligned(4)));