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 8f6942a..058c84f 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 @@ -31,7 +31,7 @@ HAL_StatusTypeDef amsReset() { amsWakeUp(); readCMD(SRST, CMD_EMPTY_BUFFER, 0); - uint8_t sidbuffer[CMD_BUFFER_SIZE(SID_GROUP_SIZE)] = {}; + /* uint8_t sidbuffer[CMD_BUFFER_SIZE(SID_GROUP_SIZE)] = {}; if (readCMD(RDSID, sidbuffer, SID_GROUP_SIZE) != HAL_OK) { bool nonzero = false; for (size_t i = 0; i < N_BMS; i++) { @@ -61,7 +61,8 @@ HAL_StatusTypeDef amsReset() { } debug_log_cont(LOG_LEVEL_INFO, "0x%lx%lx ", (uint32_t)(id >> 32), (uint32_t)(id & 0xFFFFFFFF)); //newlib does not support %llu } - } + } */ + mcuDelay(10); amsWakeUp(); amsStopBalancing(); diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_CRC_OLD.c b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_CRC_OLD.c deleted file mode 100644 index 6634bd1..0000000 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_CRC_OLD.c +++ /dev/null @@ -1,221 +0,0 @@ -#include -//command PEC calculation -//CRC-15 -//x^15 + x^14 + x^10 + x^8 + x^7 + x^4 + x^3 + 1 - -#define INITIAL_COMMAND_PEC 0x0010 -#define INITIAL_DATA_PEC 0x0010 - -uint8_t calculateCommandPEC(uint8_t* data, uint8_t datalen); -uint16_t updateCommandPEC(uint16_t currentPEC, uint8_t din); -uint8_t checkCommandPEC(uint8_t* data, uint8_t datalen); - -uint8_t calculateDataPEC(uint8_t* data, uint8_t datalen); -uint16_t updateDataPEC(uint16_t currentPEC, uint8_t din); -uint8_t checkDataPEC(uint8_t* data, uint8_t datalen); - -uint8_t calculateCommandPEC(uint8_t* data, uint8_t datalen) { - uint16_t currentpec = INITIAL_COMMAND_PEC; - if (datalen >= 3) { - for (int i = 0; i < (datalen - 2); i++) { - for (int n = 0; n < 8; n++) { - uint8_t din = data[i] << (n); - currentpec = updateCommandPEC(currentpec, din); - } - } - - data[datalen - 2] = (currentpec >> 7) & 0xFF; - data[datalen - 1] = (currentpec << 1) & 0xFF; - return 0; - } else { - return 1; - } - } - - uint8_t checkCommandPEC(uint8_t* data, uint8_t datalen) { - if (datalen <= 3) { - return 255; - } - - uint16_t currentpec = INITIAL_COMMAND_PEC; - - for (int i = 0; i < (datalen - 2); i++) { - for (int n = 0; n < 8; n++) { - uint8_t din = data[i] << (n); - currentpec = updateCommandPEC(currentpec, din); - } - } - - uint8_t pechigh = (currentpec >> 7) & 0xFF; - uint8_t peclow = (currentpec << 1) & 0xFF; - - if ((pechigh == data[datalen - 2]) && (peclow == data[datalen - 1])) { - return 0; - } - - return 1; - } - - uint16_t updateCommandPEC(uint16_t currentPEC, uint8_t din) { - din = (din >> 7) & 0x01; - uint8_t in0 = din ^ ((currentPEC >> 14) & 0x01); - uint8_t in3 = in0 ^ ((currentPEC >> 2) & 0x01); - uint8_t in4 = in0 ^ ((currentPEC >> 3) & 0x01); - uint8_t in7 = in0 ^ ((currentPEC >> 6) & 0x01); - uint8_t in8 = in0 ^ ((currentPEC >> 7) & 0x01); - uint8_t in10 = in0 ^ ((currentPEC >> 9) & 0x01); - uint8_t in14 = in0 ^ ((currentPEC >> 13) & 0x01); - - uint16_t newPEC = 0; - - newPEC |= in14 << 14; - newPEC |= (currentPEC & (0x01 << 12)) << 1; - newPEC |= (currentPEC & (0x01 << 11)) << 1; - newPEC |= (currentPEC & (0x01 << 10)) << 1; - newPEC |= in10 << 10; - newPEC |= (currentPEC & (0x01 << 8)) << 1; - newPEC |= in8 << 8; - newPEC |= in7 << 7; - newPEC |= (currentPEC & (0x01 << 5)) << 1; - newPEC |= (currentPEC & (0x01 << 4)) << 1; - newPEC |= in4 << 4; - newPEC |= in3 << 3; - newPEC |= (currentPEC & (0x01 << 1)) << 1; - newPEC |= (currentPEC & (0x01)) << 1; - newPEC |= in0; - - return newPEC; - } - - //data PEC calculation - //CRC-10 - //x^10 + x^7 + x^3 + x^2 + x + 1 - - uint16_t pec10_calc(bool rx_cmd, int len, uint8_t* data) { - uint16_t remainder = 16; /* PEC_SEED; 0000010000 */ - uint16_t polynom = 0x8F; /* x10 + x7 + x3 + x2 + x + 1 <- the CRC15 polynomial - 100 1000 1111 48F */ - - /* Perform modulo-2 division, a byte at a time. */ - for (uint8_t pbyte = 0; pbyte < len; ++pbyte) { - /* Bring the next byte into the remainder. */ - remainder ^= (uint16_t)(data[pbyte] << 2); - /* Perform modulo-2 division, a bit at a time.*/ - for (uint8_t bit_ = 8; bit_ > 0; --bit_) { - /* Try to divide the current data bit. */ - if ((remainder & 0x200) > - 0) // equivalent to remainder & 2^14 simply check for MSB - { - remainder = (uint16_t)((remainder << 1)); - remainder = (uint16_t)(remainder ^ polynom); - } else { - remainder = (uint16_t)(remainder << 1); - } - } - } - if (rx_cmd == true) { - remainder ^= (uint16_t)((data[len] & 0xFC) << 2); - /* Perform modulo-2 division, a bit at a time */ - for (uint8_t bit_ = 6; bit_ > 0; --bit_) { - /* Try to divide the current data bit */ - if ((remainder & 0x200) > - 0) // equivalent to remainder & 2^14 simply check for MSB - { - remainder = (uint16_t)((remainder << 1)); - remainder = (uint16_t)(remainder ^ polynom); - } else { - remainder = (uint16_t)((remainder << 1)); - } - } - } - return ((uint16_t)(remainder & 0x3FF)); - } - - typedef uint16_t crc; - crc F_CRC_CalculaCheckSum(uint8_t const AF_Datos[], uint16_t VF_nBytes); - - uint8_t calculateDataPEC(uint8_t* data, uint8_t datalen) { - - if (datalen >= 3) { - - - crc currentpec = pec10_calc(true, datalen - 2, data) & 0x3FF; // mask to 10 bits - - // memory layout is [[zeroes], PEC[9:8]], [PEC[7:0]] - data[datalen - 2] = (currentpec >> 8) & 0xFF; - data[datalen - 1] = currentpec & 0xFF; - - volatile uint8_t result = pec10_calc(true, datalen, data); - - return 0; - } else { - return 1; - } - } - - uint8_t checkDataPEC(uint8_t* data, uint8_t len) { - if (len <= 2) { - return 255; - } - - crc currentpec = F_CRC_CalculaCheckSum(data, len); - - return (currentpec == 0) ? 0 : 1; - } - - - static crc F_CRC_ObtenValorDeTabla(uint8_t VP_Pos_Tabla) { - crc VP_CRCTableValue = 0; - uint8_t VP_Pos_bit = 0; - - VP_CRCTableValue = ((crc)(VP_Pos_Tabla)) << (10 - 8); - - for (VP_Pos_bit = 0; VP_Pos_bit < 8; VP_Pos_bit++) { - if (VP_CRCTableValue & (((crc)1) << (10 - 1))) { - VP_CRCTableValue = (VP_CRCTableValue << 1) ^ 0x8F; - } else { - VP_CRCTableValue = (VP_CRCTableValue << 1); - } - } - return ((VP_CRCTableValue)); - } - crc F_CRC_CalculaCheckSum(uint8_t const AF_Datos[], uint16_t VF_nBytes) { - crc VP_CRCTableValue = 16; - int16_t VP_bytes = 0; - - for (VP_bytes = 0; VP_bytes < VF_nBytes; VP_bytes++) { - - VP_CRCTableValue = (VP_CRCTableValue << 8) ^ - F_CRC_ObtenValorDeTabla( - ((uint8_t)((VP_CRCTableValue >> (10 - 8)) & 0xFF)) ^ - AF_Datos[VP_bytes]); - } - - if ((8 * sizeof(crc)) > 10) { - VP_CRCTableValue = VP_CRCTableValue & ((((crc)(1)) << 10) - 1); - } - - return (VP_CRCTableValue ^ 0x0000); - } - - uint16_t updateDataPEC(uint16_t currentPEC, uint8_t din) { - din = (din >> 7) & 0x01; - uint8_t in0 = din ^ ((currentPEC >> 9) & 0x01); - uint8_t in2 = in0 ^ ((currentPEC >> 1) & 0x01); - uint8_t in3 = in0 ^ ((currentPEC >> 2) & 0x01); - uint8_t in7 = in0 ^ ((currentPEC >> 6) & 0x01); - - uint16_t newPEC = 0; - - newPEC |= (currentPEC & (0x01 << 8)) << 1; - newPEC |= (currentPEC & (0x01 << 7)) << 1; - newPEC |= in7 << 7; - newPEC |= (currentPEC & (0x01 << 5)) << 1; - newPEC |= (currentPEC & (0x01 << 4)) << 1; - newPEC |= in3 << 3; - newPEC |= in2 << 2; - newPEC |= (currentPEC & (0x01)) << 1; - newPEC |= in0; - - return newPEC; - } \ No newline at end of file diff --git a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c index d80b573..fce5053 100644 --- a/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c +++ b/AMS_Master_Code/Core/Lib/ADBMS6830B_Driver/Core/Src/ADBMS_LL_Driver.c @@ -13,14 +13,6 @@ #include #include -extern uint8_t calculateCommandPEC(uint8_t* data, uint8_t datalen); -extern uint16_t updateCommandPEC(uint16_t currentPEC, uint8_t din); -extern uint8_t checkCommandPEC(uint8_t* data, uint8_t datalen); - -extern uint8_t calculateDataPEC(uint8_t* data, uint8_t datalen); -extern uint16_t updateDataPEC(uint16_t currentPEC, uint8_t din); -extern uint8_t checkDataPEC(uint8_t* data, uint8_t datalen); - #define INITIAL_COMMAND_PEC 0x0010 #define INITIAL_DATA_PEC 0x0010 @@ -49,7 +41,7 @@ static HAL_StatusTypeDef mcuSPITransmitReceive(uint8_t* rxbuffer, uint8_t* txbuf return HAL_SPI_TransmitReceive(adbmsspi, txbuffer, rxbuffer, buffersize, ADBMS_SPI_TIMEOUT); } -/* //command PEC calculation +//command PEC calculation //CRC-15 //x^15 + x^14 + x^10 + x^8 + x^7 + x^4 + x^3 + 1 @@ -89,37 +81,32 @@ static uint8_t checkCommandPEC(uint8_t* data, uint8_t datalen) { //x^10 + x^7 + x^3 + x^2 + x + 1 static uint16_t computeCRC10(const uint8_t* data, size_t length, bool rx_cmd) { - uint16_t remainder = INITIAL_DATA_PEC; - const uint16_t poly = 0x8F; - size_t limit = length; + uint16_t remainder = INITIAL_DATA_PEC; + const uint16_t poly = 0x8F; - if (rx_cmd && (length > 0)) { - limit = length - 1; - } + for (size_t i = 0; i < length; i++) { + remainder ^= (uint16_t)(data[i] << 2); + for (int b = 0; b < 8; b++) { + if (remainder & 0x200) { + remainder = (uint16_t)((remainder << 1) ^ poly); + } else { + remainder <<= 1; + } + } + } - for (size_t i = 0; i < limit; i++) { - remainder ^= (uint16_t)(data[i] << 2); - for (int b = 0; b < 8; b++) { - if (remainder & 0x200) { - remainder = (uint16_t)((remainder << 1) ^ poly); - } else { - remainder <<= 1; - } - } - } - - // If receiving a command, handle leftover bits - if (rx_cmd && (length > 0)) { - remainder ^= (uint16_t)((data[length - 1] & 0xFC) << 2); - for (int b = 0; b < 6; b++) { - if (remainder & 0x200) { - remainder = (uint16_t)((remainder << 1) ^ poly); - } else { - remainder <<= 1; - } - } - } - return (uint16_t)(remainder & 0x3FF); + // Handle last bits of the last byte if rx_cmd is true + if (rx_cmd) { + remainder ^= (uint16_t)((data[length] & 0xFC) << 2); + for (int b = 0; b < 6; b++) { + if (remainder & 0x200) { + remainder = (uint16_t)((remainder << 1) ^ poly); + } else { + remainder <<= 1; + } + } + } + return (uint16_t)(remainder & 0x3FF); } static uint8_t calculateDataPEC(uint8_t* data, uint8_t datalen) { @@ -134,7 +121,7 @@ static uint8_t checkDataPEC(uint8_t* data, uint8_t len) { if (len <= 2) { return 255; } // Zero remainder means a valid CRC. return (computeCRC10(data, len, false) == 0) ? 0 : 1; -} */ +} static const char* const HAL_Statuses[] = {"HAL_OK", "HAL_ERROR", "HAL_BUSY", "HAL_TIMEOUT"}; @@ -196,6 +183,8 @@ HAL_StatusTypeDef ___writeCMD(uint16_t command, uint8_t * args, size_t arglen) { calculateCommandPEC(args, 4); for (size_t i = 0; i < N_BMS; i++) { + args[BUFFER_BMS_OFFSET(i, arglen) + arglen] = 0; // zero out the PEC + args[BUFFER_BMS_OFFSET(i, arglen) + arglen + 1] = 0; calculateDataPEC(&args[4 + (i * (arglen + 2))], arglen + 2); //DPEC is calculated over the data, not the command, and placed at the end of the data }