cancel
Showing results for 
Search instead for 
Did you mean: 

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

Gabriele1
Associate II

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

1 ACCEPTED SOLUTION

Accepted Solutions
Gabriele1
Associate II

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:

View solution in original post

2 REPLIES 2
Gabriele1
Associate II

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:

Eleon BORLINI
ST Employee

Good job Gabriele! :smiling_face_with_smiling_eyes:

And thank you very much for reporting your solution

-Eleon