Battery capacity options added (#3125)

This commit is contained in:
Totoo
2026-04-03 22:19:17 +02:00
committed by GitHub
parent 888634ce35
commit fba15c48e5
8 changed files with 133 additions and 49 deletions
+14 -6
View File
@@ -214,6 +214,14 @@ bool I2cDev_MAX17055::init(uint8_t addr_) {
return false;
}
bool I2cDev_MAX17055::reInit() {
if (!full_reset_and_init()) {
return false;
}
partialInit();
return true;
}
bool I2cDev_MAX17055::full_reset_and_init() {
if (!soft_reset()) {
return false;
@@ -235,12 +243,12 @@ bool I2cDev_MAX17055::soft_reset() {
}
bool I2cDev_MAX17055::initialize_custom_parameters() {
if (!write_register(0xD0, 0x03E8)) return false; // Unknown register, possibly related to battery profile
if (!write_register(0xDB, 0x0000)) return false; // ModelCfg
if (!write_register(0x05, 0x0000)) return false; // RepCap
uint32_t designcap = portapack::device_type == portapack::DEV_PORTARF ? __MAX17055_Design_Capacity_PRF__ * 2 : __MAX17055_Design_Capacity__ * 2; // the original design has a 2x multiplier here, so i keep it
if (!write_register(0x18, designcap)) return false; // DesignCap
if (!write_register(0x45, designcap / 32)) return false; // dQAcc = DesignCap / 32
if (!write_register(0xD0, 0x03E8)) return false; // Unknown register, possibly related to battery profile
if (!write_register(0xDB, 0x0000)) return false; // ModelCfg
if (!write_register(0x05, 0x0000)) return false; // RepCap
uint32_t designcap = portapack::persistent_memory::battery_cap_mah() * 2; // the original design has a 2x multiplier here, so i keep it
if (!write_register(0x18, designcap)) return false; // DesignCap
if (!write_register(0x45, designcap / 32)) return false; // dQAcc = DesignCap / 32
if (!write_register(0x1E, 0x03C0)) return false; // IChgTerm
if (!write_register(0x3A, 0x9661)) return false; // VEmpty
+1 -1
View File
@@ -290,7 +290,7 @@ class I2cDev_MAX17055 : public I2cDev {
uint16_t averageMVoltage(void);
int32_t instantCurrent(void);
uint16_t stateOfCharge(void);
bool reInit(); // call when battery parameters changed from ui. don't call if not needed, or the battery is not changed!!!
private:
const RegisterEntry* findEntry(const char* name) const;
@@ -156,8 +156,7 @@ struct misc_config_t {
bool tx_amp_disabled : 1;
uint8_t tx_gain_max_db;
uint8_t PLACEHOLDER_1;
uint8_t PLACEHOLDER_2;
uint16_t batt_cap_mah;
};
static_assert(sizeof(misc_config_t) == sizeof(uint32_t));
@@ -445,6 +444,8 @@ void defaults() {
set_config_tx_disabled(false);
set_config_tx_amp_disabled(false);
set_config_tx_gain_max_db(47);
set_battery_cap_mah(0);
}
void init() {
@@ -1243,6 +1244,32 @@ int load_persistent_settings_from_file() {
return true;
}
bool battery_cap_valid() {
return (data->misc_config.batt_cap_mah >= BATT_18650_MIN_MAH && data->misc_config.batt_cap_mah <= BATT_18650_MAX_MAH);
}
void set_battery_cap_mah(uint16_t mah) {
if ((mah < BATT_18650_MIN_MAH || mah > BATT_18650_MAX_MAH) && mah != 0) {
// Invalid value, ignore it.
return;
}
if (data->misc_config.batt_cap_mah != mah) {
data->misc_config.batt_cap_mah = mah;
}
}
uint32_t battery_cap_mah() {
if (battery_cap_valid()) {
return data->misc_config.batt_cap_mah;
}
// we don't know, need to assume.
if (portapack::device_type == portapack::DEV_PORTARF) return 3000;
#ifdef PRALINE
return 2000; // with h4 + pro and h4pro + pro it is ~safe
#endif
return 2500; // h4 + one
}
// Pmem size helper
size_t data_size() {
@@ -45,6 +45,10 @@
#define PMEM_SIZE_BYTES 256 // total amount of pmem space in bytes, including checksum
#define PMEM_SIZE_WORDS (PMEM_SIZE_BYTES / 4)
// to check battery mins and maxes in pmem, and validity
#define BATT_18650_MIN_MAH 1000
#define BATT_18650_MAX_MAH 5000
using namespace modems;
using namespace serializer;
using namespace ui;
@@ -387,6 +391,11 @@ uint32_t pmem_data_word(uint32_t index);
uint32_t pmem_stored_checksum(void);
uint32_t pmem_calculated_checksum(void);
// battery capacity settings
void set_battery_cap_mah(uint16_t mah); // 0 is not known; use assumed default based on device type/build config
uint32_t battery_cap_mah();
bool battery_cap_valid();
size_t data_size();
} /* namespace persistent_memory */