From 75d1d78714f7c7e32464bd1e583c3936a3d52db0 Mon Sep 17 00:00:00 2001 From: kbracher Date: Sun, 9 Mar 2025 00:59:13 +0100 Subject: [PATCH] refactor: improve log formatting --- .../ADBMS6830B_Driver/Core/Inc/ADBMS_Driver.h | 17 ++++++++++++ .../Core/Src/ADBMS_Abstraction.c | 1 - .../Core/Src/ADBMS_LL_Driver.c | 10 ++++--- AMS_Master_Code/Core/Src/main.c | 26 +++++++++---------- 4 files changed, 36 insertions(+), 18 deletions(-) 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 841c95d..28401f1 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 @@ -23,6 +23,23 @@ typedef enum : uint16_t { NUM_ERROR_KINDS } ADBMS_Status; +static const char* ADBMS_Status_Names[NUM_ERROR_KINDS] = { + "ADBMS_NO_ERROR", + "ADBMS_OVERTEMP", + "ADBMS_UNDERTEMP", + "ADBMS_OVERVOLT", + "ADBMS_UNDERVOLT", + "ADBMS_OPENWIRE", + "ADBMS_INTERNAL_BMS_TIMEOUT", + "ADBMS_INTERNAL_BMS_CHECKSUM_FAIL", + "ADBMS_INTERNAL_BMS_OVERTEMP", + "ADBMS_INTERNAL_BMS_FAULT" +}; + +static inline const char* ADBMS_Status_ToString(ADBMS_Status status) { + return (status < NUM_ERROR_KINDS) ? ADBMS_Status_Names[status] : "UNKNOWN"; +} + typedef struct { ADBMS_Status status : 16; int16_t bms_id : 16; // the BMS that caused the error, zero if no error, -1 if unknown BMS 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 267a82d..8aadc2b 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 @@ -209,7 +209,6 @@ HAL_StatusTypeDef amsConfigBalancing(const uint32_t channels[static N_BMS], uint for (size_t j = 0; j < 2; j++) { debug_log_cont(LOG_LEVEL_DEBUG, "%x %x ", rxbufB[offsetB + j] & 0x0F, rxbufB[offsetB + j] >> 4); } - debug_log_cont(LOG_LEVEL_DEBUG, "\n"); } } diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c index 309ce91..0914936 100644 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c @@ -163,7 +163,6 @@ static void print_spi_details() { debug_log_cont(LOG_LEVEL_ERROR, "%s ", errors[i].label); } } - debug_log_cont(LOG_LEVEL_ERROR, "\n"); } HAL_StatusTypeDef ___writeCMD(uint16_t command, uint8_t * args, size_t arglen) { @@ -206,7 +205,8 @@ HAL_StatusTypeDef ___writeCMD(uint16_t command, uint8_t * args, size_t arglen) { } if (ret != HAL_OK) { - debug_log(LOG_LEVEL_ERROR, "STM32 SPI HAL returned error %s\n SPI error bits: ", HAL_Statuses[ret]); + debug_log(LOG_LEVEL_ERROR, "STM32 SPI HAL returned error %s", HAL_Statuses[ret]); + debug_log(LOG_LEVEL_ERROR, "SPI error bits: "); print_spi_details(); } @@ -223,7 +223,8 @@ HAL_StatusTypeDef ___readCMD(uint16_t command, uint8_t * buffer, size_t arglen) mcuAdbmsCSHigh(); if (status != HAL_OK) { - debug_log(LOG_LEVEL_ERROR, "STM32 SPI HAL returned error %s\n SPI error bits: ", HAL_Statuses[status]); + debug_log(LOG_LEVEL_ERROR, "STM32 SPI HAL returned error %s", HAL_Statuses[status]); + debug_log(LOG_LEVEL_ERROR, "SPI error bits: "); print_spi_details(); return status; } @@ -268,7 +269,8 @@ HAL_StatusTypeDef __pollCMD(uint16_t command, uint8_t waitTime) { mcuAdbmsCSHigh(); if (status != HAL_OK) { - debug_log(LOG_LEVEL_ERROR, "STM32 SPI HAL returned error %s\n SPI error bits: ", HAL_Statuses[status]); + debug_log(LOG_LEVEL_ERROR, "STM32 SPI HAL returned error %s", HAL_Statuses[status]); + debug_log(LOG_LEVEL_ERROR, "SPI error bits: "); print_spi_details(); return status; } diff --git a/AMS_Master_Code/Core/Src/main.c b/AMS_Master_Code/Core/Src/main.c index 669b6db..72dc366 100644 --- a/AMS_Master_Code/Core/Src/main.c +++ b/AMS_Master_Code/Core/Src/main.c @@ -101,11 +101,11 @@ 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..."); - int status = -1; - while (status != ADBMS_NO_ERROR) { - status = AMS_Init(&hspi1).status; - if (status != ADBMS_NO_ERROR) { - debug_log(LOG_LEVEL_ERROR, "Failed to initialize BMS, AMS_Init returned %d", status); + ADBMS_DetailedStatus status = {}; + 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); } } @@ -116,9 +116,9 @@ int main(void) int count = 0; while (1) { - status = AMS_Idle_Loop().status; - if (status != ADBMS_NO_ERROR) { - debug_log(LOG_LEVEL_ERROR, "AMS_Idle_Loop returned %d", status); + 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); HAL_Delay(2000); AMS_Init(&hspi1); continue; @@ -129,13 +129,13 @@ int main(void) // 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", + 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", + 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", + 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", @@ -144,11 +144,11 @@ int main(void) // Print GPIO values debug_log(LOG_LEVEL_INFO, " GPIO values:"); - debug_log(LOG_LEVEL_INFO, " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d", + debug_log(LOG_LEVEL_INFO, " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d", modules[i].GPIO_Values[0], modules[i].GPIO_Values[1], modules[i].GPIO_Values[2], modules[i].GPIO_Values[3], modules[i].GPIO_Values[4]); - debug_log(LOG_LEVEL_INFO, " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d", + debug_log(LOG_LEVEL_INFO, " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d", modules[i].GPIO_Values[5], modules[i].GPIO_Values[6], modules[i].GPIO_Values[7], modules[i].GPIO_Values[8], modules[i].GPIO_Values[9]);