From 2864cce6d7a097112af4db08c83bed0f0f68d44d Mon Sep 17 00:00:00 2001 From: PeterKietzmann Date: Thu, 29 Jan 2015 09:55:57 +0100 Subject: [PATCH] drivers/lps331ap: Acquire exclusice access to I2C bus --- drivers/lps331ap/lps331ap.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/drivers/lps331ap/lps331ap.c b/drivers/lps331ap/lps331ap.c index ad17c234a8..86179f77da 100644 --- a/drivers/lps331ap/lps331ap.c +++ b/drivers/lps331ap/lps331ap.c @@ -18,12 +18,14 @@ * used. * * @author Hauke Petersen + * @author Peter Kietzmann * * @} */ #include +#include "mutex.h" #include "periph/i2c.h" #include "lps331ap.h" #include "lps331ap-internal.h" @@ -53,8 +55,12 @@ int lps331ap_init(lps331ap_t *dev, i2c_t i2c, uint8_t address, lps331ap_rate_t r dev->i2c = i2c; dev->address = address; + /* Acquire exclusive access to the bus. */ + i2c_acquire(dev->i2c); /* initialize underlying I2C bus */ if (i2c_init_master(dev->i2c, BUS_SPEED) < 0) { + /* Release the bus for other threads. */ + i2c_release(dev->i2c); return -1; } @@ -62,8 +68,10 @@ int lps331ap_init(lps331ap_t *dev, i2c_t i2c, uint8_t address, lps331ap_rate_t r tmp = LPS331AP_CTRL_REG1_DBDU | LPS331AP_CTRL_REG1_PD | (rate << LPS331AP_CTRL_REG1_ODR_POS); if (i2c_write_reg(dev->i2c, dev->address, LPS331AP_REG_CTRL_REG1, tmp) != 1) { + i2c_release(dev->i2c); return -1; } + i2c_release(dev->i2c); return 0; } @@ -74,9 +82,11 @@ int lps331ap_read_temp(lps331ap_t *dev) int16_t val = 0; float res = TEMP_BASE; /* reference value -> see datasheet */ + i2c_acquire(dev->i2c); i2c_read_reg(dev->i2c, dev->address, LPS331AP_REG_TEMP_OUT_L, &tmp); val |= tmp; i2c_read_reg(dev->i2c, dev->address, LPS331AP_REG_TEMP_OUT_H, &tmp); + i2c_release(dev->i2c); val |= (tmp << 8); /* compute actual temperature value in °C */ @@ -92,11 +102,13 @@ int lps331ap_read_pres(lps331ap_t *dev) int32_t val = 0; float res; + i2c_acquire(dev->i2c); i2c_read_reg(dev->i2c, dev->address, LPS331AP_REG_PRESS_OUT_XL, &tmp); val |= tmp; i2c_read_reg(dev->i2c, dev->address, LPS331AP_REG_PRESS_OUT_L, &tmp); val |= (tmp << 8); i2c_read_reg(dev->i2c, dev->address, LPS331AP_REG_PRESS_OUT_H, &tmp); + i2c_release(dev->i2c); val |= (tmp << 16); /* see if value is negative */ if (tmp & 0x80) { @@ -113,19 +125,33 @@ int lps331ap_read_pres(lps331ap_t *dev) int lps331ap_enable(lps331ap_t *dev) { char tmp; + int status; + + i2c_acquire(dev->i2c); if (i2c_read_reg(dev->i2c, dev->address, LPS331AP_REG_CTRL_REG1, &tmp) != 1) { + i2c_release(dev->i2c); return -1; } tmp |= (LPS331AP_CTRL_REG1_PD); - return i2c_write_reg(dev->i2c, dev->address, LPS331AP_REG_CTRL_REG1, tmp); + status = i2c_write_reg(dev->i2c, dev->address, LPS331AP_REG_CTRL_REG1, tmp); + i2c_release(dev->i2c); + + return status; } int lps331ap_disable(lps331ap_t *dev) { char tmp; + int status; + + i2c_acquire(dev->i2c); if (i2c_read_reg(dev->i2c, dev->address, LPS331AP_REG_CTRL_REG1, &tmp) != 1) { + i2c_release(dev->i2c); return -1; } tmp &= ~(LPS331AP_CTRL_REG1_PD); - return i2c_write_reg(dev->i2c, dev->address, LPS331AP_REG_CTRL_REG1, tmp); + status = i2c_write_reg(dev->i2c, dev->address, LPS331AP_REG_CTRL_REG1, tmp); + i2c_release(dev->i2c); + + return status; }