2023-03-06 22:02:49 -04:00
|
|
|
#include "AP_BattMonitor_config.h"
|
|
|
|
|
|
|
|
#if AP_BATTERY_SMBUS_ENABLED
|
|
|
|
|
2017-04-19 16:15:24 -03:00
|
|
|
#include "AP_BattMonitor_SMBus.h"
|
|
|
|
|
|
|
|
#define AP_BATTMONITOR_SMBUS_PEC_POLYNOME 0x07 // Polynome for CRC generation
|
|
|
|
|
2021-06-18 18:16:06 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
const AP_Param::GroupInfo AP_BattMonitor_SMBus::var_info[] = {
|
|
|
|
|
|
|
|
// @Param: I2C_BUS
|
|
|
|
// @DisplayName: Battery monitor I2C bus number
|
|
|
|
// @Description: Battery monitor I2C bus number
|
|
|
|
// @Range: 0 3
|
|
|
|
// @User: Advanced
|
|
|
|
// @RebootRequired: True
|
2021-11-01 13:23:28 -03:00
|
|
|
AP_GROUPINFO("I2C_BUS", 10, AP_BattMonitor_SMBus, _bus, 0),
|
2021-06-18 18:16:06 -03:00
|
|
|
|
|
|
|
// @Param: I2C_ADDR
|
|
|
|
// @DisplayName: Battery monitor I2C address
|
|
|
|
// @Description: Battery monitor I2C address
|
|
|
|
// @Range: 0 127
|
|
|
|
// @User: Advanced
|
|
|
|
// @RebootRequired: True
|
2021-11-01 13:23:28 -03:00
|
|
|
AP_GROUPINFO("I2C_ADDR", 11, AP_BattMonitor_SMBus, _address, AP_BATTMONITOR_SMBUS_I2C_ADDR),
|
|
|
|
|
2024-12-13 13:00:43 -04:00
|
|
|
// CHECK/UPDATE INDEX TABLE IN AP_BattMonitor_Backend.cpp WHEN CHANGING OR ADDING PARAMETERS
|
2021-06-18 18:16:06 -03:00
|
|
|
|
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
2017-04-24 03:16:49 -03:00
|
|
|
AP_BattMonitor_SMBus::AP_BattMonitor_SMBus(AP_BattMonitor &mon,
|
|
|
|
AP_BattMonitor::BattMonitor_State &mon_state,
|
2017-10-27 02:36:49 -03:00
|
|
|
AP_BattMonitor_Params ¶ms,
|
2021-06-18 18:16:06 -03:00
|
|
|
uint8_t i2c_bus)
|
|
|
|
: AP_BattMonitor_Backend(mon, mon_state, params)
|
2017-04-24 03:16:49 -03:00
|
|
|
{
|
2021-06-18 18:16:06 -03:00
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
|
|
|
_state.var_info = var_info;
|
|
|
|
|
|
|
|
_bus.set_default(i2c_bus);
|
2022-07-05 00:08:56 -03:00
|
|
|
_params._serial_number.set(AP_BATT_SERIAL_NUMBER_DEFAULT);
|
|
|
|
_params._pack_capacity.set(0);
|
2017-04-24 03:16:49 -03:00
|
|
|
}
|
|
|
|
|
2019-12-10 20:55:20 -04:00
|
|
|
void AP_BattMonitor_SMBus::init(void)
|
|
|
|
{
|
2021-06-18 18:16:06 -03:00
|
|
|
_dev = hal.i2c_mgr->get_device(_bus, _address, 100000, true, 20);
|
|
|
|
|
2017-11-04 05:41:41 -03:00
|
|
|
if (_dev) {
|
2019-12-04 08:03:33 -04:00
|
|
|
timer_handle = _dev->register_periodic_callback(100000, FUNCTOR_BIND_MEMBER(&AP_BattMonitor_SMBus::timer, void));
|
2017-11-04 05:41:41 -03:00
|
|
|
}
|
2017-10-26 00:58:56 -03:00
|
|
|
}
|
|
|
|
|
2019-12-10 20:55:20 -04:00
|
|
|
// return true if cycle count can be provided and fills in cycles argument
|
|
|
|
bool AP_BattMonitor_SMBus::get_cycle_count(uint16_t &cycles) const
|
|
|
|
{
|
|
|
|
if (!_has_cycle_count) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cycles = _cycle_count;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-24 03:16:49 -03:00
|
|
|
/// read the battery_voltage and current, should be called at 10hz
|
|
|
|
void AP_BattMonitor_SMBus::read(void)
|
|
|
|
{
|
|
|
|
// nothing to be done here for actually interacting with the battery
|
|
|
|
// however we can use this to set any parameters that need to be set
|
|
|
|
|
2017-10-27 02:36:49 -03:00
|
|
|
if (_serial_number != _params._serial_number) {
|
|
|
|
_params._serial_number.set_and_notify(_serial_number);
|
2017-04-24 03:16:49 -03:00
|
|
|
}
|
2017-04-24 03:49:51 -03:00
|
|
|
|
2017-10-27 02:36:49 -03:00
|
|
|
if (_full_charge_capacity != _params._pack_capacity) {
|
|
|
|
_params._pack_capacity.set_and_notify(_full_charge_capacity);
|
2017-04-24 03:49:51 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reads the pack full charge capacity
|
2021-07-14 03:52:59 -03:00
|
|
|
// returns if we already knew the pack capacity
|
|
|
|
void AP_BattMonitor_SMBus::read_full_charge_capacity(void)
|
2017-04-24 03:49:51 -03:00
|
|
|
{
|
|
|
|
if (_full_charge_capacity != 0) {
|
2021-07-14 03:52:59 -03:00
|
|
|
return;
|
2021-07-12 17:49:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (read_word(BATTMONITOR_SMBUS_FULL_CHARGE_CAPACITY, _full_charge_capacity)) {
|
|
|
|
_full_charge_capacity *= get_capacity_scaler();
|
2017-04-24 03:49:51 -03:00
|
|
|
}
|
2017-04-24 03:16:49 -03:00
|
|
|
}
|
2017-04-19 17:49:38 -03:00
|
|
|
|
2017-05-16 01:34:08 -03:00
|
|
|
// reads the remaining capacity
|
2021-07-14 03:52:59 -03:00
|
|
|
// which will only be read if we know the full charge capacity (accounting for battery degradation)
|
|
|
|
void AP_BattMonitor_SMBus::read_remaining_capacity(void)
|
2017-05-16 01:34:08 -03:00
|
|
|
{
|
2017-10-27 02:36:49 -03:00
|
|
|
int32_t capacity = _params._pack_capacity;
|
2017-05-16 01:34:08 -03:00
|
|
|
|
2021-07-14 03:52:59 -03:00
|
|
|
if (capacity <= 0) {
|
|
|
|
return;
|
2017-05-16 01:34:08 -03:00
|
|
|
}
|
|
|
|
|
2021-07-14 03:52:59 -03:00
|
|
|
uint16_t data;
|
|
|
|
if (read_word(BATTMONITOR_SMBUS_REMAINING_CAPACITY, data)) {
|
|
|
|
_state.consumed_mah = MAX(0, capacity - (data * get_capacity_scaler()));
|
|
|
|
}
|
2017-05-16 01:34:08 -03:00
|
|
|
}
|
|
|
|
|
2017-04-19 17:49:38 -03:00
|
|
|
// reads the temperature word from the battery
|
2021-07-14 03:52:59 -03:00
|
|
|
void AP_BattMonitor_SMBus::read_temp(void)
|
2017-04-19 17:49:38 -03:00
|
|
|
{
|
|
|
|
uint16_t data;
|
2021-07-14 03:52:59 -03:00
|
|
|
if (!read_word(BATTMONITOR_SMBUS_TEMP, data)) {
|
2021-01-07 22:11:56 -04:00
|
|
|
_has_temperature = (AP_HAL::millis() - _state.temperature_time) <= AP_BATT_MONITOR_TIMEOUT;
|
2021-07-14 03:52:59 -03:00
|
|
|
return;
|
2017-04-19 17:49:38 -03:00
|
|
|
}
|
2021-07-14 03:52:59 -03:00
|
|
|
_has_temperature = true;
|
2017-04-19 17:49:38 -03:00
|
|
|
|
2021-07-14 03:52:59 -03:00
|
|
|
_state.temperature_time = AP_HAL::millis();
|
2022-01-12 08:03:24 -04:00
|
|
|
_state.temperature = KELVIN_TO_C(0.1f * data);
|
2017-04-19 17:49:38 -03:00
|
|
|
}
|
|
|
|
|
2017-04-24 03:16:49 -03:00
|
|
|
// reads the serial number if it's not already known
|
2021-07-14 03:52:59 -03:00
|
|
|
// returns if the serial number was already known
|
|
|
|
void AP_BattMonitor_SMBus::read_serial_number(void)
|
2019-12-10 20:55:20 -04:00
|
|
|
{
|
2017-04-24 03:16:49 -03:00
|
|
|
// don't recheck the serial number if we already have it
|
|
|
|
if (_serial_number != -1) {
|
2021-07-14 03:52:59 -03:00
|
|
|
return;
|
2017-04-24 03:16:49 -03:00
|
|
|
}
|
|
|
|
|
2021-07-14 03:52:59 -03:00
|
|
|
uint16_t data;
|
|
|
|
if (read_word(BATTMONITOR_SMBUS_SERIAL, data)) {
|
|
|
|
_serial_number = data;
|
|
|
|
}
|
2017-04-24 03:16:49 -03:00
|
|
|
}
|
|
|
|
|
2019-12-10 20:55:20 -04:00
|
|
|
// reads the battery's cycle count
|
|
|
|
void AP_BattMonitor_SMBus::read_cycle_count()
|
|
|
|
{
|
|
|
|
// only read cycle count once
|
|
|
|
if (_has_cycle_count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_has_cycle_count = read_word(BATTMONITOR_SMBUS_CYCLE_COUNT, _cycle_count);
|
|
|
|
}
|
|
|
|
|
2017-04-19 17:10:15 -03:00
|
|
|
// read word from register
|
|
|
|
// returns true if read was successful, false if failed
|
|
|
|
bool AP_BattMonitor_SMBus::read_word(uint8_t reg, uint16_t& data) const
|
|
|
|
{
|
|
|
|
// buffer to hold results (1 extra byte returned holding PEC)
|
|
|
|
const uint8_t read_size = 2 + (_pec_supported ? 1 : 0);
|
|
|
|
uint8_t buff[read_size]; // buffer to hold results
|
|
|
|
|
|
|
|
// read the appropriate register from the device
|
|
|
|
if (!_dev->read_registers(reg, buff, sizeof(buff))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check PEC
|
|
|
|
if (_pec_supported) {
|
2021-06-18 18:16:06 -03:00
|
|
|
const uint8_t pec = get_PEC(_address, reg, true, buff, 2);
|
2017-04-19 17:10:15 -03:00
|
|
|
if (pec != buff[2]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert buffer to word
|
|
|
|
data = (uint16_t)buff[1]<<8 | (uint16_t)buff[0];
|
|
|
|
|
|
|
|
// return success
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:12:26 -03:00
|
|
|
// read_block - returns number of characters read if successful, zero if unsuccessful
|
|
|
|
uint8_t AP_BattMonitor_SMBus::read_block(uint8_t reg, uint8_t* data, uint8_t len) const
|
|
|
|
{
|
|
|
|
// get length
|
|
|
|
uint8_t bufflen;
|
|
|
|
// read byte (first byte indicates the number of bytes in the block)
|
|
|
|
if (!_dev->read_registers(reg, &bufflen, 1)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sanity check length returned by smbus
|
|
|
|
if (bufflen == 0 || bufflen > len) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// buffer to hold results (2 extra byte returned holding length and PEC)
|
|
|
|
const uint8_t read_size = bufflen + 1 + (_pec_supported ? 1 : 0);
|
|
|
|
uint8_t buff[read_size];
|
|
|
|
|
|
|
|
// read bytes
|
|
|
|
if (!_dev->read_registers(reg, buff, read_size)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check PEC
|
|
|
|
if (_pec_supported) {
|
|
|
|
const uint8_t pec = get_PEC(_address, reg, true, buff, bufflen+1);
|
|
|
|
if (pec != buff[bufflen+1]) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy data (excluding length & PEC)
|
|
|
|
memcpy(data, &buff[1], bufflen);
|
|
|
|
|
|
|
|
// return success
|
|
|
|
return bufflen;
|
|
|
|
}
|
|
|
|
|
2017-04-19 16:15:24 -03:00
|
|
|
/// get_PEC - calculate packet error correction code of buffer
|
|
|
|
uint8_t AP_BattMonitor_SMBus::get_PEC(const uint8_t i2c_addr, uint8_t cmd, bool reading, const uint8_t buff[], uint8_t len) const
|
|
|
|
{
|
|
|
|
// exit immediately if no data
|
|
|
|
if (len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-10 19:30:47 -04:00
|
|
|
// prepare temp buffer for calculating crc
|
2017-04-19 16:15:24 -03:00
|
|
|
uint8_t tmp_buff[len+3];
|
|
|
|
tmp_buff[0] = i2c_addr << 1;
|
|
|
|
tmp_buff[1] = cmd;
|
|
|
|
tmp_buff[2] = tmp_buff[0] | (uint8_t)reading;
|
|
|
|
memcpy(&tmp_buff[3],buff,len);
|
|
|
|
|
|
|
|
// initialise crc to zero
|
|
|
|
uint8_t crc = 0;
|
|
|
|
uint8_t shift_reg = 0;
|
|
|
|
bool do_invert;
|
|
|
|
|
|
|
|
// for each byte in the stream
|
|
|
|
for (uint8_t i=0; i<sizeof(tmp_buff); i++) {
|
|
|
|
// load next data byte into the shift register
|
|
|
|
shift_reg = tmp_buff[i];
|
|
|
|
// for each bit in the current byte
|
|
|
|
for (uint8_t j=0; j<8; j++) {
|
|
|
|
do_invert = (crc ^ shift_reg) & 0x80;
|
|
|
|
crc <<= 1;
|
|
|
|
shift_reg <<= 1;
|
|
|
|
if(do_invert) {
|
|
|
|
crc ^= AP_BATTMONITOR_SMBUS_PEC_POLYNOME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return result
|
|
|
|
return crc;
|
|
|
|
}
|
2023-03-06 22:02:49 -04:00
|
|
|
|
|
|
|
#endif // AP_BATTERY_SMBUS_ENABLED
|