lib imu

Dependents:   LoRaWan_GPS

Committer:
RoddyRod
Date:
Fri Dec 27 16:01:32 2019 +0000
Revision:
0:4b0b0a4b20e3
imu

Who changed what in which revision?

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