Skip to main content
Explorer
May 16, 2024
Question

LSM6DSL SPI read as in tsw-fcu001.html

  • May 16, 2024
  • 1 reply
  • 720 views

Hi

I was looking at how a register read works for LSM6DSL

https://www.st.com/en/embedded-software/stsw-fcu001.html

I found the function below. Does the number/amount of __asm("dsb\n") correspond to SPI clk?

Is there no easier way to do this? Like using HAL_SPI_TransmitReceive(...)?

 

/**

* @brief This function reads a single byte on SPI 3-wire.

* @param xSpiHandle : SPI Handler.

* @param val : value.

* @retval None

*/

void SPI_Read(SPI_HandleTypeDef* xSpiHandle, uint8_t *val)

{

/* In master RX mode the clock is automaticaly generated on the SPI enable.

So to guarantee the clock generation for only one data, the clock must be

disabled after the first bit and before the latest bit */

/* Interrupts should be disabled during this operation */

 

__disable_irq();

//GPIOA->BSRR = (uint32_t)GPIO_PIN_8 << 16U;

__HAL_SPI_ENABLE(xSpiHandle);

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");__asm("dsb\n");

__asm("dsb\n");

__asm("dsb\n");

__asm("dsb\n");

__asm("dsb\n");

__asm("dsb\n");

__asm("dsb\n");

__asm("dsb\n");

__asm("dsb\n");

__asm("dsb\n");

__HAL_SPI_DISABLE(xSpiHandle);

 

__enable_irq();

 

while ((xSpiHandle->Instance->SR & SPI_FLAG_RXNE) != SPI_FLAG_RXNE);

/* read the received data */

*val = *(__IO uint8_t *) &xSpiHandle->Instance->DR;

while ((xSpiHandle->Instance->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY);

}

 

    This topic has been closed for replies.

    1 reply

    Technical Moderator
    July 9, 2024

    Hi @MK..1 ,

    The software example that you share was a first release, it works but it is open to improvements. 

    __asm("dsb\n") function in the example is used to introduce delay and synchronize communication.

    I suggest to test the communication with the function provided in the example and then test the code substituting it with HAL.