refactor: check data PEC again
This commit is contained in:
parent
13958bb7a5
commit
5d2b8fd09b
|
@ -68,9 +68,9 @@ HAL_StatusTypeDef amsReset();
|
|||
HAL_StatusTypeDef initAMS(SPI_HandleTypeDef* hspi);
|
||||
HAL_StatusTypeDef amsWakeUp();
|
||||
|
||||
HAL_StatusTypeDef amsCellMeasurement(Cell_Module* module);
|
||||
HAL_StatusTypeDef amsCellMeasurement(Cell_Module (*module)[N_BMS]);
|
||||
|
||||
HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module * module[static N_BMS]);
|
||||
HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module (*module)[N_BMS]);
|
||||
|
||||
HAL_StatusTypeDef amsConfigBalancing(uint32_t channels[N_BMS], uint8_t dutyCycle);
|
||||
HAL_StatusTypeDef amsStartBalancing(uint8_t dutyCycle);
|
||||
|
|
|
@ -47,8 +47,15 @@ static inline HAL_StatusTypeDef __writeCMD(uint16_t command, uint8_t * args, siz
|
|||
#define writeCMD(command, args, arglen) \
|
||||
__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);
|
||||
HAL_StatusTypeDef ___readCMD(uint16_t command, uint8_t * buffer, size_t arglen);
|
||||
|
||||
[[gnu::access(read_write, 2, 4), gnu::nonnull(2), gnu::always_inline]] //add dummy size variable for bounds checking, should be optimized out
|
||||
static inline HAL_StatusTypeDef __readCMD(uint16_t command, uint8_t * buffer, size_t arglen, size_t _) {
|
||||
return ___readCMD(command, buffer, arglen);
|
||||
}
|
||||
|
||||
#define readCMD(command, buffer, buflen) \
|
||||
__readCMD(command, buffer, buflen, CMD_BUFFER_SIZE(buflen))
|
||||
|
||||
HAL_StatusTypeDef pollCMD(uint16_t command);
|
||||
|
||||
|
|
|
@ -23,10 +23,10 @@ extern uint8_t numberofCells;
|
|||
|
||||
HAL_StatusTypeDef amsReset() {
|
||||
amsWakeUp();
|
||||
readCMD(SRST, CMD_EMPTY_BUFFER, CMD_EMPTY_BUFFER_SIZE);
|
||||
readCMD(SRST, CMD_EMPTY_BUFFER, 0);
|
||||
|
||||
uint8_t sidbuffer[CMD_BUFFER_SIZE(SID_GROUP_SIZE)] = {};
|
||||
CHECK_RETURN(readCMD(RDSID, sidbuffer, CMD_BUFFER_SIZE(SID_GROUP_SIZE)));
|
||||
CHECK_RETURN(readCMD(RDSID, sidbuffer, SID_GROUP_SIZE));
|
||||
|
||||
debug_log(LOG_LEVEL_INFO, "BMS reset complete");
|
||||
|
||||
|
@ -65,19 +65,19 @@ HAL_StatusTypeDef initAMS(SPI_HandleTypeDef* hspi) {
|
|||
|
||||
HAL_StatusTypeDef amsWakeUp() {
|
||||
uint8_t buffer[CMD_BUFFER_SIZE(CFG_GROUP_A_SIZE)] = {0};
|
||||
return readCMD(RDCFGA, buffer, CMD_BUFFER_SIZE(CFG_GROUP_A_SIZE));
|
||||
return readCMD(RDCFGA, buffer, CFG_GROUP_A_SIZE);
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef amsCellMeasurement(Cell_Module* module) {
|
||||
HAL_StatusTypeDef amsCellMeasurement(Cell_Module (*module)[N_BMS]) {
|
||||
#warning check conversion counter to ensure that continous conversion has not been stopped
|
||||
#warning check for OW conditions: ADSV | ADSV_OW_0 / ADSV_OW_1
|
||||
return amsReadCellVoltages(module);
|
||||
return HAL_ERROR; //amsReadCellVoltages(module);
|
||||
}
|
||||
|
||||
HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module * module[static N_BMS]) {
|
||||
HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module (*module)[N_BMS]) {
|
||||
uint8_t rxbuf[CMD_BUFFER_SIZE(STATUS_GROUP_C_SIZE)] = {};
|
||||
|
||||
CHECK_RETURN(readCMD(RDSTATC, rxbuf, sizeof rxbuf));
|
||||
CHECK_RETURN(readCMD(RDSTATC, rxbuf, STATUS_GROUP_C_SIZE));
|
||||
for (size_t i = 0; i < N_BMS; i++) {
|
||||
size_t offset = BUFFER_BMS_OFFSET(i, STATUS_GROUP_C_SIZE);
|
||||
module[i]->status.CS_FLT = rxbuf[offset + 0] | (rxbuf[offset + 1] << 8);
|
||||
|
@ -104,7 +104,7 @@ HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module * module[static N_BMS])
|
|||
return HAL_BUSY;
|
||||
}
|
||||
|
||||
CHECK_RETURN(readCMD(RDAUXA, rxbuf, CMD_BUFFER_SIZE(AUX_GROUP_A_SIZE))); //STATUS_GROUP_C_SIZE is the same as AUX_GROUP_A_SIZE, so we can reuse the buffer
|
||||
CHECK_RETURN(readCMD(RDAUXA, rxbuf, AUX_GROUP_A_SIZE)); //STATUS_GROUP_C_SIZE is the same as AUX_GROUP_A_SIZE, so we can reuse the buffer
|
||||
for (size_t i = 0; i < N_BMS; i++) {
|
||||
size_t offset = BUFFER_BMS_OFFSET(i, AUX_GROUP_A_SIZE);
|
||||
module[i]->auxVoltages[0] = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8));
|
||||
|
@ -112,7 +112,7 @@ HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module * module[static N_BMS])
|
|||
module[i]->auxVoltages[2] = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8));
|
||||
}
|
||||
|
||||
CHECK_RETURN(readCMD(RDAUXB, rxbuf, CMD_BUFFER_SIZE(AUX_GROUP_B_SIZE)));
|
||||
CHECK_RETURN(readCMD(RDAUXB, rxbuf, AUX_GROUP_B_SIZE));
|
||||
for (size_t i = 0; i < N_BMS; i++) {
|
||||
size_t offset = BUFFER_BMS_OFFSET(i, AUX_GROUP_B_SIZE);
|
||||
module[i]->auxVoltages[3] = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8));
|
||||
|
@ -120,7 +120,7 @@ HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module * module[static N_BMS])
|
|||
module[i]->auxVoltages[5] = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8));
|
||||
}
|
||||
|
||||
CHECK_RETURN(readCMD(RDAUXC, rxbuf, CMD_BUFFER_SIZE(AUX_GROUP_C_SIZE)));
|
||||
CHECK_RETURN(readCMD(RDAUXC, rxbuf, AUX_GROUP_C_SIZE));
|
||||
for (size_t i = 0; i < N_BMS; i++) {
|
||||
size_t offset = BUFFER_BMS_OFFSET(i, AUX_GROUP_C_SIZE);
|
||||
module[i]->auxVoltages[6] = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8));
|
||||
|
@ -128,19 +128,19 @@ HAL_StatusTypeDef amsAuxAndStatusMeasurement(Cell_Module * module[static N_BMS])
|
|||
module[i]->auxVoltages[8] = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8));
|
||||
}
|
||||
|
||||
CHECK_RETURN(readCMD(RDAUXD, rxbuf, CMD_BUFFER_SIZE(AUX_GROUP_D_SIZE)));
|
||||
CHECK_RETURN(readCMD(RDAUXD, rxbuf, AUX_GROUP_D_SIZE));
|
||||
for (size_t i = 0; i < N_BMS; i++) {
|
||||
size_t offset = BUFFER_BMS_OFFSET(i, AUX_GROUP_D_SIZE);
|
||||
module[i]->auxVoltages[9] = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8));
|
||||
}
|
||||
|
||||
CHECK_RETURN(readCMD(RDSTATA, rxbuf, CMD_BUFFER_SIZE(STATUS_GROUP_A_SIZE)));
|
||||
CHECK_RETURN(readCMD(RDSTATA, rxbuf, STATUS_GROUP_A_SIZE));
|
||||
for (size_t i = 0; i < N_BMS; i++) {
|
||||
size_t offset = BUFFER_BMS_OFFSET(i, STATUS_GROUP_A_SIZE);
|
||||
module[i]->internalDieTemp = rxbuf[offset + 2] | (rxbuf[offset + 3] << 8);
|
||||
}
|
||||
|
||||
CHECK_RETURN(readCMD(RDSTATB, rxbuf, CMD_BUFFER_SIZE(STATUS_GROUP_B_SIZE)));
|
||||
CHECK_RETURN(readCMD(RDSTATB, rxbuf, STATUS_GROUP_B_SIZE));
|
||||
for (size_t i = 0; i < N_BMS; i++) {
|
||||
size_t offset = BUFFER_BMS_OFFSET(i, STATUS_GROUP_B_SIZE);
|
||||
module[i]->digitalSupplyVoltage = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8));
|
||||
|
@ -161,8 +161,8 @@ HAL_StatusTypeDef amsConfigBalancing(uint32_t channels[N_BMS], uint8_t dutyCycle
|
|||
uint8_t rxbufA[CMD_BUFFER_SIZE(PWM_GROUP_A_SIZE)] = {};
|
||||
uint8_t rxbufB[CMD_BUFFER_SIZE(PWM_GROUP_B_SIZE)] = {};
|
||||
|
||||
CHECK_RETURN(readCMD(RDPWMA, rxbufA, sizeof(rxbufA)));
|
||||
CHECK_RETURN(readCMD(RDPWMB, rxbufB, sizeof(rxbufB)));
|
||||
CHECK_RETURN(readCMD(RDPWMA, rxbufA, PWM_GROUP_A_SIZE));
|
||||
CHECK_RETURN(readCMD(RDPWMB, rxbufB, PWM_GROUP_B_SIZE));
|
||||
|
||||
for (size_t i = 0; i < N_BMS; i++) {
|
||||
size_t offsetA = BUFFER_BMS_OFFSET(i, PWM_GROUP_A_SIZE);
|
||||
|
@ -193,8 +193,8 @@ HAL_StatusTypeDef amsConfigBalancing(uint32_t channels[N_BMS], uint8_t dutyCycle
|
|||
}
|
||||
}
|
||||
|
||||
CHECK_RETURN(writeCMD(WRPWMA, rxbufA, sizeof(rxbufA)));
|
||||
CHECK_RETURN(writeCMD(WRPWMB, rxbufB, sizeof(rxbufB)));
|
||||
CHECK_RETURN(writeCMD(WRPWMA, rxbufA, PWM_GROUP_A_SIZE));
|
||||
CHECK_RETURN(writeCMD(WRPWMB, rxbufB, PWM_GROUP_B_SIZE));
|
||||
|
||||
return HAL_OK;
|
||||
}
|
||||
|
@ -206,7 +206,7 @@ HAL_StatusTypeDef amsStopBalancing() { return writeCMD(MUTE, CMD_EMPTY_BUFFER, 0
|
|||
HAL_StatusTypeDef amsSelfTest() { return 0; }
|
||||
|
||||
uint8_t amsConfigOverUnderVoltage(uint16_t overVoltage, uint16_t underVoltage) {
|
||||
uint8_t buffer[CFG_GROUP_A_SIZE];
|
||||
uint8_t buffer[CFG_GROUP_A_SIZE] = {};
|
||||
|
||||
if (underVoltage & 0xF000 || overVoltage & 0xF000) { // only 12 bits allowed
|
||||
return 1;
|
||||
|
|
|
@ -299,13 +299,13 @@ 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 arglen) {
|
||||
buffer[0] = (command >> 8) & 0xFF;
|
||||
buffer[1] = (command)&0xFF;
|
||||
calculateCommandPEC(buffer, 4);
|
||||
|
||||
mcuAdbmsCSLow();
|
||||
HAL_StatusTypeDef status = mcuSPITransmitReceive(buffer, buffer, buflen);
|
||||
HAL_StatusTypeDef status = mcuSPITransmitReceive(buffer, buffer, CMD_BUFFER_SIZE(arglen));
|
||||
mcuAdbmsCSHigh();
|
||||
|
||||
if (status != HAL_OK) return status;
|
||||
|
@ -319,15 +319,22 @@ HAL_StatusTypeDef readCMD(uint16_t command, uint8_t * buffer, size_t buflen) {
|
|||
//print out data bytes
|
||||
for (size_t i = 0; i < N_BMS; i++) {
|
||||
debug_log_cont(LOG_LEVEL_NOISY, "%d: ", i);
|
||||
for (size_t j = 0; j < buflen; j++) {
|
||||
debug_log_cont(LOG_LEVEL_NOISY, "%x ", buffer[4 + (i * (buflen + 2)) + j]);
|
||||
for (size_t j = 0; j < arglen; j++) {
|
||||
debug_log_cont(LOG_LEVEL_NOISY, "%x ", buffer[4 + (i * (arglen + 2)) + j]);
|
||||
}
|
||||
}
|
||||
|
||||
debug_log_cont(LOG_LEVEL_NOISY, "\n");
|
||||
}
|
||||
|
||||
//return checkDataPEC(&buffer[4], buflen + 2);
|
||||
|
||||
//check data PEC
|
||||
for (size_t i = 0; i < N_BMS; i++) {
|
||||
size_t offset = BUFFER_BMS_OFFSET(i, arglen);
|
||||
if (checkDataPEC(&buffer[offset], arglen + 2) != 0) {
|
||||
return HAL_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return HAL_OK;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue