debug

Dependencies:   mbed mbed-rtos PinDetect X_NUCLEO_53L0A1

Committer:
astovall21
Date:
Wed Apr 28 17:44:45 2021 +0000
Revision:
0:ea1c50666fc2
debug;

Who changed what in which revision?

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