Skip to main content
Sampath K Sudi
Associate
March 31, 2017
Question

SPI Master Read Write using HAL commands

  • March 31, 2017
  • 13 replies
  • 3495 views
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.

    This topic has been closed for replies.

    13 replies

    Nesrine M_O
    Associate
    March 31, 2017
    Posted on March 31, 2017 at 11:30

    Hi

    sudi.sampath

    ,

    Welcome to STM32 community

    As novice user of STM32 product,I'd highly recommend you to start with ready example under the STM32L4 cube firmware package:

    STM32Cube_FW_L4_V1.7.0\Projects\STM32L476RG-Nucleo\Examples\SPI

    -Nesrine-

    Sampath K Sudi
    Associate
    March 31, 2017
    Posted on March 31, 2017 at 23:31

    Thanks Nesrine, 

    it  tried to modify example programming still i see the same issue, all spi message are sent in 1 byte format

    for instace :- i am trying to send 3 bytes (0x12 0x34  0x56  ) , but i see data is sent 0x12 , 0x34 and 0x56 are transmitted in  3 different spi data.

    T J
    Senior III
    April 2, 2017
    Posted on April 02, 2017 at 08:03

    I use these functions after HAL initialisation:

    void quickSend8SPI(SPI_HandleTypeDef *hspi){

        while( !(    hspi->Instance->SR  & SPI_FLAG_TXE));

            *((__IO uint8_t *)&hspi->Instance->DR) =  TxSPIByte;        

    }

    void quickSendReceive8SPI(SPI_HandleTypeDef *hspi){

            while( !(    hspi->Instance->SR  & SPI_FLAG_TXE));

        

        *((__IO uint8_t *)&hspi->Instance->DR) =  TxSPIByte;                // force the SPI to transceive 8 bit

            while( !(    hspi->Instance->SR  & SPI_FLAG_TXE));

            while(  (    hspi->Instance->SR  & SPI_FLAG_BSY));

            while(  (    hspi->Instance->SR  & SPI_FLAG_RXNE))

                    RxSPIByte1 = hspi->Instance->DR;

      

    }

    Sampath K Sudi
    Associate
    April 6, 2017
    Posted on April 06, 2017 at 04:05

    still no luck.  

    i tried below steps

        HAL_GPIO_WritePin((GPIO_TypeDef *)GPIO_AF5_SPI1,GPIO_PIN_4,GPIO_PIN_RESET);

        status = HAL_SPI_TransmitReceive_IT(spiHandle,(uint8_t *)txBuff,(uint8_t *)rxBuff,Size);    

        while( spiHandle->State == HAL_SPI_STATE_BUSY );     

        HAL_GPIO_WritePin((GPIO_TypeDef *)GPIO_AF5_SPI1,GPIO_PIN_4,GPIO_PIN_SET);

    still i see byte byte transmission.

    David Littell
    Senior II
    April 6, 2017
    Posted on April 06, 2017 at 16:52

    Could the problem be:

    hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

    Sampath K Sudi
    Associate
    April 6, 2017
    Posted on April 06, 2017 at 19:47

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

    john doe
    Senior III
    April 6, 2017
    Posted on April 06, 2017 at 19:24

    hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;

    turn off hardware nss and toggle the chip select pin manually

    Sampath K Sudi
    Associate
    April 6, 2017
    Posted on April 06, 2017 at 19:46

    Hi John,

    on Disabling NSSP mode, i am observing spi timeout  .

    S.Ma
    Principal
    April 6, 2017
    Posted on April 06, 2017 at 19:24

    Is the requesr here not to have clock delays between bytes?

    Try to blindly write 3 bytes first, this might feed the hungry fifo, or write 16 bit which will be sliced as 2x8 bit by the spi if in 8 bit transmit mode...

    Sampath K Sudi
    Associate
    April 6, 2017
    Posted on April 06, 2017 at 20:25

    how do enable packing mode for spi transfer

    Andrei Chichak
    Lead
    April 6, 2017
    Posted on April 06, 2017 at 20:34

    Can you fill us in on why it matters? The clock doesn't change between the bytes, so the delay just comes down to a delay of about 700 nanoseconds. Are you that strapped for time?

    Andrei

    (

    http://embedded.fm/blog/2017/4/4/scrawls-and-traces

     )
    john doe
    Senior III
    April 6, 2017
    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 );

    Andrei Chichak
    Lead
    April 7, 2017
    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.