Compare commits

...

2 Commits

Author SHA1 Message Date
eafd97b3bd
refactor: convert log macros to functions
reduces program size by ~1/3rd
33k -> 19k
2025-03-31 19:51:59 +02:00
6145508e8b
add: better error message when DPEC fails to validate 2025-03-31 19:46:47 +02:00
3 changed files with 65 additions and 40 deletions

View File

@ -104,44 +104,58 @@ static void debug_clear_console() {
} }
} }
#define debug_log(level, msg, ...) \ [[maybe_unused, gnu::format(printf, 2, 3)]]
do { \ static inline void debug_log(unsigned int level, const char *msg, ...) {
if (DEBUG_CHANNEL_ENABLED(level)) { \ if (!DEBUG_CHANNEL_ENABLED(level)) {
char __swo_buffer[MAX_MESSAGE_LENGTH]; \ return;
size_t len = \ }
snprintf(__swo_buffer, sizeof(__swo_buffer), msg, ##__VA_ARGS__); \
__swo_putc('\n', level); \ char __swo_buffer[MAX_MESSAGE_LENGTH];
/* Print timestamp if enabled */ \ va_list args;
if (PRINT_TIMESTAMP) { \ va_start(args, msg);
char __time_buffer[16]; \ size_t len = vsnprintf(__swo_buffer, sizeof(__swo_buffer), msg, args);
if (USE_ANSI_ESCAPE_CODES) { \ va_end(args);
snprintf(__time_buffer, sizeof(__time_buffer), \
"\033[90m[%lu]\033[0m ", HAL_GetTick()); \ __swo_putc('\n', level);
} else { \
snprintf(__time_buffer, sizeof(__time_buffer), "[%lu] ", \ /* Print timestamp if enabled */
HAL_GetTick()); \ if (PRINT_TIMESTAMP) {
} \ char __time_buffer[16];
__swo_print(level, __time_buffer); \ if (USE_ANSI_ESCAPE_CODES) {
} \ snprintf(__time_buffer, sizeof(__time_buffer),
__swo_print(level, log_level_names[level]); \ "\033[90m[%lu]\033[0m ", HAL_GetTick());
__swo_print(level, __swo_buffer); \ } else {
if (len >= sizeof(__swo_buffer)) { \ snprintf(__time_buffer, sizeof(__time_buffer), "[%lu] ",
__swo_print(level, " [message length exceeded] "); \ HAL_GetTick());
} \ }
} \ __swo_print(level, __time_buffer);
} while (0) }
__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, ...) \ [[maybe_unused, gnu::format(printf, 2, 3)]]
do { \ static inline void debug_log_cont(unsigned int level, const char *msg, ...) {
if (DEBUG_CHANNEL_ENABLED(level)) { \ if (!DEBUG_CHANNEL_ENABLED(level)) {
char __swo_buffer[MAX_MESSAGE_LENGTH]; \ return;
size_t len = \ }
snprintf(__swo_buffer, sizeof(__swo_buffer), msg, ##__VA_ARGS__); \
__swo_print(level, __swo_buffer); \ char __swo_buffer[MAX_MESSAGE_LENGTH];
if (len >= sizeof(__swo_buffer)) { \ va_list args;
__swo_print(level, " [message length exceeded] "); \ va_start(args, msg);
} \ size_t len = vsnprintf(__swo_buffer, sizeof(__swo_buffer), msg, args);
} \ va_end(args);
} while (0)
__swo_print(level, __swo_buffer);
if (len >= sizeof(__swo_buffer)) {
__swo_print(level, " [message length exceeded] ");
}
}
#endif /* __SWO_LOG_H */ #endif /* __SWO_LOG_H */

View File

@ -85,8 +85,7 @@ HAL_StatusTypeDef initAMS(SPI_HandleTypeDef* hspi) {
} }
HAL_StatusTypeDef amsWakeUp() { HAL_StatusTypeDef amsWakeUp() {
uint8_t buffer[CMD_BUFFER_SIZE(CFG_GROUP_A_SIZE)] = {0}; return __pollCMD(PLADC, 100); //wake up ADBMS6830, wait for T_wake = 200 us (100 cycles at 500 kHz)
return readCMD(RDCFGA, buffer, CFG_GROUP_A_SIZE);
} }
HAL_StatusTypeDef amsCellMeasurement(Cell_Module (*module)[N_BMS]) { HAL_StatusTypeDef amsCellMeasurement(Cell_Module (*module)[N_BMS]) {

View File

@ -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++) { for (size_t i = 0; i < N_BMS; i++) {
size_t offset = BUFFER_BMS_OFFSET(i, arglen); size_t offset = BUFFER_BMS_OFFSET(i, arglen);
if (checkDataPEC(&buffer[offset], arglen + 2) != 0) { 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; return HAL_ERROR;
} }
} }