mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-24 22:13:52 +01:00
drivers/l3g4200d: Acquire exclusive access to I2C bus
This commit is contained in:
parent
2864cce6d7
commit
9c3a89edbf
@ -14,12 +14,14 @@
|
||||
* @brief Device driver implementation for the L3G4200D gyroscope
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user