From a1e8cedf6b652573ddaff663226c20b3e22a81c3 Mon Sep 17 00:00:00 2001 From: PeterKietzmann Date: Tue, 20 Jan 2015 08:54:40 +0100 Subject: [PATCH] drivers/srf08: Acquire exclusive access to I2C bus --- drivers/srf08/srf08.c | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/drivers/srf08/srf08.c b/drivers/srf08/srf08.c index 2c178b82ff..dbf131b953 100644 --- a/drivers/srf08/srf08.c +++ b/drivers/srf08/srf08.c @@ -21,8 +21,6 @@ * @} */ - -#include #include #include #include "hwtimer.h" @@ -35,17 +33,27 @@ int srf08_init(srf08_t *dev, i2c_t i2c, uint8_t addr, i2c_speed_t speed) { + int status; + dev->i2c = i2c; dev->addr = addr; + /* Acquire exclusive access to the bus. */ + i2c_acquire(dev->i2c); /* initialize i2c interface */ - if(i2c_init_master(i2c, speed) < 0) { + status = i2c_init_master(dev->i2c, speed); + /* Release the bus for other threads. */ + i2c_release(dev->i2c); + + if(status < 0) { return -1; } + /* set the maximum range */ if (srf08_set_max_range(dev, SRF08_MAX_RANGE_6M) < 0) { return -3; } + /* set the maximum gain */ if (srf08_set_max_gain(dev, SRF08_MAX_GAIN) < 0) { return -4; @@ -57,13 +65,29 @@ int srf08_init(srf08_t *dev, i2c_t i2c, uint8_t addr, i2c_speed_t speed) int srf08_set_max_range(srf08_t *dev, uint8_t max_range) { - return i2c_write_reg(dev->i2c, dev->addr, SRF08_RANGE_REG, max_range); + int status; + + /* Acquire exclusive access to the bus. */ + i2c_acquire(dev->i2c); + status = i2c_write_reg(dev->i2c, dev->addr, SRF08_RANGE_REG, max_range); + /* Release the bus for other threads. */ + i2c_release(dev->i2c); + + return status; } int srf08_set_max_gain(srf08_t *dev, uint8_t gain) { - return i2c_write_reg(dev->i2c, dev->addr, SRF08_GAIN_REG, gain); + int status; + + /* Acquire exclusive access to the bus. */ + i2c_acquire(dev->i2c); + status = i2c_write_reg(dev->i2c, dev->addr, SRF08_GAIN_REG, gain); + /* Release the bus for other threads. */ + i2c_release(dev->i2c); + + return status; } @@ -75,8 +99,12 @@ int srf08_get_distances(srf08_t *dev, uint16_t *range_array, int num_echos, srf0 char register_location; char max_reg_no_read = (num_echos * sizeof(range_bytes)) +1; + /* Acquire exclusive access to the bus. */ + i2c_acquire(dev->i2c); /* set ranging mode */ status = i2c_write_reg(dev->i2c, dev->addr, SRF08_COMMAND_REG, ranging_mode); + /* Release the bus for other threads. */ + i2c_release(dev->i2c); if (!status) { DEBUG("Write the ranging command to the i2c-interface is failed"); @@ -94,8 +122,12 @@ int srf08_get_distances(srf08_t *dev, uint16_t *range_array, int num_echos, srf0 for (register_location = 2; register_location < max_reg_no_read; register_location += sizeof(range_bytes)) { + /* Acquire exclusive access to the bus. */ + i2c_acquire(dev->i2c); /* read the echo bytes */ status = i2c_read_regs(dev->i2c, dev->addr, register_location, range_bytes, sizeof(range_bytes)); + /* Release the bus for other threads. */ + i2c_release(dev->i2c); if (!status) { DEBUG("Read the echo bytes from the i2c-interface is failed");