Skip to content

Commit

Permalink
power_path: split into early and late setup
Browse files Browse the repository at this point in the history
Configuration of power path is crutial to start battery charging. When
the battery is deeply discharged to a degree that the fuel gauge has gone
into under voltage lockout the charger needs to be engaged before
communication with the gauge wull be possible again.
Initialize battery charger before trying to communicate with battery
gauge.
  • Loading branch information
TobleMiner committed Jun 3, 2023
1 parent a371a74 commit ef977da
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
5 changes: 4 additions & 1 deletion main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,14 @@ void app_main() {
ESP_ERROR_CHECK(ssd1306_oled_init(&oled, &i2c_bus, 0x3c, GPIO_OLED_RESET));
fb_init(&fb);
*/
power_path_early_init(&smbus_bus, &i2c_bus);
vTaskDelay(pdMS_TO_TICKS(5000));

ESP_ERROR_CHECK(bq40z50_init(&bq40z50, &smbus_bus, -1));
battery_gauge_init(&bq40z50.gauge);

power_path_init(&smbus_bus, &i2c_bus);

buttons_register_single_button_event_handler(&button_held_event_handler, &button_held_cfg);
buttons_enable_event_handler(&button_held_event_handler);
/*
Expand All @@ -206,7 +210,6 @@ void app_main() {
ESP_ERROR_CHECK(gpio_isr_handler_add(GPIO_BUTTON, button_pressed, NULL));
*/

power_path_init(&smbus_bus, &i2c_bus);
/*
for (int i = 0; i < ARRAY_SIZE(ina); i++) {
ESP_ERROR_CHECK(ina219_init(&ina[i], &smbus_bus, ina_address[i], 10, ina_names[i]));
Expand Down
12 changes: 7 additions & 5 deletions main/power_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#define BATTERY_CHARGE_VOLTAGE_MV 8400
#define BATTERY_NOMINAL_VOLTAGE_MV 7400
#define DEFAULT_CHARGE_CURRENT_MA 256
#define DEFAULT_CHARGE_CURRENT_MA 128
#define MAX_CHARGE_CURRENT_MA 1024
#define MAX_INPUT_CURRENT_MA 4000

Expand Down Expand Up @@ -232,6 +232,12 @@ static void power_path_set_input_current_limit_(unsigned int current_ma) {
input_current_limit_ma = current_ma;
}

void power_path_early_init(smbus_t *smbus, i2c_bus_t *i2c_bus) {
ESP_ERROR_CHECK(bq24715_init(&bq24715, smbus));
ESP_ERROR_CHECK(bq24715_set_max_charge_voltage(&bq24715, BATTERY_CHARGE_VOLTAGE_MV));
ESP_ERROR_CHECK(bq24715_set_charge_current(&bq24715, DEFAULT_CHARGE_CURRENT_MA));
}

void power_path_init(smbus_t *smbus, i2c_bus_t *i2c_bus) {
int i;

Expand All @@ -247,10 +253,6 @@ void power_path_init(smbus_t *smbus, i2c_bus_t *i2c_bus) {
lm75_init(&lm75s[i].lm75, i2c_bus, lm75_defs[i].address, lm75_defs[i].name);
}

ESP_ERROR_CHECK(bq24715_init(&bq24715, smbus));
ESP_ERROR_CHECK(bq24715_set_max_charge_voltage(&bq24715, BATTERY_CHARGE_VOLTAGE_MV));
ESP_ERROR_CHECK(bq24715_set_charge_current(&bq24715, DEFAULT_CHARGE_CURRENT_MA));

power_path_set_input_current_limit_(settings_get_input_current_limit_ma());

scheduler_task_init(&power_path_update_task);
Expand Down
1 change: 1 addition & 0 deletions main/power_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef struct power_path_group_data {
int32_t temperature_mdegc;
} power_path_group_data_t;

void power_path_early_init(smbus_t *smbus, i2c_bus_t *i2c_bus);
void power_path_init(smbus_t *smbus, i2c_bus_t *i2c_bus);
void power_path_set_input_current_limit(unsigned int current_ma);
unsigned int power_path_get_input_current_limit_ma(void);
Expand Down

0 comments on commit ef977da

Please sign in to comment.