diff --git a/AMS_Master_Code/Core/Inc/battery.h b/AMS_Master_Code/Core/Inc/battery.h index 42e6cf1..3efa503 100644 --- a/AMS_Master_Code/Core/Inc/battery.h +++ b/AMS_Master_Code/Core/Inc/battery.h @@ -8,8 +8,10 @@ #include extern uint16_t min_voltage; extern int16_t max_temp; +extern int16_t cellTemps[N_BMS][N_CELLS]; -void battery_init(SPI_HandleTypeDef* hspi); -void battery_update(); +HAL_StatusTypeDef battery_init(SPI_HandleTypeDef* hspi); +HAL_StatusTypeDef battery_update(); +void print_battery_info(); #endif // __BATTERY_H \ No newline at end of file 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 020b3f1..3fd188f 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 @@ -79,7 +79,6 @@ struct ADBMS6830_Internal_Status { typedef struct { int16_t cellVoltages[MAXIMUM_CELL_VOLTAGES]; int16_t auxVoltages[MAXIMUM_AUX_VOLTAGES]; - uint16_t cellTemps[MAXIMUM_AUX_VOLTAGES]; uint32_t bmsID; struct ADBMS6830_Internal_Status status; 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 899cff9..e26e811 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 @@ -128,18 +128,17 @@ HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module (*module)[N_BMS]) { // return HAL_BUSY; } - const size_t auxGroupSizes[] = {AUX_GROUP_A_SIZE, AUX_GROUP_B_SIZE, AUX_GROUP_C_SIZE, AUX_GROUP_D_SIZE}; - const size_t auxVoltagesPerGroup[] = {3, 3, 3, 1}; // Number of voltages in each group - const uint8_t auxCommands[] = {RDAUXA, RDAUXB, RDAUXC, RDAUXD}; + constexpr size_t auxGroupSizes[] = {AUX_GROUP_A_SIZE, AUX_GROUP_B_SIZE, AUX_GROUP_C_SIZE, AUX_GROUP_D_SIZE}; + constexpr size_t auxVoltagesPerGroup[] = {3, 3, 3, 1}; // Number of voltages in each group + constexpr uint8_t auxCommands[] = {RDAUXA, RDAUXB, RDAUXC, RDAUXD}; - // Getting auxVoltages from the BMS and calculating cellTemps + // Getting auxVoltages from the BMS for (size_t group = 0; group < 4; group++) { CHECK_RETURN(readCMD(auxCommands[group], rxbuf, auxGroupSizes[group])); for (size_t i = 0; i < N_BMS; i++) { size_t offset = BUFFER_BMS_OFFSET(i, auxGroupSizes[group]); for (size_t j = 0; j < auxVoltagesPerGroup[group]; j++) { (*module)[i].auxVoltages[group * 3 + j] = mV_from_ADBMS6830(rxbuf[offset + j * 2] | (rxbuf[offset + j * 2 + 1] << 8)); - (*module)[i].cellTemps[group * 3 + j] = ntc_mv_to_celsius((*module)[i].auxVoltages[group * 3 + j]); } } } diff --git a/AMS_Master_Code/Core/Lib/can-halal b/AMS_Master_Code/Core/Lib/can-halal index 0b91166..4e3bb02 160000 --- a/AMS_Master_Code/Core/Lib/can-halal +++ b/AMS_Master_Code/Core/Lib/can-halal @@ -1 +1 @@ -Subproject commit 0b91166780aedce4de1defd36dfabde6c04a2961 +Subproject commit 4e3bb026f88a7ee5a89ec48dc10281e8e0a3175a diff --git a/AMS_Master_Code/Core/Src/battery.c b/AMS_Master_Code/Core/Src/battery.c index 240bf12..79fa77a 100644 --- a/AMS_Master_Code/Core/Src/battery.c +++ b/AMS_Master_Code/Core/Src/battery.c @@ -1,29 +1,189 @@ #include "battery.h" #include "ADBMS_Driver.h" +#include "NTC.h" +#include "config_ADBMS6830.h" +#include + +#define SWO_LOG_PREFIX "[BATTERY] " +#include "swo_log.h" uint16_t min_voltage = 0xFFFF; int16_t max_temp = -1; +int16_t cellTemps[N_BMS][N_CELLS]; -void battery_init(SPI_HandleTypeDef* hspi) { - AMS_Init(hspi); +HAL_StatusTypeDef battery_init(SPI_HandleTypeDef *hspi) { + auto ret = AMS_Init(hspi); + if (ret.status != ADBMS_NO_ERROR) { + debug_log(LOG_LEVEL_ERROR, "Failed to initialize BMS: %s", + ADBMS_Status_ToString(ret.status)); + if (ret.bms_id != -1) { + debug_log_cont(LOG_LEVEL_ERROR, " (on BMS ID: %hd)", ret.bms_id); + } + return HAL_ERROR; + } + debug_log(LOG_LEVEL_INFO, "Battery initialized successfully"); + return HAL_OK; } -void battery_update() { - if (AMS_Idle_Loop().status != ADBMS_NO_ERROR -) { - // Handle error - return; +HAL_StatusTypeDef battery_update() { + auto ret = AMS_Idle_Loop(); + if (ret.status != ADBMS_NO_ERROR) { + debug_log(LOG_LEVEL_ERROR, "Failed to update battery data: %s", + ADBMS_Status_ToString(ret.status)); + if (ret.bms_id != -1) { + debug_log_cont(LOG_LEVEL_ERROR, " (on BMS ID: %hd)", ret.bms_id); } - for (size_t i = 0; i < N_BMS; i++) { - for (size_t j = 0; j < N_CELLS; j++) { - if (modules[i].cellVoltages[j] > min_voltage) { - min_voltage = modules[i].cellVoltages[j]; - } - if (modules[i].cellTemps[0] > max_temp) { - //refactor this to use ntc_mv_to_celsius - // max_temp = ntc_mv_to_celsius(modules[i].auxVoltages[j]); - max_temp = modules[i].cellTemps[j]; - } - } + return HAL_ERROR; + } + + min_voltage = 0xFFFF; + max_temp = -1; + + for (size_t i = 0; i < N_BMS; i++) { + for (size_t j = 0; j < N_CELLS; j++) { + if (modules[i].cellVoltages[j] > min_voltage) { + min_voltage = modules[i].cellVoltages[j]; + } } + for (size_t j = 0; j < 10; j++) { //10 GPIOs + cellTemps[i][j] = ntc_mv_to_celsius(modules[i].auxVoltages[j]); + + if (cellTemps[i][j] > max_temp) { + max_temp = cellTemps[i][j]; + } + } + } + + return HAL_OK; +} + +void print_battery_info() { + for (size_t i = 0; i < N_BMS; i++) { + debug_log(LOG_LEVEL_INFO, "Module %d status:", i); + + // Print cell voltages in 4x4 format + debug_log(LOG_LEVEL_INFO, " Cell voltages (mV):"); + debug_log(LOG_LEVEL_INFO, " C0: %4d C1: %4d C2: %4d C3: %4d", + modules[i].cellVoltages[0], modules[i].cellVoltages[1], + modules[i].cellVoltages[2], modules[i].cellVoltages[3]); + debug_log(LOG_LEVEL_INFO, " C4: %4d C5: %4d C6: %4d C7: %4d", + modules[i].cellVoltages[4], modules[i].cellVoltages[5], + modules[i].cellVoltages[6], modules[i].cellVoltages[7]); + debug_log(LOG_LEVEL_INFO, " C8: %4d C9: %4d C10: %4d C11: %4d", + modules[i].cellVoltages[8], modules[i].cellVoltages[9], + modules[i].cellVoltages[10], modules[i].cellVoltages[11]); + debug_log(LOG_LEVEL_INFO, " C12: %4d C13: %4d C14: %4d C15: %4d", + modules[i].cellVoltages[12], modules[i].cellVoltages[13], + modules[i].cellVoltages[14], modules[i].cellVoltages[15]); + + // Print GPIO values + debug_log(LOG_LEVEL_INFO, " GPIO voltages (mV):"); + debug_log(LOG_LEVEL_INFO, + " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d", + modules[i].auxVoltages[0], modules[i].auxVoltages[1], + modules[i].auxVoltages[2], modules[i].auxVoltages[3], + modules[i].auxVoltages[4]); + debug_log(LOG_LEVEL_INFO, + " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d", + modules[i].auxVoltages[5], modules[i].auxVoltages[6], + modules[i].auxVoltages[7], modules[i].auxVoltages[8], + modules[i].auxVoltages[9]); + + // Print temperatures + debug_log(LOG_LEVEL_INFO, " GPIO as temperatures (°C):"); + debug_log(LOG_LEVEL_INFO, + " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d", + cellTemps[i][0], cellTemps[i][1], cellTemps[i][2], + cellTemps[i][3], cellTemps[i][4]); + debug_log(LOG_LEVEL_INFO, + " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d", + cellTemps[i][5], cellTemps[i][6], cellTemps[i][7], + cellTemps[i][8], cellTemps[i][9]); + + debug_log(LOG_LEVEL_INFO, + " Internal temp: %d, VAnalog: %d, VDigital: %d, VRef: %d", + modules[i].internalDieTemp, modules[i].analogSupplyVoltage, + modules[i].digitalSupplyVoltage, modules[i].refVoltage); + + // Print error flags if any are set + bool hasFlags = false; + char flagBuffer[128] = ""; + char *bufPos = flagBuffer; + + if (modules[i].status.CS_FLT) { + bufPos = stpcpy(bufPos, "CS_FLT "); + hasFlags = true; + } + if (modules[i].status.SMED) { + bufPos = stpcpy(bufPos, "SMED "); + hasFlags = true; + } + if (modules[i].status.SED) { + bufPos = stpcpy(bufPos, "SED "); + hasFlags = true; + } + if (modules[i].status.CMED) { + bufPos = stpcpy(bufPos, "CMED "); + hasFlags = true; + } + if (modules[i].status.CED) { + bufPos = stpcpy(bufPos, "CED "); + hasFlags = true; + } + if (modules[i].status.VD_UV) { + bufPos = stpcpy(bufPos, "VD_UV "); + hasFlags = true; + } + if (modules[i].status.VD_OV) { + bufPos = stpcpy(bufPos, "VD_OV "); + hasFlags = true; + } + if (modules[i].status.VA_UV) { + bufPos = stpcpy(bufPos, "VA_UV "); + hasFlags = true; + } + if (modules[i].status.VA_OV) { + bufPos = stpcpy(bufPos, "VA_OV "); + hasFlags = true; + } + if (modules[i].status.THSD) { + bufPos = stpcpy(bufPos, "THSD "); + hasFlags = true; + } + if (modules[i].status.SLEEP) { + bufPos = stpcpy(bufPos, "SLEEP "); + hasFlags = true; + } + if (modules[i].status.SPIFLT) { + bufPos = stpcpy(bufPos, "SPIFLT "); + hasFlags = true; + } + if (modules[i].status.COMPARE) { + bufPos = stpcpy(bufPos, "COMPARE "); + hasFlags = true; + } + if (modules[i].status.VDE) { + bufPos = stpcpy(bufPos, "VDE "); + hasFlags = true; + } + if (modules[i].status.VDEL) { + bufPos = stpcpy(bufPos, "VDEL "); + hasFlags = true; + } + + debug_log(LOG_LEVEL_INFO, " Status flags: %s", + hasFlags ? flagBuffer : "[none]"); + + debug_log(LOG_LEVEL_INFO, " Conversion counter: %d", + modules[i].status.CCTS); + + // Check for over/under voltage + if (modules[i].overVoltage || modules[i].underVoltage) { + debug_log(LOG_LEVEL_WARNING, + " Module %d voltage issues - OV: 0x%08lX, UV: 0x%08lX", i, + modules[i].overVoltage, modules[i].underVoltage); + } + + debug_log(LOG_LEVEL_INFO, " ---------------"); + } } \ No newline at end of file diff --git a/AMS_Master_Code/Core/Src/can.c b/AMS_Master_Code/Core/Src/can.c index 21c8375..f2142aa 100644 --- a/AMS_Master_Code/Core/Src/can.c +++ b/AMS_Master_Code/Core/Src/can.c @@ -50,7 +50,7 @@ HAL_StatusTypeDef can_send_error(TSErrorKind kind, uint8_t arg) { return ftcan_transmit(CAN_ID_AMS_ERROR, data, sizeof(data)); } -void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data) { +void ftcan_msg_received_cb(uint16_t id, size_t, const uint8_t *data) { if ((id & 0xFF0) == CAN_ID_SHUNT_BASE) { shunt_handle_can_msg(id, data); return; diff --git a/AMS_Master_Code/Core/Src/main.c b/AMS_Master_Code/Core/Src/main.c index 82d1080..4f3256f 100644 --- a/AMS_Master_Code/Core/Src/main.c +++ b/AMS_Master_Code/Core/Src/main.c @@ -21,11 +21,9 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ -#include "ADBMS_Driver.h" -#include "config_ADBMS6830.h" +#include "battery.h" #define SWO_LOG_PREFIX "[MAIN] " #include "swo_log.h" -#include //copied from master23 #include "can.h" @@ -73,7 +71,10 @@ int hv_active; int neg_air_closed; int pos_air_closed; int precharge_closed; -int pre_and_air_open; // used to be:int precharge_opened = 0; now we read if PC is open from the pin -> high if open +int pre_and_air_open; // used to be:int precharge_opened = 0; now we read if PC is open from the pin -> high if open + +#warning just a fix to make the code compile +int precharge_opened = 0; /* USER CODE END PV */ @@ -174,18 +175,16 @@ int main(void) debug_clear_console(); debug_log(LOG_LEVEL_INFO, "AMS_Master on %s (%s), compiled at %s", COMMIT_BRANCH, COMMIT_HASH, COMPILE_DATE); debug_log(LOG_LEVEL_INFO, "Starting BMS..."); - ADBMS_DetailedStatus status = {ADBMS_INTERNAL_BMS_FAULT, -1}; - while (status.status != ADBMS_NO_ERROR) { - status = AMS_Init(&hspi1); - if (status.status != ADBMS_NO_ERROR) { - debug_log(LOG_LEVEL_ERROR, "Failed to initialize BMS, AMS_Init returned %u (%s) on BMS %d", status.status, ADBMS_Status_ToString(status.status), status.bms_id); - HAL_Delay(2000); - } + auto ret = battery_init(&hspi1); + while (ret != HAL_OK) { + debug_log(LOG_LEVEL_ERROR, "Failed to initialize BMS!"); + HAL_Delay(100); + debug_log(LOG_LEVEL_INFO, "Retrying BMS initialization..."); + ret = battery_init(&hspi1); } - //init fot master functions + // init for master functions can_init(&hfdcan1); - //slaves_init(); shunt_init(); ts_sm_init(); soc_init(); @@ -203,149 +202,28 @@ int main(void) //left over from slave communication test, could be nicer and in an additional function !! if (error_count > 25) { debug_log(LOG_LEVEL_ERROR, "Too many errors, restarting BMS..."); - status = AMS_Init(&hspi1); - if (status.status != ADBMS_NO_ERROR) { - debug_log(LOG_LEVEL_ERROR, "Failed to initialize BMS, AMS_Init returned %u (%s) on BMS %d", status.status, ADBMS_Status_ToString(status.status), status.bms_id); - HAL_Delay(2000); - continue; + HAL_Delay(1000); + ret = battery_init(&hspi1); + while (ret != HAL_OK) { + debug_log(LOG_LEVEL_ERROR, "Failed to initialize BMS!"); + HAL_Delay(1000); + debug_log(LOG_LEVEL_INFO, "Retrying BMS initialization..."); + ret = battery_init(&hspi1); } error_count = 0; } - status = AMS_Idle_Loop(); - if (status.status != ADBMS_NO_ERROR) { - debug_log(LOG_LEVEL_ERROR, "AMS_Idle_Loop returned %u (%s) on BMS %d", status.status, ADBMS_Status_ToString(status.status), status.bms_id); - error_count++; - } uint32_t lastTimestamp = HAL_GetTick(); - if (count % 4 == 0) { - for (size_t i = 0; i < N_BMS; i++) { - debug_log(LOG_LEVEL_INFO, "Module %d status:", i); - - // Print cell voltages in 4x4 format - debug_log(LOG_LEVEL_INFO, " Cell voltages (mV):"); - debug_log(LOG_LEVEL_INFO, " C0: %4d C1: %4d C2: %4d C3: %4d", - modules[i].cellVoltages[0], modules[i].cellVoltages[1], - modules[i].cellVoltages[2], modules[i].cellVoltages[3]); - debug_log(LOG_LEVEL_INFO, " C4: %4d C5: %4d C6: %4d C7: %4d", - modules[i].cellVoltages[4], modules[i].cellVoltages[5], - modules[i].cellVoltages[6], modules[i].cellVoltages[7]); - debug_log(LOG_LEVEL_INFO, " C8: %4d C9: %4d C10: %4d C11: %4d", - modules[i].cellVoltages[8], modules[i].cellVoltages[9], - modules[i].cellVoltages[10], modules[i].cellVoltages[11]); - debug_log(LOG_LEVEL_INFO, " C12: %4d C13: %4d C14: %4d C15: %4d", - modules[i].cellVoltages[12], modules[i].cellVoltages[13], - modules[i].cellVoltages[14], modules[i].cellVoltages[15]); - - // Print GPIO values - debug_log(LOG_LEVEL_INFO, " GPIO voltages (mV):"); - debug_log(LOG_LEVEL_INFO, " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d", - modules[i].auxVoltages[0], modules[i].auxVoltages[1], - modules[i].auxVoltages[2], modules[i].auxVoltages[3], - modules[i].auxVoltages[4]); - debug_log(LOG_LEVEL_INFO, " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d", - modules[i].auxVoltages[5], modules[i].auxVoltages[6], - modules[i].auxVoltages[7], modules[i].auxVoltages[8], - modules[i].auxVoltages[9]); - - // Print temperatures - debug_log(LOG_LEVEL_INFO, " GPIO as temperatures (°C):"); - debug_log(LOG_LEVEL_INFO, " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d", - modules[i].cellTemps[0], modules[i].cellTemps[1], - modules[i].cellTemps[2], modules[i].cellTemps[3], modules[i].cellTemps[4]); - debug_log(LOG_LEVEL_INFO, " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d", - modules[i].cellTemps[5], modules[i].cellTemps[6], - modules[i].cellTemps[7], modules[i].cellTemps[8], modules[i].cellTemps[9]); - - debug_log(LOG_LEVEL_INFO, " Internal temp: %d, VAnalog: %d, VDigital: %d, VRef: %d", - modules[i].internalDieTemp, modules[i].analogSupplyVoltage, - modules[i].digitalSupplyVoltage, modules[i].refVoltage); - - // Print error flags if any are set - bool hasFlags = false; - char flagBuffer[128] = ""; - char *bufPos = flagBuffer; - - if (modules[i].status.CS_FLT) { - bufPos = stpcpy(bufPos, "CS_FLT "); - hasFlags = true; - } - if (modules[i].status.SMED) { - bufPos = stpcpy(bufPos, "SMED "); - hasFlags = true; - } - if (modules[i].status.SED) { - bufPos = stpcpy(bufPos, "SED "); - hasFlags = true; - } - if (modules[i].status.CMED) { - bufPos = stpcpy(bufPos, "CMED "); - hasFlags = true; - } - if (modules[i].status.CED) { - bufPos = stpcpy(bufPos, "CED "); - hasFlags = true; - } - if (modules[i].status.VD_UV) { - bufPos = stpcpy(bufPos, "VD_UV "); - hasFlags = true; - } - if (modules[i].status.VD_OV) { - bufPos = stpcpy(bufPos, "VD_OV "); - hasFlags = true; - } - if (modules[i].status.VA_UV) { - bufPos = stpcpy(bufPos, "VA_UV "); - hasFlags = true; - } - if (modules[i].status.VA_OV) { - bufPos = stpcpy(bufPos, "VA_OV "); - hasFlags = true; - } - if (modules[i].status.THSD) { - bufPos = stpcpy(bufPos, "THSD "); - hasFlags = true; - } - if (modules[i].status.SLEEP) { - bufPos = stpcpy(bufPos, "SLEEP "); - hasFlags = true; - } - if (modules[i].status.SPIFLT) { - bufPos = stpcpy(bufPos, "SPIFLT "); - hasFlags = true; - } - if (modules[i].status.COMPARE) { - bufPos = stpcpy(bufPos, "COMPARE "); - hasFlags = true; - } - if (modules[i].status.VDE) { - bufPos = stpcpy(bufPos, "VDE "); - hasFlags = true; - } - if (modules[i].status.VDEL) { - bufPos = stpcpy(bufPos, "VDEL "); - hasFlags = true; - } - - debug_log(LOG_LEVEL_INFO, " Status flags: %s", hasFlags ? flagBuffer : "[none]"); - - debug_log(LOG_LEVEL_INFO, " Conversion counter: %d", modules[i].status.CCTS); - - // Check for over/under voltage - if (modules[i].overVoltage || modules[i].underVoltage) { - debug_log(LOG_LEVEL_WARNING, " Module %d voltage issues - OV: 0x%08lX, UV: 0x%08lX", - i, modules[i].overVoltage, modules[i].underVoltage); - } - - debug_log(LOG_LEVEL_INFO, " ---------------"); - } - // get time difference - debug_log(LOG_LEVEL_INFO, " Time since last update: %d ms", HAL_GetTick() - lastTimestamp); - } update_sdc(); update_tsal_signals(); - //slaves_check(); + if (battery_update() != HAL_OK) { + error_count++; + } + if (count % 4 == 0) { + print_battery_info(); + debug_log(LOG_LEVEL_INFO, " Time since last update: %lu ms", HAL_GetTick() - lastTimestamp); + } shunt_check(); ts_sm_update(); soc_update();