From e503dbe6324714cf19b3de5207b76fe4a4f1364e Mon Sep 17 00:00:00 2001 From: jazzpi Date: Sun, 12 Jun 2022 14:44:37 +0200 Subject: [PATCH] Store details for BQ errors --- Core/Inc/BQ_Abstraction_Layer.h | 31 +++++++++--- Core/Inc/BQ_Communication.h | 10 +++- Core/Inc/BQ_Register_Definitions.h | 11 +++++ Core/Src/BQ_Abstraction_Layer.c | 75 ++++++++++++++++++++++++++---- Core/Src/BQ_Communication.c | 16 +++---- 5 files changed, 117 insertions(+), 26 deletions(-) diff --git a/Core/Inc/BQ_Abstraction_Layer.h b/Core/Inc/BQ_Abstraction_Layer.h index 9cecf1f..de0898a 100644 --- a/Core/Inc/BQ_Abstraction_Layer.h +++ b/Core/Inc/BQ_Abstraction_Layer.h @@ -19,15 +19,32 @@ #define CELL_OV_THRESHOLD 55100 // 4.2V #define CELL_UV_THRESHOLD 34100 // 2.6V -#define BQ_RDY 1 -#define BQ_STDBY 2 -#define BQ_OFF 0 -#define BQ_INIT_PHASE 3 -#define BQ_ERROR 4 - extern uint16_t cell_voltages[N_CELLS]; -extern uint8_t bq_status; +typedef enum { BQ_OFF, BQ_RDY, BQ_STDBY, BQ_INIT_PHASE, BQ_ERROR } BQ_Status; +extern BQ_Status bq_status; + +#define BQ_ERROR_KIND_HAL (1 << 0) +#define BQ_ERROR_KIND_CRC (1 << 1) +#define BQ_ERROR_KIND_FAULT (1 << 2) +#define BQ_ERROR_LOC_MEASURE (1 << 0) +typedef struct { + uint8_t kind; + uint8_t hal_loc; + uint8_t crc_loc; + uint16_t fault_uv; + uint16_t fault_ov; + uint8_t fault_aux_uv; + uint8_t fault_aux_ov; + uint16_t fault_cmp_uv; + uint16_t fault_cmp_ov; + uint16_t fault_comm; + uint8_t fault_sys; + uint16_t fault_dev; + uint8_t fault_gpi; +} BQ_Error_Description; +extern BQ_Error_Description bq_error; + extern uint32_t lastmeasurementtime; void afe_init(UART_HandleTypeDef* uarthandle); diff --git a/Core/Inc/BQ_Communication.h b/Core/Inc/BQ_Communication.h index 680ff63..4a1c943 100644 --- a/Core/Inc/BQ_Communication.h +++ b/Core/Inc/BQ_Communication.h @@ -14,6 +14,8 @@ #define BQUARTTIMEOUT 200 +typedef enum { BQ_COMM_OK, BQ_COMM_ERR_HAL, BQ_COMM_ERR_CRC } BQ_Comm_Status; + void init_BQCom(UART_HandleTypeDef* uarthandle); uint16_t Calculate_CRC(uint8_t* message_buffer, uint16_t bufferlength); @@ -23,7 +25,13 @@ uint32_t BQ_Write_Register(uint8_t registeraddress, uint8_t registersize, uint32_t data); uint8_t BQ_Read_Register(uint8_t registeraddress, uint8_t registersize, uint32_t* data); -uint32_t BQ_ReadMeasurements(uint8_t* buffer, uint8_t bufferlength); +/** + * @brief Reads Voltage Measurements from the BQ76 + * + * Result are Written into the Buffer + * Buffer size should be 2xMeasured Voltages + */ +BQ_Comm_Status BQ_ReadMeasurements(uint8_t* buffer, uint8_t bufferlength); uint8_t Communication_Reset(); uint8_t BQ_UART_Transmit(uint8_t* message_buffer, uint16_t bufferlength); diff --git a/Core/Inc/BQ_Register_Definitions.h b/Core/Inc/BQ_Register_Definitions.h index d70c875..64f743b 100644 --- a/Core/Inc/BQ_Register_Definitions.h +++ b/Core/Inc/BQ_Register_Definitions.h @@ -188,4 +188,15 @@ #define EE_BURN_SIZE 0x01 #define MAGIC2_SIZE 0x04 +#define GPI_FAULT_SUM (1 << 6) +#define CHIP_FAULT_SUM (1 << 7) +#define SYS_FAULT_SUM (1 << 8) +#define COMM_FAULT_SUM (1 << 9) +#define CMPOV_FAULT_SUM (1 << 10) +#define CMPUV_FAULT_SUM (1 << 11) +#define AUXOV_FAULT_SUM (1 << 12) +#define AUXUV_FAULT_SUM (1 << 13) +#define OV_FAULT_SUM (1 << 14) +#define UV_FAULT_SUM (1 << 15) + #endif /* INC_BQ_REGISTER_DEFINITIONS_H_ */ diff --git a/Core/Src/BQ_Abstraction_Layer.c b/Core/Src/BQ_Abstraction_Layer.c index 7bd2c1d..f802a7f 100644 --- a/Core/Src/BQ_Abstraction_Layer.c +++ b/Core/Src/BQ_Abstraction_Layer.c @@ -14,9 +14,24 @@ uint16_t cell_voltages[N_CELLS]; -uint8_t bq_status; +BQ_Status bq_status; +BQ_Error_Description bq_error; uint32_t lastmeasurementtime; +static void bq_set_error_without_loc(uint8_t kind) { + bq_status = BQ_ERROR; + bq_error.kind |= kind; +} +static void bq_set_error_with_loc(uint8_t kind, uint8_t loc) { + bq_status = BQ_ERROR; + bq_error.kind |= kind; + if (kind == BQ_ERROR_KIND_HAL) { + bq_error.hal_loc = loc; + } else if (kind == BQ_ERROR_KIND_CRC) { + bq_error.crc_loc = loc; + } +} + void afe_init(UART_HandleTypeDef* uarthandle) { // Initialise underlying BQ Communication Functions init_BQCom(uarthandle); @@ -60,19 +75,20 @@ void afe_wakeup() { void afe_measure() { uint8_t cellvoltagebuffer[2 * N_CELLS]; - uint8_t retval = BQ_ReadMeasurements(cellvoltagebuffer, 2 * N_CELLS); + BQ_Comm_Status retval = BQ_ReadMeasurements(cellvoltagebuffer, 2 * N_CELLS); lastmeasurementtime = HAL_GetTick(); - if (retval == 0) { - bq_status = BQ_ERROR; - } else { - + if (retval == BQ_COMM_OK) { for (int n = 0; n < N_CELLS; n++) { cell_voltages[N_CELLS - 1 - n] = (uint16_t)(cellvoltagebuffer[2 * n] << 8) + (uint16_t)cellvoltagebuffer[2 * n + 1]; } + } else if (retval == BQ_COMM_ERR_HAL) { + bq_set_error_with_loc(BQ_COMM_ERR_HAL, BQ_ERROR_LOC_MEASURE); + } else if (retval == BQ_COMM_ERR_CRC) { + bq_set_error_with_loc(BQ_COMM_ERR_CRC, BQ_ERROR_LOC_MEASURE); } } @@ -82,8 +98,51 @@ void afe_check_faults() { uint32_t faultflags = 0; BQ_Read_Register(FAULT_SUM, FAULT_SUM_SIZE, &faultflags); - if (faultflags != 0) { - bq_status = BQ_ERROR; + if (faultflags == 0) { + return; + } + + bq_set_error_without_loc(BQ_ERROR_KIND_FAULT); + uint32_t data; + if (faultflags & UV_FAULT_SUM) { + BQ_Read_Register(FAULT_UV, FAULT_UV_SIZE, &data); + bq_error.fault_uv = data & 0xFFFF; + } + if (faultflags & OV_FAULT_SUM) { + BQ_Read_Register(FAULT_OV, FAULT_OV_SIZE, &data); + bq_error.fault_ov = data & 0xFFFF; + } + if (faultflags & AUXUV_FAULT_SUM) { + BQ_Read_Register(FAULT_AUX, FAULT_AUX_SIZE, &data); + bq_error.fault_aux_uv = (data & 0xFF) >> 8; + } + if (faultflags & AUXOV_FAULT_SUM) { + BQ_Read_Register(FAULT_AUX, FAULT_AUX_SIZE, &data); + bq_error.fault_aux_ov = data & 0xFF; + } + if (faultflags & CMPUV_FAULT_SUM) { + BQ_Read_Register(FAULT_2UV, FAULT_2UV_SIZE, &data); + bq_error.fault_cmp_uv = data & 0xFFFF; + } + if (faultflags & CMPOV_FAULT_SUM) { + BQ_Read_Register(FAULT_2OV, FAULT_2OV_SIZE, &data); + bq_error.fault_cmp_ov = data & 0xFFFF; + } + if (faultflags & COMM_FAULT_SUM) { + BQ_Read_Register(FAULT_COM, FAULT_COM_SIZE, &data); + bq_error.fault_comm = data & 0xFFFF; + } + if (faultflags & SYS_FAULT_SUM) { + BQ_Read_Register(FAULT_SYS, FAULT_SYS_SIZE, &data); + bq_error.fault_sys = data & 0xFF; + } + if (faultflags & CHIP_FAULT_SUM) { + BQ_Read_Register(FAULT_DEV, FAULT_DEV_SIZE, &data); + bq_error.fault_dev = data & 0xFFFF; + } + if (faultflags & GPI_FAULT_SUM) { + BQ_Read_Register(FAULT_GPI, FAULT_GPI_SIZE, &data); + bq_error.fault_gpi = data & 0xFF; } } diff --git a/Core/Src/BQ_Communication.c b/Core/Src/BQ_Communication.c index 4f92376..501ac7f 100644 --- a/Core/Src/BQ_Communication.c +++ b/Core/Src/BQ_Communication.c @@ -183,13 +183,7 @@ uint8_t BQ_Read_Register(uint8_t registeraddress, uint8_t registersize, return 1; } -/*Reads Voltage Measurements from the BQ76 - * - * Result are Written into the Buffer - * Buffer size should be 2xMeasured Voltages - * - */ -uint32_t BQ_ReadMeasurements(uint8_t* buffer, uint8_t bufferlength) { +BQ_Comm_Status BQ_ReadMeasurements(uint8_t* buffer, uint8_t bufferlength) { uint8_t message[6] = {}; message[0] = FRM_WRT_R; message[1] = 0x00; @@ -203,15 +197,17 @@ uint32_t BQ_ReadMeasurements(uint8_t* buffer, uint8_t bufferlength) { uint8_t recv_buf[recv_len]; uint8_t uartstat = BQ_UART_Receive(recv_buf, recv_len); - if (uartstat != HAL_OK || Check_CRC(recv_buf, recv_len) == 0) { - return 0; + if (uartstat != HAL_OK) { + return BQ_COMM_ERR_HAL; + } else if (Check_CRC(recv_buf, recv_len) == 0) { + return BQ_COMM_ERR_CRC; } if (bufferlength <= 2 * (numofcells + numofdietemps)) { memcpy(buffer, &recv_buf[1], bufferlength); } - return 1; + return BQ_COMM_OK; } /* Hardware Layer Implementation of the UART Transmit