From 6a953d08f9e4690d9e9eb9d01cd1bbee7c1f9942 Mon Sep 17 00:00:00 2001 From: Dylan Donahue Date: Tue, 9 Jan 2024 13:02:40 -0500 Subject: [PATCH] print, debugging messages updated --- Core/Inc/stateMachine.h | 3 + Core/Src/main.c | 177 ++++++++++++++++++++-------------------- 2 files changed, 90 insertions(+), 90 deletions(-) diff --git a/Core/Inc/stateMachine.h b/Core/Inc/stateMachine.h index 47c4ed8..3b2d550 100644 --- a/Core/Inc/stateMachine.h +++ b/Core/Inc/stateMachine.h @@ -7,6 +7,9 @@ #include "analyzer.h" #include "timer.h" +/* global that can be read for debugging in main */ +extern BMSState_t current_state; + #define NUM_FAULTS 8 /** diff --git a/Core/Src/main.c b/Core/Src/main.c index 0520cc8..275a85d 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -22,6 +22,7 @@ #include "analyzer.h" #include "stateMachine.h" #include "can_handler.h" +#include /* USER CODE END Includes */ @@ -33,11 +34,11 @@ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ -#ifdef DEBUG_EVERYTHING -#define DEBUG_CHARGING +//#ifdef DEBUG_EVERYTHING +//#define DEBUG_CHARGING #define DEBUG_STATS // etc etc -#endif +//#endif /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ @@ -80,98 +81,94 @@ static void MX_I2C1_Init(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ + +/* the following reroutes printf to uart */ +#ifdef __GNUC__ +#define PUTCHAR_PROTOTYPE int __io_putchar(int ch) +#else +#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) +#endif + +PUTCHAR_PROTOTYPE +{ + HAL_UART_Transmit(&huart4, (uint8_t *)&ch, 1, HAL_MAX_DELAY); + return ch; +} + +int _write(int file, char* ptr, int len) { + int DataIdx; + + for (DataIdx = 0; DataIdx < len; DataIdx++) { + __io_putchar( *ptr++ ); + } + return len; +} + #ifdef DEBUG_STATS const void print_bms_stats(acc_data_t *acc_data) { - static Timer debug_stat_timer; + static nertimer_t debug_stat_timer; static const uint16_t PRINT_STAT_WAIT = 500; //ms - if(!debug_stat_timer.isTimerExpired()) return; - - Serial.print("Prev Fault: 0x"); - Serial.println(stateMachine.previousFault, HEX); - Serial.print("Current: "); - Serial.println((float)(acc_data->pack_current) / 10.0); - Serial.print("Min, Max, Avg Temps: "); - Serial.print(acc_data->min_temp.val); - Serial.print(", "); - Serial.print(acc_data->max_temp.val); - Serial.print(", "); - Serial.println(acc_data->avg_temp); - Serial.print("Min, Max, Avg, Delta Voltages: "); - Serial.print(acc_data->min_voltage.val); - Serial.print(", "); - Serial.print(acc_data->max_voltage.val); - Serial.print(", "); - Serial.print(acc_data->avg_voltage); - Serial.print(", "); - Serial.println(acc_data->delt_voltage); - - Serial.print("DCL: "); - Serial.println(acc_data->discharge_limit); - - Serial.print("CCL: "); - Serial.println(acc_data->charge_limit); - - Serial.print("SoC: "); - Serial.println(acc_data->soc); - - Serial.print("Is Balancing?: "); - Serial.println(segment.isBalancing()); - - Serial.print("State: "); - if (stateMachine.current_state == 0) Serial.println("BOOT"); - else if (stateMachine.current_state == 1) Serial.println("READY"); - else if (stateMachine.current_state == 2) Serial.println("CHARGING"); - else if (stateMachine.current_state == 1) Serial.println("FAULTED"); - - Serial.println("Raw Cell Voltage:"); - for(uint8_t c = 0; c < NUM_CHIPS; c++) - { - for(uint8_t cell = 0; cell < NUM_CELLS_PER_CHIP; cell++) - { - Serial.print(acc_data->chip_data[c].voltage_reading[cell]); - Serial.print("\t"); - } - Serial.println(); - } - - Serial.println("Open Cell Voltage:"); - for(uint8_t c = 0; c < NUM_CHIPS; c++) - { - for(uint8_t cell = 0; cell < NUM_CELLS_PER_CHIP; cell++) - { - Serial.print(acc_data->chip_data[c].open_cell_voltage[cell]); - Serial.print("\t"); - } - Serial.println(); - } - - Serial.println("Cell Temps:"); - for(uint8_t c = 0; c < NUM_CHIPS; c++) - { - for(uint8_t cell = 17; cell < 28; cell++) - { - Serial.print(acc_data->chip_data[c].thermistor_reading[cell]); - Serial.print("\t"); - } - Serial.println(); - } - - Serial.println("Avg Cell Temps:"); - for(uint8_t c = 0; c < NUM_CHIPS; c++) - { - for(uint8_t cell = 17; cell < 28; cell++) - { - Serial.print(acc_data->chip_data[c].thermistor_value[cell]); - Serial.print("\t"); - } - Serial.println(); - } - - - debug_stat_timer.startTimer(PRINT_STAT_WAIT); + if(!is_timer_expired(&debug_stat_timer)) return; + + //TODO get this from eeprom once implemented + // question - should we read from eeprom here, or do that on loop and store locally? + //printf("Prev Fault: %#x", previousFault); + printf("Current: %f\n", (float)(acc_data->pack_current) / 10.0); + printf("Min, Max, Avg Temps: %ld, %ld, %d\n", acc_data->min_temp.val, acc_data->max_temp.val, acc_data->avg_temp); + printf("Min, Max, Avg, Delta Voltages: %ld, %ld, %d, %d\n", acc_data->min_voltage.val, acc_data->max_voltage.val, acc_data->avg_voltage, acc_data->delt_voltage); + printf("DCL: %d\n", acc_data->discharge_limit); + printf("CCL: %d\n", acc_data->charge_limit); + printf("SoC: %d\n", acc_data->soc); + printf("Is Balancing?: %d\n", segment_is_balancing()); + printf("State: "); + if (current_state == 0) printf("BOOT\n"); + else if (current_state == 1) printf("READY\n"); + else if (current_state == 2) printf("CHARGING\n"); + else if (current_state == 1) printf("FAULTED\n"); + printf("Raw Cell Voltage:\n"); + for(uint8_t c = 0; c < NUM_CHIPS; c++) + { + for(uint8_t cell = 0; cell < NUM_CELLS_PER_CHIP; cell++) + { + printf("%d\t", acc_data->chip_data[c].voltage_reading[cell]); + } + printf("\n"); + } + + printf("Open Cell Voltage:\n"); + for(uint8_t c = 0; c < NUM_CHIPS; c++) + { + for(uint8_t cell = 0; cell < NUM_CELLS_PER_CHIP; cell++) + { + printf("%d\t", acc_data->chip_data[c].open_cell_voltage[cell]); + } + printf("\n"); +} + + printf("Cell Temps:\n"); + for(uint8_t c = 0; c < NUM_CHIPS; c++) + { + for(uint8_t cell = 17; cell < 28; cell++) + { + printf("%d\t", acc_data->chip_data[c].thermistor_reading[cell]); + } + printf("\n"); + } + + printf("Avg Cell Temps:\n"); + for(uint8_t c = 0; c < NUM_CHIPS; c++) + { + for(uint8_t cell = 17; cell < 28; cell++) + { + printf("%d\t", acc_data->chip_data[c].thermistor_value[cell]); + } + printf("\n"); + } + + start_timer(&debug_stat_timer, PRINT_STAT_WAIT); } @@ -249,7 +246,7 @@ int main(void) #ifdef DEBUG_STATS - print_bms_stats(analyzer.bmsdata); + print_bms_stats(acc_data); #endif //delay(10); // not sure if we need this in, it was in before }