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
Nesrine M_O
Lead II
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 II
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
Lead
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 II
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.

Posted on April 06, 2017 at 16:52

Could the problem be:

hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

john doe
Lead
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

S.Ma
Principal
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 II
Posted on April 06, 2017 at 20:25

how do enable packing mode for spi transfer

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

 )