diff --git a/boards/iot-lab_M3/include/board.h b/boards/iot-lab_M3/include/board.h index be7032f9f3d2382361546329e409e22d7d6107f0..3e1cf2aaabefff3650d89a8bf9e898f53d246274 100644 --- a/boards/iot-lab_M3/include/board.h +++ b/boards/iot-lab_M3/include/board.h @@ -111,8 +111,8 @@ extern "C" { * @{ */ #define LSM303DLHC_I2C I2C_0 -#define LSM303DLHC_ACC_ADDR (25) -#define LSM303DLHC_MAG_ADDR (30) +#define LSM303DLHC_ACC_ADDR (0x19) +#define LSM303DLHC_MAG_ADDR (0x1e) #define LSM303DLHC_INT1 GPIO_5 #define LSM303DLHC_INT2 GPIO_6 #define LSM303DLHC_DRDY GPIO_7 diff --git a/drivers/include/lsm303dlhc.h b/drivers/include/lsm303dlhc.h index 1425135f03c86aee042c3b466042fe12cf99b393..50126b808fca886444e5bdee54e7301d30b5bdf8 100644 --- a/drivers/include/lsm303dlhc.h +++ b/drivers/include/lsm303dlhc.h @@ -51,6 +51,8 @@ typedef struct { i2c_t i2c; /**< I2C device */ uint8_t acc_address; /**< accelerometer's I2C address */ uint8_t mag_address; /**< magnetometer's I2C address */ + gpio_t acc_pin; /**< accelerometer's data ready pin */ + gpio_t mag_pin; /**< magnetometer's data ready pin */ } lsm303dlhc_t; /** diff --git a/drivers/lsm303dlhc/lsm303dlhc.c b/drivers/lsm303dlhc/lsm303dlhc.c index b9854574122831138b037ed89836568d8f74e5d6..d4963843c5c7ea228978faa06c6baa6df228d810 100644 --- a/drivers/lsm303dlhc/lsm303dlhc.c +++ b/drivers/lsm303dlhc/lsm303dlhc.c @@ -38,6 +38,8 @@ int lsm303dlhc_init(lsm303dlhc_t *dev, i2c_t i2c, gpio_t acc_pin, gpio_t mag_pin dev->i2c = i2c; dev->acc_address = acc_address; dev->mag_address = mag_address; + dev->acc_pin = acc_pin; + dev->mag_pin = mag_pin; i2c_init_master(i2c, I2C_SPEED_NORMAL); @@ -110,6 +112,10 @@ int lsm303dlhc_read_acc(lsm303dlhc_t *dev, lsm303dlhc_3d_data_t *data) data->z_axis |= tmp<<8; DEBUG("read ... "); + data->x_axis = data->x_axis>>4; + data->y_axis = data->y_axis>>4; + data->z_axis = data->z_axis>>4; + if (res < 6) { DEBUG("[!!failed!!]\n"); return -1; @@ -124,7 +130,7 @@ int lsm303dlhc_read_mag(lsm303dlhc_t *dev, lsm303dlhc_3d_data_t *data) int res; DEBUG("lsm303dlhc: wait for mag values... "); - while (gpio_read(GPIO_8) == 0){} + while (gpio_read(dev->mag_pin) == 0){} DEBUG("read ... "); @@ -192,7 +198,7 @@ int lsm303dlhc_enable(lsm303dlhc_t *dev) tmp = (LSM303DLHC_CTRL4_A_BDU| LSM303DLHC_CTRL4_A_SCALE_2G | LSM303DLHC_CTRL4_A_HR); res += i2c_write_reg(dev->i2c, dev->acc_address, LSM303DLHC_REG_CTRL4_A, tmp); res += i2c_write_reg(dev->i2c, dev->acc_address, LSM303DLHC_REG_CTRL3_A, LSM303DLHC_CTRL3_A_I1_DRDY1); - gpio_init_in(GPIO_2, GPIO_NOPULL); + gpio_init_in(dev->acc_pin, GPIO_NOPULL); tmp = LSM303DLHC_TEMP_EN | LSM303DLHC_TEMP_SAMPLE_75HZ; res += i2c_write_reg(dev->i2c, dev->mag_address, LSM303DLHC_REG_CRA_M, tmp); @@ -203,7 +209,7 @@ int lsm303dlhc_enable(lsm303dlhc_t *dev) res += i2c_write_reg(dev->i2c, dev->mag_address, LSM303DLHC_REG_MR_M, LSM303DLHC_MAG_MODE_CONTINUOUS); - gpio_init_in(GPIO_8, GPIO_NOPULL); + gpio_init_in(dev->mag_pin, GPIO_NOPULL); return (res < 6) ? -1 : 0; } diff --git a/examples/default/Makefile b/examples/default/Makefile index 599984c90683c7262142b2dc1d42598dcffc6c74..7b86bf3fe7a634751fe3ffeed825caa277beb0b6 100644 --- a/examples/default/Makefile +++ b/examples/default/Makefile @@ -59,6 +59,7 @@ ifneq (,$(filter iot-lab_M3,$(BOARD))) USEMODULE += isl29020 USEMODULE += lps331ap USEMODULE += l3g4200d + USEMODULE += lsm303dlhc endif include $(RIOTBASE)/Makefile.include diff --git a/sys/shell/commands/Makefile b/sys/shell/commands/Makefile index b78a6a7e346653dd83acde8cd17e5d190f63719e..49dd87cc86251cb72c064a79ed146ce1e2c7f50d 100644 --- a/sys/shell/commands/Makefile +++ b/sys/shell/commands/Makefile @@ -52,5 +52,8 @@ endif ifneq (,$(filter l3g4200d,$(USEMODULE))) SRC += sc_l3g4200d.c endif +ifneq (,$(filter lsm303dlhc,$(USEMODULE))) + SRC += sc_lsm303dlhc.c +endif include $(RIOTBASE)/Makefile.base diff --git a/sys/shell/commands/sc_lsm303dlhc.c b/sys/shell/commands/sc_lsm303dlhc.c new file mode 100644 index 0000000000000000000000000000000000000000..98470177df949ff481badbaa5bdb76c60ee82ca2 --- /dev/null +++ b/sys/shell/commands/sc_lsm303dlhc.c @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2014 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup shell_commands + * @{ + * + * @file + * @brief provides shell commands to poll lsm303dlhc sensor + * + * @author Thomas Eichinger <thomas.eichinger@fu-berlin.de> + * + * @} + */ + +#include <stdio.h> +#include "board.h" +#include "lsm303dlhc.h" + +#ifdef MODULE_LSM303DLHC + +#define ACC_S_RATE LSM303DLHC_ACC_SAMPLE_RATE_10HZ +#define ACC_SCALE LSM303DLHC_ACC_SCALE_2G +#define MAG_S_RATE LSM303DLHC_MAG_SAMPLE_RATE_75HZ +#define MAG_GAIN LSM303DLHC_MAG_GAIN_400_355_GAUSS + +static lsm303dlhc_t lsm303_dev; + +void _get_lsm303dlhc_init_handler(int argc, char **argv) +{ + (void)argc; + (void)argv; + + uint8_t error; + + error = lsm303dlhc_init(&lsm303_dev, LSM303DLHC_I2C, + LSM303DLHC_INT1, LSM303DLHC_DRDY, + LSM303DLHC_ACC_ADDR, ACC_S_RATE, ACC_SCALE, + LSM303DLHC_MAG_ADDR, MAG_S_RATE, MAG_GAIN); + + if (error) { + puts("Error initializing lsm303dlhc sensor."); + } + else { + puts("Initialized lsm303dlhc sensor with default values"); + } +} + +void _get_lsm303dlhc_read_handler(int argc, char **argv) +{ + (void)argc; + (void)argv; + + uint8_t error; + lsm303dlhc_3d_data_t data; + + if (!lsm303_dev.acc_address || !lsm303_dev.mag_address) { + puts("Error: please call `lsm303dlhc_init` first!"); + return; + } + + error = lsm303dlhc_read_acc(&lsm303_dev, &data); + if (error) { + puts("Error reading accelerometer data from lsm303dlhc."); + return; + } + else { + printf("lsm303dlhc: Accelerometer {%i, %i, %i} mg\n", data.x_axis, data.y_axis, data.z_axis); + } + + error = lsm303dlhc_read_mag(&lsm303_dev, &data); + if (error) { + puts("Error reading magnetometer data from lsm303dlhc."); + return; + } + else { + printf("lsm303dlhc: Magnetometer {%i, %i, %i}/1100 gauss\n", data.x_axis, data.y_axis, data.z_axis); + } + + error = lsm303dlhc_read_temp(&lsm303_dev, &(data.x_axis)); + if (error) { + puts("Error reading temperature data from lsm303dlhc."); + return; + } + else { + printf("lsm303dlhc: Temperature %i\n", data.x_axis); + } +} + +#endif /* MODULE_LSM303DLHC */ diff --git a/sys/shell/commands/shell_commands.c b/sys/shell/commands/shell_commands.c index d0181146637c1a2ad8a6984c8f5d49f9a34b4787..6173eead200870952e9ac7e34d22cd7173d4f584 100644 --- a/sys/shell/commands/shell_commands.c +++ b/sys/shell/commands/shell_commands.c @@ -64,6 +64,11 @@ extern void _get_l3g4200d_init_handler(int argc, char **argv); extern void _get_l3g4200d_read_handler(int argc, char **argv); #endif +#ifdef MODULE_LSM303DLHC +extern void _get_lsm303dlhc_init_handler(int argc, char **argv); +extern void _get_lsm303dlhc_read_handler(int argc, char **argv); +#endif + #ifdef MODULE_LTC4150 extern void _get_current_handler(int argc, char **argv); extern void _reset_current_handler(int argc, char **argv); @@ -172,6 +177,10 @@ const shell_command_t _shell_command_list[] = { {"l3g4200d_init", "Initializes the l3g4200d sensor driver.", _get_l3g4200d_init_handler}, {"l3g4200d_read", "Prints data from the l3g4200d sensor.", _get_l3g4200d_read_handler}, #endif +#ifdef MODULE_LSM303DLHC + {"lsm303dlhc_init", "Initializes the lsm303dlhc sensor driver.", _get_lsm303dlhc_init_handler}, + {"lsm303dlhc_read", "Prints data from the lsm303dlhc sensor.", _get_lsm303dlhc_read_handler}, +#endif #ifdef MODULE_LTC4150 {"cur", "Prints current and average power consumption.", _get_current_handler}, {"rstcur", "Resets coulomb counter.", _reset_current_handler},