Skip to content

Commit

Permalink
move power status to driver, no more additional copy (#34)
Browse files Browse the repository at this point in the history
Signed-off-by: Adam BZH <adam@onekey.so>
  • Loading branch information
424778940z authored May 29, 2024
1 parent b672ea9 commit 8446ade
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 148 deletions.
61 changes: 30 additions & 31 deletions app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,25 +389,23 @@ static char ble_adv_name[ADV_NAME_LENGTH];
// NRF_LOG_FINAL_FLUSH();
// }

static Power_Status_t pmu_status;
static void pmu_status_refresh()
{
pmu_p->GetStatus(&pmu_status);

NRF_LOG_INFO("=== Power_Status_t ===");
NRF_LOG_INFO("isValid=%u", pmu_status.isValid);
NRF_LOG_INFO("batteryPresent=%u", pmu_status.batteryPresent);
NRF_LOG_INFO("batteryPercent=%u", pmu_status.batteryPercent);
NRF_LOG_INFO("batteryVoltage=%lu", pmu_status.batteryVoltage);
NRF_LOG_INFO("batteryTemp=%ld", pmu_status.batteryTemp);
NRF_LOG_INFO("pmuTemp=%lu", pmu_status.pmuTemp);
NRF_LOG_INFO("chargeAllowed=%u", pmu_status.chargeAllowed);
NRF_LOG_INFO("chargerAvailable=%u", pmu_status.chargerAvailable);
NRF_LOG_INFO("chargeFinished=%u", pmu_status.chargeFinished);
NRF_LOG_INFO("wiredCharge=%u", pmu_status.wiredCharge);
NRF_LOG_INFO("wirelessCharge=%u", pmu_status.wirelessCharge);
NRF_LOG_INFO("chargeCurrent=%lu", pmu_status.chargeCurrent);
NRF_LOG_INFO("dischargeCurrent=%lu", pmu_status.dischargeCurrent);
pmu_p->PullStatus();

NRF_LOG_INFO("=== PowerStatus ===");
NRF_LOG_INFO("batteryPresent=%u", pmu_p->PowerStatus->batteryPresent);
NRF_LOG_INFO("batteryPercent=%u", pmu_p->PowerStatus->batteryPercent);
NRF_LOG_INFO("batteryVoltage=%lu", pmu_p->PowerStatus->batteryVoltage);
NRF_LOG_INFO("batteryTemp=%ld", pmu_p->PowerStatus->batteryTemp);
NRF_LOG_INFO("pmuTemp=%lu", pmu_p->PowerStatus->pmuTemp);
NRF_LOG_INFO("chargeAllowed=%u", pmu_p->PowerStatus->chargeAllowed);
NRF_LOG_INFO("chargerAvailable=%u", pmu_p->PowerStatus->chargerAvailable);
NRF_LOG_INFO("chargeFinished=%u", pmu_p->PowerStatus->chargeFinished);
NRF_LOG_INFO("wiredCharge=%u", pmu_p->PowerStatus->wiredCharge);
NRF_LOG_INFO("wirelessCharge=%u", pmu_p->PowerStatus->wirelessCharge);
NRF_LOG_INFO("chargeCurrent=%lu", pmu_p->PowerStatus->chargeCurrent);
NRF_LOG_INFO("dischargeCurrent=%lu", pmu_p->PowerStatus->dischargeCurrent);
NRF_LOG_INFO("=== ============== ===");
NRF_LOG_FLUSH();
}
Expand Down Expand Up @@ -631,9 +629,9 @@ void battery_level_meas_timeout_handler(void* p_context)
static uint8_t battery_percent = 0;

UNUSED_PARAMETER(p_context);
if ( battery_percent != pmu_status.batteryPercent )
if ( battery_percent != pmu_p->PowerStatus->batteryPercent )
{
battery_percent = pmu_status.batteryPercent;
battery_percent = pmu_p->PowerStatus->batteryPercent;
if ( g_bas_update_flag == 1 )
{
err_code = ble_bas_battery_level_update(&m_bas, battery_percent, BLE_CONN_HANDLE_ALL);
Expand Down Expand Up @@ -2285,11 +2283,11 @@ static void manage_bat_level(void* p_event_data, uint16_t event_size)
{
static uint8_t bak_bat_persent = 0x00;

if ( bak_bat_persent != pmu_status.batteryPercent )
if ( bak_bat_persent != pmu_p->PowerStatus->batteryPercent )
{
bak_bat_persent = pmu_status.batteryPercent;
bak_bat_persent = pmu_p->PowerStatus->batteryPercent;
bak_buff[0] = BLE_SYSTEM_POWER_PERCENT;
bak_buff[1] = pmu_status.batteryPercent;
bak_buff[1] = pmu_p->PowerStatus->batteryPercent;
NRF_LOG_INFO("send_stm_data 018");
send_stm_data(bak_buff, 2);
}
Expand Down Expand Up @@ -2397,25 +2395,26 @@ static void ble_ctl_process(void* p_event_data, uint16_t event_size)
case PWR_BAT_PERCENT:
pwr_status_flag = PWR_DEF;
bak_buff[0] = BLE_SYSTEM_POWER_PERCENT;
bak_buff[1] = pmu_status.batteryPercent;
bak_buff[1] = pmu_p->PowerStatus->batteryPercent;
send_stm_data(bak_buff, 2);
break;
case PWR_USB_STATUS:
pwr_status_flag = PWR_DEF;
bak_buff[0] = BLE_CMD_POWER_STA;

if ( pmu_status.chargerAvailable )
if ( pmu_p->PowerStatus->chargerAvailable )
{
bak_buff[1] = ((pmu_status.chargeFinished && pmu_status.chargeAllowed) ? BLE_CHAGE_OVER : BLE_CHARGING_PWR);
bak_buff[2] = (pmu_status.wiredCharge ? AXP_CHARGE_TYPE_USB : AXP_CHARGE_TYPE_WIRELESS);
bak_buff[1] =
((pmu_p->PowerStatus->chargeFinished && pmu_p->PowerStatus->chargeAllowed) ? BLE_CHAGE_OVER
: BLE_CHARGING_PWR);
bak_buff[2] = (pmu_p->PowerStatus->wiredCharge ? AXP_CHARGE_TYPE_USB : AXP_CHARGE_TYPE_WIRELESS);
}
else
{
bak_buff[1] = BLE_REMOVE_POWER;
bak_buff[2] = 0;
}
send_stm_data(bak_buff, 3);
pwr_status_flag = PWR_DEF;
break;
default:
break;
Expand Down Expand Up @@ -2465,16 +2464,16 @@ static void bat_msg_report_process(void* p_event_data, uint16_t event_size)
switch ( bat_msg_flag )
{
case SEND_BAT_VOL:
val = pmu_status.batteryVoltage;
val = pmu_p->PowerStatus->batteryVoltage;
break;
case SEND_BAT_CHARGE_CUR:
val = pmu_status.chargeCurrent;
val = pmu_p->PowerStatus->chargeCurrent;
break;
case SEND_BAT_DISCHARGE_CUR:
val = pmu_status.dischargeCurrent;
val = pmu_p->PowerStatus->dischargeCurrent;
break;
case SEND_BAT_INNER_TEMP:
val = (uint16_t)(pmu_status.batteryTemp);
val = (uint16_t)(pmu_p->PowerStatus->batteryTemp);
break;
default:
return;
Expand Down
10 changes: 5 additions & 5 deletions app/power_manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ static void pmu_if_irq(const uint64_t irq)
{
if ( irq == 0 )
return;
Power_Status_t status;
pmu_p->GetStatus(&status);

pmu_p->PullStatus();

if ( 0 != (irq & (1 << PWR_IRQ_PWR_CONNECTED)) )
{
bak_buff[0] = BLE_CMD_POWER_STA;
bak_buff[1] = BLE_INSERT_POWER;
bak_buff[2] = (status.wiredCharge ? AXP_CHARGE_TYPE_USB : AXP_CHARGE_TYPE_WIRELESS);
bak_buff[2] = (pmu_p->PowerStatus->wiredCharge ? AXP_CHARGE_TYPE_USB : AXP_CHARGE_TYPE_WIRELESS);
send_stm_data_p(bak_buff, 3);
}
if ( 0 != (irq & (1 << PWR_IRQ_PWR_DISCONNECTED)) )
Expand All @@ -48,15 +48,15 @@ static void pmu_if_irq(const uint64_t irq)
{
bak_buff[0] = BLE_CMD_POWER_STA;
bak_buff[1] = BLE_CHARGING_PWR;
bak_buff[2] = (status.wiredCharge ? AXP_CHARGE_TYPE_USB : AXP_CHARGE_TYPE_WIRELESS);
bak_buff[2] = (pmu_p->PowerStatus->wiredCharge ? AXP_CHARGE_TYPE_USB : AXP_CHARGE_TYPE_WIRELESS);
send_stm_data_p(bak_buff, 3);
}
if ( 0 != (irq & (1 << PWR_IRQ_CHARGED)) )
{

bak_buff[0] = BLE_CMD_POWER_STA;
bak_buff[1] = BLE_CHAGE_OVER;
bak_buff[2] = (status.wiredCharge ? AXP_CHARGE_TYPE_USB : AXP_CHARGE_TYPE_WIRELESS);
bak_buff[2] = (pmu_p->PowerStatus->wiredCharge ? AXP_CHARGE_TYPE_USB : AXP_CHARGE_TYPE_WIRELESS);
send_stm_data_p(bak_buff, 3);
}
if ( 0 != (irq & (1 << PWR_IRQ_BATT_LOW)) ) {}
Expand Down
94 changes: 48 additions & 46 deletions drivers/pmu/axp2101.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
static bool initialized = false;
static PMU_Interface_t* pmu_interface_p = NULL;
static Power_State_t state_current = PWR_STATE_INVALID;
static Power_Status_t status_current = {0};

// functions private

static bool axp2101_config_voltage(void)
{
// voltages
Expand Down Expand Up @@ -273,32 +275,32 @@ Power_Error_t axp2101_set_state(const Power_State_t state)
{
case PWR_STATE_SOFT_OFF:
// close output
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_LDO_EN_CFG0, 0x40)); // keep cpuldo on
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_LDO_EN_CFG1, 0x00));
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_DCDC_CFG0, 0x00));
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_DCDC_CFG1, 0x00));
EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_LDO_EN_CFG0, 0x40)); // keep cpuldo on
EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_LDO_EN_CFG1, 0x00));
EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_DCDC_CFG0, 0x00));
EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_DCDC_CFG1, 0x00));
// cpuldo is not used and not connected, keep it on is just for prevent axp "sleep"
// "close all output means sleep" is a really stupid design, as axp turns off I2C when sleeping
// there is no way to wake it up if you do't enable wakeup source before close all output
// make sure config the "left on" reg first
break;
case PWR_STATE_HARD_OFF:
// close output (not needed as the pmu off will kill all output)
// EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_DCDC_CFG0, 0x00));
// EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_DCDC_CFG1, 0x00));
// EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_LDO_EN_CFG0, 0x00));
// EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_LDO_EN_CFG1, 0x00));
// EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_DCDC_CFG0, 0x00));
// EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_DCDC_CFG1, 0x00));
// EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_LDO_EN_CFG0, 0x00));
// EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_LDO_EN_CFG1, 0x00));
// pmu off
EC_E_BOOL_R_PWR_ERR(axp2101_set_bits(AXP2101_COMM_CFG, (1 << 0)));
break;
case PWR_STATE_ON:
// strong drive
pmu_interface_p->HighDriveStrengthCtrl(true);
// open output
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_LDO_EN_CFG0, 0x01)); // aldo1 on, other off
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_LDO_EN_CFG1, 0X00)); // all off
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_DCDC_CFG0, 0x01)); // dcdc1 on, other off
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_DCDC_CFG1, 0x00)); // all off
EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_LDO_EN_CFG0, 0x01)); // aldo1 on, other off
EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_LDO_EN_CFG1, 0X00)); // all off
EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_DCDC_CFG0, 0x01)); // dcdc1 on, other off
EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_DCDC_CFG1, 0x00)); // all off
// normal drive
pmu_interface_p->HighDriveStrengthCtrl(false);
break;
Expand All @@ -308,10 +310,10 @@ Power_Error_t axp2101_set_state(const Power_State_t state)
// enable wakeup
EC_E_BOOL_R_PWR_ERR(axp2101_set_bits(AXP2101_SLEEP_CFG, (1 << 3)));
// close output ("sleep")
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_DCDC_CFG0, 0x00));
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_DCDC_CFG1, 0x00));
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_LDO_EN_CFG0, 0x00));
EC_E_BOOL_R_BOOL(axp2101_reg_write(AXP2101_LDO_EN_CFG1, 0x00));
EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_DCDC_CFG0, 0x00));
EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_DCDC_CFG1, 0x00));
EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_LDO_EN_CFG0, 0x00));
EC_E_BOOL_R_PWR_ERR(axp2101_reg_write(AXP2101_LDO_EN_CFG1, 0x00));
break;
case PWR_STATE_WAKEUP:
// strong drive
Expand Down Expand Up @@ -339,68 +341,67 @@ Power_Error_t axp2101_get_state(Power_State_t* state)
return PWR_ERROR_USAGE;
}

Power_Error_t axp2101_get_status(Power_Status_t* status)
Power_Error_t axp2101_pull_status(void)
{
HL_Buff hlbuff;

memset(status, 0x00, sizeof(Power_Status_t));
status->isValid = false;
Power_Status_t status_temp = {0};

// battery present
hlbuff.u8_high = 0;
EC_E_BOOL_R_PWR_ERR(axp2101_reg_read(AXP2101_COMM_STAT0, &(hlbuff.u8_low)));
status->batteryPresent = ((hlbuff.u8_low & (1 << 3)) == (1 << 3));
status_temp.batteryPresent = ((hlbuff.u8_low & (1 << 3)) == (1 << 3));

if ( status->batteryPresent )
if ( status_temp.batteryPresent )
{
// battery percent
hlbuff.u8_high = 0;
EC_E_BOOL_R_PWR_ERR(axp2101_reg_read(AXP2101_SOC, &(hlbuff.u8_low)));
status->batteryPercent = hlbuff.u8_low;
status_temp.batteryPercent = hlbuff.u8_low;

// battery voltage
EC_E_BOOL_R_PWR_ERR(axp2101_reg_read(AXP2101_VBAT_H, &(hlbuff.u8_high)));
hlbuff.u8_high &= 0b00111111; // drop bit 7:6
EC_E_BOOL_R_PWR_ERR(axp2101_reg_read(AXP2101_VBAT_L, &(hlbuff.u8_low)));
status->batteryVoltage = hlbuff.u16;
status_temp.batteryVoltage = hlbuff.u16;

// battery temp
EC_E_BOOL_R_PWR_ERR(axp2101_reg_read(AXP2101_TS_H, &(hlbuff.u8_high)));
hlbuff.u8_high &= 0b00111111; // drop bit 7:6
EC_E_BOOL_R_PWR_ERR(axp2101_reg_read(AXP2101_TS_L, &(hlbuff.u8_low)));
status->batteryTemp =
status_temp.batteryTemp =
ntc_temp_cal_cv(NTC_Char_NCP15XH103F03RC_2585, 40, ((hlbuff.u16 * 0.5) * 0.8 + 0) * 1000); // temp_c
}
else
{
status->batteryPercent = 0;
status->batteryVoltage = 0;
status->batteryTemp = -999;
status_temp.batteryPercent = 0;
status_temp.batteryVoltage = 0;
status_temp.batteryTemp = -999;
}

// pmu temp
EC_E_BOOL_R_PWR_ERR(axp2101_reg_read(AXP2101_TDIE_H, &(hlbuff.u8_high)));
hlbuff.u8_high &= 0b00111111; // drop bit 7:6
EC_E_BOOL_R_PWR_ERR(axp2101_reg_read(AXP2101_TDIE_L, &(hlbuff.u8_low)));
status->pmuTemp = (7274 - hlbuff.u16) / 20 + 22;
status_temp.pmuTemp = (7274 - hlbuff.u16) / 20 + 22;

// charging
hlbuff.u8_high = 0;
EC_E_BOOL_R_PWR_ERR(axp2101_reg_read(AXP2101_MODULE_EN, &(hlbuff.u8_low)));
status->chargeAllowed = ((hlbuff.u8_low & (1 << 1)) == (1 << 1));
status_temp.chargeAllowed = ((hlbuff.u8_low & (1 << 1)) == (1 << 1));

hlbuff.u8_high = 0;
EC_E_BOOL_R_PWR_ERR(axp2101_reg_read(AXP2101_COMM_STAT0, &(hlbuff.u8_low)));
status->chargerAvailable = ((hlbuff.u8_low & (1 << 5)) == (1 << 5)); // vbus good
status_temp.chargerAvailable = ((hlbuff.u8_low & (1 << 5)) == (1 << 5)); // vbus good

if ( status->chargeAllowed && status->chargerAvailable )
if ( status_temp.chargeAllowed && status_temp.chargerAvailable )
{
hlbuff.u8_high = 0;
EC_E_BOOL_R_PWR_ERR(axp2101_reg_read(AXP2101_COMM_STAT1, &(hlbuff.u8_low)));
status->chargeFinished = ((hlbuff.u8_low & 0b00000111) == 0b00000100); // bit 2:0 = 100 charge done
status_temp.chargeFinished = ((hlbuff.u8_low & 0b00000111) == 0b00000100); // bit 2:0 = 100 charge done

if ( (hlbuff.u8_low & 0b01100000) == 0b00100000 || // bit 6:5 = 01 battery current charge
status->chargeFinished // still try get power source
status_temp.chargeFinished // still try get power source
)
{
// TODO: find a batter way to check GPIO?
Expand All @@ -411,30 +412,30 @@ Power_Error_t axp2101_get_status(Power_Status_t* status)
EC_E_BOOL_R_PWR_ERR(pmu_interface_p->GPIO.Read(8, &gpio_high_low));
EC_E_BOOL_R_PWR_ERR(pmu_interface_p->GPIO.Config(8, PWR_GPIO_Config_UNUSED));

status->wirelessCharge = !gpio_high_low; // low is wireless
status_temp.wirelessCharge = !gpio_high_low; // low is wireless
// if not wireless charging then it's wired
status->wiredCharge = !status->wirelessCharge;
status_temp.wiredCharge = !status_temp.wirelessCharge;
}
else
{
status->wiredCharge = false;
status->wirelessCharge = false;
status_temp.wiredCharge = false;
status_temp.wirelessCharge = false;
}

// not supported by AXP2101
status->chargeCurrent = 0;
status->dischargeCurrent = 0;
status_temp.chargeCurrent = 0;
status_temp.dischargeCurrent = 0;
}
else
{
status->chargeFinished = false;
status->wiredCharge = false;
status->wirelessCharge = false;
status->chargeCurrent = 0;
status->dischargeCurrent = 0;
status_temp.chargeFinished = false;
status_temp.wiredCharge = false;
status_temp.wirelessCharge = false;
status_temp.chargeCurrent = 0;
status_temp.dischargeCurrent = 0;
}

status->isValid = true;
memcpy(&status_current, &status_temp, sizeof(Power_Status_t));
return PWR_ERROR_NONE;
}

Expand Down Expand Up @@ -487,6 +488,7 @@ void axp2101_setup_interface(PMU_Interface_t* pmu_if_p, PMU_t* pmu_p)

pmu_p->isInitialized = &initialized;
strncpy(pmu_p->InstanceName, "AXP2101", PMU_INSTANCE_NAME_MAX_LEN);
pmu_p->PowerStatus = &status_current;

pmu_p->Init = axp2101_init;
pmu_p->Deinit = axp2101_deinit;
Expand All @@ -495,7 +497,7 @@ void axp2101_setup_interface(PMU_Interface_t* pmu_if_p, PMU_t* pmu_p)
pmu_p->Irq = axp2101_irq;
pmu_p->SetState = axp2101_set_state;
pmu_p->GetState = axp2101_get_state;
pmu_p->GetStatus = axp2101_get_status;
pmu_p->PullStatus = axp2101_pull_status;
pmu_p->SetFeature = axp2101_set_feature;
pmu_p->GetFeature = axp2101_get_feature;
}
2 changes: 1 addition & 1 deletion drivers/pmu/axp2101.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Power_Error_t axp2101_config(void);
Power_Error_t axp2101_irq(void);
Power_Error_t axp2101_set_state(const Power_State_t state);
Power_Error_t axp2101_get_state(Power_State_t* state);
Power_Error_t axp2101_get_status(Power_Status_t* status);
Power_Error_t axp2101_pull_status(void);
Power_Error_t axp2101_set_feature(Power_Featrue_t feature, bool enable);
Power_Error_t axp2101_get_feature(Power_Featrue_t feature, bool* enable);

Expand Down
Loading

0 comments on commit 8446ade

Please sign in to comment.