Skip to main content
Visitor II
September 10, 2025
Solved

Best practice for sending 9-bit SPI data with 8-bit transfer mode ?

  • September 10, 2025
  • 3 replies
  • 340 views

Hello,
I need to communicate with a component whose configuration register expects a 9-bit value. However, my current SPI setup sends data in 8-bit chunks.

In this case, what would be the correct approach? Should I send two bytes back-to-back? Or would it be better to make this configurable in main.c so that the behavior can be adjusted easily?

If you were in my place, how would you handle this?

 

durna_0-1757507563996.png

 

 

static void MX_SPI3_Init(uint32_t bit_size)
{

 /* USER CODE BEGIN SPI3_Init 0 */

 /* USER CODE END SPI3_Init 0 */

 /* USER CODE BEGIN SPI3_Init 1 */

 /* USER CODE END SPI3_Init 1 */
 /* SPI3 parameter configuration*/
 hspi3.Instance = SPI3;
 hspi3.Init.Mode = SPI_MODE_MASTER;
 hspi3.Init.Direction = SPI_DIRECTION_2LINES;
 hspi3.Init.DataSize = bit_size; // <-- here
 hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
 hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
 hspi3.Init.NSS = SPI_NSS_SOFT;
 hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
 hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
 hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
 hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 hspi3.Init.CRCPolynomial = 0x0;
 hspi3.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
 hspi3.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
 hspi3.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
 hspi3.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
 hspi3.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
 hspi3.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
 hspi3.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
 hspi3.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
 hspi3.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
 hspi3.Init.IOSwap = SPI_IO_SWAP_DISABLE;
 if (HAL_SPI_Init(&hspi3) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN SPI3_Init 2 */

 /* USER CODE END SPI3_Init 2 */

}

 

    This topic has been closed for replies.
    Best answer by durna

    I've found the answer :)

    durna_0-1757510271496.png

     

    3 replies

    Super User
    September 10, 2025

    What you need to do depends on what the slave requires/allows.

    You haven't given any details of the slave.

    Have you contacted the slave's manufacturer for advice?

     


    @durna wrote:

    If you were in my place, how would you handle this?


    For the purpose of experimentation, bit-bang it to find what works.

    Once you've found what works, see if that can be implemented using the STM32's SPI hardware.

    durnaAuthor
    Visitor II
    September 10, 2025

    I only need to send a 9-bit configuration value once during initialization. After that, all read/write operations will use standard 8-bit communication.

    durnaAuthorAnswer
    Visitor II
    September 10, 2025

    I've found the answer :)

    durna_0-1757510271496.png