4180 Lab 2

Dependencies:   mbed wave_player Servo 4DGL-uLCD-SE Motor SDFileSystem LSM9DS1_Library_cal PinDetect X_NUCLEO_53L0A1

Committer:
emilywilson
Date:
Mon Feb 03 13:22:28 2020 +0000
Revision:
1:6d8f645530b8
Parent:
0:90df82d4732d
parts 6-14 and extra credit

Who changed what in which revision?

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