Library for Bosch Sensortec BMI160 IMU

Dependents:   Rocket MAX32630FTHR_JOYSTICK MAX32630FTHR_IMU_Hello_World Pike_the_Flipper_Main_Branch ... more

Fork of BMI160 by Justin Jordan

Committer:
j3
Date:
Tue Dec 20 19:32:26 2016 +0000
Revision:
14:646eb94fa2eb
Parent:
13:5d132f873b07
Child:
15:dc35ccc0b08e
working on docs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:bb5b832891fb 1 /**********************************************************************
j3 0:bb5b832891fb 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:bb5b832891fb 3 *
j3 0:bb5b832891fb 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:bb5b832891fb 5 * copy of this software and associated documentation files (the "Software"),
j3 0:bb5b832891fb 6 * to deal in the Software without restriction, including without limitation
j3 0:bb5b832891fb 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:bb5b832891fb 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:bb5b832891fb 9 * Software is furnished to do so, subject to the following conditions:
j3 0:bb5b832891fb 10 *
j3 0:bb5b832891fb 11 * The above copyright notice and this permission notice shall be included
j3 0:bb5b832891fb 12 * in all copies or substantial portions of the Software.
j3 0:bb5b832891fb 13 *
j3 0:bb5b832891fb 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:bb5b832891fb 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:bb5b832891fb 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:bb5b832891fb 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:bb5b832891fb 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:bb5b832891fb 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:bb5b832891fb 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:bb5b832891fb 21 *
j3 0:bb5b832891fb 22 * Except as contained in this notice, the name of Maxim Integrated
j3 0:bb5b832891fb 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:bb5b832891fb 24 * Products, Inc. Branding Policy.
j3 0:bb5b832891fb 25 *
j3 0:bb5b832891fb 26 * The mere transfer of this software does not imply any licenses
j3 0:bb5b832891fb 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:bb5b832891fb 28 * trademarks, maskwork rights, or any other form of intellectual
j3 0:bb5b832891fb 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:bb5b832891fb 30 * ownership rights.
j3 0:bb5b832891fb 31 **********************************************************************/
j3 0:bb5b832891fb 32
j3 0:bb5b832891fb 33
j3 0:bb5b832891fb 34 #ifndef BMI160_H
j3 0:bb5b832891fb 35 #define BMI160_H
j3 0:bb5b832891fb 36
j3 0:bb5b832891fb 37 #include "mbed.h"
j3 0:bb5b832891fb 38
j3 0:bb5b832891fb 39 /**
j3 0:bb5b832891fb 40 @brief The BMI160 is a small, low power, low noise 16-bit inertial measurement
j3 0:bb5b832891fb 41 unit designed for use in mobile applications like augmented reality or indoor
j3 0:bb5b832891fb 42 navigation which require highly accurate, real-time sensor data.
j3 0:bb5b832891fb 43
j3 0:bb5b832891fb 44 In full operation mode, with both the accelerometer and gyroscope enabled, the
j3 0:bb5b832891fb 45 current consumption is typically 950 μA, enabling always-on applications in
j3 0:bb5b832891fb 46 battery driven devices. It is available in a compact 14-pin 2.5 x 3.0 x 0.8 mm³
j3 0:bb5b832891fb 47 LGA package."
j3 2:598e601e5846 48
j3 2:598e601e5846 49 This class is an abstract base class and can not be instaniated, use BMI160_I2C
j3 2:598e601e5846 50 or BMI160_SPI.
j3 0:bb5b832891fb 51 */
j3 0:bb5b832891fb 52 class BMI160
j3 0:bb5b832891fb 53 {
j3 0:bb5b832891fb 54 public:
j3 0:bb5b832891fb 55
j3 2:598e601e5846 56 ///Return value on success.
j3 3:e1770675eca4 57 static const uint8_t RTN_NO_ERROR = 0;
j3 0:bb5b832891fb 58
j3 13:5d132f873b07 59 ///Sensor types
j3 5:35e032c8d8aa 60 enum Sensors
j3 5:35e032c8d8aa 61 {
j3 5:35e032c8d8aa 62 MAG = 0, ///<Optional external sensor
j3 5:35e032c8d8aa 63 GYRO, ///<Angular rate sensor
j3 5:35e032c8d8aa 64 ACC ///<g sensor
j3 5:35e032c8d8aa 65 };
j3 5:35e032c8d8aa 66
j3 8:a89b529b1d96 67 ///Sensor Axis
j3 8:a89b529b1d96 68 enum SensorAxis
j3 8:a89b529b1d96 69 {
j3 8:a89b529b1d96 70 X_AXIS = 0,
j3 8:a89b529b1d96 71 Y_AXIS,
j3 8:a89b529b1d96 72 Z_AXIS
j3 8:a89b529b1d96 73 };
j3 8:a89b529b1d96 74
j3 8:a89b529b1d96 75 ///Structure for axis data
j3 8:a89b529b1d96 76 struct AxisData
j3 8:a89b529b1d96 77 {
j3 8:a89b529b1d96 78 int16_t raw; ///<Axis raw data
j3 8:a89b529b1d96 79 float scaled; ///<Axis scaled data
j3 8:a89b529b1d96 80 };
j3 8:a89b529b1d96 81
j3 8:a89b529b1d96 82 ///Structure for holding sensor data
j3 8:a89b529b1d96 83 struct SensorData
j3 8:a89b529b1d96 84 {
j3 8:a89b529b1d96 85 AxisData xAxis; ///<Sensor X axis data
j3 8:a89b529b1d96 86 AxisData yAxis; ///<Sensor Y axis data
j3 8:a89b529b1d96 87 AxisData zAxis; ///<Sensor Z axis data
j3 8:a89b529b1d96 88 };
j3 8:a89b529b1d96 89
j3 13:5d132f873b07 90
j3 1:a4c911640569 91 ///BMI160 registers
j3 0:bb5b832891fb 92 enum Registers
j3 0:bb5b832891fb 93 {
j3 3:e1770675eca4 94 CHIP_ID = 0x00, ///<Chip Identification.
j3 3:e1770675eca4 95 ERR_REG = 0x02, ///<Reports sensor error flags. Flags reset when read.
j3 3:e1770675eca4 96 PMU_STATUS, ///<Reports current power mode for sensors.
j3 3:e1770675eca4 97 DATA_0, ///<MAG_X axis bits7:0
j3 3:e1770675eca4 98 DATA_1, ///<MAG_X axis bits15:8
j3 3:e1770675eca4 99 DATA_2, ///<MAG_Y axis bits7:0
j3 3:e1770675eca4 100 DATA_3, ///<MAG_Y axis bits15:8
j3 3:e1770675eca4 101 DATA_4, ///<MAG_Z axis bits7:0
j3 3:e1770675eca4 102 DATA_5, ///<MAG_Z axis bits15:8
j3 3:e1770675eca4 103 DATA_6, ///<RHALL bits7:0
j3 3:e1770675eca4 104 DATA_7, ///<RHALL bits15:8
j3 3:e1770675eca4 105 DATA_8, ///<GYR_X axis bits7:0
j3 3:e1770675eca4 106 DATA_9, ///<GYR_X axis bits15:8
j3 3:e1770675eca4 107 DATA_10, ///<GYR_Y axis bits7:0
j3 3:e1770675eca4 108 DATA_11, ///<GYR_Y axis bits15:8
j3 3:e1770675eca4 109 DATA_12, ///<GYR_Z axis bits7:0
j3 3:e1770675eca4 110 DATA_13, ///<GYR_Z axis bits15:8
j3 3:e1770675eca4 111 DATA_14, ///<ACC_X axis bits7:0
j3 3:e1770675eca4 112 DATA_15, ///<ACC_X axis bits15:8
j3 3:e1770675eca4 113 DATA_16, ///<ACC_Y axis bits7:0
j3 3:e1770675eca4 114 DATA_17, ///<ACC_Y axis bits15:8
j3 3:e1770675eca4 115 DATA_18, ///<ACC_Z axis bits7:0
j3 3:e1770675eca4 116 DATA_19, ///<ACC_Z axis bits15:8
j3 3:e1770675eca4 117 SENSORTIME_0, ///<24bit counter synchronized with data, bits7:0
j3 3:e1770675eca4 118 SENSORTIME_1, ///<24bit counter synchronized with data, bits15:8
j3 3:e1770675eca4 119 SENSORTIME_2, ///<24bit counter synchronized with data, bits23:16
j3 3:e1770675eca4 120 STATUS, ///<Reports sensors status flags
j3 3:e1770675eca4 121 INT_STATUS_0, ///<Contains interrupt status flags
j3 3:e1770675eca4 122 INT_STATUS_1, ///<Contains interrupt status flags
j3 3:e1770675eca4 123 INT_STATUS_2, ///<Contains interrupt status flags
j3 3:e1770675eca4 124 INT_STATUS_3, ///<Contains interrupt status flags
j3 3:e1770675eca4 125 TEMPERATURE_0, ///<Contains temperature of sensor, bits7:0
j3 3:e1770675eca4 126 TEMPERATURE_1, ///<Contains temperature of sensor, bits15:8
j3 3:e1770675eca4 127 FIFO_LENGTH_0, ///<Current fill level of FIFO, bits7:0
j3 3:e1770675eca4 128 FIFO_LENGTH_1, ///<Current fill level of FIFO, bits10:8
j3 3:e1770675eca4 129 FIFO_DATA, ///<FIFO data read out register, burst read
j3 3:e1770675eca4 130 ACC_CONF = 0x40, ///<Set ODR, bandwidth, and read mode of accelerometer
j3 3:e1770675eca4 131 ACC_RANGE, ///<Sets accelerometer g-range
j3 3:e1770675eca4 132 GYR_CONF, ///<Set ODR, bandwidth, and read mode of gyroscope
j3 3:e1770675eca4 133 GYR_RANGE, ///<Sets gyroscope angular rate measurement range
j3 3:e1770675eca4 134 MAG_CONF, ///<Sets ODR of magnetometer interface
j3 3:e1770675eca4 135 FIFO_DOWNS, ///<Sets down sampling ratios of accel and gyro data
j3 3:e1770675eca4 136 ///<for FIFO
j3 3:e1770675eca4 137 FIFO_CONFIG_0, ///<Sets FIFO Watermark
j3 3:e1770675eca4 138 FIFO_CONFIG_1, ///<Sets which sensor data is available in FIFO,
j3 3:e1770675eca4 139 ///<Header/Headerless mode, Ext Int tagging, Sensortime
j3 3:e1770675eca4 140 MAG_IF_0 = 0x4B, ///<Magnetometer 7-bit I2C address, bits7:1
j3 3:e1770675eca4 141 MAG_IF_1, ///<Magnetometer interface configuration
j3 3:e1770675eca4 142 MAG_IF_2, ///<Magnetometer address to read
j3 3:e1770675eca4 143 MAG_IF_3, ///<Magnetometer address to write
j3 3:e1770675eca4 144 MAG_IF_4, ///<Magnetometer data to write
j3 3:e1770675eca4 145 INT_EN_0, ///<Interrupt enable bits
j3 3:e1770675eca4 146 INT_EN_1, ///<Interrupt enable bits
j3 3:e1770675eca4 147 INT_EN_2, ///<Interrupt enable bits
j3 3:e1770675eca4 148 INT_OUT_CTRL, ///<Contains the behavioral configuration of INT pins
j3 3:e1770675eca4 149 INT_LATCH, ///<Contains the interrupt rest bit and the interrupt
j3 3:e1770675eca4 150 ///<mode selection
j3 3:e1770675eca4 151 INT_MAP_0, ///<Controls which interrupt signals are mapped to the
j3 3:e1770675eca4 152 ///<INT1 and INT2 pins
j3 3:e1770675eca4 153 INT_MAP_1, ///<Controls which interrupt signals are mapped to the
j3 3:e1770675eca4 154 ///<INT1 and INT2 pins
j3 3:e1770675eca4 155 INT_MAP_2, ///<Controls which interrupt signals are mapped to the
j3 3:e1770675eca4 156 ///<INT1 and INT2 pins
j3 3:e1770675eca4 157 INT_DATA_0, ///<Contains the data source definition for the two
j3 3:e1770675eca4 158 ///<interrupt groups
j3 3:e1770675eca4 159 INT_DATA_1, ///<Contains the data source definition for the two
j3 3:e1770675eca4 160 ///<interrupt groups
j3 3:e1770675eca4 161 INT_LOWHIGH_0, ///<Contains the configuration for the low g interrupt
j3 3:e1770675eca4 162 INT_LOWHIGH_1, ///<Contains the configuration for the low g interrupt
j3 3:e1770675eca4 163 INT_LOWHIGH_2, ///<Contains the configuration for the low g interrupt
j3 3:e1770675eca4 164 INT_LOWHIGH_3, ///<Contains the configuration for the low g interrupt
j3 3:e1770675eca4 165 INT_LOWHIGH_4, ///<Contains the configuration for the low g interrupt
j3 3:e1770675eca4 166 INT_MOTION_0, ///<Contains the configuration for the any motion and
j3 3:e1770675eca4 167 ///<no motion interrupts
j3 3:e1770675eca4 168 INT_MOTION_1, ///<Contains the configuration for the any motion and
j3 3:e1770675eca4 169 ///<no motion interrupts
j3 3:e1770675eca4 170 INT_MOTION_2, ///<Contains the configuration for the any motion and
j3 3:e1770675eca4 171 ///<no motion interrupts
j3 3:e1770675eca4 172 INT_MOTION_3, ///<Contains the configuration for the any motion and
j3 3:e1770675eca4 173 ///<no motion interrupts
j3 3:e1770675eca4 174 INT_TAP_0, ///<Contains the configuration for the tap interrupts
j3 3:e1770675eca4 175 INT_TAP_1, ///<Contains the configuration for the tap interrupts
j3 3:e1770675eca4 176 INT_ORIENT_0, ///<Contains the configuration for the oeientation
j3 3:e1770675eca4 177 ///<interrupt
j3 3:e1770675eca4 178 INT_ORIENT_1, ///<Contains the configuration for the oeientation
j3 3:e1770675eca4 179 ///<interrupt
j3 3:e1770675eca4 180 INT_FLAT_0, ///<Contains the configuration for the flat interrupt
j3 3:e1770675eca4 181 INT_FLAT_1, ///<Contains the configuration for the flat interrupt
j3 3:e1770675eca4 182 FOC_CONF, ///<Contains configuration for the fast offset
j3 3:e1770675eca4 183 ///<compensation for the accelerometer and gyroscope
j3 3:e1770675eca4 184 CONF, ///<Configuration of sensor, nvm_prog_en bit
j3 3:e1770675eca4 185 IF_CONF, ///<Contains settings for the digital interface
j3 3:e1770675eca4 186 PMU_TRIGGER, ///<Sets trigger conditions to change gyro power modes
j3 3:e1770675eca4 187 SELF_TEST, ///<Self test configuration
j3 3:e1770675eca4 188 NV_CONF = 0x70, ///<Contains settings for the digital interface
j3 3:e1770675eca4 189 OFFSET_0, ///<Contains offset comp values for acc_off_x7:0
j3 3:e1770675eca4 190 OFFSET_1, ///<Contains offset comp values for acc_off_y7:0
j3 3:e1770675eca4 191 OFFSET_2, ///<Contains offset comp values for acc_off_z7:0
j3 3:e1770675eca4 192 OFFSET_3, ///<Contains offset comp values for gyr_off_x7:0
j3 3:e1770675eca4 193 OFFSET_4, ///<Contains offset comp values for gyr_off_y7:0
j3 3:e1770675eca4 194 OFFSET_5, ///<Contains offset comp values for gyr_off_z7:0
j3 3:e1770675eca4 195 OFFSET_6, ///<gyr/acc offset enable bit and gyr_off_(zyx) bits9:8
j3 3:e1770675eca4 196 STEP_CNT_0, ///<Step counter bits 15:8
j3 3:e1770675eca4 197 STEP_CNT_1, ///<Step counter bits 7:0
j3 3:e1770675eca4 198 STEP_CONF_0, ///<Contains configuration of the step detector
j3 3:e1770675eca4 199 STEP_CONF_1, ///<Contains configuration of the step detector
j3 3:e1770675eca4 200 CMD = 0x7E ///<Command register triggers operations like
j3 3:e1770675eca4 201 ///<softreset, NVM programming, etc.
j3 3:e1770675eca4 202 };
j3 3:e1770675eca4 203
j3 8:a89b529b1d96 204
j3 13:5d132f873b07 205 ///@name ERR_REG(0x02)
j3 13:5d132f873b07 206 ///Error register data
j3 12:64931a80340d 207 ///@{
j3 13:5d132f873b07 208
j3 14:646eb94fa2eb 209 static const uint8_t FATAL_ERR_MASK = 0x01;
j3 14:646eb94fa2eb 210 static const uint8_t FATAL_ERR_POS = 0x00;
j3 14:646eb94fa2eb 211 static const uint8_t ERR_CODE_MASK = 0x1E;
j3 14:646eb94fa2eb 212 static const uint8_t ERR_CODE_POS = 0x01;
j3 14:646eb94fa2eb 213 static const uint8_t I2C_FAIL_ERR_MASK = 0x20;
j3 14:646eb94fa2eb 214 static const uint8_t I2C_FAIL_ERR_POS = 0x05;
j3 14:646eb94fa2eb 215 static const uint8_t DROP_CMD_ERR_MASK = 0x40;
j3 14:646eb94fa2eb 216 static const uint8_t DROP_CMD_ERR_POS = 0x06;
j3 14:646eb94fa2eb 217 static const uint8_t MAG_DRDY_ERR_MASK = 0x80;
j3 14:646eb94fa2eb 218 static const uint8_t MAG_DRDY_ERR_POS = 0x08;
j3 3:e1770675eca4 219
j3 13:5d132f873b07 220 ///Enumerated error codes
j3 3:e1770675eca4 221 enum ErrorCodes
j3 3:e1770675eca4 222 {
j3 3:e1770675eca4 223 NO_ERROR = 0, ///<No Error
j3 3:e1770675eca4 224 ERROR_1, ///<Listed as error
j3 3:e1770675eca4 225 ERROR_2, ///<Listed as error
j3 3:e1770675eca4 226 LPM_INT_PFD, ///<Low-power mode and interrupt uses pre-filtered
j3 3:e1770675eca4 227 ///<data
j3 4:ebac8c8f6347 228 ODR_MISMATCH = 0x06, ///<ODRs of enabled sensors in headerless mode do
j3 4:ebac8c8f6347 229 ///<not match
j3 3:e1770675eca4 230 PFD_USED_LPM ///<Pre-filtered data are used in low power mode
j3 0:bb5b832891fb 231 };
j3 12:64931a80340d 232 ///@}
j3 0:bb5b832891fb 233
j3 8:a89b529b1d96 234
j3 13:5d132f873b07 235 ///@name ACC_CONF(0x40) and ACC_RANGE(0x41)
j3 13:5d132f873b07 236 ///Data for configuring accelerometer
j3 12:64931a80340d 237 ///@{
j3 13:5d132f873b07 238
j3 13:5d132f873b07 239 static const uint8_t ACC_ODR_MASK = 0x0F;
j3 13:5d132f873b07 240 static const uint8_t ACC_ODR_POS = 0x00;
j3 13:5d132f873b07 241 static const uint8_t ACC_BWP_MASK = 0x70;
j3 12:64931a80340d 242 static const uint8_t ACC_BWP_POS = 0x04;
j3 13:5d132f873b07 243 static const uint8_t ACC_US_MASK = 0x80;
j3 12:64931a80340d 244 static const uint8_t ACC_US_POS = 0x07;
j3 14:646eb94fa2eb 245 static const uint8_t ACC_RANGE_MASK = 0x0F;
j3 14:646eb94fa2eb 246 static const uint8_t ACC_RANGE_POS = 0x00;
j3 8:a89b529b1d96 247
j3 13:5d132f873b07 248 ///Accelerometer output data rates
j3 8:a89b529b1d96 249 enum AccOutPutDataRate
j3 8:a89b529b1d96 250 {
j3 8:a89b529b1d96 251 ACC_ODR_1 = 1, ///< 25/32Hz
j3 8:a89b529b1d96 252 ACC_ODR_2, ///< 25/16Hz
j3 8:a89b529b1d96 253 ACC_ODR_3, ///< 25/8Hz
j3 8:a89b529b1d96 254 ACC_ODR_4, ///< 25/4Hz
j3 8:a89b529b1d96 255 ACC_ODR_5, ///< 25/2Hz
j3 8:a89b529b1d96 256 ACC_ODR_6, ///< 25Hz
j3 8:a89b529b1d96 257 ACC_ODR_7, ///< 50Hz
j3 8:a89b529b1d96 258 ACC_ODR_8, ///< 100Hz
j3 8:a89b529b1d96 259 ACC_ODR_9, ///< 200Hz
j3 8:a89b529b1d96 260 ACC_ODR_10, ///< 400Hz
j3 8:a89b529b1d96 261 ACC_ODR_11, ///< 800Hz
j3 8:a89b529b1d96 262 ACC_ODR_12 ///< 1600Hz
j3 8:a89b529b1d96 263 };
j3 8:a89b529b1d96 264
j3 13:5d132f873b07 265 ///Accelerometer bandwidth parameters
j3 8:a89b529b1d96 266 enum AccBandWidthParam
j3 8:a89b529b1d96 267 {
j3 8:a89b529b1d96 268 ACC_BWP_0 = 0, ///< Average 1 cycle
j3 8:a89b529b1d96 269 ACC_BWP_1, ///< Average 2 cycles
j3 8:a89b529b1d96 270 ACC_BWP_2, ///< Average 4 cycles, use this setting when acc_us = 0
j3 8:a89b529b1d96 271 ACC_BWP_3, ///< Average 8 cycles
j3 8:a89b529b1d96 272 ACC_BWP_4, ///< Average 16 cycles
j3 8:a89b529b1d96 273 ACC_BWP_5, ///< Average 32 cycles
j3 8:a89b529b1d96 274 ACC_BWP_6, ///< Average 64 cycles
j3 8:a89b529b1d96 275 ACC_BWP_7 ///< Average 128 cycles
j3 8:a89b529b1d96 276 };
j3 8:a89b529b1d96 277
j3 13:5d132f873b07 278 ///Accelerometer undersampling
j3 8:a89b529b1d96 279 enum AccUnderSampling
j3 8:a89b529b1d96 280 {
j3 8:a89b529b1d96 281 ACC_US_OFF = 0,
j3 8:a89b529b1d96 282 ACC_US_ON
j3 8:a89b529b1d96 283 };
j3 8:a89b529b1d96 284
j3 13:5d132f873b07 285 ///Accelerometer ranges
j3 8:a89b529b1d96 286 enum AccRange
j3 8:a89b529b1d96 287 {
j3 8:a89b529b1d96 288 SENS_2G = 0, ///<Accelerometer range +-2G
j3 8:a89b529b1d96 289 SENS_4G, ///<Accelerometer range +-4G
j3 8:a89b529b1d96 290 SENS_8G, ///<Accelerometer range +-8G
j3 8:a89b529b1d96 291 SENS_16G, ///<Accelerometer range +-16G
j3 8:a89b529b1d96 292 };
j3 8:a89b529b1d96 293
j3 13:5d132f873b07 294 ///Accelerometer configuration data structure
j3 9:ca6b5fecdd63 295 struct AccConfig
j3 9:ca6b5fecdd63 296 {
j3 14:646eb94fa2eb 297 AccRange range; ///<Accelerometer range
j3 14:646eb94fa2eb 298 AccUnderSampling us; ///<Accelerometr undersampling mode
j3 14:646eb94fa2eb 299 AccBandWidthParam bwp; ///<Accelerometer bandwidth param
j3 14:646eb94fa2eb 300 AccOutPutDataRate odr; ///<Accelerometr output data rate
j3 9:ca6b5fecdd63 301 };
j3 9:ca6b5fecdd63 302
j3 13:5d132f873b07 303 ///Accelerometer default configuration
j3 9:ca6b5fecdd63 304 static const AccConfig DEFAULT_ACC_CONFIG;
j3 12:64931a80340d 305 ///@}
j3 9:ca6b5fecdd63 306
j3 12:64931a80340d 307
j3 14:646eb94fa2eb 308 ///@name GYR_CONF(0x42) and GYR_RANGE(0x43)
j3 14:646eb94fa2eb 309 ///Data for configuring gyroscope
j3 14:646eb94fa2eb 310 ///@{
j3 14:646eb94fa2eb 311
j3 14:646eb94fa2eb 312 ///@}
j3 14:646eb94fa2eb 313
j3 14:646eb94fa2eb 314
j3 13:5d132f873b07 315 ///Enumerated power modes
j3 4:ebac8c8f6347 316 enum PowerModes
j3 4:ebac8c8f6347 317 {
j3 4:ebac8c8f6347 318 SUSPEND = 0, ///<Acc and Gyro, No sampling, No FIFO data readout
j3 4:ebac8c8f6347 319 NORMAL, ///<Acc and Gyro, Full chip operation
j3 4:ebac8c8f6347 320 LOW_POWER, ///<Acc duty-cycling between suspend and normal
j3 4:ebac8c8f6347 321 FAST_START_UP ///<Gyro start up delay time to normal mode <= 10 ms
j3 4:ebac8c8f6347 322 };
j3 4:ebac8c8f6347 323
j3 12:64931a80340d 324
j3 13:5d132f873b07 325 ///Enumerated commands used with CMD register
j3 5:35e032c8d8aa 326 enum Commands
j3 5:35e032c8d8aa 327 {
j3 5:35e032c8d8aa 328 START_FOC = 0x03, ///<Starts Fast Offset Calibrartion
j3 7:9848196cb65e 329 ACC_SET_PMU_MODE = 0x10, ///<Sets acc power mode
j3 8:a89b529b1d96 330 GYR_SET_PMU_MODE = 0x14, ///<Sets gyro power mode
j3 8:a89b529b1d96 331 MAG_SET_PMU_MODE = 0x18, ///<Sets mag power mode
j3 5:35e032c8d8aa 332 PROG_NVM = 0xA0, ///<Writes NVM backed registers into NVM
j3 5:35e032c8d8aa 333 FIFO_FLUSH = 0xB0, ///<Clears FIFO
j3 5:35e032c8d8aa 334 INT_RESET, ///<Clears interrupt engine, INT_STATUS, and
j3 5:35e032c8d8aa 335 ///<the interrupt pin
j3 5:35e032c8d8aa 336 STEP_CNT_CLR, ///<Triggers reset of the step counter
j3 5:35e032c8d8aa 337 SOFT_RESET = 0xB6 ///<Triggers a reset including a reboot.
j3 5:35e032c8d8aa 338 };
j3 5:35e032c8d8aa 339
j3 0:bb5b832891fb 340
j3 0:bb5b832891fb 341 ///@brief BMI160 Destructor.\n
j3 0:bb5b832891fb 342 ///
j3 0:bb5b832891fb 343 ///On Entry:
j3 0:bb5b832891fb 344 ///@param[in] none
j3 0:bb5b832891fb 345 ///
j3 0:bb5b832891fb 346 ///On Exit:
j3 0:bb5b832891fb 347 ///@param[out] none
j3 0:bb5b832891fb 348 ///
j3 0:bb5b832891fb 349 ///@returns none
j3 2:598e601e5846 350 virtual ~BMI160(){ }
j3 0:bb5b832891fb 351
j3 0:bb5b832891fb 352
j3 0:bb5b832891fb 353 ///@brief Reads a single register.\n
j3 0:bb5b832891fb 354 ///
j3 0:bb5b832891fb 355 ///On Entry:
j3 0:bb5b832891fb 356 ///@param[in] data - pointer to memory for storing read data
j3 0:bb5b832891fb 357 ///
j3 0:bb5b832891fb 358 ///On Exit:
j3 0:bb5b832891fb 359 ///@param[out] data - holds contents of read register on success
j3 0:bb5b832891fb 360 ///
j3 0:bb5b832891fb 361 ///@returns 0 on success, non 0 on failure
j3 2:598e601e5846 362 virtual int32_t readRegister(Registers reg, uint8_t *data) = 0;
j3 0:bb5b832891fb 363
j3 0:bb5b832891fb 364
j3 0:bb5b832891fb 365 ///@brief Writes a single register.\n
j3 0:bb5b832891fb 366 ///
j3 0:bb5b832891fb 367 ///On Entry:
j3 0:bb5b832891fb 368 ///@param[in] data - data to write to register
j3 0:bb5b832891fb 369 ///
j3 0:bb5b832891fb 370 ///On Exit:
j3 0:bb5b832891fb 371 ///@param[out] none
j3 0:bb5b832891fb 372 ///
j3 0:bb5b832891fb 373 ///@returns 0 on success, non 0 on failure
j3 2:598e601e5846 374 virtual int32_t writeRegister(Registers reg, const uint8_t data) = 0;
j3 0:bb5b832891fb 375
j3 0:bb5b832891fb 376
j3 0:bb5b832891fb 377 ///@brief Reads a block of registers.\n
j3 0:bb5b832891fb 378 ///@detail User must ensure that all registers between 'startReg' and
j3 0:bb5b832891fb 379 ///'stopReg' exist and are readable. Function reads up to, including,
j3 0:bb5b832891fb 380 ///'stopReg'.\n
j3 0:bb5b832891fb 381 ///
j3 0:bb5b832891fb 382 ///On Entry:
j3 0:bb5b832891fb 383 ///@param[in] startReg - register to start reading from
j3 0:bb5b832891fb 384 ///@param[in] stopReg - register to stop reading from
j3 0:bb5b832891fb 385 ///@param[in] data - pointer to memory for storing read data
j3 0:bb5b832891fb 386 ///
j3 0:bb5b832891fb 387 ///On Exit:
j3 0:bb5b832891fb 388 ///@param[out] data - holds contents of read registers on success
j3 0:bb5b832891fb 389 ///
j3 0:bb5b832891fb 390 ///@returns 0 on success, non 0 on failure
j3 3:e1770675eca4 391 virtual int32_t readBlock(Registers startReg, Registers stopReg,
j3 3:e1770675eca4 392 uint8_t *data) = 0;
j3 0:bb5b832891fb 393
j3 0:bb5b832891fb 394
j3 0:bb5b832891fb 395 ///@brief Writes a block of registers.\n
j3 0:bb5b832891fb 396 ///@detail User must ensure that all registers between 'startReg' and
j3 0:bb5b832891fb 397 ///'stopReg' exist and are writeable. Function writes up to, including,
j3 0:bb5b832891fb 398 ///'stopReg'.\n
j3 0:bb5b832891fb 399 ///
j3 0:bb5b832891fb 400 ///On Entry:
j3 0:bb5b832891fb 401 ///@param[in] startReg - register to start writing at
j3 0:bb5b832891fb 402 ///@param[in] stopReg - register to stop writing at
j3 0:bb5b832891fb 403 ///@param[in] data - pointer to data to write to registers
j3 0:bb5b832891fb 404 ///
j3 0:bb5b832891fb 405 ///On Exit:
j3 0:bb5b832891fb 406 ///@param[out] none
j3 0:bb5b832891fb 407 ///
j3 0:bb5b832891fb 408 ///@returns 0 on success, non 0 on failure
j3 3:e1770675eca4 409 virtual int32_t writeBlock(Registers startReg, Registers stopReg,
j3 3:e1770675eca4 410 const uint8_t *data) = 0;
j3 3:e1770675eca4 411
j3 3:e1770675eca4 412
j3 5:35e032c8d8aa 413 ///@brief Sets sensors power mode through CMD register.\n
j3 5:35e032c8d8aa 414 ///@details Observe command execution times given in datasheet.\n
j3 5:35e032c8d8aa 415 ///
j3 5:35e032c8d8aa 416 ///On Entry:
j3 5:35e032c8d8aa 417 ///@param[in] sensor - Sensor which power mode we are setting
j3 5:35e032c8d8aa 418 ///@param[in] pwrMode - Desired powermode of the sensor
j3 5:35e032c8d8aa 419 ///
j3 5:35e032c8d8aa 420 ///On Exit:
j3 5:35e032c8d8aa 421 ///@param[out]
j3 5:35e032c8d8aa 422 ///
j3 5:35e032c8d8aa 423 ///@returns 0 on success, non 0 on failure
j3 5:35e032c8d8aa 424 int32_t setSensorPowerMode(Sensors sensor, PowerModes pwrMode);
j3 5:35e032c8d8aa 425
j3 5:35e032c8d8aa 426
j3 12:64931a80340d 427 ///@brief Configure Accelerometer.\n
j3 3:e1770675eca4 428 ///
j3 3:e1770675eca4 429 ///On Entry:
j3 12:64931a80340d 430 ///@param[in] config - Accelerometer configuration
j3 3:e1770675eca4 431 ///
j3 3:e1770675eca4 432 ///On Exit:
j3 12:64931a80340d 433 ///@param[out] none
j3 3:e1770675eca4 434 ///
j3 3:e1770675eca4 435 ///@returns 0 on success, non 0 on failure
j3 12:64931a80340d 436 int32_t setAccConfig(const AccConfig &config);
j3 8:a89b529b1d96 437
j3 8:a89b529b1d96 438
j3 14:646eb94fa2eb 439 ///@brief Get accelerometer configuration.\n
j3 14:646eb94fa2eb 440 ///
j3 14:646eb94fa2eb 441 ///On Entry:
j3 14:646eb94fa2eb 442 ///@param[in] config - Accelerometer configuration data structure
j3 14:646eb94fa2eb 443 ///
j3 14:646eb94fa2eb 444 ///On Exit:
j3 14:646eb94fa2eb 445 ///@param[out] config - on success, holds accelerometer's current
j3 14:646eb94fa2eb 446 ///configuration
j3 14:646eb94fa2eb 447 ///
j3 14:646eb94fa2eb 448 ///@returns 0 on success, non 0 on failure
j3 14:646eb94fa2eb 449 int32_t getAccConfig(AccConfig &config);
j3 14:646eb94fa2eb 450
j3 14:646eb94fa2eb 451
j3 8:a89b529b1d96 452 ///@brief Get accelerometer axis as float.\n
j3 3:e1770675eca4 453 ///
j3 3:e1770675eca4 454 ///On Entry:
j3 8:a89b529b1d96 455 ///@param[in] axis - Sensor axis
j3 8:a89b529b1d96 456 ///@param[in] data - AxisData structure
j3 12:64931a80340d 457 ///@param[in] range - Accelerometer range
j3 8:a89b529b1d96 458 ///
j3 8:a89b529b1d96 459 ///On Exit:
j3 8:a89b529b1d96 460 ///@param[out] data - Structure holds raw and scaled axis data
j3 8:a89b529b1d96 461 ///
j3 8:a89b529b1d96 462 ///@returns 0 on success, non 0 on failure
j3 12:64931a80340d 463 int32_t getAccAxis(SensorAxis axis, AxisData &data, AccRange range);
j3 8:a89b529b1d96 464
j3 8:a89b529b1d96 465
j3 8:a89b529b1d96 466 ///@brief Get accelerometer xyz axis as float.\n
j3 8:a89b529b1d96 467 ///
j3 8:a89b529b1d96 468 ///On Entry:
j3 8:a89b529b1d96 469 ///@param[in] data - SensorData structure
j3 12:64931a80340d 470 ///@param[in] range - Accelerometer range
j3 3:e1770675eca4 471 ///
j3 3:e1770675eca4 472 ///On Exit:
j3 8:a89b529b1d96 473 ///@param[out] data - Structure holds raw and scaled data for all three axis
j3 3:e1770675eca4 474 ///
j3 8:a89b529b1d96 475 ///@returns 0 on success, non 0 on failure
j3 12:64931a80340d 476 int32_t getAccXYZ(SensorData &data, AccRange range);
j3 10:9e219f2f1fb3 477
j3 10:9e219f2f1fb3 478
j3 10:9e219f2f1fb3 479 ///@brief Get sensor time.\n
j3 10:9e219f2f1fb3 480 ///
j3 10:9e219f2f1fb3 481 ///On Entry:
j3 10:9e219f2f1fb3 482 ///@param[in] data - pointer to float for holding data
j3 10:9e219f2f1fb3 483 ///
j3 10:9e219f2f1fb3 484 ///On Exit:
j3 10:9e219f2f1fb3 485 ///@param[out] data - time in seconds, as float
j3 10:9e219f2f1fb3 486 ///
j3 10:9e219f2f1fb3 487 ///@returns returns 0 on success, non 0 on failure
j3 10:9e219f2f1fb3 488 int32_t getSensorTime(float *data);
j3 12:64931a80340d 489
j3 12:64931a80340d 490
j3 12:64931a80340d 491 ///@brief Get die temperature.\n
j3 12:64931a80340d 492 ///
j3 12:64931a80340d 493 ///On Entry:
j3 12:64931a80340d 494 ///@param[in] temp - pointer to float for temperature
j3 12:64931a80340d 495 ///
j3 12:64931a80340d 496 ///On Exit:
j3 12:64931a80340d 497 ///@param[out] temp - on success, holds the die temperature
j3 12:64931a80340d 498 ///
j3 12:64931a80340d 499 ///@returns 0 on success, non 0 on failure
j3 12:64931a80340d 500 int32_t getTemperature(float *temp);
j3 2:598e601e5846 501 };
j3 2:598e601e5846 502
j3 2:598e601e5846 503
j3 2:598e601e5846 504 /**
j3 2:598e601e5846 505 @brief BMI160_I2C - supports BMI160 object with I2C interface
j3 2:598e601e5846 506 */
j3 2:598e601e5846 507 class BMI160_I2C: public BMI160
j3 2:598e601e5846 508 {
j3 2:598e601e5846 509 public:
j3 2:598e601e5846 510
j3 2:598e601e5846 511 ///BMI160 default I2C address.
j3 2:598e601e5846 512 static const uint8_t I2C_ADRS_SDO_LO = 0x68;
j3 2:598e601e5846 513 ///BMI160 optional I2C address.
j3 2:598e601e5846 514 static const uint8_t I2C_ADRS_SDO_HI = 0x69;
j3 0:bb5b832891fb 515
j3 2:598e601e5846 516
j3 2:598e601e5846 517 ///@brief BMI160_I2C Constructor.\n
j3 0:bb5b832891fb 518 ///
j3 0:bb5b832891fb 519 ///On Entry:
j3 2:598e601e5846 520 ///@param[in] i2cBus - reference to I2C bus for this device
j3 2:598e601e5846 521 ///@param[in] i2cAdrs - 7-bit I2C address
j3 0:bb5b832891fb 522 ///
j3 0:bb5b832891fb 523 ///On Exit:
j3 0:bb5b832891fb 524 ///@param[out] none
j3 0:bb5b832891fb 525 ///
j3 0:bb5b832891fb 526 ///@returns none
j3 2:598e601e5846 527 BMI160_I2C(I2C &i2cBus, uint8_t i2cAdrs);
j3 2:598e601e5846 528
j3 2:598e601e5846 529
j3 2:598e601e5846 530 virtual int32_t readRegister(Registers reg, uint8_t *data);
j3 2:598e601e5846 531 virtual int32_t writeRegister(Registers reg, const uint8_t data);
j3 3:e1770675eca4 532 virtual int32_t readBlock(Registers startReg, Registers stopReg,
j3 3:e1770675eca4 533 uint8_t *data);
j3 3:e1770675eca4 534 virtual int32_t writeBlock(Registers startReg, Registers stopReg,
j3 3:e1770675eca4 535 const uint8_t *data);
j3 2:598e601e5846 536
j3 0:bb5b832891fb 537 private:
j3 0:bb5b832891fb 538
j3 0:bb5b832891fb 539 I2C m_i2cBus;
j3 0:bb5b832891fb 540 uint8_t m_Wadrs, m_Radrs;
j3 0:bb5b832891fb 541 };
j3 0:bb5b832891fb 542
j3 2:598e601e5846 543
j3 2:598e601e5846 544 /**
j3 2:598e601e5846 545 @brief BMI160_SPI - supports BMI160 object with SPI interface
j3 2:598e601e5846 546 */
j3 2:598e601e5846 547 class BMI160_SPI: public BMI160
j3 2:598e601e5846 548 {
j3 2:598e601e5846 549 public:
j3 2:598e601e5846 550
j3 2:598e601e5846 551 ///@brief BMI160_SPI Constructor.\n
j3 2:598e601e5846 552 ///
j3 2:598e601e5846 553 ///On Entry:
j3 2:598e601e5846 554 ///@param[in] spiBus - reference to SPI bus for this device
j3 2:598e601e5846 555 ///@param[in] cs - reference to DigitalOut used for chip select
j3 2:598e601e5846 556 ///
j3 2:598e601e5846 557 ///On Exit:
j3 2:598e601e5846 558 ///@param[out] none
j3 2:598e601e5846 559 ///
j3 2:598e601e5846 560 ///@returns none
j3 2:598e601e5846 561 BMI160_SPI(SPI &spiBus, DigitalOut &cs);
j3 2:598e601e5846 562
j3 2:598e601e5846 563 virtual int32_t readRegister(Registers reg, uint8_t *data);
j3 2:598e601e5846 564 virtual int32_t writeRegister(Registers reg, const uint8_t data);
j3 3:e1770675eca4 565 virtual int32_t readBlock(Registers startReg, Registers stopReg,
j3 3:e1770675eca4 566 uint8_t *data);
j3 3:e1770675eca4 567 virtual int32_t writeBlock(Registers startReg, Registers stopReg,
j3 3:e1770675eca4 568 const uint8_t *data);
j3 2:598e601e5846 569
j3 2:598e601e5846 570 private:
j3 2:598e601e5846 571
j3 2:598e601e5846 572 SPI m_spiBus;
j3 2:598e601e5846 573 DigitalOut m_cs;
j3 2:598e601e5846 574 };
j3 2:598e601e5846 575
j3 0:bb5b832891fb 576 #endif /* BMI160_H */
j3 8:a89b529b1d96 577
j3 8:a89b529b1d96 578
j3 8:a89b529b1d96 579 ///@brief fx documentation template.\n
j3 8:a89b529b1d96 580 ///
j3 8:a89b529b1d96 581 ///On Entry:
j3 8:a89b529b1d96 582 ///@param[in] none
j3 8:a89b529b1d96 583 ///
j3 8:a89b529b1d96 584 ///On Exit:
j3 8:a89b529b1d96 585 ///@param[out] none
j3 8:a89b529b1d96 586 ///
j3 8:a89b529b1d96 587 ///@returns none