altb_pmic / Mbed 2 deprecated Test_optical_flow_PX4

Dependencies:   mbed

Committer:
pmic
Date:
Thu Aug 22 14:22:58 2019 +0000
Revision:
3:e03714326b83
Gyro parameterization in Flow and evaluation/comparison with pesboard gyro.

Who changed what in which revision?

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