8namespace sensirion_common {
10static const char *
const TAG =
"sensirion_i2c";
12static const size_t BUFFER_STACK_SIZE = 16;
15 const size_t required_buffer_len =
len * 3;
17 uint8_t *temp = buffer.
get();
24 for (
size_t i = 0; i <
len; i++) {
25 const size_t j = i * 3;
27 uint8_t crc =
crc8(&temp[j], 2, 0xFF, CRC_POLYNOMIAL,
true);
28 if (crc != temp[j + 2]) {
29 ESP_LOGE(TAG,
"CRC invalid @ %zu! 0x%02X != 0x%02X", i, temp[j + 2], crc);
42 const uint8_t data_len) {
43 size_t required_buffer_len = data_len * 3 + 2;
45 uint8_t *temp = buffer.
get();
48 if (command_len == 1) {
49 temp[raw_idx++] = command & 0xFF;
52 temp[raw_idx++] = command >> 8;
53 temp[raw_idx++] = command & 0xFF;
57 for (
size_t i = 0; i < data_len; i++) {
58 temp[raw_idx++] = data[i] >> 8;
59 temp[raw_idx++] = data[i] & 0xFF;
61 uint8_t crc =
crc8(&temp[raw_idx - 2], 2, 0xFF, CRC_POLYNOMIAL,
true);
62 temp[raw_idx++] = crc;
69 const uint8_t delay_ms) {
71 ESP_LOGE(TAG,
"Write failed: reg=0x%X (%d) err=%d,",
reg, command_len, this->
last_error_);
77 ESP_LOGE(TAG,
"Read failed: reg=0x%X err=%d,",
reg, this->
last_error_);
Helper class for efficient buffer allocation - uses stack for small sizes, heap for large This is use...
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
bool get_register_(uint16_t reg, CommandLen command_len, uint16_t *data, uint8_t len, uint8_t delay)
get data words from I2C register.
i2c::ErrorCode last_error_
last error code from I2C operation
bool read_data(uint16_t *data, uint8_t len)
Read data words from I2C device.
bool write_command_(uint16_t command, CommandLen command_len, const uint16_t *data, uint8_t data_len)
Write a command with arguments as words.
@ ERROR_CRC
bytes received with a CRC error
@ ERROR_OK
No error found during execution of method.
Providing packet encoding functions for exchanging data with a remote host.
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void HOT delay(uint32_t ms)