32位模式下CRC计算
uint32_t CRC_Cal32(uint32_t seed, uint8_t *msg, uint32_t sizeBytes)
{
uint32_t ctrl_reg,data_out,data_in;
uint32_t sizeDwords;
uint8_t *pCRCBytes;
uint32_t i,j;
/* 设置WaS=1,写入种子值 */
ctrl_reg = CRC0->CTRL;
CRC0->CTRL = ctrl_reg | 0x02000000;
CRC0->DATA = seed;
/* Set WaS=0,准备写入数据*/
CRC0->CTRL = ctrl_reg & 0xFD000000;
/*等待数据计算完成*/
sizeDwords = sizeBytes>>2;
j = 0;
for(i=0;i<sizeDwords;i++)
{
data_in = ((msg[j] << 24) | (msg[j+1] << 16) | (msg[j+2] << 8) | msg[j+3]); /*将数据写入CRC数据寄存器,以32方式写入*/
j += 4;
CRC0->DATA = data_in;
}
if (j<sizeBytes)
{
pCRCBytes = (uint8_t*)&CRC0->ACCESS8BIT.DATALL; /*以8位的方式写入剩余的数据*/
#if defined(BYTE_ENABLES_1_2_4_8)
/*只写单个字节*/
for(;j<sizeBytes;j++)
{
*pCRCBytes++ = msg[j];
}
#elif defined(BYTE_ENABLES_3_6_C)
/*写入两个字节*/
data_in = 0;
i = 0;
for(;j<sizeBytes;j++)
{
data_in = (data_in <<8) | msg[j];
i++;
if (i==2)
{
i = 0;
CRC0->ACCESS16BIT.DATAL = data_in;
}
}
if (i==1)
{
CRC0->ACCESS8BIT.DATALL = data_in; /*!< 写入最后一个字节 */
}
#elif defined(BYTE_ENABLES_7_E)
/*!< 写入三个字节*/
data_in = 0;
i = 0;
for(;j<sizeBytes;j++)
{
data_in = (data_in <<8) | msg[j];
i++;
if (i==3)
{
i = 0;
/*写入第一个字符*/
CRC0->ACCESS8BIT.DATAHL = (data_in>>16) & 0xff; /*!< 写入高字的低字节 */
/*写入最后两个字节*/
CRC0->ACCESS16BIT.DATAL = data_in & 0x00ffff; /*!< 写入低字 */
}
}
if ( i == 2)
{
CRC0->ACCESS16BIT.DATAL = (data_in); /*!< 写最后两个字节 */
}
else if (i == 1)
{
CRC0->ACCESS8BIT.DATALL = data_in; /*!< 写最后一个字节 */
}
#else /*!< 只写低字节 */
for(;j<sizeBytes;j++)
{
*pCRCBytes = msg[j];
}
#endif
}
data_out=CRC0->DATA;
return(data_out); //读出32位计算结果
}
|