SensorAPI - BNO055 Add external crystal support
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/33253b22 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/33253b22 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/33253b22 Branch: refs/heads/develop Commit: 33253b22f303eb924e9656a86327f4f3e5b140f6 Parents: 5a76b68 Author: Vipul Rahane <[email protected]> Authored: Mon Feb 13 11:34:26 2017 -0800 Committer: Vipul Rahane <[email protected]> Committed: Mon Feb 13 11:34:26 2017 -0800 ---------------------------------------------------------------------- apps/slinky/src/main.c | 2 +- hw/drivers/sensors/bno055/src/bno055.c | 82 ++++++++++++++++++++++-- hw/drivers/sensors/bno055/src/bno055_priv.h | 5 ++ 3 files changed, 84 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/33253b22/apps/slinky/src/main.c ---------------------------------------------------------------------- diff --git a/apps/slinky/src/main.c b/apps/slinky/src/main.c index aa58fcb..2bdcc76 100755 --- a/apps/slinky/src/main.c +++ b/apps/slinky/src/main.c @@ -292,7 +292,7 @@ config_sensor(void) #if MYNEWT_VAL(BNO055_PRESENT) struct bno055_cfg bcfg; - dev = (struct os_dev *) os_dev_open("accel0", OS_TIMEOUT_NEVER, NULL); + dev = (struct os_dev *) os_dev_open("accel1", OS_TIMEOUT_NEVER, NULL); assert(dev != NULL); rc = bno055_init(dev, NULL); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/33253b22/hw/drivers/sensors/bno055/src/bno055.c ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/bno055/src/bno055.c b/hw/drivers/sensors/bno055/src/bno055.c index 665db28..b74fcb2 100644 --- a/hw/drivers/sensors/bno055/src/bno055.c +++ b/hw/drivers/sensors/bno055/src/bno055.c @@ -160,7 +160,7 @@ bno055_read8(uint8_t reg, uint8_t *value) /* Register write */ payload = reg; rc = hal_i2c_master_write(MYNEWT_VAL(BNO055_I2CBUS), &data_struct, - OS_TICKS_PER_SEC / 10, 1); + OS_TICKS_PER_SEC / 10, 0); if (rc) { BNO055_ERR("I2C register write failed at address 0x%02X:0x%02X\n", data_struct.address, reg); @@ -366,6 +366,62 @@ err: return rc; } +/** + * Use external crystal 32.768KHz + * + * @return 0 on success, non-zero on failure + */ +static int +bno055_set_ext_xtal_use(uint8_t use_xtal) +{ + int rc; + uint8_t prev_mode; + + prev_mode = g_bno055_mode; + + /* Switch to config mode */ + rc = bno055_set_mode(BNO055_OPERATION_MODE_CONFIG); + if (rc) { + goto err; + } + + os_time_delay(OS_TICKS_PER_SEC/1000 * 25); + + rc = bno055_write8(BNO055_PAGE_ID_ADDR, 0); + if (rc) { + goto err; + } + + if (use_xtal) { + /* Use External Clock */ + rc = bno055_write8(BNO055_SYS_TRIGGER_ADDR, BNO055_SYS_TRIGGER_CLK_SEL); + if (rc) { + goto err; + } + } else { + /* Use Internal clock */ + rc = bno055_write8(BNO055_SYS_TRIGGER_ADDR, 0x00); + if (rc) { + goto err; + } + } + + os_time_delay(OS_TICKS_PER_SEC/1000 * 10); + + /* Reset to previous operating mode */ + rc = bno055_set_mode(prev_mode); + if (rc) { + goto err; + } + + os_time_delay(OS_TICKS_PER_SEC/1000 * 20); + + return 0; +err: + return rc; +} + + int bno055_config(struct bno055 *bno055, struct bno055_cfg *cfg) { @@ -375,6 +431,16 @@ bno055_config(struct bno055 *bno055, struct bno055_cfg *cfg) prev_mode = g_bno055_mode; + /** + * As per Section 5.5 in the BNO055 Datasheet, + * external crystal should be used for accurate + * results + */ + rc = bno055_set_ext_xtal_use(1); + if (rc) { + goto err; + } + /* Check if we can read the chip address */ rc = bno055_read8(BNO055_CHIP_ID_ADDR, &id); if (rc) { @@ -390,6 +456,7 @@ bno055_config(struct bno055 *bno055, struct bno055_cfg *cfg) } if(id != BNO055_ID) { + rc = SYS_EINVAL; goto err; } } @@ -430,8 +497,6 @@ bno055_config(struct bno055 *bno055, struct bno055_cfg *cfg) goto err; } - os_time_delay(OS_TICKS_PER_SEC/1000 * 20); - return 0; err: return rc; @@ -514,6 +579,13 @@ bno055_find_reg(sensor_type_t type, uint8_t *reg) return rc; } +/** + * Get vector data from sensor + * + * @param pointer to teh structure to be filled up + * @param Type of sensor + * @return 0 on success, non-zero on error + */ int bno055_get_vector_data(void *datastruct, int type) { @@ -592,7 +664,8 @@ err: /** * Get temperature from bno055 sensor * - * @return temperature in degree celcius + * @param pointer to the temperature variable to be filled up + * @return 0 on success, non-zero on error */ int bno055_get_temp(int8_t *temp) @@ -826,3 +899,4 @@ bno055_sensor_get_config(struct sensor *sensor, sensor_type_t type, err: return (rc); } + http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/33253b22/hw/drivers/sensors/bno055/src/bno055_priv.h ---------------------------------------------------------------------- diff --git a/hw/drivers/sensors/bno055/src/bno055_priv.h b/hw/drivers/sensors/bno055/src/bno055_priv.h index 3a2da68..295e83b 100644 --- a/hw/drivers/sensors/bno055/src/bno055_priv.h +++ b/hw/drivers/sensors/bno055/src/bno055_priv.h @@ -107,6 +107,11 @@ #define BNO055_PWR_MODE_ADDR 0X3E #define BNO055_SYS_TRIGGER_ADDR 0X3F +#define BNO055_SYS_TRIGGER_CLK_SEL (0x01 << 7) +#define BNO055_SYS_TRIGGER_RST_INT (0x01 << 6) +#define BNO055_SYS_TRIGGER_RST_SYS (0x01 << 5) +#define BNO055_SYS_TRIGGER_SELF_TEST (0x01) + #define BNO055_TEMP_SOURCE_ADDR 0X40 /* Axis remap registers */
