1. h文件
/******************************************************************************
* 插座
* (c) Copyright 2016, RD Department, SHFZWKEJI Industry Co.Ltd
* All Rights Reserved
*
* FileName : CRC.h
* Description :
* Version : V1.0
* Function List:
* 1.CRC校验算法
* 2:CRC多项式 X32+X26+X23+X22+X16+X12+X11+X10+X8+X7+X5+X4+X2+X+1
* 3:CRC掩码 0x04C11DB7
*------------------------------Revision History-------------------------------
* No. Version Date Revised By Item Description
* 1 V1.0 2017-03-08 Liu Qi Sen 插座 Original Version
******************************************************************************/
/******************************************************************************
* Multi-Include-Prevent *
******************************************************************************/
#ifndef _CRC_H_
#define _CRC_H_
#ifdef __cplusplus
extern "C"{
#endif
/******************************************************************************
* Debug Switch Section *
******************************************************************************/
#define DEBUG_CRC
/******************************************************************************
* Include File Section *
******************************************************************************/
#include "typedef.h"
/******************************************************************************
* Pubilc Macro Define Section *
******************************************************************************/
#ifndef _CRC_C_
#define CRC_EXT extern
#else
#define CRC_EXT
#endif
/******************************************************************************
* Struct Define Section *
******************************************************************************/
/******************************************************************************
* Global Variable Declare Section *
******************************************************************************/
/******************************************************************************
* Function Declare Section *
******************************************************************************/
CRC_EXT void CRC_ModuleInit(void);
CRC_EXT uint32 CRC_8BitHardwareCalc(uint8 *buf, uint32 len);
CRC_EXT uint32 CRC_8BitHardwareCalc_ota(uint8 *buf, uint32 len);
CRC_EXT uint32 CRC_32BitHardwareCalc(uint32 *buf, uint32 len);
CRC_EXT void CRC_ResetValue(void);
#ifdef __cplusplus
}
#endif
#endif
/*********** (C) COPYRIGHT 2016 SHFZWKEJI Industry ******** END OF FILE *******/
2. c文件
/******************************************************************************
* 插座
* (c) Copyright 2017, RD Department, SHFZWKEJI Industry Co.Ltd
* All Rights Reserved
*
* FileName : CRC.c
* Description :
* Version : V1.0
* Function List:
* 1.CRC校验算法
* 2:CRC多项式 X32+X26+X23+X22+X16+X12+X11+X10+X8+X7+X5+X4+X2+X+1
* 2:CRC掩码 0x04C11DB7
*------------------------------Revision History-------------------------------
* No. Version Date Revised By Item Description
* 1 V1.0 2017-03-21 Liuqisen 插座 Original Version
******************************************************************************/
/******************************************************************************
* Include File Section *
******************************************************************************/
#define _CRC_C_
#include "CRC.h"
#undef _CRC_C_
/******************************************************************************
* Private Macro Define Section *
******************************************************************************/
/******************************************************************************
* Struct Define Section *
******************************************************************************/
/******************************************************************************
* Prototype Declare Section *
******************************************************************************/
/******************************************************************************
* Global Variable Declare Section *
******************************************************************************/
/******************************************************************************
* File Static Variable Declare Section *
******************************************************************************/
/******************************************************************************
* Function Declare Section *
******************************************************************************/
void CRC_ResetValue(void);
/*============================== External Function ===========================*/
/******************************************************************************
* Function Name:
* Decription :CRC 硬件模块初始化
* Calls :
* Called By :
* Input :
* Output :
* Return Value :
* Others :
*-------------------------------- Revision History ---------------------------
* No. Version Date Revised by Item Description
* 1 V1.0 2017-03-21 Liu qi sen 插座 Original Version
******************************************************************************/
void CRC_ModuleInit(void)
{
/* 打开CRC时钟 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE);
/* 复位CRC模块 */
CRC_ResetValue();
}
/******************************************************************************
* Function Name:
* Decription :CRC 8bit硬件计算
* Calls :
* Called By :
* Input :
* Output :
* Return Value :
* Others :
*-------------------------------- Revision History ---------------------------
* No. Version Date Revised by Item Description
* 1 V1.0 2017-03-21 Liu qi sen 插座 Original Version
******************************************************************************/
#if 0
uint32 CRC_8BitHardwareCalc(uint8 *buf, uint32 len)
{
uint32 loop;
uint8 buff[4] = { 0xff,0xff,0xff,0xff };
uint32 *p32 = (uint32 *)buff;
CRC_ResetValue();
for (loop = 0; loop < len; loop++)
{
buff[3] = buf[loop];
CRC->DR = *p32;
}
return CRC->DR;
}
#endif
uint32 CRC_8BitHardwareCalc(uint8 *buf, uint32 len)
{
uint32 index = 0;
CRC_ResetValue();
for (index = 0; index < len; index++)
{
CRC->DR = (uint32)buf[index];
}
return (CRC->DR);
}
//crc校验 wtj
uint32 CRC_8BitHardwareCalc_ota(uint8 *buf, uint32 len)
{
uint32 index = 0;
//CRC_ResetValue();
for (index = 0; index < len; index++)
{
CRC->DR = (uint32)buf[index];
}
return (CRC->DR);
}
/******************************************************************************
* Function Name:
* Decription :CRC 32bit硬件计算
* Calls :
* Called By :
* Input :
* Output :
* Return Value :
* Others :
*-------------------------------- Revision History ---------------------------
* No. Version Date Revised by Item Description
* 1 V1.0 2017-03-21 Liu qi sen 插座 Original Version
******************************************************************************/
uint32 CRC_32BitHardwareCalc(uint32 *buf, uint32 len)
{
uint32 loop;
CRC_ResetValue();
for (loop = 0; loop < len; loop++)
{
CRC->DR = buf[loop];
}
return CRC->DR;
}
/*============================== Internal Function ===========================*/
/******************************************************************************
* Function Name:
* Decription :CRC 硬件模块复位
* Calls :
* Called By :
* Input :
* Output :
* Return Value :
* Others :
*-------------------------------- Revision History ---------------------------
* No. Version Date Revised by Item Description
* 1 V1.0 2017-03-21 Liu qi sen 插座 Original Version
******************************************************************************/
void CRC_ResetValue(void)
{
CRC->CR = 1; //复位CRC寄存器
}
/******************************************************************************
* Function Name:
* Decription :软件CRC8bit计算
* Calls :
* Called By :
* Input :
* Output :
* Return Value :
* Others :
*-------------------------------- Revision History ---------------------------
* No. Version Date Revised by Item Description
* 1 V1.0 2017-03-21 Liu qi sen 插座 Original Version
******************************************************************************/
#if 0
static uint32 CRC_8BitSoftwareCalc(uint8 *buf, uint32 len)
{
uint32 xbit;
uint32 data;
uint32 CRC32 = 0xFFFFFFFF; //初值
uint32 bits;
uint32 loop;
uint32 buff[4] = { 0xff,0xff,0xff,0xff };
uint32 *p32 = (u32 *)buff;
const uint32 dwPolynomial = 0x04c11db7;
for (loop = 0; loop < len; loop++)
{
xbit = 1 << 31;
buff[3] = buf[i];
data = *p32;
for (bits = 0; bits < 32; bits++)
{
if (CRC32 & 0x80000000)
{
CRC32 <<= 1;
CRC32 ^= dwPolynomial;
}
else
{
CRC32 <<= 1;
}
if (data & xbit)
{
CRC32 ^= dwPolynomial;
}
xbit >>= 1;
}
}
return CRC32;
}
#endif
/******************************************************************************
* Function Name:
* Decription :软件CRC32bit计算
* Calls :
* Called By :
* Input :
* Output :
* Return Value :
* Others :
*-------------------------------- Revision History ---------------------------
* No. Version Date Revised by Item Description
* 1 V1.0 2017-03-21 Liu qi sen 插座 Original Version
******************************************************************************/
#if 0
static uint32 CRC_32BitSoftwareCalc(uint32 *buf, uint32 len)
{
uint32 xbit;
uint32 data;
uint32 CRC32 = 0xFFFFFFFF;
uint32 bits;
const u32 dwPolynomial = 0x04c11db7;
uint32 loop;
for (loop = 0; loop < len; loop++)
{
xbit = 1 << 31;
data = buf[i];
for (bits = 0; bits < 32; bits++)
{
if (CRC32 & 0x80000000)
{
CRC32 <<= 1;
CRC32 ^= dwPolynomial;
}
else
{
CRC32 <<= 1;
}
if (data & xbit)
{
CRC32 ^= dwPolynomial;
}
xbit >>= 1;
}
}
return CRC32;
}
#endif
/*********** (C) COPYRIGHT 2017 SHFZWKEJI Industry ******** END OF FILE ***********/