changes for old crc

This commit is contained in:
Kilian Bracher 2025-03-22 16:07:14 +01:00
parent d5609dc54b
commit 016afc755f
3 changed files with 222 additions and 6 deletions

View File

@ -184,6 +184,6 @@
#define PWM_GROUP_ID 17 #define PWM_GROUP_ID 17
#define PWM_S_CONTROL_GROUP_B_ID 18 #define PWM_S_CONTROL_GROUP_B_ID 18
#define SID_GROUP_SIZE 1 #define SID_GROUP_SIZE 6
#endif /* INC_ADBMS_CMD_MAKROS_H_ */ #endif /* INC_ADBMS_CMD_MAKROS_H_ */

View File

@ -31,8 +31,6 @@ HAL_StatusTypeDef amsReset() {
amsWakeUp(); amsWakeUp();
readCMD(SRST, CMD_EMPTY_BUFFER, 0); 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) { if (readCMD(RDSID, sidbuffer, SID_GROUP_SIZE) != HAL_OK) {
bool nonzero = false; bool nonzero = false;
@ -64,9 +62,6 @@ 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 debug_log_cont(LOG_LEVEL_INFO, "0x%lx%lx ", (uint32_t)(id >> 32), (uint32_t)(id & 0xFFFFFFFF)); //newlib does not support %llu
} }
} }
*/
mcuDelay(10); mcuDelay(10);
amsWakeUp(); amsWakeUp();
amsStopBalancing(); amsStopBalancing();

View File

@ -0,0 +1,221 @@
#include <stdint.h>
//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;
}