Compare commits

...

4 Commits

6 changed files with 61 additions and 29 deletions

View File

@ -15,7 +15,8 @@
#define MAX_MESSAGE_LENGTH 256
#define USE_MULTIPLE_CHANNELS false // if true, each log level has its own channel (FATAL : 0, ERROR : 1, etc.)
#define USE_ANSI_ESCAPE_CODES true // if true, log messages will be colored according to their log level, and the console can be cleared
#define USE_ANSI_ESCAPE_CODES true // if true, log messages will be colored according to their log level, and the console can be cleared
#define PRINT_TIMESTAMP false // if true, timestamp (from HAL_GetTick) is printed before each log message
#if !USE_MULTIPLE_CHANNELS
#define DEBUG_CHANNEL 0 // channel to output messages on
@ -48,14 +49,14 @@ enum log_level_t {
static inline bool __ITM_channel_enabled(uint32_t channel) {
#if !USE_MULTIPLE_CHANNELS
#if USE_CHANNEL_MASK_VARIABLE
return ((ITM->TER & (1UL << DEBUG_CHANNEL)) != 0UL) &&
((MASK_VARIABLE & (1UL << channel)) != 0UL);
#else
channel = DEBUG_CHANNEL;
#endif
#if USE_CHANNEL_MASK_VARIABLE
return ((ITM->TER & (1UL << DEBUG_CHANNEL)) != 0UL) &&
((MASK_VARIABLE & (1UL << channel)) != 0UL);
#else
channel = DEBUG_CHANNEL;
#endif
return ((ITM->TER & (1UL << channel)) != 0UL);
#endif
return ((ITM->TER & (1UL << channel)) != 0UL);
}
// adapted from ITM_SendChar() in the CMSIS-Core
@ -110,6 +111,18 @@ static void debug_clear_console() {
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)) { \

View File

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

View File

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

View File

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

View File

@ -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,11 +116,12 @@ 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;
}
if (count % 4 == 0) {
for (size_t i = 0; i < N_BMS; i++) {
@ -128,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",
@ -143,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]);

View File

@ -24,9 +24,9 @@ cDefinitions:
- STM32H7
- USE_HAL_DRIVER
- FTCAN_NUM_FILTERS=32
- COMMIT_HASH='"$(shell git describe --always --dirty --abbrev=8)"'
- COMMIT_BRANCH='"$(shell git rev-parse --abbrev-ref HEAD)"'
- COMPILE_DATE='"$(shell date +'%Y-%m-%d %H:%M:%S')"'
- COMMIT_HASH='"$(shell git describe --always --dirty --abbrev=8 2>/dev/null || echo unknown)"'
- COMMIT_BRANCH='"$(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || echo unknown)"'
- COMPILE_DATE='"$(shell date "+%Y-%m-%d %H:%M:%S")"'
cxxDefinitions: []
asDefinitions: []