IMU LSM9DS1 Library

Dependencies:   PinDetect mbed

Dependents:   Latch9DOF_LSM9DS1

Fork of LSM9DS1_Library by Jason Mar

Committer:
afmiee
Date:
Tue Jul 26 21:16:58 2016 +0000
Revision:
5:58710c38076c
Parent:
4:e1404cbaf2a9
Made corrections to library.

Who changed what in which revision?

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