cancel
Showing results for 
Search instead for 
Did you mean: 

Proper way to send 16 bit SPI data

jhanc.11
Senior

I need to send 8 bit and 16 bit data with my 32f769. I found a register modification routine to change the bits from 8 to 16 and back. I checked the SFRs and it works (changing the bits that is),

When I transmit 8 bit data, all is fine. I can send 1,2,3 etc bytes without trouble.

When I load up a uint16_t with the correct data, verified in debug, with the SPI datasize bits set to 16 and a (uint8_t)&data pointing at the single uint16_t of data to be sent, I see 16 clock pulses on the scope but it sends two bytes with the data split between them padding the resert with 0x00.

How should I set it up properly or should I just split the data and send it myself as 8bits?

thanks

4 REPLIES 4
AScha.3
Chief III

i would stay always at 8bit mode, send 2x for 16bit data.

or : if most time 16bit data is used, set the SPI to 16bit mode and for 8 bit data use only LSB (MSB set 0x0).

(i think, switching the mode will create problems. )

If you feel a post has answered your question, please click "Accept as Solution".
jhanc.11
Senior

I've fought with this for two days now. If I config for 16bit, it will send 8 bits of data in a 16bit clock length and throw away the 2nd byte that I tell it to send.

If I use 8bit, the device I am using works fine for 8bit register writes and reads. No problem but as soon as I send 2 bytes, I see my data getting split across two writes. So I send for instance, 8bit AA, AA, AA, then 16bit BBCC, BBCC,BBCC then the important command I send to clear its interrupts, DDEE is sent, but I see CCDD, EEDD, EEDD, EEDD on the scope trace instead of DDEE,DDEE,DDEE, if you follow my example. I'm not changing the frame at all though I've tried to flip it to 16 but again, it send 8 bits of data in a 16 bit clock frame. There's something to be said for the Arduino because this is driving me crazy. I'm going to try sending a dummy byte in the morning to see if that works to bring it back in alignment.

AScha.3
Chief III

in 8bit mode , send byte is ok - right?

to send 16b , you have to split it in 2 bytes and send them one after the other.

to send DDEE -> send Byte DD , then send EE . on receiver side put it together again, to get DDEE .

or show your code, what you write to SPI.

If you feel a post has answered your question, please click "Accept as Solution".
S.Ma
Principal

Assuming this SPi has 32 bit fifo hw, use spi in 8 bit mode. The SPI will queue bytes.

Write a byte in the DR (byte cast), and a byte is queued.

Write a 16bit in the DR and it will be sliced and queue as double write (beware msb or lsb first)... etc.... the spi mode should be the smallest common size. The fifo chunks and queue...