Skip to main content
Visitor II
February 24, 2021
Solved

Is someone using the LSM6DSL with SPI using BSP Custom driver?

  • February 24, 2021
  • 2 replies
  • 1424 views

Hi everyone, I'm using an STM32L4 microcontroller and a LSM6DSL MEMS. They are conected with the spi2 interface and for configuring everithing I'm using STMCubeIDE.

I'm able to use the MEMS without BSP Custom, but when I try to set the BSP, it doesn't work at all.

Here the code I used to link BSP with SPI:

 /* Link SPI functions to the LSM6DSL driver */

 io_ctx.BusType   = LSM6DSL_SPI_4WIRES_BUS;

 io_ctx.Init    = BSP_SPI2_Init;

 io_ctx.DeInit   = BSP_SPI2_DeInit;

 io_ctx.ReadReg   = BSP_SPI2_SendRecv;

 io_ctx.WriteReg  = BSP_SPI2_Send;

 io_ctx.GetTick   = BSP_GetTick;

 LSM6DSL_RegisterBusIO(&MotionSensor, &io_ctx);

I have read on internet that it works fine with I2C, but it seems to be a little tricky to set it up with SPI.

Anyone could help?

Thanks

Gabriele

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

    Hello everyone again,

    I solved the issue. My mistake was in these two line:

     io_ctx.ReadReg   = BSP_SPI2_SendRecv;

     io_ctx.WriteReg  = BSP_SPI2_Send;

    BSP_SPI2_SendRecv and BSP_SPI2_Send functions are incompatible so I did the following wrap:

    int32_t wrap_platform_read(uint8_t Address, uint8_t Reg, uint8_t *Bufp, uint16_t len){
     Reg |= 0x80;
     HAL_GPIO_WritePin(CS_SPI2_GPIO_Port, CS_SPI2_Pin, GPIO_PIN_RESET);
     BSP_SPI2_Send(&Reg, 1);
     BSP_SPI2_SendRecv(&Reg, Bufp, len);
     HAL_GPIO_WritePin(CS_SPI2_GPIO_Port, CS_SPI2_Pin, GPIO_PIN_SET);
     return P_OK;
    }
     
    int32_t wrap_platform_write(uint8_t Address, uint8_t Reg, uint8_t *Bufp, uint16_t len){
     HAL_GPIO_WritePin(CS_SPI2_GPIO_Port, CS_SPI2_Pin, GPIO_PIN_RESET);
     BSP_SPI2_Send(&Reg, 1);
     BSP_SPI2_Send(Bufp, len);
     HAL_GPIO_WritePin(CS_SPI2_GPIO_Port, CS_SPI2_Pin, GPIO_PIN_SET);
     return P_OK;
    }

    and here the correct link:

     /* Link SPI functions to the LSM6DSL driver */
     io_ctx.BusType = LSM6DSL_SPI_4WIRES_BUS;
     io_ctx.Address = 0;
     io_ctx.Init = BSP_SPI2_Init;
     io_ctx.DeInit = BSP_SPI2_DeInit;
     io_ctx.ReadReg = platform_read;
     io_ctx.WriteReg = platform_write;
     io_ctx.GetTick = BSP_GetTick;
     LSM6DSL_RegisterBusIO(&MotionSensor, &io_ctx);

    :grinning_face:

    2 replies

    Gabriele1AuthorAnswer
    Visitor II
    February 25, 2021

    Hello everyone again,

    I solved the issue. My mistake was in these two line:

     io_ctx.ReadReg   = BSP_SPI2_SendRecv;

     io_ctx.WriteReg  = BSP_SPI2_Send;

    BSP_SPI2_SendRecv and BSP_SPI2_Send functions are incompatible so I did the following wrap:

    int32_t wrap_platform_read(uint8_t Address, uint8_t Reg, uint8_t *Bufp, uint16_t len){
     Reg |= 0x80;
     HAL_GPIO_WritePin(CS_SPI2_GPIO_Port, CS_SPI2_Pin, GPIO_PIN_RESET);
     BSP_SPI2_Send(&Reg, 1);
     BSP_SPI2_SendRecv(&Reg, Bufp, len);
     HAL_GPIO_WritePin(CS_SPI2_GPIO_Port, CS_SPI2_Pin, GPIO_PIN_SET);
     return P_OK;
    }
     
    int32_t wrap_platform_write(uint8_t Address, uint8_t Reg, uint8_t *Bufp, uint16_t len){
     HAL_GPIO_WritePin(CS_SPI2_GPIO_Port, CS_SPI2_Pin, GPIO_PIN_RESET);
     BSP_SPI2_Send(&Reg, 1);
     BSP_SPI2_Send(Bufp, len);
     HAL_GPIO_WritePin(CS_SPI2_GPIO_Port, CS_SPI2_Pin, GPIO_PIN_SET);
     return P_OK;
    }

    and here the correct link:

     /* Link SPI functions to the LSM6DSL driver */
     io_ctx.BusType = LSM6DSL_SPI_4WIRES_BUS;
     io_ctx.Address = 0;
     io_ctx.Init = BSP_SPI2_Init;
     io_ctx.DeInit = BSP_SPI2_DeInit;
     io_ctx.ReadReg = platform_read;
     io_ctx.WriteReg = platform_write;
     io_ctx.GetTick = BSP_GetTick;
     LSM6DSL_RegisterBusIO(&MotionSensor, &io_ctx);

    :grinning_face:

    ST Employee
    February 25, 2021

    Good job Gabriele! :smiling_face_with_smiling_eyes:

    And thank you very much for reporting your solution

    -Eleon