ISM330DHCX - How to use X-CUBE-MEMS1 generated library with SPI?
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(®, 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(®, 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)
