From 90cd1330c23ee3cf98eabc77298a9dc5f5b05ede Mon Sep 17 00:00:00 2001 From: Kilian Bracher Date: Tue, 25 Feb 2025 20:06:19 +0100 Subject: [PATCH] refactor: change OV/UV thresholds to use mV --- .../Core/Inc/ADBMS_Abstraction.h | 13 +++++---- .../ADBMS6830B_Driver/Core/Inc/ADBMS_Driver.h | 3 ++ .../Core/Src/ADBMS_Abstraction.c | 28 +++++++++++-------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Abstraction.h b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Abstraction.h index 11daf11..bb29f60 100644 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Abstraction.h +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Abstraction.h @@ -13,13 +13,14 @@ #include "ADBMS_LL_Driver.h" #include "main.h" - -// see table 103 in datasheet (page 71) -#define DEFAULT_UV 417 // VUV * 16 * 150 uV + 1.5 V Default Setting 2.5V -#define DEFAULT_OV 1125 // VOV * 16 * 150 uV + 1.5 V Default Setting 4.2V - #define mV_from_ADBMS6830(x) (((((int16_t)(x))) * 0.150) + 1500) +// Macro to convert voltage in mV to the internal format for under/over voltage thresholds. +// Calculation: internal = (mV - 1500) / (16 * 0.15) = (mV - 1500) / 2.4. +// To perform integer arithmetic with rounding, we compute: +// internal = ((mV - 1500) * 5 + 6) / 12 +#define mV_to_ADBMS6830(mV) ((((mV) - 1500) * 5 + 6) / 12) + HAL_StatusTypeDef amsReset(); HAL_StatusTypeDef initAMS(SPI_HandleTypeDef* hspi); @@ -35,7 +36,7 @@ HAL_StatusTypeDef amsStopBalancing(); HAL_StatusTypeDef amsSelfTest(); -HAL_StatusTypeDef amsConfigOverUnderVoltage(uint16_t overVoltage, uint16_t underVoltage); +HAL_StatusTypeDef amsConfigOverUnderVoltage(uint16_t overVoltage, uint16_t underVoltage); //arguments in mV HAL_StatusTypeDef amsCheckUnderOverVoltage(Cell_Module (*module)[N_BMS]); diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Driver.h b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Driver.h index 5bb9307..841c95d 100644 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Driver.h +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Driver.h @@ -6,6 +6,9 @@ #define ERROR_TIME_THRESH 150 // ms +#define DEFAULT_UV 3000 // mV +#define DEFAULT_OV 4200 // mV + typedef enum : uint16_t { ADBMS_NO_ERROR = 0, ADBMS_OVERTEMP, diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c index e1e0ed9..698af91 100644 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c @@ -67,13 +67,13 @@ HAL_StatusTypeDef amsReset() { amsStopBalancing(); amsConfigOverUnderVoltage(DEFAULT_OV, DEFAULT_UV); - uint8_t flagsbuffer[CMD_BUFFER_SIZE(6)] = {[0 ... CMD_BUFFER_SIZE(6) - 1] = 0xFF}; //technically not all 6 bytes are used, but it's easier to just set all to 0xFF - //see page 27 of the datasheet for the memory map + uint8_t flagsbuffer[CMD_BUFFER_SIZE(6)] = {[0 ... CMD_BUFFER_SIZE(6) - 1] = 0xFF}; //technically not all 6 bytes are used, but it's easier to just set all to 0xFF + //see page 27 of the datasheet for the memory map - CHECK_RETURN(writeCMD(CLRFLAG, flagsbuffer, 6)); //clear flags, - CHECK_RETURN(writeCMD(CLOVUV, flagsbuffer, 6)); //OVUV flags - CHECK_RETURN(writeCMD(ADCV | ADCV_CONT | ADCV_RD, flagsbuffer, 0)); //start continuous cell voltage measurement with redundancy - CHECK_RETURN(writeCMD(ADAX | ADAX_CONV_ALL, flagsbuffer, 0)); //start aux measurement + CHECK_RETURN(writeCMD(CLRFLAG, flagsbuffer, 6)); //clear flags, + CHECK_RETURN(writeCMD(CLOVUV, flagsbuffer, 6)); //OVUV flags + CHECK_RETURN(writeCMD(ADCV | ADCV_CONT | ADCV_RD, flagsbuffer, 0)); //start continuous cell voltage measurement with redundancy + CHECK_RETURN(writeCMD(ADAX | ADAX_CONV_ALL, flagsbuffer, 0)); //start aux measurement return 0; } @@ -229,15 +229,19 @@ HAL_StatusTypeDef amsSelfTest() { return 0; } HAL_StatusTypeDef amsConfigOverUnderVoltage(uint16_t overVoltage, uint16_t underVoltage) { uint8_t buffer[CMD_BUFFER_SIZE(CFG_GROUP_B_SIZE)] = {}; - if (underVoltage & 0xF000 || overVoltage & 0xF000) { // only 12 bits allowed - return HAL_ERROR; - } - - debug_log(LOG_LEVEL_INFO, "Configuring OV/UV thresholds to %f/%f", mV_from_ADBMS6830(overVoltage), - mV_from_ADBMS6830(underVoltage)); + debug_log(LOG_LEVEL_INFO, "Configuring OV/UV thresholds to %u mV (%u internal) / %u mV (%u internal)", overVoltage, + mV_to_ADBMS6830(overVoltage), underVoltage, mV_to_ADBMS6830(underVoltage)); CHECK_RETURN(readCMD(RDCFGB, buffer, CFG_GROUP_B_SIZE)); + overVoltage = mV_to_ADBMS6830(overVoltage); + underVoltage = mV_to_ADBMS6830(underVoltage); + + if (underVoltage & 0xF000 || overVoltage & 0xF000) { // only 12 bits allowed + debug_log(LOG_LEVEL_ERROR, "Invalid OV/UV thresholds: %u mV / %u mV", overVoltage, underVoltage); + return HAL_ERROR; + } + for (size_t i = 0; i < N_BMS; i++) { size_t offset = BUFFER_BMS_OFFSET(i, CFG_GROUP_B_SIZE);