52 lines
1.6 KiB
C
52 lines
1.6 KiB
C
#include "esp_log.h"
|
|
|
|
#include "user_board.h"
|
|
|
|
#include "app_lib_bat_impl.h"
|
|
|
|
#define LTC2941_ADDR 0x64 // Unshifted
|
|
|
|
static const char *TAG = "BAT_IMPL";
|
|
|
|
ltc2941_ret_t app_lib_bat_read_register(app_lib_bat_impl_t *impl, uint8_t reg, uint8_t *value) {
|
|
ESP_LOGD(TAG, "Read register: %d", reg);
|
|
|
|
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
|
i2c_master_start(cmd);
|
|
i2c_master_write_byte(cmd, LTC2941_ADDR << 1 | I2C_MASTER_WRITE, true);
|
|
i2c_master_write_byte(cmd, reg, true);
|
|
i2c_master_start(cmd);
|
|
i2c_master_write_byte(cmd, LTC2941_ADDR << 1 | I2C_MASTER_READ, true);
|
|
i2c_master_read_byte(cmd, value, I2C_MASTER_LAST_NACK);
|
|
i2c_master_stop(cmd);
|
|
|
|
esp_err_t err = i2c_master_cmd_begin(impl->i2c_num, cmd, pdMS_TO_TICKS(1000));
|
|
i2c_cmd_link_delete(cmd);
|
|
|
|
if(err != ESP_OK) {
|
|
ESP_LOGE(TAG, "Read register reported error, %s", esp_err_to_name(err));
|
|
return LTC2941_ERROR;
|
|
}
|
|
|
|
return LTC2941_OK;
|
|
}
|
|
ltc2941_ret_t app_lib_bat_write_register(app_lib_bat_impl_t *impl, uint8_t reg, uint8_t value) {
|
|
ESP_LOGD(TAG, "Write register: %d", reg);
|
|
|
|
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
|
i2c_master_start(cmd);
|
|
i2c_master_write_byte(cmd, LTC2941_ADDR << 1 | I2C_MASTER_WRITE, true);
|
|
i2c_master_write_byte(cmd, reg, true);
|
|
i2c_master_write_byte(cmd, value, true);
|
|
i2c_master_stop(cmd);
|
|
|
|
esp_err_t err = i2c_master_cmd_begin(impl->i2c_num, cmd, pdMS_TO_TICKS(1000));
|
|
i2c_cmd_link_delete(cmd);
|
|
|
|
if(err != ESP_OK) {
|
|
ESP_LOGE(TAG, "Write register reported error, %s", esp_err_to_name(err));
|
|
return LTC2941_ERROR;
|
|
}
|
|
|
|
return LTC2941_OK;
|
|
} |