SPI : transferring 16-bit into FIFO


2 posts
by Arjan » Sat Nov 03, 2012 7:18 pm
Hi,

Looking at the function
uint8_t bcm2835_spi_transfer ( uint8_t value)
(http://www.open.com.au/mikem/bcm2835/group__spi.html#ga06884e9cd71dfe380f846b09f47b7248),
then it writes 8 bit data byte to MOSI
Code: Select all
// Writes (and reads) a single byte to SPI
uint8_t bcm2835_spi_transfer(uint8_t value)
{
    volatile uint32_t* paddr = spi0 + BCM2835_SPI0_CS/4;
    volatile uint32_t* fifo = spi0 + BCM2835_SPI0_FIFO/4;
.....
// Write to FIFO, no barrier
bcm2835_peri_write_nb(fifo, value);

where
Code: Select all
// write to peripheral without the write barrier
void bcm2835_peri_write_nb(volatile uint32_t* paddr, uint32_t value)
{
    if (debug)
    {
   printf("bcm2835_peri_write_nb paddr %08X, value %08X\n",
               (unsigned) paddr, value);
    }
    else
    {
   *paddr = value;
    }
}
I am wondering how I can change the function into
Code: Select all
uint16_t bcm2835_spi_transfer(uint16_t value)
and then writing 16 bits data at once to the FIFO.

Any suggestions?

Thanks. Regards, Arjan
Posts: 130
Joined: Sat Sep 08, 2012 1:59 pm
by msperl » Sun Nov 04, 2012 6:25 pm
Hi Arjan!

You can only shift in 8 byte at a time.
So you have to split your 16 bit value in 2 assigns to the FIFO.
The only exception to this rule is DMA triggered transfer, where 32 bit get transferred at a time.

So you would have to do this for your 16 bit value:
*fifo=(value>>8);
*fifo=(value&0xff);

Before writing you always should check if the fifo is filled to the max (by testing one of the bits in another register).

Ciao,
Martin
Posts: 234
Joined: Thu Sep 20, 2012 3:40 pm