From 980f41afc61edd26ee64df674e18cf4a09258683 Mon Sep 17 00:00:00 2001 From: Kilian Bracher Date: Sat, 12 Apr 2025 17:46:17 +0200 Subject: [PATCH] add logging to master --- AMS_Master_Code/.vscode/launch.json | 32 +++- AMS_Master_Code/Core/Inc/battery.h | 7 + AMS_Master_Code/Core/Inc/imd_monitoring.h | 23 +++ AMS_Master_Code/Core/Inc/main.h | 2 +- .../Inc/{util.h => print_master_status.h} | 2 +- AMS_Master_Code/Core/Inc/ts_state_machine.h | 21 +++ AMS_Master_Code/Core/Src/battery.c | 164 ++++-------------- AMS_Master_Code/Core/Src/can.c | 2 +- .../Core/Src/print_master_status.c | 74 ++++++++ .../Core/Src/print_module_status.c | 143 +++++++++++++++ AMS_Master_Code/Core/Src/shunt_monitoring.c | 2 - AMS_Master_Code/Core/Src/ts_state_machine.c | 11 ++ AMS_Master_Code/Core/Src/util.c | 8 - 13 files changed, 340 insertions(+), 151 deletions(-) rename AMS_Master_Code/Core/Inc/{util.h => print_master_status.h} (78%) create mode 100644 AMS_Master_Code/Core/Src/print_master_status.c create mode 100644 AMS_Master_Code/Core/Src/print_module_status.c delete mode 100644 AMS_Master_Code/Core/Src/util.c diff --git a/AMS_Master_Code/.vscode/launch.json b/AMS_Master_Code/.vscode/launch.json index aedae9a..de8a540 100644 --- a/AMS_Master_Code/.vscode/launch.json +++ b/AMS_Master_Code/.vscode/launch.json @@ -23,7 +23,19 @@ "port": 0, "type": "console", "label": "SWO LOG", - "encoding": "ascii" + "encoding": "utf8" + }, + { + "port": 1, + "type": "console", + "label": "Master Status", + "encoding": "utf8" + }, + { + "port": 2, + "type": "console", + "label": "Module Status", + "encoding": "utf8" } ] }, @@ -54,7 +66,19 @@ "port": 0, "type": "console", "label": "SWO LOG", - "encoding": "ascii" + "encoding": "utf8" + }, + { + "port": 1, + "type": "console", + "label": "Master Status", + "encoding": "utf8" + }, + { + "port": 2, + "type": "console", + "label": "Module Status", + "encoding": "utf8" } ] } @@ -66,8 +90,8 @@ "args": [], "stopAtEntry": false, "externalConsole": true, - "cwd": "c:/lene/fasttube/FT25/03_ams/ams-master/AMS_Master_Code/Core/Src", - "program": "c:/lene/fasttube/FT25/03_ams/ams-master/AMS_Master_Code/Core/Src/build/Debug/outDebug", + "cwd": "${workspaceFolder}", + "program": "${workspaceFolder}/build/debug/AMS_Master_Nucleo.elf", "MIMode": "gdb", "miDebuggerPath": "gdb", "setupCommands": [ diff --git a/AMS_Master_Code/Core/Inc/battery.h b/AMS_Master_Code/Core/Inc/battery.h index 3efa503..4c7a664 100644 --- a/AMS_Master_Code/Core/Inc/battery.h +++ b/AMS_Master_Code/Core/Inc/battery.h @@ -7,11 +7,18 @@ #include #include extern uint16_t min_voltage; +extern uint16_t max_voltage; +extern struct {uint16_t min; uint16_t max;} module_voltages[N_BMS]; extern int16_t max_temp; +extern int16_t min_temp; +extern struct {int16_t min; int16_t max;} module_temps[N_BMS]; + extern int16_t cellTemps[N_BMS][N_CELLS]; HAL_StatusTypeDef battery_init(SPI_HandleTypeDef* hspi); HAL_StatusTypeDef battery_update(); + void print_battery_info(); +void print_master_status(); #endif // __BATTERY_H \ No newline at end of file diff --git a/AMS_Master_Code/Core/Inc/imd_monitoring.h b/AMS_Master_Code/Core/Inc/imd_monitoring.h index 04bb227..eb1a0c1 100644 --- a/AMS_Master_Code/Core/Inc/imd_monitoring.h +++ b/AMS_Master_Code/Core/Inc/imd_monitoring.h @@ -16,6 +16,29 @@ typedef enum { IMD_STATE_GND_FAULT, } IMDState; +static inline const char *IMDStateToString(IMDState state) { + switch (state) { + case IMD_STATE_UNKNOWN: + return "UNKNOWN"; + case IMD_STATE_SHORTCIRCUIT_SUPPLY: + return "SHORTCIRCUIT_SUPPLY"; + case IMD_STATE_SHORTCIRCUIT_GND: + return "SHORTCIRCUIT_GND"; + case IMD_STATE_NORMAL: + return "NORMAL"; + case IMD_STATE_UNDERVOLTAGE: + return "UNDERVOLTAGE"; + case IMD_STATE_SST: + return "SST"; + case IMD_STATE_DEV_ERROR: + return "DEV_ERROR"; + case IMD_STATE_GND_FAULT: + return "GND_FAULT"; + default: + return "INVALID STATE"; + } +} + typedef struct { int ok; diff --git a/AMS_Master_Code/Core/Inc/main.h b/AMS_Master_Code/Core/Inc/main.h index 8f4567d..5f054d2 100644 --- a/AMS_Master_Code/Core/Inc/main.h +++ b/AMS_Master_Code/Core/Inc/main.h @@ -48,7 +48,7 @@ extern int hv_active; extern int neg_air_closed; extern int pos_air_closed; extern int precharge_closed; -extern int precharge_opened; +extern int pre_and_air_open; /* USER CODE END EC */ diff --git a/AMS_Master_Code/Core/Inc/util.h b/AMS_Master_Code/Core/Inc/print_master_status.h similarity index 78% rename from AMS_Master_Code/Core/Inc/util.h rename to AMS_Master_Code/Core/Inc/print_master_status.h index 8060c62..dda2d9d 100644 --- a/AMS_Master_Code/Core/Inc/util.h +++ b/AMS_Master_Code/Core/Inc/print_master_status.h @@ -3,6 +3,6 @@ #include -void set_error_led(); +void print_status(); #endif // INC_UTIL_H diff --git a/AMS_Master_Code/Core/Inc/ts_state_machine.h b/AMS_Master_Code/Core/Inc/ts_state_machine.h index ba2bfe5..85c878d 100644 --- a/AMS_Master_Code/Core/Inc/ts_state_machine.h +++ b/AMS_Master_Code/Core/Inc/ts_state_machine.h @@ -30,6 +30,27 @@ typedef enum { TS_CHARGING } TSState; +static inline const char *TSStateToString(TSState state) { + switch (state) { + case TS_INACTIVE: + return "INACTIVE"; + case TS_ACTIVE: + return "ACTIVE"; + case TS_PRECHARGE: + return "PRECHARGE"; + case TS_DISCHARGE: + return "DISCHARGE"; + case TS_ERROR: + return "ERROR"; + case TS_CHARGING_CHECK: + return "CHARGING_CHECK"; + case TS_CHARGING: + return "CHARGING"; + default: + return "INVALID STATE"; + } +} + typedef enum { TS_ERRORKIND_NONE = 0x00, TS_ERRORKIND_SLAVE_TIMEOUT = 0x01, diff --git a/AMS_Master_Code/Core/Src/battery.c b/AMS_Master_Code/Core/Src/battery.c index 79fa77a..16c16db 100644 --- a/AMS_Master_Code/Core/Src/battery.c +++ b/AMS_Master_Code/Core/Src/battery.c @@ -6,9 +6,14 @@ #define SWO_LOG_PREFIX "[BATTERY] " #include "swo_log.h" - + uint16_t min_voltage = 0xFFFF; -int16_t max_temp = -1; +uint16_t max_voltage = 0; +typeof(module_voltages) module_voltages = {[0 ... N_BMS - 1] = {0xFFFF, 0}}; +int16_t min_temp = INT16_MAX; +int16_t max_temp = INT16_MIN; +typeof(module_temps) module_temps = {[0 ... N_BMS - 1] = {INT16_MAX, INT16_MIN}}; + int16_t cellTemps[N_BMS][N_CELLS]; HAL_StatusTypeDef battery_init(SPI_HandleTypeDef *hspi) { @@ -37,153 +42,44 @@ HAL_StatusTypeDef battery_update() { } min_voltage = 0xFFFF; - max_temp = -1; + max_voltage = 0; + min_temp = INT16_MAX; + max_temp = INT16_MIN; for (size_t i = 0; i < N_BMS; i++) { for (size_t j = 0; j < N_CELLS; j++) { if (modules[i].cellVoltages[j] > min_voltage) { min_voltage = modules[i].cellVoltages[j]; } + if (modules[i].cellVoltages[j] < max_voltage) { + max_voltage = modules[i].cellVoltages[j]; + } + if (modules[i].cellVoltages[j] > module_voltages[i].max) { + module_voltages[i].max = modules[i].cellVoltages[j]; + } + if (modules[i].cellVoltages[j] < module_voltages[i].min) { + module_voltages[i].min = modules[i].cellVoltages[j]; + } } + for (size_t j = 0; j < 10; j++) { //10 GPIOs cellTemps[i][j] = ntc_mv_to_celsius(modules[i].auxVoltages[j]); if (cellTemps[i][j] > max_temp) { max_temp = cellTemps[i][j]; } + if (cellTemps[i][j] < min_temp) { + min_temp = cellTemps[i][j]; + } + + if (cellTemps[i][j] > module_temps[i].max) { + module_temps[i].max = cellTemps[i][j]; + } + if (cellTemps[i][j] < module_temps[i].min) { + module_temps[i].min = cellTemps[i][j]; + } } } return HAL_OK; -} - -void print_battery_info() { - for (size_t i = 0; i < N_BMS; i++) { - debug_log(LOG_LEVEL_INFO, "Module %d status:", i); - - // 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", - 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", - 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", - 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", - modules[i].cellVoltages[12], modules[i].cellVoltages[13], - modules[i].cellVoltages[14], modules[i].cellVoltages[15]); - - // Print GPIO values - debug_log(LOG_LEVEL_INFO, " GPIO voltages (mV):"); - debug_log(LOG_LEVEL_INFO, - " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d", - modules[i].auxVoltages[0], modules[i].auxVoltages[1], - modules[i].auxVoltages[2], modules[i].auxVoltages[3], - modules[i].auxVoltages[4]); - debug_log(LOG_LEVEL_INFO, - " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d", - modules[i].auxVoltages[5], modules[i].auxVoltages[6], - modules[i].auxVoltages[7], modules[i].auxVoltages[8], - modules[i].auxVoltages[9]); - - // Print temperatures - debug_log(LOG_LEVEL_INFO, " GPIO as temperatures (°C):"); - debug_log(LOG_LEVEL_INFO, - " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d", - cellTemps[i][0], cellTemps[i][1], cellTemps[i][2], - cellTemps[i][3], cellTemps[i][4]); - debug_log(LOG_LEVEL_INFO, - " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d", - cellTemps[i][5], cellTemps[i][6], cellTemps[i][7], - cellTemps[i][8], cellTemps[i][9]); - - debug_log(LOG_LEVEL_INFO, - " Internal temp: %d, VAnalog: %d, VDigital: %d, VRef: %d", - modules[i].internalDieTemp, modules[i].analogSupplyVoltage, - modules[i].digitalSupplyVoltage, modules[i].refVoltage); - - // Print error flags if any are set - bool hasFlags = false; - char flagBuffer[128] = ""; - char *bufPos = flagBuffer; - - if (modules[i].status.CS_FLT) { - bufPos = stpcpy(bufPos, "CS_FLT "); - hasFlags = true; - } - if (modules[i].status.SMED) { - bufPos = stpcpy(bufPos, "SMED "); - hasFlags = true; - } - if (modules[i].status.SED) { - bufPos = stpcpy(bufPos, "SED "); - hasFlags = true; - } - if (modules[i].status.CMED) { - bufPos = stpcpy(bufPos, "CMED "); - hasFlags = true; - } - if (modules[i].status.CED) { - bufPos = stpcpy(bufPos, "CED "); - hasFlags = true; - } - if (modules[i].status.VD_UV) { - bufPos = stpcpy(bufPos, "VD_UV "); - hasFlags = true; - } - if (modules[i].status.VD_OV) { - bufPos = stpcpy(bufPos, "VD_OV "); - hasFlags = true; - } - if (modules[i].status.VA_UV) { - bufPos = stpcpy(bufPos, "VA_UV "); - hasFlags = true; - } - if (modules[i].status.VA_OV) { - bufPos = stpcpy(bufPos, "VA_OV "); - hasFlags = true; - } - if (modules[i].status.THSD) { - bufPos = stpcpy(bufPos, "THSD "); - hasFlags = true; - } - if (modules[i].status.SLEEP) { - bufPos = stpcpy(bufPos, "SLEEP "); - hasFlags = true; - } - if (modules[i].status.SPIFLT) { - bufPos = stpcpy(bufPos, "SPIFLT "); - hasFlags = true; - } - if (modules[i].status.COMPARE) { - bufPos = stpcpy(bufPos, "COMPARE "); - hasFlags = true; - } - if (modules[i].status.VDE) { - bufPos = stpcpy(bufPos, "VDE "); - hasFlags = true; - } - if (modules[i].status.VDEL) { - bufPos = stpcpy(bufPos, "VDEL "); - hasFlags = true; - } - - debug_log(LOG_LEVEL_INFO, " Status flags: %s", - hasFlags ? flagBuffer : "[none]"); - - debug_log(LOG_LEVEL_INFO, " Conversion counter: %d", - modules[i].status.CCTS); - - // Check for over/under voltage - if (modules[i].overVoltage || modules[i].underVoltage) { - debug_log(LOG_LEVEL_WARNING, - " Module %d voltage issues - OV: 0x%08lX, UV: 0x%08lX", i, - modules[i].overVoltage, modules[i].underVoltage); - } - - debug_log(LOG_LEVEL_INFO, " ---------------"); - } } \ No newline at end of file diff --git a/AMS_Master_Code/Core/Src/can.c b/AMS_Master_Code/Core/Src/can.c index f2142aa..63561dd 100644 --- a/AMS_Master_Code/Core/Src/can.c +++ b/AMS_Master_Code/Core/Src/can.c @@ -39,7 +39,7 @@ HAL_StatusTypeDef can_send_status() { } data[0] = (sdc_closed_nodelay << 0) | (ts_error << 1) | (hv_active << 2) | (neg_air_closed << 3) | (pos_air_closed << 4) | - (precharge_closed << 5) | (precharge_opened << 6); + (precharge_closed << 5) | (pre_and_air_open << 6); return ftcan_transmit(CAN_ID_AMS_SIGNALS, data, 1); } diff --git a/AMS_Master_Code/Core/Src/print_master_status.c b/AMS_Master_Code/Core/Src/print_master_status.c new file mode 100644 index 0000000..5bf30ee --- /dev/null +++ b/AMS_Master_Code/Core/Src/print_master_status.c @@ -0,0 +1,74 @@ +#include "print_master_status.h" +#include "imd_monitoring.h" +#include "main.h" +#include "soc_estimation.h" +#include "stm32h7xx_hal.h" + +#include "shunt_monitoring.h" +#include "ts_state_machine.h" +#include "battery.h" + +#define DEBUG_CHANNEL 1 // channel to output messages on +#include "swo_log.h" + +void print_master_status() { + 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, "\nGeneral:"); + debug_log(LOG_LEVEL_INFO, " State: %s", TSStateToString(ts_state.current_state)); + debug_log(LOG_LEVEL_INFO, " Target: %s", TSStateToString(ts_state.target_state)); + debug_log(LOG_LEVEL_INFO, " Err Source: %s", ts_state.error_source == 0 ? "NONE" : (ts_state.error_source == 0b11 ? "SHUNT, SLAVE" : (ts_state.error_source == 0b01 ? "SHUNT" : "SLAVE"))); + debug_log(LOG_LEVEL_INFO, " Err type: %s", ts_state.error_type == 0 ? "NONE" : (ts_state.error_type == 0x01 ? "SLAVE_TIMEOUT" : (ts_state.error_type == 0x02 ? "SLAVE_PANIC" : (ts_state.error_type == 0x03 ? "SHUNT_TIMEOUT" : (ts_state.error_type == 0x04 ? "SHUNT_OVERCURRENT" : (ts_state.error_type == 0x05 ? "SHUNT_OVERTEMP" : "UNKNOWN")))))); + debug_log(LOG_LEVEL_INFO, " HV active: %s", hv_active ? "YES" : "NO"); + + debug_log(LOG_LEVEL_INFO, "\nRelay positions:"); + debug_log(LOG_LEVEL_INFO, " SDC: %s", sdc_closed ? "CLOSED" : "OPEN"); + debug_log(LOG_LEVEL_INFO, " Air-: %s", neg_air_closed ? "CLOSED" : "OPEN"); + debug_log(LOG_LEVEL_INFO, " Air+: %s", pos_air_closed ? "CLOSED" : "OPEN"); + debug_log(LOG_LEVEL_INFO, " Precharge: %s", precharge_closed ? "CLOSED" : "OPEN"); + debug_log(LOG_LEVEL_INFO, " Precharge/Air+ sense: %s", pre_and_air_open ? "HIGH" : "LOW"); + + debug_log(LOG_LEVEL_INFO, "\nIMD data:"); + debug_log(LOG_LEVEL_INFO, " State: %s", IMDStateToString(imd_data.state)); + debug_log(LOG_LEVEL_INFO, " R_iso: %lu kOhm", imd_data.r_iso); + debug_log(LOG_LEVEL_INFO, " Frequency: %lu Hz", imd_data.freq); + debug_log(LOG_LEVEL_INFO, " Duty cycle: %lu %%", imd_data.duty_cycle); + debug_log(LOG_LEVEL_INFO, " Last high: %lu ms", imd_data.last_high); + debug_log(LOG_LEVEL_INFO, " OK: %s", imd_data.ok ? "YES" : "NO"); + + debug_log(LOG_LEVEL_INFO, "\nShunt data:"); + debug_log(LOG_LEVEL_INFO, " Voltage: %ld mV", shunt_data.voltage_bat); + debug_log(LOG_LEVEL_INFO, " Current: %ld mA", shunt_data.current); + debug_log(LOG_LEVEL_INFO, " Power: %ld W", shunt_data.power); + debug_log(LOG_LEVEL_INFO, " Energy: %ld Ws", shunt_data.energy); + debug_log(LOG_LEVEL_INFO, " Temp: %ld °C", shunt_data.busbartemp / 10); + debug_log(LOG_LEVEL_INFO, " Time delta: %lu ms", HAL_GetTick() - shunt_data.last_message); + + debug_log(LOG_LEVEL_INFO, "\nBattery data:"); + debug_log(LOG_LEVEL_INFO, " Min/Max voltage: %d mV / %d mV", min_voltage, max_voltage); + debug_log(LOG_LEVEL_INFO, " Min/Max temp: %d °C / %d °C", min_temp, max_temp); + debug_log(LOG_LEVEL_INFO, " SoC: %.2f %%", current_soc); + debug_log(LOG_LEVEL_INFO, " Module data: Min V | Max V | Min T | Max T"); + for (size_t i = 0; i < N_BMS; i++) { + #if USE_ANSI_ESCAPE_CODES + #define COLOR_MIN "\033[34m" // Blue for min + #define COLOR_MAX "\033[31m" // Red for max + #define COLOR_RESET "\033[0m" + debug_log(LOG_LEVEL_INFO, " %2zu: %s%5d mV%s | %s%5d mV%s | %s%3d °C%s | %s%3d °C%s", i, + (module_voltages[i].min == min_voltage) ? COLOR_MIN : "", (module_voltages[i].min), COLOR_RESET, + (module_voltages[i].max == max_voltage) ? COLOR_MAX : "", (module_voltages[i].max), COLOR_RESET, + (module_temps[i].min == min_temp) ? COLOR_MIN : "", (module_temps[i].min), COLOR_RESET, + (module_temps[i].max == max_temp) ? COLOR_MAX : "", (module_temps[i].max), COLOR_RESET); + #else + debug_log(LOG_LEVEL_INFO, " %2zu: %5d mV | %5d mV | %3d °C | %3d °C", i, + module_voltages[i].min, module_voltages[i].max, + module_temps[i].min, module_temps[i].max); + #endif + } + + debug_log(LOG_LEVEL_INFO, "\n------ Updated at %lu ------", HAL_GetTick()); +} + diff --git a/AMS_Master_Code/Core/Src/print_module_status.c b/AMS_Master_Code/Core/Src/print_module_status.c new file mode 100644 index 0000000..826ea06 --- /dev/null +++ b/AMS_Master_Code/Core/Src/print_module_status.c @@ -0,0 +1,143 @@ +#include "battery.h" +#include "ADBMS_Driver.h" +#include "NTC.h" +#include "config_ADBMS6830.h" +#include + +#define DEBUG_CHANNEL 2 // channel to output messages on +#include "swo_log.h" + +void print_battery_info() { + debug_clear_console(); + debug_log(LOG_LEVEL_INFO, "------ AMS_Master on %s (%s), compiled at %s ------\n", COMMIT_BRANCH, + COMMIT_HASH, COMPILE_DATE); + + for (size_t i = 0; i < N_BMS; i++) { + debug_log(LOG_LEVEL_INFO, "Module %d status:", i); + + // 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", + 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", + 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", + 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", + modules[i].cellVoltages[12], modules[i].cellVoltages[13], + modules[i].cellVoltages[14], modules[i].cellVoltages[15]); + + // Print GPIO values + debug_log(LOG_LEVEL_INFO, " GPIO voltages (mV):"); + debug_log(LOG_LEVEL_INFO, + " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d", + modules[i].auxVoltages[0], modules[i].auxVoltages[1], + modules[i].auxVoltages[2], modules[i].auxVoltages[3], + modules[i].auxVoltages[4]); + debug_log(LOG_LEVEL_INFO, + " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d", + modules[i].auxVoltages[5], modules[i].auxVoltages[6], + modules[i].auxVoltages[7], modules[i].auxVoltages[8], + modules[i].auxVoltages[9]); + + // Print temperatures + debug_log(LOG_LEVEL_INFO, " GPIO as temperatures (°C):"); + debug_log(LOG_LEVEL_INFO, + " G0: %4d G1: %4d G2: %4d G3: %4d G4: %4d", + cellTemps[i][0], cellTemps[i][1], cellTemps[i][2], + cellTemps[i][3], cellTemps[i][4]); + debug_log(LOG_LEVEL_INFO, + " G5: %4d G6: %4d G7: %4d G8: %4d G9: %4d", + cellTemps[i][5], cellTemps[i][6], cellTemps[i][7], + cellTemps[i][8], cellTemps[i][9]); + + debug_log(LOG_LEVEL_INFO, + " Internal temp: %d, VAnalog: %d, VDigital: %d, VRef: %d", + modules[i].internalDieTemp, modules[i].analogSupplyVoltage, + modules[i].digitalSupplyVoltage, modules[i].refVoltage); + + // Print error flags if any are set + bool hasFlags = false; + char flagBuffer[128] = ""; + char *bufPos = flagBuffer; + + if (modules[i].status.CS_FLT) { + bufPos = stpcpy(bufPos, "CS_FLT "); + hasFlags = true; + } + if (modules[i].status.SMED) { + bufPos = stpcpy(bufPos, "SMED "); + hasFlags = true; + } + if (modules[i].status.SED) { + bufPos = stpcpy(bufPos, "SED "); + hasFlags = true; + } + if (modules[i].status.CMED) { + bufPos = stpcpy(bufPos, "CMED "); + hasFlags = true; + } + if (modules[i].status.CED) { + bufPos = stpcpy(bufPos, "CED "); + hasFlags = true; + } + if (modules[i].status.VD_UV) { + bufPos = stpcpy(bufPos, "VD_UV "); + hasFlags = true; + } + if (modules[i].status.VD_OV) { + bufPos = stpcpy(bufPos, "VD_OV "); + hasFlags = true; + } + if (modules[i].status.VA_UV) { + bufPos = stpcpy(bufPos, "VA_UV "); + hasFlags = true; + } + if (modules[i].status.VA_OV) { + bufPos = stpcpy(bufPos, "VA_OV "); + hasFlags = true; + } + if (modules[i].status.THSD) { + bufPos = stpcpy(bufPos, "THSD "); + hasFlags = true; + } + if (modules[i].status.SLEEP) { + bufPos = stpcpy(bufPos, "SLEEP "); + hasFlags = true; + } + if (modules[i].status.SPIFLT) { + bufPos = stpcpy(bufPos, "SPIFLT "); + hasFlags = true; + } + if (modules[i].status.COMPARE) { + bufPos = stpcpy(bufPos, "COMPARE "); + hasFlags = true; + } + if (modules[i].status.VDE) { + bufPos = stpcpy(bufPos, "VDE "); + hasFlags = true; + } + if (modules[i].status.VDEL) { + bufPos = stpcpy(bufPos, "VDEL "); + hasFlags = true; + } + + debug_log(LOG_LEVEL_INFO, " Status flags: %s", + hasFlags ? flagBuffer : "[none]"); + + debug_log(LOG_LEVEL_INFO, " Conversion counter: %d", + modules[i].status.CCTS); + + // Check for over/under voltage + if (modules[i].overVoltage || modules[i].underVoltage) { + debug_log(LOG_LEVEL_WARNING, + " Module %d voltage issues - OV: 0x%08lX, UV: 0x%08lX", i, + modules[i].overVoltage, modules[i].underVoltage); + } + + debug_log(LOG_LEVEL_INFO, "\n------ Updated at %lu ------", HAL_GetTick()); + } + } \ No newline at end of file diff --git a/AMS_Master_Code/Core/Src/shunt_monitoring.c b/AMS_Master_Code/Core/Src/shunt_monitoring.c index 22a5265..3655fa0 100644 --- a/AMS_Master_Code/Core/Src/shunt_monitoring.c +++ b/AMS_Master_Code/Core/Src/shunt_monitoring.c @@ -4,8 +4,6 @@ #include "can.h" #include "ts_state_machine.h" -#include "util.h" - #include "can-halal.h" ShuntData shunt_data; diff --git a/AMS_Master_Code/Core/Src/ts_state_machine.c b/AMS_Master_Code/Core/Src/ts_state_machine.c index e2bbad9..bcdfe09 100644 --- a/AMS_Master_Code/Core/Src/ts_state_machine.c +++ b/AMS_Master_Code/Core/Src/ts_state_machine.c @@ -6,6 +6,9 @@ #include "stm32h7xx_hal.h" #include +#define SWO_LOG_PREFIX "[TS SM] " +#include "swo_log.h" + TSStateHandle ts_state; static uint32_t precharge_95_reached_timestamp = 0; @@ -24,6 +27,8 @@ void ts_sm_update() { ts_state.current_state = TS_ERROR; } + auto old_state = ts_state.current_state; + switch (ts_state.current_state) { case TS_INACTIVE: ts_state.current_state = ts_sm_update_inactive(); @@ -48,6 +53,12 @@ void ts_sm_update() { break; } + if (ts_state.current_state != old_state) { + debug_log(LOG_LEVEL_DEBUG, + "Transitioned from %s to %s", TSStateToString(old_state), + TSStateToString(ts_state.current_state)); + } + ts_sm_set_relay_positions(ts_state.current_state); status_led_state(ts_state.current_state, (TSErrorKind)ts_state.error_type); } diff --git a/AMS_Master_Code/Core/Src/util.c b/AMS_Master_Code/Core/Src/util.c deleted file mode 100644 index 3623bd3..0000000 --- a/AMS_Master_Code/Core/Src/util.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "util.h" -#include "main.h" -#include "stm32h7xx_hal.h" - -void set_error_led() { - #warning "check callers of this function" - //HAL_GPIO_WritePin(STATUS2_GPIO_Port, STATUS2_Pin, GPIO_PIN_SET); -}