From 8f6a9492b2740a1759a6a7b65452ecda5bd482f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Sat, 16 Nov 2024 13:03:11 +0100 Subject: [PATCH] bq769x2: Fix alert_mask configuration write-back Fixes #80 --- drivers/bms_ic/bq769x2/bq769x2.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/drivers/bms_ic/bq769x2/bq769x2.c b/drivers/bms_ic/bq769x2/bq769x2.c index 59b67bb8..0865c837 100644 --- a/drivers/bms_ic/bq769x2/bq769x2.c +++ b/drivers/bms_ic/bq769x2/bq769x2.c @@ -406,24 +406,23 @@ static int bq769x2_configure_balancing(const struct device *dev, struct bms_ic_c static int bq769x2_configure_alerts(const struct device *dev, struct bms_ic_conf *ic_conf) { + uint32_t alert_mask = 0; int err = 0; union bq769x2_reg_alarm_sf_alert_mask_a sf_alert_mask_a = { 0 }; union bq769x2_reg_alarm_sf_alert_mask_b sf_alert_mask_b = { 0 }; - ic_conf->alert_mask = 0; - sf_alert_mask_a.CUV = !!(ic_conf->alert_mask & BMS_ERR_CELL_UNDERVOLTAGE); sf_alert_mask_a.COV = !!(ic_conf->alert_mask & BMS_ERR_CELL_OVERVOLTAGE); sf_alert_mask_a.SCD = !!(ic_conf->alert_mask & BMS_ERR_SHORT_CIRCUIT); sf_alert_mask_a.OCD1 = !!(ic_conf->alert_mask & BMS_ERR_DIS_OVERCURRENT); sf_alert_mask_a.OCC = !!(ic_conf->alert_mask & BMS_ERR_CHG_OVERCURRENT); - ic_conf->alert_mask |= (ic_conf->alert_mask & BMS_ERR_CELL_UNDERVOLTAGE); - ic_conf->alert_mask |= (ic_conf->alert_mask & BMS_ERR_CELL_OVERVOLTAGE); - ic_conf->alert_mask |= (ic_conf->alert_mask & BMS_ERR_SHORT_CIRCUIT); - ic_conf->alert_mask |= (ic_conf->alert_mask & BMS_ERR_DIS_OVERCURRENT); - ic_conf->alert_mask |= (ic_conf->alert_mask & BMS_ERR_CHG_OVERCURRENT); + alert_mask |= (ic_conf->alert_mask & BMS_ERR_CELL_UNDERVOLTAGE); + alert_mask |= (ic_conf->alert_mask & BMS_ERR_CELL_OVERVOLTAGE); + alert_mask |= (ic_conf->alert_mask & BMS_ERR_SHORT_CIRCUIT); + alert_mask |= (ic_conf->alert_mask & BMS_ERR_DIS_OVERCURRENT); + alert_mask |= (ic_conf->alert_mask & BMS_ERR_CHG_OVERCURRENT); err |= bq769x2_datamem_write_u1(dev, BQ769X2_SET_ALARM_SF_ALERT_MASK_A, sf_alert_mask_a.byte); @@ -434,18 +433,20 @@ static int bq769x2_configure_alerts(const struct device *dev, struct bms_ic_conf sf_alert_mask_b.OTINT = !!(ic_conf->alert_mask & BMS_ERR_INT_OVERTEMP); sf_alert_mask_b.OTF = !!(ic_conf->alert_mask & BMS_ERR_FET_OVERTEMP); - ic_conf->alert_mask |= (ic_conf->alert_mask & BMS_ERR_DIS_UNDERTEMP); - ic_conf->alert_mask |= (ic_conf->alert_mask & BMS_ERR_DIS_OVERTEMP); - ic_conf->alert_mask |= (ic_conf->alert_mask & BMS_ERR_CHG_UNDERTEMP); - ic_conf->alert_mask |= (ic_conf->alert_mask & BMS_ERR_CHG_OVERTEMP); - ic_conf->alert_mask |= (ic_conf->alert_mask & BMS_ERR_INT_OVERTEMP); - ic_conf->alert_mask |= (ic_conf->alert_mask & BMS_ERR_FET_OVERTEMP); + alert_mask |= (ic_conf->alert_mask & BMS_ERR_DIS_UNDERTEMP); + alert_mask |= (ic_conf->alert_mask & BMS_ERR_DIS_OVERTEMP); + alert_mask |= (ic_conf->alert_mask & BMS_ERR_CHG_UNDERTEMP); + alert_mask |= (ic_conf->alert_mask & BMS_ERR_CHG_OVERTEMP); + alert_mask |= (ic_conf->alert_mask & BMS_ERR_INT_OVERTEMP); + alert_mask |= (ic_conf->alert_mask & BMS_ERR_FET_OVERTEMP); err |= bq769x2_datamem_write_u1(dev, BQ769X2_SET_ALARM_SF_ALERT_MASK_B, sf_alert_mask_b.byte); /* enable alarm (triggering of ALERT pin) for SF alert masks configured above */ err |= bq769x2_datamem_write_u2(dev, BQ769X2_SET_ALARM_DEFAULT_MASK, 0x1000); + ic_conf->alert_mask = alert_mask; + return err == 0 ? 0 : -EIO; }