diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c index 1f370b2..e1e0ed9 100644 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_Abstraction.c @@ -96,79 +96,80 @@ HAL_StatusTypeDef amsCellMeasurement(Cell_Module (*module)[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, 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); - module[i]->status.CCTS = rxbuf[offset + 2] | (rxbuf[offset + 3] << 8); - module[i]->status.VA_OV = (rxbuf[offset + 4] >> 7) & 0x01; - module[i]->status.VA_UV = (rxbuf[offset + 4] >> 6) & 0x01; - module[i]->status.VD_OV = (rxbuf[offset + 4] >> 5) & 0x01; - module[i]->status.VD_UV = (rxbuf[offset + 4] >> 4) & 0x01; - module[i]->status.CED = (rxbuf[offset + 4] >> 3) & 0x01; - module[i]->status.CMED = (rxbuf[offset + 4] >> 2) & 0x01; - module[i]->status.SED = (rxbuf[offset + 4] >> 1) & 0x01; - module[i]->status.SMED = (rxbuf[offset + 4] >> 0) & 0x01; - module[i]->status.VDEL = (rxbuf[offset + 5] >> 7) & 0x01; - module[i]->status.VDE = (rxbuf[offset + 5] >> 6) & 0x01; - module[i]->status.COMPARE= (rxbuf[offset + 5] >> 5) & 0x01; - module[i]->status.SPIFLT = (rxbuf[offset + 5] >> 4) & 0x01; - module[i]->status.SLEEP = (rxbuf[offset + 5] >> 3) & 0x01; - module[i]->status.THSD = (rxbuf[offset + 5] >> 2) & 0x01; - module[i]->status.TMODCHK= (rxbuf[offset + 5] >> 1) & 0x01; - module[i]->status.OSCCHK = (rxbuf[offset + 5] >> 0) & 0x01; + (*module)[i].status.CS_FLT = rxbuf[offset + 0] | (rxbuf[offset + 1] << 8); + (*module)[i].status.CCTS = rxbuf[offset + 2] | (rxbuf[offset + 3] << 8); + (*module)[i].status.VA_OV = (rxbuf[offset + 4] >> 7) & 0x01; + (*module)[i].status.VA_UV = (rxbuf[offset + 4] >> 6) & 0x01; + (*module)[i].status.VD_OV = (rxbuf[offset + 4] >> 5) & 0x01; + (*module)[i].status.VD_UV = (rxbuf[offset + 4] >> 4) & 0x01; + (*module)[i].status.CED = (rxbuf[offset + 4] >> 3) & 0x01; + (*module)[i].status.CMED = (rxbuf[offset + 4] >> 2) & 0x01; + (*module)[i].status.SED = (rxbuf[offset + 4] >> 1) & 0x01; + (*module)[i].status.SMED = (rxbuf[offset + 4] >> 0) & 0x01; + (*module)[i].status.VDEL = (rxbuf[offset + 5] >> 7) & 0x01; + (*module)[i].status.VDE = (rxbuf[offset + 5] >> 6) & 0x01; + (*module)[i].status.COMPARE = (rxbuf[offset + 5] >> 5) & 0x01; + (*module)[i].status.SPIFLT = (rxbuf[offset + 5] >> 4) & 0x01; + (*module)[i].status.SLEEP = (rxbuf[offset + 5] >> 3) & 0x01; + (*module)[i].status.THSD = (rxbuf[offset + 5] >> 2) & 0x01; + (*module)[i].status.TMODCHK = (rxbuf[offset + 5] >> 1) & 0x01; + (*module)[i].status.OSCCHK = (rxbuf[offset + 5] >> 0) & 0x01; } if (pollCMD(PLAUX) == HAL_BUSY) { return HAL_BUSY; } - 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 + 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)); - module[i]->auxVoltages[1] = mV_from_ADBMS6830(rxbuf[offset + 2] | (rxbuf[offset + 3] << 8)); - module[i]->auxVoltages[2] = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8)); + (*module)[i].auxVoltages[0] = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8)); + (*module)[i].auxVoltages[1] = mV_from_ADBMS6830(rxbuf[offset + 2] | (rxbuf[offset + 3] << 8)); + (*module)[i].auxVoltages[2] = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8)); } 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)); - module[i]->auxVoltages[4] = mV_from_ADBMS6830(rxbuf[offset + 2] | (rxbuf[offset + 3] << 8)); - module[i]->auxVoltages[5] = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8)); + (*module)[i].auxVoltages[3] = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8)); + (*module)[i].auxVoltages[4] = mV_from_ADBMS6830(rxbuf[offset + 2] | (rxbuf[offset + 3] << 8)); + (*module)[i].auxVoltages[5] = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8)); } 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)); - module[i]->auxVoltages[7] = mV_from_ADBMS6830(rxbuf[offset + 2] | (rxbuf[offset + 3] << 8)); - module[i]->auxVoltages[8] = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8)); + (*module)[i].auxVoltages[6] = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8)); + (*module)[i].auxVoltages[7] = mV_from_ADBMS6830(rxbuf[offset + 2] | (rxbuf[offset + 3] << 8)); + (*module)[i].auxVoltages[8] = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8)); } 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)); + (*module)[i].auxVoltages[9] = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8)); } 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); + (*module)[i].internalDieTemp = rxbuf[offset + 2] | (rxbuf[offset + 3] << 8); } 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)); - module[i]->analogSupplyVoltage = mV_from_ADBMS6830(rxbuf[offset + 2] | (rxbuf[offset + 3] << 8)); - module[i]->refVoltage = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8)); + (*module)[i].digitalSupplyVoltage = mV_from_ADBMS6830(rxbuf[offset + 0] | (rxbuf[offset + 1] << 8)); + (*module)[i].analogSupplyVoltage = mV_from_ADBMS6830(rxbuf[offset + 2] | (rxbuf[offset + 3] << 8)); + (*module)[i].refVoltage = mV_from_ADBMS6830(rxbuf[offset + 4] | (rxbuf[offset + 5] << 8)); } - CHECK_RETURN(writeCMD(ADAX | ADAX_CONV_ALL, rxbuf, 0)); //start aux conversion for next iteration + CHECK_RETURN(writeCMD(ADAX | ADAX_CONV_ALL, rxbuf, 0)); // start aux conversion for next iteration return HAL_OK; } @@ -190,7 +191,7 @@ HAL_StatusTypeDef amsConfigBalancing(const uint32_t channels[static N_BMS], uint for (size_t c = 0; c < 16; c += 2) { uint8_t high = (channels[i] & (1 << (c + 1))) ? (dutyCycle << 4) : 0; - uint8_t low = (channels[i] & (1 << c)) ? dutyCycle : 0; + uint8_t low = (channels[i] & (1 << c)) ? dutyCycle : 0; if (c < 12) { rxbufA[offsetA + (c / 2)] = high | low; @@ -199,8 +200,8 @@ HAL_StatusTypeDef amsConfigBalancing(const uint32_t channels[static N_BMS], uint } } - //log the new PWM settings - //output is: PWM - [BMS_ID]: [PWM0] ... [PWM16] + // log the new PWM settings + // output is: PWM - [BMS_ID]: [PWM0] ... [PWM16] if (DEBUG_CHANNEL_ENABLED(LOG_LEVEL_DEBUG)) { debug_log(LOG_LEVEL_DEBUG, "PWM - %d: ", i); for (size_t j = 0; j < 6; j++) { @@ -232,19 +233,20 @@ HAL_StatusTypeDef amsConfigOverUnderVoltage(uint16_t overVoltage, uint16_t under return HAL_ERROR; } - debug_log(LOG_LEVEL_INFO, "Configuring OV/UV thresholds to %f/%f", mV_from_ADBMS6830(overVoltage), mV_from_ADBMS6830(underVoltage)); + debug_log(LOG_LEVEL_INFO, "Configuring OV/UV thresholds to %f/%f", mV_from_ADBMS6830(overVoltage), + mV_from_ADBMS6830(underVoltage)); CHECK_RETURN(readCMD(RDCFGB, buffer, CFG_GROUP_B_SIZE)); for (size_t i = 0; i < N_BMS; i++) { size_t offset = BUFFER_BMS_OFFSET(i, CFG_GROUP_B_SIZE); - //UV + // UV buffer[offset + 0] = (uint8_t)(underVoltage & 0xFF); buffer[offset + 1] &= 0xF0; buffer[offset + 1] |= (uint8_t)((underVoltage >> 8) & 0x0F); - //OV + // OV buffer[offset + 1] &= 0x0F; buffer[offset + 1] |= (uint8_t)(overVoltage << 4); buffer[offset + 2] = (uint8_t)(overVoltage >> 4); @@ -261,15 +263,16 @@ HAL_StatusTypeDef amsCheckUnderOverVoltage(Cell_Module (*module)[N_BMS]) { for (size_t i = 0; i < N_BMS; i++) { size_t offset = BUFFER_BMS_OFFSET(i, STATUS_GROUP_D_SIZE); uint32_t ov_uv_data = 0; - ov_uv_data = (regbuffer[offset + 0] << 0) | (regbuffer[offset + 1] << 8) | - (regbuffer[offset + 2] << 16) | (regbuffer[offset + 3] << 24); + ov_uv_data = (regbuffer[offset + 0] << 0) | (regbuffer[offset + 1] << 8) | (regbuffer[offset + 2] << 16) | + (regbuffer[offset + 3] << 24); - module[i]->overVoltage = 0; - module[i]->underVoltage = 0; + (*module)[i].overVoltage = 0; + (*module)[i].underVoltage = 0; - for (size_t j = 0; j < numberofCells; j++) { // ov/uv flags are 1-bit flags for each cell C0UV, C0OV, C1UV, C1OV, ... - module[i]->underVoltage |= (ov_uv_data >> (j * 2)) & 0x01; - module[i]->overVoltage |= (ov_uv_data >> (j * 2 + 1)) & 0x01; + for (size_t j = 0; j < numberofCells; + j++) { // ov/uv flags are 1-bit flags for each cell C0UV, C0OV, C1UV, C1OV, ... + (*module)[i].underVoltage |= (ov_uv_data >> (j * 2)) & 0x01; + (*module)[i].overVoltage |= (ov_uv_data >> (j * 2 + 1)) & 0x01; } } @@ -281,9 +284,7 @@ HAL_StatusTypeDef amsClearFlag() { return writeCMD(CLRFLAG, buffer, 6); } -HAL_StatusTypeDef amsClearAux() { - return writeCMD(CLRAUX, CMD_EMPTY_BUFFER, 0); -} +HAL_StatusTypeDef amsClearAux() { return writeCMD(CLRAUX, CMD_EMPTY_BUFFER, 0); } HAL_StatusTypeDef amsReadCellVoltages(Cell_Module (*module)[N_BMS]) { uint8_t rxbuffer[CMD_BUFFER_SIZE(CV_GROUP_A_SIZE)] = {}; @@ -291,47 +292,47 @@ HAL_StatusTypeDef amsReadCellVoltages(Cell_Module (*module)[N_BMS]) { CHECK_RETURN(readCMD(RDCVA, rxbuffer, CV_GROUP_A_SIZE)); for (size_t i = 0; i < N_BMS; i++) { size_t offset = BUFFER_BMS_OFFSET(i, CV_GROUP_A_SIZE); - module[i]->cellVoltages[0] = mV_from_ADBMS6830(rxbuffer[offset + 0] | (rxbuffer[offset + 1] << 8)); - module[i]->cellVoltages[1] = mV_from_ADBMS6830(rxbuffer[offset + 2] | (rxbuffer[offset + 3] << 8)); - module[i]->cellVoltages[2] = mV_from_ADBMS6830(rxbuffer[offset + 4] | (rxbuffer[offset + 5] << 8)); + (*module)[i].cellVoltages[0] = mV_from_ADBMS6830(rxbuffer[offset + 0] | (rxbuffer[offset + 1] << 8)); + (*module)[i].cellVoltages[1] = mV_from_ADBMS6830(rxbuffer[offset + 2] | (rxbuffer[offset + 3] << 8)); + (*module)[i].cellVoltages[2] = mV_from_ADBMS6830(rxbuffer[offset + 4] | (rxbuffer[offset + 5] << 8)); } CHECK_RETURN(readCMD(RDCVB, rxbuffer, CV_GROUP_A_SIZE)); for (size_t i = 0; i < N_BMS; i++) { size_t offset = BUFFER_BMS_OFFSET(i, CV_GROUP_A_SIZE); - module[i]->cellVoltages[3] = mV_from_ADBMS6830(rxbuffer[offset + 0] | (rxbuffer[offset + 1] << 8)); - module[i]->cellVoltages[4] = mV_from_ADBMS6830(rxbuffer[offset + 2] | (rxbuffer[offset + 3] << 8)); - module[i]->cellVoltages[5] = mV_from_ADBMS6830(rxbuffer[offset + 4] | (rxbuffer[offset + 5] << 8)); + (*module)[i].cellVoltages[3] = mV_from_ADBMS6830(rxbuffer[offset + 0] | (rxbuffer[offset + 1] << 8)); + (*module)[i].cellVoltages[4] = mV_from_ADBMS6830(rxbuffer[offset + 2] | (rxbuffer[offset + 3] << 8)); + (*module)[i].cellVoltages[5] = mV_from_ADBMS6830(rxbuffer[offset + 4] | (rxbuffer[offset + 5] << 8)); } CHECK_RETURN(readCMD(RDCVC, rxbuffer, CV_GROUP_A_SIZE)); for (size_t i = 0; i < N_BMS; i++) { size_t offset = BUFFER_BMS_OFFSET(i, CV_GROUP_A_SIZE); - module[i]->cellVoltages[6] = mV_from_ADBMS6830(rxbuffer[offset + 0] | (rxbuffer[offset + 1] << 8)); - module[i]->cellVoltages[7] = mV_from_ADBMS6830(rxbuffer[offset + 2] | (rxbuffer[offset + 3] << 8)); - module[i]->cellVoltages[8] = mV_from_ADBMS6830(rxbuffer[offset + 4] | (rxbuffer[offset + 5] << 8)); + (*module)[i].cellVoltages[6] = mV_from_ADBMS6830(rxbuffer[offset + 0] | (rxbuffer[offset + 1] << 8)); + (*module)[i].cellVoltages[7] = mV_from_ADBMS6830(rxbuffer[offset + 2] | (rxbuffer[offset + 3] << 8)); + (*module)[i].cellVoltages[8] = mV_from_ADBMS6830(rxbuffer[offset + 4] | (rxbuffer[offset + 5] << 8)); } CHECK_RETURN(readCMD(RDCVD, rxbuffer, CV_GROUP_A_SIZE)); for (size_t i = 0; i < N_BMS; i++) { size_t offset = BUFFER_BMS_OFFSET(i, CV_GROUP_A_SIZE); - module[i]->cellVoltages[9] = mV_from_ADBMS6830(rxbuffer[offset + 0] | (rxbuffer[offset + 1] << 8)); - module[i]->cellVoltages[10] = mV_from_ADBMS6830(rxbuffer[offset + 2] | (rxbuffer[offset + 3] << 8)); - module[i]->cellVoltages[11] = mV_from_ADBMS6830(rxbuffer[offset + 4] | (rxbuffer[offset + 5] << 8)); + (*module)[i].cellVoltages[9] = mV_from_ADBMS6830(rxbuffer[offset + 0] | (rxbuffer[offset + 1] << 8)); + (*module)[i].cellVoltages[10] = mV_from_ADBMS6830(rxbuffer[offset + 2] | (rxbuffer[offset + 3] << 8)); + (*module)[i].cellVoltages[11] = mV_from_ADBMS6830(rxbuffer[offset + 4] | (rxbuffer[offset + 5] << 8)); } CHECK_RETURN(readCMD(RDCVE, rxbuffer, CV_GROUP_A_SIZE)); for (size_t i = 0; i < N_BMS; i++) { size_t offset = BUFFER_BMS_OFFSET(i, CV_GROUP_A_SIZE); - module[i]->cellVoltages[12] = mV_from_ADBMS6830(rxbuffer[offset + 0] | (rxbuffer[offset + 1] << 8)); - module[i]->cellVoltages[13] = mV_from_ADBMS6830(rxbuffer[offset + 2] | (rxbuffer[offset + 3] << 8)); - module[i]->cellVoltages[14] = mV_from_ADBMS6830(rxbuffer[offset + 4] | (rxbuffer[offset + 5] << 8)); + (*module)[i].cellVoltages[12] = mV_from_ADBMS6830(rxbuffer[offset + 0] | (rxbuffer[offset + 1] << 8)); + (*module)[i].cellVoltages[13] = mV_from_ADBMS6830(rxbuffer[offset + 2] | (rxbuffer[offset + 3] << 8)); + (*module)[i].cellVoltages[14] = mV_from_ADBMS6830(rxbuffer[offset + 4] | (rxbuffer[offset + 5] << 8)); } CHECK_RETURN(readCMD(RDCVF, rxbuffer, CV_GROUP_A_SIZE)); for (size_t i = 0; i < N_BMS; i++) { size_t offset = BUFFER_BMS_OFFSET(i, CV_GROUP_A_SIZE); - module[i]->cellVoltages[15] = mV_from_ADBMS6830(rxbuffer[offset + 0] | (rxbuffer[offset + 1] << 8)); + (*module)[i].cellVoltages[15] = mV_from_ADBMS6830(rxbuffer[offset + 0] | (rxbuffer[offset + 1] << 8)); } return HAL_OK;