diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index 9f4d2386..413edc56 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -339,6 +339,7 @@ add_example (tmp006) add_example (mma8x5x) add_example (mag3110) add_example (hdc1000) +add_example (bmg160) # These are special cases where you specify example binary, source file and module(s) include_directories (${PROJECT_SOURCE_DIR}/src) @@ -365,7 +366,6 @@ add_custom_example (light-sensor-example-cxx light-sensor.cxx "si1132;max44009") add_custom_example (light-controller-example-cxx light-controller.cxx "lp8860;ds1808lc;hlg150h") add_custom_example (bme280-example-cxx bme280.cxx bmp280) add_custom_example (bma250e-example-cxx bma250e.cxx bmx055) -add_custom_example (bmg160-example-cxx bmg160.cxx bmx055) add_custom_example (bmm150-example-cxx bmm150.cxx bmx055) add_custom_example (bmc150-example-cxx bmc150.cxx bmx055) add_custom_example (bmi055-example-cxx bmi055.cxx bmx055) diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index fa952e51..56133308 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -153,6 +153,7 @@ add_example (bno055) add_example (bmp280) add_example (abpdrrt005pg2a5) add_example (lcdks) +add_example (bmg160) # Custom examples add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) diff --git a/examples/c/bmg160.c b/examples/c/bmg160.c new file mode 100644 index 00000000..09e725af --- /dev/null +++ b/examples/c/bmg160.c @@ -0,0 +1,99 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2017 Intel Corporation. + * + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include + +#include "upm_utilities.h" +#include "bmg160.h" + +bool shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + + +int main(int argc, char **argv) +{ + signal(SIGINT, sig_handler); +//! [Interesting] + +#if defined(CONFIG_BOARD_ARDUINO_101_SSS) + // ARDUINO_101_SSS (ARC core) must use I2C + // Instantiate a BMG160 instance using default i2c bus and address + bmg160_context sensor = bmg160_init(BMG160_DEFAULT_I2C_BUS, + BMG160_DEFAULT_ADDR, -1); +#elif defined(CONFIG_BOARD_ARDUINO_101) + // ARDUINO_101 (Quark core) where you must use SPI + // Instantiate a BMG160 instance using default SPI bus and pin 10 as CS + bmg160_context sensor = bmg160_init(BMG160_DEFAULT_SPI_BUS, + -1, 10); +#else + // everything else use I2C by default + // Instantiate a BMG160 instance using default i2c bus and address + bmg160_context sensor = bmg160_init(BMG160_DEFAULT_I2C_BUS, + BMG160_DEFAULT_ADDR, -1); +#endif + + if (!sensor) + { + printf("bmg160_init() failed.\n"); + return 1; + } + + // now output data every 250 milliseconds + while (shouldRun) + { + float x, y, z; + + if (bmg160_update(sensor)) + { + printf("bmg160_update() failed\n"); + return 1; + } + + bmg160_get_gyroscope(sensor, &x, &y, &z); + printf("Gyroscope x: %f y: %f z: %f degrees/s\n", + x, y, z); + + printf("Compensation Temperature: %f C\n\n", + bmg160_get_temperature(sensor)); + + upm_delay_ms(250); + } + + printf("Exiting...\n"); + + bmg160_close(sensor); + +//! [Interesting] + + return 0; +} diff --git a/examples/java/BMG160_Example.java b/examples/java/BMG160_Example.java index 09667b94..aff97c4d 100644 --- a/examples/java/BMG160_Example.java +++ b/examples/java/BMG160_Example.java @@ -1,6 +1,6 @@ /* * Author: Jon Trulson - * Copyright (c) 2016 Intel Corporation. + * Copyright (c) 2016-2017 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -22,7 +22,7 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import upm_bmx055.BMG160; +import upm_bmg160.BMG160; public class BMG160_Example { @@ -42,11 +42,11 @@ public class BMG160_Example // update our values from the sensor sensor.update(); - float dataA[] = sensor.getGyroscope(); + upm_bmg160.floatVector data = sensor.getGyroscope(); - System.out.println("Gyroscope x: " + dataA[0] - + " y: " + dataA[1] - + " z: " + dataA[2] + System.out.println("Gyroscope x: " + data.get(0) + + " y: " + data.get(1) + + " z: " + data.get(2) + " degrees/s"); System.out.println("Compensation Temperature: " diff --git a/examples/java/CMakeLists.txt b/examples/java/CMakeLists.txt index abc73534..98f1adce 100644 --- a/examples/java/CMakeLists.txt +++ b/examples/java/CMakeLists.txt @@ -166,6 +166,7 @@ add_example(MAX30100_Example max30100) add_example(Ads1115Sample ads1x15) add_example(SensorTemplateSample sensortemplate) add_example(P9813Sample p9813) +add_example(BMG160_Example bmg160) add_example_with_path(Jhd1313m1_lcdSample jhd1313m1 jhd1313m1) add_example_with_path(Jhd1313m1Sample jhd1313m1 jhd1313m1) @@ -180,7 +181,6 @@ if(SWIG_VERSION VERSION_GREATER 3.0.8) endif() add_example_with_path(BMA250E_Example bmx055 bmx055) -add_example_with_path(BMG160_Example bmx055 bmx055) add_example_with_path(BMM150_Example bmx055 bmx055) add_example_with_path(BMC150_Example bmx055 bmx055) add_example_with_path(BMI055_Example bmx055 bmx055) diff --git a/examples/javascript/bmg160.js b/examples/javascript/bmg160.js index b6fb3434..f31f5f75 100644 --- a/examples/javascript/bmg160.js +++ b/examples/javascript/bmg160.js @@ -22,7 +22,7 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -var sensorObj = require('jsupm_bmx055'); +var sensorObj = require('jsupm_bmg160'); // Instantiate a BMG160 instance using default i2c bus and address var sensor = new sensorObj.BMG160(); @@ -30,21 +30,17 @@ var sensor = new sensorObj.BMG160(); // For SPI, bus 0, you would pass -1 as the address, and a valid pin for CS: // BMG160(0, -1, 10); -var x = new sensorObj.new_floatp(); -var y = new sensorObj.new_floatp(); -var z = new sensorObj.new_floatp(); - // now output data every 250 milliseconds setInterval(function() { // update our values from the sensor sensor.update(); - sensor.getGyroscope(x, y, z); + var gyroData = sensor.getGyroscope(); console.log("Gyroscope x: " - + sensorObj.floatp_value(x) - + " y: " + sensorObj.floatp_value(y) - + " z: " + sensorObj.floatp_value(z) + + gyroData.get(0) + + " y: " + gyroData.get(1) + + " z: " + gyroData.get(2) + " degrees/s"); // we show both C and F for temperature diff --git a/examples/python/bmg160.py b/examples/python/bmg160.py index b3820917..daf1ce38 100755 --- a/examples/python/bmg160.py +++ b/examples/python/bmg160.py @@ -23,7 +23,7 @@ from __future__ import print_function import time, sys, signal, atexit -from upm import pyupm_bmx055 as sensorObj +from upm import pyupm_bmg160 as sensorObj def main(): # Instantiate a BMP250E instance using default i2c bus and address @@ -46,18 +46,14 @@ def main(): atexit.register(exitHandler) signal.signal(signal.SIGINT, SIGINTHandler) - x = sensorObj.new_floatp() - y = sensorObj.new_floatp() - z = sensorObj.new_floatp() - # now output data every 250 milliseconds while (1): sensor.update() - sensor.getGyroscope(x, y, z) - print("Gyroscope x:", sensorObj.floatp_value(x), end=' ') - print(" y:", sensorObj.floatp_value(y), end=' ') - print(" z:", sensorObj.floatp_value(z), end=' ') + gyroData = sensor.getGyroscope() + print("Gyroscope x:", gyroData[0], end=' ') + print(" y:", gyroData[1], end=' ') + print(" z:", gyroData[2], end=' ') print(" degrees/s") # we show both C and F for temperature diff --git a/src/bmg160/CMakeLists.txt b/src/bmg160/CMakeLists.txt new file mode 100644 index 00000000..0bdefc22 --- /dev/null +++ b/src/bmg160/CMakeLists.txt @@ -0,0 +1,9 @@ +upm_mixed_module_init (NAME bmg160 + DESCRIPTION "3-Axis Digital Gyroscope" + C_HDR bmg160.h bmg160_defs.h + C_SRC bmg160.c + CPP_HDR bmg160.hpp + CPP_SRC bmg160.cxx + FTI_SRC bmg160_fti.c + CPP_WRAPS_C + REQUIRES mraa) diff --git a/src/bmg160/bmg160.c b/src/bmg160/bmg160.c new file mode 100644 index 00000000..7390deab --- /dev/null +++ b/src/bmg160/bmg160.c @@ -0,0 +1,777 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2017 Intel Corporation. + * + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include + +#include "upm_utilities.h" +#include "bmg160.h" + +// macro for converting a uint8_t low/high pair into a float +#define INT16_TO_FLOAT(h, l) \ + (float)( (int16_t)( (l) | ((h) << 8) ) ) + +// SPI CS on and off functions +static void _csOn(const bmg160_context dev) +{ + assert(dev != NULL); + + if (dev->gpioCS) + mraa_gpio_write(dev->gpioCS, 0); +} + +static void _csOff(const bmg160_context dev) +{ + assert(dev != NULL); + + if (dev->gpioCS) + mraa_gpio_write(dev->gpioCS, 1); +} + +// init +bmg160_context bmg160_init(int bus, int addr, int cs) +{ + bmg160_context dev = + (bmg160_context)malloc(sizeof(struct _bmg160_context)); + + if (!dev) + return NULL; + + // zero out context + memset((void *)dev, 0, sizeof(struct _bmg160_context)); + + // make sure MRAA is initialized + if (mraa_init() != MRAA_SUCCESS) + { + printf("%s: mraa_init() failed.\n", __FUNCTION__); + bmg160_close(dev); + return NULL; + } + + if (addr < 0) + dev->isSPI = true; + + if (dev->isSPI) + { + if (!(dev->spi = mraa_spi_init(bus))) + { + printf("%s: mraa_spi_init() failed.\n", __FUNCTION__); + bmg160_close(dev); + return NULL; + } + + // Only create cs context if we are actually using a valid pin. + // A hardware controlled pin should specify cs as -1. + if (cs >= 0) + { + if (!(dev->gpioCS = mraa_gpio_init(cs))) + { + printf("%s: mraa_gpio_init() failed.\n", __FUNCTION__); + bmg160_close(dev); + return NULL; + } + mraa_gpio_dir(dev->gpioCS, MRAA_GPIO_OUT); + } + + mraa_spi_mode(dev->spi, MRAA_SPI_MODE0); + if (mraa_spi_frequency(dev->spi, 5000000)) + { + printf("%s: mraa_spi_frequency() failed.\n", __FUNCTION__); + bmg160_close(dev); + return NULL; + } + } + else + { + // I2C + + if (!(dev->i2c = mraa_i2c_init(bus))) + { + printf("%s: mraa_i2c_init() failed.\n", __FUNCTION__); + bmg160_close(dev); + return NULL; + } + + if (mraa_i2c_address(dev->i2c, addr)) + { + printf("%s: mraa_i2c_address() failed.\n", __FUNCTION__); + bmg160_close(dev); + return NULL; + } + } + + // check the chip id + + uint8_t chipID = bmg160_get_chip_id(dev); + if (chipID != BMG160_CHIPID) + { + printf("%s: invalid chip id: %02x. Expected %02x\n", + __FUNCTION__, chipID, BMG160_CHIPID); + bmg160_close(dev); + return NULL; + } + + // call devinit with default options + if (bmg160_devinit(dev, BMG160_POWER_MODE_NORMAL, BMG160_RANGE_250, + BMG160_BW_400_47)) + { + printf("%s: bmg160_devinit() failed.\n", __FUNCTION__); + bmg160_close(dev); + return NULL; + } + + return dev; +} + +void bmg160_close(bmg160_context dev) +{ + assert(dev != NULL); + + bmg160_uninstall_isr(dev, BMG160_INTERRUPT_INT1); + bmg160_uninstall_isr(dev, BMG160_INTERRUPT_INT2); + + if (dev->i2c) + mraa_i2c_stop(dev->i2c); + if (dev->spi) + mraa_spi_stop(dev->spi); + if (dev->gpioCS) + mraa_gpio_close(dev->gpioCS); + + free(dev); +} + +upm_result_t bmg160_devinit(const bmg160_context dev, + BMG160_POWER_MODE_T pwr, + BMG160_RANGE_T range, + BMG160_BW_T bw) +{ + assert(dev != NULL); + + if (bmg160_set_power_mode(dev, pwr)) + { + printf("%s: bmg160_set_power_mode() failed.\n", __FUNCTION__); + return UPM_ERROR_OPERATION_FAILED; + } + + upm_delay_ms(50); // 50ms, in case we are waking up + + // set our range and bandwidth, make sure register shadowing is + // enabled, enable output filtering, and set our FIFO config + + if (bmg160_set_range(dev, range) + || bmg160_set_bandwidth(dev, bw) + || bmg160_enable_register_shadowing(dev, true) + || bmg160_enable_output_filtering(dev, true) + || bmg160_fifo_config(dev, BMG160_FIFO_MODE_BYPASS, + BMG160_FIFO_DATA_SEL_XYZ)) + { + printf("%s: failed to set configuration parameters.\n", + __FUNCTION__); + return UPM_ERROR_OPERATION_FAILED; + } + + bmg160_enable_fifo(dev, true); + + // settle + upm_delay_ms(50); + + return UPM_SUCCESS; +} + +upm_result_t bmg160_update(const bmg160_context dev) +{ + assert(dev != NULL); + + int bufLen = 7; // max, non-FIFO + uint8_t startReg = BMG160_REG_RATE_X_LSB; + + if (dev->useFIFO) + { + bufLen = 6; + startReg = BMG160_REG_FIFO_DATA; + } + + uint8_t buf[bufLen]; + + if (bmg160_read_regs(dev, startReg, buf, bufLen) != bufLen) + { + printf("%s: bmg160_read_regs() failed to read %d bytes\n", + __FUNCTION__, bufLen); + return UPM_ERROR_OPERATION_FAILED; + } + + // x msb lsb + dev->gyrX = INT16_TO_FLOAT(buf[1], buf[0]); + + // y + dev->gyrY = INT16_TO_FLOAT(buf[3], buf[2]); + + // z + dev->gyrZ = INT16_TO_FLOAT(buf[5], buf[4]); + + // get the temperature... + + int8_t temp = 0; + if (dev->useFIFO) + { + // we have to read temperature separately... + temp = (int8_t)bmg160_read_reg(dev, BMG160_REG_TEMP); + } + else + { + // we already got it + temp = (int8_t)buf[6]; + } + + // .5K/LSB, 23C center point + dev->temperature = ((float)temp / 2.0) + 23.0; + + return UPM_SUCCESS; +} + +void bmg160_enable_fifo(const bmg160_context dev, bool useFIFO) +{ + assert(dev != NULL); + + dev->useFIFO = useFIFO; +} + +uint8_t bmg160_read_reg(const bmg160_context dev, uint8_t reg) +{ + assert(dev != NULL); + + if (dev->isSPI) + { + reg |= 0x80; // needed for read + uint8_t pkt[2] = {reg, 0}; + + _csOn(dev); + if (mraa_spi_transfer_buf(dev->spi, pkt, pkt, 2)) + { + _csOff(dev); + printf("%s: mraa_spi_transfer_buf() failed.\n", __FUNCTION__); + return 0xff; + } + _csOff(dev); + + return pkt[1]; + } + else + return (uint8_t)mraa_i2c_read_byte_data(dev->i2c, reg); +} + +int bmg160_read_regs(const bmg160_context dev, uint8_t reg, + uint8_t *buffer, int len) +{ + assert(dev != NULL); + + if (dev->isSPI) + { + reg |= 0x80; // needed for read + + uint8_t sbuf[len + 1]; + memset((char *)sbuf, 0, len + 1); + sbuf[0] = reg; + + // We need to do it this way for edison - ie: use a single + // transfer rather than breaking it up into two like we used to. + // This means a buffer copy is now required, but that's the way + // it goes. + + _csOn(dev); + if (mraa_spi_transfer_buf(dev->spi, sbuf, sbuf, len + 1)) + { + _csOff(dev); + printf("%s: mraa_spi_transfer_buf() failed.\n", __FUNCTION__); + return -1; + } + _csOff(dev); + + // now copy it into user buffer + for (int i=0; ii2c, reg, buffer, len) != len) + return -1; + } + + return len; +} + +upm_result_t bmg160_write_reg(const bmg160_context dev, + uint8_t reg, uint8_t val) +{ + assert(dev != NULL); + + if (dev->isSPI) + { + reg &= 0x7f; // mask off 0x80 for writing + uint8_t pkt[2] = {reg, val}; + + _csOn(dev); + if (mraa_spi_transfer_buf(dev->spi, pkt, NULL, 2)) + { + _csOff(dev); + printf("%s: mraa_spi_transfer_buf() failed.", + __FUNCTION__); + + return UPM_ERROR_OPERATION_FAILED; + } + _csOff(dev); + } + else + { + if (mraa_i2c_write_byte_data(dev->i2c, val, reg)) + { + printf("%s: mraa_i2c_write_byte_data() failed.", + __FUNCTION__); + return UPM_ERROR_OPERATION_FAILED; + } + } + + return UPM_SUCCESS; +} + +uint8_t bmg160_get_chip_id(const bmg160_context dev) +{ + assert(dev != NULL); + + return bmg160_read_reg(dev, BMG160_REG_CHIP_ID); +} + +void bmg160_get_gyroscope(const bmg160_context dev, + float *x, float *y, float *z) +{ + assert(dev != NULL); + + if (x) + *x = (dev->gyrX * dev->gyrScale) / 1000.0; + + if (y) + *y = (dev->gyrY * dev->gyrScale) / 1000.0; + + if (z) + *z = (dev->gyrZ * dev->gyrScale) / 1000.0; +} + +float bmg160_get_temperature(const bmg160_context dev) +{ + assert(dev != NULL); + + return dev->temperature; +} + +upm_result_t bmg160_reset(const bmg160_context dev) +{ + assert(dev != NULL); + + if (bmg160_write_reg(dev, BMG160_REG_SOFTRESET, BMG160_RESET_BYTE)) + return UPM_ERROR_OPERATION_FAILED; + + upm_delay(1); + + return UPM_SUCCESS; +} + +upm_result_t bmg160_set_range(const bmg160_context dev, + BMG160_RANGE_T range) +{ + assert(dev != NULL); + + // we also have to write a fixed '0x10' to the high-order bits for + // some reason (according to datasheet) + uint8_t reg = range | (_BMG160_GYR_RANGE_FIXED_VALUE + << _BMG160_GYR_RANGE_FIXED_SHIFT); + if (bmg160_write_reg(dev, BMG160_REG_GYR_RANGE, reg)) + return UPM_ERROR_OPERATION_FAILED; + + switch(range) + { + case BMG160_RANGE_125: + dev->gyrScale = 3.8; // milli-degrees + break; + + case BMG160_RANGE_250: + dev->gyrScale = 7.6; + break; + + case BMG160_RANGE_500: + dev->gyrScale = 15.3; + break; + + case BMG160_RANGE_1000: + dev->gyrScale = 30.5; + break; + + case BMG160_RANGE_2000: + dev->gyrScale = 61.0; + break; + } + + return UPM_SUCCESS; +} + +upm_result_t bmg160_set_bandwidth(const bmg160_context dev, + BMG160_BW_T bw) +{ + assert(dev != NULL); + + if (bmg160_write_reg(dev, BMG160_REG_GYR_BW, bw)) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} + +upm_result_t bmg160_set_power_mode(const bmg160_context dev, + BMG160_POWER_MODE_T power) +{ + assert(dev != NULL); + + // mask off reserved bits first + uint8_t reg = + bmg160_read_reg(dev, BMG160_REG_LPM1) & ~_BMG160_LPM1_RESERVED_BITS; + + reg &= ~(_BMG160_LPM1_POWER_MODE_MASK << _BMG160_LPM1_POWER_MODE_SHIFT); + reg |= (power << _BMG160_LPM1_POWER_MODE_SHIFT); + + if (bmg160_write_reg(dev, BMG160_REG_LPM1, power)) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} + +upm_result_t bmg160_fifo_set_watermark(const bmg160_context dev, int wm) +{ + assert(dev != NULL); + + // mask off illegal values + uint8_t reg = ((uint8_t)wm) & _BMG160_FIFO_CONFIG_0_WATER_MARK_MASK; + + if (bmg160_write_reg(dev, BMG160_REG_FIFO_CONFIG_0, reg)) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} + +upm_result_t bmg160_fifo_config(const bmg160_context dev, + BMG160_FIFO_MODE_T mode, + BMG160_FIFO_DATA_SEL_T axes) +{ + assert(dev != NULL); + + uint8_t reg = ( (mode << _BMG160_FIFO_CONFIG_1_FIFO_MODE_SHIFT) | + (axes << _BMG160_FIFO_CONFIG_1_FIFO_DATA_SHIFT) ); + + if (bmg160_write_reg(dev, BMG160_REG_FIFO_CONFIG_1, reg)) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} + +uint8_t bmg160_get_interrupt_enable0(const bmg160_context dev) +{ + assert(dev != NULL); + + return (bmg160_read_reg(dev, BMG160_REG_INT_EN_0) + & ~_BMG160_INT_EN_0_RESERVED_BITS); +} + +upm_result_t bmg160_set_interrupt_enable0(const bmg160_context dev, + uint8_t bits) +{ + assert(dev != NULL); + + uint8_t reg = bits & ~_BMG160_INT_EN_0_RESERVED_BITS; + + if (bmg160_write_reg(dev, BMG160_REG_INT_EN_0, reg)) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} + +uint8_t bmg160_get_interrupt_map0(const bmg160_context dev) +{ + assert(dev != NULL); + + return bmg160_read_reg(dev, BMG160_REG_INT_MAP_0) + & ~_BMG160_INT_MAP_0_RESERVED_BITS; +} + +upm_result_t bmg160_set_interrupt_map0(const bmg160_context dev, uint8_t bits) +{ + assert(dev != NULL); + + uint8_t reg = bits & ~_BMG160_INT_MAP_0_RESERVED_BITS; + + if (bmg160_write_reg(dev, BMG160_REG_INT_MAP_0, reg)) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} + +uint8_t bmg160_get_interrupt_map1(const bmg160_context dev) +{ + assert(dev != NULL); + + return bmg160_read_reg(dev, BMG160_REG_INT_MAP_1); +} + +upm_result_t bmg160_set_interrupt_map1(const bmg160_context dev, uint8_t bits) +{ + assert(dev != NULL); + + if (bmg160_write_reg(dev, BMG160_REG_INT_MAP_1, bits)) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} + +// REG_INT_EN1, for some strange reason +uint8_t bmg160_get_interrupt_src(const bmg160_context dev) +{ + assert(dev != NULL); + + return (bmg160_read_reg(dev, BMG160_REG_INT_EN_1) + & ~_BMG160_INT_EN_1_INT1_RESERVED_BITS); +} + +upm_result_t bmg160_set_interrupt_src(const bmg160_context dev, uint8_t bits) +{ + assert(dev != NULL); + + uint8_t reg = bits & ~_BMG160_INT_EN_1_INT1_RESERVED_BITS; + + if (bmg160_write_reg(dev, BMG160_REG_INT_EN_1, reg)) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} + +uint8_t bmg160_get_interrupt_output_control(const bmg160_context dev) +{ + assert(dev != NULL); + + return (bmg160_read_reg(dev, BMG160_REG_INT_EN_1) + & ~_BMG160_INT_EN_1_INT1_RESERVED_BITS); +} + +upm_result_t bmg160_set_interrupt_output_control(const bmg160_context dev, + uint8_t bits) +{ + assert(dev != NULL); + + uint8_t reg = bits & ~_BMG160_INT_EN_1_INT1_RESERVED_BITS; + + if (bmg160_write_reg(dev, BMG160_REG_INT_EN_1, reg)) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} + +upm_result_t bmg160_clear_interrupt_latches(const bmg160_context dev) +{ + assert(dev != NULL); + + uint8_t reg = + bmg160_read_reg(dev, BMG160_REG_INT_RST_LATCH) & ~_BMG160_INT_RST_LATCH_RESERVED_BITS; + + reg |= BMG160_INT_RST_LATCH_RESET_INT; + + if (bmg160_write_reg(dev, BMG160_REG_INT_RST_LATCH, reg)) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} + +BMG160_RST_LATCH_T bmg160_get_interrupt_latch_behavior(const bmg160_context dev) +{ + assert(dev != NULL); + + uint8_t reg = bmg160_read_reg(dev, BMG160_REG_INT_RST_LATCH) + & ~_BMG160_INT_RST_LATCH_RESERVED_BITS; + + reg &= (_BMG160_INT_RST_LATCH_MASK << _BMG160_INT_RST_LATCH_SHIFT); + + return (BMG160_RST_LATCH_T)reg; +} + +upm_result_t bmg160_set_interrupt_latch_behavior(const bmg160_context dev, + BMG160_RST_LATCH_T latch) +{ + assert(dev != NULL); + + uint8_t reg = + bmg160_read_reg(dev, BMG160_REG_INT_RST_LATCH) & ~_BMG160_INT_RST_LATCH_RESERVED_BITS; + + reg &= ~(_BMG160_INT_RST_LATCH_MASK << _BMG160_INT_RST_LATCH_SHIFT); + reg |= (latch << _BMG160_INT_RST_LATCH_SHIFT); + + if (bmg160_write_reg(dev, BMG160_REG_INT_RST_LATCH, reg)) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} + +upm_result_t bmg160_enable_register_shadowing(const bmg160_context dev, + bool shadow) +{ + assert(dev != NULL); + + uint8_t reg = + bmg160_read_reg(dev, BMG160_REG_RATE_HBW) & ~_BMG160_RATE_HBW_RESERVED_BITS; + + if (shadow) + reg &= ~BMG160_RATE_HBW_SHADOW_DIS; + else + reg |= BMG160_RATE_HBW_SHADOW_DIS; + + if (bmg160_write_reg(dev, BMG160_REG_RATE_HBW, reg)) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} + +upm_result_t bmg160_enable_output_filtering(const bmg160_context dev, + bool filter) +{ + assert(dev != NULL); + + uint8_t reg = + bmg160_read_reg(dev, BMG160_REG_RATE_HBW) & ~_BMG160_RATE_HBW_RESERVED_BITS; + + if (filter) + reg &= ~BMG160_RATE_HBW_DATA_HIGH_BW; + else + reg |= BMG160_RATE_HBW_DATA_HIGH_BW; + + if (bmg160_write_reg(dev, BMG160_REG_RATE_HBW, reg)) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} + +uint8_t bmg160_get_interrupt_status0(const bmg160_context dev) +{ + assert(dev != NULL); + + return (bmg160_read_reg(dev, BMG160_REG_INT_STATUS_0) + & ~_BMG160_INT_STATUS_0_RESERVED_BITS); +} + +uint8_t bmg160_get_interrupt_status1(const bmg160_context dev) +{ + assert(dev != NULL); + + return (bmg160_read_reg(dev, BMG160_REG_INT_STATUS_1) + & ~_BMG160_INT_STATUS_1_RESERVED_BITS); +} + +uint8_t bmg160_get_interrupt_status2(const bmg160_context dev) +{ + assert(dev != NULL); + + return (bmg160_read_reg(dev, BMG160_REG_INT_STATUS_2) + & ~_BMG160_INT_STATUS_2_RESERVED_BITS); +} + +uint8_t bmg160_get_interrupt_status3(const bmg160_context dev) +{ + assert(dev != NULL); + + return (bmg160_read_reg(dev, BMG160_REG_INT_STATUS_3) + & ~_BMG160_INT_STATUS_3_RESERVED_BITS); +} + +upm_result_t bmg160_install_isr(const bmg160_context dev, + BMG160_INTERRUPT_PINS_T intr, int gpio, + mraa_gpio_edge_t level, + void (*isr)(void *), void *arg) +{ + assert(dev != NULL); + + // delete any existing ISR and GPIO context for this interrupt + bmg160_uninstall_isr(dev, intr); + + mraa_gpio_context gpio_isr = NULL; + + // create gpio context + if (!(gpio_isr = mraa_gpio_init(gpio))) + { + printf("%s: mraa_gpio_init() failed.\n", __FUNCTION__); + return UPM_ERROR_OPERATION_FAILED; + } + + mraa_gpio_dir(gpio_isr, MRAA_GPIO_IN); + + if (mraa_gpio_isr(gpio_isr, level, isr, arg)) + { + mraa_gpio_close(gpio_isr); + printf("%s: mraa_gpio_isr() failed.\n", __FUNCTION__); + return UPM_ERROR_OPERATION_FAILED; + } + + switch (intr) + { + case BMG160_INTERRUPT_INT1: + dev->gpio1 = gpio_isr; + break; + + case BMG160_INTERRUPT_INT2: + dev->gpio2 = gpio_isr; + break; + } + + return UPM_SUCCESS; +} + +void bmg160_uninstall_isr(const bmg160_context dev, + BMG160_INTERRUPT_PINS_T intr) +{ + assert(dev != NULL); + + switch (intr) + { + case BMG160_INTERRUPT_INT1: + if (dev->gpio1) + { + mraa_gpio_isr_exit(dev->gpio1); + mraa_gpio_close(dev->gpio1); + dev->gpio1 = NULL; + } + break; + + case BMG160_INTERRUPT_INT2: + if (dev->gpio2) + { + mraa_gpio_isr_exit(dev->gpio2); + mraa_gpio_close(dev->gpio2); + dev->gpio2 = NULL; + } + break; + } +} diff --git a/src/bmg160/bmg160.cxx b/src/bmg160/bmg160.cxx new file mode 100644 index 00000000..877b8372 --- /dev/null +++ b/src/bmg160/bmg160.cxx @@ -0,0 +1,293 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016-2017 Intel Corporation. + * + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include + +#include "bmg160.hpp" + +using namespace upm; +using namespace std; + +// conversion from Celsius to Fahrenheit + +static float c2f(float c) +{ + return (c * (9.0 / 5.0) + 32.0); +} + +BMG160::BMG160(int bus, int addr, int cs) : + m_bmg160(bmg160_init(bus, addr, cs)) +{ + if (!m_bmg160) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_init() failed"); +} + +BMG160::~BMG160() +{ + bmg160_close(m_bmg160); +} + +void BMG160::init(BMG160_POWER_MODE_T pwr, BMG160_RANGE_T range, + BMG160_BW_T bw) +{ + if (bmg160_devinit(m_bmg160, pwr, range, bw)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_devinit() failed"); +} + +void BMG160::update() +{ + if (bmg160_update(m_bmg160)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_update() failed"); +} + +void BMG160::enableFIFO(bool useFIFO) +{ + bmg160_enable_fifo(m_bmg160, useFIFO); +} + +uint8_t BMG160::readReg(uint8_t reg) +{ + return bmg160_read_reg(m_bmg160, reg); +} + +int BMG160::readRegs(uint8_t reg, uint8_t *buffer, int len) +{ + int rv = bmg160_read_regs(m_bmg160, reg, buffer, len); + if (rv < 0) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_read_regs() failed"); + + return rv; +} + +void BMG160::writeReg(uint8_t reg, uint8_t val) +{ + if (bmg160_write_reg(m_bmg160, reg, val)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_write_reg() failed"); +} + +uint8_t BMG160::getChipID() +{ + return bmg160_get_chip_id(m_bmg160); +} + +void BMG160::getGyroscope(float *x, float *y, float *z) +{ + bmg160_get_gyroscope(m_bmg160, x, y, z); +} + +std::vector BMG160::getGyroscope() +{ + float v[3]; + + getGyroscope(&v[0], &v[1], &v[2]); + return std::vector(v, v+3); +} + +float BMG160::getTemperature(bool fahrenheit) +{ + float temperature = bmg160_get_temperature(m_bmg160); + if (fahrenheit) + return c2f(temperature); + else + return temperature; +} + +void BMG160::reset() +{ + if (bmg160_reset(m_bmg160)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_reset() failed"); +} + +void BMG160::setRange(BMG160_RANGE_T range) +{ + if (bmg160_set_range(m_bmg160, range)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_set_range() failed"); +} + +void BMG160::setBandwidth(BMG160_BW_T bw) +{ + if (bmg160_set_bandwidth(m_bmg160, bw)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_set_bandwidth() failed"); +} + +void BMG160::setPowerMode(BMG160_POWER_MODE_T power) +{ + if (bmg160_set_power_mode(m_bmg160, power)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_set_power_mode() failed"); +} + +void BMG160::fifoSetWatermark(int wm) +{ + if (bmg160_fifo_set_watermark(m_bmg160, wm)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_fifo_set_watermark() failed"); +} + +void BMG160::fifoConfig(BMG160_FIFO_MODE_T mode, BMG160_FIFO_DATA_SEL_T axes) +{ + if (bmg160_fifo_config(m_bmg160, mode, axes)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_fifo_config() failed"); +} + +uint8_t BMG160::getInterruptEnable0() +{ + return bmg160_get_interrupt_enable0(m_bmg160); +} + +void BMG160::setInterruptEnable0(uint8_t bits) +{ + if (bmg160_set_interrupt_enable0(m_bmg160, bits)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_set_interrupt_enable0() failed"); +} + +uint8_t BMG160::getInterruptMap0() +{ + return bmg160_get_interrupt_map0(m_bmg160); +} + +void BMG160::setInterruptMap0(uint8_t bits) +{ + if (bmg160_set_interrupt_map0(m_bmg160, bits)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_set_interrupt_map0() failed"); +} + +uint8_t BMG160::getInterruptMap1() +{ + return bmg160_get_interrupt_map1(m_bmg160); +} + +void BMG160::setInterruptMap1(uint8_t bits) +{ + if (bmg160_set_interrupt_map1(m_bmg160, bits)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_set_interrupt_map1() failed"); +} + +uint8_t BMG160::getInterruptSrc() +{ + return bmg160_get_interrupt_src(m_bmg160); +} + +void BMG160::setInterruptSrc(uint8_t bits) +{ + if (bmg160_set_interrupt_src(m_bmg160, bits)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_set_interrupt_src() failed"); +} + +uint8_t BMG160::getInterruptOutputControl() +{ + return bmg160_get_interrupt_output_control(m_bmg160); +} + +void BMG160::setInterruptOutputControl(uint8_t bits) +{ + if (bmg160_set_interrupt_output_control(m_bmg160, bits)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_set_interrupt_output_control() failed"); +} + +void BMG160::clearInterruptLatches() +{ + if (bmg160_clear_interrupt_latches(m_bmg160)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_clear_interrupt_latches() failed"); +} + +BMG160_RST_LATCH_T BMG160::getInterruptLatchBehavior() +{ + return bmg160_get_interrupt_latch_behavior(m_bmg160); +} + +void BMG160::setInterruptLatchBehavior(BMG160_RST_LATCH_T latch) +{ + if (bmg160_set_interrupt_latch_behavior(m_bmg160, latch)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_set_interrupt_latch_behavior() failed"); +} + +void BMG160::enableRegisterShadowing(bool shadow) +{ + if (bmg160_enable_register_shadowing(m_bmg160, shadow)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_enable_register_shadowing() failed"); +} + +void BMG160::enableOutputFiltering(bool filter) +{ + if (bmg160_enable_output_filtering(m_bmg160, filter)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_enable_output_filtering() failed"); +} + +uint8_t BMG160::getInterruptStatus0() +{ + return bmg160_get_interrupt_status0(m_bmg160); +} + +uint8_t BMG160::getInterruptStatus1() +{ + return bmg160_get_interrupt_status1(m_bmg160); +} + +uint8_t BMG160::getInterruptStatus2() +{ + return bmg160_get_interrupt_status2(m_bmg160); +} + +uint8_t BMG160::getInterruptStatus3() +{ + return bmg160_get_interrupt_status3(m_bmg160); +} + +void BMG160::installISR(BMG160_INTERRUPT_PINS_T intr, int gpio, + mraa_gpio_edge_t level, + void (*isr)(void *), void *arg) +{ + if (bmg160_install_isr(m_bmg160, intr, gpio, level, isr, arg)) + throw std::runtime_error(string(__FUNCTION__) + + ": bmg160_install_isr() failed"); +} + +void BMG160::uninstallISR(BMG160_INTERRUPT_PINS_T intr) +{ + bmg160_uninstall_isr(m_bmg160, intr); +} diff --git a/src/bmg160/bmg160.h b/src/bmg160/bmg160.h new file mode 100644 index 00000000..7bea1fef --- /dev/null +++ b/src/bmg160/bmg160.h @@ -0,0 +1,519 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2017 Intel Corporation. + * + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#pragma once + +#include +#include + +#include +#include +#include + +#include "upm.h" + +#include "bmg160_defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + + /** + * @file bmg160.h + * @library bmg160 + * @brief C API for the bmg160 driver + * + * @include bmg160.c + */ + + /** + * Device context + */ + typedef struct _bmg160_context { + mraa_i2c_context i2c; + mraa_spi_context spi; + mraa_gpio_context gpioCS; // SPI CS pin + mraa_gpio_context gpio1; // intr 1 + mraa_gpio_context gpio2; // intr 2 + + // using SPI? + bool isSPI; + // use the FIFO? + bool useFIFO; + + // always stored in C + float temperature; + + // gyr data + float gyrX; + float gyrY; + float gyrZ; + + // gyr scaling + float gyrScale; + } *bmg160_context; + + /** + * BMG160 initialization. + * + * This device can support both I2C and SPI. For SPI, set the addr + * to -1, and specify a positive integer representing the Chip + * Select (CS) pin for the cs argument. If you are using a + * hardware CS pin (like Intel Edison with Arduino breakout), then + * you can connect the proper pin to the hardware CS pin on your + * MCU and supply -1 for cs. + * + * @param bus I2C or SPI bus to use. + * @param addr The address for this device, or -1 for SPI. + * @param cs The gpio pin to use for the SPI Chip Select. -Use 1 for + * I2C or for SPI with a hardware controlled pin. + * @return The device context, or NULL on error. + */ + bmg160_context bmg160_init(int bus, int addr, int cs); + + /** + * BMG160 Destructor. + * + * @param dev The device context. + */ + void bmg160_close(bmg160_context dev); + + /** + * Update the internal stored values from sensor data. + * + * @param dev The device context. + * @return UPM result. + */ + upm_result_t bmg160_update(const bmg160_context dev); + + /** + * Return the chip ID. + * + * @param dev The device context. + * @return The chip ID (BMG160_CHIPID). + */ + uint8_t bmg160_get_chip_id(const bmg160_context dev); + + /** + * Return gyroscope data in degrees per second. bmg160_update() must + * have been called prior to calling this method. + * + * @param dev The device context. + * @param x Pointer to a floating point value that will have the + * current x component placed into it. + * @param y Pointer to a floating point value that will have the + * current y component placed into it. + * @param z Pointer to a floating point value that will have the + * current z component placed into it. + */ + void bmg160_get_gyroscope(const bmg160_context dev, + float *x, float *y, float *z); + + /** + * Return the current measured temperature. Note, this is not + * ambient temperature. bmg160_update() must have been called prior to + * calling this method. + * + * @param dev The device context. + * @return The temperature in degrees Celsius. + */ + float bmg160_get_temperature(const bmg160_context dev); + + /** + * Initialize the device and start operation. This function is + * called from bmg160_init(), so it will not need to be called by + * a user unless the device is reset. + * + * In addition, it will enable register shadowing and enable the + * FIFO in bypass mode. + * + * @param dev The device context. + * @param pwr One of the BMG160_POWER_MODE_T values. + * @param range One of the BMG160_RANGE_T values. + * @param bw One of the filtering BMG160_BW_T values. + * @return UPM result. + */ + upm_result_t bmg160_devinit(const bmg160_context dev, + BMG160_POWER_MODE_T pwr, + BMG160_RANGE_T range, + BMG160_BW_T bw); + + /** + * Reset the device as if during a power on reset. All configured + * values are lost when this happens. You should call + * bmg160_devinit() afterwards, or at least perform the same + * initialization bmg160_devinit() does before continuing. + * + * @param dev The device context. + * @return UPM result. + */ + upm_result_t bmg160_reset(const bmg160_context dev); + + /** + * Set the gyroscope detection scaling range. This device + * supports 125, 250, 500, 1000, and 2000 degree/s ranges. + * + * @param dev The device context. + * @param range One of the BMG160_RANGE_T values. + * @return UPM result. + */ + upm_result_t bmg160_set_range(const bmg160_context dev, + BMG160_RANGE_T range); + + /** + * Set the output filtering bandwidth of the device. + * + * @param dev The device context. + * @param bw One of the BMG160_BW_T values. + * @return UPM result. + */ + upm_result_t bmg160_set_bandwidth(const bmg160_context dev, + BMG160_BW_T bw); + + /** + * Set the power mode of the device. Care must be taken when + * setting a low power or suspend mode. See the datasheet for + * details. In certain power modes, register write must be + * drastically slowed down. which we cannot support. + * + * @param dev The device context. + * @param power One of the BMG160_POWER_MODE_T values. + * @return UPM result. + */ + upm_result_t bmg160_set_power_mode(const bmg160_context dev, + BMG160_POWER_MODE_T power); + + /** + * Enable bmg160_update() to read from the FIFO rather than the gyroscope + * axis registers directly. bmg160_devinit() enables this mode by default. + * An advantage to this mode that all axis data is sampled from + * the same timeslice. When reading directly from the gyroscope + * output registers, it's possible for one axis to be updated + * while another is being read, causing a temporal + * inconsistency. + * + * Using the FIFO removes this problem. + * + * @param dev The device context. + * @param useFIFO true to enable bmg160_update() to read from the FIFO. + * When false, update will read from the gyroscope output + * registers directly. + */ + void bmg160_enable_fifo(const bmg160_context dev, bool useFIFO); + + /** + * Set the FIFO watermark. When the watermark is reached an + * interrupt (if enabled) will be generated. + * + * @param dev The device context. + * @param wm The FIFO watermark to use. The maximum value is 63. + * @return UPM result. + */ + upm_result_t bmg160_fifo_set_watermark(const bmg160_context dev, int wm); + + /** + * Set the FIFO configuration. bmg160_devinit() uses the + * BMG160_FIFO_MODE_BYPASS mode with axes set to + * BMG160_FIFO_DATA_SEL_XYZ by default. + * + * @param dev The device context. + * @param mode One of the BMG160_FIFO_MODE_T values. + * @param axes One of the BMG160_FIFO_DATA_SEL_T values. + * @return UPM result. + */ + upm_result_t bmg160_fifo_config(const bmg160_context dev, + BMG160_FIFO_MODE_T mode, + BMG160_FIFO_DATA_SEL_T axes); + + /** + * Return the Interrupt Enables 0 register. These registers + * allow you to enable various interrupt conditions. See the + * datasheet for details. + * + * @param dev The device context. + * @return A bitmask of BMG160_INT_EN_0_BITS_T bits. + */ + uint8_t bmg160_get_interrupt_enable0(const bmg160_context dev); + + /** + * Set the Interrupt Enables 0 register. See the datasheet for + * details. + * + * @param dev The device context. + * @param bits A bitmask of BMG160_INT_EN_0_BITS_T bits. + * @return UPM result. + */ + upm_result_t bmg160_set_interrupt_enable0(const bmg160_context dev, + uint8_t bits); + + /** + * Return the Interrupt Map 0 register. These registers allow you + * to map specific interrupts to the interrupt 1 or interrupt 2 + * pin. See the datasheet for details. + * + * @param dev The device context. + * @return A bitmask of BMG160_INT_MAP_0_BITS_T bits. + */ + uint8_t bmg160_get_interrupt_map0(const bmg160_context dev); + + /** + * Set the Interrupt Map 0 register. These registers allow you + * to map specific interrupts to the interrupt 1 or interrupt 2 + * pin. See the datasheet for details. + * + * @param dev The device context. + * @param A bitmask of BMG160_INT_MAP_0_BITS_T bits. + * @return UPM result. + */ + upm_result_t bmg160_set_interrupt_map0(const bmg160_context dev, + uint8_t bits); + + /** + * Return the Interrupt Map 1 register. See the datasheet for + * details. + * + * @param dev The device context. + * @return A bitmask of BMG160_INT_MAP_1_BITS_T bits. + */ + uint8_t bmg160_get_interrupt_map1(const bmg160_context dev); + + /** + * Set the Interrupt Map 1 register. See the datasheet for + * details. + * + * @param dev The device context. + * @param A bitmask of BMG160_INT_MAP_1_BITS_T bits. + * @return UPM result. + */ + upm_result_t bmg160_set_interrupt_map1(const bmg160_context dev, + uint8_t bits); + + /** + * Return the Interrupt source register. This register allows + * determining where data comes from (filtered/unfiltered) for + * those interrupt sources where this is selectable. See the + * datasheet for details. + * + * @param dev The device context. + * @return A bitmask of BMG160_INT_1A_BITS_T bits. + */ + uint8_t bmg160_get_interrupt_src(const bmg160_context dev); + + /** + * Set the Interrupt source register. This register allows + * determining where data comes from (filtered/unfiltered) for + * those interrupt sources where this is selectable. See the + * datasheet for details. + * + * @param dev The device context. + * @param bits A bitmask of BMG160_INT_1A_BITS_T bits. + * @return UPM result. + */ + upm_result_t bmg160_set_interrupt_src(const bmg160_context dev, + uint8_t bits); + + /** + * Return the Interrupt output control register. This register + * allows determining the electrical characteristics of the 2 + * interrupt pins (open-drain/push-pull and level/edge + * triggering). See the datasheet for details. + * + * @param dev The device context. + * @return A bitmask of BMG160_INT_EN_1_BITS_T bits. + */ + uint8_t bmg160_get_interrupt_output_control(const bmg160_context dev); + + /** + * Set the Interrupt output control register. This register + * allows determining the electrical characteristics of the 2 + * interrupt pins (open-drain/push-pull and level/edge + * triggering). See the datasheet for details. + * + * @param dev The device context. + * @param bits A bitmask of BMG160_INT_EN_1_BITS_T bits. + * @return UPM result. + */ + upm_result_t bmg160_set_interrupt_output_control(const bmg160_context dev, + uint8_t bits); + + /** + * Clear all latched interrupts. See the datasheet for details. + * + * @param dev The device context. + * @return UPM result. + */ + upm_result_t bmg160_clear_interrupt_latches(const bmg160_context dev); + + /** + * Return the current interrupt latching behavior. See the + * datasheet for details. + * + * @param dev The device context. + * @return One of the BMG160_RST_LATCH_T values. + */ + BMG160_RST_LATCH_T bmg160_get_interrupt_latch_behavior( + const bmg160_context dev); + + /** + * Set the current interrupt latching behavior. See the datasheet + * for details. + * + * @param dev The device context. + * @param latch One of the BMG160_RST_LATCH_T values. + * @return UPM result. + */ + upm_result_t bmg160_set_interrupt_latch_behavior(const bmg160_context dev, + BMG160_RST_LATCH_T latch); + + /** + * Return the interrupt status 0 register. These registers + * indicate which interrupts have been triggered. See the + * datasheet for details. + * + * @param dev The device context. + * @return a bitmask of BMG160_INT_STATUS_0_BITS_T bits. + */ + uint8_t bmg160_get_interrupt_status0(const bmg160_context dev); + + /** + * Return the interrupt status 1 register. See the datasheet for + * details. + * + * @param dev The device context. + * @return a bitmask of BMG160_INT_STATUS_1_BITS_T bits. + */ + uint8_t bmg160_get_interrupt_status1(const bmg160_context dev); + + /** + * Return the interrupt status 2 register. See the datasheet for + * details. + * + * @param dev The device context. + * @return a bitmask of BMG160_INT_STATUS_2_BITS_T bits. + */ + uint8_t bmg160_get_interrupt_status2(const bmg160_context dev); + + /** + * Return the interrupt status 3 register. See the datasheet for + * details. + * + * @param dev The device context. + * @return a bitmask of BMG160_INT_STATUS_3_BITS_T bits. + */ + uint8_t bmg160_get_interrupt_status3(const bmg160_context dev); + + /** + * Enable shadowing of the gyroscope output registers. When + * enabled, a read of an axis LSB register automatically locks the + * MSB register of that axis until it has been read. This is + * usually a good thing to have enabled. bmg160_devinit() enables this by + * default. If disabled, then it becomes possible for part of an + * axis value to change while another part is being read, causing + * inconsistent data. + * + * @param dev The device context. + * @param shadow true to enable axis register shadowing, false otherwise. + * @return UPM result. + */ + upm_result_t bmg160_enable_register_shadowing(const bmg160_context dev, + bool shadow); + + /** + * Enable filtering of the gyroscope axis data. bmg160_devinit() + * enables this by default. If disabled, then gyroscope data + * that is read will be raw and unfiltered (rated R). See the + * datasheet for details. + * + * @param dev The device context. + * @param filter true to enable filtering, false to disable. + * @return UPM result. + */ + upm_result_t bmg160_enable_output_filtering(const bmg160_context dev, + bool filter); + + /** + * install an interrupt handler. + * + * @param dev The device context. + * @param intr one of the INTERRUPT_PINS_T values specifying which + * interrupt pin you are installing. + * @param gpio gpio pin to use as interrupt pin + * @param level the interrupt trigger level (one of mraa_gpio_edge_t + * values). Make sure that you have configured the interrupt pin + * properly for whatever level you choose. + * @param isr the interrupt handler, accepting a void * argument + * @param arg the argument to pass the the interrupt handler + * @return UPM result. + */ + upm_result_t bmg160_install_isr(const bmg160_context dev, + BMG160_INTERRUPT_PINS_T intr, int gpio, + mraa_gpio_edge_t level, + void (*isr)(void *), void *arg); + + /** + * uninstall a previously installed interrupt handler + * + * @param dev The device context. + * @param intr one of the INTERRUPT_PINS_T values specifying which + * interrupt pin you are removing. + */ + void bmg160_uninstall_isr(const bmg160_context dev, + BMG160_INTERRUPT_PINS_T intr); + + /** + * Read a register. + * + * @param dev The device context. + * @param reg The register to read. + * @return The value of the register. + */ + uint8_t bmg160_read_reg(const bmg160_context dev, uint8_t reg); + + /** + * Read contiguous registers into a buffer. + * + * @param dev The device context. + * @param buffer The buffer to store the results. + * @param len The number of registers to read. + * @return The number of bytes read, or -1 on error. + */ + int bmg160_read_regs(const bmg160_context dev, uint8_t reg, + uint8_t *buffer, int len); + + /** + * Write to a register. + * + * @param dev The device context. + * @param reg The register to write to. + * @param val The value to write. + * @return UPM result. + */ + upm_result_t bmg160_write_reg(const bmg160_context dev, + uint8_t reg, uint8_t val); + + +#ifdef __cplusplus +} +#endif diff --git a/src/bmg160/bmg160.hpp b/src/bmg160/bmg160.hpp new file mode 100644 index 00000000..3e398353 --- /dev/null +++ b/src/bmg160/bmg160.hpp @@ -0,0 +1,483 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016-2017 Intel Corporation. + * + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#pragma once + +#include +#include + +#include "bmg160.h" + +namespace upm { + + /** + * @library bmx050 + * @sensor bmg160 + * @comname 3-axis Gyroscope Sensor + * @type gyro + * @man bosch + * @con i2c spi gpio + * @web https://www.bosch-sensortec.com/bst/products/all_products/bmg160 + * + * @brief API for the BMG160 16 bit Triaxial Gyroscope + * + * The BMG160 is a 3-axis angular rate sensor that is made of a + * surface micro machined sensing element and an evaluation ASIC. + * Both parts are packed into one single LGA 3.0mm x 3.0mm x 0.95mm + * housing. The BMG160 is designed to meet requirements for + * consumer applications such as image stabilization (DSC and + * camera-phone), gaming and pointing devices. It is capable to + * measure angular rates in three perpendicular room dimensions, the + * x-, y- and z-axis, and to provide the corresponding output + * signals. The BMG160 is fitted with digital bi-directional SPI and + * I2C interfaces for optimum system integration. + * + * Not all functionality of this chip has been implemented in this + * driver, however all the pieces are present to add any desired + * functionality. This driver supports both I2C (default) and SPI + * operation. + * + * This device requires 3.3v operation. + * + * @snippet bmg160.cxx Interesting + */ + + class BMG160 { + public: + /** + * BMG160 constructor. + * + * This device can support both I2C and SPI. For SPI, set the addr + * to -1, and specify a positive integer representing the Chip + * Select (CS) pin for the cs argument. If you are using a + * hardware CS pin (like edison with arduino breakout), then you + * can connect the proper pin to the hardware CS pin on your MCU + * and supply -1 for cs. The default operating mode is I2C. + * + * @param bus I2C or SPI bus to use. + * @param addr The address for this device. -1 for SPI. + * @param cs The gpio pin to use for the SPI Chip Select. -1 for + * I2C or for SPI with a hardware controlled pin. + * @throws std::runtime_error on initialization failure. + */ + BMG160(int bus=BMG160_DEFAULT_I2C_BUS, int addr=BMG160_DEFAULT_ADDR, + int cs=-1); + + /** + * BMG160 Destructor. + */ + ~BMG160(); + + /** + * Update the internal stored values from sensor data. + * + * @throws std::runtime_error on failure. + */ + void update(); + + /** + * Return the chip ID. + * + * @return The chip ID (BMG160_CHIPID). + */ + uint8_t getChipID(); + + /** + * Return gyroscope data in degrees per second. update() must + * have been called prior to calling this method. + * + * @param x Pointer to a floating point value that will have the + * current x component placed into it. + * @param y Pointer to a floating point value that will have the + * current y component placed into it. + * @param z Pointer to a floating point value that will have the + * current z component placed into it. + */ + void getGyroscope(float *x, float *y, float *z); + + /** + * Return gyroscope data in degrees per second in the form of + * a floating point vector. update() must have been called + * prior to calling this method. + * + * @return A floating point vector containing x, y, and z in + * that order. + */ + std::vector getGyroscope(); + + /** + * Return the current measured temperature. Note, this is not + * ambient temperature. update() must have been called prior to + * calling this method. + * + * @param fahrenheit true to return data in Fahrenheit, false for + * Celicus. Celsius is the default. + * @return The temperature in degrees Celsius or Fahrenheit. + */ + float getTemperature(bool fahrenheit=false); + + /** + * Initialize the device and start operation. This function is + * called from the constructor so will not typically need to be + * called by a user unless the device is reset. + * + * @param pwr One of the BMG160_POWER_MODE_T values. The default is + * BMG160_POWER_MODE_NORMAL. + * @param range One of the BMG160_RANGE_T values. The default is + * BMG160_RANGE_250. + * @param bw One of the filtering BMG160_BW_T values. The default is + * BMG160_BW_400_47. + * @throws std::runtime_error on failure. + */ + void init(BMG160_POWER_MODE_T pwr=BMG160_POWER_MODE_NORMAL, + BMG160_RANGE_T range=BMG160_RANGE_250, + BMG160_BW_T bw=BMG160_BW_400_47); + + /** + * Reset the device as if during a power on reset. All configured + * values are lost when this happens. You should call init() + * afterwards, or at least perform the same initialization init() + * does before continuing. + * + * @throws std::runtime_error on failure. + */ + void reset(); + + /** + * Set the gyroscope detection scaling range. This device + * supports 125, 250, 500, 1000, and 2000 degree/s ranges. + * + * @param range One of the BMG160_RANGE_T values. + * @throws std::runtime_error on failure. + */ + void setRange(BMG160_RANGE_T range); + + /** + * Set the output filtering bandwidth of the device. + * + * @param bw One of the BMG160_BW_T values. + * @throws std::runtime_error on failure. + */ + void setBandwidth(BMG160_BW_T bw); + + /** + * Set the power mode of the device. Care must be taken when + * setting a low power or suspend mode. See the datasheet for + * details. I ncertain power modes, register write must be + * drastically slowed down. which we cannot support. + * + * @param power One of the POWER_MODE_T values. + * @throws std::runtime_error on failure. + */ + void setPowerMode(BMG160_POWER_MODE_T power); + + /** + * Enable update() to read from the FIFO rather than the gyroscope + * axis registers directly. init() enables this mode by default. + * An advantage to this mode that all axis data is sampled from + * the same timeslice. When reading directly from the gyroscope + * output registers, it's possible for one axis to be updated + * while another is being read, causing a temporal + * inconsistancy.. + * + * Using the FIFO removes this problem. + * + * @param useFIFO true to enable update() to read from the FIFO. + * When false, update will read from the gyroscope output + * registers directly. + */ + void enableFIFO(bool useFIFO); + + /** + * Set the FIFO watermark. When the watermark is reached an + * interrupt (if enabled) will be generated. + * + * @param wm The FIFO watermark to use. The maximum value is 63. + * @throws std::runtime_error on failure. + */ + void fifoSetWatermark(int wm); + + /** + * Set the FIFO configuration. init() uses the + * BMG160_FIFO_MODE_BYPASS mode with axes set to + * BMG160_FIFO_DATA_SEL_XYZ by default. + * + * @param mode One of the BMG160_FIFO_MODE_T values. + * @param axes One of the BMG160_FIFO_DATA_SEL_T values. + * @throws std::runtime_error on failure. + */ + void fifoConfig(BMG160_FIFO_MODE_T mode, BMG160_FIFO_DATA_SEL_T axes); + + /** + * Return the Interrupt Enables 0 register. These registers + * allow you to enable various interrupt conditions. See the + * datasheet for details. + * + * @return A bitmask of INT_EN_0_BITS_T bits. + */ + uint8_t getInterruptEnable0(); + + /** + * Set the Interrupt Enables 0 register. See the datasheet for + * details. + * + * @param bits A bitmask of BMG160_INT_EN_0_BITS_T bits. + * @throws std::runtime_error on failure. + */ + void setInterruptEnable0(uint8_t bits); + + /** + * Return the Interrupt Map 0 register. These registers allow you + * to map specific interrupts to the interrupt 1 or interrupt 2 + * pin. See the datasheet for details. + * + * @return A bitmask of INT_MAP_0_BITS_T bits. + */ + uint8_t getInterruptMap0(); + + /** + * Set the Interrupt Map 0 register. These registers allow you + * to map specific interrupts to the interrupt 1 or interrupt 2 + * pin. See the datasheet for details. + * + * @param A bitmask of BMG160_INT_MAP_0_BITS_T bits. + * @throws std::runtime_error on failure. + */ + void setInterruptMap0(uint8_t bits); + + /** + * Return the Interrupt Map 1 register. See the datasheet for + * details. + * + * @return A bitmask of BMG160_INT_MAP_1_BITS_T bits. + */ + uint8_t getInterruptMap1(); + + /** + * Set the Interrupt Map 1 register. See the datasheet for + * details. + * + * @param A bitmask of BMG160_INT_MAP_1_BITS_T bits. + * @throws std::runtime_error on failure. + */ + void setInterruptMap1(uint8_t bits); + + /** + * Return the Interrupt source register. This register allows + * determining where data comes from (filtered/unfiltered) for + * those interrupt sources where this is selectable. See the + * datasheet for details. + * + * @return A bitmask of INT_1A_BITS_T bits. + */ + uint8_t getInterruptSrc(); + + /** + * Set the Interrupt source register. This register allows + * determining where data comes from (filtered/unfiltered) for + * those interrupt sources where this is selectable. See the + * datasheet for details. + * + * @param bits A bitmask of INT_1A_BITS_T bits. + * @throws std::runtime_error on failure. + */ + void setInterruptSrc(uint8_t bits); + + /** + * Return the Interrupt output control register. This register + * allows determining the electrical characteristics of the 2 + * interrupt pins (open-drain/push-pull and level/edge + * triggering). See the datasheet for details. + * + * @return A bitmask of INT_EN_1_BITS_T bits. + */ + uint8_t getInterruptOutputControl(); + + /** + * Set the Interrupt output control register. This register + * allows determining the electrical characteristics of the 2 + * interrupt pins (open-drain/push-pull and level/edge + * triggering). See the datasheet for details. + * + * @param bits A bitmask of INT_EN_1_BITS_T bits. + * @throws std::runtime_error on failure. + */ + void setInterruptOutputControl(uint8_t bits); + + /** + * Clear all latched interrupts. See the datasheet for details. + * + * @throws std::runtime_error on failure. + */ + void clearInterruptLatches(); + + /** + * Return the current interrupt latching behavior. See the + * datasheet for details. + * + * @return One of the RST_LATCH_T values. + */ + BMG160_RST_LATCH_T getInterruptLatchBehavior(); + + /** + * Set the current interrupt latching behavior. See the datasheet + * for details. + * + * @param latch One of the RST_LATCH_T values. + * @throws std::runtime_error on failure. + */ + void setInterruptLatchBehavior(BMG160_RST_LATCH_T latch); + + /** + * Return the interrupt status 0 register. These registers + * indicate which interrupts have been triggered. See the + * datasheet for details. + * + * @return a bitmask of INT_STATUS_0_BITS_T bits. + */ + uint8_t getInterruptStatus0(); + + /** + * Return the interrupt status 1 register. See the datasheet for + * details. + * + * @return a bitmask of INT_STATUS_1_BITS_T bits. + */ + uint8_t getInterruptStatus1(); + + /** + * Return the interrupt status 2 register. See the datasheet for + * details. + * + * @return a bitmask of INT_STATUS_2_BITS_T bits. + */ + uint8_t getInterruptStatus2(); + + /** + * Return the interrupt status 3 register. See the datasheet for + * details. + * + * @return a bitmask of INT_STATUS_3_BITS_T bits. + */ + uint8_t getInterruptStatus3(); + + /** + * Enable shadowing of the gyroscope output registers. When + * enabled, a read of an axis LSB register automatically locks the + * MSB register of that axis until it has been read. This is + * usually a good thing to have enabled. init() enables this by + * default. If disabled, then it becomes possible for part of an + * axis value to change while another part is being read, causing + * inconsistent data. + * + * @param shadow true to enable axis register shadowing, false + * otherwise. + * @throws std::runtime_error on failure. + */ + void enableRegisterShadowing(bool shadow); + + /** + * Enable filtering of the gyroscope axis data. init() + * enables this by default. If disabled, then gyroscope data + * that is read will be raw and unfiltered (rated R). See the + * datasheet for details. + * + * @param filter true to enable filtering, false to disable. + */ + void enableOutputFiltering(bool filter); + +#if defined(SWIGJAVA) || defined(JAVACALLBACK) + void installISR(BMG160_INTERRUPT_PINS_T intr, int gpio, + mraa_gpio_edge_t level, jobject runnable) + { + installISR(intr, gpio, level, mraa_java_isr_callback, runnable); + } +#else + /** + * install an interrupt handler. + * + * @param intr one of the BMG160_INTERRUPT_PINS_T values + * specifying which interrupt pin you are installing. + * @param gpio gpio pin to use as interrupt pin + * @param level the interrupt trigger level (one of the + * mraa_gpio_edge_t values). Make sure that you have + * configured the interrupt pin properly for whatever level + * you choose. + * @param isr the interrupt handler, accepting a void * argument + * @param arg the argument to pass the the interrupt handler + * @throws std::runtime_error on failure. + */ + void installISR(BMG160_INTERRUPT_PINS_T intr, int gpio, + mraa_gpio_edge_t level, + void (*isr)(void *), void *arg); +#endif + + /** + * uninstall a previously installed interrupt handler + * + * @param intr one of the INTERRUPT_PINS_T values specifying which + * interrupt pin you are removing. + */ + void uninstallISR(BMG160_INTERRUPT_PINS_T intr); + + /** + * Read a register. + * + * @param reg The register to read. + * @return The value of the register. + */ + uint8_t readReg(uint8_t reg); + + /** + * Read contiguous registers into a buffer. + * + * @param buffer The buffer to store the results. + * @param len The number of registers to read. + * @return The number of bytes read. + * @throws std::runtime_error on failure. + */ + int readRegs(uint8_t reg, uint8_t *buffer, int len); + + /** + * Write to a register + * + * @param reg The register to write to. + * @param val The value to write. + * @throws std::runtime_error on failure. + */ + void writeReg(uint8_t reg, uint8_t val); + + protected: + bmg160_context m_bmg160; + + private: + // needs to be private for SWIG Java builds +#if defined(SWIGJAVA) || defined(JAVACALLBACK) + void installISR(BMG160_INTERRUPT_PINS_T intr, int gpio, + mraa_gpio_edge_t level, + void (*isr)(void *), void *arg); +#endif + }; +} diff --git a/src/bmg160/bmg160_defs.h b/src/bmg160/bmg160_defs.h new file mode 100644 index 00000000..98490c95 --- /dev/null +++ b/src/bmg160/bmg160_defs.h @@ -0,0 +1,793 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2017 Intel Corporation. + * + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#define BMG160_DEFAULT_I2C_BUS 0 +#define BMG160_DEFAULT_SPI_BUS 0 +#define BMG160_DEFAULT_ADDR 0x68 + +// special reset byte +#define BMG160_RESET_BYTE 0xb6 + +#define BMG160_CHIPID 0x0f + + // NOTE: Reserved registers must not be written into. Reading + // from them may return indeterminate values. Registers + // containing reserved bitfields must be written as 0. Reading + // reserved bitfields may return indeterminate values. + + /** + * BMG160 registers + */ + typedef enum { + BMG160_REG_CHIP_ID = 0x00, + + // 0x01 reserved + + BMG160_REG_RATE_X_LSB = 0x02, + BMG160_REG_RATE_X_MSB = 0x03, + BMG160_REG_RATE_Y_LSB = 0x04, + BMG160_REG_RATE_Y_MSB = 0x05, + BMG160_REG_RATE_Z_LSB = 0x06, + BMG160_REG_RATE_Z_MSB = 0x07, + + BMG160_REG_TEMP = 0x08, + + BMG160_REG_INT_STATUS_0 = 0x09, + BMG160_REG_INT_STATUS_1 = 0x0a, + BMG160_REG_INT_STATUS_2 = 0x0b, + BMG160_REG_INT_STATUS_3 = 0x0c, + + // 0x0d reserved + + BMG160_REG_FIFO_STATUS = 0x0e, + + BMG160_REG_GYR_RANGE = 0x0f, + BMG160_REG_GYR_BW = 0x10, + BMG160_REG_LPM1 = 0x11, + BMG160_REG_LPM2 = 0x12, + + BMG160_REG_RATE_HBW = 0x13, + + BMG160_REG_SOFTRESET = 0x14, + + BMG160_REG_INT_EN_0 = 0x15, + BMG160_REG_INT_EN_1 = 0x16, + + BMG160_REG_INT_MAP_0 = 0x17, + BMG160_REG_INT_MAP_1 = 0x18, + BMG160_REG_INT_MAP_2 = 0x19, + + BMG160_REG_INT_1A = 0x1a, + BMG160_REG_INT_1B = 0x1b, + BMG160_REG_INT_1C = 0x1c, + + // 0x1d reserved + + BMG160_REG_INT_1E = 0x1e, + + // 0x1f-0x20 reserved + + BMG160_REG_INT_RST_LATCH = 0x21, + + BMG160_REG_HIGH_TH_X = 0x22, + BMG160_REG_HIGH_DUR_X = 0x23, + BMG160_REG_HIGH_TH_Y = 0x24, + BMG160_REG_HIGH_DUR_Y = 0x25, + BMG160_REG_HIGH_TH_Z = 0x26, + BMG160_REG_HIGH_DUR_Z = 0x27, + + // 0x28-0x30 reserved + + BMG160_REG_SOC = 0x31, + BMG160_REG_A_FOC = 0x32, + + BMG160_REG_TRIM_NVM_CTRL = 0x33, + + BMG160_REG_SPI3_WDT = 0x34, + + // 0x35 reserved + + BMG160_REG_OFC1 = 0x36, + BMG160_REG_OFC2 = 0x37, + BMG160_REG_OFC3 = 0x38, + BMG160_REG_OFC4 = 0x39, + + BMG160_REG_TRIM_GP0 = 0x3a, + BMG160_REG_TRIM_GP1 = 0x3b, + + BMG160_REG_BIST = 0x3c, + + BMG160_REG_FIFO_CONFIG_0 = 0x3d, + BMG160_REG_FIFO_CONFIG_1 = 0x3e, + + BMG160_REG_FIFO_DATA = 0x3f + + } BMG160_REGS_T; + + /** + * REG_INT_STATUS_0 bits + */ + typedef enum { + _BMG160_INT_STATUS_0_RESERVED_BITS = 0xf0 | 0x08 | 0x01, + + BMG160_INT_STATUS_0_HIGH_INT = 0x02, + BMG160_INT_STATUS_0_ANY_INT = 0x04 + } BMG160_INT_STATUS_0_BITS_T; + + /** + * REG_INT_STATUS_1 bits + */ + typedef enum { + _BMG160_INT_STATUS_1_RESERVED_BITS = 0x0f, + + BMG160_INT_STATUS_1_FIFO_INT = 0x10, + BMG160_INT_STATUS_1_FAST_OFFSET_INT = 0x20, + BMG160_INT_STATUS_1_AUTO_OFFSET_INT = 0x40, + BMG160_INT_STATUS_1_DATA_INT = 0x80 + } BMG160_INT_STATUS_1_BITS_T; + + /** + * REG_INT_STATUS_2 bits + */ + typedef enum { + _BMG160_INT_STATUS_2_RESERVED_BITS = 0xf0, + + BMG160_INT_STATUS_2_ANY_FIRST_X = 0x01, + BMG160_INT_STATUS_2_ANY_FIRST_Y = 0x02, + BMG160_INT_STATUS_2_ANY_FIRST_Z = 0x04, + BMG160_INT_STATUS_2_ANY_SIGN = 0x08 + } BMG160_INT_STATUS_2_BITS_T; + + /** + * REG_INT_STATUS_3 bits + */ + typedef enum { + _BMG160_INT_STATUS_3_RESERVED_BITS = 0xf0, + + BMG160_INT_STATUS_3_HIGH_FIRST_X = 0x01, + BMG160_INT_STATUS_3_HIGH_FIRST_Y = 0x02, + BMG160_INT_STATUS_3_HIGH_FIRST_Z = 0x04, + BMG160_INT_STATUS_3_HIGH_SIGN = 0x08 + } BMG160_INT_STATUS_3_BITS_T; + + /** + * REG_FIFO_STATUS bits + */ + typedef enum { + BMG160_FIFO_STATUS_FRAME_COUNTER0 = 0x01, + BMG160_FIFO_STATUS_FRAME_COUNTER1 = 0x02, + BMG160_FIFO_STATUS_FRAME_COUNTER2 = 0x04, + BMG160_FIFO_STATUS_FRAME_COUNTER3 = 0x08, + BMG160_FIFO_STATUS_FRAME_COUNTER4 = 0x10, + BMG160_FIFO_STATUS_FRAME_COUNTER5 = 0x20, + BMG160_FIFO_STATUS_FRAME_COUNTER6 = 0x40, + _BMG160_FIFO_STATUS_FRAME_COUNTER_MASK = 127, + _BMG160_FIFO_STATUS_FRAME_COUNTER_SHIFT = 0, + + BMG160_FIFO_STATUS_FIFO_OVERRUN = 0x80 + } BMG160_FIFO_STATUS_BITS_T; + + /** + * REG_GYR_RANGE bits + */ + typedef enum { + _BMG160_GYR_RANGE_RESERVED_BITS = 0x20 | 0x10 | 0x08, + + BMG160_GYR_RANGE0 = 0x01, + BMG160_GYR_RANGE1 = 0x02, + BMG160_GYR_RANGE2 = 0x04, + _BMG160_GYR_RANGE_MASK = 7, + _BMG160_GYR_RANGE_SHIFT = 0, + + BMG160_GYR_RANGE_FIXED0 = 0x40, // bits need + // hardcoding + // to 0b10 + BMG160_GYR_RANGE_FIXED1 = 0x80, // for some + // odd + // reason... + _BMG160_GYR_RANGE_FIXED_MASK = 3, + _BMG160_GYR_RANGE_FIXED_SHIFT = 6, + _BMG160_GYR_RANGE_FIXED_VALUE = 2 // 0b10 + } BMG160_GYR_RANGE_BITS_T; + + /** + * GYR_RANGE (gyroscope deg/s range) values + */ + typedef enum { + BMG160_RANGE_2000 = 0, // degrees/sec + BMG160_RANGE_1000 = 1, + BMG160_RANGE_500 = 2, + BMG160_RANGE_250 = 3, + BMG160_RANGE_125 = 4 + } BMG160_RANGE_T; + + /** + * REG_GYR_BW bits + */ + typedef enum { + _BMG160_GYR_BW_RESERVED_BITS = 0xf0, + + BMG160_GYR_BW0 = 0x01, + BMG160_GYR_BW1 = 0x02, + BMG160_GYR_BW2 = 0x04, + BMG160_GYR_BW3 = 0x08, + _BMG160_GYR_BW_MASK = 15, + _BMG160_GYR_BW_SHIFT = 0 + } BMG160_GYR_BW_BITS_T; + + /** + * GYR_BW (gyroscope filter bandwidth) values + */ + typedef enum { + BMG160_BW_2000_UNFILTERED = 0, // ODR/Filter BW + BMG160_BW_2000_230 = 1, // ODR 2000Hz, + // Filter BW + // 230Hz + BMG160_BW_1000_116 = 2, + BMG160_BW_400_47 = 3, + BMG160_BW_200_23 = 4, + BMG160_BW_100_12 = 5, + BMG160_BW_200_64 = 6, + BMG160_BW_100_32 = 7 + } BMG160_BW_T; + + /** + * REG_LPM1 bits + */ + typedef enum { + // 0x01 reserved + _BMG160_LPM1_RESERVED_BITS = 0x40 | 0x10 | 0x01, + + BMG160_LPM1_SLEEP_DUR0 = 0x02, // sleep dur + // in low + // power mode + BMG160_LPM1_SLEEP_DUR1 = 0x04, + BMG160_LPM1_SLEEP_DUR2 = 0x08, + _BMG160_LPM1_SLEEP_MASK = 7, + _BMG160_LPM1_SLEEP_SHIFT = 1, + + // These are separate bits, deep_suspend and suspend (and if all + // 0, normal). Since only specific combinations are allowed, we + // will treat this as a 3 bit bitfield called POWER_MODE. + BMG160_LPM1_POWER_MODE0 = 0x20, // deep_suspend + BMG160_LPM1_POWER_MODE1 = 0x40, // must always be 0! + BMG160_LPM1_POWER_MODE2 = 0x80, // suspend + _BMG160_LPM1_POWER_MODE_MASK = 7, + _BMG160_LPM1_POWER_MODE_SHIFT = 5 + } BMG160_LPM1_BITS_T; + + /** + * SLEEP_DUR values + */ + typedef enum { + BMG160_SLEEP_DUR_2 = 0, // 2ms + BMG160_SLEEP_DUR_4 = 1, + BMG160_SLEEP_DUR_5 = 2, + BMG160_SLEEP_DUR_8 = 3, + BMG160_SLEEP_DUR_10 = 4, + BMG160_SLEEP_DUR_15 = 5, + BMG160_SLEEP_DUR_18 = 6, + BMG160_SLEEP_DUR_20 = 7 + } BMG160_SLEEP_DUR_T; + + /** + * POWER_MODE values + */ + typedef enum { + BMG160_POWER_MODE_NORMAL = 0, + BMG160_POWER_MODE_DEEP_SUSPEND = 1, + BMG160_POWER_MODE_SUSPEND = 4 + } BMG160_POWER_MODE_T; + + /** + * REG_LPM2 bits + */ + typedef enum { + _BMG160_LPM2_RESERVED_BITS = 0x08, + + BMG160_LPM2_AUTOSLEEP_DUR0 = 0x01, + BMG160_LPM2_AUTOSLEEP_DUR1 = 0x02, + BMG160_LPM2_AUTOSLEEP_DUR2 = 0x04, + _BMG160_LPM2_AUTOSLEEP_DUR_MASK = 7, + _BMG160_LPM2_AUTOSLEEP_DUR_SHIFT = 0, + + BMG160_LPM2_EXT_TRIG_SEL0 = 0x10, + BMG160_LPM2_EXT_TRIG_SEL1 = 0x20, + _BMG160_LPM2_EXT_TRIG_SEL_MASK = 3, + _BMG160_LPM2_EXT_TRIG_SEL_SHIFT = 4, + + BMG160_LPM2_POWER_SAVE_MODE = 0x40, + BMG160_LPM2_FAST_POWERUP = 0x80 + } BMG160_LPM2_BITS_T; + + + /** + * LPM2_AUTOSLEEP_DUR values + */ + typedef enum { + BMG160_AUTOSLEEP_DUR_NONE = 0, + BMG160_AUTOSLEEP_DUR_4MS = 1, + BMG160_AUTOSLEEP_DUR_5MS = 2, + BMG160_AUTOSLEEP_DUR_8MS = 3, + BMG160_AUTOSLEEP_DUR_10MS = 4, + BMG160_AUTOSLEEP_DUR_15MS = 5, + BMG160_AUTOSLEEP_DUR_20MS = 6, + BMG160_AUTOSLEEP_DUR_40MS = 7 + } BMG160_AUTOSLEEP_DUR_T; + + /** + * LPM2_EXT_TRIG_SEL values + */ + typedef enum { + BMG160_EXT_TRIG_SEL_NONE = 0, + BMG160_EXT_TRIG_SEL_INT1 = 1, + BMG160_EXT_TRIG_SEL_INT2 = 2, + BMG160_EXT_TRIG_SEL_SDO = 3 // if SPI3 mode + // (unsupported) + } BMG160_EXT_TRIG_SEL_T; + + /** + * REG_RATE_HBW bits + */ + typedef enum { + _BMG160_RATE_HBW_RESERVED_BITS = 0x0f | 0x10 | 0x20, + + BMG160_RATE_HBW_SHADOW_DIS = 0x40, + BMG160_RATE_HBW_DATA_HIGH_BW = 0x80 + } BMG160_RATE_HBW_BITS_T; + + /** + * REG_INT_EN_0 bits + */ + typedef enum { + _BMG160_INT_EN_0_RESERVED_BITS = 0x20 | 0x10 | 0x08 | 0x02 + | 0x01, + + BMG160_INT_EN_0_AUTO_OFFSET_EN = 0x04, + + BMG160_INT_EN_0_FIFO_EN = 0x40, + BMG160_INT_EN_0_DATA_EN = 0x80 + } BMG160_INT_EN_0_BITS_T; + + /** + * REG_INT_EN_1 bits + */ + typedef enum { + _BMG160_INT_EN_1_INT1_RESERVED_BITS = 0xf0, + + BMG160_INT_EN_1_INT1_LVL = 0x01, // level or edge + BMG160_INT_EN_1_INT1_OD = 0x02, // push-pull + // or open + // drain + BMG160_INT_EN_1_INT2_LVL = 0x04, + BMG160_INT_EN_1_INT2_OD = 0x08 + } BMG160_INT_EN_1_BITS_T; + + /** + * REG_INT_MAP_0 bits + */ + typedef enum { + _BMG160_INT_MAP_0_RESERVED_BITS = 0xf0 | 0x04 | 0x01, + + BMG160_INT_MAP_0_INT1_ANY = 0x02, + BMG160_INT_MAP_0_INT1_HIGH = 0x08 + } BMG160_INT_MAP_0_BITS_T; + + /** + * REG_INT_MAP_1 bits + */ + typedef enum { + BMG160_INT_MAP_1_INT1_DATA = 0x01, + BMG160_INT_MAP_1_INT1_FAST_OFFSET = 0x02, + BMG160_INT_MAP_1_INT1_FIFO = 0x04, + BMG160_INT_MAP_1_INT1_AUTO_OFFSET = 0x08, + BMG160_INT_MAP_1_INT2_AUTO_OFFSET = 0x10, + BMG160_INT_MAP_1_INT2_FIFO = 0x20, + BMG160_INT_MAP_1_INT2_FAST_OFFSET = 0x40, + BMG160_INT_MAP_1_INT2_DATA = 0x80 + } BMG160_INT_MAP_1_BITS_T; + + /** + * REG_INT_1A bits + */ + typedef enum { + _BMG160_INT_1A_RESERVED_BITS = 0xd5, + + BMG160_INT_1A_ANY_UNFILT_DATA = 0x02, + BMG160_INT_1A_HIGH_UNFILT_DATA = 0x08, + BMG160_INT_1A_SLOW_OFFSET_UNFILT = 0x20 + } BMG160_INT_1A_BITS_T; + + /** + * REG_INT_1B bits + */ + typedef enum { + BMG160_INT_1B_ANY_TH0 = 0x01, + BMG160_INT_1B_ANY_TH1 = 0x02, + BMG160_INT_1B_ANY_TH2 = 0x04, + BMG160_INT_1B_ANY_TH3 = 0x08, + BMG160_INT_1B_ANY_TH4 = 0x10, + BMG160_INT_1B_ANY_TH5 = 0x20, + BMG160_INT_1B_ANY_TH6 = 0x40, + _BMG160_INT_1B_ANY_TH_MASK = 127, + _BMG160_INT_1B_ANY_TH_SHIFT = 0, + + BMG160_INT_1B_FAST_OFFSET_UNFILT = 0x80 + } BMG160_INT_1B_BITS_T; + + /** + * REG_INT_1C bits + */ + typedef enum { + _BMG160_INT_1C_RESERVED_BITS = 0x08, + + BMG160_INT_1C_ANY_EN_X = 0x01, + BMG160_INT_1C_ANY_EN_Y = 0x02, + BMG160_INT_1C_ANY_EN_Z = 0x04, + + BMG160_INT_1C_ANY_DUR_SAMPLE0 = 0x10, + BMG160_INT_1C_ANY_DUR_SAMPLE1 = 0x20, + BMG160_INT_1C_ANY_DUR_SAMPLE_MASK = 3, + BMG160_INT_1C_ANY_DUR_SAMPLE_SHIFT = 4, + + BMG160_INT_1C_AWAKE_DUR0 = 0x40, + BMG160_INT_1C_AWAKE_DUR1 = 0x80, + BMG160_INT_1C_AWAKE_DUR_MASK = 3, + BMG160_INT_1C_AWAKE_DUR_SHIFT = 6 + } BMG160_INT_1C_BITS_T; + + /** + * INT_1C_ANY_DUR_SAMPLE values + */ + typedef enum { + BMG160_ANY_DUR_SAMPLE_4 = 0, // samples + BMG160_ANY_DUR_SAMPLE_8 = 1, + BMG160_ANY_DUR_SAMPLE_12 = 2, + BMG160_ANY_DUR_SAMPLE_16 = 3 + } BMG160_ANY_DUR_SAMPLE_T; + + /** + * INT_1C_AWAKE_DUR values + */ + typedef enum { + BMG160_AWAKE_DUR_SAMPLE_8 = 0, // samples + BMG160_AWAKE_DUR_SAMPLE_16 = 1, + BMG160_AWAKE_DUR_SAMPLE_32 = 2, + BMG160_AWAKE_DUR_SAMPLE_64 = 3 + } BMG160_AWAKE_DUR_SAMPLE_T; + + /** + * REG_INT_1E bits + */ + typedef enum { + _BMG160_INT_1E_RESERVED_BITS = 0x7f, + + BMG160_INT_1E_FIFO_WM_EN = 0x80 + } BMG160_INT_1E_BITS_T; + + /** + * REG_INT_RST_LATCH bits + */ + typedef enum { + _BMG160_INT_RST_LATCH_RESERVED_BITS = 0x20, + + BMG160_INT_RST_LATCH0 = 0x01, + BMG160_INT_RST_LATCH1 = 0x02, + BMG160_INT_RST_LATCH2 = 0x04, + BMG160_INT_RST_LATCH3 = 0x08, + _BMG160_INT_RST_LATCH_MASK = 15, + _BMG160_INT_RST_LATCH_SHIFT = 0, + + BMG160_INT_RST_LATCH_STATUS_BIT = 0x10, + + BMG160_INT_RST_LATCH_OFFSET_RESET = 0x40, + BMG160_INT_RST_LATCH_RESET_INT = 0x80 + } BMG160_INT_RST_LATCH_BITS_T; + + /** + * RST_LATCH values + */ + typedef enum { + BMG160_RST_LATCH_NON_LATCHED = 0, + BMG160_RST_LATCH_TEMPORARY_250MS = 1, + BMG160_RST_LATCH_TEMPORARY_500MS = 2, + BMG160_RST_LATCH_TEMPORARY_1S = 3, + BMG160_RST_LATCH_TEMPORARY_2S = 4, + BMG160_RST_LATCH_TEMPORARY_4S = 5, + BMG160_RST_LATCH_TEMPORARY_8S = 6, + BMG160_RST_LATCH_LATCHED = 7, + + // 8 == non latched + + BMG160_RST_LATCH_TEMPORARY_250US = 9, + BMG160_RST_LATCH_TEMPORARY_500US = 10, + BMG160_RST_LATCH_TEMPORARY_1MS = 11, + BMG160_RST_LATCH_TEMPORARY_12_5MS = 12, + BMG160_RST_LATCH_TEMPORARY_25MS = 13, + BMG160_RST_LATCH_TEMPORARY_50MS = 14 + + // 15 == latched + } BMG160_RST_LATCH_T; + + /** + * REG_HIGH_TH_* (X, Y, and Z) register bits + */ + typedef enum { + BMG160_HIGH_TH_EN = 0x01, + + BMG160_HIGH_TH_TH0 = 0x02, + BMG160_HIGH_TH_TH1 = 0x04, + BMG160_HIGH_TH_TH2 = 0x08, + BMG160_HIGH_TH_TH3 = 0x10, + BMG160_HIGH_TH_TH4 = 0x20, + _BMG160_HIGH_TH_TH_MASK = 31, + _BMG160_HIGH_TH_TH_SHIFT = 1, + + BMG160_HIGH_TH_HY0 = 0x40, + BMG160_HIGH_TH_HY1 = 0x80, + _BMG160_HIGH_TH_HY_MASK = 3, + _BMG160_HIGH_TH_HY_SHIFT = 6 + } BMG160_HIGH_TH_BITS_T; + + /** + * REG_SOC bits + */ + typedef enum { + BMG160_SOC_SLOW_OFFSET_EN_X = 0x01, + BMG160_SOC_SLOW_OFFSET_EN_Y = 0x02, + BMG160_SOC_SLOW_OFFSET_EN_Z = 0x04, + + BMG160_SOC_SLOW_OFFSET_DUR0 = 0x08, + BMG160_SOC_SLOW_OFFSET_DUR1 = 0x10, + BMG160_SOC_SLOW_OFFSET_DUR2 = 0x20, + _BMG160_SOC_SLOW_OFFSET_DUR_MASK = 7, + _BMG160_SOC_SLOW_OFFSET_DUR_SHIFT = 3, + + BMG160_SOC_SLOW_OFFSET_TH0 = 0x40, + BMG160_SOC_SLOW_OFFSET_TH1 = 0x80, + _BMG160_SOC_SLOW_OFFSET_TH_MASK = 3, + _BMG160_SOC_SLOW_OFFSET_TH_SHIFT = 6 + } BMG160_SOC_BITS_T; + + /** + * SOC_SLOW_OFFSET_DUR values + */ + typedef enum { + BMG160_SLOW_OFFSET_DUR_40MS = 0, // 40ms + BMG160_SLOW_OFFSET_DUR_80MS = 1, + BMG160_SLOW_OFFSET_DUR_160MS = 2, + BMG160_SLOW_OFFSET_DUR_320MS = 3, + BMG160_SLOW_OFFSET_DUR_640MS = 4, + BMG160_SLOW_OFFSET_DUR_1280MS = 5 + } BMG160_SLOW_OFFSET_DUR_T; + + /** + * SOC_SLOW_OFFSET_TH values + */ + typedef enum { + BMG160_SLOW_OFFSET_TH_0_1 = 0, // 0.1 degree/s + BMG160_SLOW_OFFSET_TH_0_2 = 1, + BMG160_SLOW_OFFSET_TH_0_5 = 2, + BMG160_SLOW_OFFSET_TH_1 = 3 + } BMG160_SLOW_OFFSET_TH_T; + + /** + * REG_A_FOC bits + */ + typedef enum { + BMG160_A_FOC_FAST_OFFSET_EN_X = 0x01, + BMG160_A_FOC_FAST_OFFSET_EN_Y = 0x02, + BMG160_A_FOC_FAST_OFFSET_EN_Z = 0x04, + + BMG160_A_FOC_FAST_OFFSET_EN = 0x08, + + BMG160_A_FOC_FAST_OFFSET_WORDLENGTH0 = 0x10, + BMG160_A_FOC_FAST_OFFSET_WORDLENGTH1 = 0x20, + _BMG160_A_FOC_FAST_OFFSET_WORDLENGTH_MASK = 3, + _BMG160_A_FOC_FAST_OFFSET_WORDLENGTH_SHIFT = 4, + + BMG160_A_FOC_AUTO_OFFSET_WORDLENGTH0 = 0x40, + BMG160_A_FOC_AUTO_OFFSET_WORDLENGTH1 = 0x80, + _BMG160_A_FOC_AUTO_OFFSET_WORDLENGTH_MASK = 3, + _BMG160_A_FOC_AUTO_OFFSET_WORDLENGTH_SHIFT = 6 + } BMG160_A_FOC_BITS_T; + + /** + * FAST_OFFSET_WORDLENGTH values + */ + typedef enum { + BMG160_FAST_OFFSET_WORDLENGTH_32 = 0, // samples + BMG160_FAST_OFFSET_WORDLENGTH_64 = 1, + BMG160_FAST_OFFSET_WORDLENGTH_128 = 2, + BMG160_FAST_OFFSET_WORDLENGTH_256 = 3 + } BMG160_FAST_OFFSET_WORDLENGTH_T; + + /** + * AUTO_OFFSET_WORDLENGTH values + */ + typedef enum { + BMG160_AUTO_OFFSET_WORDLENGTH_32 = 0, // samples + BMG160_AUTO_OFFSET_WORDLENGTH_64 = 1, + BMG160_AUTO_OFFSET_WORDLENGTH_128 = 2, + BMG160_AUTO_OFFSET_WORDLENGTH_256 = 3 + } BMG160_AUTO_OFFSET_WORDLENGTH_T; + + /** + * REG_TRIM_NVM_CTRL bits + */ + typedef enum { + BMG160_TRIM_NVM_CTRL_NVM_PROG_MODE = 0x01, + BMG160_TRIM_NVM_CTRL_NVM_PROG_TRIG = 0x02, + BMG160_TRIM_NVM_CTRL_NVM_PROG_RDY = 0x04, + BMG160_TRIM_NVM_CTRL_NVM_PROG_LOAD = 0x08, + + BMG160_TRIM_NVM_CTRL_NVM_REMAIN0 = 0x10, + BMG160_TRIM_NVM_CTRL_NVM_REMAIN1 = 0x20, + BMG160_TRIM_NVM_CTRL_NVM_REMAIN2 = 0x40, + BMG160_TRIM_NVM_CTRL_NVM_REMAIN3 = 0x80, + _BMG160_TRIM_NVM_CTRL_NVM_REMAIN_MASK = 15, + _BMG160_TRIM_NVM_CTRL_NVM_REMAIN_SHIFT = 4 + } BMG160_TRIM_NVM_CTRL_BITS_T; + + /** + * REG_SPI3_WDT bits + */ + typedef enum { + _BMG160_SPI3_WDT_RESERVED_BITS = 0xf0 | 0x08, + + BMG160_SPI3_WDT_SPI3 = 0x01, // 3-wire SPI - + // NOT + // SUPPORTED + + BMG160_SPI3_WDT_I2C_WDT_SEL = 0x02, + BMG160_SPI3_WDT_I2C_WDT_EN = 0x04 + + // 0x08-0x80 reserved + } BMG160_SPI3_WDT_BITS_T; + + /** + * REG_OFC1 bits, the missing x, y, and z llsb bits are in GP0 + */ + typedef enum { + BMG160_OFC1_OFFSET_Z0 = 0x01, // Z lsb (3:1) + BMG160_OFC1_OFFSET_Z1 = 0x02, + BMG160_OFC1_OFFSET_Z2 = 0x04, + _BMG160_OFC1_OFFSET_Z_MASK = 7, + _BMG160_OFC1_OFFSET_Z_SHIFT = 0, + + BMG160_OFC1_OFFSET_Y0 = 0x08, // Y lsb (3:1) + BMG160_OFC1_OFFSET_Y1 = 0x10, + BMG160_OFC1_OFFSET_Y2 = 0x20, + _BMG160_OFC1_OFFSET_Y_MASK = 7, + _BMG160_OFC1_OFFSET_Y_SHIFT = 3, + + BMG160_OFC1_OFFSET_X0 = 0x08, // bits 3:2 of + // X lsb. geez + BMG160_OFC1_OFFSET_X1 = 0x10, + _BMG160_OFC1_OFFSET_X_MASK = 3, + _BMG160_OFC1_OFFSET_X_SHIFT = 6 + } BMG160_OFC1_OFFSET_BITS_T; + + /** + * REG_GP0 bits + */ + typedef enum { + BMG160_GP0_OFFSET_Z = 0x01, // Z llsb (bit 0) + BMG160_GP0_OFFSET_Y = 0x02, // Y llsb (bit 0) + + BMG160_GP0_OFFSET_X0 = 0x04, // X llsbs (bits 1:0) + BMG160_GP0_OFFSET_X1 = 0x08, + _BMG160_GP0_OFFSET_X_MASK = 3, + _BMG160_GP0_OFFSET_X_SHIFT = 2, + + BMG160_GP0_GP00 = 0x10, + BMG160_GP0_GP01 = 0x20, + BMG160_GP0_GP02 = 0x40, + BMG160_GP0_GP03 = 0x80, + _BMG160_GP0_GP0_MASK = 15, + _BMG160_GP0_GP0_SHIFT = 4 + } BMG160_GP0_BITS_T; + + /** + * REG_BIST bits + */ + typedef enum { + _BMG160_BIST_RESERVED_BITS = 0x80 | 0x40 | 0x20 | 0x08, + + BMG160_BIST_TRIG_BIST = 0x01, + BMG160_BIST_BIST_RDY = 0x02, + BMG160_BIST_BIST_FAIL = 0x04, + + BMG160_BIST_RATE_OK = 0x10 + } BMG160_BIST_BITS_T; + + /** + * REG_FIFO_CONFIG_0 bits + */ + typedef enum { + BMG160_FIFO_CONFIG_0_WATER_MARK0 = 0x01, + BMG160_FIFO_CONFIG_0_WATER_MARK1 = 0x02, + BMG160_FIFO_CONFIG_0_WATER_MARK2 = 0x04, + BMG160_FIFO_CONFIG_0_WATER_MARK3 = 0x08, + BMG160_FIFO_CONFIG_0_WATER_MARK4 = 0x10, + BMG160_FIFO_CONFIG_0_WATER_MARK5 = 0x20, + BMG160_FIFO_CONFIG_0_WATER_MARK6 = 0x40, + _BMG160_FIFO_CONFIG_0_WATER_MARK_MASK = 127, + _BMG160_FIFO_CONFIG_0_WATER_MARK_SHIFT = 0, + + BMG160_FIFO_CONFIG_0_TAG = 0x80 + } BMG160_FIFO_CONFIG_0_BITS_T; + + /** + * REG_FIFO_CONFIG_1 bits + */ + typedef enum { + _BMG160_FIFO_CONFIG_1_RESERVED_BITS = 0x20 | 0x10 |0x08 | 0x04, + + BMG160_FIFO_CONFIG_1_FIFO_DATA_SEL0 = 0x01, + BMG160_FIFO_CONFIG_1_FIFO_DATA_SEL1 = 0x02, + _BMG160_FIFO_CONFIG_1_FIFO_DATA_SEL = 3, + _BMG160_FIFO_CONFIG_1_FIFO_DATA_SHIFT = 0, + + BMG160_FIFO_CONFIG_1_FIFO_MODE0 = 0x40, + BMG160_FIFO_CONFIG_1_FIFO_MODE1 = 0x80, + _BMG160_FIFO_CONFIG_1_FIFO_MODE_MASK = 3, + _BMG160_FIFO_CONFIG_1_FIFO_MODE_SHIFT = 6 + } BMG160_FIFO_CONFIG_1_BITS_T; + + /** + * FIFO_DATA_SEL values + */ + typedef enum { + BMG160_FIFO_DATA_SEL_XYZ = 0, + BMG160_FIFO_DATA_SEL_X = 1, + BMG160_FIFO_DATA_SEL_Y = 2, + BMG160_FIFO_DATA_SEL_Z = 3 + } BMG160_FIFO_DATA_SEL_T; + + /** + * FIFO_MODE values + */ + typedef enum { + BMG160_FIFO_MODE_BYPASS = 0, + BMG160_FIFO_MODE_FIFO = 1, + BMG160_FIFO_MODE_STREAM = 2 + } BMG160_FIFO_MODE_T; + + // interrupt selection for installISR() and uninstallISR() + typedef enum { + BMG160_INTERRUPT_INT1, + BMG160_INTERRUPT_INT2 + } BMG160_INTERRUPT_PINS_T; + +#ifdef __cplusplus +} +#endif diff --git a/src/bmg160/bmg160_fti.c b/src/bmg160/bmg160_fti.c new file mode 100644 index 00000000..03ef0b10 --- /dev/null +++ b/src/bmg160/bmg160_fti.c @@ -0,0 +1,106 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "bmg160.h" +#include "upm_fti.h" + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_bmg160_name[] = "BMG160"; +const char upm_bmg160_description[] = "Triple Axis Digital Gyroscope"; +const upm_protocol_t upm_bmg160_protocol[] = {UPM_I2C, UPM_SPI}; +const upm_sensor_t upm_bmg160_category[] = {UPM_GYROSCOPE}; + +// forward declarations +const void* upm_bmg160_get_ft(upm_sensor_t sensor_type); +void* upm_bmg160_init_name(); +void upm_bmg160_close(void *dev); +upm_result_t upm_bmg160_get_value(void *dev, float *value); + +const upm_sensor_descriptor_t upm_bmg160_get_descriptor() +{ + upm_sensor_descriptor_t usd; + usd.name = upm_bmg160_name; + usd.description = upm_bmg160_description; + usd.protocol_size = 2; + usd.protocol = upm_bmg160_protocol; + usd.category_size = 1; + usd.category = upm_bmg160_category; + return usd; +} + +static const upm_sensor_ft ft = +{ + .upm_sensor_init_name = &upm_bmg160_init_name, + .upm_sensor_close = &upm_bmg160_close, +}; + +static const upm_gyroscope_ft gft = +{ + .upm_gyroscope_get_value = &upm_bmg160_get_value +}; + +const void* upm_bmg160_get_ft(upm_sensor_t sensor_type) +{ + switch(sensor_type) + { + case UPM_SENSOR: + return &ft; + + case UPM_GYROSCOPE: + return &gft; + + default: + return NULL; + } +} + +void* upm_bmg160_init_name() +{ + return NULL; +} + + +void upm_bmg160_close(void *dev) +{ + bmg160_close((bmg160_context)dev); +} + +upm_result_t upm_bmg160_get_value(void *dev, float *value) +{ + if (bmg160_update((bmg160_context)dev)) + return UPM_ERROR_OPERATION_FAILED; + + float x, y, z; + + bmg160_get_gyroscope(dev, &x, &y, &z); + + value[0] = x; + value[1] = y; + value[2] = z; + + return UPM_SUCCESS; +} diff --git a/src/bmg160/javaupm_bmg160.i b/src/bmg160/javaupm_bmg160.i new file mode 100644 index 00000000..94cfa356 --- /dev/null +++ b/src/bmg160/javaupm_bmg160.i @@ -0,0 +1,23 @@ +%module javaupm_bmg160 +%include "../upm.i" +%include "typemaps.i" +%include "../upm_vectortypes.i" + +%ignore getGyroscope(float *, float *, float *); + +%include "bmg160_defs.h" +%include "bmg160.hpp" +%{ + #include "bmg160.hpp" +%} + +%pragma(java) jniclasscode=%{ + static { + try { + System.loadLibrary("javaupm_bmg160"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. \n" + e); + System.exit(1); + } + } +%} diff --git a/src/bmg160/jsupm_bmg160.i b/src/bmg160/jsupm_bmg160.i new file mode 100644 index 00000000..ae050f3e --- /dev/null +++ b/src/bmg160/jsupm_bmg160.i @@ -0,0 +1,14 @@ +%module jsupm_bmg160 +%include "../upm.i" +%include "cpointer.i" +%include "../upm_vectortypes.i" + +/* Send "int *" and "float *" to JavaScript as intp and floatp */ +%pointer_functions(int, intp); +%pointer_functions(float, floatp); + +%include "bmg160_defs.h" +%include "bmg160.hpp" +%{ + #include "bmg160.hpp" +%} diff --git a/src/bmg160/pyupm_bmg160.i b/src/bmg160/pyupm_bmg160.i new file mode 100644 index 00000000..e226322c --- /dev/null +++ b/src/bmg160/pyupm_bmg160.i @@ -0,0 +1,22 @@ +// Include doxygen-generated documentation +%include "pyupm_doxy2swig.i" +%module pyupm_bmg160 +%include "../upm.i" +%include "cpointer.i" +%include "../upm_vectortypes.i" + +/* Send "int *" and "float *" to python as intp and floatp */ +%pointer_functions(int, intp); +%pointer_functions(float, floatp); + +%feature("autodoc", "3"); + +#ifdef DOXYGEN +%include "bmg160_doc.i" +#endif + +%include "bmg160_defs.h" +%include "bmg160.hpp" +%{ + #include "bmg160.hpp" +%}