7 namespace sensirion_common {
9 static const char *
const TAG =
"sensirion_i2c";
11 static const size_t BUFFER_STACK_SIZE = 16;
14 const uint8_t num_bytes = len * 3;
15 std::vector<uint8_t> buf(num_bytes);
22 for (uint8_t i = 0; i <
len; i++) {
23 const uint8_t j = 3 * i;
25 if (crc != buf[j + 2]) {
26 ESP_LOGE(TAG,
"CRC8 Checksum invalid at pos %d! 0x%02X != 0x%02X", i, buf[j + 2], crc);
40 uint8_t temp_stack[BUFFER_STACK_SIZE];
41 std::unique_ptr<uint8_t[]> temp_heap;
43 size_t required_buffer_len = data_len * 3 + 2;
46 if (required_buffer_len >= BUFFER_STACK_SIZE) {
47 temp_heap = std::unique_ptr<uint8_t[]>(
new uint8_t[required_buffer_len]);
48 temp = temp_heap.get();
54 if (command_len == 1) {
55 temp[raw_idx++] = command & 0xFF;
58 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 59 temp[raw_idx++] = command >> 8;
60 temp[raw_idx++] = command & 0xFF;
62 temp[raw_idx++] = command & 0xFF;
63 temp[raw_idx++] = command >> 8;
68 for (
size_t i = 0; i < data_len; i++) {
69 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 70 temp[raw_idx++] = data[i] >> 8;
71 temp[raw_idx++] = data[i] & 0xFF;
73 temp[raw_idx++] = data[i] & 0xFF;
74 temp[raw_idx++] = data[i] >> 8;
85 ESP_LOGE(TAG,
"Failed to write i2c register=0x%X (%d) err=%d,", reg, command_len, this->
last_error_);
91 ESP_LOGE(TAG,
"Failed to read data from register=0x%X err=%d,", reg, this->
last_error_);
100 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 105 for (bit = 8; bit > 0; --bit) {
112 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 117 for (bit = 8; bit > 0; --bit) {
bool get_register_(uint16_t reg, CommandLen command_len, uint16_t *data, uint8_t len, uint8_t delay)
get data words from i2c register.
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
bool write_command_(uint16_t command, CommandLen command_len, const uint16_t *data, uint8_t data_len)
Write a command with arguments as words.
bool read_data(uint16_t *data, uint8_t len)
Read data words from i2c device.
uint8_t sht_crc_(uint16_t data)
8-bit CRC checksum that is transmitted after each data word for read and write operation ...
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
No error found during execution of method.
bytes received with a CRC error
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
i2c::ErrorCode last_error_
last error code from i2c operation
Implementation of SPI Controller mode.
void IRAM_ATTR HOT delay(uint32_t ms)