Esempio n. 1
0
/*----------------------------------------------------------------------*
 * Read multiple bytes from RTC RAM.                                    *
 * Valid address range is 0x00 - 0xFF, no checking.                     *
 * Number of bytes (nBytes) must be between 1 and 32 (Wire library      *
 * limitation).                                                         *
 * Returns the I2C status (zero if successful).                         *
 *----------------------------------------------------------------------*/
byte DS3232RTC::readRTC(byte addr, byte *values, byte nBytes)
{
    i2cBeginTransmission(RTC_ADDR);
    i2cWrite(addr);
    if ( byte e = i2cEndTransmission() ) return e;
    i2cRequestFrom( (uint8_t)RTC_ADDR, nBytes );
    for (byte i=0; i<nBytes; i++) values[i] = i2cRead();
    return 0;
}
Esempio n. 2
0
static void mma8452Read(int16_t *accelData)
{
    uint8_t buf[6];

    i2cRead(MMA8452_ADDRESS, MMA8452_OUT_X_MSB, 6, buf);
    accelData[0] = ((int16_t)(buf[0] << 8) | buf[1]);
    accelData[1] = ((int16_t)(buf[2] << 8) | buf[3]);
    accelData[2] = ((int16_t)(buf[4] << 8) | buf[5]);
}
Esempio n. 3
0
bool hmc5883lDetect(void)
{
    bool ack = false;
    uint8_t sig = 0;

    delay(50);
    ack = i2cRead(MAG_ADDRESS, 0x0A, 1, &sig);
    if (!ack)
    {
        delay(50);
        ack = i2cRead(MAG_ADDRESS, 0x0A, 1, &sig);
    }

    if (!ack || sig != 'H')
        return false;

    return true;
}
bool ak8975Read(int16_t *magData)
{
    bool ack;
    UNUSED(ack);
    uint8_t status;
    uint8_t buf[6];

    ack = i2cRead(MAG_I2C_INSTANCE, AK8975_MAG_I2C_ADDRESS, AK8975_MAG_REG_STATUS1, 1, &status);
    if (!ack || (status & BIT_STATUS1_REG_DATA_READY) == 0) {
        return false;
    }

#if 1 // USE_I2C_SINGLE_BYTE_READS
    ack = i2cRead(MAG_I2C_INSTANCE, AK8975_MAG_I2C_ADDRESS, AK8975_MAG_REG_HXL, 6, buf); // read from AK8975_MAG_REG_HXL to AK8975_MAG_REG_HZH
#else
    for (uint8_t i = 0; i < 6; i++) {
        ack = i2cRead(AK8975_MAG_I2C_ADDRESS, AK8975_MAG_REG_HXL + i, 1, &buf[i]); // read from AK8975_MAG_REG_HXL to AK8975_MAG_REG_HZH
        if (!ack) {
            return false
        }
    }
#endif

    ack = i2cRead(MAG_I2C_INSTANCE, AK8975_MAG_I2C_ADDRESS, AK8975_MAG_REG_STATUS2, 1, &status);
    if (!ack) {
        return false;
    }

    if (status & BIT_STATUS2_REG_DATA_ERROR) {
        return false;
    }

    if (status & BIT_STATUS2_REG_MAG_SENSOR_OVERFLOW) {
        return false;
    }

    magData[X] = -(int16_t)(buf[1] << 8 | buf[0]) * 4;
    magData[Y] = -(int16_t)(buf[3] << 8 | buf[2]) * 4;
    magData[Z] = -(int16_t)(buf[5] << 8 | buf[4]) * 4;


    ack = i2cWrite(MAG_I2C_INSTANCE, AK8975_MAG_I2C_ADDRESS, AK8975_MAG_REG_CNTL, 0x01); // start reading again
    return true;
}
Esempio n. 5
0
static uint16_t ms56xx_prom(int8_t coef_num)
{
    uint8_t rxbuf[2] = { 0, 0 };
#if defined(USE_BARO_SPI_MS5611) || defined(USE_BARO_SPI_MS5607)
    ms56xxSpiReadCommand(CMD_PROM_RD + coef_num * 2, 2, rxbuf); // send PROM READ command
#else
    i2cRead(BARO_I2C_INSTANCE, MS56XX_ADDR, CMD_PROM_RD + coef_num * 2, 2, rxbuf); // send PROM READ command
#endif
    return rxbuf[0] << 8 | rxbuf[1];
}
Esempio n. 6
0
/*
 * get raw data
 */
void mpu6050_getRawData(char address, short* ax, short* ay, short* az, short* gx, short* gy, short* gz) {
	i2cRead(address, MPU6050_ACCEL_XOUT_H, (unsigned char *)buffer, 14);

	*ax = (((short)buffer[0]) << 8) | buffer[1];
	*ay = (((short)buffer[2]) << 8) | buffer[3];
	*az = (((short)buffer[4]) << 8) | buffer[5];
	*gx = (((short)buffer[8]) << 8) | buffer[9];
	*gy = (((short)buffer[10]) << 8) | buffer[11];
	*gz = (((short)buffer[12]) << 8) | buffer[13];
}
Esempio n. 7
0
bool mpu6050AccDetect(acc_t *acc)
{
    uint8_t readBuffer[6];
    uint8_t revision;
    uint8_t productId;

    if (!mpu6050Detect()) {
        return false;
    }

    // There is a map of revision contained in the android source tree which is quite comprehensive and may help to understand this code
    // See https://android.googlesource.com/kernel/msm.git/+/eaf36994a3992b8f918c18e4f7411e8b2320a35f/drivers/misc/mpu6050/mldl_cfg.c

    // determine product ID and accel revision
    i2cRead(MPU6050_ADDRESS, MPU_RA_XA_OFFS_H, 6, readBuffer);
    revision = ((readBuffer[5] & 0x01) << 2) | ((readBuffer[3] & 0x01) << 1) | (readBuffer[1] & 0x01);
    if (revision) {
        /* Congrats, these parts are better. */
        if (revision == 1) {
            mpuAccelTrim = MPU_6050_HALF_RESOLUTION;
        } else if (revision == 2) {
            mpuAccelTrim = MPU_6050_FULL_RESOLUTION;
        } else {
            failureMode(5);
        }
    } else {
        i2cRead(MPU6050_ADDRESS, MPU_RA_PRODUCT_ID, 1, &productId);
        revision = productId & 0x0F;
        if (!revision) {
            failureMode(5);
        } else if (revision == 4) {
            mpuAccelTrim = MPU_6050_HALF_RESOLUTION;
        } else {
            mpuAccelTrim = MPU_6050_FULL_RESOLUTION;
        }
    }

    acc->init = mpu6050AccInit;
    acc->read = mpu6050AccRead;
    acc->revisionCode = (mpuAccelTrim == MPU_6050_HALF_RESOLUTION ? 'o' : 'n'); // es/non-es variance between MPU6050 sensors, half of the naze boards are mpu6000ES.

    return true;
}
Esempio n. 8
0
// Read 3 gyro values into user-provided buffer. No overrun checking is done.
static void mpu3050Read(int16_t *gyroADC)
{
    uint8_t buf[6];

    i2cRead(MPU3050_ADDRESS, MPU3050_GYRO_OUT, 6, buf);

    gyroADC[0] = (int16_t)((buf[0] << 8) | buf[1]);
    gyroADC[1] = (int16_t)((buf[2] << 8) | buf[3]);
    gyroADC[2] = (int16_t)((buf[4] << 8) | buf[5]);
}
Esempio n. 9
0
// Return gyro temperature
uint8_t l3gd20GetTemp(int16_t * temp)
{
	uint8_t value;

	if (!i2cRead(L3GD20_ADDRESS, L3GD20_OUT_TEMP, 1, &value))
		return MEMS_ERROR;

	*temp = (int16_t) (value);
	return MEMS_SUCCESS;
}
Esempio n. 10
0
size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop)
{
    if(size > I2C_BUFFER_LENGTH) {
        size = I2C_BUFFER_LENGTH;
    }
    size_t read = (i2cRead(i2c, address, false, rxBuffer, size, sendStop) == 0)?size:0;
    rxIndex = 0;
    rxLength = read;
    return read;
}
/* tmbslHdmiTxSysArgs_t definition is located into tmbslHdmiTx_type.h file.   */
tmErrorCode_t TxI2cReadFunction(tmbslHdmiTxSysArgs_t *pSysArgs)
{
    tmErrorCode_t errCode = TM_OK;

    if (pSysArgs->slaveAddr == 0x70)
    {
        errCode = i2cRead(reg_TDA998X, (tmbslHdmiSysArgs_t *) pSysArgs);
    }
    else if (pSysArgs->slaveAddr == 0x34)
    {
       errCode = i2cRead(reg_TDA9989_CEC, (tmbslHdmiSysArgs_t *) pSysArgs); 
    }
    else
    {
        errCode = ~TM_OK;
    }
    
    return errCode;
}
Esempio n. 12
0
static void mpu6050GyroRead(int16_t *gyroData)
{
    uint8_t buf[6];

    i2cRead(MPU6050_ADDRESS, MPU_RA_GYRO_XOUT_H, 6, buf);

    gyroData[0] = (int16_t)((buf[0] << 8) | buf[1]);
    gyroData[1] = (int16_t)((buf[2] << 8) | buf[3]);
    gyroData[2] = (int16_t)((buf[4] << 8) | buf[5]);
}
Esempio n. 13
0
void hmc5883lRead(int16_t *magData)
{
    uint8_t buf[6];

    i2cRead(MAG_ADDRESS, MAG_DATA_REGISTER, 6, buf);

    magData[0] = buf[0] << 8 | buf[1];
    magData[1] = buf[2] << 8 | buf[3];
    magData[2] = buf[4] << 8 | buf[5];
}
Esempio n. 14
0
bool mag3110Read(int16_t *magData)
{
    bool ack;
    UNUSED(ack);
    uint8_t status;
    uint8_t buf[6];

    ack = i2cRead(MAG3110_MAG_I2C_ADDRESS, MAG3110_MAG_REG_STATUS, 1, &status);
    if (!ack || (status & BIT_STATUS_REG_DATA_READY) == 0) {
        return false;
    }

    ack = i2cRead(MAG3110_MAG_I2C_ADDRESS, MAG3110_MAG_REG_HXL, 6, buf);

    magData[X] = (int16_t)(buf[0] << 8 | buf[1]);
    magData[Y] = (int16_t)(buf[2] << 8 | buf[3]);
    magData[Z] = (int16_t)(buf[4] << 8 | buf[5]);

    return true;
}
Esempio n. 15
0
void hmc5883lRead(int16_t *magData)
{
    uint8_t buf[6];

    i2cRead(MAG_ADDRESS, MAG_DATA_REGISTER, 6, buf);
    // During calibration, magGain is 1.0, so the read returns normal non-calibrated values.
    // After calibration is done, magGain is set to calculated gain values.
    magData[X] = (int16_t)(buf[0] << 8 | buf[1]) * magGain[X];
    magData[Z] = (int16_t)(buf[2] << 8 | buf[3]) * magGain[Z];
    magData[Y] = (int16_t)(buf[4] << 8 | buf[5]) * magGain[Y];
}
Esempio n. 16
0
// Return FIFO stored data level
uint8_t l3gd20GetFIFOLevel(void)
{
	uint8_t level;

	if (!i2cRead(L3GD20_ADDRESS, L3GD20_FIFO_SRC_REG, 1, &level))
		return MEMS_ERROR;

	level &= 0x0F;

	return level;
}
Esempio n. 17
0
static bool mpu3050ReadTemp(int16_t *tempData)
{
    uint8_t buf[2];
    if (!i2cRead(MPU3050_ADDRESS, MPU3050_TEMP_OUT, 2, buf, MPU3050_BUS)) {
        return false;
    }

    *tempData = 35 + ((int32_t)(buf[0] << 8 | buf[1]) + 13200) / 280;

    return true;
}
Esempio n. 18
0
/*! 
    \brief Retrieve the value of the three ID registers.    

    Note:  Both the HMC5843 and HMC5883L have the same 'H43' identification register values. (Looks like someone at Honeywell screwed up.)

    \param id [out] Returns the three id register values.
*/
void HMC58X3::getID(char id[3]) 
{
  unsigned char tx[2];

  tx[0] = HMC58X3_R_IDA;
  i2cWrite(HMC58X3_ADDR, tx, 1);
  
  if (i2cRead(HMC58X3_ADDR, (unsigned char*)id, 3))
  	id[0] = id[1] = id[2] = 0;
  UARTprintf("id %d %d %d %d %d %d\n", id[0], id[1], id[2], sizeof(int), sizeof(long int), sizeof(short));
}   // getID().
Esempio n. 19
0
/*
 * Get RTC ready
 */
STATUS
m41t81_tod_init(void)
{
    char byte;
    int polls;
    int status;

    /*
     * Reset HT bit to "0" to update registers with current time.
     */
    status = i2cRead(M41T81_SMBUS_CHAN,
                     M41T81_CCR_ADDRESS,
                     I2C_DEVICE_TYPE_RTC_M41T48,
                     M41T81REG_AHR,
                     1, 
                     &byte);
    byte &= ~M41T81REG_AHR_HT;
    status = i2cWrite(M41T81_SMBUS_CHAN,
                     M41T81_CCR_ADDRESS,
                     I2C_DEVICE_TYPE_RTC_M41T48,
                     M41T81REG_AHR,
                     1, 
                     &byte);

    /*
     * Try to read from the device.  If it does not
     * respond, fail.  We may need to do this for up to 300ms.
     */
    for (polls = 0; polls < 300; polls++) {
        taskDelay(1);
        status = i2cRead(M41T81_SMBUS_CHAN,
                     M41T81_CCR_ADDRESS,
                     I2C_DEVICE_TYPE_RTC_M41T48,
                     0,
                     1, 
                     &byte);
        if (status == OK) break;              /* read is ok */
    }

    return 0;
}
Esempio n. 20
0
static void mpu6050GyroRead(int16_t *gyroADC)
{
    uint8_t buf[6];

    if (!i2cRead(MPU6050_ADDRESS, MPU_RA_GYRO_XOUT_H, 6, buf)) {
        return;
    }

    gyroADC[0] = (int16_t)((buf[0] << 8) | buf[1]);
    gyroADC[1] = (int16_t)((buf[2] << 8) | buf[3]);
    gyroADC[2] = (int16_t)((buf[4] << 8) | buf[5]);
}
Esempio n. 21
0
bool bma280Detect(accDev_t *acc)
{
    uint8_t sig = 0;
    bool ack = i2cRead(MPU_I2C_INSTANCE, BMA280_ADDRESS, 0x00, 1, &sig);

    if (!ack || sig != 0xFB)
        return false;

    acc->initFn = bma280Init;
    acc->readFn = bma280Read;
    return true;
}
Esempio n. 22
0
static void mpu6050AccRead(int16_t *accData)
{
    uint8_t buf[6];

    if (!i2cRead(MPU6050_ADDRESS, MPU_RA_ACCEL_XOUT_H, 6, buf)) {
        return;
    }

    accData[0] = (int16_t)((buf[0] << 8) | buf[1]);
    accData[1] = (int16_t)((buf[2] << 8) | buf[3]);
    accData[2] = (int16_t)((buf[4] << 8) | buf[5]);
}
Esempio n. 23
0
int init3115(int i2cNum)
{
    unsigned char data;

    i2cDev = i2cOpen(i2cNum);

    if (i2cDev < 0)
        return 0;

    i2cWrite(MPL3115_I2C_ID, MPL3115A2_CTRL_REG1, 0);

    i2cRead(MPL3115_I2C_ID, MPL_3115_WHO_AM_I_REG, &data);

    if (data == MPL_3115_WHO_AM_I)
    {
    	i2cRead(MPL3115_I2C_ID, MPL3115A2_CTRL_REG1, &data);
        i2cWrite(MPL3115_I2C_ID, MPL3115A2_CTRL_REG1, ACTIVE_MASK);
        return 1;
    }
    return 0;
}
void PCF8574::i2cSend(){
	PCFBUFFER = i2cRead(0x00);
	for(int i=0;i<8;i++){
		if(bitRead(PCFTRISA,i) == INPUT) bitWrite(PCFPORTA,i,bitRead(PCFBUFFER,i));
		if(PCFPULL[i] == 1) bitWrite(PCFPORTA,i,1); // Required for unblock pull level
		if(PCFPULL[i] == 2) bitWrite(PCFPORTA,i,0); // Required for unblock pull level
	}
	Wire1.beginTransmission(PCFaddress);
	Wire1.write((uint8_t )PCFPORTA);
	Wire1.endTransmission();

}
Esempio n. 25
0
int8_t i2cread(uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf)
{
	if(i2cRead(addr,reg,len,buf))
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
	//return FALSE;
}
Esempio n. 26
0
int BMA180::getRegValue(int adr)
{
	unsigned char tx[1];
	unsigned char rx[1];
	tx[0] = adr;

	i2cWrite(address, tx, 1);
	int result = i2cRead(address, rx, 1);

	checkResult(result);
	return (int)rx[0];
}
Esempio n. 27
0
static void mpu6050AccRead(int16_t *accData)
{
    uint8_t buf[6];
    int16_t data[3];

    i2cRead(MPU6050_ADDRESS, MPU_RA_ACCEL_XOUT_H, 6, buf);
    data[0] = (int16_t)((buf[0] << 8) | buf[1]);
    data[1] = (int16_t)((buf[2] << 8) | buf[3]);
    data[2] = (int16_t)((buf[4] << 8) | buf[5]);

    alignSensors(data, accData, accAlign);
}
Esempio n. 28
0
// Read 3 gyro values into user-provided buffer. No overrun checking is done.
static void l3g4200dRead(int16_t *gyroData)
{
    uint8_t buf[6];
    int16_t data[3];

    i2cRead(L3G4200D_ADDRESS, L3G4200D_AUTOINCR | L3G4200D_GYRO_OUT, 6, buf);
    data[X] = (int16_t)((buf[0] << 8) | buf[1]) / 4;
    data[Y] = (int16_t)((buf[2] << 8) | buf[3]) / 4;
    data[Z] = (int16_t)((buf[4] << 8) | buf[5]) / 4;

    alignSensors(data, gyroData, gyroAlign);
}
Esempio n. 29
0
static void mpu6050GyroRead(int16_t *gyroData)
{
    uint8_t buf[6];
    int16_t data[3];

    i2cRead(MPU6050_ADDRESS, MPU_RA_GYRO_XOUT_H, 6, buf);
    data[0] = (int16_t)((buf[0] << 8) | buf[1]) / 4;
    data[1] = (int16_t)((buf[2] << 8) | buf[3]) / 4;
    data[2] = (int16_t)((buf[4] << 8) | buf[5]) / 4;

    alignSensors(data, gyroData, gyroAlign);
}
Esempio n. 30
0
static bool adxl345Read(int16_t *accelData)
{
    uint8_t buf[8];

    if (useFifo) {
        int32_t x = 0;
        int32_t y = 0;
        int32_t z = 0;
        uint8_t i = 0;
        uint8_t samples_remaining;

        do {
            i++;

            if (!i2cRead(MPU_I2C_INSTANCE, ADXL345_ADDRESS, ADXL345_DATA_OUT, 8, buf)) {
                return false;
            }

            x += (int16_t)(buf[0] + (buf[1] << 8));
            y += (int16_t)(buf[2] + (buf[3] << 8));
            z += (int16_t)(buf[4] + (buf[5] << 8));
            samples_remaining = buf[7] & 0x7F;
        } while ((i < 32) && (samples_remaining > 0));
        accelData[0] = x / i;
        accelData[1] = y / i;
        accelData[2] = z / i;
        acc_samples = i;
    } else {

        if (!i2cRead(MPU_I2C_INSTANCE, ADXL345_ADDRESS, ADXL345_DATA_OUT, 6, buf)) {
            return false;
        }

        accelData[0] = buf[0] + (buf[1] << 8);
        accelData[1] = buf[2] + (buf[3] << 8);
        accelData[2] = buf[4] + (buf[5] << 8);
    }

    return true;
}