cancel
Showing results for 
Search instead for 
Did you mean: 

I can't read register WhoAmI from LIS2DW12 with I2C

MAndr.233
Associate III

Hi,

I've bought a ST MKI179V1 board to interface via I2C with a Nucelo-L433RC . Furthermore, I'm using ST oficial drivers to MEMS sensors. The problem is when I try to read the register it always return 0. I've already made a lot of tests and I can't discover the problem. For me it looks to be so simple but I'm not makinng this work.

I let you my schematic:

0693W000000VxzoQAC.png

And my simplified code:

  • Field definitions;
static uint8_t whoamI;
static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len);

  • Read Function:
static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len) {
	HAL_I2C_Mem_Read(handle, LIS2DW12_I2C_ADD_L, reg, I2C_MEMADD_SIZE_16BIT,
			bufp, len, 1000);
	return 0;
}
  • Main Function;
int main(void){
stmdev_ctx_t dev_ctx;
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
dev_ctx.handle = &hi2c1;
 
MX_I2C1_Init();
...
lis2dw12_device_id_get(&dev_ctx, &whoamI);
if (whoamI != LIS2DW12_ID){
//  		Device not found
}
...
}

Furthermore when I use HAL_I2C_IsDeviceReady it seems to identify the device.

if(HAL_I2C_IsDeviceReady(&hi2c1, LIS2DW12_I2C_ADD_L,1,1000) == HAL_OK){
// It enters here correctly
       debugPrintln(&huart2, "Device Is Ready");
}

It proves that the device is correctly connected.

I would appreciate any help.

Best Regards

1 ACCEPTED SOLUTION

Accepted Solutions
MAndr.233
Associate III

I solved my problem. The problem was in read function. The right way is:

static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len) {
	HAL_I2C_Mem_Read(handle, LIS2DW12_I2C_ADD_L, reg, I2C_MEMADD_SIZE_8BIT,
			bufp, len, 1000);
	return 0;
}

Thank you for help

View solution in original post

3 REPLIES 3
Eleon BORLINI
ST Employee

Hi @Community member​ , did you place the pull-up resistors on I2C SDA and SCL lines? You can also check if you structured well your project by checking the STM32CubeL4 function package examples for Nucelo-L433RC (folder \Projects\NUCLEO-L433RC-P) from MCU side, and the X-CUBE-MEMS1 function pack (folder \Projects\STM32L476RG-Nucleo\Examples\IKS01A3), from LIS2DW12 MEMS + STM32L4 family side. Regards

MAndr.233
Associate III

Thank you for the response,

Yes I already tried with pull up resistors and the result it's exactly the same. I was wondering if I have to initialize device in some way. But following the example in Driver's example it seems that nothing is initialized.

MAndr.233
Associate III

I solved my problem. The problem was in read function. The right way is:

static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, uint16_t len) {
	HAL_I2C_Mem_Read(handle, LIS2DW12_I2C_ADD_L, reg, I2C_MEMADD_SIZE_8BIT,
			bufp, len, 1000);
	return 0;
}

Thank you for help