bool read_registers_uint16_le(uint8_t addr, uint8_t reg, uint16_t* data, uint8_t size) { QASSERT(s_is_initialized); QASSERT(s_is_locked); QASSERT(data && size > 0); if (!data || size == 0) { return 0; } uint8_t* data_ptr8 = reinterpret_cast<uint8_t*>(data) + 1; //the data_ptr8 will advance like this //1, 0, 3, 2, 5, 4, 7, 6, 9, 8 if (!_start()) goto error; if (!_send_address(SLA_W(addr))) goto error; if (!_send_byte(reg)) goto error; if (!_start()) goto error; if (!_send_address(SLA_R(addr))) goto error; { uint8_t i = size - 1; while (i > 0) { if (!_receive_byte_ack()) goto error; *data_ptr8 = TWDR; data_ptr8 -= 1; if (!_receive_byte_ack()) goto error; *data_ptr8 = TWDR; data_ptr8 += 3; --i; } } //last bytes { if (!_receive_byte_ack()) goto error; *data_ptr8 = TWDR; data_ptr8 -= 1; if (!_receive_byte()) goto error; *data_ptr8 = TWDR; //data_ptr8 += 3; } if (!_stop()) goto error; return true; error: s_lockup_count++; return false; }
bool read_registers(uint8_t addr, uint8_t reg, uint8_t* data, uint8_t size) { QASSERT(s_is_initialized); QASSERT(s_is_locked); QASSERT(size > 0); if (size == 0) { return 0; } if (!_start()) goto error; if (!_send_address(SLA_W(addr))) goto error; if (!_send_byte(reg)) goto error; if (!_start()) goto error; if (!_send_address(SLA_R(addr))) goto error; if (data) { uint8_t i = size - 1; while (i > 0) { if (!_receive_byte_ack()) goto error; *data++ = TWDR; --i; } //last item { if (!_receive_byte()) goto error; *data = TWDR; } } else { uint8_t i = size - 1; while (i > 0) { if (!_receive_byte_ack()) goto error; s_i2c_null_sink = TWDR; --i; } //last item { if (!_receive_byte()) goto error; s_i2c_null_sink = TWDR; } } if (!_stop()) goto error; return true; error: s_lockup_count++; return false; }
uint8_t I2C_Write_Bytes(I2C_t device, uint8_t address, uint8_t *data, uint8_t length) { _send_start(device.port); _send_address(device.port, address, 0); int i = 0; for (; i < length; i++) { _send_byte(device.port, data[i]); } return i; }
uint8_t I2C_Read_Bytes(I2C_t device, uint8_t address, uint8_t *data, uint8_t length) { _send_start(device.port); _send_address(device.port, address, 0x01); int i = 0; for (; i < length; i++) { data[i] = _recv_byte(device.port); } return i; }
bool write(uint8_t addr, const uint8_t* data, uint8_t size) { QASSERT(s_is_initialized); QASSERT(s_is_locked); QASSERT(data && size > 0); if (!_start()) goto error; if (!_send_address(SLA_W(addr))) goto error; for (int8_t i = size; i > 0; i--) { if (!_send_byte(*data++)) goto error; } if (!_stop()) goto error; return true; error: s_lockup_count++; return false; }
bool read(uint8_t addr, uint8_t* data, uint8_t size) { QASSERT(s_is_initialized); QASSERT(s_is_locked); QASSERT(size > 0); if (size == 0) { return 0; } if (!_start()) goto error; if (!_send_address(SLA_R(addr))) goto error; if (data) { for (int8_t i = size - 1; i > 0; i--) //write size - 1 butes { if (!_receive_byte_ack()) goto error; *data++ = TWDR; } //last item { if (!_receive_byte()) goto error; *data = TWDR; } } else { for (int8_t i = size - 1; i > 0; i--) //write size - 1 butes { if (!_receive_byte_ack()) goto error; s_i2c_null_sink = TWDR; } //last item { if (!_receive_byte()) goto error; s_i2c_null_sink = TWDR; } } if (!_stop()) goto error; return true; error: s_lockup_count++; return false; }
uint8_t I2C_Write_Byte(I2C_t device, uint8_t address, uint8_t data) { _send_start(device.port); _send_address(device.port, address, 0); return _send_byte(device.port, data); }
uint8_t I2C_Read_Byte(I2C_t device, uint8_t address) { _send_start(device.port); _send_address(device.port, address, 0x01); return _recv_byte(device.port); }