Skip to main content
PBU
Associate III
March 28, 2019
Question

Sending 9 bit data using SPI STM32F7

  • March 28, 2019
  • 2 replies
  • 1699 views

Hi

I'm using STM32F7 series microcontroller. Is it possible to send 9 bit data using SPI? the reason is i have to send 8 bit data along with command bit (ie 9th bit). I have seen some option in datasheet like we can chose the data length between 4 bits to 16 bits in SPI control register 2 (SPIx_CR2) register.

Thanks

This topic has been closed for replies.

2 replies

After Forever
Senior III
March 28, 2019

Yes, it is possible. You can choose the data length by changing the DS[3:0] bits of the SPIx_CR2 register, use binary 1000 for 9-bit data length. Read the reference manual.

PBU
PBUAuthor
Associate III
April 1, 2019

Hi @After Forever​ 

Right now i'm using HAL Library. I have chosen SPI data size to 9 bits. While sending data through SPI, only the first time 9bit gets clocked out, After that 8 bits are clocked out. Do we need to change any other register settings?

After Forever
Senior III
April 1, 2019

So why didn't you mention that you are using HAL in your first post or least in the tags? :)

I don't use HAL, but seeing its source code, you can set the data length using the DataSize parameter before calling HAL_SPI_Init:

SPI_HandleTypeDef SpiHandle;
...
...
SpiHandle.Init.DataSize = SPI_DATASIZE_9BIT;
...
...
if (HAL_SPI_Init(&SpiHandle) != HAL_OK) {
 Error_Handler();
}

waclawek.jan
Super User
April 1, 2019

Read out SPI registers content, and check/compare between "working" and "non-working" state.

JW