Exemplo n.º 1
0
error_t pca9685ReadBytes(uint8_t reg, uint8_t *buffer, size_t length)
{
  uint32_t i;

  /* Try to avoid buffer overflow */
  ASSERT(length <= I2C_BUFSIZE, ERROR_BUFFEROVERFLOW);

  /* Read and write need to be handled in separate transactions or the
     PCA9685 increments the current register one step ahead of where
     we should be. */

  /* Write transaction */
  I2CWriteLength = 2;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = _pca9685Address;
  I2CMasterBuffer[1] = reg;
  i2cEngine();

  /* Read transaction */
  I2CWriteLength = 0;
  I2CReadLength = length;
  I2CMasterBuffer[0] = _pca9685Address | PCA9685_READBIT;
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  /* Fill the buffer with the I2C response */
  for ( i = 0; i < length; i++ )
  {
    buffer[i] = I2CSlaveBuffer[i];
  }

  return ERROR_NONE;
}
Exemplo n.º 2
0
err_t adxl345GetXYZ(int16_t *x, int16_t *y, int16_t *z)
{
  int32_t i2cState;

  if (!_adxl345Initialised)
  {
    ASSERT_STATUS(adxl345Init());
  }

  I2CWriteLength = 2;
  I2CReadLength = 6;
  I2CMasterBuffer[0] = ADXL345_ADDRESS;
  I2CMasterBuffer[1] = ADXL345_REG_DATAX0;
  I2CMasterBuffer[2] = ADXL345_ADDRESS | ADXL345_READBIT;
  i2cState = i2cEngine();

  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cState);

  /* Shift values to create properly formed integer */
  *x = (I2CSlaveBuffer[1] << 8) | (I2CSlaveBuffer[0]);
  *y = (I2CSlaveBuffer[3] << 8) | (I2CSlaveBuffer[2]);
  *z = (I2CSlaveBuffer[5] << 8) | (I2CSlaveBuffer[4]);

  return ERROR_NONE;
}
Exemplo n.º 3
0
error_t mpl115a2ReadPressureTemp(uint16_t *pressure, uint16_t *temp)
{
  I2CWriteLength = 3;
  I2CReadLength = 1;
  I2CMasterBuffer[0] = MPL115A2_ADDRESS;
  I2CMasterBuffer[1] = MPL115A2_REGISTER_STARTCONVERSION;
  I2CMasterBuffer[2] = 0x00;  // Why is this necessary to get results?
  i2cEngine();

  /* Wait a bit for the conversion to complete (3ms max) */
  delay(4);

  I2CWriteLength = 2;
  I2CReadLength = 4;
  I2CMasterBuffer[0] = MPL115A2_ADDRESS;
  I2CMasterBuffer[1] = MPL115A2_REGISTER_PRESSURE_MSB;
  I2CMasterBuffer[2] = MPL115A2_ADDRESS | MPL115A2_READBIT;
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  /* Shift values to create properly formed integers */
  *pressure = ((I2CSlaveBuffer[0] << 8) | (I2CSlaveBuffer[1])) >> 6;
  *temp = ((I2CSlaveBuffer[2] << 8) | (I2CSlaveBuffer[3])) >> 6;

  return ERROR_NONE;
}
Exemplo n.º 4
0
error_t mpl115a2ReadCoefficients(void)
{
  int16_t a0coeff;
  int16_t b1coeff;
  int16_t b2coeff;
  int16_t c12coeff;

  I2CWriteLength = 2;
  I2CReadLength = 8;
  I2CMasterBuffer[0] = MPL115A2_ADDRESS;
  I2CMasterBuffer[1] = MPL115A2_REGISTER_A0_COEFF_MSB;
  I2CMasterBuffer[2] = MPL115A2_ADDRESS | MPL115A2_READBIT;
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  a0coeff = (I2CSlaveBuffer[0] << 8 ) | I2CSlaveBuffer[1];
  b1coeff = (I2CSlaveBuffer[2] << 8 ) | I2CSlaveBuffer[3];
  b2coeff = (I2CSlaveBuffer[4] << 8 ) | I2CSlaveBuffer[5];
  c12coeff = ((I2CSlaveBuffer[6] << 8 ) | I2CSlaveBuffer[7]) >> 2;

  _mpl115a2_a0 = (float)a0coeff / 8;
  _mpl115a2_b1 = (float)b1coeff / 8192;
  _mpl115a2_b2 = (float)b2coeff / 16384;
  _mpl115a2_c12 = (float)c12coeff / 4194304;

  return ERROR_NONE;
}
Exemplo n.º 5
0
err_t tsl2561WriteCmd (uint8_t cmd)
{
  I2CWriteLength = 2;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = TSL2561_ADDRESS;
  I2CMasterBuffer[1] = cmd;
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  return ERROR_NONE;
}
Exemplo n.º 6
0
error_t lsm303accelWrite8(uint8_t addr, uint8_t reg, uint8_t value)
{
  I2CWriteLength = 3;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = addr;
  I2CMasterBuffer[1] = reg;
  I2CMasterBuffer[2] = (value);
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  return ERROR_NONE;
}
Exemplo n.º 7
0
static err_t adxl345Write8 (uint8_t reg, uint8_t value)
{
  I2CWriteLength = 3;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = ADXL345_ADDRESS;
  I2CMasterBuffer[1] = reg;
  I2CMasterBuffer[2] = value;
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  return ERROR_NONE;
}
Exemplo n.º 8
0
err_t tsl2561Write8 (uint8_t reg, uint32_t value)
{
  I2CWriteLength = 3;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = TSL2561_ADDRESS;
  I2CMasterBuffer[1] = reg;
  I2CMasterBuffer[2] = (value & 0xFF);
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  return ERROR_NONE;
}
Exemplo n.º 9
0
error_t tcs34725Write8 (uint8_t reg, uint8_t value)
{
  I2CWriteLength = 3;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = TCS34725_ADDRESS;
  I2CMasterBuffer[1] = TCS34725_COMMAND_BIT | reg;
  I2CMasterBuffer[2] = value;
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  return ERROR_NONE;
}
Exemplo n.º 10
0
error_t lm75bWrite8 (uint8_t reg, uint32_t value)
{
  I2CWriteLength = 3;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = LM75B_ADDRESS;             // I2C device address
  I2CMasterBuffer[1] = reg;                       // Command register
  I2CMasterBuffer[2] = (value & 0xFF);            // Value to write
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  return ERROR_NONE;
}
Exemplo n.º 11
0
error_t cmd_i2cWriteWrapper(uint8_t addr, uint8_t len, uint8_t* values)
{
  uint8_t i;

  I2CWriteLength = len;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = (addr << 1);
  /* Copy payload into the buffer */
  for (i=0;i<len-1;i++)
  {
    I2CMasterBuffer[i+1] = values[i];
  }
  ASSERT_I2C_STATUS(i2cEngine());

  return ERROR_NONE;
}
Exemplo n.º 12
0
static err_t adxl345Read8(uint8_t reg, uint8_t *value)
{
  I2CWriteLength = 2;
  I2CReadLength = 1;
  I2CMasterBuffer[0] = ADXL345_ADDRESS;
  I2CMasterBuffer[1] = reg;
  /* Append address w/read bit */
  I2CMasterBuffer[2] = ADXL345_ADDRESS | ADXL345_READBIT;
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  /* Shift values to create properly formed integer */
  *value = I2CSlaveBuffer[0];

  return ERROR_NONE;
}
Exemplo n.º 13
0
error_t tcs34725Read8(uint8_t reg, uint8_t *value)
{
  /* Write transaction */
  I2CWriteLength = 2;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = TCS34725_ADDRESS;
  I2CMasterBuffer[1] = TCS34725_COMMAND_BIT | reg;
  i2cEngine();

  /* Read transaction */
  I2CWriteLength = 0;
  I2CReadLength = 1;
  I2CMasterBuffer[0] = TCS34725_ADDRESS | TCS34725_READBIT;
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  *value = I2CSlaveBuffer[0];

  return ERROR_NONE;
}
Exemplo n.º 14
0
error_t tcs34725Read16(uint8_t reg, uint16_t *value)
{
  /* Write transaction */
  I2CWriteLength = 2;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = TCS34725_ADDRESS;
  I2CMasterBuffer[1] = TCS34725_COMMAND_BIT | reg;
  i2cEngine();

  /* Read transaction */
  I2CWriteLength = 0;
  I2CReadLength = 2;
  I2CMasterBuffer[0] = TCS34725_ADDRESS | TCS34725_READBIT;
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  /* Shift values to create properly formed integer (low byte first) */
  *value = (I2CSlaveBuffer[0] | (I2CSlaveBuffer[1] << 8));

  return ERROR_NONE;
}
Exemplo n.º 15
0
error_t lsm303accelRead8(uint8_t addr, uint8_t reg, uint8_t *value)
{
  /* Write transaction */
  I2CWriteLength = 2;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = addr;
  I2CMasterBuffer[1] = reg;
  i2cEngine();

  /* Read transaction */
  I2CWriteLength = 0;
  I2CReadLength = 1;
  I2CMasterBuffer[0] = addr | 0x01;
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  /* Send received value back to the caller */
  *value = I2CSlaveBuffer[0];

  return ERROR_NONE;
}
Exemplo n.º 16
0
error_t lm75bRead16(uint8_t reg, int32_t *value)
{
  I2CWriteLength = 2;
  I2CReadLength = 2;
  I2CMasterBuffer[0] = LM75B_ADDRESS;
  I2CMasterBuffer[1] = reg;
  I2CMasterBuffer[2] = LM75B_ADDRESS | LM75B_READBIT;
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  // Shift values to create properly formed integer
  *value = ((I2CSlaveBuffer[0] << 8) | I2CSlaveBuffer[1]) >> 5;

  //  Sign extend negative numbers
  if (I2CSlaveBuffer[0] & 0x80)
  {
    // Negative number
    *value |= 0xFFFFFC00;
  }

  return ERROR_NONE;
}
Exemplo n.º 17
0
error_t pca9685WriteBytes(uint8_t reg, uint8_t *buffer, size_t length)
{
  uint32_t i;

  /* Try to avoid buffer overflow */
  ASSERT(length <= I2C_BUFSIZE - 2, ERROR_BUFFEROVERFLOW);

  /* Fill write buffer */
  for ( i = 2; i < length+2; i++ )
  {
    I2CMasterBuffer[i] = buffer[i-2];
  }

  /* Write transaction */
  I2CWriteLength = 2+length;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = _pca9685Address;
  I2CMasterBuffer[1] = reg;
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  return ERROR_NONE;
}
Exemplo n.º 18
0
error_t lsm303accelRead48(uint8_t addr, uint8_t reg, uint8_t buffer[6])
{
  /* Write transaction */
  I2CWriteLength = 2;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = addr;
  I2CMasterBuffer[1] = reg | (0x80);
  i2cEngine();

  /* Read transaction */
  I2CWriteLength = 0;
  I2CReadLength = 6;
  I2CMasterBuffer[0] = addr | 0x01;
  /* Check if we got an ACK or TIMEOUT error */
  ASSERT_I2C_STATUS(i2cEngine());

  uint8_t i;
  for (i = 0; i < 6; i++)
  {
    buffer[i] = I2CSlaveBuffer[i];
  }

  return ERROR_NONE;
}