wuyage

Kinetis 硬件CRC使用

1
阅读(1735)

http://blog.chinaaet.com/wuyage/p/5100049902我在这一篇了介绍了CRC的具体计算过程,那么如何使用Kinetis MCU的硬件CRC模块呢?

这里以超核的CRC例程为例来说明,板子用的FRDM-K64F

int main(void) { uint32_t result; uint8_t buffer[10] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30}; CRC_QuickInit(kCRC16_CCITT); result = CRC_Generate(buffer, 10); printf("crc16 hardware:0x%04x\r\n", result); ... }

调用两个函数就可以参数CRC了。

2.jpg


再用软件计算一下,

1.jpg


两个结果是一样的。

在下面这个结构里定义个不同的CRC校验协议

/* common CRC protrool define */ static CRC_InitTypeDef CRCProtocolAttrTable[] = { {16, 0x0000U, 0x8005, kCRCTransposeBits, kCRCTransposeBoth, false}, {16, 0x0000U, 0x8005, kCRCTransposeBits, kCRCTransposeBoth, true}, {16, 0xFFFFU, 0x8005, kCRCTransposeBits, kCRCTransposeBoth, true}, {16, 0xFFFFU, 0x8005, kCRCTransposeBits, kCRCTransposeBoth, false}, {16, 0x0000U, 0x1021, kCRCTransposeBits, kCRCTransposeBoth, false}, //CCITT {16, 0xFFFFU, 0x1021, kCRCNoTranspose, kCRCNoTranspose, false}, //CCITT False {16, 0xFFFFU, 0x1021, kCRCTransposeBits, kCRCTransposeBoth, true}, //X25 {16, 0x0000U, 0x1021, kCRCNoTranspose, kCRCNoTranspose, false}, //XMODEM {16, 0x0000U, 0x3D65, kCRCTransposeBits, kCRCTransposeBoth, true}, //DNP {32, 0xFFFFFFFFU, 0x04C11DB7, kCRCTransposeBits, kCRCTransposeBoth, true}, //CRC32 {32, 0xFFFFFFFFU, 0x04C11DB7, kCRCNoTranspose, kCRCNoTranspose, false}, //CRC32-MPEG2 };

该数据结构的定义如下:

typedef struct { uint8_t crcWidth; /*!< Selects 16 or 32-bit CRC protocol */ uint32_t seed; /*!< Value of the seed (initial) CRC value */ uint32_t polynomial; /*!< Value of the polynomial for the CRC calculation */ CRC_Transpose_Type writeTranspose; /*!< Defines transpose configuration of the data written to the CRC data register */ CRC_Transpose_Type readTranspose; /*!< Defines transpose configuration of the value read from the CRC data register */ bool complementRead; /*!< Enables complement read of CRC data register */ } CRC_InitTypeDef;

这个结构体和CRC计算软件里的那几个参数是一一对应的。

16, 0x0000U, 0x1021, kCRCTransposeBits, kCRCTransposeBoth, false

16 对应width

0x0000U对应Init

0x1021对应Poly

kCRCTransposeBits对应RefIn是True

kCRCTransposeBoth 对应Refout是True

false对应XorOut 是0x0000

Baidu
map