12bit formatting for SPI write

01 Jan 2011

 

Hey guys,

When configuring the SPI module on an mbed is it possible to do something like this:

SPI mySPI (p5, p6, p7);

mySPI.format(12,0);

or will the format only accept numbers which conform to data types i.e. 8/16/32 ?

There are a number of devices which make use of 10, 12 or 14bit data inputs/outputs so my question actually covers a much broader set of problems then the one I am looking at at the moment.

Specifically, I have some LED drivers on my bench (some TI TLC5947 and some Allegro A6281 based ShiftBrites from macetech) both of which make use of 14bits per channel to set LED brightness.

 

The 5947 has 24 output channels so for the sake of my example lets say that we have an array of 16bit numbers, 24 elements long. Each element in the array contains a 12bit value and 4 bits of 'padding'.

 

If "mySPI.format(12,0);" is possible, how do I pass the array to "mySPI.write();" so it sends everything in one go but only sends the first 12 of 16 bits per element.

 

If this is not possible then I am going to have to re-arrange my data so that two 12 bit values are split across three bytes. In my head this process involves a lot of shifting/rotating and copying nibbles around the place. A 24 element input array would result in a 36 element byte/char array. Is there a more efficient way of doing this, is there function I've missed or is there some pre-existing code somewhere which I can pass an array and get another one back as long as I also passed it the relative input and output sizes as well as the location of the padding bits.

I am assuming at this stage that it is possible to pass an array to the mySPI.write (in the same way an array can be passed to myI2C.write();

Any thoughts on the matter much appreciated.

Cheers

D

 

01 Jan 2011

Dev, you can indeed do what you propose, with SPI.format(12,0). That's exactly what I do with my TLC5947 driver project :-). The TLC5947 is a great part.

I store the RGB values as integers and pass them directly to spi.write() like this:

int r,g,b; spi.write(r); spi.write(b); spi.write(g);

spi.write takes an integer, using only the specified number of bits from each int passed in. You can't pass a pointer/array into the write function. To write the full string of 24*12=288 bits you have to put this code into a loop.

good luck, steve

01 Jan 2011

Ah awesome!

 

for(int i = 0; i < 23; i++){
    spi.write(LedArray[i]);
}

or similar should do okay then.

 

Thanks Steve

D