Skip to main content
June 16, 2021
Solved

Sending more than 8 bits through SPI using HAL

  • June 16, 2021
  • 6 replies
  • 2682 views

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.
Best answer by TDK

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);

6 replies

Tesla DeLorean
Guru
June 16, 2021

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..
June 16, 2021

stm32f205

June 16, 2021

do you mean sendig a 2 bytes array ?

TDK
TDKBest answer
June 16, 2021

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""."
June 17, 2021

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

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

TDK
June 17, 2021

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