diff --git a/drivers/bh1750fvi/bh1750fvi.c b/drivers/bh1750fvi/bh1750fvi.c index 2cba4f83d0..7f063a5b1a 100644 --- a/drivers/bh1750fvi/bh1750fvi.c +++ b/drivers/bh1750fvi/bh1750fvi.c @@ -25,6 +25,9 @@ #include "bh1750fvi.h" #include "bh1750fvi_internal.h" +#define ENABLE_DEBUG (0) +#include "debug.h" + int bh1750fvi_init(bh1750fvi_t *dev, bh1750fvi_params_t *params) { int res; @@ -41,9 +44,9 @@ int bh1750fvi_init(bh1750fvi_t *dev, bh1750fvi_params_t *params) res = i2c_write_byte(dev->i2c, dev->addr, OP_POWER_DOWN); i2c_release(dev->i2c); if (res < 0) { - return -1; + return BH1750FVI_ERR_I2C; } - return 0; + return BH1750FVI_OK; } uint16_t bh1750fvi_sample(bh1750fvi_t *dev) @@ -52,6 +55,7 @@ uint16_t bh1750fvi_sample(bh1750fvi_t *dev) uint8_t raw[2]; /* power on the device and send single H-mode measurement command */ + DEBUG("[bh1750fvi] sample: triggering a conversion\n"); i2c_acquire(dev->i2c); i2c_write_byte(dev->i2c, dev->addr, OP_POWER_ON); i2c_write_byte(dev->i2c, dev->addr, OP_SINGLE_HRES1); @@ -61,6 +65,7 @@ uint16_t bh1750fvi_sample(bh1750fvi_t *dev) xtimer_usleep(DELAY_HMODE); /* read the results */ + DEBUG("[bh1750fvi] sample: reading the results\n"); i2c_acquire(dev->i2c); i2c_read_bytes(dev->i2c, dev->addr, raw, 2); i2c_release(dev->i2c); diff --git a/drivers/include/bh1750fvi.h b/drivers/include/bh1750fvi.h index d46172988f..690ab60780 100644 --- a/drivers/include/bh1750fvi.h +++ b/drivers/include/bh1750fvi.h @@ -48,6 +48,14 @@ extern "C" { */ #define BH1750FVI_I2C_MAX_CLK I2C_SPEED_FAST +/** + * @brief Status and error return codes + */ +enum { + BH1750FVI_OK = 0, /**< everything was fine */ + BH1750FVI_ERR_I2C = -1 /**< error initializing the I2C bus */ +}; + /** * @brief Device descriptor for BH1570FVI devices */ diff --git a/tests/driver_bh1750/main.c b/tests/driver_bh1750/main.c index 0c276aa6bc..84448a582d 100644 --- a/tests/driver_bh1750/main.c +++ b/tests/driver_bh1750/main.c @@ -28,13 +28,18 @@ int main(void) { + int res; bh1750fvi_t dev; uint32_t last = xtimer_now(); puts("BH1750FVI ambient light sensor test\n"); /* initialize the device */ - bh1750fvi_init(&dev, (bh1750fvi_params_t *)(&bh1750fvi_params)); + res = bh1750fvi_init(&dev, (bh1750fvi_params_t *)(&bh1750fvi_params)); + if (res != BH1750FVI_OK) { + puts("error: unable to initialize sensor [I2C initialization error]"); + return 1; + } /* periodically sample the sensor */ while(1) {