cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Master Read Write using HAL commands

Sampath K Sudi
Associate II
Posted on March 31, 2017 at 04:56

Hi All,

   I am new to STM , I  have problem in  read and write data simultaneously  using STM32L476 board. i just created a new project using STMcubeMX and try to read and write  simultaneously  using HAL_SPI_TransmitReceive_IT , and when i checked on my spi sniffer, spi data is sent byte ,byte ...   Here my  spi init config

hspi1.Instance = SPI1;

  hspi1.Init.Mode = SPI_MODE_MASTER;

  hspi1.Init.Direction = SPI_DIRECTION_2LINES;

  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;

  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

  hspi1.Init.NSS = SPI_NSS_SOFT ;

  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;

  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;

  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

  hspi1.Init.CRCPolynomial = 7;

  hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;

  hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;

if (HAL_SPI_Init(&hspi1) != HAL_OK)

is any  missing in my configuration.

Thanks in advance.

13 REPLIES 13
Posted on April 06, 2017 at 19:46

Hi John,

on Disabling NSSP mode, i am observing spi timeout  .

Posted on April 06, 2017 at 19:47

Hi David, No Luck still i am seeing the same behaviour.

john doe
Lead
Posted on April 07, 2017 at 01:44

here's how I do spi master using hal

    HAL_GPIO_WritePin( GPIOB, SPI2_CS1_Pin, GPIO_PIN_RESET );

    status = HAL_SPI_TransmitReceive( &hspi2, (uint8_t *)(&txarray), (uint8_t *)(&rxarray), cnt+1, 5);

    while( hspi2.State == HAL_SPI_STATE_BUSY ) {};

    HAL_GPIO_WritePin( GPIOB, SPI2_CS1_Pin, GPIO_PIN_SET );

Posted on April 07, 2017 at 07:15

You don't need the while. HAL_SPI_TransmitReceive will spin on the RXNE bit until the data has been shifted out or it hits your 5 ms timeout. It might be a better idea to check status for HAL_TIMEOUT.