cancel
Showing results for 
Search instead for 
Did you mean: 

Sending more than 8 bits through SPI using HAL

Kolab
Senior

Hello! I need to send/receive a 16 bits data to an external ADC, but I realized that SPI HAL functions just support 8 bits buffers. How can I send more than 8 bits? Even DMA function all work with 8 bits. In CubeMX I set the word length as 16.

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

All of the HAL_SPI_* functions can transfer multiple bytes at once. Perhaps you are not using them correctly. Should be something like this if word size is configured to 8 bits:

uint8_t data[2] = {0x00, 0x01};
HAL_SPI_Transmit(&hspi, data, 2, HAL_MAX_DELAY);

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

View solution in original post

6 REPLIES 6

Which STM32?

A pair of 8-bit bytes have 16 clocks, does this somehow differ from sending one 16-bit word?

The SPI DR typically is sensitive to the width of the write operation, some models may also use a FIFO

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Kolab
Senior

stm32f205

Kolab
Senior

do you mean sendig a 2 bytes array ?

TDK
Guru

All of the HAL_SPI_* functions can transfer multiple bytes at once. Perhaps you are not using them correctly. Should be something like this if word size is configured to 8 bits:

uint8_t data[2] = {0x00, 0x01};
HAL_SPI_Transmit(&hspi, data, 2, HAL_MAX_DELAY);

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

@TDK​ does  SPI_Transmit send first data[0] as LSB?

what about the reception, will data[0] also be LSB?

TDK
Guru

> does SPI_Transmit send first data[0] as LSB?

LSB refers to order of bits, not bytes.

data[0] will be sent/received as the first byte.

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