Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/Battery/BQ25120a.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ int BQ25120a::reset() {
return 0;
}

int BQ25120a::set_wakeup_int() {
int BQ25120a::set_wakeup_int(bool wake_on_power_good) {
int ret;

ret = device_is_ready(pg_pin.port); //bool
Expand All @@ -76,21 +76,37 @@ int BQ25120a::set_wakeup_int() {
return -1;
}

ret = gpio_pin_interrupt_configure_dt(&pg_pin, GPIO_INT_LEVEL_ACTIVE);
ret = gpio_pin_interrupt_configure_dt(&int_pin, GPIO_INT_LEVEL_ACTIVE);
if (ret != 0) {
LOG_ERR("Failed to setup interrupt on PG.\n");
LOG_ERR("Failed to setup interrupt on INT.\n");
return ret;
}

ret = gpio_pin_interrupt_configure_dt(&int_pin, GPIO_INT_LEVEL_ACTIVE);
ret = gpio_pin_interrupt_configure_dt(&pg_pin,
wake_on_power_good ? GPIO_INT_LEVEL_ACTIVE :
GPIO_INT_DISABLE);
if (ret != 0) {
LOG_ERR("Failed to setup interrupt on INT.\n");
LOG_ERR("Failed to setup interrupt on PG.\n");
return ret;
}

return 0;
}

void BQ25120a::clear_interrupt_latches() {
for (int attempt = 0; attempt < 3; ++attempt) {
(void)read_fault();
(void)read_ts_fault();

button_state btn = read_button_state();
if (!btn.wake_1 && !btn.wake_2) {
break;
}

k_msleep(10);
}
}

bool BQ25120a::readReg(uint8_t reg, uint8_t * buffer, uint16_t len) {
int ret;
uint64_t now = micros();
Expand Down Expand Up @@ -426,4 +442,4 @@ int BQ25120a::set_power_connect_callback(gpio_callback_handler_t handler) {
int BQ25120a::set_int_callback(gpio_callback_handler_t handler) {
gpio_init_callback(&int_cb_data, handler, int_cb_data.pin_mask | BIT(int_pin.pin));
return gpio_add_callback(int_pin.port, &int_cb_data);
}
}
5 changes: 3 additions & 2 deletions src/Battery/BQ25120a.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class BQ25120a {
BQ25120a(TWIM * i2c);

int begin();
int set_wakeup_int();
int set_wakeup_int(bool wake_on_power_good = false);

int reset();

Expand Down Expand Up @@ -79,6 +79,7 @@ class BQ25120a {
uint8_t write_LS_control(bool enable);

button_state read_button_state();
void clear_interrupt_latches();

int set_power_connect_callback(gpio_callback_handler_t handler);
int set_int_callback(gpio_callback_handler_t handler);
Expand Down Expand Up @@ -107,4 +108,4 @@ class BQ25120a {

extern BQ25120a battery_controller;

#endif
#endif
8 changes: 4 additions & 4 deletions src/Battery/PowerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,10 @@ int PowerManager::power_down(bool fault) {
bool charging = battery_controller.power_connected();

if (!charging) {
ret = battery_controller.set_wakeup_int();
if (ret != 0) return ret;
battery_controller.exit_high_impedance();
battery_controller.clear_interrupt_latches();

ret = fuel_gauge.set_wakeup_int();
ret = battery_controller.set_wakeup_int(false);
if (ret != 0) return ret;

// check battery good
Expand Down Expand Up @@ -758,4 +758,4 @@ SHELL_STATIC_SUBCMD_SET_CREATE(battery_cmd,

SHELL_CMD_REGISTER(battery, &battery_cmd, "Power Manager Commands", NULL);

PowerManager power_manager;
PowerManager power_manager;