19 #include "LSM9DS1.hpp"    20 #include "LSM9DS1_Registers.h"    21 #include "LSM9DS1_Types.h"    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    40     init(IMU_MODE_I2C, LSM9DS1_AG_ADDR(1), LSM9DS1_M_ADDR(1));
    43 LSM9DS1::LSM9DS1(interface_mode interface, uint8_t xgAddr, uint8_t mAddr)
    45     init(interface, xgAddr, mAddr);
    48 void LSM9DS1::init(interface_mode interface, uint8_t xgAddr, uint8_t mAddr)
    50     settings.device.commInterface = interface;
    51     settings.device.agAddress = xgAddr;
    52     settings.device.mAddress = mAddr;
    54     settings.gyro.enabled = 
true;
    55     settings.gyro.enableX = 
true;
    56     settings.gyro.enableY = 
true;
    57     settings.gyro.enableZ = 
true;
    59     settings.gyro.scale = 245;
    64     settings.gyro.sampleRate = 6;
    68     settings.gyro.bandwidth = 0;
    69     settings.gyro.lowPowerEnable = 
false;
    70     settings.gyro.HPFEnable = 
false;
    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;
    81     settings.accel.enabled = 
true;
    82     settings.accel.enableX = 
true;
    83     settings.accel.enableY = 
true;
    84     settings.accel.enableZ = 
true;
    86     settings.accel.scale = 2;
    91     settings.accel.sampleRate = 6;
    96     settings.accel.bandwidth = -1;
    97     settings.accel.highResEnable = 
false;
   102     settings.accel.highResBandwidth = 0;
   104     settings.mag.enabled = 
true;
   106     settings.mag.scale = 4;
   112     settings.mag.sampleRate = 7;
   113     settings.mag.tempCompensationEnable = 
false;
   117     settings.mag.XYPerformance = 3;
   118     settings.mag.ZPerformance = 3;
   119     settings.mag.lowPowerEnable = 
false;
   124     settings.mag.operatingMode = 0;
   126     settings.temp.enabled = 
true;
   127     for (
int i=0; i<3; i++)
   143     _xgAddress = settings.device.agAddress;
   144     _mAddress = settings.device.mAddress;
   154     if (settings.device.commInterface == IMU_MODE_I2C)  
   156     else if (settings.device.commInterface == IMU_MODE_SPI)     
   161     uint8_t mTest = mReadByte(WHO_AM_I_M);      
   162     uint8_t xgTest = xgReadByte(WHO_AM_I_XG);   
   163     uint16_t whoAmICombined = (xgTest << 8) | mTest;
   165     if (whoAmICombined != ((WHO_AM_I_AG_RSP << 8) | WHO_AM_I_M_RSP))
   178     return whoAmICombined;
   181 void LSM9DS1::initGyro()
   183     uint8_t tempRegValue = 0;
   193     if (settings.gyro.enabled)
   195         tempRegValue = (settings.gyro.sampleRate & 0x07) << 5;
   197     switch (settings.gyro.scale)
   200             tempRegValue |= (0x1 << 3);
   203             tempRegValue |= (0x3 << 3);
   207     tempRegValue |= (settings.gyro.bandwidth & 0x3);
   208     xgWriteByte(CTRL_REG1_G, tempRegValue);
   214     xgWriteByte(CTRL_REG2_G, 0x00); 
   221     tempRegValue = settings.gyro.lowPowerEnable ? (1<<7) : 0;
   222     if (settings.gyro.HPFEnable)
   224         tempRegValue |= (1<<6) | (settings.gyro.HPFCutoff & 0x0F);
   226     xgWriteByte(CTRL_REG3_G, tempRegValue);
   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);
   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);
   253 void LSM9DS1::initAccel()
   255     uint8_t tempRegValue = 0;
   264     if (settings.accel.enableZ) tempRegValue |= (1<<5);
   265     if (settings.accel.enableY) tempRegValue |= (1<<4);
   266     if (settings.accel.enableX) tempRegValue |= (1<<3);
   268     xgWriteByte(CTRL_REG5_XL, tempRegValue);
   278     if (settings.accel.enabled)
   280         tempRegValue |= (settings.accel.sampleRate & 0x07) << 5;
   282     switch (settings.accel.scale)
   285             tempRegValue |= (0x2 << 3);
   288             tempRegValue |= (0x3 << 3);
   291             tempRegValue |= (0x1 << 3);
   295     if (settings.accel.bandwidth >= 0)
   297         tempRegValue |= (1<<2); 
   298         tempRegValue |= (settings.accel.bandwidth & 0x03);
   300     xgWriteByte(CTRL_REG6_XL, tempRegValue);
   309     if (settings.accel.highResEnable)
   311         tempRegValue |= (1<<7); 
   312         tempRegValue |= (settings.accel.highResBandwidth & 0x3) << 5;
   314     xgWriteByte(CTRL_REG7_XL, tempRegValue);
   324 void LSM9DS1::calibrate(
bool autoCalc)
   326     uint8_t data[6] = {0, 0, 0, 0, 0, 0};
   329     int32_t aBiasRawTemp[3] = {0, 0, 0};
   330     int32_t gBiasRawTemp[3] = {0, 0, 0};
   334     setFIFO(FIFO_THS, 0x1F);
   335     while (samples < 0x1F)
   337         samples = (xgReadByte(FIFO_SRC) & 0x3F); 
   339     for(ii = 0; ii < samples ; ii++) 
   342         gBiasRawTemp[0] += gx;
   343         gBiasRawTemp[1] += gy;
   344         gBiasRawTemp[2] += gz;
   346         aBiasRawTemp[0] += ax;
   347         aBiasRawTemp[1] += ay;
   348         aBiasRawTemp[2] += az - (int16_t)(1./aRes); 
   350     for (ii = 0; ii < 3; ii++)
   352         gBiasRaw[ii] = gBiasRawTemp[ii] / samples;
   353         gBias[ii] = calcGyro(gBiasRaw[ii]);
   354         aBiasRaw[ii] = aBiasRawTemp[ii] / samples;
   355         aBias[ii] = calcAccel(aBiasRaw[ii]);
   359     setFIFO(FIFO_OFF, 0x00);
   361     if (autoCalc) _autoCalc = 
true;
   364 void LSM9DS1::calibrateMag(
bool loadIn)
   367     int16_t magMin[3] = {0, 0, 0};
   368     int16_t magMax[3] = {0, 0, 0}; 
   370     for (i=0; i<128; i++)
   372         while (!magAvailable())
   375         int16_t magTemp[3] = {0, 0, 0};
   379         for (j = 0; j < 3; j++)
   381             if (magTemp[j] > magMax[j]) magMax[j] = magTemp[j];
   382             if (magTemp[j] < magMin[j]) magMin[j] = magTemp[j];
   385     for (j = 0; j < 3; j++)
   387         mBiasRaw[j] = (magMax[j] + magMin[j]) / 2;
   388         mBias[j] = calcMag(mBiasRaw[j]);
   390             magOffset(j, mBiasRaw[j]);
   394 void LSM9DS1::magOffset(uint8_t axis, int16_t offset)
   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);
   405 void LSM9DS1::initMag()
   407     uint8_t tempRegValue = 0;
   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);
   428     switch (settings.mag.scale)
   431             tempRegValue |= (0x1 << 5);
   434             tempRegValue |= (0x2 << 5);
   437             tempRegValue |= (0x3 << 5);
   441     mWriteByte(CTRL_REG2_M, tempRegValue); 
   452     if (settings.mag.lowPowerEnable) tempRegValue |= (1<<5);
   453     tempRegValue |= (settings.mag.operatingMode & 0x3);
   454     mWriteByte(CTRL_REG3_M, tempRegValue); 
   463     tempRegValue = (settings.mag.ZPerformance & 0x3) << 2;
   464     mWriteByte(CTRL_REG4_M, tempRegValue);
   471     mWriteByte(CTRL_REG5_M, tempRegValue);
   474 uint8_t LSM9DS1::accelAvailable()
   476     uint8_t status = xgReadByte(STATUS_REG_1);
   478     return (status & (1<<0));
   481 uint8_t LSM9DS1::gyroAvailable()
   483     uint8_t status = xgReadByte(STATUS_REG_1);
   485     return ((status & (1<<1)) >> 1);
   488 uint8_t LSM9DS1::tempAvailable()
   490     uint8_t status = xgReadByte(STATUS_REG_1);
   492     return ((status & (1<<2)) >> 2);
   495 uint8_t LSM9DS1::magAvailable(lsm9ds1_axis axis)
   498     status = mReadByte(STATUS_REG_M);
   500     return ((status & (1<<axis)) >> axis);
   503 void LSM9DS1::readAccel()
   506     if ( xgReadBytes(OUT_X_L_XL, temp, 6) == 6 ) 
   508         ax = (temp[1] << 8) | temp[0]; 
   509         ay = (temp[3] << 8) | temp[2]; 
   510         az = (temp[5] << 8) | temp[4]; 
   513             ax -= aBiasRaw[X_AXIS];
   514             ay -= aBiasRaw[Y_AXIS];
   515             az -= aBiasRaw[Z_AXIS];
   520 int16_t LSM9DS1::readAccel(lsm9ds1_axis axis)
   524     if ( xgReadBytes(OUT_X_L_XL + (2 * axis), temp, 2) == 2)
   526         value = (temp[1] << 8) | temp[0];
   529             value -= aBiasRaw[axis];
   536 void LSM9DS1::readMag()
   539     if ( mReadBytes(OUT_X_L_M, temp, 6) == 6) 
   541         mx = (temp[1] << 8) | temp[0]; 
   542         my = (temp[3] << 8) | temp[2]; 
   543         mz = (temp[5] << 8) | temp[4]; 
   547 int16_t LSM9DS1::readMag(lsm9ds1_axis axis)
   550     if ( mReadBytes(OUT_X_L_M + (2 * axis), temp, 2) == 2)
   552         return (temp[1] << 8) | temp[0];
   557 void LSM9DS1::readTemp()
   560     if ( xgReadBytes(OUT_TEMP_L, temp, 2) == 2 ) 
   563         temperature = offset + ((((int16_t)temp[1] << 8) | temp[0]) >> 8) ;
   567 void LSM9DS1::readGyro()
   570     if ( xgReadBytes(OUT_X_L_G, temp, 6) == 6) 
   572         gx = (temp[1] << 8) | temp[0]; 
   573         gy = (temp[3] << 8) | temp[2]; 
   574         gz = (temp[5] << 8) | temp[4]; 
   577             gx -= gBiasRaw[X_AXIS];
   578             gy -= gBiasRaw[Y_AXIS];
   579             gz -= gBiasRaw[Z_AXIS];
   584 int16_t LSM9DS1::readGyro(lsm9ds1_axis axis)
   589     if ( xgReadBytes(OUT_X_L_G + (2 * axis), temp, 2) == 2)
   591         value = (temp[1] << 8) | temp[0];
   594             value -= gBiasRaw[axis];
   601 float LSM9DS1::calcGyro(int16_t gyro)
   607 float LSM9DS1::calcAccel(int16_t accel)
   613 float LSM9DS1::calcMag(int16_t mag)
   619 void LSM9DS1::setGyroScale(uint16_t gScl)
   622     uint8_t ctrl1RegValue = xgReadByte(CTRL_REG1_G);
   624     ctrl1RegValue &= 0xE7;
   628             ctrl1RegValue |= (0x1 << 3);
   629             settings.gyro.scale = 500;
   632             ctrl1RegValue |= (0x3 << 3);
   633             settings.gyro.scale = 2000;
   636             settings.gyro.scale = 245;
   639     xgWriteByte(CTRL_REG1_G, ctrl1RegValue);
   644 void LSM9DS1::setAccelScale(uint8_t aScl)
   647     uint8_t tempRegValue = xgReadByte(CTRL_REG6_XL);
   649     tempRegValue &= 0xE7;
   654             tempRegValue |= (0x2 << 3);
   655             settings.accel.scale = 4;
   658             tempRegValue |= (0x3 << 3);
   659             settings.accel.scale = 8;
   662             tempRegValue |= (0x1 << 3);
   663             settings.accel.scale = 16;
   666             settings.accel.scale = 2;
   669     xgWriteByte(CTRL_REG6_XL, tempRegValue);
   675 void LSM9DS1::setMagScale(uint8_t mScl)
   678     uint8_t temp = mReadByte(CTRL_REG2_M);
   680     temp &= 0xFF^(0x3 << 5);
   686             settings.mag.scale = 8;
   690             settings.mag.scale = 12;
   694             settings.mag.scale = 16;
   697             settings.mag.scale = 4;
   702     mWriteByte(CTRL_REG2_M, temp);
   711 void LSM9DS1::setGyroODR(uint8_t gRate)
   714     if ((gRate & 0x07) != 0)
   717         uint8_t temp = xgReadByte(CTRL_REG1_G);
   719         temp &= 0xFF^(0x7 << 5);
   720         temp |= (gRate & 0x07) << 5;
   722         settings.gyro.sampleRate = gRate & 0x07;
   724         xgWriteByte(CTRL_REG1_G, temp);
   728 void LSM9DS1::setAccelODR(uint8_t aRate)
   731     if ((aRate & 0x07) != 0)
   734         uint8_t temp = xgReadByte(CTRL_REG6_XL);
   738         temp |= ((aRate & 0x07) << 5);
   739         settings.accel.sampleRate = aRate & 0x07;
   741         xgWriteByte(CTRL_REG6_XL, temp);
   745 void LSM9DS1::setMagODR(uint8_t mRate)
   748     uint8_t temp = mReadByte(CTRL_REG1_M);
   750     temp &= 0xFF^(0x7 << 2);
   752     temp |= ((mRate & 0x07) << 2);
   753     settings.mag.sampleRate = mRate & 0x07;
   755     mWriteByte(CTRL_REG1_M, temp);
   758 void LSM9DS1::calcgRes()
   760     switch (settings.gyro.scale)
   763             gRes = SENSITIVITY_GYROSCOPE_245;
   766             gRes = SENSITIVITY_GYROSCOPE_500;
   769             gRes = SENSITIVITY_GYROSCOPE_2000;
   776 void LSM9DS1::calcaRes()
   778     switch (settings.accel.scale)
   781             aRes = SENSITIVITY_ACCELEROMETER_2;
   784             aRes = SENSITIVITY_ACCELEROMETER_4;
   787             aRes = SENSITIVITY_ACCELEROMETER_8;
   790             aRes = SENSITIVITY_ACCELEROMETER_16;
   797 void LSM9DS1::calcmRes()
   799     switch (settings.mag.scale)
   802             mRes = SENSITIVITY_MAGNETOMETER_4;
   805             mRes = SENSITIVITY_MAGNETOMETER_8;
   808             mRes = SENSITIVITY_MAGNETOMETER_12;
   811             mRes = SENSITIVITY_MAGNETOMETER_16;
   816 void LSM9DS1::configInt(interrupt_select itrpt, uint8_t generator,
   817         h_lactive activeLow, pp_od pushPull)
   822     xgWriteByte(itrpt, generator);
   826     temp = xgReadByte(CTRL_REG8);
   828     if (activeLow) temp |= (1<<5);
   829     else temp &= ~(1<<5);
   831     if (pushPull) temp &= ~(1<<4);
   834     xgWriteByte(CTRL_REG8, temp);
   837 void LSM9DS1::configInactivity(uint8_t duration, uint8_t threshold, 
bool sleepOn)
   841     temp = threshold & 0x7F;
   842     if (sleepOn) temp |= (1<<7);
   843     xgWriteByte(ACT_THS, temp);
   845     xgWriteByte(ACT_DUR, duration);
   848 uint8_t LSM9DS1::getInactivity()
   850     uint8_t temp = xgReadByte(STATUS_REG_0);
   855 void LSM9DS1::configAccelInt(uint8_t generator, 
bool andInterrupts)
   859     uint8_t temp = generator;
   860     if (andInterrupts) temp |= 0x80;
   861     xgWriteByte(INT_GEN_CFG_XL, temp);
   864 void LSM9DS1::configAccelThs(uint8_t threshold, lsm9ds1_axis axis, uint8_t duration, 
bool wait)
   868     xgWriteByte(INT_GEN_THS_X_XL + axis, threshold);
   872     temp = (duration & 0x7F);
   873     if (wait) temp |= 0x80;
   874     xgWriteByte(INT_GEN_DUR_XL, temp);
   877 uint8_t LSM9DS1::getAccelIntSrc()
   879     uint8_t intSrc = xgReadByte(INT_GEN_SRC_XL);
   884         return (intSrc & 0x3F);
   890 void LSM9DS1::configGyroInt(uint8_t generator, 
bool aoi, 
bool latch)
   894     uint8_t temp = generator;
   895     if (aoi) temp |= 0x80;
   896     if (latch) temp |= 0x40;
   897     xgWriteByte(INT_GEN_CFG_G, temp);
   900 void LSM9DS1::configGyroThs(int16_t threshold, lsm9ds1_axis axis, uint8_t duration, 
bool wait)
   903     buffer[0] = (threshold & 0x7F00) >> 8;
   904     buffer[1] = (threshold & 0x00FF);
   907     xgWriteByte(INT_GEN_THS_XH_G + (axis * 2), buffer[0]);
   908     xgWriteByte(INT_GEN_THS_XH_G + 1 + (axis * 2), buffer[1]);
   912     temp = (duration & 0x7F);
   913     if (wait) temp |= 0x80;
   914     xgWriteByte(INT_GEN_DUR_G, temp);
   917 uint8_t LSM9DS1::getGyroIntSrc()
   919     uint8_t intSrc = xgReadByte(INT_GEN_SRC_G);
   924         return (intSrc & 0x3F);
   930 void LSM9DS1::configMagInt(uint8_t generator, h_lactive activeLow, 
bool latch)
   933     uint8_t config = (generator & 0xE0);    
   935     if (activeLow == INT_ACTIVE_HIGH) config |= (1<<2);
   937     if (!latch) config |= (1<<1);
   939     if (generator != 0) config |= (1<<0);
   941     mWriteByte(INT_CFG_M, config);
   944 void LSM9DS1::configMagThs(uint16_t threshold)
   947     mWriteByte(INT_THS_H_M, uint8_t((threshold & 0x7F00) >> 8));
   949     mWriteByte(INT_THS_L_M, uint8_t(threshold & 0x00FF));
   952 uint8_t LSM9DS1::getMagIntSrc()
   954     uint8_t intSrc = mReadByte(INT_SRC_M);
   959         return (intSrc & 0xFE);
   965 void LSM9DS1::sleepGyro(
bool enable)
   967     uint8_t temp = xgReadByte(CTRL_REG9);
   968     if (enable) temp |= (1<<6);
   969     else temp &= ~(1<<6);
   970     xgWriteByte(CTRL_REG9, temp);
   973 void LSM9DS1::enableFIFO(
bool enable)
   975     uint8_t temp = xgReadByte(CTRL_REG9);
   976     if (enable) temp |= (1<<1);
   977     else temp &= ~(1<<1);
   978     xgWriteByte(CTRL_REG9, temp);
   981 void LSM9DS1::setFIFO(fifoMode_type fifoMode, uint8_t fifoThs)
   985     uint8_t threshold = fifoThs <= 0x1F ? fifoThs : 0x1F;
   986     xgWriteByte(FIFO_CTRL, ((fifoMode & 0x7) << 5) | (threshold & 0x1F));
   989 uint8_t LSM9DS1::getFIFOSamples()
   991     return (xgReadByte(FIFO_SRC) & 0x3F);
   994 void LSM9DS1::constrainScales()
   996     if ((settings.gyro.scale != 245) && (settings.gyro.scale != 500) && 
   997             (settings.gyro.scale != 2000))
   999         settings.gyro.scale = 245;
  1002     if ((settings.accel.scale != 2) && (settings.accel.scale != 4) &&
  1003             (settings.accel.scale != 8) && (settings.accel.scale != 16))
  1005         settings.accel.scale = 2;
  1008     if ((settings.mag.scale != 4) && (settings.mag.scale != 8) &&
  1009             (settings.mag.scale != 12) && (settings.mag.scale != 16))
  1011         settings.mag.scale = 4;
  1015 void LSM9DS1::xgWriteByte(uint8_t subAddress, uint8_t data)
  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);
  1025 void LSM9DS1::mWriteByte(uint8_t subAddress, uint8_t data)
  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);
  1035 uint8_t LSM9DS1::xgReadByte(uint8_t subAddress)
  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);
  1046 uint8_t LSM9DS1::xgReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count)
  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);
  1057 uint8_t LSM9DS1::mReadByte(uint8_t subAddress)
  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);
  1068 uint8_t LSM9DS1::mReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count)
  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);
  1139 void LSM9DS1::initI2C()
  1143     i2cSetMode(i2cREG1, I2C_MASTER);
  1146 void LSM9DS1::I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data)
  1149     i2cSetSlaveAdd(i2cREG1, address);
  1150     i2cSetDirection(i2cREG1, I2C_TRANSMITTER);
  1151     i2cSetStop(i2cREG1);
  1152     i2cSetStart(i2cREG1);
  1154     i2cSendByte(i2cREG1, subAddress);
  1156     i2cSendByte(i2cREG1, data);
  1159     while(i2cIsBusBusy(i2cREG1));
  1161     while(!i2cIsStopDetected(i2cREG1));
  1164 uint8_t LSM9DS1::I2CreadByte(uint8_t address, uint8_t subAddress)
  1168     i2cSetSlaveAdd(i2cREG1, address);
  1169     i2cSetDirection(i2cREG1, I2C_TRANSMITTER);
  1170     i2cSetStop(i2cREG1);
  1171     i2cSetStart(i2cREG1);
  1173     i2cSendByte(i2cREG1, subAddress);
  1175     while(i2cIsBusBusy(i2cREG1));
  1177     while(!i2cIsStopDetected(i2cREG1));
  1179     i2cClearSCD(i2cREG1);
  1182     i2cSetDirection(i2cREG1, I2C_RECEIVER);
  1183     i2cSetStop(i2cREG1);
  1184     i2cSetStart(i2cREG1);
  1185     data = i2cReceiveByte(i2cREG1);
  1186     while(i2cIsBusBusy(i2cREG1));
  1188     while(!i2cIsStopDetected(i2cREG1));
  1189     i2cClearSCD(i2cREG1);
  1195 uint8_t LSM9DS1::I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count)
  1198     i2cSetSlaveAdd(i2cREG1, address);
  1199     i2cSetDirection(i2cREG1, I2C_TRANSMITTER);
  1200     i2cSetStop(i2cREG1);
  1201     i2cSetStart(i2cREG1);
  1203     i2cSendByte(i2cREG1, subAddress | 0x80);
  1204     while(i2cIsBusBusy(i2cREG1));
  1206     while(!i2cIsStopDetected(i2cREG1));
  1208     i2cClearSCD(i2cREG1);
  1210     i2cSetDirection(i2cREG1, I2C_RECEIVER);
  1211     i2cSetStop(i2cREG1);
  1212     i2cSetStart(i2cREG1);
  1213     i2cReceive(i2cREG1, count, dest);
  1214     while(i2cIsBusBusy(i2cREG1));
  1216     while(!i2cIsStopDetected(i2cREG1));
  1218     i2cClearSCD(i2cREG1);