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