diff --git a/AMS_Master_Code/Core/Inc/swo_log.h b/AMS_Master_Code/Core/Inc/swo_log.h index a727821..bf55151 100644 --- a/AMS_Master_Code/Core/Inc/swo_log.h +++ b/AMS_Master_Code/Core/Inc/swo_log.h @@ -57,8 +57,8 @@ static inline uint32_t __swo_putc(uint32_t c, unsigned int channel) { (ch < 32) ? __ITM_channel_enabled(ch) : false; \ }) -[[gnu::nonnull(1), gnu::null_terminated_string_arg(1)]] -static inline void __swo_print(const char *str, unsigned int channel) { +[[gnu::nonnull(2), gnu::null_terminated_string_arg(2)]] +static inline void __swo_print(unsigned int channel, const char *str) { if (!__ITM_channel_enabled(channel)) { return; } @@ -67,15 +67,28 @@ static inline void __swo_print(const char *str, unsigned int channel) { } } -#define DEBUG_LOG(level, msg, ...) \ +#define debug_log(level, msg, ...) \ do { \ if (DEBUG_CHANNEL_ENABLED(level)) { \ char buffer[MAX_MESSAGE_LENGTH]; \ size_t len = snprintf(buffer, sizeof(buffer), msg, ##__VA_ARGS__); \ - __swo_print(log_level_names[level], level); \ - __swo_print(buffer, level); \ + __swo_print(level, log_level_names[level]); \ + __swo_print(level, buffer); \ if (len >= sizeof(buffer)) { \ - __swo_print(" [message length exceeded] ", level); \ + __swo_print(level, " [message length exceeded] "); \ + } \ + __swo_putc('\n', level); \ + } \ + } while (0) + +#define debug_log_cont(level, msg, ...) \ + do { \ + if (DEBUG_CHANNEL_ENABLED(level)) { \ + char buffer[MAX_MESSAGE_LENGTH]; \ + size_t len = snprintf(buffer, sizeof(buffer), msg, ##__VA_ARGS__); \ + __swo_print(level, buffer); \ + if (len >= sizeof(buffer)) { \ + __swo_print(level, " [message length exceeded] "); \ } \ __swo_putc('\n', level); \ } \ diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_CMD_MAKROS.h b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_CMD_MAKROS.h index 929f366..2cfdee9 100755 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_CMD_MAKROS.h +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Inc/ADBMS_CMD_MAKROS.h @@ -83,6 +83,8 @@ #define MUTE 0x0028 // Mute Discharge #define UNMUTE 0x0029 // Unmute Discharge +#define RDSID 0x002C // Read Serial ID + /* GPIO Selection for ADC Converion @@ -168,4 +170,6 @@ #define PWM_GROUP_ID 17 #define PWM_S_CONTROL_GROUP_B_ID 18 +#define SID_GROUP_SIZE 6 + #endif /* INC_ADBMS_CMD_MAKROS_H_ */ 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 11088e1..dc4c772 100755 --- 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 @@ -24,6 +24,11 @@ uint8 adbmsDriverInit(SPI_HandleTypeDef* hspi); //2 command + 2 PEC + (data + 2 DPEC) per BMS #define CMD_BUFFER_SIZE(datalen) (4 + (N_BMS * (datalen + 2))) +#define BUFFER_BMS_OFFSET(bms, datalen) (4 + (bms * (datalen + 2))) + +#define CMD_EMPTY_BUFFER ((uint8_t[CMD_BUFFER_SIZE(0)]){0}) +#define CMD_EMPTY_BUFFER_SIZE CMD_BUFFER_SIZE(0) + //macro to function for better type checking (access attribute) [[gnu::access(read_only, 4, 1)]] static inline void __modify_writeCMD_args(size_t arglen, uint8_t args[static restrict N_BMS * (arglen + 2)], uint8_t bms, uint8_t * restrict data) { @@ -48,13 +53,7 @@ static inline HAL_StatusTypeDef __writeCMD(uint16_t command, uint8_t * args, siz __writeCMD(command, args, arglen, CMD_BUFFER_SIZE(arglen)) [[gnu::access(read_write, 2, 3), gnu::nonnull(2)]] -HAL_StatusTypeDef __readCMD(uint16_t command, uint8_t * buffer, size_t buflen); - -#define readCMD(command, buffer, buflen) ({ \ - HAL_StatusTypeDef status = __readCMD(command, buffer, buflen); \ - buffer += 4; /* skip command and PEC */ \ - status; \ -}) +HAL_StatusTypeDef readCMD(uint16_t command, uint8_t * buffer, size_t buflen); uint8_t pollCMD(uint16_t command); 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 8266bba..bc6960c 100755 --- 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 @@ -8,6 +8,7 @@ #include "ADBMS_Abstraction.h" #include "ADBMS_CMD_MAKROS.h" #include "ADBMS_LL_Driver.h" +#include "swo_log.h" #include extern uint8 numberofCells; @@ -21,14 +22,30 @@ extern uint8 numberofCells; uint8 amsReset() { amsWakeUp(); - readCMD(SRST, NULL, 0); + readCMD(SRST, CMD_EMPTY_BUFFER, CMD_EMPTY_BUFFER_SIZE); + + uint8_t buffer[CMD_BUFFER_SIZE(SID_GROUP_SIZE)] = {0}; + CHECK_RETURN(readCMD(RDSID, buffer, CMD_BUFFER_SIZE(SID_GROUP_SIZE))); + + debug_log(LOG_LEVEL_INFO, "BMS reset complete\n"); + + if (DEBUG_CHANNEL_ENABLED(LOG_LEVEL_INFO)) { + debug_log(LOG_LEVEL_INFO, "Found IDs: "); + for (size_t i = 0; i < N_BMS; i++) { + uint64_t id = 0; + for (size_t j = 0; j < SID_GROUP_SIZE; j++) { + id |= buffer[BUFFER_BMS_OFFSET(i, SID_GROUP_SIZE) + j] << (j * 8); + } + debug_log_cont(LOG_LEVEL_INFO, "0x%llx ", id); + } + debug_log_cont(LOG_LEVEL_INFO, "\n"); + } + mcuDelay(10); amsWakeUp(); amsStopBalancing(); amsConfigOverUnderVoltage(DEFAULT_OV, DEFAULT_UV); - uint8 buffer[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; - CHECK_RETURN(writeCMD(CLRFLAG, buffer, 6)); //clear flags, CHECK_RETURN(writeCMD(CLOVUV, buffer, 6)); //OVUV flags CHECK_RETURN(writeCMD(ADCV | ADCV_CONT | ADCV_RD, NULL, 0)); //start continuous cell voltage measurement with redundancy @@ -43,8 +60,8 @@ uint8 initAMS(SPI_HandleTypeDef* hspi) { } uint8 amsWakeUp() { - uint8 buf[6]; - return readCMD(RDCFGA, buf, 6); + uint8_t buffer[CMD_BUFFER_SIZE(CFG_GROUP_A_SIZE)] = {0}; + return readCMD(RDCFGA, buffer, CMD_BUFFER_SIZE(CFG_GROUP_A_SIZE)); } uint8 amsCellMeasurement(Cell_Module* module) { 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 2c96b39..d6eaa92 100755 --- 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 @@ -266,13 +266,13 @@ HAL_StatusTypeDef ___writeCMD(uint16_t command, uint8_t * args, size_t arglen) { //print out data bytes for (size_t i = 0; i < N_BMS; i++) { - debug_log(LOG_LEVEL_NOISY, "%d: ", i); + debug_log_cont(LOG_LEVEL_NOISY, "%d: ", i); for (size_t j = 0; j < arglen; j++) { - debug_log(LOG_LEVEL_NOISY, "%x ", args[4 + (i * (arglen + 2)) + j]); + debug_log_cont(LOG_LEVEL_NOISY, "%x ", args[4 + (i * (arglen + 2)) + j]); } } - debug_log(LOG_LEVEL_NOISY, "\n"); + debug_log_cont(LOG_LEVEL_NOISY, "\n"); } calculateCommandPEC(args, 4); @@ -299,7 +299,7 @@ HAL_StatusTypeDef ___writeCMD(uint16_t command, uint8_t * args, size_t arglen) { -HAL_StatusTypeDef __readCMD(uint16_t command, uint8_t * buffer, size_t buflen) { +HAL_StatusTypeDef readCMD(uint16_t command, uint8_t * buffer, size_t buflen) { buffer[0] = (command >> 8) & 0xFF; buffer[1] = (command)&0xFF; calculateCommandPEC(buffer, 4); @@ -314,17 +314,17 @@ HAL_StatusTypeDef __readCMD(uint16_t command, uint8_t * buffer, size_t buflen) { //TODO: check command counter? if (DEBUG_CHANNEL_ENABLED(LOG_LEVEL_NOISY)) { - debug_log(LOG_LEVEL_NOISY, "%d R | %x %x ", HAL_GetTick(), command >> 8, command & 0xFF); + debug_log(LOG_LEVEL_NOISY, "%u R | %x %x ", HAL_GetTick(), command >> 8, command & 0xFF); //print out data bytes for (size_t i = 0; i < N_BMS; i++) { - debug_log(LOG_LEVEL_NOISY, "%d: ", i); + debug_log_cont(LOG_LEVEL_NOISY, "%d: ", i); for (size_t j = 0; j < buflen; j++) { - debug_log(LOG_LEVEL_NOISY, "%x ", buffer[4 + (i * (buflen + 2)) + j]); + debug_log_cont(LOG_LEVEL_NOISY, "%x ", buffer[4 + (i * (buflen + 2)) + j]); } } - debug_log(LOG_LEVEL_NOISY, "\n"); + debug_log_cont(LOG_LEVEL_NOISY, "\n"); } return checkDataPEC(&buffer[4], buflen + 2);