mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge pull request #10526 from OTAkeys/pr/adcxx1c_improve
drivers/adcxx1c: improve error handling
This commit is contained in:
commit
b291c638d3
@ -37,6 +37,7 @@
|
|||||||
int adcxx1c_init(adcxx1c_t *dev, const adcxx1c_params_t *params)
|
int adcxx1c_init(adcxx1c_t *dev, const adcxx1c_params_t *params)
|
||||||
{
|
{
|
||||||
assert(dev && params);
|
assert(dev && params);
|
||||||
|
int status;
|
||||||
|
|
||||||
dev->params = *params;
|
dev->params = *params;
|
||||||
dev->cb = NULL;
|
dev->cb = NULL;
|
||||||
@ -45,24 +46,28 @@ int adcxx1c_init(adcxx1c_t *dev, const adcxx1c_params_t *params)
|
|||||||
uint8_t reg = 0;
|
uint8_t reg = 0;
|
||||||
|
|
||||||
/* Test communication write and read configuration register */
|
/* Test communication write and read configuration register */
|
||||||
i2c_write_reg(I2C, ADDR, ADCXX1C_CONF_ADDR, CONF_TEST_VALUE, 0);
|
status = i2c_write_reg(I2C, ADDR, ADCXX1C_CONF_ADDR, CONF_TEST_VALUE, 0);
|
||||||
i2c_read_reg(I2C, ADDR, ADCXX1C_CONF_ADDR, ®, 0);
|
status += i2c_read_reg(I2C, ADDR, ADCXX1C_CONF_ADDR, ®, 0);
|
||||||
|
|
||||||
if (reg != CONF_TEST_VALUE) {
|
if (status < 0 || reg != CONF_TEST_VALUE) {
|
||||||
i2c_release(I2C);
|
i2c_release(I2C);
|
||||||
DEBUG("[adcxx1c] init - error: unable to communicate with the device (reg=%x)\n", reg);
|
DEBUG("[adcxx1c] init - error: unable to communicate with the device "
|
||||||
|
"(reg=%x)\n", reg);
|
||||||
return ADCXX1C_NODEV;
|
return ADCXX1C_NODEV;
|
||||||
}
|
}
|
||||||
|
|
||||||
reg = dev->params.cycle << 5;
|
reg = dev->params.cycle << 5;
|
||||||
i2c_write_reg(I2C, ADDR, ADCXX1C_CONF_ADDR, reg, 0);
|
status = i2c_write_reg(I2C, ADDR, ADCXX1C_CONF_ADDR, reg, 0);
|
||||||
i2c_release(I2C);
|
i2c_release(I2C);
|
||||||
|
if (status < 0) {
|
||||||
|
DEBUG("[adcxx1c] init - error: unable to communicate with the device "
|
||||||
|
"(err=%x)\n", status);
|
||||||
|
return ADCXX1C_NOI2C;
|
||||||
|
}
|
||||||
|
|
||||||
adcxx1c_set_alert_parameters(dev, dev->params.low_limit,
|
return adcxx1c_set_alert_parameters(dev, dev->params.low_limit,
|
||||||
dev->params.high_limit,
|
dev->params.high_limit,
|
||||||
dev->params.hysteresis);
|
dev->params.hysteresis);
|
||||||
|
|
||||||
return ADCXX1C_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int adcxx1c_read_raw(const adcxx1c_t *dev, int16_t *raw)
|
int adcxx1c_read_raw(const adcxx1c_t *dev, int16_t *raw)
|
||||||
@ -94,13 +99,19 @@ static void _alert_cb(void *arg)
|
|||||||
int adcxx1c_enable_alert(adcxx1c_t *dev, adcxx1c_cb_t cb, void *arg)
|
int adcxx1c_enable_alert(adcxx1c_t *dev, adcxx1c_cb_t cb, void *arg)
|
||||||
{
|
{
|
||||||
uint8_t reg;
|
uint8_t reg;
|
||||||
|
int status;
|
||||||
|
|
||||||
i2c_acquire(I2C);
|
i2c_acquire(I2C);
|
||||||
i2c_read_reg(I2C, ADDR, ADCXX1C_CONF_ADDR, ®, 0);
|
i2c_read_reg(I2C, ADDR, ADCXX1C_CONF_ADDR, ®, 0);
|
||||||
reg |= (dev->params.alert_pin != GPIO_UNDEF ? ADCXX1C_CONF_ALERT_PIN_EN : 0)
|
reg |= (dev->params.alert_pin != GPIO_UNDEF ? ADCXX1C_CONF_ALERT_PIN_EN : 0)
|
||||||
| ADCXX1C_CONF_ALERT_FLAG_EN;
|
| ADCXX1C_CONF_ALERT_FLAG_EN;
|
||||||
i2c_write_reg(I2C, ADDR, ADCXX1C_CONF_ADDR, reg, 0);
|
status = i2c_write_reg(I2C, ADDR, ADCXX1C_CONF_ADDR, reg, 0);
|
||||||
i2c_release(I2C);
|
i2c_release(I2C);
|
||||||
|
if (status < 0) {
|
||||||
|
DEBUG("[adcxx1c] enable_alert - error: unable to communicate with the "
|
||||||
|
"device (err=%d)\n", status);
|
||||||
|
return ADCXX1C_NOI2C;
|
||||||
|
}
|
||||||
|
|
||||||
if (dev->params.alert_pin != GPIO_UNDEF) {
|
if (dev->params.alert_pin != GPIO_UNDEF) {
|
||||||
dev->cb = cb;
|
dev->cb = cb;
|
||||||
@ -116,23 +127,42 @@ int adcxx1c_set_alert_parameters(const adcxx1c_t *dev, int16_t low_limit,
|
|||||||
int16_t high_limit, int16_t hysteresis)
|
int16_t high_limit, int16_t hysteresis)
|
||||||
{
|
{
|
||||||
uint8_t buf[2];
|
uint8_t buf[2];
|
||||||
|
int status;
|
||||||
|
|
||||||
i2c_acquire(I2C);
|
i2c_acquire(I2C);
|
||||||
|
|
||||||
low_limit <<= (12 - dev->params.bits);
|
low_limit <<= (12 - dev->params.bits);
|
||||||
buf[0] = low_limit >> 8;
|
buf[0] = low_limit >> 8;
|
||||||
buf[1] = low_limit & 0xFF;
|
buf[1] = low_limit & 0xFF;
|
||||||
i2c_write_regs(I2C, ADDR, ADCXX1C_LOW_LIMIT_ADDR, buf, 2, 0);
|
status = i2c_write_regs(I2C, ADDR, ADCXX1C_LOW_LIMIT_ADDR, buf, 2, 0);
|
||||||
|
if (status < 0) {
|
||||||
|
i2c_release(I2C);
|
||||||
|
DEBUG("[adcxx1c] set_alert (low limit) - error: unable to communicate "
|
||||||
|
"with the device (err=%d)\n", status);
|
||||||
|
return ADCXX1C_NOI2C;
|
||||||
|
}
|
||||||
|
|
||||||
high_limit <<= (12 - dev->params.bits);
|
high_limit <<= (12 - dev->params.bits);
|
||||||
buf[0] = high_limit >> 8;
|
buf[0] = high_limit >> 8;
|
||||||
buf[1] = high_limit & 0xFF;
|
buf[1] = high_limit & 0xFF;
|
||||||
i2c_write_regs(I2C, ADDR, ADCXX1C_HIGH_LIMIT_ADDR, buf, 2, 0);
|
status = i2c_write_regs(I2C, ADDR, ADCXX1C_HIGH_LIMIT_ADDR, buf, 2, 0);
|
||||||
|
if (status < 0) {
|
||||||
|
i2c_release(I2C);
|
||||||
|
DEBUG("[adcxx1c] set_alert (high limit) - error: unable to communicate "
|
||||||
|
"with the device (err=%d)\n", status);
|
||||||
|
return ADCXX1C_NOI2C;
|
||||||
|
}
|
||||||
|
|
||||||
hysteresis <<= (12 - dev->params.bits);
|
hysteresis <<= (12 - dev->params.bits);
|
||||||
buf[0] = hysteresis >> 8;
|
buf[0] = hysteresis >> 8;
|
||||||
buf[1] = hysteresis & 0xFF;
|
buf[1] = hysteresis & 0xFF;
|
||||||
i2c_write_regs(I2C, ADDR, ADCXX1C_HYSTERESIS_ADDR, buf, 2, 0);
|
status = i2c_write_regs(I2C, ADDR, ADCXX1C_HYSTERESIS_ADDR, buf, 2, 0);
|
||||||
|
if (status < 0) {
|
||||||
|
i2c_release(I2C);
|
||||||
|
DEBUG("[adcxx1c] set_alert (hysteresis) - error: unable to communicate "
|
||||||
|
"with the device (err=%d)\n", status);
|
||||||
|
return ADCXX1C_NOI2C;
|
||||||
|
}
|
||||||
|
|
||||||
i2c_release(I2C);
|
i2c_release(I2C);
|
||||||
|
|
||||||
|
@ -26,7 +26,9 @@
|
|||||||
|
|
||||||
static int read_adc(const void *dev, phydat_t *res)
|
static int read_adc(const void *dev, phydat_t *res)
|
||||||
{
|
{
|
||||||
adcxx1c_read_raw((const adcxx1c_t *)dev, res->val);
|
if (adcxx1c_read_raw((const adcxx1c_t *)dev, &res->val[0])) {
|
||||||
|
return -ECANCELED;
|
||||||
|
}
|
||||||
|
|
||||||
res->unit = UNIT_NONE;
|
res->unit = UNIT_NONE;
|
||||||
res->scale = 0;
|
res->scale = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user