Judah Okeleye / Mbed 2 deprecated SLVM

Dependencies:   mbed-rtos mbed

Committer:
jerziboi732
Date:
Wed Dec 03 22:33:03 2014 +0000
Revision:
0:354a8831107d
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jerziboi732 0:354a8831107d 1 #include "LSM9DS0.h"
jerziboi732 0:354a8831107d 2
jerziboi732 0:354a8831107d 3 LSM9DS0::LSM9DS0(PinName sda, PinName scl, uint8_t gAddr, uint8_t xmAddr)
jerziboi732 0:354a8831107d 4 {
jerziboi732 0:354a8831107d 5 // xmAddress and gAddress will store the 7-bit I2C address, if using I2C.
jerziboi732 0:354a8831107d 6 xmAddress = xmAddr;
jerziboi732 0:354a8831107d 7 gAddress = gAddr;
jerziboi732 0:354a8831107d 8
jerziboi732 0:354a8831107d 9 i2c_ = new I2Cdev(sda, scl);
jerziboi732 0:354a8831107d 10 }
jerziboi732 0:354a8831107d 11
jerziboi732 0:354a8831107d 12 uint16_t LSM9DS0::begin(gyro_scale gScl, accel_scale aScl, mag_scale mScl,
jerziboi732 0:354a8831107d 13 gyro_odr gODR, accel_odr aODR, mag_odr mODR)
jerziboi732 0:354a8831107d 14 {
jerziboi732 0:354a8831107d 15 // Store the given scales in class variables. These scale variables
jerziboi732 0:354a8831107d 16 // are used throughout to calculate the actual g's, DPS,and Gs's.
jerziboi732 0:354a8831107d 17 gScale = gScl;
jerziboi732 0:354a8831107d 18 aScale = aScl;
jerziboi732 0:354a8831107d 19 mScale = mScl;
jerziboi732 0:354a8831107d 20
jerziboi732 0:354a8831107d 21 // Once we have the scale values, we can calculate the resolution
jerziboi732 0:354a8831107d 22 // of each sensor. That's what these functions are for. One for each sensor
jerziboi732 0:354a8831107d 23 calcgRes(); // Calculate DPS / ADC tick, stored in gRes variable
jerziboi732 0:354a8831107d 24 calcmRes(); // Calculate Gs / ADC tick, stored in mRes variable
jerziboi732 0:354a8831107d 25 calcaRes(); // Calculate g / ADC tick, stored in aRes variable
jerziboi732 0:354a8831107d 26
jerziboi732 0:354a8831107d 27
jerziboi732 0:354a8831107d 28 // To verify communication, we can read from the WHO_AM_I register of
jerziboi732 0:354a8831107d 29 // each device. Store those in a variable so we can return them.
jerziboi732 0:354a8831107d 30 uint8_t gTest = gReadByte(WHO_AM_I_G); // Read the gyro WHO_AM_I
jerziboi732 0:354a8831107d 31 uint8_t xmTest = xmReadByte(WHO_AM_I_XM); // Read the accel/mag WHO_AM_I
jerziboi732 0:354a8831107d 32
jerziboi732 0:354a8831107d 33 // Gyro initialization stuff:
jerziboi732 0:354a8831107d 34 initGyro(); // This will "turn on" the gyro. Setting up interrupts, etc.
jerziboi732 0:354a8831107d 35 setGyroODR(gODR); // Set the gyro output data rate and bandwidth.
jerziboi732 0:354a8831107d 36 setGyroScale(gScale); // Set the gyro range
jerziboi732 0:354a8831107d 37
jerziboi732 0:354a8831107d 38 // Accelerometer initialization stuff:
jerziboi732 0:354a8831107d 39 initAccel(); // "Turn on" all axes of the accel. Set up interrupts, etc.
jerziboi732 0:354a8831107d 40 setAccelODR(aODR); // Set the accel data rate.
jerziboi732 0:354a8831107d 41 setAccelScale(aScale); // Set the accel range.
jerziboi732 0:354a8831107d 42
jerziboi732 0:354a8831107d 43 // Magnetometer initialization stuff:
jerziboi732 0:354a8831107d 44 initMag(); // "Turn on" all axes of the mag. Set up interrupts, etc.
jerziboi732 0:354a8831107d 45 setMagODR(mODR); // Set the magnetometer output data rate.
jerziboi732 0:354a8831107d 46 setMagScale(mScale); // Set the magnetometer's range.
jerziboi732 0:354a8831107d 47
jerziboi732 0:354a8831107d 48 // Once everything is initialized, return the WHO_AM_I registers we read:
jerziboi732 0:354a8831107d 49 return (xmTest << 8) | gTest;
jerziboi732 0:354a8831107d 50 }
jerziboi732 0:354a8831107d 51
jerziboi732 0:354a8831107d 52 void LSM9DS0::initGyro()
jerziboi732 0:354a8831107d 53 {
jerziboi732 0:354a8831107d 54
jerziboi732 0:354a8831107d 55 gWriteByte(CTRL_REG1_G, 0x0F); // Normal mode, enable all axes
jerziboi732 0:354a8831107d 56 gWriteByte(CTRL_REG2_G, 0x00); // Normal mode, high cutoff frequency
jerziboi732 0:354a8831107d 57 gWriteByte(CTRL_REG3_G, 0x88); //Interrupt enabled on both INT_G and I2_DRDY
jerziboi732 0:354a8831107d 58 gWriteByte(CTRL_REG4_G, 0x00); // Set scale to 245 dps
jerziboi732 0:354a8831107d 59 gWriteByte(CTRL_REG5_G, 0x00); //Init default values
jerziboi732 0:354a8831107d 60
jerziboi732 0:354a8831107d 61 }
jerziboi732 0:354a8831107d 62
jerziboi732 0:354a8831107d 63 void LSM9DS0::initAccel()
jerziboi732 0:354a8831107d 64 {
jerziboi732 0:354a8831107d 65 xmWriteByte(CTRL_REG0_XM, 0x00);
jerziboi732 0:354a8831107d 66 xmWriteByte(CTRL_REG1_XM, 0x57); // 50Hz data rate, x/y/z all enabled
jerziboi732 0:354a8831107d 67 xmWriteByte(CTRL_REG2_XM, 0x00); // Set scale to 2g
jerziboi732 0:354a8831107d 68 xmWriteByte(CTRL_REG3_XM, 0x04); // Accelerometer data ready on INT1_XM (0x04)
jerziboi732 0:354a8831107d 69
jerziboi732 0:354a8831107d 70 }
jerziboi732 0:354a8831107d 71
jerziboi732 0:354a8831107d 72 void LSM9DS0::initMag()
jerziboi732 0:354a8831107d 73 {
jerziboi732 0:354a8831107d 74 xmWriteByte(CTRL_REG5_XM, 0x94); // Mag data rate - 100 Hz, enable temperature sensor
jerziboi732 0:354a8831107d 75 xmWriteByte(CTRL_REG6_XM, 0x00); // Mag scale to +/- 2GS
jerziboi732 0:354a8831107d 76 xmWriteByte(CTRL_REG7_XM, 0x00); // Continuous conversion mode
jerziboi732 0:354a8831107d 77 xmWriteByte(CTRL_REG4_XM, 0x04); // Magnetometer data ready on INT2_XM (0x08)
jerziboi732 0:354a8831107d 78 xmWriteByte(INT_CTRL_REG_M, 0x09); // Enable interrupts for mag, active-low, push-pull
jerziboi732 0:354a8831107d 79 }
jerziboi732 0:354a8831107d 80
jerziboi732 0:354a8831107d 81 void LSM9DS0::calLSM9DS0(float * gbias, float * abias)
jerziboi732 0:354a8831107d 82 {
jerziboi732 0:354a8831107d 83 uint8_t data[6] = {0, 0, 0, 0, 0, 0};
jerziboi732 0:354a8831107d 84 int16_t gyro_bias[3] = {0, 0, 0}, accel_bias[3] = {0, 0, 0};
jerziboi732 0:354a8831107d 85 int samples, ii;
jerziboi732 0:354a8831107d 86
jerziboi732 0:354a8831107d 87 // First get gyro bias
jerziboi732 0:354a8831107d 88 uint8_t c = gReadByte(CTRL_REG5_G);
jerziboi732 0:354a8831107d 89 gWriteByte(CTRL_REG5_G, c | 0x40); // Enable gyro FIFO
jerziboi732 0:354a8831107d 90 wait_ms(20); // Wait for change to take effect
jerziboi732 0:354a8831107d 91 gWriteByte(FIFO_CTRL_REG_G, 0x20 | 0x1F); // Enable gyro FIFO stream mode and set watermark at 32 samples
jerziboi732 0:354a8831107d 92 wait_ms(1000); // delay 1000 milliseconds to collect FIFO samples
jerziboi732 0:354a8831107d 93
jerziboi732 0:354a8831107d 94 samples = (gReadByte(FIFO_SRC_REG_G) & 0x1F); // Read number of stored samples
jerziboi732 0:354a8831107d 95
jerziboi732 0:354a8831107d 96 for(ii = 0; ii < samples ; ii++) { // Read the gyro data stored in the FIFO
jerziboi732 0:354a8831107d 97
jerziboi732 0:354a8831107d 98 data[0] = gReadByte(OUT_X_L_G);
jerziboi732 0:354a8831107d 99 data[1] = gReadByte(OUT_X_H_G);
jerziboi732 0:354a8831107d 100 data[2] = gReadByte(OUT_Y_L_G);
jerziboi732 0:354a8831107d 101 data[3] = gReadByte(OUT_Y_H_G);
jerziboi732 0:354a8831107d 102 data[4] = gReadByte(OUT_Z_L_G);
jerziboi732 0:354a8831107d 103 data[5] = gReadByte(OUT_Z_H_G);
jerziboi732 0:354a8831107d 104
jerziboi732 0:354a8831107d 105 gyro_bias[0] += (((int16_t)data[1] << 8) | data[0]);
jerziboi732 0:354a8831107d 106 gyro_bias[1] += (((int16_t)data[3] << 8) | data[2]);
jerziboi732 0:354a8831107d 107 gyro_bias[2] += (((int16_t)data[5] << 8) | data[4]);
jerziboi732 0:354a8831107d 108 }
jerziboi732 0:354a8831107d 109
jerziboi732 0:354a8831107d 110 gyro_bias[0] /= samples; // average the data
jerziboi732 0:354a8831107d 111 gyro_bias[1] /= samples;
jerziboi732 0:354a8831107d 112 gyro_bias[2] /= samples;
jerziboi732 0:354a8831107d 113
jerziboi732 0:354a8831107d 114 gbias[0] = (float)gyro_bias[0]*gRes; // Properly scale the data to get deg/s
jerziboi732 0:354a8831107d 115 gbias[1] = (float)gyro_bias[1]*gRes;
jerziboi732 0:354a8831107d 116 gbias[2] = (float)gyro_bias[2]*gRes;
jerziboi732 0:354a8831107d 117
jerziboi732 0:354a8831107d 118 c = gReadByte(CTRL_REG5_G);
jerziboi732 0:354a8831107d 119 gWriteByte(CTRL_REG5_G, c & ~0x40); // Disable gyro FIFO
jerziboi732 0:354a8831107d 120 wait_ms(20);
jerziboi732 0:354a8831107d 121 gWriteByte(FIFO_CTRL_REG_G, 0x00); // Enable gyro bypass mode
jerziboi732 0:354a8831107d 122
jerziboi732 0:354a8831107d 123 // Now get the accelerometer biases
jerziboi732 0:354a8831107d 124 c = xmReadByte(CTRL_REG0_XM);
jerziboi732 0:354a8831107d 125 xmWriteByte(CTRL_REG0_XM, c | 0x40); // Enable accelerometer FIFO
jerziboi732 0:354a8831107d 126 wait_ms(20); // Wait for change to take effect
jerziboi732 0:354a8831107d 127 xmWriteByte(FIFO_CTRL_REG, 0x20 | 0x1F); // Enable accelerometer FIFO stream mode and set watermark at 32 samples
jerziboi732 0:354a8831107d 128 wait_ms(1000); // delay 1000 milliseconds to collect FIFO samples
jerziboi732 0:354a8831107d 129
jerziboi732 0:354a8831107d 130 samples = (xmReadByte(FIFO_SRC_REG) & 0x1F); // Read number of stored accelerometer samples
jerziboi732 0:354a8831107d 131
jerziboi732 0:354a8831107d 132 for(ii = 0; ii < samples ; ii++) { // Read the accelerometer data stored in the FIFO
jerziboi732 0:354a8831107d 133
jerziboi732 0:354a8831107d 134 data[0] = xmReadByte(OUT_X_L_A);
jerziboi732 0:354a8831107d 135 data[1] = xmReadByte(OUT_X_H_A);
jerziboi732 0:354a8831107d 136 data[2] = xmReadByte(OUT_Y_L_A);
jerziboi732 0:354a8831107d 137 data[3] = xmReadByte(OUT_Y_H_A);
jerziboi732 0:354a8831107d 138 data[4] = xmReadByte(OUT_Z_L_A);
jerziboi732 0:354a8831107d 139 data[5] = xmReadByte(OUT_Z_H_A);
jerziboi732 0:354a8831107d 140 accel_bias[0] += (((int16_t)data[1] << 8) | data[0]);
jerziboi732 0:354a8831107d 141 accel_bias[1] += (((int16_t)data[3] << 8) | data[2]);
jerziboi732 0:354a8831107d 142 accel_bias[2] += (((int16_t)data[5] << 8) | data[4]) - (int16_t)(1./aRes); // Assumes sensor facing up!
jerziboi732 0:354a8831107d 143 }
jerziboi732 0:354a8831107d 144
jerziboi732 0:354a8831107d 145 accel_bias[0] /= samples; // average the data
jerziboi732 0:354a8831107d 146 accel_bias[1] /= samples;
jerziboi732 0:354a8831107d 147 accel_bias[2] /= samples;
jerziboi732 0:354a8831107d 148
jerziboi732 0:354a8831107d 149 abias[0] = (float)accel_bias[0]*aRes; // Properly scale data to get gs
jerziboi732 0:354a8831107d 150 abias[1] = (float)accel_bias[1]*aRes;
jerziboi732 0:354a8831107d 151 abias[2] = (float)accel_bias[2]*aRes;
jerziboi732 0:354a8831107d 152
jerziboi732 0:354a8831107d 153 c = xmReadByte(CTRL_REG0_XM);
jerziboi732 0:354a8831107d 154 xmWriteByte(CTRL_REG0_XM, c & ~0x40); // Disable accelerometer FIFO
jerziboi732 0:354a8831107d 155 wait_ms(20);
jerziboi732 0:354a8831107d 156 xmWriteByte(FIFO_CTRL_REG, 0x00); // Enable accelerometer bypass mode
jerziboi732 0:354a8831107d 157
jerziboi732 0:354a8831107d 158 }
jerziboi732 0:354a8831107d 159 void LSM9DS0::readAccel()
jerziboi732 0:354a8831107d 160 {
jerziboi732 0:354a8831107d 161 uint16_t Temp = 0;
jerziboi732 0:354a8831107d 162
jerziboi732 0:354a8831107d 163 //Get x
jerziboi732 0:354a8831107d 164 Temp = xmReadByte(OUT_X_H_A);
jerziboi732 0:354a8831107d 165 Temp = Temp<<8;
jerziboi732 0:354a8831107d 166 Temp |= xmReadByte(OUT_X_L_A);
jerziboi732 0:354a8831107d 167 ax = Temp;
jerziboi732 0:354a8831107d 168
jerziboi732 0:354a8831107d 169
jerziboi732 0:354a8831107d 170 //Get y
jerziboi732 0:354a8831107d 171 Temp=0;
jerziboi732 0:354a8831107d 172 Temp = xmReadByte(OUT_Y_H_A);
jerziboi732 0:354a8831107d 173 Temp = Temp<<8;
jerziboi732 0:354a8831107d 174 Temp |= xmReadByte(OUT_Y_L_A);
jerziboi732 0:354a8831107d 175 ay = Temp;
jerziboi732 0:354a8831107d 176
jerziboi732 0:354a8831107d 177 //Get z
jerziboi732 0:354a8831107d 178 Temp=0;
jerziboi732 0:354a8831107d 179 Temp = xmReadByte(OUT_Z_H_A);
jerziboi732 0:354a8831107d 180 Temp = Temp<<8;
jerziboi732 0:354a8831107d 181 Temp |= xmReadByte(OUT_Z_L_A);
jerziboi732 0:354a8831107d 182 az = Temp;
jerziboi732 0:354a8831107d 183
jerziboi732 0:354a8831107d 184 }
jerziboi732 0:354a8831107d 185
jerziboi732 0:354a8831107d 186 void LSM9DS0::readMag()
jerziboi732 0:354a8831107d 187 {
jerziboi732 0:354a8831107d 188 uint16_t Temp = 0;
jerziboi732 0:354a8831107d 189
jerziboi732 0:354a8831107d 190 //Get x
jerziboi732 0:354a8831107d 191 Temp = xmReadByte(OUT_X_H_M);
jerziboi732 0:354a8831107d 192 Temp = Temp<<8;
jerziboi732 0:354a8831107d 193 Temp |= xmReadByte(OUT_X_L_M);
jerziboi732 0:354a8831107d 194 mx = Temp;
jerziboi732 0:354a8831107d 195
jerziboi732 0:354a8831107d 196
jerziboi732 0:354a8831107d 197 //Get y
jerziboi732 0:354a8831107d 198 Temp=0;
jerziboi732 0:354a8831107d 199 Temp = xmReadByte(OUT_Y_H_M);
jerziboi732 0:354a8831107d 200 Temp = Temp<<8;
jerziboi732 0:354a8831107d 201 Temp |= xmReadByte(OUT_Y_L_M);
jerziboi732 0:354a8831107d 202 my = Temp;
jerziboi732 0:354a8831107d 203
jerziboi732 0:354a8831107d 204 //Get z
jerziboi732 0:354a8831107d 205 Temp=0;
jerziboi732 0:354a8831107d 206 Temp = xmReadByte(OUT_Z_H_M);
jerziboi732 0:354a8831107d 207 Temp = Temp<<8;
jerziboi732 0:354a8831107d 208 Temp |= xmReadByte(OUT_Z_L_M);
jerziboi732 0:354a8831107d 209 mz = Temp;
jerziboi732 0:354a8831107d 210 }
jerziboi732 0:354a8831107d 211
jerziboi732 0:354a8831107d 212 void LSM9DS0::readTemp()
jerziboi732 0:354a8831107d 213 {
jerziboi732 0:354a8831107d 214 uint8_t temp[2]; // We'll read two bytes from the temperature sensor into temp
jerziboi732 0:354a8831107d 215
jerziboi732 0:354a8831107d 216 temp[0] = xmReadByte(OUT_TEMP_L_XM);
jerziboi732 0:354a8831107d 217 temp[1] = xmReadByte(OUT_TEMP_H_XM);
jerziboi732 0:354a8831107d 218
jerziboi732 0:354a8831107d 219 temperature = (((int16_t) temp[1] << 12) | temp[0] << 4 ) >> 4; // Temperature is a 12-bit signed integer
jerziboi732 0:354a8831107d 220 }
jerziboi732 0:354a8831107d 221
jerziboi732 0:354a8831107d 222
jerziboi732 0:354a8831107d 223 void LSM9DS0::readGyro()
jerziboi732 0:354a8831107d 224 {
jerziboi732 0:354a8831107d 225 uint16_t Temp = 0;
jerziboi732 0:354a8831107d 226
jerziboi732 0:354a8831107d 227 //Get x
jerziboi732 0:354a8831107d 228 Temp = gReadByte(OUT_X_H_G);
jerziboi732 0:354a8831107d 229 Temp = Temp<<8;
jerziboi732 0:354a8831107d 230 Temp |= gReadByte(OUT_X_L_G);
jerziboi732 0:354a8831107d 231 gx = Temp;
jerziboi732 0:354a8831107d 232
jerziboi732 0:354a8831107d 233
jerziboi732 0:354a8831107d 234 //Get y
jerziboi732 0:354a8831107d 235 Temp=0;
jerziboi732 0:354a8831107d 236 Temp = gReadByte(OUT_Y_H_G);
jerziboi732 0:354a8831107d 237 Temp = Temp<<8;
jerziboi732 0:354a8831107d 238 Temp |= gReadByte(OUT_Y_L_G);
jerziboi732 0:354a8831107d 239 gy = Temp;
jerziboi732 0:354a8831107d 240
jerziboi732 0:354a8831107d 241 //Get z
jerziboi732 0:354a8831107d 242 Temp=0;
jerziboi732 0:354a8831107d 243 Temp = gReadByte(OUT_Z_H_G);
jerziboi732 0:354a8831107d 244 Temp = Temp<<8;
jerziboi732 0:354a8831107d 245 Temp |= gReadByte(OUT_Z_L_G);
jerziboi732 0:354a8831107d 246 gz = Temp;
jerziboi732 0:354a8831107d 247 }
jerziboi732 0:354a8831107d 248
jerziboi732 0:354a8831107d 249 float LSM9DS0::calcGyro(int16_t gyro)
jerziboi732 0:354a8831107d 250 {
jerziboi732 0:354a8831107d 251 // Return the gyro raw reading times our pre-calculated DPS / (ADC tick):
jerziboi732 0:354a8831107d 252 return gRes * gyro;
jerziboi732 0:354a8831107d 253 }
jerziboi732 0:354a8831107d 254
jerziboi732 0:354a8831107d 255 float LSM9DS0::calcAccel(int16_t accel)
jerziboi732 0:354a8831107d 256 {
jerziboi732 0:354a8831107d 257 // Return the accel raw reading times our pre-calculated g's / (ADC tick):
jerziboi732 0:354a8831107d 258 return aRes * accel;
jerziboi732 0:354a8831107d 259 }
jerziboi732 0:354a8831107d 260
jerziboi732 0:354a8831107d 261 float LSM9DS0::calcMag(int16_t mag)
jerziboi732 0:354a8831107d 262 {
jerziboi732 0:354a8831107d 263 // Return the mag raw reading times our pre-calculated Gs / (ADC tick):
jerziboi732 0:354a8831107d 264 return mRes * mag;
jerziboi732 0:354a8831107d 265 }
jerziboi732 0:354a8831107d 266
jerziboi732 0:354a8831107d 267 void LSM9DS0::setGyroScale(gyro_scale gScl)
jerziboi732 0:354a8831107d 268 {
jerziboi732 0:354a8831107d 269 // We need to preserve the other bytes in CTRL_REG4_G. So, first read it:
jerziboi732 0:354a8831107d 270 uint8_t temp = gReadByte(CTRL_REG4_G);
jerziboi732 0:354a8831107d 271 // Then mask out the gyro scale bits:
jerziboi732 0:354a8831107d 272 temp &= 0xFF^(0x3 << 4);
jerziboi732 0:354a8831107d 273 // Then shift in our new scale bits:
jerziboi732 0:354a8831107d 274 temp |= gScl << 4;
jerziboi732 0:354a8831107d 275 // And write the new register value back into CTRL_REG4_G:
jerziboi732 0:354a8831107d 276 gWriteByte(CTRL_REG4_G, temp);
jerziboi732 0:354a8831107d 277
jerziboi732 0:354a8831107d 278 // We've updated the sensor, but we also need to update our class variables
jerziboi732 0:354a8831107d 279 // First update gScale:
jerziboi732 0:354a8831107d 280 gScale = gScl;
jerziboi732 0:354a8831107d 281 // Then calculate a new gRes, which relies on gScale being set correctly:
jerziboi732 0:354a8831107d 282 calcgRes();
jerziboi732 0:354a8831107d 283 }
jerziboi732 0:354a8831107d 284
jerziboi732 0:354a8831107d 285 void LSM9DS0::setAccelScale(accel_scale aScl)
jerziboi732 0:354a8831107d 286 {
jerziboi732 0:354a8831107d 287 // We need to preserve the other bytes in CTRL_REG2_XM. So, first read it:
jerziboi732 0:354a8831107d 288 uint8_t temp = xmReadByte(CTRL_REG2_XM);
jerziboi732 0:354a8831107d 289 // Then mask out the accel scale bits:
jerziboi732 0:354a8831107d 290 temp &= 0xFF^(0x3 << 3);
jerziboi732 0:354a8831107d 291 // Then shift in our new scale bits:
jerziboi732 0:354a8831107d 292 temp |= aScl << 3;
jerziboi732 0:354a8831107d 293 // And write the new register value back into CTRL_REG2_XM:
jerziboi732 0:354a8831107d 294 xmWriteByte(CTRL_REG2_XM, temp);
jerziboi732 0:354a8831107d 295
jerziboi732 0:354a8831107d 296 // We've updated the sensor, but we also need to update our class variables
jerziboi732 0:354a8831107d 297 // First update aScale:
jerziboi732 0:354a8831107d 298 aScale = aScl;
jerziboi732 0:354a8831107d 299 // Then calculate a new aRes, which relies on aScale being set correctly:
jerziboi732 0:354a8831107d 300 calcaRes();
jerziboi732 0:354a8831107d 301 }
jerziboi732 0:354a8831107d 302
jerziboi732 0:354a8831107d 303 void LSM9DS0::setMagScale(mag_scale mScl)
jerziboi732 0:354a8831107d 304 {
jerziboi732 0:354a8831107d 305 // We need to preserve the other bytes in CTRL_REG6_XM. So, first read it:
jerziboi732 0:354a8831107d 306 uint8_t temp = xmReadByte(CTRL_REG6_XM);
jerziboi732 0:354a8831107d 307 // Then mask out the mag scale bits:
jerziboi732 0:354a8831107d 308 temp &= 0xFF^(0x3 << 5);
jerziboi732 0:354a8831107d 309 // Then shift in our new scale bits:
jerziboi732 0:354a8831107d 310 temp |= mScl << 5;
jerziboi732 0:354a8831107d 311 // And write the new register value back into CTRL_REG6_XM:
jerziboi732 0:354a8831107d 312 xmWriteByte(CTRL_REG6_XM, temp);
jerziboi732 0:354a8831107d 313
jerziboi732 0:354a8831107d 314 // We've updated the sensor, but we also need to update our class variables
jerziboi732 0:354a8831107d 315 // First update mScale:
jerziboi732 0:354a8831107d 316 mScale = mScl;
jerziboi732 0:354a8831107d 317 // Then calculate a new mRes, which relies on mScale being set correctly:
jerziboi732 0:354a8831107d 318 calcmRes();
jerziboi732 0:354a8831107d 319 }
jerziboi732 0:354a8831107d 320
jerziboi732 0:354a8831107d 321 void LSM9DS0::setGyroODR(gyro_odr gRate)
jerziboi732 0:354a8831107d 322 {
jerziboi732 0:354a8831107d 323 // We need to preserve the other bytes in CTRL_REG1_G. So, first read it:
jerziboi732 0:354a8831107d 324 uint8_t temp = gReadByte(CTRL_REG1_G);
jerziboi732 0:354a8831107d 325 // Then mask out the gyro ODR bits:
jerziboi732 0:354a8831107d 326 temp &= 0xFF^(0xF << 4);
jerziboi732 0:354a8831107d 327 // Then shift in our new ODR bits:
jerziboi732 0:354a8831107d 328 temp |= (gRate << 4);
jerziboi732 0:354a8831107d 329 // And write the new register value back into CTRL_REG1_G:
jerziboi732 0:354a8831107d 330 gWriteByte(CTRL_REG1_G, temp);
jerziboi732 0:354a8831107d 331 }
jerziboi732 0:354a8831107d 332 void LSM9DS0::setAccelODR(accel_odr aRate)
jerziboi732 0:354a8831107d 333 {
jerziboi732 0:354a8831107d 334 // We need to preserve the other bytes in CTRL_REG1_XM. So, first read it:
jerziboi732 0:354a8831107d 335 uint8_t temp = xmReadByte(CTRL_REG1_XM);
jerziboi732 0:354a8831107d 336 // Then mask out the accel ODR bits:
jerziboi732 0:354a8831107d 337 temp &= 0xFF^(0xF << 4);
jerziboi732 0:354a8831107d 338 // Then shift in our new ODR bits:
jerziboi732 0:354a8831107d 339 temp |= (aRate << 4);
jerziboi732 0:354a8831107d 340 // And write the new register value back into CTRL_REG1_XM:
jerziboi732 0:354a8831107d 341 xmWriteByte(CTRL_REG1_XM, temp);
jerziboi732 0:354a8831107d 342 }
jerziboi732 0:354a8831107d 343 void LSM9DS0::setMagODR(mag_odr mRate)
jerziboi732 0:354a8831107d 344 {
jerziboi732 0:354a8831107d 345 // We need to preserve the other bytes in CTRL_REG5_XM. So, first read it:
jerziboi732 0:354a8831107d 346 uint8_t temp = xmReadByte(CTRL_REG5_XM);
jerziboi732 0:354a8831107d 347 // Then mask out the mag ODR bits:
jerziboi732 0:354a8831107d 348 temp &= 0xFF^(0x7 << 2);
jerziboi732 0:354a8831107d 349 // Then shift in our new ODR bits:
jerziboi732 0:354a8831107d 350 temp |= (mRate << 2);
jerziboi732 0:354a8831107d 351 // And write the new register value back into CTRL_REG5_XM:
jerziboi732 0:354a8831107d 352 xmWriteByte(CTRL_REG5_XM, temp);
jerziboi732 0:354a8831107d 353 }
jerziboi732 0:354a8831107d 354
jerziboi732 0:354a8831107d 355 void LSM9DS0::configGyroInt(uint8_t int1Cfg, uint16_t int1ThsX, uint16_t int1ThsY, uint16_t int1ThsZ, uint8_t duration)
jerziboi732 0:354a8831107d 356 {
jerziboi732 0:354a8831107d 357 gWriteByte(INT1_CFG_G, int1Cfg);
jerziboi732 0:354a8831107d 358 gWriteByte(INT1_THS_XH_G, (int1ThsX & 0xFF00) >> 8);
jerziboi732 0:354a8831107d 359 gWriteByte(INT1_THS_XL_G, (int1ThsX & 0xFF));
jerziboi732 0:354a8831107d 360 gWriteByte(INT1_THS_YH_G, (int1ThsY & 0xFF00) >> 8);
jerziboi732 0:354a8831107d 361 gWriteByte(INT1_THS_YL_G, (int1ThsY & 0xFF));
jerziboi732 0:354a8831107d 362 gWriteByte(INT1_THS_ZH_G, (int1ThsZ & 0xFF00) >> 8);
jerziboi732 0:354a8831107d 363 gWriteByte(INT1_THS_ZL_G, (int1ThsZ & 0xFF));
jerziboi732 0:354a8831107d 364 if (duration)
jerziboi732 0:354a8831107d 365 gWriteByte(INT1_DURATION_G, 0x80 | duration);
jerziboi732 0:354a8831107d 366 else
jerziboi732 0:354a8831107d 367 gWriteByte(INT1_DURATION_G, 0x00);
jerziboi732 0:354a8831107d 368 }
jerziboi732 0:354a8831107d 369
jerziboi732 0:354a8831107d 370 void LSM9DS0::calcgRes()
jerziboi732 0:354a8831107d 371 {
jerziboi732 0:354a8831107d 372 // Possible gyro scales (and their register bit settings) are:
jerziboi732 0:354a8831107d 373 // 245 DPS (00), 500 DPS (01), 2000 DPS (10). Here's a bit of an algorithm
jerziboi732 0:354a8831107d 374 // to calculate DPS/(ADC tick) based on that 2-bit value:
jerziboi732 0:354a8831107d 375 switch (gScale)
jerziboi732 0:354a8831107d 376 {
jerziboi732 0:354a8831107d 377 case G_SCALE_245DPS:
jerziboi732 0:354a8831107d 378 gRes = 245.0 / 32768.0;
jerziboi732 0:354a8831107d 379 break;
jerziboi732 0:354a8831107d 380 case G_SCALE_500DPS:
jerziboi732 0:354a8831107d 381 gRes = 500.0 / 32768.0;
jerziboi732 0:354a8831107d 382 break;
jerziboi732 0:354a8831107d 383 case G_SCALE_2000DPS:
jerziboi732 0:354a8831107d 384 gRes = 2000.0 / 32768.0;
jerziboi732 0:354a8831107d 385 break;
jerziboi732 0:354a8831107d 386 }
jerziboi732 0:354a8831107d 387 }
jerziboi732 0:354a8831107d 388
jerziboi732 0:354a8831107d 389 void LSM9DS0::calcaRes()
jerziboi732 0:354a8831107d 390 {
jerziboi732 0:354a8831107d 391 // Possible accelerometer scales (and their register bit settings) are:
jerziboi732 0:354a8831107d 392 // 2 g (000), 4g (001), 6g (010) 8g (011), 16g (100). Here's a bit of an
jerziboi732 0:354a8831107d 393 // algorithm to calculate g/(ADC tick) based on that 3-bit value:
jerziboi732 0:354a8831107d 394 aRes = aScale == A_SCALE_16G ? 16.0 / 32768.0 :
jerziboi732 0:354a8831107d 395 (((float) aScale + 1.0) * 2.0) / 32768.0;
jerziboi732 0:354a8831107d 396 }
jerziboi732 0:354a8831107d 397
jerziboi732 0:354a8831107d 398 void LSM9DS0::calcmRes()
jerziboi732 0:354a8831107d 399 {
jerziboi732 0:354a8831107d 400 // Possible magnetometer scales (and their register bit settings) are:
jerziboi732 0:354a8831107d 401 // 2 Gs (00), 4 Gs (01), 8 Gs (10) 12 Gs (11). Here's a bit of an algorithm
jerziboi732 0:354a8831107d 402 // to calculate Gs/(ADC tick) based on that 2-bit value:
jerziboi732 0:354a8831107d 403 mRes = mScale == M_SCALE_2GS ? 2.0 / 32768.0 :
jerziboi732 0:354a8831107d 404 (float) (mScale << 2) / 32768.0;
jerziboi732 0:354a8831107d 405 }
jerziboi732 0:354a8831107d 406
jerziboi732 0:354a8831107d 407 void LSM9DS0::gWriteByte(uint8_t subAddress, uint8_t data)
jerziboi732 0:354a8831107d 408 {
jerziboi732 0:354a8831107d 409 // Whether we're using I2C or SPI, write a byte using the
jerziboi732 0:354a8831107d 410 // gyro-specific I2C address or SPI CS pin.
jerziboi732 0:354a8831107d 411 I2CwriteByte(gAddress, subAddress, data);
jerziboi732 0:354a8831107d 412 }
jerziboi732 0:354a8831107d 413
jerziboi732 0:354a8831107d 414 void LSM9DS0::xmWriteByte(uint8_t subAddress, uint8_t data)
jerziboi732 0:354a8831107d 415 {
jerziboi732 0:354a8831107d 416 // Whether we're using I2C or SPI, write a byte using the
jerziboi732 0:354a8831107d 417 // accelerometer-specific I2C address or SPI CS pin.
jerziboi732 0:354a8831107d 418 return I2CwriteByte(xmAddress, subAddress, data);
jerziboi732 0:354a8831107d 419 }
jerziboi732 0:354a8831107d 420
jerziboi732 0:354a8831107d 421 uint8_t LSM9DS0::gReadByte(uint8_t subAddress)
jerziboi732 0:354a8831107d 422 {
jerziboi732 0:354a8831107d 423 return I2CreadByte(gAddress, subAddress);
jerziboi732 0:354a8831107d 424 }
jerziboi732 0:354a8831107d 425
jerziboi732 0:354a8831107d 426 void LSM9DS0::gReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count)
jerziboi732 0:354a8831107d 427 {
jerziboi732 0:354a8831107d 428 // Whether we're using I2C or SPI, read multiple bytes using the
jerziboi732 0:354a8831107d 429 // gyro-specific I2C address.
jerziboi732 0:354a8831107d 430 I2CreadBytes(gAddress, subAddress, dest, count);
jerziboi732 0:354a8831107d 431 }
jerziboi732 0:354a8831107d 432
jerziboi732 0:354a8831107d 433 uint8_t LSM9DS0::xmReadByte(uint8_t subAddress)
jerziboi732 0:354a8831107d 434 {
jerziboi732 0:354a8831107d 435 // Whether we're using I2C or SPI, read a byte using the
jerziboi732 0:354a8831107d 436 // accelerometer-specific I2C address.
jerziboi732 0:354a8831107d 437 return I2CreadByte(xmAddress, subAddress);
jerziboi732 0:354a8831107d 438 }
jerziboi732 0:354a8831107d 439
jerziboi732 0:354a8831107d 440 void LSM9DS0::xmReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count)
jerziboi732 0:354a8831107d 441 {
jerziboi732 0:354a8831107d 442 // read multiple bytes using the
jerziboi732 0:354a8831107d 443 // accelerometer-specific I2C address.
jerziboi732 0:354a8831107d 444 I2CreadBytes(xmAddress, subAddress, dest, count);
jerziboi732 0:354a8831107d 445 }
jerziboi732 0:354a8831107d 446
jerziboi732 0:354a8831107d 447
jerziboi732 0:354a8831107d 448 void LSM9DS0::I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data)
jerziboi732 0:354a8831107d 449 {
jerziboi732 0:354a8831107d 450 i2c_->writeByte(address,subAddress,data);
jerziboi732 0:354a8831107d 451 }
jerziboi732 0:354a8831107d 452
jerziboi732 0:354a8831107d 453 uint8_t LSM9DS0::I2CreadByte(uint8_t address, uint8_t subAddress)
jerziboi732 0:354a8831107d 454 {
jerziboi732 0:354a8831107d 455 char data[1]; // `data` will store the register data
jerziboi732 0:354a8831107d 456
jerziboi732 0:354a8831107d 457 I2CreadBytes(address, subAddress,(uint8_t*)data, 1);
jerziboi732 0:354a8831107d 458 return (uint8_t)data[0];
jerziboi732 0:354a8831107d 459
jerziboi732 0:354a8831107d 460 }
jerziboi732 0:354a8831107d 461
jerziboi732 0:354a8831107d 462 void LSM9DS0::I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest,
jerziboi732 0:354a8831107d 463 uint8_t count)
jerziboi732 0:354a8831107d 464 {
jerziboi732 0:354a8831107d 465 i2c_->readBytes(address, subAddress, count, dest);
jerziboi732 0:354a8831107d 466 }