AHRS

Dependencies:   Eigen

Dependents:   IndNav_QK3_T265

Committer:
pmic
Date:
Wed Sep 11 08:06:12 2019 +0000
Revision:
13:3a41ef85fe7e
Parent:
12:1070a10a4624
Child:
14:c357be9a3fc8
Set accel AAF to 50 Hz, enable gyro LPF2 and set it to 57 Hz.

Who changed what in which revision?

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