James Bartholomew / LSM9DS1
Committer:
JamB
Date:
Thu Apr 11 22:16:47 2019 +0000
Revision:
0:801ebe391b00
This is a version for my project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JamB 0:801ebe391b00 1 /******************************************************************************
JamB 0:801ebe391b00 2 SFE_LSM9DS1.h
JamB 0:801ebe391b00 3 SFE_LSM9DS1 Library Header File
JamB 0:801ebe391b00 4 Jim Lindblom @ SparkFun Electronics
JamB 0:801ebe391b00 5 Original Creation Date: February 27, 2015
JamB 0:801ebe391b00 6 https://github.com/sparkfun/LSM9DS1_Breakout
JamB 0:801ebe391b00 7
JamB 0:801ebe391b00 8 This file prototypes the LSM9DS1 class, implemented in SFE_LSM9DS1.cpp. In
JamB 0:801ebe391b00 9 addition, it defines every register in the LSM9DS1 (both the Gyro and Accel/
JamB 0:801ebe391b00 10 Magnetometer registers).
JamB 0:801ebe391b00 11
JamB 0:801ebe391b00 12 Development environment specifics:
JamB 0:801ebe391b00 13 IDE: Arduino 1.6.0
JamB 0:801ebe391b00 14 Hardware Platform: Arduino Uno
JamB 0:801ebe391b00 15 LSM9DS1 Breakout Version: 1.0
JamB 0:801ebe391b00 16
JamB 0:801ebe391b00 17 This code is beerware; if you see me (or any other SparkFun employee) at the
JamB 0:801ebe391b00 18 local, and you've found our code helpful, please buy us a round!
JamB 0:801ebe391b00 19
JamB 0:801ebe391b00 20 Distributed as-is; no warranty is given.
JamB 0:801ebe391b00 21 ******************************************************************************/
JamB 0:801ebe391b00 22 #ifndef __SparkFunLSM9DS1_H__
JamB 0:801ebe391b00 23 #define __SparkFunLSM9DS1_H__
JamB 0:801ebe391b00 24
JamB 0:801ebe391b00 25 //#if defined(ARDUINO) && ARDUINO >= 100
JamB 0:801ebe391b00 26 // #include "Arduino.h"
JamB 0:801ebe391b00 27 //#else
JamB 0:801ebe391b00 28 // #include "WProgram.h"
JamB 0:801ebe391b00 29 // #include "pins_arduino.h"
JamB 0:801ebe391b00 30 //#endif
JamB 0:801ebe391b00 31
JamB 0:801ebe391b00 32 #include "mbed.h"
JamB 0:801ebe391b00 33 #include <stdint.h>
JamB 0:801ebe391b00 34 #include "LSM9DS1_Registers.h"
JamB 0:801ebe391b00 35 #include "LSM9DS1_Types.h"
JamB 0:801ebe391b00 36
JamB 0:801ebe391b00 37 #define LSM9DS1_AG_ADDR(sa0) ((sa0) == 0 ? 0x6A : 0x6B)
JamB 0:801ebe391b00 38 #define LSM9DS1_M_ADDR(sa1) ((sa1) == 0 ? 0x1C : 0x1E)
JamB 0:801ebe391b00 39
JamB 0:801ebe391b00 40 enum lsm9ds1_axis {
JamB 0:801ebe391b00 41 X_AXIS,
JamB 0:801ebe391b00 42 Y_AXIS,
JamB 0:801ebe391b00 43 Z_AXIS,
JamB 0:801ebe391b00 44 ALL_AXIS
JamB 0:801ebe391b00 45 };
JamB 0:801ebe391b00 46
JamB 0:801ebe391b00 47 class LSM9DS1
JamB 0:801ebe391b00 48 {
JamB 0:801ebe391b00 49 public:
JamB 0:801ebe391b00 50 IMUSettings settings;
JamB 0:801ebe391b00 51
JamB 0:801ebe391b00 52 // We'll store the gyro, accel, and magnetometer readings in a series of
JamB 0:801ebe391b00 53 // public class variables. Each sensor gets three variables -- one for each
JamB 0:801ebe391b00 54 // axis. Call readGyro(), readAccel(), and readMag() first, before using
JamB 0:801ebe391b00 55 // these variables!
JamB 0:801ebe391b00 56 // These values are the RAW signed 16-bit readings from the sensors.
JamB 0:801ebe391b00 57 int16_t gx, gy, gz; // x, y, and z axis readings of the gyroscope
JamB 0:801ebe391b00 58 int16_t ax, ay, az; // x, y, and z axis readings of the accelerometer
JamB 0:801ebe391b00 59 int16_t mx, my, mz; // x, y, and z axis readings of the magnetometer
JamB 0:801ebe391b00 60 int16_t temperature; // Chip temperature
JamB 0:801ebe391b00 61 float gBias[3], aBias[3], mBias[3];
JamB 0:801ebe391b00 62 int16_t gBiasRaw[3], aBiasRaw[3], mBiasRaw[3];
JamB 0:801ebe391b00 63
JamB 0:801ebe391b00 64 // LSM9DS1 -- LSM9DS1 class constructor
JamB 0:801ebe391b00 65 // The constructor will set up a handful of private variables, and set the
JamB 0:801ebe391b00 66 // communication mode as well.
JamB 0:801ebe391b00 67 /**Input:
JamB 0:801ebe391b00 68 * - interface = Either IMU_MODE_SPI or IMU_MODE_I2C, whichever you're using
JamB 0:801ebe391b00 69 * to talk to the IC.
JamB 0:801ebe391b00 70 * - xgAddr = If IMU_MODE_I2C, this is the I2C address of the accel/gyroscope.
JamB 0:801ebe391b00 71 * If IMU_MODE_SPI, this is the chip select pin of the gyro (CS_AG)
JamB 0:801ebe391b00 72 * - mAddr = If IMU_MODE_I2C, this is the I2C address of the magnetometer.
JamB 0:801ebe391b00 73 * If IMU_MODE_SPI, this is the cs pin of the magnetometer (CS_M)
JamB 0:801ebe391b00 74
JamB 0:801ebe391b00 75 */
JamB 0:801ebe391b00 76 LSM9DS1(PinName sda, PinName scl, uint8_t xgAddr, uint8_t mAddr);
JamB 0:801ebe391b00 77 //LSM9DS1(interface_mode interface, uint8_t xgAddr, uint8_t mAddr);
JamB 0:801ebe391b00 78 //LSM9DS1();
JamB 0:801ebe391b00 79
JamB 0:801ebe391b00 80
JamB 0:801ebe391b00 81 /** begin() -- Initialize the gyro, accelerometer, and magnetometer.
JamB 0:801ebe391b00 82 *This will set up the scale and output rate of each sensor. The values set
JamB 0:801ebe391b00 83 * in the IMUSettings struct will take effect after calling this function.
JamB 0:801ebe391b00 84 */
JamB 0:801ebe391b00 85 uint16_t begin();
JamB 0:801ebe391b00 86
JamB 0:801ebe391b00 87 void calibrate(bool autoCalc = true);
JamB 0:801ebe391b00 88 void calibrateMag(bool loadIn = true);
JamB 0:801ebe391b00 89 void magOffset(uint8_t axis, int16_t offset);
JamB 0:801ebe391b00 90
JamB 0:801ebe391b00 91 /** accelAvailable() -- Polls the accelerometer status register to check
JamB 0:801ebe391b00 92 * if new data is available.
JamB 0:801ebe391b00 93 * Output: 1 - New data available
JamB 0:801ebe391b00 94 * 0 - No new data available
JamB 0:801ebe391b00 95 */
JamB 0:801ebe391b00 96 uint8_t accelAvailable();
JamB 0:801ebe391b00 97
JamB 0:801ebe391b00 98 /** gyroAvailable() -- Polls the gyroscope status register to check
JamB 0:801ebe391b00 99 * if new data is available.
JamB 0:801ebe391b00 100 * Output: 1 - New data available
JamB 0:801ebe391b00 101 * 0 - No new data available
JamB 0:801ebe391b00 102 */
JamB 0:801ebe391b00 103 uint8_t gyroAvailable();
JamB 0:801ebe391b00 104
JamB 0:801ebe391b00 105 /** gyroAvailable() -- Polls the temperature status register to check
JamB 0:801ebe391b00 106 * if new data is available.
JamB 0:801ebe391b00 107 * Output: 1 - New data available
JamB 0:801ebe391b00 108 * 0 - No new data available
JamB 0:801ebe391b00 109 */
JamB 0:801ebe391b00 110 uint8_t tempAvailable();
JamB 0:801ebe391b00 111
JamB 0:801ebe391b00 112 /** magAvailable() -- Polls the accelerometer status register to check
JamB 0:801ebe391b00 113 * if new data is available.
JamB 0:801ebe391b00 114 * Input:
JamB 0:801ebe391b00 115 * - axis can be either X_AXIS, Y_AXIS, Z_AXIS, to check for new data
JamB 0:801ebe391b00 116 * on one specific axis. Or ALL_AXIS (default) to check for new data
JamB 0:801ebe391b00 117 * on all axes.
JamB 0:801ebe391b00 118 * Output: 1 - New data available
JamB 0:801ebe391b00 119 * 0 - No new data available
JamB 0:801ebe391b00 120 */
JamB 0:801ebe391b00 121 uint8_t magAvailable(lsm9ds1_axis axis = ALL_AXIS);
JamB 0:801ebe391b00 122
JamB 0:801ebe391b00 123 /** readGyro() -- Read the gyroscope output registers.
JamB 0:801ebe391b00 124 * This function will read all six gyroscope output registers.
JamB 0:801ebe391b00 125 * The readings are stored in the class' gx, gy, and gz variables. Read
JamB 0:801ebe391b00 126 * those _after_ calling readGyro().
JamB 0:801ebe391b00 127 */
JamB 0:801ebe391b00 128 void readGyro();
JamB 0:801ebe391b00 129
JamB 0:801ebe391b00 130 /** int16_t readGyro(axis) -- Read a specific axis of the gyroscope.
JamB 0:801ebe391b00 131 * [axis] can be any of X_AXIS, Y_AXIS, or Z_AXIS.
JamB 0:801ebe391b00 132 * Input:
JamB 0:801ebe391b00 133 * - axis: can be either X_AXIS, Y_AXIS, or Z_AXIS.
JamB 0:801ebe391b00 134 * Output:
JamB 0:801ebe391b00 135 * A 16-bit signed integer with sensor data on requested axis.
JamB 0:801ebe391b00 136 */
JamB 0:801ebe391b00 137 int16_t readGyro(lsm9ds1_axis axis);
JamB 0:801ebe391b00 138
JamB 0:801ebe391b00 139 /** readAccel() -- Read the accelerometer output registers.
JamB 0:801ebe391b00 140 * This function will read all six accelerometer output registers.
JamB 0:801ebe391b00 141 * The readings are stored in the class' ax, ay, and az variables. Read
JamB 0:801ebe391b00 142 * those _after_ calling readAccel().
JamB 0:801ebe391b00 143 */
JamB 0:801ebe391b00 144 void readAccel();
JamB 0:801ebe391b00 145
JamB 0:801ebe391b00 146 /** int16_t readAccel(axis) -- Read a specific axis of the accelerometer.
JamB 0:801ebe391b00 147 * [axis] can be any of X_AXIS, Y_AXIS, or Z_AXIS.
JamB 0:801ebe391b00 148 * Input:
JamB 0:801ebe391b00 149 * - axis: can be either X_AXIS, Y_AXIS, or Z_AXIS.
JamB 0:801ebe391b00 150 * Output:
JamB 0:801ebe391b00 151 * A 16-bit signed integer with sensor data on requested axis.
JamB 0:801ebe391b00 152 */
JamB 0:801ebe391b00 153 int16_t readAccel(lsm9ds1_axis axis);
JamB 0:801ebe391b00 154
JamB 0:801ebe391b00 155 /** readMag() -- Read the magnetometer output registers.
JamB 0:801ebe391b00 156 * This function will read all six magnetometer output registers.
JamB 0:801ebe391b00 157 * The readings are stored in the class' mx, my, and mz variables. Read
JamB 0:801ebe391b00 158 * those _after_ calling readMag().
JamB 0:801ebe391b00 159 */
JamB 0:801ebe391b00 160 void readMag();
JamB 0:801ebe391b00 161
JamB 0:801ebe391b00 162 /** int16_t readMag(axis) -- Read a specific axis of the magnetometer.
JamB 0:801ebe391b00 163 * [axis] can be any of X_AXIS, Y_AXIS, or Z_AXIS.
JamB 0:801ebe391b00 164 * Input:
JamB 0:801ebe391b00 165 * - axis: can be either X_AXIS, Y_AXIS, or Z_AXIS.
JamB 0:801ebe391b00 166 * Output:
JamB 0:801ebe391b00 167 * A 16-bit signed integer with sensor data on requested axis.
JamB 0:801ebe391b00 168 */
JamB 0:801ebe391b00 169 int16_t readMag(lsm9ds1_axis axis);
JamB 0:801ebe391b00 170
JamB 0:801ebe391b00 171 /** readTemp() -- Read the temperature output register.
JamB 0:801ebe391b00 172 * This function will read two temperature output registers.
JamB 0:801ebe391b00 173 * The combined readings are stored in the class' temperature variables. Read
JamB 0:801ebe391b00 174 * those _after_ calling readTemp().
JamB 0:801ebe391b00 175 */
JamB 0:801ebe391b00 176 void readTemp();
JamB 0:801ebe391b00 177
JamB 0:801ebe391b00 178 /** calcGyro() -- Convert from RAW signed 16-bit value to degrees per second
JamB 0:801ebe391b00 179 * This function reads in a signed 16-bit value and returns the scaled
JamB 0:801ebe391b00 180 * DPS. This function relies on gScale and gRes being correct.
JamB 0:801ebe391b00 181 * Input:
JamB 0:801ebe391b00 182 * - gyro = A signed 16-bit raw reading from the gyroscope.
JamB 0:801ebe391b00 183 */
JamB 0:801ebe391b00 184 float calcGyro(int16_t gyro);
JamB 0:801ebe391b00 185
JamB 0:801ebe391b00 186 /** calcAccel() -- Convert from RAW signed 16-bit value to gravity (g's).
JamB 0:801ebe391b00 187 * This function reads in a signed 16-bit value and returns the scaled
JamB 0:801ebe391b00 188 * g's. This function relies on aScale and aRes being correct.
JamB 0:801ebe391b00 189 * Input:
JamB 0:801ebe391b00 190 * - accel = A signed 16-bit raw reading from the accelerometer.
JamB 0:801ebe391b00 191 */
JamB 0:801ebe391b00 192 float calcAccel(int16_t accel);
JamB 0:801ebe391b00 193
JamB 0:801ebe391b00 194 /** calcMag() -- Convert from RAW signed 16-bit value to Gauss (Gs)
JamB 0:801ebe391b00 195 * This function reads in a signed 16-bit value and returns the scaled
JamB 0:801ebe391b00 196 * Gs. This function relies on mScale and mRes being correct.
JamB 0:801ebe391b00 197 * Input:
JamB 0:801ebe391b00 198 * - mag = A signed 16-bit raw reading from the magnetometer.
JamB 0:801ebe391b00 199 */
JamB 0:801ebe391b00 200 float calcMag(int16_t mag);
JamB 0:801ebe391b00 201
JamB 0:801ebe391b00 202 /** setGyroScale() -- Set the full-scale range of the gyroscope.
JamB 0:801ebe391b00 203 * This function can be called to set the scale of the gyroscope to
JamB 0:801ebe391b00 204 * 245, 500, or 200 degrees per second.
JamB 0:801ebe391b00 205 * Input:
JamB 0:801ebe391b00 206 * - gScl = The desired gyroscope scale. Must be one of three possible
JamB 0:801ebe391b00 207 * values from the gyro_scale.
JamB 0:801ebe391b00 208 */
JamB 0:801ebe391b00 209 void setGyroScale(uint16_t gScl);
JamB 0:801ebe391b00 210
JamB 0:801ebe391b00 211 /** setAccelScale() -- Set the full-scale range of the accelerometer.
JamB 0:801ebe391b00 212 * This function can be called to set the scale of the accelerometer to
JamB 0:801ebe391b00 213 * 2, 4, 6, 8, or 16 g's.
JamB 0:801ebe391b00 214 * Input:
JamB 0:801ebe391b00 215 * - aScl = The desired accelerometer scale. Must be one of five possible
JamB 0:801ebe391b00 216 * values from the accel_scale.
JamB 0:801ebe391b00 217 */
JamB 0:801ebe391b00 218 void setAccelScale(uint8_t aScl);
JamB 0:801ebe391b00 219
JamB 0:801ebe391b00 220 /** setMagScale() -- Set the full-scale range of the magnetometer.
JamB 0:801ebe391b00 221 * This function can be called to set the scale of the magnetometer to
JamB 0:801ebe391b00 222 * 2, 4, 8, or 12 Gs.
JamB 0:801ebe391b00 223 * Input:
JamB 0:801ebe391b00 224 * - mScl = The desired magnetometer scale. Must be one of four possible
JamB 0:801ebe391b00 225 * values from the mag_scale.
JamB 0:801ebe391b00 226 */
JamB 0:801ebe391b00 227 void setMagScale(uint8_t mScl);
JamB 0:801ebe391b00 228
JamB 0:801ebe391b00 229 /** setGyroODR() -- Set the output data rate and bandwidth of the gyroscope
JamB 0:801ebe391b00 230 * Input:
JamB 0:801ebe391b00 231 * - gRate = The desired output rate and cutoff frequency of the gyro.
JamB 0:801ebe391b00 232 */
JamB 0:801ebe391b00 233 void setGyroODR(uint8_t gRate);
JamB 0:801ebe391b00 234
JamB 0:801ebe391b00 235 // setAccelODR() -- Set the output data rate of the accelerometer
JamB 0:801ebe391b00 236 // Input:
JamB 0:801ebe391b00 237 // - aRate = The desired output rate of the accel.
JamB 0:801ebe391b00 238 void setAccelODR(uint8_t aRate);
JamB 0:801ebe391b00 239
JamB 0:801ebe391b00 240 // setMagODR() -- Set the output data rate of the magnetometer
JamB 0:801ebe391b00 241 // Input:
JamB 0:801ebe391b00 242 // - mRate = The desired output rate of the mag.
JamB 0:801ebe391b00 243 void setMagODR(uint8_t mRate);
JamB 0:801ebe391b00 244
JamB 0:801ebe391b00 245 // configInactivity() -- Configure inactivity interrupt parameters
JamB 0:801ebe391b00 246 // Input:
JamB 0:801ebe391b00 247 // - duration = Inactivity duration - actual value depends on gyro ODR
JamB 0:801ebe391b00 248 // - threshold = Activity Threshold
JamB 0:801ebe391b00 249 // - sleepOn = Gyroscope operating mode during inactivity.
JamB 0:801ebe391b00 250 // true: gyroscope in sleep mode
JamB 0:801ebe391b00 251 // false: gyroscope in power-down
JamB 0:801ebe391b00 252 void configInactivity(uint8_t duration, uint8_t threshold, bool sleepOn);
JamB 0:801ebe391b00 253
JamB 0:801ebe391b00 254 // configAccelInt() -- Configure Accelerometer Interrupt Generator
JamB 0:801ebe391b00 255 // Input:
JamB 0:801ebe391b00 256 // - generator = Interrupt axis/high-low events
JamB 0:801ebe391b00 257 // Any OR'd combination of ZHIE_XL, ZLIE_XL, YHIE_XL, YLIE_XL, XHIE_XL, XLIE_XL
JamB 0:801ebe391b00 258 // - andInterrupts = AND/OR combination of interrupt events
JamB 0:801ebe391b00 259 // true: AND combination
JamB 0:801ebe391b00 260 // false: OR combination
JamB 0:801ebe391b00 261 void configAccelInt(uint8_t generator, bool andInterrupts = false);
JamB 0:801ebe391b00 262
JamB 0:801ebe391b00 263 // configAccelThs() -- Configure the threshold of an accelereomter axis
JamB 0:801ebe391b00 264 // Input:
JamB 0:801ebe391b00 265 // - threshold = Interrupt threshold. Possible values: 0-255.
JamB 0:801ebe391b00 266 // Multiply by 128 to get the actual raw accel value.
JamB 0:801ebe391b00 267 // - axis = Axis to be configured. Either X_AXIS, Y_AXIS, or Z_AXIS
JamB 0:801ebe391b00 268 // - duration = Duration value must be above or below threshold to trigger interrupt
JamB 0:801ebe391b00 269 // - wait = Wait function on duration counter
JamB 0:801ebe391b00 270 // true: Wait for duration samples before exiting interrupt
JamB 0:801ebe391b00 271 // false: Wait function off
JamB 0:801ebe391b00 272 void configAccelThs(uint8_t threshold, lsm9ds1_axis axis, uint8_t duration = 0, bool wait = 0);
JamB 0:801ebe391b00 273
JamB 0:801ebe391b00 274 // configGyroInt() -- Configure Gyroscope Interrupt Generator
JamB 0:801ebe391b00 275 // Input:
JamB 0:801ebe391b00 276 // - generator = Interrupt axis/high-low events
JamB 0:801ebe391b00 277 // Any OR'd combination of ZHIE_G, ZLIE_G, YHIE_G, YLIE_G, XHIE_G, XLIE_G
JamB 0:801ebe391b00 278 // - aoi = AND/OR combination of interrupt events
JamB 0:801ebe391b00 279 // true: AND combination
JamB 0:801ebe391b00 280 // false: OR combination
JamB 0:801ebe391b00 281 // - latch: latch gyroscope interrupt request.
JamB 0:801ebe391b00 282 void configGyroInt(uint8_t generator, bool aoi, bool latch);
JamB 0:801ebe391b00 283
JamB 0:801ebe391b00 284 // configGyroThs() -- Configure the threshold of a gyroscope axis
JamB 0:801ebe391b00 285 // Input:
JamB 0:801ebe391b00 286 // - threshold = Interrupt threshold. Possible values: 0-0x7FF.
JamB 0:801ebe391b00 287 // Value is equivalent to raw gyroscope value.
JamB 0:801ebe391b00 288 // - axis = Axis to be configured. Either X_AXIS, Y_AXIS, or Z_AXIS
JamB 0:801ebe391b00 289 // - duration = Duration value must be above or below threshold to trigger interrupt
JamB 0:801ebe391b00 290 // - wait = Wait function on duration counter
JamB 0:801ebe391b00 291 // true: Wait for duration samples before exiting interrupt
JamB 0:801ebe391b00 292 // false: Wait function off
JamB 0:801ebe391b00 293 void configGyroThs(int16_t threshold, lsm9ds1_axis axis, uint8_t duration, bool wait);
JamB 0:801ebe391b00 294
JamB 0:801ebe391b00 295 // configInt() -- Configure INT1 or INT2 (Gyro and Accel Interrupts only)
JamB 0:801ebe391b00 296 // Input:
JamB 0:801ebe391b00 297 // - interrupt = Select INT1 or INT2
JamB 0:801ebe391b00 298 // Possible values: XG_INT1 or XG_INT2
JamB 0:801ebe391b00 299 // - generator = Or'd combination of interrupt generators.
JamB 0:801ebe391b00 300 // Possible values: INT_DRDY_XL, INT_DRDY_G, INT1_BOOT (INT1 only), INT2_DRDY_TEMP (INT2 only)
JamB 0:801ebe391b00 301 // INT_FTH, INT_OVR, INT_FSS5, INT_IG_XL (INT1 only), INT1_IG_G (INT1 only), INT2_INACT (INT2 only)
JamB 0:801ebe391b00 302 // - activeLow = Interrupt active configuration
JamB 0:801ebe391b00 303 // Can be either INT_ACTIVE_HIGH or INT_ACTIVE_LOW
JamB 0:801ebe391b00 304 // - pushPull = Push-pull or open drain interrupt configuration
JamB 0:801ebe391b00 305 // Can be either INT_PUSH_PULL or INT_OPEN_DRAIN
JamB 0:801ebe391b00 306 void configInt(interrupt_select interupt, uint8_t generator,
JamB 0:801ebe391b00 307 h_lactive activeLow = INT_ACTIVE_LOW, pp_od pushPull = INT_PUSH_PULL);
JamB 0:801ebe391b00 308
JamB 0:801ebe391b00 309 /** configMagInt() -- Configure Magnetometer Interrupt Generator
JamB 0:801ebe391b00 310 * Input:
JamB 0:801ebe391b00 311 * - generator = Interrupt axis/high-low events
JamB 0:801ebe391b00 312 * Any OR'd combination of ZIEN, YIEN, XIEN
JamB 0:801ebe391b00 313 * - activeLow = Interrupt active configuration
JamB 0:801ebe391b00 314 * Can be either INT_ACTIVE_HIGH or INT_ACTIVE_LOW
JamB 0:801ebe391b00 315 * - latch: latch gyroscope interrupt request.
JamB 0:801ebe391b00 316 */
JamB 0:801ebe391b00 317 void configMagInt(uint8_t generator, h_lactive activeLow, bool latch = true);
JamB 0:801ebe391b00 318
JamB 0:801ebe391b00 319 /** configMagThs() -- Configure the threshold of a gyroscope axis
JamB 0:801ebe391b00 320 * Input:
JamB 0:801ebe391b00 321 * - threshold = Interrupt threshold. Possible values: 0-0x7FF.
JamB 0:801ebe391b00 322 * Value is equivalent to raw magnetometer value.
JamB 0:801ebe391b00 323 */
JamB 0:801ebe391b00 324 void configMagThs(uint16_t threshold);
JamB 0:801ebe391b00 325
JamB 0:801ebe391b00 326 //! getGyroIntSrc() -- Get contents of Gyroscope interrupt source register
JamB 0:801ebe391b00 327 uint8_t getGyroIntSrc();
JamB 0:801ebe391b00 328
JamB 0:801ebe391b00 329 //! getGyroIntSrc() -- Get contents of accelerometer interrupt source register
JamB 0:801ebe391b00 330 uint8_t getAccelIntSrc();
JamB 0:801ebe391b00 331
JamB 0:801ebe391b00 332 //! getGyroIntSrc() -- Get contents of magnetometer interrupt source register
JamB 0:801ebe391b00 333 uint8_t getMagIntSrc();
JamB 0:801ebe391b00 334
JamB 0:801ebe391b00 335 //! getGyroIntSrc() -- Get status of inactivity interrupt
JamB 0:801ebe391b00 336 uint8_t getInactivity();
JamB 0:801ebe391b00 337
JamB 0:801ebe391b00 338 /** sleepGyro() -- Sleep or wake the gyroscope
JamB 0:801ebe391b00 339 * Input:
JamB 0:801ebe391b00 340 * - enable: True = sleep gyro. False = wake gyro.
JamB 0:801ebe391b00 341 */
JamB 0:801ebe391b00 342 void sleepGyro(bool enable = true);
JamB 0:801ebe391b00 343
JamB 0:801ebe391b00 344 /** enableFIFO() - Enable or disable the FIFO
JamB 0:801ebe391b00 345 * Input:
JamB 0:801ebe391b00 346 * - enable: true = enable, false = disable.
JamB 0:801ebe391b00 347 */
JamB 0:801ebe391b00 348 void enableFIFO(bool enable = true);
JamB 0:801ebe391b00 349
JamB 0:801ebe391b00 350 /** setFIFO() - Configure FIFO mode and Threshold
JamB 0:801ebe391b00 351 * Input:
JamB 0:801ebe391b00 352 * - fifoMode: Set FIFO mode to off, FIFO (stop when full), continuous, bypass
JamB 0:801ebe391b00 353 * Possible inputs: FIFO_OFF, FIFO_THS, FIFO_CONT_TRIGGER, FIFO_OFF_TRIGGER, FIFO_CONT
JamB 0:801ebe391b00 354 * - fifoThs: FIFO threshold level setting
JamB 0:801ebe391b00 355 * Any value from 0-0x1F is acceptable.
JamB 0:801ebe391b00 356 */
JamB 0:801ebe391b00 357 void setFIFO(fifoMode_type fifoMode, uint8_t fifoThs);
JamB 0:801ebe391b00 358
JamB 0:801ebe391b00 359 //! getFIFOSamples() - Get number of FIFO samples
JamB 0:801ebe391b00 360 uint8_t getFIFOSamples();
JamB 0:801ebe391b00 361
JamB 0:801ebe391b00 362
JamB 0:801ebe391b00 363 protected:
JamB 0:801ebe391b00 364 // x_mAddress and gAddress store the I2C address or SPI chip select pin
JamB 0:801ebe391b00 365 // for each sensor.
JamB 0:801ebe391b00 366 uint8_t _mAddress, _xgAddress;
JamB 0:801ebe391b00 367
JamB 0:801ebe391b00 368 // gRes, aRes, and mRes store the current resolution for each sensor.
JamB 0:801ebe391b00 369 // Units of these values would be DPS (or g's or Gs's) per ADC tick.
JamB 0:801ebe391b00 370 // This value is calculated as (sensor scale) / (2^15).
JamB 0:801ebe391b00 371 float gRes, aRes, mRes;
JamB 0:801ebe391b00 372
JamB 0:801ebe391b00 373 // _autoCalc keeps track of whether we're automatically subtracting off
JamB 0:801ebe391b00 374 // accelerometer and gyroscope bias calculated in calibrate().
JamB 0:801ebe391b00 375 bool _autoCalc;
JamB 0:801ebe391b00 376
JamB 0:801ebe391b00 377 // init() -- Sets up gyro, accel, and mag settings to default.
JamB 0:801ebe391b00 378 // - interface - Sets the interface mode (IMU_MODE_I2C or IMU_MODE_SPI)
JamB 0:801ebe391b00 379 // - xgAddr - Sets either the I2C address of the accel/gyro or SPI chip
JamB 0:801ebe391b00 380 // select pin connected to the CS_XG pin.
JamB 0:801ebe391b00 381 // - mAddr - Sets either the I2C address of the magnetometer or SPI chip
JamB 0:801ebe391b00 382 // select pin connected to the CS_M pin.
JamB 0:801ebe391b00 383 void init(interface_mode interface, uint8_t xgAddr, uint8_t mAddr);
JamB 0:801ebe391b00 384
JamB 0:801ebe391b00 385 // initGyro() -- Sets up the gyroscope to begin reading.
JamB 0:801ebe391b00 386 // This function steps through all five gyroscope control registers.
JamB 0:801ebe391b00 387 // Upon exit, the following parameters will be set:
JamB 0:801ebe391b00 388 // - CTRL_REG1_G = 0x0F: Normal operation mode, all axes enabled.
JamB 0:801ebe391b00 389 // 95 Hz ODR, 12.5 Hz cutoff frequency.
JamB 0:801ebe391b00 390 // - CTRL_REG2_G = 0x00: HPF set to normal mode, cutoff frequency
JamB 0:801ebe391b00 391 // set to 7.2 Hz (depends on ODR).
JamB 0:801ebe391b00 392 // - CTRL_REG3_G = 0x88: Interrupt enabled on INT_G (set to push-pull and
JamB 0:801ebe391b00 393 // active high). Data-ready output enabled on DRDY_G.
JamB 0:801ebe391b00 394 // - CTRL_REG4_G = 0x00: Continuous update mode. Data LSB stored in lower
JamB 0:801ebe391b00 395 // address. Scale set to 245 DPS. SPI mode set to 4-wire.
JamB 0:801ebe391b00 396 // - CTRL_REG5_G = 0x00: FIFO disabled. HPF disabled.
JamB 0:801ebe391b00 397 void initGyro();
JamB 0:801ebe391b00 398
JamB 0:801ebe391b00 399 // initAccel() -- Sets up the accelerometer to begin reading.
JamB 0:801ebe391b00 400 // This function steps through all accelerometer related control registers.
JamB 0:801ebe391b00 401 // Upon exit these registers will be set as:
JamB 0:801ebe391b00 402 // - CTRL_REG0_XM = 0x00: FIFO disabled. HPF bypassed. Normal mode.
JamB 0:801ebe391b00 403 // - CTRL_REG1_XM = 0x57: 100 Hz data rate. Continuous update.
JamB 0:801ebe391b00 404 // all axes enabled.
JamB 0:801ebe391b00 405 // - CTRL_REG2_XM = 0x00: 2g scale. 773 Hz anti-alias filter BW.
JamB 0:801ebe391b00 406 // - CTRL_REG3_XM = 0x04: Accel data ready signal on INT1_XM pin.
JamB 0:801ebe391b00 407 void initAccel();
JamB 0:801ebe391b00 408
JamB 0:801ebe391b00 409 // initMag() -- Sets up the magnetometer to begin reading.
JamB 0:801ebe391b00 410 // This function steps through all magnetometer-related control registers.
JamB 0:801ebe391b00 411 // Upon exit these registers will be set as:
JamB 0:801ebe391b00 412 // - CTRL_REG4_XM = 0x04: Mag data ready signal on INT2_XM pin.
JamB 0:801ebe391b00 413 // - CTRL_REG5_XM = 0x14: 100 Hz update rate. Low resolution. Interrupt
JamB 0:801ebe391b00 414 // requests don't latch. Temperature sensor disabled.
JamB 0:801ebe391b00 415 // - CTRL_REG6_XM = 0x00: 2 Gs scale.
JamB 0:801ebe391b00 416 // - CTRL_REG7_XM = 0x00: Continuous conversion mode. Normal HPF mode.
JamB 0:801ebe391b00 417 // - INT_CTRL_REG_M = 0x09: Interrupt active-high. Enable interrupts.
JamB 0:801ebe391b00 418 void initMag();
JamB 0:801ebe391b00 419
JamB 0:801ebe391b00 420 // gReadByte() -- Reads a byte from a specified gyroscope register.
JamB 0:801ebe391b00 421 // Input:
JamB 0:801ebe391b00 422 // - subAddress = Register to be read from.
JamB 0:801ebe391b00 423 // Output:
JamB 0:801ebe391b00 424 // - An 8-bit value read from the requested address.
JamB 0:801ebe391b00 425 uint8_t mReadByte(uint8_t subAddress);
JamB 0:801ebe391b00 426
JamB 0:801ebe391b00 427 // gReadBytes() -- Reads a number of bytes -- beginning at an address
JamB 0:801ebe391b00 428 // and incrementing from there -- from the gyroscope.
JamB 0:801ebe391b00 429 // Input:
JamB 0:801ebe391b00 430 // - subAddress = Register to be read from.
JamB 0:801ebe391b00 431 // - * dest = A pointer to an array of uint8_t's. Values read will be
JamB 0:801ebe391b00 432 // stored in here on return.
JamB 0:801ebe391b00 433 // - count = The number of bytes to be read.
JamB 0:801ebe391b00 434 // Output: No value is returned, but the `dest` array will store
JamB 0:801ebe391b00 435 // the data read upon exit.
JamB 0:801ebe391b00 436 void mReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count);
JamB 0:801ebe391b00 437
JamB 0:801ebe391b00 438 // gWriteByte() -- Write a byte to a register in the gyroscope.
JamB 0:801ebe391b00 439 // Input:
JamB 0:801ebe391b00 440 // - subAddress = Register to be written to.
JamB 0:801ebe391b00 441 // - data = data to be written to the register.
JamB 0:801ebe391b00 442 void mWriteByte(uint8_t subAddress, uint8_t data);
JamB 0:801ebe391b00 443
JamB 0:801ebe391b00 444 // xmReadByte() -- Read a byte from a register in the accel/mag sensor
JamB 0:801ebe391b00 445 // Input:
JamB 0:801ebe391b00 446 // - subAddress = Register to be read from.
JamB 0:801ebe391b00 447 // Output:
JamB 0:801ebe391b00 448 // - An 8-bit value read from the requested register.
JamB 0:801ebe391b00 449 uint8_t xgReadByte(uint8_t subAddress);
JamB 0:801ebe391b00 450
JamB 0:801ebe391b00 451 // xmReadBytes() -- Reads a number of bytes -- beginning at an address
JamB 0:801ebe391b00 452 // and incrementing from there -- from the accelerometer/magnetometer.
JamB 0:801ebe391b00 453 // Input:
JamB 0:801ebe391b00 454 // - subAddress = Register to be read from.
JamB 0:801ebe391b00 455 // - * dest = A pointer to an array of uint8_t's. Values read will be
JamB 0:801ebe391b00 456 // stored in here on return.
JamB 0:801ebe391b00 457 // - count = The number of bytes to be read.
JamB 0:801ebe391b00 458 // Output: No value is returned, but the `dest` array will store
JamB 0:801ebe391b00 459 // the data read upon exit.
JamB 0:801ebe391b00 460 void xgReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count);
JamB 0:801ebe391b00 461
JamB 0:801ebe391b00 462 // xmWriteByte() -- Write a byte to a register in the accel/mag sensor.
JamB 0:801ebe391b00 463 // Input:
JamB 0:801ebe391b00 464 // - subAddress = Register to be written to.
JamB 0:801ebe391b00 465 // - data = data to be written to the register.
JamB 0:801ebe391b00 466 void xgWriteByte(uint8_t subAddress, uint8_t data);
JamB 0:801ebe391b00 467
JamB 0:801ebe391b00 468 // calcgRes() -- Calculate the resolution of the gyroscope.
JamB 0:801ebe391b00 469 // This function will set the value of the gRes variable. gScale must
JamB 0:801ebe391b00 470 // be set prior to calling this function.
JamB 0:801ebe391b00 471 void calcgRes();
JamB 0:801ebe391b00 472
JamB 0:801ebe391b00 473 // calcmRes() -- Calculate the resolution of the magnetometer.
JamB 0:801ebe391b00 474 // This function will set the value of the mRes variable. mScale must
JamB 0:801ebe391b00 475 // be set prior to calling this function.
JamB 0:801ebe391b00 476 void calcmRes();
JamB 0:801ebe391b00 477
JamB 0:801ebe391b00 478 // calcaRes() -- Calculate the resolution of the accelerometer.
JamB 0:801ebe391b00 479 // This function will set the value of the aRes variable. aScale must
JamB 0:801ebe391b00 480 // be set prior to calling this function.
JamB 0:801ebe391b00 481 void calcaRes();
JamB 0:801ebe391b00 482
JamB 0:801ebe391b00 483 //////////////////////
JamB 0:801ebe391b00 484 // Helper Functions //
JamB 0:801ebe391b00 485 //////////////////////
JamB 0:801ebe391b00 486 void constrainScales();
JamB 0:801ebe391b00 487
JamB 0:801ebe391b00 488 ///////////////////
JamB 0:801ebe391b00 489 // SPI Functions //
JamB 0:801ebe391b00 490 ///////////////////
JamB 0:801ebe391b00 491 // initSPI() -- Initialize the SPI hardware.
JamB 0:801ebe391b00 492 // This function will setup all SPI pins and related hardware.
JamB 0:801ebe391b00 493 void initSPI();
JamB 0:801ebe391b00 494
JamB 0:801ebe391b00 495 // SPIwriteByte() -- Write a byte out of SPI to a register in the device
JamB 0:801ebe391b00 496 // Input:
JamB 0:801ebe391b00 497 // - csPin = The chip select pin of the slave device.
JamB 0:801ebe391b00 498 // - subAddress = The register to be written to.
JamB 0:801ebe391b00 499 // - data = Byte to be written to the register.
JamB 0:801ebe391b00 500 void SPIwriteByte(uint8_t csPin, uint8_t subAddress, uint8_t data);
JamB 0:801ebe391b00 501
JamB 0:801ebe391b00 502 // SPIreadByte() -- Read a single byte from a register over SPI.
JamB 0:801ebe391b00 503 // Input:
JamB 0:801ebe391b00 504 // - csPin = The chip select pin of the slave device.
JamB 0:801ebe391b00 505 // - subAddress = The register to be read from.
JamB 0:801ebe391b00 506 // Output:
JamB 0:801ebe391b00 507 // - The byte read from the requested address.
JamB 0:801ebe391b00 508 uint8_t SPIreadByte(uint8_t csPin, uint8_t subAddress);
JamB 0:801ebe391b00 509
JamB 0:801ebe391b00 510 // SPIreadBytes() -- Read a series of bytes, starting at a register via SPI
JamB 0:801ebe391b00 511 // Input:
JamB 0:801ebe391b00 512 // - csPin = The chip select pin of a slave device.
JamB 0:801ebe391b00 513 // - subAddress = The register to begin reading.
JamB 0:801ebe391b00 514 // - * dest = Pointer to an array where we'll store the readings.
JamB 0:801ebe391b00 515 // - count = Number of registers to be read.
JamB 0:801ebe391b00 516 // Output: No value is returned by the function, but the registers read are
JamB 0:801ebe391b00 517 // all stored in the *dest array given.
JamB 0:801ebe391b00 518 void SPIreadBytes(uint8_t csPin, uint8_t subAddress,
JamB 0:801ebe391b00 519 uint8_t * dest, uint8_t count);
JamB 0:801ebe391b00 520
JamB 0:801ebe391b00 521 ///////////////////
JamB 0:801ebe391b00 522 // I2C Functions //
JamB 0:801ebe391b00 523 ///////////////////
JamB 0:801ebe391b00 524 // initI2C() -- Initialize the I2C hardware.
JamB 0:801ebe391b00 525 // This function will setup all I2C pins and related hardware.
JamB 0:801ebe391b00 526 void initI2C();
JamB 0:801ebe391b00 527
JamB 0:801ebe391b00 528 // I2CwriteByte() -- Write a byte out of I2C to a register in the device
JamB 0:801ebe391b00 529 // Input:
JamB 0:801ebe391b00 530 // - address = The 7-bit I2C address of the slave device.
JamB 0:801ebe391b00 531 // - subAddress = The register to be written to.
JamB 0:801ebe391b00 532 // - data = Byte to be written to the register.
JamB 0:801ebe391b00 533 void I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data);
JamB 0:801ebe391b00 534
JamB 0:801ebe391b00 535 // I2CreadByte() -- Read a single byte from a register over I2C.
JamB 0:801ebe391b00 536 // Input:
JamB 0:801ebe391b00 537 // - address = The 7-bit I2C address of the slave device.
JamB 0:801ebe391b00 538 // - subAddress = The register to be read from.
JamB 0:801ebe391b00 539 // Output:
JamB 0:801ebe391b00 540 // - The byte read from the requested address.
JamB 0:801ebe391b00 541 uint8_t I2CreadByte(uint8_t address, uint8_t subAddress);
JamB 0:801ebe391b00 542
JamB 0:801ebe391b00 543 // I2CreadBytes() -- Read a series of bytes, starting at a register via SPI
JamB 0:801ebe391b00 544 // Input:
JamB 0:801ebe391b00 545 // - address = The 7-bit I2C address of the slave device.
JamB 0:801ebe391b00 546 // - subAddress = The register to begin reading.
JamB 0:801ebe391b00 547 // - * dest = Pointer to an array where we'll store the readings.
JamB 0:801ebe391b00 548 // - count = Number of registers to be read.
JamB 0:801ebe391b00 549 // Output: No value is returned by the function, but the registers read are
JamB 0:801ebe391b00 550 // all stored in the *dest array given.
JamB 0:801ebe391b00 551 uint8_t I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count);
JamB 0:801ebe391b00 552
JamB 0:801ebe391b00 553 private:
JamB 0:801ebe391b00 554 I2C i2c;
JamB 0:801ebe391b00 555 };
JamB 0:801ebe391b00 556
JamB 0:801ebe391b00 557 #endif // SFE_LSM9DS1_H //