refactor: change OV/UV thresholds to use mV

This commit is contained in:
Kilian Bracher 2025-02-25 20:06:19 +01:00
parent d6de68c358
commit 90cd1330c2
Signed by: k.bracher
SSH Key Fingerprint: SHA256:mXpyZkK7RDiJ7qeHCKJX108woM0cl5TrCvNBJASu6lM
3 changed files with 26 additions and 18 deletions
AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core

@ -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]);

@ -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,

@ -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);