Justin Gensel
/
LSM9DS1_Library_cal2
Added Soft Iron Calibration
Fork of LSM9DS1_Library_cal by
LSM9DS1.cpp@2:36abf8e18ade, 2016-02-03 (annotated)
- Committer:
- 4180_1
- Date:
- Wed Feb 03 18:45:40 2016 +0000
- Revision:
- 2:36abf8e18ade
- Parent:
- 1:87d535bf8c53
- Child:
- 3:f5be1e31a464
ver 0.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmar7 | 0:e8167f37725c | 1 | /****************************************************************************** |
jmar7 | 0:e8167f37725c | 2 | SFE_LSM9DS1.cpp |
jmar7 | 0:e8167f37725c | 3 | SFE_LSM9DS1 Library Source 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 implements all functions of the LSM9DS1 class. Functions here range |
jmar7 | 0:e8167f37725c | 9 | from higher level stuff, like reading/writing LSM9DS1 registers to low-level, |
jmar7 | 0:e8167f37725c | 10 | hardware reads and writes. Both SPI and I2C handler functions can be found |
jmar7 | 0:e8167f37725c | 11 | towards the bottom of this file. |
jmar7 | 0:e8167f37725c | 12 | |
jmar7 | 0:e8167f37725c | 13 | Development environment specifics: |
jmar7 | 0:e8167f37725c | 14 | IDE: Arduino 1.6 |
jmar7 | 0:e8167f37725c | 15 | Hardware Platform: Arduino Uno |
jmar7 | 0:e8167f37725c | 16 | LSM9DS1 Breakout Version: 1.0 |
jmar7 | 0:e8167f37725c | 17 | |
jmar7 | 0:e8167f37725c | 18 | This code is beerware; if you see me (or any other SparkFun employee) at the |
jmar7 | 0:e8167f37725c | 19 | local, and you've found our code helpful, please buy us a round! |
jmar7 | 0:e8167f37725c | 20 | |
jmar7 | 0:e8167f37725c | 21 | Distributed as-is; no warranty is given. |
jmar7 | 0:e8167f37725c | 22 | ******************************************************************************/ |
jmar7 | 0:e8167f37725c | 23 | |
jmar7 | 0:e8167f37725c | 24 | #include "LSM9DS1.h" |
jmar7 | 0:e8167f37725c | 25 | #include "LSM9DS1_Registers.h" |
jmar7 | 0:e8167f37725c | 26 | #include "LSM9DS1_Types.h" |
jmar7 | 0:e8167f37725c | 27 | //#include <Wire.h> // Wire library is used for I2C |
jmar7 | 0:e8167f37725c | 28 | //#include <SPI.h> // SPI library is used for...SPI. |
jmar7 | 0:e8167f37725c | 29 | |
jmar7 | 0:e8167f37725c | 30 | //#if defined(ARDUINO) && ARDUINO >= 100 |
jmar7 | 0:e8167f37725c | 31 | // #include "Arduino.h" |
jmar7 | 0:e8167f37725c | 32 | //#else |
jmar7 | 0:e8167f37725c | 33 | // #include "WProgram.h" |
jmar7 | 0:e8167f37725c | 34 | //#endif |
jmar7 | 0:e8167f37725c | 35 | |
jmar7 | 0:e8167f37725c | 36 | #define LSM9DS1_COMMUNICATION_TIMEOUT 1000 |
jmar7 | 0:e8167f37725c | 37 | |
jmar7 | 0:e8167f37725c | 38 | float magSensitivity[4] = {0.00014, 0.00029, 0.00043, 0.00058}; |
jmar7 | 0:e8167f37725c | 39 | extern Serial pc; |
jmar7 | 0:e8167f37725c | 40 | |
jmar7 | 0:e8167f37725c | 41 | LSM9DS1::LSM9DS1(PinName sda, PinName scl, uint8_t xgAddr, uint8_t mAddr) |
jmar7 | 0:e8167f37725c | 42 | :i2c(sda, scl) |
jmar7 | 0:e8167f37725c | 43 | { |
jmar7 | 0:e8167f37725c | 44 | init(IMU_MODE_I2C, xgAddr, mAddr); // dont know about 0xD6 or 0x3B |
jmar7 | 0:e8167f37725c | 45 | } |
jmar7 | 1:87d535bf8c53 | 46 | /* |
jmar7 | 0:e8167f37725c | 47 | LSM9DS1::LSM9DS1() |
jmar7 | 0:e8167f37725c | 48 | { |
jmar7 | 0:e8167f37725c | 49 | init(IMU_MODE_I2C, LSM9DS1_AG_ADDR(1), LSM9DS1_M_ADDR(1)); |
jmar7 | 0:e8167f37725c | 50 | } |
jmar7 | 0:e8167f37725c | 51 | |
jmar7 | 0:e8167f37725c | 52 | LSM9DS1::LSM9DS1(interface_mode interface, uint8_t xgAddr, uint8_t mAddr) |
jmar7 | 0:e8167f37725c | 53 | { |
jmar7 | 0:e8167f37725c | 54 | init(interface, xgAddr, mAddr); |
jmar7 | 0:e8167f37725c | 55 | } |
jmar7 | 0:e8167f37725c | 56 | */ |
jmar7 | 0:e8167f37725c | 57 | |
jmar7 | 0:e8167f37725c | 58 | void LSM9DS1::init(interface_mode interface, uint8_t xgAddr, uint8_t mAddr) |
jmar7 | 0:e8167f37725c | 59 | { |
jmar7 | 0:e8167f37725c | 60 | settings.device.commInterface = interface; |
jmar7 | 0:e8167f37725c | 61 | settings.device.agAddress = xgAddr; |
jmar7 | 0:e8167f37725c | 62 | settings.device.mAddress = mAddr; |
jmar7 | 0:e8167f37725c | 63 | |
jmar7 | 0:e8167f37725c | 64 | settings.gyro.enabled = true; |
jmar7 | 0:e8167f37725c | 65 | settings.gyro.enableX = true; |
jmar7 | 0:e8167f37725c | 66 | settings.gyro.enableY = true; |
jmar7 | 0:e8167f37725c | 67 | settings.gyro.enableZ = true; |
jmar7 | 0:e8167f37725c | 68 | // gyro scale can be 245, 500, or 2000 |
jmar7 | 0:e8167f37725c | 69 | settings.gyro.scale = 245; |
jmar7 | 0:e8167f37725c | 70 | // gyro sample rate: value between 1-6 |
jmar7 | 0:e8167f37725c | 71 | // 1 = 14.9 4 = 238 |
jmar7 | 0:e8167f37725c | 72 | // 2 = 59.5 5 = 476 |
jmar7 | 0:e8167f37725c | 73 | // 3 = 119 6 = 952 |
jmar7 | 0:e8167f37725c | 74 | settings.gyro.sampleRate = 6; |
jmar7 | 0:e8167f37725c | 75 | // gyro cutoff frequency: value between 0-3 |
jmar7 | 0:e8167f37725c | 76 | // Actual value of cutoff frequency depends |
jmar7 | 0:e8167f37725c | 77 | // on sample rate. |
jmar7 | 0:e8167f37725c | 78 | settings.gyro.bandwidth = 0; |
jmar7 | 0:e8167f37725c | 79 | settings.gyro.lowPowerEnable = false; |
jmar7 | 0:e8167f37725c | 80 | settings.gyro.HPFEnable = false; |
jmar7 | 0:e8167f37725c | 81 | // Gyro HPF cutoff frequency: value between 0-9 |
jmar7 | 0:e8167f37725c | 82 | // Actual value depends on sample rate. Only applies |
jmar7 | 0:e8167f37725c | 83 | // if gyroHPFEnable is true. |
jmar7 | 0:e8167f37725c | 84 | settings.gyro.HPFCutoff = 0; |
jmar7 | 0:e8167f37725c | 85 | settings.gyro.flipX = false; |
jmar7 | 0:e8167f37725c | 86 | settings.gyro.flipY = false; |
jmar7 | 0:e8167f37725c | 87 | settings.gyro.flipZ = false; |
jmar7 | 0:e8167f37725c | 88 | settings.gyro.orientation = 0; |
jmar7 | 0:e8167f37725c | 89 | settings.gyro.latchInterrupt = true; |
jmar7 | 0:e8167f37725c | 90 | |
jmar7 | 0:e8167f37725c | 91 | settings.accel.enabled = true; |
jmar7 | 0:e8167f37725c | 92 | settings.accel.enableX = true; |
jmar7 | 0:e8167f37725c | 93 | settings.accel.enableY = true; |
jmar7 | 0:e8167f37725c | 94 | settings.accel.enableZ = true; |
jmar7 | 0:e8167f37725c | 95 | // accel scale can be 2, 4, 8, or 16 |
jmar7 | 0:e8167f37725c | 96 | settings.accel.scale = 2; |
jmar7 | 0:e8167f37725c | 97 | // accel sample rate can be 1-6 |
jmar7 | 0:e8167f37725c | 98 | // 1 = 10 Hz 4 = 238 Hz |
jmar7 | 0:e8167f37725c | 99 | // 2 = 50 Hz 5 = 476 Hz |
jmar7 | 0:e8167f37725c | 100 | // 3 = 119 Hz 6 = 952 Hz |
jmar7 | 0:e8167f37725c | 101 | settings.accel.sampleRate = 6; |
4180_1 | 2:36abf8e18ade | 102 | // Accel cutoff freqeuncy can be any value between -1 - 3. |
jmar7 | 0:e8167f37725c | 103 | // -1 = bandwidth determined by sample rate |
jmar7 | 0:e8167f37725c | 104 | // 0 = 408 Hz 2 = 105 Hz |
jmar7 | 0:e8167f37725c | 105 | // 1 = 211 Hz 3 = 50 Hz |
jmar7 | 0:e8167f37725c | 106 | settings.accel.bandwidth = -1; |
jmar7 | 0:e8167f37725c | 107 | settings.accel.highResEnable = false; |
jmar7 | 0:e8167f37725c | 108 | // accelHighResBandwidth can be any value between 0-3 |
jmar7 | 0:e8167f37725c | 109 | // LP cutoff is set to a factor of sample rate |
jmar7 | 0:e8167f37725c | 110 | // 0 = ODR/50 2 = ODR/9 |
jmar7 | 0:e8167f37725c | 111 | // 1 = ODR/100 3 = ODR/400 |
jmar7 | 0:e8167f37725c | 112 | settings.accel.highResBandwidth = 0; |
jmar7 | 0:e8167f37725c | 113 | |
jmar7 | 0:e8167f37725c | 114 | settings.mag.enabled = true; |
jmar7 | 0:e8167f37725c | 115 | // mag scale can be 4, 8, 12, or 16 |
jmar7 | 0:e8167f37725c | 116 | settings.mag.scale = 4; |
jmar7 | 0:e8167f37725c | 117 | // mag data rate can be 0-7 |
jmar7 | 0:e8167f37725c | 118 | // 0 = 0.625 Hz 4 = 10 Hz |
jmar7 | 0:e8167f37725c | 119 | // 1 = 1.25 Hz 5 = 20 Hz |
jmar7 | 0:e8167f37725c | 120 | // 2 = 2.5 Hz 6 = 40 Hz |
jmar7 | 0:e8167f37725c | 121 | // 3 = 5 Hz 7 = 80 Hz |
jmar7 | 0:e8167f37725c | 122 | settings.mag.sampleRate = 7; |
jmar7 | 0:e8167f37725c | 123 | settings.mag.tempCompensationEnable = false; |
jmar7 | 0:e8167f37725c | 124 | // magPerformance can be any value between 0-3 |
jmar7 | 0:e8167f37725c | 125 | // 0 = Low power mode 2 = high performance |
jmar7 | 0:e8167f37725c | 126 | // 1 = medium performance 3 = ultra-high performance |
jmar7 | 0:e8167f37725c | 127 | settings.mag.XYPerformance = 3; |
jmar7 | 0:e8167f37725c | 128 | settings.mag.ZPerformance = 3; |
jmar7 | 0:e8167f37725c | 129 | settings.mag.lowPowerEnable = false; |
jmar7 | 0:e8167f37725c | 130 | // magOperatingMode can be 0-2 |
jmar7 | 0:e8167f37725c | 131 | // 0 = continuous conversion |
jmar7 | 0:e8167f37725c | 132 | // 1 = single-conversion |
jmar7 | 0:e8167f37725c | 133 | // 2 = power down |
jmar7 | 0:e8167f37725c | 134 | settings.mag.operatingMode = 0; |
jmar7 | 0:e8167f37725c | 135 | |
jmar7 | 0:e8167f37725c | 136 | settings.temp.enabled = true; |
4180_1 | 2:36abf8e18ade | 137 | for (int i=0; i<3; i++) { |
jmar7 | 0:e8167f37725c | 138 | gBias[i] = 0; |
jmar7 | 0:e8167f37725c | 139 | aBias[i] = 0; |
jmar7 | 0:e8167f37725c | 140 | mBias[i] = 0; |
jmar7 | 0:e8167f37725c | 141 | gBiasRaw[i] = 0; |
jmar7 | 0:e8167f37725c | 142 | aBiasRaw[i] = 0; |
jmar7 | 0:e8167f37725c | 143 | mBiasRaw[i] = 0; |
jmar7 | 0:e8167f37725c | 144 | } |
jmar7 | 0:e8167f37725c | 145 | _autoCalc = false; |
jmar7 | 0:e8167f37725c | 146 | } |
jmar7 | 0:e8167f37725c | 147 | |
jmar7 | 0:e8167f37725c | 148 | |
jmar7 | 0:e8167f37725c | 149 | uint16_t LSM9DS1::begin() |
jmar7 | 0:e8167f37725c | 150 | { |
jmar7 | 0:e8167f37725c | 151 | //! Todo: don't use _xgAddress or _mAddress, duplicating memory |
jmar7 | 0:e8167f37725c | 152 | _xgAddress = settings.device.agAddress; |
jmar7 | 0:e8167f37725c | 153 | _mAddress = settings.device.mAddress; |
4180_1 | 2:36abf8e18ade | 154 | |
jmar7 | 0:e8167f37725c | 155 | constrainScales(); |
jmar7 | 0:e8167f37725c | 156 | // Once we have the scale values, we can calculate the resolution |
jmar7 | 0:e8167f37725c | 157 | // of each sensor. That's what these functions are for. One for each sensor |
jmar7 | 0:e8167f37725c | 158 | calcgRes(); // Calculate DPS / ADC tick, stored in gRes variable |
jmar7 | 0:e8167f37725c | 159 | calcmRes(); // Calculate Gs / ADC tick, stored in mRes variable |
jmar7 | 0:e8167f37725c | 160 | calcaRes(); // Calculate g / ADC tick, stored in aRes variable |
4180_1 | 2:36abf8e18ade | 161 | |
jmar7 | 0:e8167f37725c | 162 | // Now, initialize our hardware interface. |
jmar7 | 0:e8167f37725c | 163 | if (settings.device.commInterface == IMU_MODE_I2C) // If we're using I2C |
jmar7 | 0:e8167f37725c | 164 | initI2C(); // Initialize I2C |
jmar7 | 0:e8167f37725c | 165 | else if (settings.device.commInterface == IMU_MODE_SPI) // else, if we're using SPI |
jmar7 | 0:e8167f37725c | 166 | initSPI(); // Initialize SPI |
4180_1 | 2:36abf8e18ade | 167 | |
jmar7 | 0:e8167f37725c | 168 | // To verify communication, we can read from the WHO_AM_I register of |
jmar7 | 0:e8167f37725c | 169 | // each device. Store those in a variable so we can return them. |
jmar7 | 0:e8167f37725c | 170 | uint8_t mTest = mReadByte(WHO_AM_I_M); // Read the gyro WHO_AM_I |
jmar7 | 0:e8167f37725c | 171 | uint8_t xgTest = xgReadByte(WHO_AM_I_XG); // Read the accel/mag WHO_AM_I |
jmar7 | 0:e8167f37725c | 172 | pc.printf("%x, %x, %x, %x\n\r", mTest, xgTest, _xgAddress, _mAddress); |
jmar7 | 0:e8167f37725c | 173 | uint16_t whoAmICombined = (xgTest << 8) | mTest; |
4180_1 | 2:36abf8e18ade | 174 | |
jmar7 | 0:e8167f37725c | 175 | if (whoAmICombined != ((WHO_AM_I_AG_RSP << 8) | WHO_AM_I_M_RSP)) |
jmar7 | 0:e8167f37725c | 176 | return 0; |
4180_1 | 2:36abf8e18ade | 177 | |
jmar7 | 0:e8167f37725c | 178 | // Gyro initialization stuff: |
jmar7 | 0:e8167f37725c | 179 | initGyro(); // This will "turn on" the gyro. Setting up interrupts, etc. |
4180_1 | 2:36abf8e18ade | 180 | |
jmar7 | 0:e8167f37725c | 181 | // Accelerometer initialization stuff: |
jmar7 | 0:e8167f37725c | 182 | initAccel(); // "Turn on" all axes of the accel. Set up interrupts, etc. |
4180_1 | 2:36abf8e18ade | 183 | |
jmar7 | 0:e8167f37725c | 184 | // Magnetometer initialization stuff: |
jmar7 | 0:e8167f37725c | 185 | initMag(); // "Turn on" all axes of the mag. Set up interrupts, etc. |
jmar7 | 0:e8167f37725c | 186 | |
jmar7 | 0:e8167f37725c | 187 | // Once everything is initialized, return the WHO_AM_I registers we read: |
jmar7 | 0:e8167f37725c | 188 | return whoAmICombined; |
jmar7 | 0:e8167f37725c | 189 | } |
jmar7 | 0:e8167f37725c | 190 | |
jmar7 | 0:e8167f37725c | 191 | void LSM9DS1::initGyro() |
jmar7 | 0:e8167f37725c | 192 | { |
jmar7 | 0:e8167f37725c | 193 | uint8_t tempRegValue = 0; |
4180_1 | 2:36abf8e18ade | 194 | |
jmar7 | 0:e8167f37725c | 195 | // CTRL_REG1_G (Default value: 0x00) |
jmar7 | 0:e8167f37725c | 196 | // [ODR_G2][ODR_G1][ODR_G0][FS_G1][FS_G0][0][BW_G1][BW_G0] |
jmar7 | 0:e8167f37725c | 197 | // ODR_G[2:0] - Output data rate selection |
jmar7 | 0:e8167f37725c | 198 | // FS_G[1:0] - Gyroscope full-scale selection |
jmar7 | 0:e8167f37725c | 199 | // BW_G[1:0] - Gyroscope bandwidth selection |
4180_1 | 2:36abf8e18ade | 200 | |
jmar7 | 0:e8167f37725c | 201 | // To disable gyro, set sample rate bits to 0. We'll only set sample |
jmar7 | 0:e8167f37725c | 202 | // rate if the gyro is enabled. |
4180_1 | 2:36abf8e18ade | 203 | if (settings.gyro.enabled) { |
jmar7 | 0:e8167f37725c | 204 | tempRegValue = (settings.gyro.sampleRate & 0x07) << 5; |
jmar7 | 0:e8167f37725c | 205 | } |
4180_1 | 2:36abf8e18ade | 206 | switch (settings.gyro.scale) { |
jmar7 | 0:e8167f37725c | 207 | case 500: |
jmar7 | 0:e8167f37725c | 208 | tempRegValue |= (0x1 << 3); |
jmar7 | 0:e8167f37725c | 209 | break; |
jmar7 | 0:e8167f37725c | 210 | case 2000: |
jmar7 | 0:e8167f37725c | 211 | tempRegValue |= (0x3 << 3); |
jmar7 | 0:e8167f37725c | 212 | break; |
4180_1 | 2:36abf8e18ade | 213 | // Otherwise we'll set it to 245 dps (0x0 << 4) |
jmar7 | 0:e8167f37725c | 214 | } |
jmar7 | 0:e8167f37725c | 215 | tempRegValue |= (settings.gyro.bandwidth & 0x3); |
jmar7 | 0:e8167f37725c | 216 | xgWriteByte(CTRL_REG1_G, tempRegValue); |
4180_1 | 2:36abf8e18ade | 217 | |
jmar7 | 0:e8167f37725c | 218 | // CTRL_REG2_G (Default value: 0x00) |
jmar7 | 0:e8167f37725c | 219 | // [0][0][0][0][INT_SEL1][INT_SEL0][OUT_SEL1][OUT_SEL0] |
jmar7 | 0:e8167f37725c | 220 | // INT_SEL[1:0] - INT selection configuration |
jmar7 | 0:e8167f37725c | 221 | // OUT_SEL[1:0] - Out selection configuration |
4180_1 | 2:36abf8e18ade | 222 | xgWriteByte(CTRL_REG2_G, 0x00); |
4180_1 | 2:36abf8e18ade | 223 | |
jmar7 | 0:e8167f37725c | 224 | // CTRL_REG3_G (Default value: 0x00) |
jmar7 | 0:e8167f37725c | 225 | // [LP_mode][HP_EN][0][0][HPCF3_G][HPCF2_G][HPCF1_G][HPCF0_G] |
jmar7 | 0:e8167f37725c | 226 | // LP_mode - Low-power mode enable (0: disabled, 1: enabled) |
jmar7 | 0:e8167f37725c | 227 | // HP_EN - HPF enable (0:disabled, 1: enabled) |
jmar7 | 0:e8167f37725c | 228 | // HPCF_G[3:0] - HPF cutoff frequency |
jmar7 | 0:e8167f37725c | 229 | tempRegValue = settings.gyro.lowPowerEnable ? (1<<7) : 0; |
4180_1 | 2:36abf8e18ade | 230 | if (settings.gyro.HPFEnable) { |
jmar7 | 0:e8167f37725c | 231 | tempRegValue |= (1<<6) | (settings.gyro.HPFCutoff & 0x0F); |
jmar7 | 0:e8167f37725c | 232 | } |
jmar7 | 0:e8167f37725c | 233 | xgWriteByte(CTRL_REG3_G, tempRegValue); |
4180_1 | 2:36abf8e18ade | 234 | |
jmar7 | 0:e8167f37725c | 235 | // CTRL_REG4 (Default value: 0x38) |
jmar7 | 0:e8167f37725c | 236 | // [0][0][Zen_G][Yen_G][Xen_G][0][LIR_XL1][4D_XL1] |
jmar7 | 0:e8167f37725c | 237 | // Zen_G - Z-axis output enable (0:disable, 1:enable) |
jmar7 | 0:e8167f37725c | 238 | // Yen_G - Y-axis output enable (0:disable, 1:enable) |
jmar7 | 0:e8167f37725c | 239 | // Xen_G - X-axis output enable (0:disable, 1:enable) |
jmar7 | 0:e8167f37725c | 240 | // LIR_XL1 - Latched interrupt (0:not latched, 1:latched) |
jmar7 | 0:e8167f37725c | 241 | // 4D_XL1 - 4D option on interrupt (0:6D used, 1:4D used) |
jmar7 | 0:e8167f37725c | 242 | tempRegValue = 0; |
jmar7 | 0:e8167f37725c | 243 | if (settings.gyro.enableZ) tempRegValue |= (1<<5); |
jmar7 | 0:e8167f37725c | 244 | if (settings.gyro.enableY) tempRegValue |= (1<<4); |
jmar7 | 0:e8167f37725c | 245 | if (settings.gyro.enableX) tempRegValue |= (1<<3); |
jmar7 | 0:e8167f37725c | 246 | if (settings.gyro.latchInterrupt) tempRegValue |= (1<<1); |
jmar7 | 0:e8167f37725c | 247 | xgWriteByte(CTRL_REG4, tempRegValue); |
4180_1 | 2:36abf8e18ade | 248 | |
jmar7 | 0:e8167f37725c | 249 | // ORIENT_CFG_G (Default value: 0x00) |
jmar7 | 0:e8167f37725c | 250 | // [0][0][SignX_G][SignY_G][SignZ_G][Orient_2][Orient_1][Orient_0] |
jmar7 | 0:e8167f37725c | 251 | // SignX_G - Pitch axis (X) angular rate sign (0: positive, 1: negative) |
jmar7 | 0:e8167f37725c | 252 | // Orient [2:0] - Directional user orientation selection |
jmar7 | 0:e8167f37725c | 253 | tempRegValue = 0; |
jmar7 | 0:e8167f37725c | 254 | if (settings.gyro.flipX) tempRegValue |= (1<<5); |
jmar7 | 0:e8167f37725c | 255 | if (settings.gyro.flipY) tempRegValue |= (1<<4); |
jmar7 | 0:e8167f37725c | 256 | if (settings.gyro.flipZ) tempRegValue |= (1<<3); |
jmar7 | 0:e8167f37725c | 257 | xgWriteByte(ORIENT_CFG_G, tempRegValue); |
jmar7 | 0:e8167f37725c | 258 | } |
jmar7 | 0:e8167f37725c | 259 | |
jmar7 | 0:e8167f37725c | 260 | void LSM9DS1::initAccel() |
jmar7 | 0:e8167f37725c | 261 | { |
jmar7 | 0:e8167f37725c | 262 | uint8_t tempRegValue = 0; |
4180_1 | 2:36abf8e18ade | 263 | |
jmar7 | 0:e8167f37725c | 264 | // CTRL_REG5_XL (0x1F) (Default value: 0x38) |
jmar7 | 0:e8167f37725c | 265 | // [DEC_1][DEC_0][Zen_XL][Yen_XL][Zen_XL][0][0][0] |
jmar7 | 0:e8167f37725c | 266 | // DEC[0:1] - Decimation of accel data on OUT REG and FIFO. |
jmar7 | 0:e8167f37725c | 267 | // 00: None, 01: 2 samples, 10: 4 samples 11: 8 samples |
jmar7 | 0:e8167f37725c | 268 | // Zen_XL - Z-axis output enabled |
jmar7 | 0:e8167f37725c | 269 | // Yen_XL - Y-axis output enabled |
jmar7 | 0:e8167f37725c | 270 | // Xen_XL - X-axis output enabled |
jmar7 | 0:e8167f37725c | 271 | if (settings.accel.enableZ) tempRegValue |= (1<<5); |
jmar7 | 0:e8167f37725c | 272 | if (settings.accel.enableY) tempRegValue |= (1<<4); |
jmar7 | 0:e8167f37725c | 273 | if (settings.accel.enableX) tempRegValue |= (1<<3); |
4180_1 | 2:36abf8e18ade | 274 | |
jmar7 | 0:e8167f37725c | 275 | xgWriteByte(CTRL_REG5_XL, tempRegValue); |
4180_1 | 2:36abf8e18ade | 276 | |
jmar7 | 0:e8167f37725c | 277 | // CTRL_REG6_XL (0x20) (Default value: 0x00) |
jmar7 | 0:e8167f37725c | 278 | // [ODR_XL2][ODR_XL1][ODR_XL0][FS1_XL][FS0_XL][BW_SCAL_ODR][BW_XL1][BW_XL0] |
jmar7 | 0:e8167f37725c | 279 | // ODR_XL[2:0] - Output data rate & power mode selection |
jmar7 | 0:e8167f37725c | 280 | // FS_XL[1:0] - Full-scale selection |
jmar7 | 0:e8167f37725c | 281 | // BW_SCAL_ODR - Bandwidth selection |
jmar7 | 0:e8167f37725c | 282 | // BW_XL[1:0] - Anti-aliasing filter bandwidth selection |
jmar7 | 0:e8167f37725c | 283 | tempRegValue = 0; |
jmar7 | 0:e8167f37725c | 284 | // To disable the accel, set the sampleRate bits to 0. |
4180_1 | 2:36abf8e18ade | 285 | if (settings.accel.enabled) { |
jmar7 | 0:e8167f37725c | 286 | tempRegValue |= (settings.accel.sampleRate & 0x07) << 5; |
jmar7 | 0:e8167f37725c | 287 | } |
4180_1 | 2:36abf8e18ade | 288 | switch (settings.accel.scale) { |
jmar7 | 0:e8167f37725c | 289 | case 4: |
jmar7 | 0:e8167f37725c | 290 | tempRegValue |= (0x2 << 3); |
jmar7 | 0:e8167f37725c | 291 | break; |
jmar7 | 0:e8167f37725c | 292 | case 8: |
jmar7 | 0:e8167f37725c | 293 | tempRegValue |= (0x3 << 3); |
jmar7 | 0:e8167f37725c | 294 | break; |
jmar7 | 0:e8167f37725c | 295 | case 16: |
jmar7 | 0:e8167f37725c | 296 | tempRegValue |= (0x1 << 3); |
jmar7 | 0:e8167f37725c | 297 | break; |
4180_1 | 2:36abf8e18ade | 298 | // Otherwise it'll be set to 2g (0x0 << 3) |
jmar7 | 0:e8167f37725c | 299 | } |
4180_1 | 2:36abf8e18ade | 300 | if (settings.accel.bandwidth >= 0) { |
jmar7 | 0:e8167f37725c | 301 | tempRegValue |= (1<<2); // Set BW_SCAL_ODR |
jmar7 | 0:e8167f37725c | 302 | tempRegValue |= (settings.accel.bandwidth & 0x03); |
jmar7 | 0:e8167f37725c | 303 | } |
jmar7 | 0:e8167f37725c | 304 | xgWriteByte(CTRL_REG6_XL, tempRegValue); |
4180_1 | 2:36abf8e18ade | 305 | |
jmar7 | 0:e8167f37725c | 306 | // CTRL_REG7_XL (0x21) (Default value: 0x00) |
jmar7 | 0:e8167f37725c | 307 | // [HR][DCF1][DCF0][0][0][FDS][0][HPIS1] |
jmar7 | 0:e8167f37725c | 308 | // HR - High resolution mode (0: disable, 1: enable) |
jmar7 | 0:e8167f37725c | 309 | // DCF[1:0] - Digital filter cutoff frequency |
jmar7 | 0:e8167f37725c | 310 | // FDS - Filtered data selection |
jmar7 | 0:e8167f37725c | 311 | // HPIS1 - HPF enabled for interrupt function |
jmar7 | 0:e8167f37725c | 312 | tempRegValue = 0; |
4180_1 | 2:36abf8e18ade | 313 | if (settings.accel.highResEnable) { |
jmar7 | 0:e8167f37725c | 314 | tempRegValue |= (1<<7); // Set HR bit |
jmar7 | 0:e8167f37725c | 315 | tempRegValue |= (settings.accel.highResBandwidth & 0x3) << 5; |
jmar7 | 0:e8167f37725c | 316 | } |
jmar7 | 0:e8167f37725c | 317 | xgWriteByte(CTRL_REG7_XL, tempRegValue); |
jmar7 | 0:e8167f37725c | 318 | } |
jmar7 | 0:e8167f37725c | 319 | |
jmar7 | 0:e8167f37725c | 320 | // This is a function that uses the FIFO to accumulate sample of accelerometer and gyro data, average |
jmar7 | 0:e8167f37725c | 321 | // them, scales them to gs and deg/s, respectively, and then passes the biases to the main sketch |
jmar7 | 0:e8167f37725c | 322 | // for subtraction from all subsequent data. There are no gyro and accelerometer bias registers to store |
jmar7 | 0:e8167f37725c | 323 | // the data as there are in the ADXL345, a precursor to the LSM9DS0, or the MPU-9150, so we have to |
jmar7 | 0:e8167f37725c | 324 | // subtract the biases ourselves. This results in a more accurate measurement in general and can |
jmar7 | 0:e8167f37725c | 325 | // remove errors due to imprecise or varying initial placement. Calibration of sensor data in this manner |
jmar7 | 0:e8167f37725c | 326 | // is good practice. |
jmar7 | 0:e8167f37725c | 327 | void LSM9DS1::calibrate(bool autoCalc) |
4180_1 | 2:36abf8e18ade | 328 | { |
jmar7 | 0:e8167f37725c | 329 | uint8_t data[6] = {0, 0, 0, 0, 0, 0}; |
jmar7 | 0:e8167f37725c | 330 | uint8_t samples = 0; |
jmar7 | 0:e8167f37725c | 331 | int ii; |
jmar7 | 0:e8167f37725c | 332 | int32_t aBiasRawTemp[3] = {0, 0, 0}; |
jmar7 | 0:e8167f37725c | 333 | int32_t gBiasRawTemp[3] = {0, 0, 0}; |
4180_1 | 2:36abf8e18ade | 334 | pc.printf("\n\rPlace IMU on level surface and do not move it for gyro and accel calibration.\n\r"); |
4180_1 | 2:36abf8e18ade | 335 | wait(1); |
jmar7 | 0:e8167f37725c | 336 | // Turn on FIFO and set threshold to 32 samples |
jmar7 | 0:e8167f37725c | 337 | enableFIFO(true); |
jmar7 | 0:e8167f37725c | 338 | setFIFO(FIFO_THS, 0x1F); |
4180_1 | 2:36abf8e18ade | 339 | while (samples < 0x1F) { |
jmar7 | 0:e8167f37725c | 340 | samples = (xgReadByte(FIFO_SRC) & 0x3F); // Read number of stored samples |
jmar7 | 0:e8167f37725c | 341 | } |
4180_1 | 2:36abf8e18ade | 342 | for(ii = 0; ii < samples ; ii++) { |
4180_1 | 2:36abf8e18ade | 343 | // Read the gyro data stored in the FIFO |
jmar7 | 0:e8167f37725c | 344 | readGyro(); |
jmar7 | 0:e8167f37725c | 345 | gBiasRawTemp[0] += gx; |
jmar7 | 0:e8167f37725c | 346 | gBiasRawTemp[1] += gy; |
jmar7 | 0:e8167f37725c | 347 | gBiasRawTemp[2] += gz; |
jmar7 | 0:e8167f37725c | 348 | readAccel(); |
jmar7 | 0:e8167f37725c | 349 | aBiasRawTemp[0] += ax; |
jmar7 | 0:e8167f37725c | 350 | aBiasRawTemp[1] += ay; |
jmar7 | 0:e8167f37725c | 351 | aBiasRawTemp[2] += az - (int16_t)(1./aRes); // Assumes sensor facing up! |
4180_1 | 2:36abf8e18ade | 352 | } |
4180_1 | 2:36abf8e18ade | 353 | for (ii = 0; ii < 3; ii++) { |
jmar7 | 0:e8167f37725c | 354 | gBiasRaw[ii] = gBiasRawTemp[ii] / samples; |
jmar7 | 0:e8167f37725c | 355 | gBias[ii] = calcGyro(gBiasRaw[ii]); |
jmar7 | 0:e8167f37725c | 356 | aBiasRaw[ii] = aBiasRawTemp[ii] / samples; |
jmar7 | 0:e8167f37725c | 357 | aBias[ii] = calcAccel(aBiasRaw[ii]); |
jmar7 | 0:e8167f37725c | 358 | } |
4180_1 | 2:36abf8e18ade | 359 | |
jmar7 | 0:e8167f37725c | 360 | enableFIFO(false); |
jmar7 | 0:e8167f37725c | 361 | setFIFO(FIFO_OFF, 0x00); |
4180_1 | 2:36abf8e18ade | 362 | |
jmar7 | 0:e8167f37725c | 363 | if (autoCalc) _autoCalc = true; |
jmar7 | 0:e8167f37725c | 364 | } |
jmar7 | 0:e8167f37725c | 365 | |
jmar7 | 0:e8167f37725c | 366 | void LSM9DS1::calibrateMag(bool loadIn) |
jmar7 | 0:e8167f37725c | 367 | { |
jmar7 | 0:e8167f37725c | 368 | int i, j; |
jmar7 | 0:e8167f37725c | 369 | int16_t magMin[3] = {0, 0, 0}; |
jmar7 | 0:e8167f37725c | 370 | int16_t magMax[3] = {0, 0, 0}; // The road warrior |
4180_1 | 2:36abf8e18ade | 371 | pc.printf("\n\n\r Rotate IMU device at least 360 in horizontal plane for magnetometer calibration\n\r"); |
4180_1 | 2:36abf8e18ade | 372 | wait(0.5); |
4180_1 | 2:36abf8e18ade | 373 | for (i=0; i<1000; i++) { |
4180_1 | 2:36abf8e18ade | 374 | while (!magAvailable(ALL_AXIS)); |
jmar7 | 0:e8167f37725c | 375 | readMag(); |
jmar7 | 0:e8167f37725c | 376 | int16_t magTemp[3] = {0, 0, 0}; |
4180_1 | 2:36abf8e18ade | 377 | magTemp[0] = mx; |
jmar7 | 0:e8167f37725c | 378 | magTemp[1] = my; |
jmar7 | 0:e8167f37725c | 379 | magTemp[2] = mz; |
4180_1 | 2:36abf8e18ade | 380 | for (j = 0; j < 3; j++) { |
jmar7 | 0:e8167f37725c | 381 | if (magTemp[j] > magMax[j]) magMax[j] = magTemp[j]; |
jmar7 | 0:e8167f37725c | 382 | if (magTemp[j] < magMin[j]) magMin[j] = magTemp[j]; |
jmar7 | 0:e8167f37725c | 383 | } |
jmar7 | 0:e8167f37725c | 384 | } |
4180_1 | 2:36abf8e18ade | 385 | for (j = 0; j < 3; j++) { |
jmar7 | 0:e8167f37725c | 386 | mBiasRaw[j] = (magMax[j] + magMin[j]) / 2; |
jmar7 | 0:e8167f37725c | 387 | mBias[j] = calcMag(mBiasRaw[j]); |
4180_1 | 2:36abf8e18ade | 388 | pc.printf("%f ",mBias[j]); |
jmar7 | 0:e8167f37725c | 389 | if (loadIn) |
jmar7 | 0:e8167f37725c | 390 | magOffset(j, mBiasRaw[j]); |
jmar7 | 0:e8167f37725c | 391 | } |
4180_1 | 2:36abf8e18ade | 392 | pc.printf("\n\rMAG calibration done\n\r"); |
jmar7 | 0:e8167f37725c | 393 | } |
jmar7 | 0:e8167f37725c | 394 | void LSM9DS1::magOffset(uint8_t axis, int16_t offset) |
jmar7 | 0:e8167f37725c | 395 | { |
jmar7 | 0:e8167f37725c | 396 | if (axis > 2) |
jmar7 | 0:e8167f37725c | 397 | return; |
jmar7 | 0:e8167f37725c | 398 | uint8_t msb, lsb; |
jmar7 | 0:e8167f37725c | 399 | msb = (offset & 0xFF00) >> 8; |
jmar7 | 0:e8167f37725c | 400 | lsb = offset & 0x00FF; |
jmar7 | 0:e8167f37725c | 401 | mWriteByte(OFFSET_X_REG_L_M + (2 * axis), lsb); |
jmar7 | 0:e8167f37725c | 402 | mWriteByte(OFFSET_X_REG_H_M + (2 * axis), msb); |
jmar7 | 0:e8167f37725c | 403 | } |
jmar7 | 0:e8167f37725c | 404 | |
jmar7 | 0:e8167f37725c | 405 | void LSM9DS1::initMag() |
jmar7 | 0:e8167f37725c | 406 | { |
jmar7 | 0:e8167f37725c | 407 | uint8_t tempRegValue = 0; |
4180_1 | 2:36abf8e18ade | 408 | |
jmar7 | 0:e8167f37725c | 409 | // CTRL_REG1_M (Default value: 0x10) |
jmar7 | 0:e8167f37725c | 410 | // [TEMP_COMP][OM1][OM0][DO2][DO1][DO0][0][ST] |
jmar7 | 0:e8167f37725c | 411 | // TEMP_COMP - Temperature compensation |
jmar7 | 0:e8167f37725c | 412 | // OM[1:0] - X & Y axes op mode selection |
jmar7 | 0:e8167f37725c | 413 | // 00:low-power, 01:medium performance |
jmar7 | 0:e8167f37725c | 414 | // 10: high performance, 11:ultra-high performance |
jmar7 | 0:e8167f37725c | 415 | // DO[2:0] - Output data rate selection |
jmar7 | 0:e8167f37725c | 416 | // ST - Self-test enable |
jmar7 | 0:e8167f37725c | 417 | if (settings.mag.tempCompensationEnable) tempRegValue |= (1<<7); |
jmar7 | 0:e8167f37725c | 418 | tempRegValue |= (settings.mag.XYPerformance & 0x3) << 5; |
jmar7 | 0:e8167f37725c | 419 | tempRegValue |= (settings.mag.sampleRate & 0x7) << 2; |
jmar7 | 0:e8167f37725c | 420 | mWriteByte(CTRL_REG1_M, tempRegValue); |
4180_1 | 2:36abf8e18ade | 421 | |
jmar7 | 0:e8167f37725c | 422 | // CTRL_REG2_M (Default value 0x00) |
jmar7 | 0:e8167f37725c | 423 | // [0][FS1][FS0][0][REBOOT][SOFT_RST][0][0] |
jmar7 | 0:e8167f37725c | 424 | // FS[1:0] - Full-scale configuration |
jmar7 | 0:e8167f37725c | 425 | // REBOOT - Reboot memory content (0:normal, 1:reboot) |
jmar7 | 0:e8167f37725c | 426 | // SOFT_RST - Reset config and user registers (0:default, 1:reset) |
jmar7 | 0:e8167f37725c | 427 | tempRegValue = 0; |
4180_1 | 2:36abf8e18ade | 428 | switch (settings.mag.scale) { |
4180_1 | 2:36abf8e18ade | 429 | case 8: |
4180_1 | 2:36abf8e18ade | 430 | tempRegValue |= (0x1 << 5); |
4180_1 | 2:36abf8e18ade | 431 | break; |
4180_1 | 2:36abf8e18ade | 432 | case 12: |
4180_1 | 2:36abf8e18ade | 433 | tempRegValue |= (0x2 << 5); |
4180_1 | 2:36abf8e18ade | 434 | break; |
4180_1 | 2:36abf8e18ade | 435 | case 16: |
4180_1 | 2:36abf8e18ade | 436 | tempRegValue |= (0x3 << 5); |
4180_1 | 2:36abf8e18ade | 437 | break; |
4180_1 | 2:36abf8e18ade | 438 | // Otherwise we'll default to 4 gauss (00) |
jmar7 | 0:e8167f37725c | 439 | } |
jmar7 | 0:e8167f37725c | 440 | mWriteByte(CTRL_REG2_M, tempRegValue); // +/-4Gauss |
4180_1 | 2:36abf8e18ade | 441 | |
jmar7 | 0:e8167f37725c | 442 | // CTRL_REG3_M (Default value: 0x03) |
jmar7 | 0:e8167f37725c | 443 | // [I2C_DISABLE][0][LP][0][0][SIM][MD1][MD0] |
jmar7 | 0:e8167f37725c | 444 | // I2C_DISABLE - Disable I2C interace (0:enable, 1:disable) |
jmar7 | 0:e8167f37725c | 445 | // LP - Low-power mode cofiguration (1:enable) |
jmar7 | 0:e8167f37725c | 446 | // SIM - SPI mode selection (0:write-only, 1:read/write enable) |
jmar7 | 0:e8167f37725c | 447 | // MD[1:0] - Operating mode |
jmar7 | 0:e8167f37725c | 448 | // 00:continuous conversion, 01:single-conversion, |
jmar7 | 0:e8167f37725c | 449 | // 10,11: Power-down |
jmar7 | 0:e8167f37725c | 450 | tempRegValue = 0; |
jmar7 | 0:e8167f37725c | 451 | if (settings.mag.lowPowerEnable) tempRegValue |= (1<<5); |
jmar7 | 0:e8167f37725c | 452 | tempRegValue |= (settings.mag.operatingMode & 0x3); |
jmar7 | 0:e8167f37725c | 453 | mWriteByte(CTRL_REG3_M, tempRegValue); // Continuous conversion mode |
4180_1 | 2:36abf8e18ade | 454 | |
jmar7 | 0:e8167f37725c | 455 | // CTRL_REG4_M (Default value: 0x00) |
jmar7 | 0:e8167f37725c | 456 | // [0][0][0][0][OMZ1][OMZ0][BLE][0] |
jmar7 | 0:e8167f37725c | 457 | // OMZ[1:0] - Z-axis operative mode selection |
jmar7 | 0:e8167f37725c | 458 | // 00:low-power mode, 01:medium performance |
jmar7 | 0:e8167f37725c | 459 | // 10:high performance, 10:ultra-high performance |
jmar7 | 0:e8167f37725c | 460 | // BLE - Big/little endian data |
jmar7 | 0:e8167f37725c | 461 | tempRegValue = 0; |
jmar7 | 0:e8167f37725c | 462 | tempRegValue = (settings.mag.ZPerformance & 0x3) << 2; |
jmar7 | 0:e8167f37725c | 463 | mWriteByte(CTRL_REG4_M, tempRegValue); |
4180_1 | 2:36abf8e18ade | 464 | |
jmar7 | 0:e8167f37725c | 465 | // CTRL_REG5_M (Default value: 0x00) |
jmar7 | 0:e8167f37725c | 466 | // [0][BDU][0][0][0][0][0][0] |
jmar7 | 0:e8167f37725c | 467 | // BDU - Block data update for magnetic data |
jmar7 | 0:e8167f37725c | 468 | // 0:continuous, 1:not updated until MSB/LSB are read |
jmar7 | 0:e8167f37725c | 469 | tempRegValue = 0; |
jmar7 | 0:e8167f37725c | 470 | mWriteByte(CTRL_REG5_M, tempRegValue); |
jmar7 | 0:e8167f37725c | 471 | } |
jmar7 | 0:e8167f37725c | 472 | |
jmar7 | 0:e8167f37725c | 473 | uint8_t LSM9DS1::accelAvailable() |
jmar7 | 0:e8167f37725c | 474 | { |
jmar7 | 0:e8167f37725c | 475 | uint8_t status = xgReadByte(STATUS_REG_1); |
4180_1 | 2:36abf8e18ade | 476 | |
jmar7 | 0:e8167f37725c | 477 | return (status & (1<<0)); |
jmar7 | 0:e8167f37725c | 478 | } |
jmar7 | 0:e8167f37725c | 479 | |
jmar7 | 0:e8167f37725c | 480 | uint8_t LSM9DS1::gyroAvailable() |
jmar7 | 0:e8167f37725c | 481 | { |
jmar7 | 0:e8167f37725c | 482 | uint8_t status = xgReadByte(STATUS_REG_1); |
4180_1 | 2:36abf8e18ade | 483 | |
jmar7 | 0:e8167f37725c | 484 | return ((status & (1<<1)) >> 1); |
jmar7 | 0:e8167f37725c | 485 | } |
jmar7 | 0:e8167f37725c | 486 | |
jmar7 | 0:e8167f37725c | 487 | uint8_t LSM9DS1::tempAvailable() |
jmar7 | 0:e8167f37725c | 488 | { |
jmar7 | 0:e8167f37725c | 489 | uint8_t status = xgReadByte(STATUS_REG_1); |
4180_1 | 2:36abf8e18ade | 490 | |
jmar7 | 0:e8167f37725c | 491 | return ((status & (1<<2)) >> 2); |
jmar7 | 0:e8167f37725c | 492 | } |
jmar7 | 0:e8167f37725c | 493 | |
jmar7 | 0:e8167f37725c | 494 | uint8_t LSM9DS1::magAvailable(lsm9ds1_axis axis) |
jmar7 | 0:e8167f37725c | 495 | { |
jmar7 | 0:e8167f37725c | 496 | uint8_t status; |
jmar7 | 0:e8167f37725c | 497 | status = mReadByte(STATUS_REG_M); |
4180_1 | 2:36abf8e18ade | 498 | |
jmar7 | 0:e8167f37725c | 499 | return ((status & (1<<axis)) >> axis); |
jmar7 | 0:e8167f37725c | 500 | } |
jmar7 | 0:e8167f37725c | 501 | |
jmar7 | 0:e8167f37725c | 502 | void LSM9DS1::readAccel() |
jmar7 | 0:e8167f37725c | 503 | { |
4180_1 | 2:36abf8e18ade | 504 | uint8_t temp[6]; // We'll read six bytes from the accelerometer into temp |
jmar7 | 0:e8167f37725c | 505 | xgReadBytes(OUT_X_L_XL, temp, 6); // Read 6 bytes, beginning at OUT_X_L_XL |
jmar7 | 0:e8167f37725c | 506 | ax = (temp[1] << 8) | temp[0]; // Store x-axis values into ax |
jmar7 | 0:e8167f37725c | 507 | ay = (temp[3] << 8) | temp[2]; // Store y-axis values into ay |
jmar7 | 0:e8167f37725c | 508 | az = (temp[5] << 8) | temp[4]; // Store z-axis values into az |
4180_1 | 2:36abf8e18ade | 509 | if (_autoCalc) { |
jmar7 | 0:e8167f37725c | 510 | ax -= aBiasRaw[X_AXIS]; |
jmar7 | 0:e8167f37725c | 511 | ay -= aBiasRaw[Y_AXIS]; |
jmar7 | 0:e8167f37725c | 512 | az -= aBiasRaw[Z_AXIS]; |
jmar7 | 0:e8167f37725c | 513 | } |
jmar7 | 0:e8167f37725c | 514 | } |
jmar7 | 0:e8167f37725c | 515 | |
jmar7 | 0:e8167f37725c | 516 | int16_t LSM9DS1::readAccel(lsm9ds1_axis axis) |
jmar7 | 0:e8167f37725c | 517 | { |
jmar7 | 0:e8167f37725c | 518 | uint8_t temp[2]; |
jmar7 | 0:e8167f37725c | 519 | int16_t value; |
jmar7 | 0:e8167f37725c | 520 | xgReadBytes(OUT_X_L_XL + (2 * axis), temp, 2); |
jmar7 | 0:e8167f37725c | 521 | value = (temp[1] << 8) | temp[0]; |
4180_1 | 2:36abf8e18ade | 522 | |
jmar7 | 0:e8167f37725c | 523 | if (_autoCalc) |
jmar7 | 0:e8167f37725c | 524 | value -= aBiasRaw[axis]; |
4180_1 | 2:36abf8e18ade | 525 | |
jmar7 | 0:e8167f37725c | 526 | return value; |
jmar7 | 0:e8167f37725c | 527 | } |
jmar7 | 0:e8167f37725c | 528 | |
jmar7 | 0:e8167f37725c | 529 | void LSM9DS1::readMag() |
jmar7 | 0:e8167f37725c | 530 | { |
4180_1 | 2:36abf8e18ade | 531 | uint8_t temp[6]; // We'll read six bytes from the mag into temp |
jmar7 | 0:e8167f37725c | 532 | mReadBytes(OUT_X_L_M, temp, 6); // Read 6 bytes, beginning at OUT_X_L_M |
jmar7 | 0:e8167f37725c | 533 | mx = (temp[1] << 8) | temp[0]; // Store x-axis values into mx |
jmar7 | 0:e8167f37725c | 534 | my = (temp[3] << 8) | temp[2]; // Store y-axis values into my |
jmar7 | 0:e8167f37725c | 535 | mz = (temp[5] << 8) | temp[4]; // Store z-axis values into mz |
4180_1 | 2:36abf8e18ade | 536 | mx = mx - mBiasRaw[0]; |
4180_1 | 2:36abf8e18ade | 537 | my = my - mBiasRaw[1]; |
4180_1 | 2:36abf8e18ade | 538 | mz = mz - mBiasRaw[2]; |
jmar7 | 0:e8167f37725c | 539 | } |
jmar7 | 0:e8167f37725c | 540 | |
jmar7 | 0:e8167f37725c | 541 | int16_t LSM9DS1::readMag(lsm9ds1_axis axis) |
jmar7 | 0:e8167f37725c | 542 | { |
jmar7 | 0:e8167f37725c | 543 | uint8_t temp[2]; |
jmar7 | 0:e8167f37725c | 544 | mReadBytes(OUT_X_L_M + (2 * axis), temp, 2); |
jmar7 | 0:e8167f37725c | 545 | return (temp[1] << 8) | temp[0]; |
jmar7 | 0:e8167f37725c | 546 | } |
jmar7 | 0:e8167f37725c | 547 | |
jmar7 | 0:e8167f37725c | 548 | void LSM9DS1::readTemp() |
jmar7 | 0:e8167f37725c | 549 | { |
4180_1 | 2:36abf8e18ade | 550 | uint8_t temp[2]; // We'll read two bytes from the temperature sensor into temp |
jmar7 | 0:e8167f37725c | 551 | xgReadBytes(OUT_TEMP_L, temp, 2); // Read 2 bytes, beginning at OUT_TEMP_L |
4180_1 | 2:36abf8e18ade | 552 | temperature = (int16_t)((temp[1] << 8) | temp[0]); |
jmar7 | 0:e8167f37725c | 553 | } |
jmar7 | 0:e8167f37725c | 554 | |
jmar7 | 0:e8167f37725c | 555 | void LSM9DS1::readGyro() |
jmar7 | 0:e8167f37725c | 556 | { |
jmar7 | 0:e8167f37725c | 557 | uint8_t temp[6]; // We'll read six bytes from the gyro into temp |
jmar7 | 0:e8167f37725c | 558 | xgReadBytes(OUT_X_L_G, temp, 6); // Read 6 bytes, beginning at OUT_X_L_G |
jmar7 | 0:e8167f37725c | 559 | gx = (temp[1] << 8) | temp[0]; // Store x-axis values into gx |
jmar7 | 0:e8167f37725c | 560 | gy = (temp[3] << 8) | temp[2]; // Store y-axis values into gy |
jmar7 | 0:e8167f37725c | 561 | gz = (temp[5] << 8) | temp[4]; // Store z-axis values into gz |
4180_1 | 2:36abf8e18ade | 562 | if (_autoCalc) { |
jmar7 | 0:e8167f37725c | 563 | gx -= gBiasRaw[X_AXIS]; |
jmar7 | 0:e8167f37725c | 564 | gy -= gBiasRaw[Y_AXIS]; |
jmar7 | 0:e8167f37725c | 565 | gz -= gBiasRaw[Z_AXIS]; |
jmar7 | 0:e8167f37725c | 566 | } |
jmar7 | 0:e8167f37725c | 567 | } |
jmar7 | 0:e8167f37725c | 568 | |
jmar7 | 0:e8167f37725c | 569 | int16_t LSM9DS1::readGyro(lsm9ds1_axis axis) |
jmar7 | 0:e8167f37725c | 570 | { |
jmar7 | 0:e8167f37725c | 571 | uint8_t temp[2]; |
jmar7 | 0:e8167f37725c | 572 | int16_t value; |
4180_1 | 2:36abf8e18ade | 573 | |
jmar7 | 0:e8167f37725c | 574 | xgReadBytes(OUT_X_L_G + (2 * axis), temp, 2); |
4180_1 | 2:36abf8e18ade | 575 | |
jmar7 | 0:e8167f37725c | 576 | value = (temp[1] << 8) | temp[0]; |
4180_1 | 2:36abf8e18ade | 577 | |
jmar7 | 0:e8167f37725c | 578 | if (_autoCalc) |
jmar7 | 0:e8167f37725c | 579 | value -= gBiasRaw[axis]; |
4180_1 | 2:36abf8e18ade | 580 | |
jmar7 | 0:e8167f37725c | 581 | return value; |
jmar7 | 0:e8167f37725c | 582 | } |
jmar7 | 0:e8167f37725c | 583 | |
jmar7 | 0:e8167f37725c | 584 | float LSM9DS1::calcGyro(int16_t gyro) |
jmar7 | 0:e8167f37725c | 585 | { |
jmar7 | 0:e8167f37725c | 586 | // Return the gyro raw reading times our pre-calculated DPS / (ADC tick): |
4180_1 | 2:36abf8e18ade | 587 | return gRes * gyro; |
jmar7 | 0:e8167f37725c | 588 | } |
jmar7 | 0:e8167f37725c | 589 | |
jmar7 | 0:e8167f37725c | 590 | float LSM9DS1::calcAccel(int16_t accel) |
jmar7 | 0:e8167f37725c | 591 | { |
jmar7 | 0:e8167f37725c | 592 | // Return the accel raw reading times our pre-calculated g's / (ADC tick): |
jmar7 | 0:e8167f37725c | 593 | return aRes * accel; |
jmar7 | 0:e8167f37725c | 594 | } |
jmar7 | 0:e8167f37725c | 595 | |
jmar7 | 0:e8167f37725c | 596 | float LSM9DS1::calcMag(int16_t mag) |
jmar7 | 0:e8167f37725c | 597 | { |
jmar7 | 0:e8167f37725c | 598 | // Return the mag raw reading times our pre-calculated Gs / (ADC tick): |
jmar7 | 0:e8167f37725c | 599 | return mRes * mag; |
jmar7 | 0:e8167f37725c | 600 | } |
jmar7 | 0:e8167f37725c | 601 | |
jmar7 | 0:e8167f37725c | 602 | void LSM9DS1::setGyroScale(uint16_t gScl) |
jmar7 | 0:e8167f37725c | 603 | { |
jmar7 | 0:e8167f37725c | 604 | // Read current value of CTRL_REG1_G: |
jmar7 | 0:e8167f37725c | 605 | uint8_t ctrl1RegValue = xgReadByte(CTRL_REG1_G); |
jmar7 | 0:e8167f37725c | 606 | // Mask out scale bits (3 & 4): |
jmar7 | 0:e8167f37725c | 607 | ctrl1RegValue &= 0xE7; |
4180_1 | 2:36abf8e18ade | 608 | switch (gScl) { |
jmar7 | 0:e8167f37725c | 609 | case 500: |
jmar7 | 0:e8167f37725c | 610 | ctrl1RegValue |= (0x1 << 3); |
jmar7 | 0:e8167f37725c | 611 | settings.gyro.scale = 500; |
jmar7 | 0:e8167f37725c | 612 | break; |
jmar7 | 0:e8167f37725c | 613 | case 2000: |
jmar7 | 0:e8167f37725c | 614 | ctrl1RegValue |= (0x3 << 3); |
jmar7 | 0:e8167f37725c | 615 | settings.gyro.scale = 2000; |
jmar7 | 0:e8167f37725c | 616 | break; |
jmar7 | 0:e8167f37725c | 617 | default: // Otherwise we'll set it to 245 dps (0x0 << 4) |
jmar7 | 0:e8167f37725c | 618 | settings.gyro.scale = 245; |
jmar7 | 0:e8167f37725c | 619 | break; |
jmar7 | 0:e8167f37725c | 620 | } |
jmar7 | 0:e8167f37725c | 621 | xgWriteByte(CTRL_REG1_G, ctrl1RegValue); |
4180_1 | 2:36abf8e18ade | 622 | |
4180_1 | 2:36abf8e18ade | 623 | calcgRes(); |
jmar7 | 0:e8167f37725c | 624 | } |
jmar7 | 0:e8167f37725c | 625 | |
jmar7 | 0:e8167f37725c | 626 | void LSM9DS1::setAccelScale(uint8_t aScl) |
jmar7 | 0:e8167f37725c | 627 | { |
jmar7 | 0:e8167f37725c | 628 | // We need to preserve the other bytes in CTRL_REG6_XL. So, first read it: |
jmar7 | 0:e8167f37725c | 629 | uint8_t tempRegValue = xgReadByte(CTRL_REG6_XL); |
jmar7 | 0:e8167f37725c | 630 | // Mask out accel scale bits: |
jmar7 | 0:e8167f37725c | 631 | tempRegValue &= 0xE7; |
4180_1 | 2:36abf8e18ade | 632 | |
4180_1 | 2:36abf8e18ade | 633 | switch (aScl) { |
jmar7 | 0:e8167f37725c | 634 | case 4: |
jmar7 | 0:e8167f37725c | 635 | tempRegValue |= (0x2 << 3); |
jmar7 | 0:e8167f37725c | 636 | settings.accel.scale = 4; |
jmar7 | 0:e8167f37725c | 637 | break; |
jmar7 | 0:e8167f37725c | 638 | case 8: |
jmar7 | 0:e8167f37725c | 639 | tempRegValue |= (0x3 << 3); |
jmar7 | 0:e8167f37725c | 640 | settings.accel.scale = 8; |
jmar7 | 0:e8167f37725c | 641 | break; |
jmar7 | 0:e8167f37725c | 642 | case 16: |
jmar7 | 0:e8167f37725c | 643 | tempRegValue |= (0x1 << 3); |
jmar7 | 0:e8167f37725c | 644 | settings.accel.scale = 16; |
jmar7 | 0:e8167f37725c | 645 | break; |
jmar7 | 0:e8167f37725c | 646 | default: // Otherwise it'll be set to 2g (0x0 << 3) |
jmar7 | 0:e8167f37725c | 647 | settings.accel.scale = 2; |
jmar7 | 0:e8167f37725c | 648 | break; |
jmar7 | 0:e8167f37725c | 649 | } |
jmar7 | 0:e8167f37725c | 650 | xgWriteByte(CTRL_REG6_XL, tempRegValue); |
4180_1 | 2:36abf8e18ade | 651 | |
jmar7 | 0:e8167f37725c | 652 | // Then calculate a new aRes, which relies on aScale being set correctly: |
jmar7 | 0:e8167f37725c | 653 | calcaRes(); |
jmar7 | 0:e8167f37725c | 654 | } |
jmar7 | 0:e8167f37725c | 655 | |
jmar7 | 0:e8167f37725c | 656 | void LSM9DS1::setMagScale(uint8_t mScl) |
jmar7 | 0:e8167f37725c | 657 | { |
jmar7 | 0:e8167f37725c | 658 | // We need to preserve the other bytes in CTRL_REG6_XM. So, first read it: |
jmar7 | 0:e8167f37725c | 659 | uint8_t temp = mReadByte(CTRL_REG2_M); |
jmar7 | 0:e8167f37725c | 660 | // Then mask out the mag scale bits: |
jmar7 | 0:e8167f37725c | 661 | temp &= 0xFF^(0x3 << 5); |
4180_1 | 2:36abf8e18ade | 662 | |
4180_1 | 2:36abf8e18ade | 663 | switch (mScl) { |
4180_1 | 2:36abf8e18ade | 664 | case 8: |
4180_1 | 2:36abf8e18ade | 665 | temp |= (0x1 << 5); |
4180_1 | 2:36abf8e18ade | 666 | settings.mag.scale = 8; |
4180_1 | 2:36abf8e18ade | 667 | break; |
4180_1 | 2:36abf8e18ade | 668 | case 12: |
4180_1 | 2:36abf8e18ade | 669 | temp |= (0x2 << 5); |
4180_1 | 2:36abf8e18ade | 670 | settings.mag.scale = 12; |
4180_1 | 2:36abf8e18ade | 671 | break; |
4180_1 | 2:36abf8e18ade | 672 | case 16: |
4180_1 | 2:36abf8e18ade | 673 | temp |= (0x3 << 5); |
4180_1 | 2:36abf8e18ade | 674 | settings.mag.scale = 16; |
4180_1 | 2:36abf8e18ade | 675 | break; |
4180_1 | 2:36abf8e18ade | 676 | default: // Otherwise we'll default to 4 gauss (00) |
4180_1 | 2:36abf8e18ade | 677 | settings.mag.scale = 4; |
4180_1 | 2:36abf8e18ade | 678 | break; |
4180_1 | 2:36abf8e18ade | 679 | } |
4180_1 | 2:36abf8e18ade | 680 | |
jmar7 | 0:e8167f37725c | 681 | // And write the new register value back into CTRL_REG6_XM: |
jmar7 | 0:e8167f37725c | 682 | mWriteByte(CTRL_REG2_M, temp); |
4180_1 | 2:36abf8e18ade | 683 | |
jmar7 | 0:e8167f37725c | 684 | // We've updated the sensor, but we also need to update our class variables |
jmar7 | 0:e8167f37725c | 685 | // First update mScale: |
jmar7 | 0:e8167f37725c | 686 | //mScale = mScl; |
jmar7 | 0:e8167f37725c | 687 | // Then calculate a new mRes, which relies on mScale being set correctly: |
jmar7 | 0:e8167f37725c | 688 | calcmRes(); |
jmar7 | 0:e8167f37725c | 689 | } |
jmar7 | 0:e8167f37725c | 690 | |
jmar7 | 0:e8167f37725c | 691 | void LSM9DS1::setGyroODR(uint8_t gRate) |
jmar7 | 0:e8167f37725c | 692 | { |
jmar7 | 0:e8167f37725c | 693 | // Only do this if gRate is not 0 (which would disable the gyro) |
4180_1 | 2:36abf8e18ade | 694 | if ((gRate & 0x07) != 0) { |
jmar7 | 0:e8167f37725c | 695 | // We need to preserve the other bytes in CTRL_REG1_G. So, first read it: |
jmar7 | 0:e8167f37725c | 696 | uint8_t temp = xgReadByte(CTRL_REG1_G); |
jmar7 | 0:e8167f37725c | 697 | // Then mask out the gyro ODR bits: |
jmar7 | 0:e8167f37725c | 698 | temp &= 0xFF^(0x7 << 5); |
jmar7 | 0:e8167f37725c | 699 | temp |= (gRate & 0x07) << 5; |
jmar7 | 0:e8167f37725c | 700 | // Update our settings struct |
jmar7 | 0:e8167f37725c | 701 | settings.gyro.sampleRate = gRate & 0x07; |
jmar7 | 0:e8167f37725c | 702 | // And write the new register value back into CTRL_REG1_G: |
jmar7 | 0:e8167f37725c | 703 | xgWriteByte(CTRL_REG1_G, temp); |
jmar7 | 0:e8167f37725c | 704 | } |
jmar7 | 0:e8167f37725c | 705 | } |
jmar7 | 0:e8167f37725c | 706 | |
jmar7 | 0:e8167f37725c | 707 | void LSM9DS1::setAccelODR(uint8_t aRate) |
jmar7 | 0:e8167f37725c | 708 | { |
jmar7 | 0:e8167f37725c | 709 | // Only do this if aRate is not 0 (which would disable the accel) |
4180_1 | 2:36abf8e18ade | 710 | if ((aRate & 0x07) != 0) { |
jmar7 | 0:e8167f37725c | 711 | // We need to preserve the other bytes in CTRL_REG1_XM. So, first read it: |
jmar7 | 0:e8167f37725c | 712 | uint8_t temp = xgReadByte(CTRL_REG6_XL); |
jmar7 | 0:e8167f37725c | 713 | // Then mask out the accel ODR bits: |
jmar7 | 0:e8167f37725c | 714 | temp &= 0x1F; |
jmar7 | 0:e8167f37725c | 715 | // Then shift in our new ODR bits: |
jmar7 | 0:e8167f37725c | 716 | temp |= ((aRate & 0x07) << 5); |
jmar7 | 0:e8167f37725c | 717 | settings.accel.sampleRate = aRate & 0x07; |
jmar7 | 0:e8167f37725c | 718 | // And write the new register value back into CTRL_REG1_XM: |
jmar7 | 0:e8167f37725c | 719 | xgWriteByte(CTRL_REG6_XL, temp); |
jmar7 | 0:e8167f37725c | 720 | } |
jmar7 | 0:e8167f37725c | 721 | } |
jmar7 | 0:e8167f37725c | 722 | |
jmar7 | 0:e8167f37725c | 723 | void LSM9DS1::setMagODR(uint8_t mRate) |
jmar7 | 0:e8167f37725c | 724 | { |
jmar7 | 0:e8167f37725c | 725 | // We need to preserve the other bytes in CTRL_REG5_XM. So, first read it: |
jmar7 | 0:e8167f37725c | 726 | uint8_t temp = mReadByte(CTRL_REG1_M); |
jmar7 | 0:e8167f37725c | 727 | // Then mask out the mag ODR bits: |
jmar7 | 0:e8167f37725c | 728 | temp &= 0xFF^(0x7 << 2); |
jmar7 | 0:e8167f37725c | 729 | // Then shift in our new ODR bits: |
jmar7 | 0:e8167f37725c | 730 | temp |= ((mRate & 0x07) << 2); |
jmar7 | 0:e8167f37725c | 731 | settings.mag.sampleRate = mRate & 0x07; |
jmar7 | 0:e8167f37725c | 732 | // And write the new register value back into CTRL_REG5_XM: |
jmar7 | 0:e8167f37725c | 733 | mWriteByte(CTRL_REG1_M, temp); |
jmar7 | 0:e8167f37725c | 734 | } |
jmar7 | 0:e8167f37725c | 735 | |
jmar7 | 0:e8167f37725c | 736 | void LSM9DS1::calcgRes() |
jmar7 | 0:e8167f37725c | 737 | { |
jmar7 | 0:e8167f37725c | 738 | gRes = ((float) settings.gyro.scale) / 32768.0; |
jmar7 | 0:e8167f37725c | 739 | } |
jmar7 | 0:e8167f37725c | 740 | |
jmar7 | 0:e8167f37725c | 741 | void LSM9DS1::calcaRes() |
jmar7 | 0:e8167f37725c | 742 | { |
jmar7 | 0:e8167f37725c | 743 | aRes = ((float) settings.accel.scale) / 32768.0; |
jmar7 | 0:e8167f37725c | 744 | } |
jmar7 | 0:e8167f37725c | 745 | |
jmar7 | 0:e8167f37725c | 746 | void LSM9DS1::calcmRes() |
jmar7 | 0:e8167f37725c | 747 | { |
jmar7 | 0:e8167f37725c | 748 | //mRes = ((float) settings.mag.scale) / 32768.0; |
4180_1 | 2:36abf8e18ade | 749 | switch (settings.mag.scale) { |
4180_1 | 2:36abf8e18ade | 750 | case 4: |
4180_1 | 2:36abf8e18ade | 751 | mRes = magSensitivity[0]; |
4180_1 | 2:36abf8e18ade | 752 | break; |
4180_1 | 2:36abf8e18ade | 753 | case 8: |
4180_1 | 2:36abf8e18ade | 754 | mRes = magSensitivity[1]; |
4180_1 | 2:36abf8e18ade | 755 | break; |
4180_1 | 2:36abf8e18ade | 756 | case 12: |
4180_1 | 2:36abf8e18ade | 757 | mRes = magSensitivity[2]; |
4180_1 | 2:36abf8e18ade | 758 | break; |
4180_1 | 2:36abf8e18ade | 759 | case 16: |
4180_1 | 2:36abf8e18ade | 760 | mRes = magSensitivity[3]; |
4180_1 | 2:36abf8e18ade | 761 | break; |
jmar7 | 0:e8167f37725c | 762 | } |
4180_1 | 2:36abf8e18ade | 763 | |
jmar7 | 0:e8167f37725c | 764 | } |
jmar7 | 0:e8167f37725c | 765 | |
jmar7 | 0:e8167f37725c | 766 | void LSM9DS1::configInt(interrupt_select interrupt, uint8_t generator, |
4180_1 | 2:36abf8e18ade | 767 | h_lactive activeLow, pp_od pushPull) |
jmar7 | 0:e8167f37725c | 768 | { |
jmar7 | 0:e8167f37725c | 769 | // Write to INT1_CTRL or INT2_CTRL. [interupt] should already be one of |
jmar7 | 0:e8167f37725c | 770 | // those two values. |
jmar7 | 0:e8167f37725c | 771 | // [generator] should be an OR'd list of values from the interrupt_generators enum |
jmar7 | 0:e8167f37725c | 772 | xgWriteByte(interrupt, generator); |
4180_1 | 2:36abf8e18ade | 773 | |
jmar7 | 0:e8167f37725c | 774 | // Configure CTRL_REG8 |
jmar7 | 0:e8167f37725c | 775 | uint8_t temp; |
jmar7 | 0:e8167f37725c | 776 | temp = xgReadByte(CTRL_REG8); |
4180_1 | 2:36abf8e18ade | 777 | |
jmar7 | 0:e8167f37725c | 778 | if (activeLow) temp |= (1<<5); |
jmar7 | 0:e8167f37725c | 779 | else temp &= ~(1<<5); |
4180_1 | 2:36abf8e18ade | 780 | |
jmar7 | 0:e8167f37725c | 781 | if (pushPull) temp &= ~(1<<4); |
jmar7 | 0:e8167f37725c | 782 | else temp |= (1<<4); |
4180_1 | 2:36abf8e18ade | 783 | |
jmar7 | 0:e8167f37725c | 784 | xgWriteByte(CTRL_REG8, temp); |
jmar7 | 0:e8167f37725c | 785 | } |
jmar7 | 0:e8167f37725c | 786 | |
jmar7 | 0:e8167f37725c | 787 | void LSM9DS1::configInactivity(uint8_t duration, uint8_t threshold, bool sleepOn) |
jmar7 | 0:e8167f37725c | 788 | { |
jmar7 | 0:e8167f37725c | 789 | uint8_t temp = 0; |
4180_1 | 2:36abf8e18ade | 790 | |
jmar7 | 0:e8167f37725c | 791 | temp = threshold & 0x7F; |
jmar7 | 0:e8167f37725c | 792 | if (sleepOn) temp |= (1<<7); |
jmar7 | 0:e8167f37725c | 793 | xgWriteByte(ACT_THS, temp); |
4180_1 | 2:36abf8e18ade | 794 | |
jmar7 | 0:e8167f37725c | 795 | xgWriteByte(ACT_DUR, duration); |
jmar7 | 0:e8167f37725c | 796 | } |
jmar7 | 0:e8167f37725c | 797 | |
jmar7 | 0:e8167f37725c | 798 | uint8_t LSM9DS1::getInactivity() |
jmar7 | 0:e8167f37725c | 799 | { |
jmar7 | 0:e8167f37725c | 800 | uint8_t temp = xgReadByte(STATUS_REG_0); |
jmar7 | 0:e8167f37725c | 801 | temp &= (0x10); |
jmar7 | 0:e8167f37725c | 802 | return temp; |
jmar7 | 0:e8167f37725c | 803 | } |
jmar7 | 0:e8167f37725c | 804 | |
jmar7 | 0:e8167f37725c | 805 | void LSM9DS1::configAccelInt(uint8_t generator, bool andInterrupts) |
jmar7 | 0:e8167f37725c | 806 | { |
jmar7 | 0:e8167f37725c | 807 | // Use variables from accel_interrupt_generator, OR'd together to create |
jmar7 | 0:e8167f37725c | 808 | // the [generator]value. |
jmar7 | 0:e8167f37725c | 809 | uint8_t temp = generator; |
jmar7 | 0:e8167f37725c | 810 | if (andInterrupts) temp |= 0x80; |
jmar7 | 0:e8167f37725c | 811 | xgWriteByte(INT_GEN_CFG_XL, temp); |
jmar7 | 0:e8167f37725c | 812 | } |
jmar7 | 0:e8167f37725c | 813 | |
jmar7 | 0:e8167f37725c | 814 | void LSM9DS1::configAccelThs(uint8_t threshold, lsm9ds1_axis axis, uint8_t duration, bool wait) |
jmar7 | 0:e8167f37725c | 815 | { |
jmar7 | 0:e8167f37725c | 816 | // Write threshold value to INT_GEN_THS_?_XL. |
jmar7 | 0:e8167f37725c | 817 | // axis will be 0, 1, or 2 (x, y, z respectively) |
jmar7 | 0:e8167f37725c | 818 | xgWriteByte(INT_GEN_THS_X_XL + axis, threshold); |
4180_1 | 2:36abf8e18ade | 819 | |
jmar7 | 0:e8167f37725c | 820 | // Write duration and wait to INT_GEN_DUR_XL |
jmar7 | 0:e8167f37725c | 821 | uint8_t temp; |
jmar7 | 0:e8167f37725c | 822 | temp = (duration & 0x7F); |
jmar7 | 0:e8167f37725c | 823 | if (wait) temp |= 0x80; |
jmar7 | 0:e8167f37725c | 824 | xgWriteByte(INT_GEN_DUR_XL, temp); |
jmar7 | 0:e8167f37725c | 825 | } |
jmar7 | 0:e8167f37725c | 826 | |
jmar7 | 0:e8167f37725c | 827 | uint8_t LSM9DS1::getAccelIntSrc() |
jmar7 | 0:e8167f37725c | 828 | { |
jmar7 | 0:e8167f37725c | 829 | uint8_t intSrc = xgReadByte(INT_GEN_SRC_XL); |
4180_1 | 2:36abf8e18ade | 830 | |
jmar7 | 0:e8167f37725c | 831 | // Check if the IA_XL (interrupt active) bit is set |
4180_1 | 2:36abf8e18ade | 832 | if (intSrc & (1<<6)) { |
jmar7 | 0:e8167f37725c | 833 | return (intSrc & 0x3F); |
jmar7 | 0:e8167f37725c | 834 | } |
4180_1 | 2:36abf8e18ade | 835 | |
jmar7 | 0:e8167f37725c | 836 | return 0; |
jmar7 | 0:e8167f37725c | 837 | } |
jmar7 | 0:e8167f37725c | 838 | |
jmar7 | 0:e8167f37725c | 839 | void LSM9DS1::configGyroInt(uint8_t generator, bool aoi, bool latch) |
jmar7 | 0:e8167f37725c | 840 | { |
jmar7 | 0:e8167f37725c | 841 | // Use variables from accel_interrupt_generator, OR'd together to create |
jmar7 | 0:e8167f37725c | 842 | // the [generator]value. |
jmar7 | 0:e8167f37725c | 843 | uint8_t temp = generator; |
jmar7 | 0:e8167f37725c | 844 | if (aoi) temp |= 0x80; |
jmar7 | 0:e8167f37725c | 845 | if (latch) temp |= 0x40; |
jmar7 | 0:e8167f37725c | 846 | xgWriteByte(INT_GEN_CFG_G, temp); |
jmar7 | 0:e8167f37725c | 847 | } |
jmar7 | 0:e8167f37725c | 848 | |
jmar7 | 0:e8167f37725c | 849 | void LSM9DS1::configGyroThs(int16_t threshold, lsm9ds1_axis axis, uint8_t duration, bool wait) |
jmar7 | 0:e8167f37725c | 850 | { |
jmar7 | 0:e8167f37725c | 851 | uint8_t buffer[2]; |
jmar7 | 0:e8167f37725c | 852 | buffer[0] = (threshold & 0x7F00) >> 8; |
jmar7 | 0:e8167f37725c | 853 | buffer[1] = (threshold & 0x00FF); |
jmar7 | 0:e8167f37725c | 854 | // Write threshold value to INT_GEN_THS_?H_G and INT_GEN_THS_?L_G. |
jmar7 | 0:e8167f37725c | 855 | // axis will be 0, 1, or 2 (x, y, z respectively) |
jmar7 | 0:e8167f37725c | 856 | xgWriteByte(INT_GEN_THS_XH_G + (axis * 2), buffer[0]); |
jmar7 | 0:e8167f37725c | 857 | xgWriteByte(INT_GEN_THS_XH_G + 1 + (axis * 2), buffer[1]); |
4180_1 | 2:36abf8e18ade | 858 | |
jmar7 | 0:e8167f37725c | 859 | // Write duration and wait to INT_GEN_DUR_XL |
jmar7 | 0:e8167f37725c | 860 | uint8_t temp; |
jmar7 | 0:e8167f37725c | 861 | temp = (duration & 0x7F); |
jmar7 | 0:e8167f37725c | 862 | if (wait) temp |= 0x80; |
jmar7 | 0:e8167f37725c | 863 | xgWriteByte(INT_GEN_DUR_G, temp); |
jmar7 | 0:e8167f37725c | 864 | } |
jmar7 | 0:e8167f37725c | 865 | |
jmar7 | 0:e8167f37725c | 866 | uint8_t LSM9DS1::getGyroIntSrc() |
jmar7 | 0:e8167f37725c | 867 | { |
jmar7 | 0:e8167f37725c | 868 | uint8_t intSrc = xgReadByte(INT_GEN_SRC_G); |
4180_1 | 2:36abf8e18ade | 869 | |
jmar7 | 0:e8167f37725c | 870 | // Check if the IA_G (interrupt active) bit is set |
4180_1 | 2:36abf8e18ade | 871 | if (intSrc & (1<<6)) { |
jmar7 | 0:e8167f37725c | 872 | return (intSrc & 0x3F); |
jmar7 | 0:e8167f37725c | 873 | } |
4180_1 | 2:36abf8e18ade | 874 | |
jmar7 | 0:e8167f37725c | 875 | return 0; |
jmar7 | 0:e8167f37725c | 876 | } |
jmar7 | 0:e8167f37725c | 877 | |
jmar7 | 0:e8167f37725c | 878 | void LSM9DS1::configMagInt(uint8_t generator, h_lactive activeLow, bool latch) |
jmar7 | 0:e8167f37725c | 879 | { |
jmar7 | 0:e8167f37725c | 880 | // Mask out non-generator bits (0-4) |
4180_1 | 2:36abf8e18ade | 881 | uint8_t config = (generator & 0xE0); |
jmar7 | 0:e8167f37725c | 882 | // IEA bit is 0 for active-low, 1 for active-high. |
jmar7 | 0:e8167f37725c | 883 | if (activeLow == INT_ACTIVE_HIGH) config |= (1<<2); |
jmar7 | 0:e8167f37725c | 884 | // IEL bit is 0 for latched, 1 for not-latched |
jmar7 | 0:e8167f37725c | 885 | if (!latch) config |= (1<<1); |
jmar7 | 0:e8167f37725c | 886 | // As long as we have at least 1 generator, enable the interrupt |
jmar7 | 0:e8167f37725c | 887 | if (generator != 0) config |= (1<<0); |
4180_1 | 2:36abf8e18ade | 888 | |
jmar7 | 0:e8167f37725c | 889 | mWriteByte(INT_CFG_M, config); |
jmar7 | 0:e8167f37725c | 890 | } |
jmar7 | 0:e8167f37725c | 891 | |
jmar7 | 0:e8167f37725c | 892 | void LSM9DS1::configMagThs(uint16_t threshold) |
jmar7 | 0:e8167f37725c | 893 | { |
jmar7 | 0:e8167f37725c | 894 | // Write high eight bits of [threshold] to INT_THS_H_M |
jmar7 | 0:e8167f37725c | 895 | mWriteByte(INT_THS_H_M, uint8_t((threshold & 0x7F00) >> 8)); |
jmar7 | 0:e8167f37725c | 896 | // Write low eight bits of [threshold] to INT_THS_L_M |
jmar7 | 0:e8167f37725c | 897 | mWriteByte(INT_THS_L_M, uint8_t(threshold & 0x00FF)); |
jmar7 | 0:e8167f37725c | 898 | } |
jmar7 | 0:e8167f37725c | 899 | |
jmar7 | 0:e8167f37725c | 900 | uint8_t LSM9DS1::getMagIntSrc() |
jmar7 | 0:e8167f37725c | 901 | { |
jmar7 | 0:e8167f37725c | 902 | uint8_t intSrc = mReadByte(INT_SRC_M); |
4180_1 | 2:36abf8e18ade | 903 | |
jmar7 | 0:e8167f37725c | 904 | // Check if the INT (interrupt active) bit is set |
4180_1 | 2:36abf8e18ade | 905 | if (intSrc & (1<<0)) { |
jmar7 | 0:e8167f37725c | 906 | return (intSrc & 0xFE); |
jmar7 | 0:e8167f37725c | 907 | } |
4180_1 | 2:36abf8e18ade | 908 | |
jmar7 | 0:e8167f37725c | 909 | return 0; |
jmar7 | 0:e8167f37725c | 910 | } |
jmar7 | 0:e8167f37725c | 911 | |
jmar7 | 0:e8167f37725c | 912 | void LSM9DS1::sleepGyro(bool enable) |
jmar7 | 0:e8167f37725c | 913 | { |
jmar7 | 0:e8167f37725c | 914 | uint8_t temp = xgReadByte(CTRL_REG9); |
jmar7 | 0:e8167f37725c | 915 | if (enable) temp |= (1<<6); |
jmar7 | 0:e8167f37725c | 916 | else temp &= ~(1<<6); |
jmar7 | 0:e8167f37725c | 917 | xgWriteByte(CTRL_REG9, temp); |
jmar7 | 0:e8167f37725c | 918 | } |
jmar7 | 0:e8167f37725c | 919 | |
jmar7 | 0:e8167f37725c | 920 | void LSM9DS1::enableFIFO(bool enable) |
jmar7 | 0:e8167f37725c | 921 | { |
jmar7 | 0:e8167f37725c | 922 | uint8_t temp = xgReadByte(CTRL_REG9); |
jmar7 | 0:e8167f37725c | 923 | if (enable) temp |= (1<<1); |
jmar7 | 0:e8167f37725c | 924 | else temp &= ~(1<<1); |
jmar7 | 0:e8167f37725c | 925 | xgWriteByte(CTRL_REG9, temp); |
jmar7 | 0:e8167f37725c | 926 | } |
jmar7 | 0:e8167f37725c | 927 | |
jmar7 | 0:e8167f37725c | 928 | void LSM9DS1::setFIFO(fifoMode_type fifoMode, uint8_t fifoThs) |
jmar7 | 0:e8167f37725c | 929 | { |
jmar7 | 0:e8167f37725c | 930 | // Limit threshold - 0x1F (31) is the maximum. If more than that was asked |
jmar7 | 0:e8167f37725c | 931 | // limit it to the maximum. |
jmar7 | 0:e8167f37725c | 932 | uint8_t threshold = fifoThs <= 0x1F ? fifoThs : 0x1F; |
jmar7 | 0:e8167f37725c | 933 | xgWriteByte(FIFO_CTRL, ((fifoMode & 0x7) << 5) | (threshold & 0x1F)); |
jmar7 | 0:e8167f37725c | 934 | } |
jmar7 | 0:e8167f37725c | 935 | |
jmar7 | 0:e8167f37725c | 936 | uint8_t LSM9DS1::getFIFOSamples() |
jmar7 | 0:e8167f37725c | 937 | { |
jmar7 | 0:e8167f37725c | 938 | return (xgReadByte(FIFO_SRC) & 0x3F); |
jmar7 | 0:e8167f37725c | 939 | } |
jmar7 | 0:e8167f37725c | 940 | |
jmar7 | 0:e8167f37725c | 941 | void LSM9DS1::constrainScales() |
jmar7 | 0:e8167f37725c | 942 | { |
4180_1 | 2:36abf8e18ade | 943 | if ((settings.gyro.scale != 245) && (settings.gyro.scale != 500) && |
4180_1 | 2:36abf8e18ade | 944 | (settings.gyro.scale != 2000)) { |
jmar7 | 0:e8167f37725c | 945 | settings.gyro.scale = 245; |
jmar7 | 0:e8167f37725c | 946 | } |
4180_1 | 2:36abf8e18ade | 947 | |
jmar7 | 0:e8167f37725c | 948 | if ((settings.accel.scale != 2) && (settings.accel.scale != 4) && |
4180_1 | 2:36abf8e18ade | 949 | (settings.accel.scale != 8) && (settings.accel.scale != 16)) { |
jmar7 | 0:e8167f37725c | 950 | settings.accel.scale = 2; |
jmar7 | 0:e8167f37725c | 951 | } |
4180_1 | 2:36abf8e18ade | 952 | |
jmar7 | 0:e8167f37725c | 953 | if ((settings.mag.scale != 4) && (settings.mag.scale != 8) && |
4180_1 | 2:36abf8e18ade | 954 | (settings.mag.scale != 12) && (settings.mag.scale != 16)) { |
jmar7 | 0:e8167f37725c | 955 | settings.mag.scale = 4; |
jmar7 | 0:e8167f37725c | 956 | } |
jmar7 | 0:e8167f37725c | 957 | } |
jmar7 | 0:e8167f37725c | 958 | |
jmar7 | 0:e8167f37725c | 959 | void LSM9DS1::xgWriteByte(uint8_t subAddress, uint8_t data) |
jmar7 | 0:e8167f37725c | 960 | { |
jmar7 | 0:e8167f37725c | 961 | // Whether we're using I2C or SPI, write a byte using the |
jmar7 | 0:e8167f37725c | 962 | // gyro-specific I2C address or SPI CS pin. |
jmar7 | 0:e8167f37725c | 963 | if (settings.device.commInterface == IMU_MODE_I2C) { |
4180_1 | 2:36abf8e18ade | 964 | pc.printf("yo"); |
jmar7 | 0:e8167f37725c | 965 | I2CwriteByte(_xgAddress, subAddress, data); |
jmar7 | 0:e8167f37725c | 966 | } else if (settings.device.commInterface == IMU_MODE_SPI) { |
jmar7 | 0:e8167f37725c | 967 | SPIwriteByte(_xgAddress, subAddress, data); |
jmar7 | 0:e8167f37725c | 968 | } |
jmar7 | 0:e8167f37725c | 969 | } |
jmar7 | 0:e8167f37725c | 970 | |
jmar7 | 0:e8167f37725c | 971 | void LSM9DS1::mWriteByte(uint8_t subAddress, uint8_t data) |
jmar7 | 0:e8167f37725c | 972 | { |
jmar7 | 0:e8167f37725c | 973 | // Whether we're using I2C or SPI, write a byte using the |
jmar7 | 0:e8167f37725c | 974 | // accelerometer-specific I2C address or SPI CS pin. |
4180_1 | 2:36abf8e18ade | 975 | if (settings.device.commInterface == IMU_MODE_I2C) { |
4180_1 | 2:36abf8e18ade | 976 | pc.printf("mo"); |
jmar7 | 0:e8167f37725c | 977 | return I2CwriteByte(_mAddress, subAddress, data); |
4180_1 | 2:36abf8e18ade | 978 | } else if (settings.device.commInterface == IMU_MODE_SPI) |
jmar7 | 0:e8167f37725c | 979 | return SPIwriteByte(_mAddress, subAddress, data); |
jmar7 | 0:e8167f37725c | 980 | } |
jmar7 | 0:e8167f37725c | 981 | |
jmar7 | 0:e8167f37725c | 982 | uint8_t LSM9DS1::xgReadByte(uint8_t subAddress) |
jmar7 | 0:e8167f37725c | 983 | { |
jmar7 | 0:e8167f37725c | 984 | // Whether we're using I2C or SPI, read a byte using the |
jmar7 | 0:e8167f37725c | 985 | // gyro-specific I2C address or SPI CS pin. |
jmar7 | 0:e8167f37725c | 986 | if (settings.device.commInterface == IMU_MODE_I2C) |
jmar7 | 0:e8167f37725c | 987 | return I2CreadByte(_xgAddress, subAddress); |
jmar7 | 0:e8167f37725c | 988 | else if (settings.device.commInterface == IMU_MODE_SPI) |
jmar7 | 0:e8167f37725c | 989 | return SPIreadByte(_xgAddress, subAddress); |
jmar7 | 0:e8167f37725c | 990 | } |
jmar7 | 0:e8167f37725c | 991 | |
jmar7 | 0:e8167f37725c | 992 | void LSM9DS1::xgReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count) |
jmar7 | 0:e8167f37725c | 993 | { |
jmar7 | 0:e8167f37725c | 994 | // Whether we're using I2C or SPI, read multiple bytes using the |
jmar7 | 0:e8167f37725c | 995 | // gyro-specific I2C address or SPI CS pin. |
jmar7 | 0:e8167f37725c | 996 | if (settings.device.commInterface == IMU_MODE_I2C) { |
jmar7 | 0:e8167f37725c | 997 | I2CreadBytes(_xgAddress, subAddress, dest, count); |
jmar7 | 0:e8167f37725c | 998 | } else if (settings.device.commInterface == IMU_MODE_SPI) { |
jmar7 | 0:e8167f37725c | 999 | SPIreadBytes(_xgAddress, subAddress, dest, count); |
jmar7 | 0:e8167f37725c | 1000 | } |
jmar7 | 0:e8167f37725c | 1001 | } |
jmar7 | 0:e8167f37725c | 1002 | |
jmar7 | 0:e8167f37725c | 1003 | uint8_t LSM9DS1::mReadByte(uint8_t subAddress) |
jmar7 | 0:e8167f37725c | 1004 | { |
jmar7 | 0:e8167f37725c | 1005 | // Whether we're using I2C or SPI, read a byte using the |
jmar7 | 0:e8167f37725c | 1006 | // accelerometer-specific I2C address or SPI CS pin. |
jmar7 | 0:e8167f37725c | 1007 | if (settings.device.commInterface == IMU_MODE_I2C) |
jmar7 | 0:e8167f37725c | 1008 | return I2CreadByte(_mAddress, subAddress); |
jmar7 | 0:e8167f37725c | 1009 | else if (settings.device.commInterface == IMU_MODE_SPI) |
jmar7 | 0:e8167f37725c | 1010 | return SPIreadByte(_mAddress, subAddress); |
jmar7 | 0:e8167f37725c | 1011 | } |
jmar7 | 0:e8167f37725c | 1012 | |
jmar7 | 0:e8167f37725c | 1013 | void LSM9DS1::mReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count) |
jmar7 | 0:e8167f37725c | 1014 | { |
jmar7 | 0:e8167f37725c | 1015 | // Whether we're using I2C or SPI, read multiple bytes using the |
jmar7 | 0:e8167f37725c | 1016 | // accelerometer-specific I2C address or SPI CS pin. |
jmar7 | 0:e8167f37725c | 1017 | if (settings.device.commInterface == IMU_MODE_I2C) |
jmar7 | 0:e8167f37725c | 1018 | I2CreadBytes(_mAddress, subAddress, dest, count); |
jmar7 | 0:e8167f37725c | 1019 | else if (settings.device.commInterface == IMU_MODE_SPI) |
jmar7 | 0:e8167f37725c | 1020 | SPIreadBytes(_mAddress, subAddress, dest, count); |
jmar7 | 0:e8167f37725c | 1021 | } |
jmar7 | 0:e8167f37725c | 1022 | |
jmar7 | 0:e8167f37725c | 1023 | void LSM9DS1::initSPI() |
jmar7 | 0:e8167f37725c | 1024 | { |
4180_1 | 2:36abf8e18ade | 1025 | /* |
jmar7 | 0:e8167f37725c | 1026 | pinMode(_xgAddress, OUTPUT); |
jmar7 | 0:e8167f37725c | 1027 | digitalWrite(_xgAddress, HIGH); |
jmar7 | 0:e8167f37725c | 1028 | pinMode(_mAddress, OUTPUT); |
jmar7 | 0:e8167f37725c | 1029 | digitalWrite(_mAddress, HIGH); |
4180_1 | 2:36abf8e18ade | 1030 | |
jmar7 | 0:e8167f37725c | 1031 | SPI.begin(); |
jmar7 | 0:e8167f37725c | 1032 | // Maximum SPI frequency is 10MHz, could divide by 2 here: |
jmar7 | 0:e8167f37725c | 1033 | SPI.setClockDivider(SPI_CLOCK_DIV2); |
jmar7 | 0:e8167f37725c | 1034 | // Data is read and written MSb first. |
jmar7 | 0:e8167f37725c | 1035 | SPI.setBitOrder(MSBFIRST); |
jmar7 | 0:e8167f37725c | 1036 | // Data is captured on rising edge of clock (CPHA = 0) |
jmar7 | 0:e8167f37725c | 1037 | // Base value of the clock is HIGH (CPOL = 1) |
jmar7 | 0:e8167f37725c | 1038 | SPI.setDataMode(SPI_MODE0); |
jmar7 | 0:e8167f37725c | 1039 | */ |
jmar7 | 0:e8167f37725c | 1040 | } |
jmar7 | 0:e8167f37725c | 1041 | |
jmar7 | 0:e8167f37725c | 1042 | void LSM9DS1::SPIwriteByte(uint8_t csPin, uint8_t subAddress, uint8_t data) |
jmar7 | 0:e8167f37725c | 1043 | { |
jmar7 | 1:87d535bf8c53 | 1044 | /* |
jmar7 | 0:e8167f37725c | 1045 | digitalWrite(csPin, LOW); // Initiate communication |
4180_1 | 2:36abf8e18ade | 1046 | |
jmar7 | 0:e8167f37725c | 1047 | // If write, bit 0 (MSB) should be 0 |
jmar7 | 0:e8167f37725c | 1048 | // If single write, bit 1 should be 0 |
jmar7 | 0:e8167f37725c | 1049 | SPI.transfer(subAddress & 0x3F); // Send Address |
jmar7 | 0:e8167f37725c | 1050 | SPI.transfer(data); // Send data |
4180_1 | 2:36abf8e18ade | 1051 | |
jmar7 | 0:e8167f37725c | 1052 | digitalWrite(csPin, HIGH); // Close communication |
jmar7 | 0:e8167f37725c | 1053 | */ |
jmar7 | 0:e8167f37725c | 1054 | } |
jmar7 | 0:e8167f37725c | 1055 | |
jmar7 | 0:e8167f37725c | 1056 | uint8_t LSM9DS1::SPIreadByte(uint8_t csPin, uint8_t subAddress) |
jmar7 | 0:e8167f37725c | 1057 | { |
jmar7 | 0:e8167f37725c | 1058 | uint8_t temp; |
4180_1 | 2:36abf8e18ade | 1059 | // Use the multiple read function to read 1 byte. |
jmar7 | 0:e8167f37725c | 1060 | // Value is returned to `temp`. |
jmar7 | 0:e8167f37725c | 1061 | SPIreadBytes(csPin, subAddress, &temp, 1); |
jmar7 | 0:e8167f37725c | 1062 | return temp; |
jmar7 | 0:e8167f37725c | 1063 | } |
jmar7 | 0:e8167f37725c | 1064 | |
jmar7 | 0:e8167f37725c | 1065 | void LSM9DS1::SPIreadBytes(uint8_t csPin, uint8_t subAddress, |
4180_1 | 2:36abf8e18ade | 1066 | uint8_t * dest, uint8_t count) |
jmar7 | 0:e8167f37725c | 1067 | { |
jmar7 | 0:e8167f37725c | 1068 | // To indicate a read, set bit 0 (msb) of first byte to 1 |
jmar7 | 0:e8167f37725c | 1069 | uint8_t rAddress = 0x80 | (subAddress & 0x3F); |
4180_1 | 2:36abf8e18ade | 1070 | // Mag SPI port is different. If we're reading multiple bytes, |
jmar7 | 0:e8167f37725c | 1071 | // set bit 1 to 1. The remaining six bytes are the address to be read |
jmar7 | 0:e8167f37725c | 1072 | if ((csPin == _mAddress) && count > 1) |
jmar7 | 0:e8167f37725c | 1073 | rAddress |= 0x40; |
4180_1 | 2:36abf8e18ade | 1074 | |
4180_1 | 2:36abf8e18ade | 1075 | /* |
jmar7 | 0:e8167f37725c | 1076 | digitalWrite(csPin, LOW); // Initiate communication |
jmar7 | 0:e8167f37725c | 1077 | SPI.transfer(rAddress); |
jmar7 | 0:e8167f37725c | 1078 | for (int i=0; i<count; i++) |
jmar7 | 0:e8167f37725c | 1079 | { |
jmar7 | 0:e8167f37725c | 1080 | dest[i] = SPI.transfer(0x00); // Read into destination array |
jmar7 | 0:e8167f37725c | 1081 | } |
jmar7 | 0:e8167f37725c | 1082 | digitalWrite(csPin, HIGH); // Close communication |
jmar7 | 0:e8167f37725c | 1083 | */ |
jmar7 | 0:e8167f37725c | 1084 | } |
jmar7 | 0:e8167f37725c | 1085 | |
jmar7 | 0:e8167f37725c | 1086 | void LSM9DS1::initI2C() |
jmar7 | 0:e8167f37725c | 1087 | { |
4180_1 | 2:36abf8e18ade | 1088 | /* |
jmar7 | 0:e8167f37725c | 1089 | Wire.begin(); // Initialize I2C library |
jmar7 | 0:e8167f37725c | 1090 | */ |
4180_1 | 2:36abf8e18ade | 1091 | |
jmar7 | 0:e8167f37725c | 1092 | //already initialized in constructor! |
jmar7 | 0:e8167f37725c | 1093 | } |
jmar7 | 0:e8167f37725c | 1094 | |
jmar7 | 0:e8167f37725c | 1095 | // Wire.h read and write protocols |
jmar7 | 0:e8167f37725c | 1096 | void LSM9DS1::I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data) |
jmar7 | 0:e8167f37725c | 1097 | { |
4180_1 | 2:36abf8e18ade | 1098 | /* |
jmar7 | 0:e8167f37725c | 1099 | Wire.beginTransmission(address); // Initialize the Tx buffer |
jmar7 | 0:e8167f37725c | 1100 | Wire.write(subAddress); // Put slave register address in Tx buffer |
jmar7 | 0:e8167f37725c | 1101 | Wire.write(data); // Put data in Tx buffer |
jmar7 | 0:e8167f37725c | 1102 | Wire.endTransmission(); // Send the Tx buffer |
jmar7 | 0:e8167f37725c | 1103 | */ |
jmar7 | 0:e8167f37725c | 1104 | char temp_data[2] = {subAddress, data}; |
jmar7 | 0:e8167f37725c | 1105 | i2c.write(address, temp_data, 2); |
jmar7 | 0:e8167f37725c | 1106 | } |
jmar7 | 0:e8167f37725c | 1107 | |
jmar7 | 0:e8167f37725c | 1108 | uint8_t LSM9DS1::I2CreadByte(uint8_t address, uint8_t subAddress) |
jmar7 | 0:e8167f37725c | 1109 | { |
4180_1 | 2:36abf8e18ade | 1110 | /* |
jmar7 | 0:e8167f37725c | 1111 | int timeout = LSM9DS1_COMMUNICATION_TIMEOUT; |
4180_1 | 2:36abf8e18ade | 1112 | uint8_t data; // `data` will store the register data |
4180_1 | 2:36abf8e18ade | 1113 | |
jmar7 | 0:e8167f37725c | 1114 | Wire.beginTransmission(address); // Initialize the Tx buffer |
jmar7 | 0:e8167f37725c | 1115 | Wire.write(subAddress); // Put slave register address in Tx buffer |
jmar7 | 0:e8167f37725c | 1116 | Wire.endTransmission(true); // Send the Tx buffer, but send a restart to keep connection alive |
4180_1 | 2:36abf8e18ade | 1117 | Wire.requestFrom(address, (uint8_t) 1); // Read one byte from slave register address |
jmar7 | 0:e8167f37725c | 1118 | while ((Wire.available() < 1) && (timeout-- > 0)) |
jmar7 | 0:e8167f37725c | 1119 | delay(1); |
4180_1 | 2:36abf8e18ade | 1120 | |
jmar7 | 0:e8167f37725c | 1121 | if (timeout <= 0) |
jmar7 | 0:e8167f37725c | 1122 | return 255; //! Bad! 255 will be misinterpreted as a good value. |
4180_1 | 2:36abf8e18ade | 1123 | |
jmar7 | 0:e8167f37725c | 1124 | data = Wire.read(); // Fill Rx buffer with result |
jmar7 | 0:e8167f37725c | 1125 | return data; // Return data read from slave register |
jmar7 | 0:e8167f37725c | 1126 | */ |
jmar7 | 0:e8167f37725c | 1127 | char data; |
4180_1 | 2:36abf8e18ade | 1128 | char temp[2] = {subAddress}; |
4180_1 | 2:36abf8e18ade | 1129 | |
jmar7 | 0:e8167f37725c | 1130 | i2c.write(address, temp, 1); |
jmar7 | 0:e8167f37725c | 1131 | //i2c.write(address & 0xFE); |
jmar7 | 0:e8167f37725c | 1132 | temp[1] = 0x00; |
jmar7 | 0:e8167f37725c | 1133 | i2c.write(address, temp, 1); |
jmar7 | 0:e8167f37725c | 1134 | //i2c.write( address | 0x01); |
jmar7 | 0:e8167f37725c | 1135 | int a = i2c.read(address, &data, 1); |
jmar7 | 0:e8167f37725c | 1136 | return data; |
jmar7 | 0:e8167f37725c | 1137 | } |
jmar7 | 0:e8167f37725c | 1138 | |
jmar7 | 0:e8167f37725c | 1139 | uint8_t LSM9DS1::I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count) |
4180_1 | 2:36abf8e18ade | 1140 | { |
4180_1 | 2:36abf8e18ade | 1141 | /* |
jmar7 | 0:e8167f37725c | 1142 | int timeout = LSM9DS1_COMMUNICATION_TIMEOUT; |
jmar7 | 0:e8167f37725c | 1143 | Wire.beginTransmission(address); // Initialize the Tx buffer |
jmar7 | 0:e8167f37725c | 1144 | // Next send the register to be read. OR with 0x80 to indicate multi-read. |
jmar7 | 0:e8167f37725c | 1145 | Wire.write(subAddress | 0x80); // Put slave register address in Tx buffer |
jmar7 | 0:e8167f37725c | 1146 | |
jmar7 | 0:e8167f37725c | 1147 | Wire.endTransmission(true); // Send the Tx buffer, but send a restart to keep connection alive |
jmar7 | 0:e8167f37725c | 1148 | uint8_t i = 0; |
4180_1 | 2:36abf8e18ade | 1149 | Wire.requestFrom(address, count); // Read bytes from slave register address |
jmar7 | 0:e8167f37725c | 1150 | while ((Wire.available() < count) && (timeout-- > 0)) |
jmar7 | 0:e8167f37725c | 1151 | delay(1); |
jmar7 | 0:e8167f37725c | 1152 | if (timeout <= 0) |
jmar7 | 0:e8167f37725c | 1153 | return -1; |
4180_1 | 2:36abf8e18ade | 1154 | |
jmar7 | 0:e8167f37725c | 1155 | for (int i=0; i<count;) |
jmar7 | 0:e8167f37725c | 1156 | { |
jmar7 | 0:e8167f37725c | 1157 | if (Wire.available()) |
jmar7 | 0:e8167f37725c | 1158 | { |
jmar7 | 0:e8167f37725c | 1159 | dest[i++] = Wire.read(); |
jmar7 | 0:e8167f37725c | 1160 | } |
jmar7 | 0:e8167f37725c | 1161 | } |
jmar7 | 0:e8167f37725c | 1162 | return count; |
jmar7 | 0:e8167f37725c | 1163 | */ |
jmar7 | 0:e8167f37725c | 1164 | int i; |
jmar7 | 0:e8167f37725c | 1165 | char temp_dest[count]; |
jmar7 | 0:e8167f37725c | 1166 | char temp[1] = {subAddress}; |
jmar7 | 0:e8167f37725c | 1167 | i2c.write(address, temp, 1); |
jmar7 | 0:e8167f37725c | 1168 | i2c.read(address, temp_dest, count); |
4180_1 | 2:36abf8e18ade | 1169 | |
jmar7 | 0:e8167f37725c | 1170 | //i2c doesn't take uint8_ts, but rather chars so do this nasty af conversion |
jmar7 | 0:e8167f37725c | 1171 | for (i=0; i < count; i++) { |
4180_1 | 2:36abf8e18ade | 1172 | dest[i] = temp_dest[i]; |
jmar7 | 0:e8167f37725c | 1173 | } |
jmar7 | 0:e8167f37725c | 1174 | return count; |
jmar7 | 0:e8167f37725c | 1175 | } |