Store details for BQ errors

This commit is contained in:
jazzpi
2022-06-12 14:44:37 +02:00
parent 6986e5dd3b
commit e503dbe632
5 changed files with 117 additions and 26 deletions

View File

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

View File

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