Skip to content

Commit

Permalink
Merge pull request #9165 from ZetaR60/RIOT_ads101x_i2c_api
Browse files Browse the repository at this point in the history
drivers/ads101x: update I2C API
  • Loading branch information
dylad committed Jul 11, 2018
2 parents babecc6 + 6332585 commit e6debb5
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 35 deletions.
60 changes: 31 additions & 29 deletions drivers/ads101x/ads101x.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2017 OTA keys S.A.
* 2018 Matthew Blue <[email protected]>
* 2018 Acutam Automation, LLC
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
Expand Down Expand Up @@ -35,8 +35,6 @@
#define ADS101X_READ_DELAY (8 * US_PER_MS) /* Compatible with 128SPS */
#endif

#define I2C_SPEED I2C_SPEED_FAST

#define I2C (dev->params.i2c)
#define ADDR (dev->params.addr)

Expand Down Expand Up @@ -67,24 +65,31 @@ int ads101x_alert_init(ads101x_alert_t *dev,
return _ads101x_init_test(I2C, ADDR);
}

int _ads101x_init_test(i2c_t i2c, uint8_t addr)
static int _ads101x_init_test(i2c_t i2c, uint8_t addr)
{
uint8_t regs[2];

/* Acquire test */
i2c_acquire(i2c);
if (i2c_init_master(i2c, I2C_SPEED) < 0) {

/* Register read test */
if (i2c_read_regs(i2c, addr, ADS101X_CONF_ADDR, &regs, 2, 0x0) < 0) {
DEBUG("[ads101x] init - error: unable to read reg %x\n",
ADS101X_CONF_ADDR);
i2c_release(i2c);
DEBUG("[ads101x] init - error: unable to initialize I2C bus\n");
return ADS101X_NOI2C;
return ADS101X_NODEV;
}

/* Register read/write test */
i2c_read_regs(i2c, addr, ADS101X_CONF_ADDR, &regs, 2);
regs[1] = (regs[1] & ~ADS101X_DATAR_MASK) | ADS101X_DATAR_3300;

i2c_write_regs(i2c, addr, ADS101X_CONF_ADDR, &regs, 2);
i2c_read_regs(i2c, addr, ADS101X_CONF_ADDR, &regs, 2);
/* Register write test */
if (i2c_write_regs(i2c, addr, ADS101X_CONF_ADDR, &regs, 2, 0x0) < 0) {
DEBUG("[ads101x] init - error: unable to write reg %x\n",
ADS101X_CONF_ADDR);
i2c_release(i2c);
return ADS101X_NODEV;
}

i2c_read_regs(i2c, addr, ADS101X_CONF_ADDR, &regs, 2, 0x0);

i2c_release(i2c);

Expand All @@ -103,7 +108,7 @@ int ads101x_set_mux_gain(const ads101x_t *dev, uint8_t mux_gain)

i2c_acquire(I2C);

i2c_read_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2);
i2c_read_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2, 0x0);

/* Zero mux and gain */
regs[0] &= ~ADS101X_MUX_MASK;
Expand All @@ -112,7 +117,7 @@ int ads101x_set_mux_gain(const ads101x_t *dev, uint8_t mux_gain)
/* Write mux and gain */
regs[0] |= mux_gain;

i2c_write_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2);
i2c_write_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2, 0x0);

i2c_release(I2C);

Expand All @@ -122,30 +127,27 @@ int ads101x_set_mux_gain(const ads101x_t *dev, uint8_t mux_gain)
int ads101x_read_raw(const ads101x_t *dev, int16_t *raw)
{
uint8_t regs[2];
int status;

i2c_acquire(I2C);

/* Read control register */
i2c_read_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2);
i2c_read_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2, 0x0);

/* Tell the ADC to aquire a single-shot sample */
regs[0] |= ADS101X_CONF_OS_CONV;
i2c_write_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2);
i2c_write_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2, 0x0);

/* Wait for the sample to be aquired */
xtimer_usleep(ADS101X_READ_DELAY);

/* Read the sample */
status = i2c_read_regs(I2C, ADDR, ADS101X_CONV_RES_ADDR, &regs, 2);

i2c_release(I2C);

/* Status should equal bytes asked for (2) */
if (status != 2) {
if (i2c_read_regs(I2C, ADDR, ADS101X_CONV_RES_ADDR, &regs, 2, 0x0) < 0) {
i2c_release(I2C);
return ADS101X_NODATA;
}

i2c_release(I2C);

/* If all okay, change raw value */
*raw = (int16_t)(regs[0] << 8) | (int16_t)(regs[1]);

Expand All @@ -163,11 +165,11 @@ int ads101x_enable_alert(ads101x_alert_t *dev,

/* Read control register */
i2c_acquire(I2C);
i2c_read_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2);
i2c_read_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2, 0x0);

/* Enable alert comparator */
regs[1] &= ~ADS101X_CONF_COMP_DIS;
i2c_write_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2);
i2c_write_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2, 0x0);

i2c_release(I2C);

Expand All @@ -189,15 +191,15 @@ int ads101x_set_alert_parameters(const ads101x_alert_t *dev,
/* Set up low_limit */
regs[0] = (uint8_t)(low_limit >> 8);
regs[1] = (uint8_t)low_limit;
i2c_write_regs(I2C, ADDR, ADS101X_LOW_LIMIT_ADDR, &regs, 2);
i2c_write_regs(I2C, ADDR, ADS101X_LOW_LIMIT_ADDR, &regs, 2, 0x0);

/* Set up high_limit */
regs[0] = (uint8_t)(high_limit >> 8);
regs[1] = (uint8_t)high_limit;
i2c_write_regs(I2C, ADDR, ADS101X_HIGH_LIMIT_ADDR, &regs, 2);
i2c_write_regs(I2C, ADDR, ADS101X_HIGH_LIMIT_ADDR, &regs, 2, 0x0);

/* Read control register */
i2c_read_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2);
i2c_read_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2, 0x0);

/* Set up window mode */
if (low_limit != 0) {
Expand All @@ -208,7 +210,7 @@ int ads101x_set_alert_parameters(const ads101x_alert_t *dev,
/* Disable window mode */
regs[1] &= ~ADS101X_CONF_COMP_MODE_WIND;
}
i2c_write_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2);
i2c_write_regs(I2C, ADDR, ADS101X_CONF_ADDR, &regs, 2, 0x0);

i2c_release(I2C);

Expand Down
2 changes: 1 addition & 1 deletion drivers/ads101x/ads101x_saul.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2017 OTA keys S.A.
* 2018 Matthew Blue <[email protected]>
* 2018 Acutam Automation, LLC
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
Expand Down
2 changes: 1 addition & 1 deletion drivers/ads101x/include/ads101x_params.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2017 OTA keys S.A.
* 2018 Matthew Blue <[email protected]>
* 2018 Acutam Automation, LLC
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
Expand Down
2 changes: 1 addition & 1 deletion drivers/ads101x/include/ads101x_regs.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2017 OTA keys S.A.
* 2018 Matthew Blue <[email protected]>
* 2018 Acutam Automation, LLC
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
Expand Down
2 changes: 1 addition & 1 deletion drivers/include/ads101x.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2017 OTA keys S.A.
* 2018 Matthew Blue <[email protected]>
* 2018 Acutam Automation, LLC
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
Expand Down
2 changes: 1 addition & 1 deletion sys/auto_init/saul/auto_init_ads101x.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2017 OTA keys S.A.
* 2018 Matthew Blue <[email protected]>
* 2018 Acutam Automation, LLC
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
Expand Down
2 changes: 1 addition & 1 deletion tests/driver_ads101x/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2017 OTA keys S.A.
* 2018 Matthew Blue <[email protected]>
* 2018 Acutam Automation, LLC
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
Expand Down

0 comments on commit e6debb5

Please sign in to comment.