int i2c_read_bytes(i2c_t dev, uint8_t address, char *data, int length) { I2C_Type *i2c; int n = 0; switch (dev) { #if I2C_0_EN case I2C_0: i2c = I2C_0_DEV; break; #endif default: return -1; } if (_i2c_start(i2c, address, I2C_FLAG_READ)) { _i2c_reset(i2c); return -1; } n = _i2c_receive(i2c, (uint8_t *)data, length); if (n < 0) { _i2c_reset(i2c); return -1; } _i2c_stop(i2c); return n; }
int i2c_write_regs(i2c_t dev, uint8_t address, uint8_t reg, char *data, int length) { I2C_Type *i2c; int n = 0; switch (dev) { #if I2C_0_EN case I2C_0: i2c = I2C_0_DEV; break; #endif default: return -1; } if (_i2c_start(i2c, address, I2C_FLAG_WRITE)) { _i2c_reset(i2c); return -1; } n = _i2c_transmit(i2c, ®, 1); if (!n) { _i2c_stop(i2c); return n; } n = _i2c_transmit(i2c, (uint8_t *)data, length); _i2c_stop(i2c); return n; }
int i2c_write_bytes(i2c_t dev, uint8_t address, const void *data, int length) { I2C_Type *i2c; int n = 0; switch (dev) { #if I2C_0_EN case I2C_0: i2c = I2C_0_DEV; break; #endif default: return -1; } if (_i2c_start(i2c, address, I2C_FLAG_WRITE)) { _i2c_reset(i2c); return -1; } n = _i2c_transmit(i2c, data, length); _i2c_stop(i2c); return n; }
int i2c_read_regs(i2c_t dev, uint8_t address, uint8_t reg, void *data, int length) { I2C_Type *i2c; int n = 0; switch (dev) { #if I2C_0_EN case I2C_0: i2c = I2C_0_DEV; break; #endif default: return -1; } if (_i2c_start(i2c, address, I2C_FLAG_WRITE)) { _i2c_reset(i2c); return -1; } /* send reg */ n = _i2c_transmit(i2c, ®, 1); if (!n) { _i2c_stop(i2c); return n; } if (_i2c_restart(i2c, address, I2C_FLAG_READ)) { _i2c_reset(i2c); return -1; } n = _i2c_receive(i2c, (uint8_t *)data, length); if (n < 0) { _i2c_reset(i2c); return -1; } _i2c_stop(i2c); return n; }