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