mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
drivers/lps331ap: Acquire exclusice access to I2C bus
This commit is contained in:
parent
0a9e837dba
commit
2864cce6d7
@ -18,12 +18,14 @@
|
||||
* used.
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user