LIS2DW12 acceleration driver

Hi,

EDIT: I'm using nRF9160.

I have tried to get the latest driver of Zephyr's LIS2DW12 accelerometer to use but had no success with write and read registers. Earlier I've been using the driver from Zephyr revision of nRF Connect SDK v1.0-branch and I have imported the driver codes under my project and included them to that project so I am able to use the latest master branch of nRF Connect SDK.

nRF Connect SDK master branch has Zephyr's master revision:
https://github.com/NordicPlayground/fw-nrfconnect-zephyr/tree/master/drivers/sensor/lis2dw12
This driver is using stmemsc v1.02 (lis2dw12_StdC HAL interface).

nRF Connect SDK v1.0-branch has Zephyr's v1.14.99-ncs2 revision:
https://github.com/NordicPlayground/fw-nrfconnect-zephyr/tree/v1.14.99-ncs2/drivers/sensor/lis2dw12
This driver doesn't use STdC.

I have this overlay for nrf9160_pca10090ns:

&i2c2 {
	compatible = "nordic,nrf-twim";
	status = "okay";
	sda-pin = <11>;
	scl-pin = <12>;
	clock-frequency = <I2C_BITRATE_STANDARD>;

	lis2dw12@19 {
		compatible = "st,lis2dw12";
		label = "LIS2DW12";
		reg = <0x19>;
		irq-gpios = <&gpio0 10 GPIO_INT_ACTIVE_HIGH>;
	};
};

I have tried to write and read registers straight with using lis2dw12_read_reg() and lis2dw12_write_reg() in lis2dw12.c and also with functions like lis2dw12_wkup_dur_get(). I'm just getting 0 from all of these registers.

static int lis2dw12_init(struct device *dev)
{
    .
    .
    .
    u8_t readreg;
	lis2dw12_read_reg(lis2dw12->ctx, LIS2DW12_WAKE_UP_DUR, &readreg, 1);
	LOG_DBG("LIS2DW12_WAKE_UP_DUR: %d", readreg);
	lis2dw12_read_reg(lis2dw12->ctx, LIS2DW12_WAKE_UP_THS, &readreg, 1);
	LOG_DBG("LIS2DW12_WAKE_UP_THS: %d", readreg);
	lis2dw12_read_reg(lis2dw12->ctx, LIS2DW12_CTRL5_INT2_PAD_CTRL, &readreg, 1);
	LOG_DBG("LIS2DW12_CTRL5_INT2_PAD_CTRL: %d", readreg);
	lis2dw12_read_reg(lis2dw12->ctx, LIS2DW12_CTRL_REG7, &readreg, 1);
	LOG_DBG("LIS2DW12_CTRL_REG7: %d", readreg);

	u8_t writereg = 0x30U;
	lis2dw12_write_reg(lis2dw12->ctx, LIS2DW12_WAKE_UP_DUR, &writereg, 1);
	writereg = 0x44U;
	lis2dw12_write_reg(lis2dw12->ctx, LIS2DW12_WAKE_UP_THS, &writereg, 1);
	writereg = 0x40U;
	lis2dw12_write_reg(lis2dw12->ctx, LIS2DW12_CTRL5_INT2_PAD_CTRL, &writereg, 1);
	writereg = 0x20U;
	lis2dw12_write_reg(lis2dw12->ctx, LIS2DW12_CTRL_REG7, &writereg, 1);

	lis2dw12_read_reg(lis2dw12->ctx, LIS2DW12_WAKE_UP_DUR, &readreg, 1);
	LOG_DBG("LIS2DW12_WAKE_UP_DUR: %d", readreg);
	lis2dw12_read_reg(lis2dw12->ctx, LIS2DW12_WAKE_UP_THS, &readreg, 1);
	LOG_DBG("LIS2DW12_WAKE_UP_THS: %d", readreg);
	lis2dw12_read_reg(lis2dw12->ctx, LIS2DW12_CTRL5_INT2_PAD_CTRL, &readreg, 1);
	LOG_DBG("LIS2DW12_CTRL5_INT2_PAD_CTRL: %d", readreg);
	lis2dw12_read_reg(lis2dw12->ctx, LIS2DW12_CTRL_REG7, &readreg, 1);
	LOG_DBG("LIS2DW12_CTRL_REG7: %d", readreg);
	
#ifdef CONFIG_LIS2DW12_TRIGGER
	if (lis2dw12_init_interrupt(dev) < 0) {
	.
	.
	.
}

static int lis2dw12_init(struct device *dev)
{
    .
    .
    .
// START WAKEUP DURATION
	/* get wakeup duration */
	if (lis2dw12_wkup_dur_get(lis2dw12->ctx, &reg) < 0) {
		LOG_ERR("Failed to get wakeup duration");
	}
	else {
		LOG_DBG("wakeup duration: %d", reg);
	}

	/* set wakeup duration */
	if (lis2dw12_wkup_dur_set(lis2dw12->ctx, 0x30U) < 0) { //0x30U for test, 0x3BU for 1h
		LOG_ERR("Failed to set wakeup duration");
		return -EIO;
	}

	/* get wakeup duration */
	if (lis2dw12_wkup_dur_get(lis2dw12->ctx, &reg) < 0) {
		LOG_ERR("Failed to get wakeup duration");
	}
	else {
		LOG_DBG("wakeup duration: %d", reg);
	}
// END WAKEUP DURATION
// START WAKEUP THRESHOLD
	/* get wakeup threshold */
	if (lis2dw12_wkup_threshold_get(lis2dw12->ctx, &reg) < 0) {
		LOG_ERR("Failed to get wakeup threshold");
	}
	else {
		LOG_DBG("wakeup threshold: %d", reg);
	}

	/* set wakeup threshold */
	if (lis2dw12_wkup_threshold_set(lis2dw12->ctx, 0x44U) < 0) {
		LOG_ERR("Failed to set wakeup threshold");
		return -EIO;
	}

	/* get wakeup threshold */
	if (lis2dw12_wkup_threshold_get(lis2dw12->ctx, &reg) < 0) {
		LOG_ERR("Failed to get wakeup threshold");
	}
	else {
		LOG_DBG("wakeup threshold: %d", reg);
	}
// END WAKEUP THRESHOLD
// START STATIONARY / MOTION DETECTION
	/* get stationary / motion detection */
	if (lis2dw12_act_mode_get(lis2dw12->ctx, &reg) < 0) {
		LOG_ERR("Failed to get stationary / motion detection");
	}
	else {
		LOG_DBG("stationary / motion detection: %d", reg);
	}

	/* set stationary / motion detection */
	if (lis2dw12_act_mode_set(lis2dw12->ctx, LIS2DW12_DETECT_STAT_MOTION) < 0) {
		LOG_ERR("Failed to set stationary / motion detection");
		return -EIO;
	}
        
	/* get stationary / motion detection */
	if (lis2dw12_act_mode_get(lis2dw12->ctx, &reg) < 0) {
		LOG_ERR("Failed to get stationary / motion detection");
	}
	else {
		LOG_DBG("stationary / motion detection: %d", reg);
	}
// END STATIONARY / MOTION DETECTION

#ifdef CONFIG_LIS2DW12_TRIGGER
	if (lis2dw12_init_interrupt(dev) < 0) {
	.
	.
	.
}

Earlier (with older driver) I've successfully read and write registers with these:

static int lis2dw12_init(struct device *dev)
{
    .
    .
    .
	if (lis2dw12->hw_tf->write_reg(lis2dw12, LIS2DW12_WAKE_UP_DUR, 0x30)) {//0x30 for test, 0x3B for 1h
		return -EIO;
	}

	if (lis2dw12->hw_tf->write_reg(lis2dw12, LIS2DW12_WAKE_UP_THS, 0x44)) {
		return -EIO;
	}

	if (lis2dw12->hw_tf->write_reg(lis2dw12, LIS2DW12_CTRL5_ADDR, 0x40)) {
		return -EIO;
	}
   
   	if (lis2dw12->hw_tf->write_reg(lis2dw12, LIS2DW12_CTRL7_ADDR, 0x20)) {
		return -EIO;
	}
   
	if (lis2dw12_init_interrupt(dev) < 0) {
	.
	.
	.
}

And when I've read the registers (with older driver), I've got these:

LIS2DW12_WAKE_UP_DUR: 	0       // before writing
LIS2DW12_WAKE_UP_DUR: 	48		// after writing
LIS2DW12_WAKE_UP_THS: 	0		// before writing
LIS2DW12_WAKE_UP_THS: 	68		// after writing
LIS2DW12_CTRL5_ADDR: 	0		// before writing
LIS2DW12_CTRL5_ADDR: 	64		// after writing
LIS2DW12_CTRL7_ADDR: 	0		// before writing
LIS2DW12_CTRL7_ADDR: 	32		// after writing

Am I doing something wrong? How could I write and read register successfully with the latest driver of LIS2DW12?

I would really appreciate if anyone could help me to get this LIS2DW12 driver working properly.

Regards,
Tero

Related