Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve battery level #179

Merged
merged 4 commits into from
Jul 2, 2024
Merged
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
6 changes: 4 additions & 2 deletions drivers/tildagon_power/bq25895/bq25895.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ static void write_scaled( bq_state_t* state, scaled_register_t scaledregister, f
* @brief initialise the bq25895
* @details reset then setup 500mA Iin limit, boost disabled, charging enabled,
* ADC at 1Hz, disable unused features and disable watchdog to reduce interruptions
* charge current limit to be 0.85C for the 1800mAh and 0.77C for 2000mAh batteries
* termination and precharge current to 64mA. min Vbat 3.5V
* @param state pmic object
*/
void bq_init( bq_state_t* state )
{
write_bits( state, register_reset, 1 );
uint8_t write_buffer[3] = { 0x02, 0x60, 0x1A };
mp_machine_i2c_buf_t buffer = { .len = 3, .buf = write_buffer };
uint8_t write_buffer[5] = { 0x02, 0x60, 0x1A, 0x18, 0x00 };
mp_machine_i2c_buf_t buffer = { .len = 5, .buf = write_buffer };
tildagon_mux_i2c_transaction( state->mux_port, ADDRESS, 1, &buffer, WRITE );
write_buffer[0] = 0x07;
write_buffer[1] = 0x8C;
Expand Down
55 changes: 54 additions & 1 deletion drivers/tildagon_power/mp_power.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
#include <stdint.h>
#include "tildagon_power.h"

/*
minimum input voltage for step down at max (4A) load.
minimum input voltage = ( Rl(DCR) + Rdson ) * max current + output voltage
(0.078ohms * 4.0A) + 3.3V = 3.6V
most users won't use 4A so badge will run lower than this so use 3.5V as minimum.
*/
#define VBAT_DIS_MAX 4.14F
#define VBAT_DIS_MIN 3.5F
#define VBAT_CHG_MAX 4.2F
#define VBAT_CHG_MIN 3.6F
#define IBAT_TERM 0.064F
#define VBAT_CI_PERCENT 85.0F

static mp_obj_t power_enable5V( mp_obj_t a_obj )
{
bool enable = (bool)mp_obj_get_int(a_obj);
Expand Down Expand Up @@ -56,7 +69,47 @@ static MP_DEFINE_CONST_FUN_OBJ_0( Icharge_obj, power_Icharge);
static mp_obj_t power_BatteryLevel( void )
{
bq_update_state( &pmic );
return mp_obj_new_float( ( ( pmic.vbat-VBATMIN ) / ( VBATMAX- VBATMIN ) ) * 100.0F );
float level = 0.0F;
if( ( ( pmic.status & BQ_CHARGE_STAT_MASK ) == BQ_NOTCHARGING )
|| ( ( pmic.status & BQ_CHARGE_STAT_MASK ) == BQ_TERMINATED ) )
{
level = ( ( pmic.vbat-VBAT_DIS_MIN ) / ( VBAT_DIS_MAX- VBAT_DIS_MIN ) ) * 100.0F;
}
else
{
float max_current = 1.536F;
float vbat_cv_percent = 20.0F;
float vbat_ci_percent = 100.0F - vbat_cv_percent;
if ( usb_in.fusb.input_current_limit == 1500U )
{
max_current = 1.3F;
vbat_cv_percent = 17.0F;
vbat_ci_percent = 100.0F - vbat_cv_percent;
}
else if ( usb_in.fusb.input_current_limit == 500 )
{
max_current = 0.4F;
vbat_cv_percent = 2.0F;
vbat_ci_percent = 100.0F - vbat_cv_percent;
}
if ( pmic.vbat < VBAT_CHG_MAX )
{
level = ( ( pmic.vbat-VBAT_CHG_MIN ) / ( VBAT_CHG_MAX - VBAT_CHG_MIN ) ) * vbat_ci_percent;
}
else
{
level = 100.0F - ( ( pmic.ichrg / ( max_current - IBAT_TERM ) ) * vbat_cv_percent );
}
}
if ( level > 100.0F )
{
level = 100.0F;
}
else if ( level < 0.0F )
{
level = 0.0F;
}
return mp_obj_new_float(level);
}

static MP_DEFINE_CONST_FUN_OBJ_0( BatteryLevel_obj, power_BatteryLevel);
Expand Down
4 changes: 0 additions & 4 deletions drivers/tildagon_power/tildagon_power.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
#include "fusb302b/fusb302b_pd.h"
#include "fusb302b/fusb302b.h"


#define VBATMAX 4.104F
#define VBATMIN 3.500F

typedef struct
{
fusb_state_t fusb;
Expand Down
Loading