Skip to main content
Jun 16, 2021 2:33 PM UTC
Solved

Sending more than 8 bits through SPI using HAL

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.

This topic has been closed for replies.
TDK
TDKBest answer
Jun 16, 2021 9:29 PM UTC

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""."

6 replies

Tesla DeLorean
Guru
Jun 16, 2021 2:37 PM UTC

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
Jun 16, 2021 3:19 PM UTC

stm32f205

Jun 16, 2021 3:21 PM UTC

do you mean sendig a 2 bytes array ?

TDK
TDKBest answer
Jun 16, 2021 9:29 PM UTC

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""."
Jun 17, 2021 7:29 AM UTC

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

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

TDK
Jun 17, 2021 1:25 PM UTC

> 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""."