|
|
发表于 2025-12-19 16:10:49
|
显示全部楼层
可以的,我在G4系列上测试过,H7跟G4类似,CRC都是多项式、宽度可配置的,应该也是可以的
[C] 纯文本查看 复制代码
struct crc_info_t
{
MOL_CRC_Width_T width;
uint32_t poly;
uint32_t init;
uint32_t xor_out;
bool ref_in;
bool ref_out;
};
enum class MOL_CRC_Type_T
{
CRC32_MPEG2 = 0,
CRC32_HDLC,
CRC32_ISCSI,
CRC32_CKSUM,
CRC32_BZIP2,
CRC32_AUTOSAR,
CRC16_XMODEM,
CRC16_MODBUS,
CRC8_SMBUS,
CRC8_AUTOSAR,
CRC8_BLUETOOTH,
LENGTH
};
namespace
{
crc_info_t crc_info[static_cast<uint32_t>(MOL_CRC_Type_T::LENGTH)] = {
{MOL_CRC_Width_T::Width_32bit, 0x04C11DB7, 0xFFFFFFFF, 0x00000000, false, false},
{MOL_CRC_Width_T::Width_32bit, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true},
{MOL_CRC_Width_T::Width_32bit, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true},
{MOL_CRC_Width_T::Width_32bit, 0x04C11DB7, 0x00000000, 0xFFFFFFFF, false, false},
{MOL_CRC_Width_T::Width_32bit, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false},
{MOL_CRC_Width_T::Width_32bit, 0xF4ACFB13, 0xFFFFFFFF, 0xFFFFFFFF, true, true},
{MOL_CRC_Width_T::Width_16bit, 0x1021, 0x0000, 0x0000, false, false},
{MOL_CRC_Width_T::Width_16bit, 0x8005, 0xFFFF, 0x0000, true, true},
{MOL_CRC_Width_T::Width_8bit, 0x07, 0x00, 0x00, false, false},
{MOL_CRC_Width_T::Width_8bit, 0x2F, 0xFF, 0xFF, false, false},
{MOL_CRC_Width_T::Width_8bit, 0xA7, 0x00, 0x00, true, true},
};
}
[C] 纯文本查看 复制代码 class MOL_CRC_T
{
static constexpr crc_info_t const* info_tbl = crc_info;
MOL_CRC_Type_T type;
crc_info_t crc_cfg;
public:
MOL_CRC_T() = default;
~MOL_CRC_T() = default;
explicit MOL_CRC_T(MOL_CRC_Type_T _type)
{
this->type = _type;
crc_cfg = info_tbl[static_cast<uint32_t>(_type)];
SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_CRCEN);
LL_CRC_SetPolynomialSize(CRC, static_cast<uint32_t>(crc_cfg.width));
LL_CRC_SetInitialData(CRC, crc_cfg.init);
LL_CRC_SetPolynomialCoef(CRC, crc_cfg.poly);
// 根据ref_in配置输入数据反转模式
if (crc_cfg.ref_in)
{
LL_CRC_SetInputDataReverseMode(CRC, LL_CRC_INDATA_REVERSE_BYTE);
}
else
{
LL_CRC_SetInputDataReverseMode(CRC, LL_CRC_INDATA_REVERSE_NONE);
}
// 根据ref_out配置输出数据反转模式
if (crc_cfg.ref_out)
{
LL_CRC_SetOutputDataReverseMode(CRC, LL_CRC_OUTDATA_REVERSE_BIT);
}
else
{
LL_CRC_SetOutputDataReverseMode(CRC, LL_CRC_OUTDATA_REVERSE_NONE);
}
}
uint32_t clac_w_reset(uint32_t start_addr, uint32_t len)
{
LL_CRC_ResetCRCCalculationUnit(CRC);
return clac_wo_reset(start_addr, len);
}
uint32_t clac_wo_reset(uint32_t start_addr, uint32_t len)
{
auto ptr = reinterpret_cast<uint8_t*>(start_addr);
for (uint32_t i = 0; i < len; i++)
{
LL_CRC_FeedData8(CRC, ptr[i]);
}
uint32_t result = 0;
if (crc_cfg.width == MOL_CRC_Width_T::Width_32bit)
{
result = LL_CRC_ReadData32(CRC);
}
else if (crc_cfg.width == MOL_CRC_Width_T::Width_16bit)
{
result = LL_CRC_ReadData16(CRC);
}
else if (crc_cfg.width == MOL_CRC_Width_T::Width_8bit)
{
result = LL_CRC_ReadData8(CRC);
}
else
{
result = LL_CRC_ReadData7(CRC);
}
// 应用XOR_OUT
return result ^ crc_cfg.xor_out;
}
};
这些都验证测试过,三方验证,MCU计算,网页计算,python计算,还验证出python中AutoSAR的计算有特殊之处要用一个专用的库才行
|
|