|
本帖最后由 流心芝士鸡蛋仔 于 2023-4-26 17:17 编辑
HX280025 VCRC模块支持crc8、crc16、crc24、crc32和可配置多项式,均提供C版查表法和汇编算法。
对于crc16,其多项式包括 0x8005和0x1021,使用方法如下:
1. 使用前添加对应头文件
- #include "hx_vcrc.h"
- #include "crctable0x8005.h"
- #include "crctable0x8005reflected.h"
- #include "crctable0x1021.h"
- #include "crctable0x1021reflected.h"
复制代码
2. 将crc16算法文件 CRC_16_ASM.S 添加到工程中
3. CRC结构体定义
- //! \brief CRC structure
- //!
- typedef struct _CRC_Obj_{
- uint32_t seedValue; //!< Initial value of the CRC calculation
- uint16_t nMsgBytes; //!< the number of bytes in the message buffer
- CRC_parity_e parity; //!< the location, in a word, of the first byte of the CRC calculation
- uint32_t crcResult; //!< the calculated CRC
- void *pMsgBuffer; //!< Pointer to the message buffer
- void *pCrcTable; //!< Pointer to the CRC lookup table
- uint32_t nMsgBits; // provde the data size in bits if the CRC is to be computed bitwise
- uint32_t polynomial; // added to ensure customized polynomial can be added by the user
- uint16_t polySize; // provide the polynomial size for the customized polynomial
- uint16_t dataSize;
- uint16_t reflected;
- void (*init)(void *); //!< Function pointer to CRC initialization routine
- void (*run)(void *); //!< Function pointer to CRC computation routine
- }CRC_Obj;
复制代码 4. 结构体赋值及运算
- CRC_Obj CRC;
- CRC_Handle handleCRC;
- uint32_t crcResultC_1, crcResultC_2 ;
- ////////////////////////////////////////////////// C-16 /////////////////////////////////////////////////
- // Step 2: Initialize the CRC object
- CRC.seedValue = INIT_CRC16;
- CRC.nMsgBytes = NBYTES;
- CRC.parity = CRC_parity_even;
- CRC.crcResult = 0;
- CRC.pMsgBuffer = (uint8_t *)&testInput_test2[0];
- CRC.pCrcTable = (uint16_t *)&crc16P1Table[0];
- CRC.init = (void (*)(void *))CRC_init16Bit;
- CRC.run = (void (*)(void *))CRC_run16BitTableLookupC;
- // Step 3: Initialize the handle
- handleCRC = &CRC;
- // Step 4: Run the 16-bit table look-up CRC routine and save the first result
- CRC.init(handleCRC); //CRC_init16Bit
- CRC.run(handleCRC); //CRC_run16BitTableLookupC
- crcResultC_1 = CRC.crcResult; //=0x9331 16P1(0x8005)
- // Step 5: Load the second lookup table and run the C routine
- CRC.pCrcTable = (uint16_t *)&crc16P2Table[0];
- CRC.crcResult = 0;
- CRC.run(handleCRC);
- crcResultC_2 = CRC.crcResult; //=0x67fc 16P2(0x1021)
复制代码 5. 计算结果保存在 CRC.crcResult 中,读取即可。
所需文件及例程已经上传到附件中:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|