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