cancel
Showing results for 
Search instead for 
Did you mean: 

ISM330DHCX - How to use X-CUBE-MEMS1 generated library with SPI?

PSław.1
Associate

Hi!

I've configured an ISM330DHCX in CubeIDE with the X-CUBE-MEMS package. The code was generated, however, I have absolutely no idea how to initialize and use the sensor in my main.c code. So far I've managed to make following observations:

  • There is a ISM330DHCX_RegisterBusIO function. It needs a ISM330DHCX_Object_t pointer and ISM330DHCX_IO_t.
  • When I tried to create and fill ISM330DHCX_IO_t values by hand, I've noticed, that I can't find any function, that suits the WriteReg and ReadReg fields. ISM330DHCX_WriteReg_Func and ISM330DHCX_ReadReg_Func seem to be written with I2C in mind, requiring address and register value, while BSP_SPI2_Send and BSP_SPI2_Recv, generated in custom_bus.h file along other files to support the MEMS module, seem incompatible with the needed type.
  • There is a ISM330DHCX_Init() function, but i'm not sure it can be called before calling RegisterBusIO, which leaves me with the previous problem.

So how do I initialize the MEMS for 4-wire SPI bus, and why isn't this done automatically, when CubeMX even asks the user to point it to SPI instance responsible for communicationg with the sensor, as well as the CS pin for this task?

Thanks!

EDIT:

So far I've tried to use the library in the following way:

#include "custom_mems_conf.h"
#include "ism330dhcx.h"
 
static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp,
                              uint16_t len)
{
  reg |= 0x40;
  HAL_GPIO_WritePin(CUSTOM_ISM330DHCX_0_CS_PORT, CUSTOM_ISM330DHCX_0_CS_PIN, GPIO_PIN_RESET);
  CUSTOM_ISM330DHCX_0_SPI_Send(&reg, 1);
  CUSTOM_ISM330DHCX_0_SPI_Send((uint8_t*) bufp, len);
  HAL_GPIO_WritePin(CUSTOM_ISM330DHCX_0_CS_PORT, CUSTOM_ISM330DHCX_0_CS_PIN, GPIO_PIN_SET);
 
  return 0;
}
 
/*
 * @brief  Read generic device register (platform dependent)
 *
 * @param  handle    customizable argument. In this examples is used in
 *                   order to select the correct sensor bus handler.
 * @param  reg       register to read
 * @param  bufp      pointer to buffer that store the data read
 * @param  len       number of consecutive register to read
 *
 */
static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp,
                             uint16_t len)
{
  reg |= 0xC0;
  HAL_GPIO_WritePin(CUSTOM_ISM330DHCX_0_CS_PORT, CUSTOM_ISM330DHCX_0_CS_PIN, GPIO_PIN_RESET);
  CUSTOM_ISM330DHCX_0_SPI_Send(&reg, 1);
  CUSTOM_ISM330DHCX_0_SPI_Recv(bufp, len);
  HAL_GPIO_WritePin(CUSTOM_ISM330DHCX_0_CS_PORT, CUSTOM_ISM330DHCX_0_CS_PIN, GPIO_PIN_SET);
 
  return 0;
}
 
ISM330DHCX_IO_t io_ctx;
ISM330DHCX_Object_t ISM330;
ISM330DHCX_Axes_t axes_data;
uint8_t ISM_ID;
io_ctx.BusType     = ISM330DHCX_SPI_4WIRES_BUS;
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     = HAL_GetTick;
ISM330DHCX_RegisterBusIO(&ISM330, &io_ctx);
ISM330DHCX_Init(&ISM330);
ISM330DHCX_ACC_Enable(&ISM330);
ISM330DHCX_ReadID(&ISM330, &ISM_ID);
ISM330DHCX_ACC_GetAxes(&ISM330, &axes_data);

But that's a blind shot, and I keep getting zeros everywhere (all axes, as well as ID are read as 0)

1 REPLY 1
parata.carlo
Associate III

Hello @PSław.1,

Please, make sure to select ISM330DHCX Board Part as SPI, MOTION_SENSOR in the Board Support Custom in the X-CUBE-MEMS1 pack of CubeIDE and enable the correct pins for SPI and Chip Select. Make also sure that the Chip Select pin is initialized HIGH in the "System Core/GPIO" tab and that you select also a suitable SPI speed and mode (the sensor should support CPOL=0 and CPHASE=1Edge). At this point you can try to generate the code and in the main.c you can call:

CUSTOM_MOTION_SENSOR_Init(CUSTOM_ISM330DHCX_0, MOTION_ACCELERO|MOTION_GYRO);

to initialize the sensor and in the while(1) loop you can call:

CUSTOM_MOTION_SENSOR_Axes_t acc_value;

CUSTOM_MOTION_SENSOR_Axes_t gyr_value;

CUSTOM_MOTION_SENSOR_GetAxes(CUSTOM_ISM330DHCX_0, MOTION_ACCELERO, &acc_value);

CUSTOM_MOTION_SENSOR_GetAxes(CUSTOM_ISM330DHCX_0, MOTION_GYRO, &gyr_value);

to get the values of accelerometer and gyroscope.

Best Regards,

Carlo