debug

Dependencies:   mbed mbed-rtos PinDetect X_NUCLEO_53L0A1

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

Who changed what in which revision?

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