From 8a82ad4ceec5c83f2d01d21ad7d76112fb9dbd40 Mon Sep 17 00:00:00 2001 From: kbracher <k.bracher@fasttube.de> Date: Thu, 27 Mar 2025 16:53:11 +0100 Subject: [PATCH 1/9] further optimize ntc math --- AMS_Master_Code/Core/Inc/NTC.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/AMS_Master_Code/Core/Inc/NTC.h b/AMS_Master_Code/Core/Inc/NTC.h index a47dcac..b5bf2d5 100644 --- a/AMS_Master_Code/Core/Inc/NTC.h +++ b/AMS_Master_Code/Core/Inc/NTC.h @@ -1,18 +1,20 @@ #include <math.h> +#include <stdint.h> #define NTC_A1 0.003354016f // see NTC RT csv from Vishay #define NTC_B1 0.000300131f #define NTC_C1 5.08516E-06f #define NTC_D1 2.18765E-07f -#define VREF 3000.0 +#define VREF 3000.0f // More efficient (?) calc using: // R_T = R_Pullup / (V_REF / ADC - 1) // With R_T/R_0 and R_0 = R_T@25C // R_T/R_0 = 1 / V_REF / ADC - 1 +[[gnu::optimize("fast-math")]] static inline int ntc_mv_to_celsius(int16_t adc) { - float log_ohms = logf(1/((VREF/adc)-1)); + float log_ohms = logf(1 / ((VREF / adc) - 1)); return 1.0f / (NTC_A1 + NTC_B1 * log_ohms + NTC_C1 * log_ohms * log_ohms + NTC_D1 * log_ohms * log_ohms * log_ohms) - 273.15f; } \ No newline at end of file From ae54db62e0651ec22fa2823d88604440a4be0c74 Mon Sep 17 00:00:00 2001 From: kbracher <k.bracher@fasttube.de> Date: Thu, 27 Mar 2025 16:56:54 +0100 Subject: [PATCH 2/9] fix: make adbms overtemp threshold configurable --- AMS_Master_Code/Core/Inc/config_ADBMS6830.h | 2 +- .../Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_Driver.h | 2 +- .../Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/AMS_Master_Code/Core/Inc/config_ADBMS6830.h b/AMS_Master_Code/Core/Inc/config_ADBMS6830.h index 4b60e4c..dedb364 100644 --- a/AMS_Master_Code/Core/Inc/config_ADBMS6830.h +++ b/AMS_Master_Code/Core/Inc/config_ADBMS6830.h @@ -4,7 +4,7 @@ #include "main.h" #define N_BMS 1 -#define ADBMS_SPI_TIMEOUT 100 // Timeout in ms +#define ADBMS_MAX_CHIP_TEMP 110 // max temperature of ADBMS6830B (not battery) in C [[maybe_unused]] static inline void mcuAdbmsCSLow() { HAL_GPIO_WritePin(AMS_CS_GPIO_Port, AMS_CS_Pin, GPIO_PIN_RESET); 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 d3cdd65..c3b958d 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 @@ -81,7 +81,7 @@ typedef struct { int16_t auxVoltages[MAXIMUM_AUX_VOLTAGES]; struct ADBMS6830_Internal_Status status; - uint16_t internalDieTemp; + int16_t internalDieTemp; uint16_t analogSupplyVoltage; uint16_t digitalSupplyVoltage; uint16_t sumOfCellMeasurements; diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c index e30a1ed..6fb50f7 100644 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c @@ -106,8 +106,7 @@ ADBMS_DetailedStatus AMS_Idle_Loop() { return (ADBMS_DetailedStatus){ADBMS_UNDERVOLT, match - 1}; } - // TODO: replace with the correct threshold for the internal die temperature - if ((match = any(module.internalDieTemp > 28000 || module.status.THSD))) { + if ((match = any(module.internalDieTemp > ADBMS_MAX_CHIP_TEMP || module.status.THSD))) { return (ADBMS_DetailedStatus){ADBMS_INTERNAL_BMS_OVERTEMP, match - 1}; } From 4dda2084a3766fc4d25fccf4df399551562450fa Mon Sep 17 00:00:00 2001 From: kbracher <k.bracher@fasttube.de> Date: Thu, 27 Mar 2025 19:01:45 +0100 Subject: [PATCH 3/9] refactor: move more options to config file --- AMS_Master_Code/Core/Inc/config_ADBMS6830.h | 8 ++++++-- .../Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_HighLevel.h | 2 -- .../Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c | 5 +---- .../Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c | 4 +--- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/AMS_Master_Code/Core/Inc/config_ADBMS6830.h b/AMS_Master_Code/Core/Inc/config_ADBMS6830.h index dedb364..2fdaa6a 100644 --- a/AMS_Master_Code/Core/Inc/config_ADBMS6830.h +++ b/AMS_Master_Code/Core/Inc/config_ADBMS6830.h @@ -4,13 +4,17 @@ #include "main.h" #define N_BMS 1 +#define N_CELLS 16 #define ADBMS_MAX_CHIP_TEMP 110 // max temperature of ADBMS6830B (not battery) in C +#define ADBMS_SPI_TIMEOUT 50 // Timeout in ms -[[maybe_unused]] static inline void mcuAdbmsCSLow() { +[[maybe_unused, gnu::always_inline]] +static inline void mcuAdbmsCSLow() { HAL_GPIO_WritePin(AMS_CS_GPIO_Port, AMS_CS_Pin, GPIO_PIN_RESET); } -[[maybe_unused]] static inline void mcuAdbmsCSHigh() { +[[maybe_unused, gnu::always_inline]] +static inline void mcuAdbmsCSHigh() { HAL_GPIO_WritePin(AMS_CS_GPIO_Port, AMS_CS_Pin, GPIO_PIN_SET); } diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_HighLevel.h b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_HighLevel.h index 2222503..ced8255 100644 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_HighLevel.h +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_HighLevel.h @@ -30,6 +30,4 @@ extern Cell_Module modules[N_BMS]; extern uint32_t balancedCells; extern bool BalancingActive; -extern uint8_t numberofCells; - #endif /* INC_AMS_HIGHLEVEL_H_ */ 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 4db1611..bd3f2f1 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 @@ -12,8 +12,6 @@ #include "swo_log.h" #include <stddef.h> -extern uint8_t numberofCells; - static const char* const HAL_Statuses[] = {"HAL_OK", "HAL_ERROR", "HAL_BUSY", "HAL_TIMEOUT"}; #define CHECK_RETURN(x) \ @@ -275,8 +273,7 @@ HAL_StatusTypeDef amsCheckUnderOverVoltage(Cell_Module (*module)[N_BMS]) { (*module)[i].overVoltage = 0; (*module)[i].underVoltage = 0; - for (size_t j = 0; j < numberofCells; - j++) { // ov/uv flags are 1-bit flags for each cell C0UV, C0OV, C1UV, C1OV, ... + for (size_t j = 0; j < N_CELLS; j++) { // ov/uv flags are 1-bit flags for each cell C0UV, C0OV, C1UV, C1OV, ... (*module)[i].underVoltage |= (ov_uv_data >> (j * 2)) & 0x01; (*module)[i].overVoltage |= (ov_uv_data >> (j * 2 + 1)) & 0x01; } diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c index 6fb50f7..afd150f 100644 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_HighLevel.c @@ -18,8 +18,6 @@ Cell_Module modules[N_BMS] = {}; uint32_t balancedCells = 0; bool balancingActive = false; -uint8_t numberofCells = 16; - uint8_t packetChecksumFails = 0; #define MAX_PACKET_CHECKSUM_FAILS 5 @@ -37,7 +35,7 @@ static constexpr ADBMS_DetailedStatus NO_ERROR = {ADBMS_NO_ERROR}; ADBMS_DetailedStatus AMS_Init(SPI_HandleTypeDef* hspi) { debug_log(LOG_LEVEL_INFO, "ADBMS6830B HAL - configured for %d controllers and %d cells per controller...", N_BMS, - numberofCells); + N_CELLS); if (initAMS(hspi) != HAL_OK) { debug_log(LOG_LEVEL_ERROR, "ADBMS6830B HAL - initialization failed"); return (ADBMS_DetailedStatus){ADBMS_INTERNAL_BMS_FAULT, -1}; From c7be10b37f59cd4ce78fb9ea702e9f08a3b92cb9 Mon Sep 17 00:00:00 2001 From: kbracher <k.bracher@fasttube.de> Date: Thu, 27 Mar 2025 22:23:17 +0100 Subject: [PATCH 4/9] fix polling hopefully --- .../Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a35c61c..8d4a2b9 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 @@ -271,5 +271,5 @@ HAL_StatusTypeDef __pollCMD(uint16_t command, uint8_t waitTime) { return status; } - return ((buffer[4 + (N_BMS * 2)] & 0x0F) == 0x0) ? HAL_BUSY : HAL_OK; //SDO goes high when data is ready + return ((buffer[sizeof buffer] & 0x0F) == 0x0) ? HAL_BUSY : HAL_OK; //SDO goes high when data is ready } \ No newline at end of file From c9c9ac06d31b7cb6ce6bb94813b0dd54faa9c677 Mon Sep 17 00:00:00 2001 From: kbracher <k.bracher@fasttube.de> Date: Mon, 31 Mar 2025 18:57:37 +0200 Subject: [PATCH 5/9] cleanup --- .../Core/Inc/ADBMS_Abstraction.h | 3 --- .../Core/Inc/ADBMS_LL_Driver.h | 2 -- .../Core/Src/ADBMS_LL_Driver.c | 22 ++++++++++++------- 3 files changed, 14 insertions(+), 13 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 406e92c..8b1fdca 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 @@ -8,10 +8,7 @@ #ifndef INC_ADBMS_ABSTRACTION_H_ #define INC_ADBMS_ABSTRACTION_H_ -#include "ADBMS_CMD_MAKROS.h" #include "ADBMS_Driver.h" -#include "ADBMS_LL_Driver.h" -#include "main.h" #define mV_from_ADBMS6830(x) (((((int16_t)(x))) * 0.150) + 1500) diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_LL_Driver.h b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_LL_Driver.h index 73a0156..cb785ce 100644 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_LL_Driver.h +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_LL_Driver.h @@ -9,9 +9,7 @@ #define ADBMS_LL_DRIVER_H_ #include "config_ADBMS6830.h" -#include "stm32h7xx_hal.h" #include <stdint.h> -#define TARGET_STM32 uint8_t adbmsDriverInit(SPI_HandleTypeDef* hspi); 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 8d4a2b9..6f2520b 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 @@ -15,6 +15,13 @@ #define INITIAL_COMMAND_PEC 0x0010 #define INITIAL_DATA_PEC 0x0010 +// CRC polynomial constants +#define CRC15_POLY 0xC599 +#define CRC10_POLY 0x8F +#define CRC15_REMAINDER_MASK 0x4000 +#define CRC10_REMAINDER_MASK 0x200 +#define CRC10_RESULT_MASK 0x3FF + SPI_HandleTypeDef* adbmsspi; uint8_t adbmsDriverInit(SPI_HandleTypeDef* hspi) { @@ -49,8 +56,8 @@ static uint16_t computeCRC15(const uint8_t* data, size_t length) { for (size_t i = 0; i < length; i++) { remainder ^= (data[i] << 7); for (int b = 0; b < 8; b++) { - if (remainder & 0x4000) { - remainder = (uint16_t)((remainder << 1) ^ 0xC599); + if (remainder & CRC15_REMAINDER_MASK) { + remainder = (uint16_t)((remainder << 1) ^ CRC15_POLY); } else { remainder <<= 1; } @@ -81,13 +88,12 @@ static uint8_t checkCommandPEC(uint8_t* data, uint8_t datalen) { static uint16_t computeCRC10(const uint8_t* data, size_t length, bool rx_cmd) { uint16_t remainder = INITIAL_DATA_PEC; - const uint16_t poly = 0x8F; for (size_t i = 0; i < length; i++) { remainder ^= (uint16_t)(data[i] << 2); for (int b = 0; b < 8; b++) { - if (remainder & 0x200) { - remainder = (uint16_t)((remainder << 1) ^ poly); + if (remainder & CRC10_REMAINDER_MASK) { + remainder = (uint16_t)((remainder << 1) ^ CRC10_POLY); } else { remainder <<= 1; } @@ -98,14 +104,14 @@ static uint16_t computeCRC10(const uint8_t* data, size_t length, bool rx_cmd) { if (rx_cmd) { remainder ^= (uint16_t)((data[length] & 0xFC) << 2); for (int b = 0; b < 6; b++) { - if (remainder & 0x200) { - remainder = (uint16_t)((remainder << 1) ^ poly); + if (remainder & CRC10_REMAINDER_MASK) { + remainder = (uint16_t)((remainder << 1) ^ CRC10_POLY); } else { remainder <<= 1; } } } - return (uint16_t)(remainder & 0x3FF); + return (uint16_t)(remainder & CRC10_RESULT_MASK); } static uint8_t calculateDataPEC(uint8_t* data, uint8_t datalen) { From 6145508e8b5423da20d51dbd258fe0ee110351e5 Mon Sep 17 00:00:00 2001 From: Kilian Bracher <k.bracher@fasttube.de> Date: Mon, 31 Mar 2025 19:46:47 +0200 Subject: [PATCH 6/9] add: better error message when DPEC fails to validate --- .../ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c | 3 +-- .../Lib/ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c | 12 ++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) 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 bd3f2f1..52dc513 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 @@ -85,8 +85,7 @@ HAL_StatusTypeDef initAMS(SPI_HandleTypeDef* hspi) { } HAL_StatusTypeDef amsWakeUp() { - uint8_t buffer[CMD_BUFFER_SIZE(CFG_GROUP_A_SIZE)] = {0}; - return readCMD(RDCFGA, buffer, CFG_GROUP_A_SIZE); + return __pollCMD(PLADC, 100); //wake up ADBMS6830, wait for T_wake = 200 us (100 cycles at 500 kHz) } HAL_StatusTypeDef amsCellMeasurement(Cell_Module (*module)[N_BMS]) { 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 6f2520b..a35f37b 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 @@ -252,6 +252,18 @@ HAL_StatusTypeDef ___readCMD(uint16_t command, uint8_t * buffer, size_t arglen) for (size_t i = 0; i < N_BMS; i++) { size_t offset = BUFFER_BMS_OFFSET(i, arglen); if (checkDataPEC(&buffer[offset], arglen + 2) != 0) { + debug_log(LOG_LEVEL_ERROR, "Invalid data PEC when reading BMS %d", i); + debug_log(LOG_LEVEL_ERROR, "Received: "); + for (size_t j = 0; j < arglen + 2; j++) { + debug_log_cont(LOG_LEVEL_ERROR, "%02X ", buffer[offset + j]); + } + debug_log_cont(LOG_LEVEL_ERROR, " | %02X %02X ", buffer[offset + arglen], buffer[offset + arglen + 1]); //print out the DPEC + debug_log(LOG_LEVEL_ERROR, "^ DATA"); + //print out spaces until start of DPEC + for (size_t j = 0; j < arglen; j++) { + debug_log_cont(LOG_LEVEL_ERROR, (arglen < 2) ? "" : " "); + } + debug_log(LOG_LEVEL_ERROR, " ^ DPEC"); return HAL_ERROR; } } From 11a2121fd80be4072919d5693842880f45739dc1 Mon Sep 17 00:00:00 2001 From: Kilian Bracher <k.bracher@fasttube.de> Date: Mon, 31 Mar 2025 19:51:59 +0200 Subject: [PATCH 7/9] refactor: convert log macros to functions reduces program size by ~1/3rd 33k -> 19k --- AMS_Master_Code/Core/Inc/swo_log.h | 93 +++++++++++++++++------------- 1 file changed, 53 insertions(+), 40 deletions(-) diff --git a/AMS_Master_Code/Core/Inc/swo_log.h b/AMS_Master_Code/Core/Inc/swo_log.h index 1eb1e68..1a1a3c6 100644 --- a/AMS_Master_Code/Core/Inc/swo_log.h +++ b/AMS_Master_Code/Core/Inc/swo_log.h @@ -93,8 +93,7 @@ static inline void __swo_print(unsigned int channel, const char *str) { } } -[[maybe_unused]] -static void debug_clear_console() { +static inline void debug_clear_console() { for (int i = 0; i < LOG_LEVEL_NOISY; i++) { #if USE_ANSI_ESCAPE_CODES __swo_print(i, "\033[2J\033[;H"); // clear screen @@ -104,44 +103,58 @@ static void debug_clear_console() { } } -#define debug_log(level, msg, ...) \ - do { \ - if (DEBUG_CHANNEL_ENABLED(level)) { \ - char __swo_buffer[MAX_MESSAGE_LENGTH]; \ - size_t len = \ - snprintf(__swo_buffer, sizeof(__swo_buffer), msg, ##__VA_ARGS__); \ - __swo_putc('\n', level); \ - /* Print timestamp if enabled */ \ - if (PRINT_TIMESTAMP) { \ - char __time_buffer[16]; \ - if (USE_ANSI_ESCAPE_CODES) { \ - snprintf(__time_buffer, sizeof(__time_buffer), \ - "\033[90m[%lu]\033[0m ", HAL_GetTick()); \ - } else { \ - snprintf(__time_buffer, sizeof(__time_buffer), "[%lu] ", \ - HAL_GetTick()); \ - } \ - __swo_print(level, __time_buffer); \ - } \ - __swo_print(level, log_level_names[level]); \ - __swo_print(level, __swo_buffer); \ - if (len >= sizeof(__swo_buffer)) { \ - __swo_print(level, " [message length exceeded] "); \ - } \ - } \ - } while (0) +[[gnu::format(printf, 2, 3)]] +static inline void debug_log(unsigned int level, const char *msg, ...) { + if (!DEBUG_CHANNEL_ENABLED(level)) { + return; + } + + char __swo_buffer[MAX_MESSAGE_LENGTH]; + va_list args; + va_start(args, msg); + size_t len = vsnprintf(__swo_buffer, sizeof(__swo_buffer), msg, args); + va_end(args); + + __swo_putc('\n', level); + + /* Print timestamp if enabled */ + if (PRINT_TIMESTAMP) { + char __time_buffer[16]; + if (USE_ANSI_ESCAPE_CODES) { + snprintf(__time_buffer, sizeof(__time_buffer), + "\033[90m[%lu]\033[0m ", HAL_GetTick()); + } else { + snprintf(__time_buffer, sizeof(__time_buffer), "[%lu] ", + HAL_GetTick()); + } + __swo_print(level, __time_buffer); + } + + __swo_print(level, log_level_names[level]); + __swo_print(level, __swo_buffer); + + if (len >= sizeof(__swo_buffer)) { + __swo_print(level, " [message length exceeded] "); + } +} -#define debug_log_cont(level, msg, ...) \ - do { \ - if (DEBUG_CHANNEL_ENABLED(level)) { \ - char __swo_buffer[MAX_MESSAGE_LENGTH]; \ - size_t len = \ - snprintf(__swo_buffer, sizeof(__swo_buffer), msg, ##__VA_ARGS__); \ - __swo_print(level, __swo_buffer); \ - if (len >= sizeof(__swo_buffer)) { \ - __swo_print(level, " [message length exceeded] "); \ - } \ - } \ - } while (0) +[[gnu::format(printf, 2, 3)]] +static inline void debug_log_cont(unsigned int level, const char *msg, ...) { + if (!DEBUG_CHANNEL_ENABLED(level)) { + return; + } + + char __swo_buffer[MAX_MESSAGE_LENGTH]; + va_list args; + va_start(args, msg); + size_t len = vsnprintf(__swo_buffer, sizeof(__swo_buffer), msg, args); + va_end(args); + + __swo_print(level, __swo_buffer); + + if (len >= sizeof(__swo_buffer)) { + __swo_print(level, " [message length exceeded] "); + } +} #endif /* __SWO_LOG_H */ From 44c012082bb026eee20e966daf381c523d1777c1 Mon Sep 17 00:00:00 2001 From: kbracher <k.bracher@fasttube.de> Date: Mon, 31 Mar 2025 21:54:57 +0200 Subject: [PATCH 8/9] fix: formatting of DPEC errors --- AMS_Master_Code/Core/Inc/swo_log.h | 4 ++-- .../ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/AMS_Master_Code/Core/Inc/swo_log.h b/AMS_Master_Code/Core/Inc/swo_log.h index 1a1a3c6..ed95e66 100644 --- a/AMS_Master_Code/Core/Inc/swo_log.h +++ b/AMS_Master_Code/Core/Inc/swo_log.h @@ -104,7 +104,7 @@ static inline void debug_clear_console() { } [[gnu::format(printf, 2, 3)]] -static inline void debug_log(unsigned int level, const char *msg, ...) { +static inline void debug_log(enum log_level_t level, const char *msg, ...) { if (!DEBUG_CHANNEL_ENABLED(level)) { return; } @@ -139,7 +139,7 @@ static inline void debug_log(unsigned int level, const char *msg, ...) { } [[gnu::format(printf, 2, 3)]] -static inline void debug_log_cont(unsigned int level, const char *msg, ...) { +static inline void debug_log_cont(enum log_level_t level, const char *msg, ...) { if (!DEBUG_CHANNEL_ENABLED(level)) { return; } 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 a35f37b..f951a04 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 @@ -248,6 +248,10 @@ HAL_StatusTypeDef ___readCMD(uint16_t command, uint8_t * buffer, size_t arglen) } } + if (arglen == 0) { + return HAL_OK; //no data to check + } + //check data PEC for (size_t i = 0; i < N_BMS; i++) { size_t offset = BUFFER_BMS_OFFSET(i, arglen); @@ -257,13 +261,14 @@ HAL_StatusTypeDef ___readCMD(uint16_t command, uint8_t * buffer, size_t arglen) for (size_t j = 0; j < arglen + 2; j++) { debug_log_cont(LOG_LEVEL_ERROR, "%02X ", buffer[offset + j]); } - debug_log_cont(LOG_LEVEL_ERROR, " | %02X %02X ", buffer[offset + arglen], buffer[offset + arglen + 1]); //print out the DPEC - debug_log(LOG_LEVEL_ERROR, "^ DATA"); + debug_log_cont(LOG_LEVEL_ERROR, "| %02X %02X ", buffer[offset + arglen], buffer[offset + arglen + 1]); //print out the DPEC + debug_log(LOG_LEVEL_ERROR, " DATA ^"); //print out spaces until start of DPEC - for (size_t j = 0; j < arglen; j++) { - debug_log_cont(LOG_LEVEL_ERROR, (arglen < 2) ? "" : " "); + for (size_t j = 0; j < arglen - 1; j++) { + debug_log_cont(LOG_LEVEL_ERROR, (arglen < 2) ? "" : "^^^"); } - debug_log(LOG_LEVEL_ERROR, " ^ DPEC"); + debug_log_cont(LOG_LEVEL_ERROR, "^^ "); + debug_log_cont(LOG_LEVEL_ERROR, " PEC ^"); return HAL_ERROR; } } From 48a06b87ae55df9c1d2a6a65612a37f491ad9815 Mon Sep 17 00:00:00 2001 From: kbracher <k.bracher@fasttube.de> Date: Mon, 31 Mar 2025 21:55:15 +0200 Subject: [PATCH 9/9] fix: attach to STM faster --- AMS_Master_Code/.vscode/launch.json | 1 - 1 file changed, 1 deletion(-) diff --git a/AMS_Master_Code/.vscode/launch.json b/AMS_Master_Code/.vscode/launch.json index 986584c..f6ee3d2 100644 --- a/AMS_Master_Code/.vscode/launch.json +++ b/AMS_Master_Code/.vscode/launch.json @@ -36,7 +36,6 @@ "request": "attach", "type": "cortex-debug", "servertype": "openocd", - "preLaunchTask": "Build STM", "device": "stm32h7a3xxq.s", "configFiles": [ "openocd.cfg"