diff --git a/drivers/l3g4200d/l3g4200d.c b/drivers/l3g4200d/l3g4200d.c index b770702c10..79bfab203d 100644 --- a/drivers/l3g4200d/l3g4200d.c +++ b/drivers/l3g4200d/l3g4200d.c @@ -14,12 +14,14 @@ * @brief Device driver implementation for the L3G4200D gyroscope * * @author Hauke Petersen + * @author Peter Kietzmann * * @} */ #include +#include "mutex.h" #include "l3g4200d.h" #include "l3g4200d-regs.h" #include "periph/i2c.h" @@ -38,10 +40,15 @@ int l3g4200d_init(l3g4200d_t *dev, i2c_t i2c, uint8_t address, { char tmp; + /* Acquire exclusive access to the bus. */ + i2c_acquire(dev->i2c); /* initialize the I2C bus */ if (i2c_init_master(i2c, I2C_SPEED) < 0) { + /* Release the bus for other threads. */ + i2c_release(dev->i2c); return -1; } + i2c_release(dev->i2c); /* write device descriptor */ dev->i2c = i2c; @@ -67,13 +74,17 @@ int l3g4200d_init(l3g4200d_t *dev, i2c_t i2c, uint8_t address, /* configure CTRL_REG1 */ tmp = ((mode & 0xf) << L3G4200D_CTRL1_MODE_POS) | L3G4200D_CTRL1_ALLON; + i2c_acquire(dev->i2c); if (i2c_write_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL1, tmp) != 1) { + i2c_release(dev->i2c); return -1; } tmp = ((scale & 0x3) << L3G4200D_CTRL4_FS_POS) | L3G4200D_CTRL4_BDU; if (i2c_write_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL4, tmp) != 1) { + i2c_release(dev->i2c); return -1; } + i2c_release(dev->i2c); return 0; } @@ -82,8 +93,10 @@ int l3g4200d_read(l3g4200d_t *dev, l3g4200d_data_t *data) char tmp[6]; int16_t res; + i2c_acquire(dev->i2c); /* get acceleration in x direction */ i2c_read_regs(dev->i2c, dev->addr, L3G4200D_REG_OUT_X_L | L3G4200D_AUTOINC, tmp, 6); + i2c_release(dev->i2c); /* parse and normalize data into result vector */ res = (tmp[1] << 8) | tmp[0]; @@ -100,14 +113,18 @@ int l3g4200d_enable(l3g4200d_t *dev) char tmp; int res; + i2c_acquire(dev->i2c); res = i2c_read_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL1, &tmp); if (res < 1) { + i2c_release(dev->i2c); return res; } tmp |= L3G4200D_CTRL1_PD; if (i2c_write_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL1, tmp) != 1) { + i2c_release(dev->i2c); return -1; } + i2c_release(dev->i2c); return 0; } @@ -116,13 +133,17 @@ int l3g4200d_disable(l3g4200d_t *dev) char tmp; int res; + i2c_acquire(dev->i2c); res = i2c_read_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL1, &tmp); if (res < 1) { + i2c_release(dev->i2c); return res; } tmp &= ~L3G4200D_CTRL1_PD; if (i2c_write_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL1, tmp) != 1) { + i2c_release(dev->i2c); return -1; } + i2c_release(dev->i2c); return 0; }