self-balancing-robot

Dependencies:   mbed mbed-rtos Motor

Committer:
pandirimukund
Date:
Mon Apr 27 16:57:40 2020 +0000
Revision:
41:b9c8d527dd2b
Parent:
13:8d8ac3189984
added comments and cleaned up code

Who changed what in which revision?

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