1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 10:12:45 +01:00

drivers/lsm303dlhc: Acquire exclusive access to I2C bus

This commit is contained in:
PeterKietzmann 2015-01-28 12:19:44 +01:00
parent 35750592f2
commit bf6757fe1d

View File

@ -14,10 +14,12 @@
* @brief Device driver implementation for the LSM303DLHC light sensor
*
* @author Thomas Eichinger <thomas.eichinger@fu-berlin.de>
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
*
* @}
*/
#include "mutex.h"
#include "lsm303dlhc.h"
#include "lsm303dlhc-internal.h"
@ -41,11 +43,15 @@ int lsm303dlhc_init(lsm303dlhc_t *dev, i2c_t i2c, gpio_t acc_pin, gpio_t mag_pin
dev->acc_pin = acc_pin;
dev->mag_pin = mag_pin;
/* Acquire exclusive access to the bus. */
i2c_acquire(dev->i2c);
i2c_init_master(i2c, I2C_SPEED_NORMAL);
DEBUG("lsm303dlhc reboot ");
res = i2c_write_reg(dev->i2c, dev->acc_address,
LSM303DLHC_REG_CTRL5_A, LSM303DLHC_REG_CTRL5_A_BOOT);
/* Release the bus for other threads. */
i2c_release(dev->i2c);
DEBUG("[OK]");
/* configure accelerometer */
@ -54,6 +60,7 @@ int lsm303dlhc_init(lsm303dlhc_t *dev, i2c_t i2c, gpio_t acc_pin, gpio_t mag_pin
| LSM303DLHC_CTRL1_A_YEN
| LSM303DLHC_CTRL1_A_ZEN
| acc_sample_rate);
i2c_acquire(dev->i2c);
res += i2c_write_reg(dev->i2c, dev->acc_address,
LSM303DLHC_REG_CTRL1_A, tmp);
/* update on read, MSB @ low address, scale and high-resolution */
@ -77,6 +84,7 @@ int lsm303dlhc_init(lsm303dlhc_t *dev, i2c_t i2c, gpio_t acc_pin, gpio_t mag_pin
/* set continuous mode */
res += i2c_write_reg(dev->i2c, dev->mag_address,
LSM303DLHC_REG_MR_M, LSM303DLHC_MAG_MODE_CONTINUOUS);
i2c_release(dev->i2c);
/* configure mag data ready pin */
gpio_init_in(mag_pin, GPIO_NOPULL);
@ -88,6 +96,7 @@ int lsm303dlhc_read_acc(lsm303dlhc_t *dev, lsm303dlhc_3d_data_t *data)
int res;
char tmp;
i2c_acquire(dev->i2c);
i2c_read_reg(dev->i2c, dev->acc_address, LSM303DLHC_REG_STATUS_A, &tmp);
DEBUG("lsm303dlhc status: %x\n", tmp);
DEBUG("lsm303dlhc: wait for acc values ... ");
@ -110,6 +119,7 @@ int lsm303dlhc_read_acc(lsm303dlhc_t *dev, lsm303dlhc_3d_data_t *data)
res += i2c_read_reg(dev->i2c, dev->acc_address,
LSM303DLHC_REG_OUT_Z_H_A, &tmp);
data->z_axis |= tmp<<8;
i2c_release(dev->i2c);
DEBUG("read ... ");
data->x_axis = data->x_axis>>4;
@ -134,8 +144,10 @@ int lsm303dlhc_read_mag(lsm303dlhc_t *dev, lsm303dlhc_3d_data_t *data)
DEBUG("read ... ");
i2c_acquire(dev->i2c);
res = i2c_read_regs(dev->i2c, dev->mag_address,
LSM303DLHC_REG_OUT_X_H_M, (char*)data, 6);
i2c_release(dev->i2c);
if (res < 6) {
DEBUG("[!!failed!!]\n");
@ -160,7 +172,9 @@ int lsm303dlhc_read_temp(lsm303dlhc_t *dev, int16_t *value)
{
int res;
i2c_acquire(dev->i2c);
res = i2c_read_regs(dev->i2c, dev->mag_address, LSM303DLHC_REG_TEMP_OUT_H, (char*)value, 2);
i2c_release(dev->i2c);
if (res < 2) {
return -1;
@ -176,12 +190,15 @@ int lsm303dlhc_read_temp(lsm303dlhc_t *dev, int16_t *value)
int lsm303dlhc_disable(lsm303dlhc_t *dev)
{
int res;
i2c_acquire(dev->i2c);
res = i2c_write_reg(dev->i2c, dev->acc_address,
LSM303DLHC_REG_CTRL1_A, LSM303DLHC_CTRL1_A_POWEROFF);
res += i2c_write_reg(dev->i2c, dev->mag_address,
LSM303DLHC_REG_MR_M, LSM303DLHC_MAG_MODE_SLEEP);
res += i2c_write_reg(dev->i2c, dev->acc_address,
LSM303DLHC_REG_CRA_M, LSM303DLHC_TEMP_DIS);
i2c_release(dev->i2c);
return (res < 3) ? -1 : 0;
}
@ -193,6 +210,7 @@ int lsm303dlhc_enable(lsm303dlhc_t *dev)
| LSM303DLHC_CTRL1_A_YEN
| LSM303DLHC_CTRL1_A_ZEN
| LSM303DLHC_CTRL1_A_N1344HZ_L5376HZ);
i2c_acquire(dev->i2c);
res = i2c_write_reg(dev->i2c, dev->acc_address, LSM303DLHC_REG_CTRL1_A, tmp);
tmp = (LSM303DLHC_CTRL4_A_BDU| LSM303DLHC_CTRL4_A_SCALE_2G | LSM303DLHC_CTRL4_A_HR);
@ -208,6 +226,7 @@ int lsm303dlhc_enable(lsm303dlhc_t *dev)
res += i2c_write_reg(dev->i2c, dev->mag_address,
LSM303DLHC_REG_MR_M, LSM303DLHC_MAG_MODE_CONTINUOUS);
i2c_release(dev->i2c);
gpio_init_in(dev->mag_pin, GPIO_NOPULL);