Skip to content

Commit

Permalink
Max17055 (#2194)
Browse files Browse the repository at this point in the history
 max17055
  • Loading branch information
jLynx committed Jul 15, 2024
1 parent 9ef58b7 commit ae75540
Show file tree
Hide file tree
Showing 10 changed files with 1,122 additions and 18 deletions.
1 change: 1 addition & 0 deletions firmware/application/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ set(CPPSRC
${COMMON}/utility.cpp
${COMMON}/wm8731.cpp
${COMMON}/ads1110.cpp
${COMMON}/max17055.cpp
${COMMON}/battery.cpp
${COMMON}/performance_counter.cpp
${COMMON}/bmpfile.cpp
Expand Down
8 changes: 4 additions & 4 deletions firmware/application/apps/ui_battinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void BattinfoView::update_result() {
return;
}
bool uichg = false;
battery::BatteryManagement::getBatteryInfo(percent, voltage, current, isCharging);
battery::BatteryManagement::getBatteryInfo(percent, voltage, current);
// update text fields
if (percent <= 100)
text_percent.set(to_string_dec_uint(percent) + " %");
Expand All @@ -69,8 +69,8 @@ void BattinfoView::update_result() {
labels_opt.hidden(false);
text_current.hidden(false);
text_charge.hidden(false);
text_current.set(to_string_dec_int(current) + " mA");
text_charge.set(isCharging ? "charge" : "discharge");
text_current.set(to_string_decimal(current / 100000.0, 3) + " mA");
text_charge.set(current >= 0 ? "Charging" : "Discharging");
labels_opt.hidden(false);
} else {
if (!labels_opt.hidden()) uichg = true;
Expand All @@ -80,7 +80,7 @@ void BattinfoView::update_result() {
}
if (uichg) set_dirty();
// to update status bar too, send message in behalf of batt manager
BatteryStateMessage msg{percent, isCharging, voltage};
BatteryStateMessage msg{percent, current >= 0, voltage};
EventDispatcher::send_message(msg);
}

Expand Down
1 change: 0 additions & 1 deletion firmware/application/apps/ui_battinfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class BattinfoView : public View {
uint8_t percent = 0;
uint16_t voltage = 0;
int32_t current = 0;
bool isCharging = false;

Labels labels{
{{2 * 8, 1 * 16}, "Percent:", Theme::getInstance()->fg_light->foreground},
Expand Down
11 changes: 10 additions & 1 deletion firmware/application/apps/ui_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ uint32_t RegistersWidget::reg_read(const uint32_t register_number) {
return radio::debug::second_if::register_read(register_number);
case CT_SI5351:
return portapack::clock_generator.read_register(register_number);
case CT_BATTERY:
return battery::BatteryManagement::read_register(register_number);
case CT_AUDIO:
return audio::debug::reg_read(register_number);
}
Expand All @@ -246,6 +248,9 @@ void RegistersWidget::reg_write(const uint32_t register_number, const uint32_t v
case CT_SI5351:
portapack::clock_generator.write_register(register_number, value);
break;
case CT_BATTERY:
battery::BatteryManagement::write_register(register_number, value);
break;
case CT_AUDIO:
audio::debug::reg_write(register_number, value);
break;
Expand Down Expand Up @@ -459,6 +464,10 @@ void DebugPeripheralsMenuView::on_populate() {
{si5351x, Theme::getInstance()->fg_darkcyan->foreground, &bitmap_icon_peripherals_details, [this, si5351x]() { nav_.push<RegistersView>(si5351x, RegistersWidgetConfig{CT_SI5351, 188, 96, 8}); }},
{audio::debug::codec_name(), Theme::getInstance()->fg_darkcyan->foreground, &bitmap_icon_peripherals_details, [this]() { nav_.push<RegistersView>(audio::debug::codec_name(), RegistersWidgetConfig{CT_AUDIO, audio::debug::reg_count(), audio::debug::reg_count(), audio::debug::reg_bits()}); }},
});
if (battery::BatteryManagement::detectedModule() == battery::BatteryManagement::BatteryModules::BATT_MAX17055) {
add_item(
{"MAX17055", Theme::getInstance()->fg_darkcyan->foreground, &bitmap_icon_peripherals_details, [this]() { nav_.push<RegistersView>("MAX17055", RegistersWidgetConfig{CT_BATTERY, 256, 16, 16}); }});
}
set_max_rows(2); // allow wider buttons
}

Expand Down Expand Up @@ -496,10 +505,10 @@ void DebugMenuView::on_populate() {
{"Peripherals", ui::Theme::getInstance()->fg_darkcyan->foreground, &bitmap_icon_peripherals, [this]() { nav_.push<DebugPeripheralsMenuView>(); }},
{"Pers. Memory", ui::Theme::getInstance()->fg_darkcyan->foreground, &bitmap_icon_memory, [this]() { nav_.push<DebugPmemView>(); }},
//{ "Radio State", ui::Theme::getInstance()->bg_darkest->foreground, nullptr, [this](){ nav_.push<NotImplementedView>(); } },
{"Reboot", ui::Theme::getInstance()->fg_darkcyan->foreground, &bitmap_icon_setup, [this]() { nav_.push<DebugReboot>(); }},
{"SD Card", ui::Theme::getInstance()->fg_darkcyan->foreground, &bitmap_icon_sdcard, [this]() { nav_.push<SDCardDebugView>(); }},
{"Temperature", ui::Theme::getInstance()->fg_darkcyan->foreground, &bitmap_icon_temperature, [this]() { nav_.push<TemperatureView>(); }},
{"Touch Test", ui::Theme::getInstance()->fg_darkcyan->foreground, &bitmap_icon_notepad, [this]() { nav_.push<DebugScreenTest>(); }},
{"Reboot", ui::Theme::getInstance()->fg_darkcyan->foreground, &bitmap_icon_setup, [this]() { nav_.push<DebugReboot>(); }},
});

for (auto const& gridItem : ExternalItemsMenuLoader::load_external_items(app_location_t::DEBUG, nav_)) {
Expand Down
1 change: 1 addition & 0 deletions firmware/application/apps/ui_debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ typedef enum {
CT_MAX283X,
CT_SI5351,
CT_AUDIO,
CT_BATTERY,
} chip_type_t;

struct RegistersWidgetConfig {
Expand Down
1 change: 1 addition & 0 deletions firmware/application/portapack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "usb_serial.hpp"

#include "ads1110.hpp"
#include "max17055.hpp"

#include "radio.hpp"
#include "clock_manager.hpp"
Expand Down
41 changes: 30 additions & 11 deletions firmware/common/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "event_m0.hpp"
#include "portapack.hpp"
#include "ads1110.hpp"
#include "max17055.hpp"

// uncomment if you want to emulate batt management system
// #define USE_BATT_EMULATOR
Expand All @@ -14,6 +15,7 @@ constexpr uint32_t BATTERY_UPDATE_INTERVAL = 30000;
BatteryManagement::BatteryModules BatteryManagement::detected_ = BatteryManagement::BATT_NONE;

ads1110::ADS1110 battery_ads1110{portapack::i2c0, 0x48};
max17055::MAX17055 battery_max17055{portapack::i2c0, 0x36};

Thread* BatteryManagement::thread = nullptr;

Expand All @@ -23,6 +25,9 @@ void BatteryManagement::init() {
if (battery_ads1110.detect()) {
battery_ads1110.init();
detected_ = BATT_ADS1110;
} else if (battery_max17055.detect()) {
battery_max17055.init();
detected_ = BATT_MAX17055;
}

// add new supported module detect + init here
Expand All @@ -40,11 +45,15 @@ void BatteryManagement::init() {
}

// sets the values, it the currend module supports it.
bool BatteryManagement::getBatteryInfo(uint8_t& batteryPercentage, uint16_t& voltage, int32_t& current, bool& isCharging) {
if (detected_ == BATT_NONE) return false;
if (detected_ == BATT_ADS1110) {
bool BatteryManagement::getBatteryInfo(uint8_t& batteryPercentage, uint16_t& voltage, int32_t& current) {
if (detected_ == BATT_NONE) {
return false;
} else if (detected_ == BATT_ADS1110) {
battery_ads1110.getBatteryInfo(batteryPercentage, voltage);
return true;
} else if (detected_ == BATT_MAX17055) {
battery_max17055.getBatteryInfo(batteryPercentage, voltage, current);
return true;
}
// add new module query here

Expand All @@ -59,44 +68,54 @@ bool BatteryManagement::getBatteryInfo(uint8_t& batteryPercentage, uint16_t& vol
}
#endif

(void)isCharging; // keep the compiler calm
(void)current;
return false;
}

uint16_t BatteryManagement::read_register(const uint8_t reg) {
if (detected_ == BATT_MAX17055) {
return battery_max17055.read_register(reg);
}
return 0xFFFF;
}

bool BatteryManagement::write_register(const uint8_t reg, const uint16_t value) {
if (detected_ == BATT_MAX17055) {
return battery_max17055.write_register(reg, value);
}
return false;
}

uint8_t BatteryManagement::getPercent() {
if (detected_ == BATT_NONE) return 102;
uint8_t batteryPercentage = 0;
bool isCharging = false;
uint16_t voltage = 0;
int32_t current = 0;
getBatteryInfo(batteryPercentage, voltage, current, isCharging);
getBatteryInfo(batteryPercentage, voltage, current);
return batteryPercentage;
}

uint16_t BatteryManagement::getVoltage() {
if (detected_ == BATT_NONE) return 0;
if (detected_ == BATT_NONE) return 102;
uint8_t batteryPercentage = 0;
bool isCharging = false;
uint16_t voltage = 0;
int32_t current = 0;
getBatteryInfo(batteryPercentage, voltage, current, isCharging);
getBatteryInfo(batteryPercentage, voltage, current);
return voltage;
}

msg_t BatteryManagement::timer_fn(void* arg) {
(void)arg;
if (!detected_) return 0;
uint8_t batteryPercentage = 102;
bool isCharging = false;
uint16_t voltage = 0;
int32_t current = 0;
chThdSleepMilliseconds(1000); // wait ui for fully load
while (1) {
if (BatteryManagement::getBatteryInfo(batteryPercentage, voltage, current, isCharging)) {
if (BatteryManagement::getBatteryInfo(batteryPercentage, voltage, current)) {
// send local message
BatteryStateMessage msg{batteryPercentage, isCharging, voltage};
BatteryStateMessage msg{batteryPercentage, current >= 0, voltage};
EventDispatcher::send_message(msg);
}
chThdSleepMilliseconds(BATTERY_UPDATE_INTERVAL);
Expand Down
6 changes: 5 additions & 1 deletion firmware/common/battery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ class BatteryManagement {
enum BatteryModules {
BATT_NONE = 0,
BATT_ADS1110 = 1,
BATT_MAX17055 = 2,
BATT_EMULATOR = 254
};
static void init();
static bool isDetected() { return detected_ != BATT_NONE; }
static bool getBatteryInfo(uint8_t& batteryPercentage, uint16_t& voltage, int32_t& current, bool& isCharging);
static BatteryModules detectedModule() { return detected_; }
static bool getBatteryInfo(uint8_t& batteryPercentage, uint16_t& voltage, int32_t& current);
static uint16_t getVoltage();
static uint8_t getPercent();
static uint16_t read_register(const uint8_t reg);
static bool write_register(const uint8_t reg, const uint16_t value);

private:
static void create_thread();
Expand Down
Loading

0 comments on commit ae75540

Please sign in to comment.