AP_BattMonitor: auto-probe INA2XX address if address is zero

this makes life easier for users who don't know which device they have
This commit is contained in:
Andrew Tridgell 2023-05-01 16:06:31 +10:00 committed by Randy Mackay
parent f54d851364
commit b8b9f82084
2 changed files with 12 additions and 1 deletions

View File

@ -52,6 +52,9 @@ extern const AP_HAL::HAL& hal;
#define HAL_BATTMON_INA2XX_ADDR 0 #define HAL_BATTMON_INA2XX_ADDR 0
#endif #endif
// list of addresses to probe if I2C_ADDR is zero
const uint8_t AP_BattMonitor_INA2XX::i2c_probe_addresses[] { 0x41, 0x44, 0x45 };
const AP_Param::GroupInfo AP_BattMonitor_INA2XX::var_info[] = { const AP_Param::GroupInfo AP_BattMonitor_INA2XX::var_info[] = {
// @Param: I2C_BUS // @Param: I2C_BUS
@ -64,7 +67,7 @@ const AP_Param::GroupInfo AP_BattMonitor_INA2XX::var_info[] = {
// @Param: I2C_ADDR // @Param: I2C_ADDR
// @DisplayName: Battery monitor I2C address // @DisplayName: Battery monitor I2C address
// @Description: Battery monitor I2C address // @Description: Battery monitor I2C address. If this is zero then probe list of supported addresses
// @Range: 0 127 // @Range: 0 127
// @User: Advanced // @User: Advanced
// @RebootRequired: True // @RebootRequired: True
@ -234,6 +237,11 @@ bool AP_BattMonitor_INA2XX::detect_device(void)
WITH_SEMAPHORE(dev->get_semaphore()); WITH_SEMAPHORE(dev->get_semaphore());
if (i2c_address.get() == 0) {
dev->set_address(i2c_probe_addresses[i2c_probe_next]);
i2c_probe_next = (i2c_probe_next+1) % sizeof(i2c_probe_addresses);
}
if (read_word16(REG_228_MANUFACT_ID, id) && id == 0x5449 && if (read_word16(REG_228_MANUFACT_ID, id) && id == 0x5449 &&
read_word16(REG_228_DEVICE_ID, id) && (id&0xFFF0) == 0x2280) { read_word16(REG_228_DEVICE_ID, id) && (id&0xFFF0) == 0x2280) {
return configure(DevType::INA228); return configure(DevType::INA228);

View File

@ -37,6 +37,9 @@ private:
INA238, INA238,
}; };
static const uint8_t i2c_probe_addresses[];
uint8_t i2c_probe_next;
bool configure(DevType dtype); bool configure(DevType dtype);
bool read_word16(const uint8_t reg, int16_t& data) const; bool read_word16(const uint8_t reg, int16_t& data) const;
bool read_word24(const uint8_t reg, int32_t& data) const; bool read_word24(const uint8_t reg, int32_t& data) const;