LIS2DH12 init problem for FIFO stream mode
Hi Everybody,
I have a weird problem initializing LIS2DH12 in FIFO stream mode with the official ST driver. This is my initialization code:
bool lis2dh12wInit(lis2dh12w_cfg_t *cfg)
{
uint8_t whoamI;
lis2dh12_ctrl_reg3_t ctrl_reg3;
/* set read and write functions for the platform-independent driver */
dev_ctx.write_reg = platform_write;
dev_ctx.read_reg = platform_read;
dev_ctx.handle = NULL; // this optional parameter is not used
/* check device ID */
lis2dh12_device_id_get(&dev_ctx, &whoamI);
if (whoamI != LIS2DH12_ID) return false; // not o.k.
cfgSave = *cfg;
/* reset register contents */
lis2dh12_boot_set(&dev_ctx, 1);
/* enable Block Data Update to avoid reading data values related to different samples */
lis2dh12_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
/* set Output Data Rate */
lis2dh12_data_rate_set(&dev_ctx, cfg->odr);
/* set full scale */
lis2dh12_full_scale_set(&dev_ctx, cfg->fullScale);
/* set device resolution (continuous mode) */
lis2dh12_operating_mode_set(&dev_ctx, cfg->highRes ? LIS2DH12_HR_12bit : LIS2DH12_LP_8bit);
if (cfg->fifo == 0) { // normal mode
/* set interrupt */
memset(&ctrl_reg3, 0, sizeof(ctrl_reg3)); // reset all flags
ctrl_reg3.i1_zyxda = 1; // "data available" interrupt
lis2dh12_pin_int1_config_set(&dev_ctx, &ctrl_reg3);
} else { // FIFO mode
md_external_delay_ms(10); // MAGIC: somewhy this is needed to make it work
/* enable FIFO */
lis2dh12_fifo_set(&dev_ctx, 1);
/* set FIFO watermark level */
lis2dh12_fifo_watermark_set(&dev_ctx, (cfg->fifo - 1) & 0x1F);
/* set FIFO mode */
lis2dh12_fifo_mode_set(&dev_ctx, LIS2DH12_DYNAMIC_STREAM_MODE);
/* set interrupt */
memset(&ctrl_reg3, 0, sizeof(ctrl_reg3)); // reset all flags
ctrl_reg3.i1_wtm = 1; // "FIFO watermark" interrupt
lis2dh12_pin_int1_config_set(&dev_ctx, &ctrl_reg3);
}
return true;
} /* lis2dh12wInit */I spent a lot of time to figure out that it is working with the magic delay inserted to line 33. Without it the sensor will also generate watermark interrupts fine, but the acceleration value is always the same (reads the same FIFO item for infinity?). With this 10 msec delay strictly before enabling FIFO it is o.k.
What may be the reason of this strange behaviour? I've found nothing in community topics and the documentation. Thank you in advance.
Gábor
