From d0f86d06115c200dcc655c2f56691b2030cf214a Mon Sep 17 00:00:00 2001 From: MrKevinWeiss Date: Wed, 27 Jun 2018 09:24:39 +0200 Subject: [PATCH] drivers/srf02: Update to new i2c API --- drivers/include/srf02.h | 11 ++++++-- drivers/srf02/srf02.c | 55 ++++++++++++++++++++++++--------------- tests/driver_srf02/main.c | 18 ++++++++++--- 3 files changed, 57 insertions(+), 27 deletions(-) diff --git a/drivers/include/srf02.h b/drivers/include/srf02.h index 4f8f7fa8d3..80eaa5c0dd 100644 --- a/drivers/include/srf02.h +++ b/drivers/include/srf02.h @@ -19,6 +19,7 @@ * @author Zakaria Kasmi * @author Peter Kietzmann * @author Hauke Petersen + * @author Kevin Weiss */ #ifndef SRF02_H @@ -87,8 +88,11 @@ int srf02_init(srf02_t *dev, i2c_t i2c, uint8_t addr); * the result in inches, centimeters or microseconds. * Another set of three fake ranging modes do the same * but without transmitting the burst + * + * @return 0 On success, otherwise i2c_write error + * */ -void srf02_trigger(const srf02_t *dev, srf02_mode_t mode); +int srf02_trigger(const srf02_t *dev, srf02_mode_t mode); /** * @brief Read the results of the last ranging operation @@ -122,8 +126,11 @@ uint16_t srf02_get_distance(const srf02_t *dev, srf02_mode_t mode); * * @param[in] dev device to program * @param[in] new_addr new address to program the given device to + * + * @return 0 On success, otherwise i2c_write error + * */ -void srf02_set_addr(srf02_t *dev, uint8_t new_addr); +int srf02_set_addr(srf02_t *dev, uint8_t new_addr); #ifdef __cplusplus } diff --git a/drivers/srf02/srf02.c b/drivers/srf02/srf02.c index b678326237..8a79a5c76f 100644 --- a/drivers/srf02/srf02.c +++ b/drivers/srf02/srf02.c @@ -17,6 +17,7 @@ * @author Zakaria Kasmi * @author Peter Kietzmann * @author Hauke Petersen + * @author Kevin Weiss * * @} */ @@ -31,11 +32,6 @@ #define ENABLE_DEBUG (0) #include "debug.h" -/** - * @brief Per default use normal speed on the I2C bus - */ -#define BUS_SPEED (I2C_SPEED_NORMAL) - /** * @brief SRF02 register addresses * @{ @@ -61,17 +57,13 @@ int srf02_init(srf02_t *dev, i2c_t i2c, uint8_t addr) { dev->i2c = i2c; dev->addr = (addr >> 1); /* internally we right align the 7-bit addr */ - uint8_t rev; + uint8_t rev = 0; /* Acquire exclusive access to the bus. */ i2c_acquire(dev->i2c); - /* initialize i2c interface */ - if (i2c_init_master(dev->i2c, BUS_SPEED) < 0) { - DEBUG("[srf02] error initializing I2C bus\n"); - return -1; - } + /* try to read the software revision (read the CMD reg) from the device */ - i2c_read_reg(i2c, dev->addr, REG_CMD, &rev); + i2c_read_reg(i2c, dev->addr, REG_CMD, &rev, 0); if (rev == 0 || rev == 255) { i2c_release(dev->i2c); DEBUG("[srf02] error reading the devices software revision\n"); @@ -86,22 +78,24 @@ int srf02_init(srf02_t *dev, i2c_t i2c, uint8_t addr) return 0; } -void srf02_trigger(const srf02_t *dev, srf02_mode_t mode) +int srf02_trigger(const srf02_t *dev, srf02_mode_t mode) { + int status; /* trigger a new measurement by writing the mode to the CMD register */ DEBUG("[srf02] trigger new reading\n"); i2c_acquire(dev->i2c); - i2c_write_reg(dev->i2c, dev->addr, REG_CMD, mode); + status = i2c_write_reg(dev->i2c, dev->addr, REG_CMD, mode, 0); i2c_release(dev->i2c); + return status; } uint16_t srf02_read(const srf02_t *dev) { - uint8_t res[2]; + uint8_t res[2] = {0xFF, 0xFF}; /* read the results */ i2c_acquire(dev->i2c); - i2c_read_regs(dev->i2c, dev->addr, REG_HIGH, res, 2); + i2c_read_regs(dev->i2c, dev->addr, REG_HIGH, res, 2, 0); i2c_release(dev->i2c); DEBUG("[srf02] result - high: 0x%02x low: 0x%02x\n", res[0], res[1]); @@ -119,20 +113,39 @@ uint16_t srf02_get_distance(const srf02_t *dev, srf02_mode_t mode) return srf02_read(dev); } -void srf02_set_addr(srf02_t *dev, uint8_t new_addr) +int srf02_set_addr(srf02_t *dev, uint8_t new_addr) { + int status; /* get access to the bus */ i2c_acquire(dev->i2c); DEBUG("[srf02] reprogramming device address to 0x%02x\n", (int)new_addr); /* write the new address, for this we need to follow a certain sequence */ - i2c_write_reg(dev->i2c, dev->addr, REG_CMD, CMD_ADDR_SEQ1); - i2c_write_reg(dev->i2c, dev->addr, REG_CMD, CMD_ADDR_SEQ2); - i2c_write_reg(dev->i2c, dev->addr, REG_CMD, CMD_ADDR_SEQ3); - i2c_write_reg(dev->i2c, dev->addr, REG_CMD, new_addr); + status = i2c_write_reg(dev->i2c, dev->addr, REG_CMD, CMD_ADDR_SEQ1, 0); + if (status != 0) { + i2c_release(dev->i2c); + return status; + } + status = i2c_write_reg(dev->i2c, dev->addr, REG_CMD, CMD_ADDR_SEQ2, 0); + if (status != 0) { + i2c_release(dev->i2c); + return status; + } + status = i2c_write_reg(dev->i2c, dev->addr, REG_CMD, CMD_ADDR_SEQ3, 0); + if (status != 0) { + i2c_release(dev->i2c); + return status; + } + status = i2c_write_reg(dev->i2c, dev->addr, REG_CMD, new_addr, 0); + if (status != 0) { + i2c_release(dev->i2c); + return status; + } + dev->addr = (new_addr >> 1); /* release the bus */ i2c_release(dev->i2c); + return status; } diff --git a/tests/driver_srf02/main.c b/tests/driver_srf02/main.c index 028b55a148..44547bb6c6 100644 --- a/tests/driver_srf02/main.c +++ b/tests/driver_srf02/main.c @@ -17,6 +17,7 @@ * @author Peter Kietzmann * @author Zakaria Kasmi * @author Hauke Petersen + * @author Kevin Weiss * * @} */ @@ -44,7 +45,12 @@ static srf02_t dev; static void sample(void) { uint16_t distance = srf02_get_distance(&dev, TEST_MODE); - printf("distance = %3i cm\n", distance); + if (distance != 0xFFFF) { + printf("distance = %3i cm\n", distance); + } + else { + printf("Sample failed!\n"); + } } static int cmd_init(int argc, char **argv) @@ -104,9 +110,13 @@ static int cmd_set_addr(int argc, char **argv) } new_addr = atoi(argv[1]); - srf02_set_addr(&dev, new_addr); - printf("Set address to %i\n", (int)new_addr); - return 0; + if (srf02_set_addr(&dev, new_addr) == 0) { + printf("Set address to %i\n", (int)new_addr); + return 0; + } + + printf("Set address failed\n"); + return 1; } static const shell_command_t shell_commands[] = {