From 1cb82f25b5aad565487aa395448e2d208f29c067 Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Sun, 30 Jan 2022 04:14:13 +0100 Subject: [PATCH] Add Cell ballancing helpers to BQ769x0 BMS support --- extdrv/bq769x0_bms.c | 33 +++++++++++++++++++++++++++++++++ include/extdrv/bq769x0_bms.h | 6 ++++++ 2 files changed, 39 insertions(+) diff --git a/extdrv/bq769x0_bms.c b/extdrv/bq769x0_bms.c index de04a45..76357c1 100644 --- a/extdrv/bq769x0_bms.c +++ b/extdrv/bq769x0_bms.c @@ -88,6 +88,10 @@ enum bq769x0_internal_reg_numbers { /* The documentation asks for CC_CFG to be set to 0x19 upon startup */ #define CC_CFG_DEFAULT 0x19 +/* Cell balancing helpers */ +#define CELL_BALANCE(x) (x) +#define BCELL(x) (0x01 << ((x) - 1)) + /* Check the bms presence, return 1 if bms was found. * This is a basic check, it could be anything with the same address ... @@ -378,6 +382,35 @@ int bq769x0_bms_set_ranges(struct bq769x0_bms_conf* conf, return 0; } +/* Cell balancing + * set corresponds to the cells set number : [1 .. 3] + * cell is the cell number within the set : [1 .. 5] or 0 to turn off cell balancing for this set + */ +int bq769x0_cell_balance(struct bq769x0_bms_conf* conf, uint8_t set, uint8_t cell) +{ + int ret = 0; + uint8_t tmp = 0; + + if ((set == 0) || (set > 3)) { + return -1; + } + if (cell > 5) { + return -2; + } + /* Turn off cell balancing for this set */ + if (cell == 0) { + tmp = 0; + } else { + tmp = BCELL(cell); + } + ret = bq769x0_bms_set_regs(conf, CELL_BALANCE(set), &tmp, 1); + if (ret != 0) { + conf->probe_ok = 20; + return ret; + } + return 0; +} + /* Read ADC values. * type is one of : diff --git a/include/extdrv/bq769x0_bms.h b/include/extdrv/bq769x0_bms.h index ad523cb..06a4098 100644 --- a/include/extdrv/bq769x0_bms.h +++ b/include/extdrv/bq769x0_bms.h @@ -123,6 +123,12 @@ int bq769x0_bms_set_ranges(struct bq769x0_bms_conf* conf, uint8_t uvlo, uint8_t ovp, uint8_t ocd, uint8_t scd); +/* Cell balancing + * set corresponds to the cells set number : [1 .. 3] + * cell is the cell number within the set : [1 .. 5] or 0 to turn off cell balancing for this set + */ +int bq769x0_cell_balance(struct bq769x0_bms_conf* conf, uint8_t set, uint8_t cell); + enum bq769x0_bms_adc_values { ALL = 0, -- 2.43.0