49 I2C(
"BATT_SMBUS_I2C", nullptr, bus_num, address, 100000)
57 int result = transfer(&cmd_code, 1, buf + 3, 3);
59 if (result == PX4_OK) {
60 data = buf[3] | ((uint16_t)buf[4] << 8);
62 uint8_t addr = get_device_address() << 1;
67 uint8_t pec =
get_pec(buf,
sizeof(buf) - 1);
69 if (pec != buf[
sizeof(buf) - 1]) {
81 buf[0] = (get_device_address() << 1) | 0x10;
84 buf[3] = (data >> 8) & 0xff;
88 int result = transfer(&buf[1], 4,
nullptr, 0);
95 unsigned byte_count = 0;
97 uint8_t rx_data[32 + 5];
99 int result = transfer(&cmd_code, 1, (uint8_t *)&rx_data[3], length + 2);
101 uint8_t device_address = get_device_address();
102 rx_data[0] = (device_address << 1) | 0x00;
103 rx_data[1] = cmd_code;
104 rx_data[2] = (device_address << 1) | 0x01;
105 byte_count = rx_data[3];
107 memcpy(data, &rx_data[4], byte_count);
110 uint8_t pec =
get_pec(rx_data, byte_count + 4);
112 if (pec != rx_data[byte_count + 4]) {
126 buf[1] = (uint8_t)byte_count;
127 memcpy(&buf[2], data, byte_count);
130 uint8_t pec =
get_pec(buf, byte_count + 2);
131 buf[byte_count + 2] = pec;
139 while (i < 10 && ((result = transfer((uint8_t *)buf, byte_count + 2,
nullptr, 0)) != PX4_OK)) {
143 if (i == 10 || result) {
144 PX4_WARN(
"Block_write failed %d times", i);
155 uint8_t shift_register = 0;
159 for (uint8_t i = 0; i < len; i++) {
161 shift_register = buff[i];
164 for (uint8_t j = 0; j < 8; j++) {
165 invert_crc = (crc ^ shift_register) & 0x80;
167 shift_register <<= 1;
int block_read(const uint8_t cmd_code, void *data, const uint8_t length, bool use_pec)
Sends a block read command.
SMBus(int bus_num, uint16_t address)
int block_write(const uint8_t cmd_code, void *data, uint8_t byte_count, bool use_pec)
Sends a block write command.
int read_word(const uint8_t cmd_code, uint16_t &data)
Sends a read word command.
int write_word(const uint8_t cmd_code, uint16_t data)
Sends a write word command.
SMBus v2.0 protocol implementation.
uint8_t get_pec(uint8_t *buffer, uint8_t length)
Calculates the PEC from the data.
#define SMBUS_PEC_POLYNOMIAL
Polynomial for calculating PEC.