diff --git a/Kconfig b/Kconfig new file mode 100644 index 0000000..d2ff6fe --- /dev/null +++ b/Kconfig @@ -0,0 +1,165 @@ + +# Kconfig file for package crclib + +menuconfig PKG_USING_CRCLIB + bool "crclib : A library of functions with 8-bit ,16-bit ,32-bit crc check calculations." + default n + +if PKG_USING_CRCLIB + + config PKG_CRCLIB_PATH + string + default "/packages/misc/crclib" + + config CRCLIB_USING_CRC8 + bool "using 8 bit crc" + default y + + if CRCLIB_USING_CRC8 + config CRC8_USING_CONST_TABLE + bool "using const table" + default y + + choice + prompt "crc8 polynome" + default CRC8_POLY_8C + help + Select crc8 polynome + config CRC8_POLY_8C + bool "x8+x5+x4+1" + config CRC8_POLY_D9 + bool "x8+x7+x4+x3+x+1" + config CRC8_POLY_CUSTOM + bool "custom value" + endchoice + + if CRC8_POLY_CUSTOM + config CRC8_POLY_CUSTOM_STRING + int "crc8 polynome value (int)" + default 0 + endif + + config CRC8_POLY + int + default 140 if CRC8_POLY_8C + default 217 if CRC8_POLY_D9 + default CRC8_POLY_CUSTOM_STRING if CRC8_POLY_CUSTOM + endif + + config CRCLIB_USING_CRC16 + bool "using 16 bit crc" + default y + + if CRCLIB_USING_CRC16 + config CRC16_USING_CONST_TABLE + bool "using const table" + default y + + choice + prompt "crc16 polynome" + default CRC16_POLY_A001 + help + Select crc16 polynome + config CRC16_POLY_A001 + bool "x16+x15+x2+1 (MODBUS,IBM,SDLC)" + config CRC16_POLY_8404 + bool "x16+x12+x5+1 (CCITT,ISO,HDLC,ITUX25,PPP-FCS)" + config CRC16_POLY_CUSTOM + bool "custom value" + endchoice + + if CRC16_POLY_CUSTOM + config CRC16_POLY_CUSTOM_STRING + int "crc16 polynome value (int)" + default 0 + endif + + config CRC16_POLY + int + default 40961 if CRC16_POLY_A001 + default 33800 if CRC16_POLY_8404 + default CRC16_POLY_CUSTOM_STRING if CRC16_POLY_CUSTOM + endif + + config CRCLIB_USING_CRC32 + bool "using 32 bit crc" + default y + + if CRCLIB_USING_CRC32 + config CRC32_USING_CONST_TABLE + bool "using const table" + default y + + choice + prompt "crc32 polynome" + default CRC32_POLY_EDB88320 + help + Select crc32 polynome + config CRC32_POLY_EDB88320 + bool "x32+x26+x23+...+x2+x+1 (ZIP,RAR,IEEE,LAN/FDDI,PPP-FCS)" + config CRC32_POLY_82F63B78 + bool "x32+x28+x27+...+x8+x6+1 (SCTP)" + config CRC32_POLY_CUSTOM + bool "custom value" + endchoice + + if CRC32_POLY_CUSTOM + config CRC32_POLY_CUSTOM_STRING + int "crc32 polynome value (int)" + default 0 + endif + + config CRC32_POLY + int + default 3988292384 if CRC32_POLY_EDB88320 + default 2197175160 if CRC32_POLY_82F63B78 + default CRC32_POLY_CUSTOM_STRING if CRC32_POLY_CUSTOM + endif + + config CRCLIB_USING_CRC8_EX + bool "using 8 bit crc expand" + default n + + config CRCLIB_USING_CRC16_EX + bool "using 16 bit crc expand" + default n + + config CRCLIB_USING_CRC32_EX + bool "using 32 bit crc expand" + default n + + config CRCLIB_USING_CRC16_EX_SAMPLE + bool "using 16 bit crc expand sample" + default n + + config CRCLIB_USING_CRC_HW + bool "using hardware crc" + default n + + config CRCLIB_USING_CRC_HW_SAMPLE + bool "using hardware crc sample" + default n + + choice + prompt "Version" + default PKG_USING_CRCLIB_V102 + help + Select the package version + config PKG_USING_CRCLIB_LATEST_VERSION + bool "latest" + config PKG_USING_CRCLIB_V102 + bool "v1.02" + config PKG_USING_CRCLIB_V101 + bool "v1.01" + config PKG_USING_CRCLIB_V100 + bool "v1.00" + endchoice + + config PKG_CRCLIB_VER + string + default "latest" if PKG_USING_CRCLIB_LATEST_VERSION + default "v1.02" if PKG_USING_CRCLIB_V102 + default "v1.01" if PKG_USING_CRCLIB_V101 + default "v1.00" if PKG_USING_CRCLIB_V100 + +endif diff --git a/SConscript b/SConscript index 8b5c295..a44ac90 100644 --- a/SConscript +++ b/SConscript @@ -2,8 +2,8 @@ from building import * cwd = GetCurrentDir() path = [cwd+'/inc'] -src = Glob('src/*.c') - -group = DefineGroup('crclib', src, depend = ['PKG_USING_CRCLIB'], CPPPATH = path) +src = Glob('src/*.c') -Return('group') \ No newline at end of file +group = DefineGroup('crclib', src, depend=['PKG_USING_CRCLIB'], CPPPATH=path) + +Return('group') diff --git a/inc/crc.h b/inc/crc.h index 57ad72f..e6038d5 100644 --- a/inc/crc.h +++ b/inc/crc.h @@ -9,15 +9,20 @@ #ifndef __CRC_H__ #define __CRC_H__ -#include +#ifdef __cplusplus +extern "C" { +#endif + #include -#include -#include #include +#include #include -#include +#include +#include #include #include - +#include +#ifdef __cplusplus +} +#endif #endif - diff --git a/inc/crc16.h b/inc/crc16.h index 91892bb..a1d74a2 100644 --- a/inc/crc16.h +++ b/inc/crc16.h @@ -9,13 +9,18 @@ #ifndef __CRC16_H__ #define __CRC16_H__ -#include "typedef.h" +#ifdef __cplusplus +extern "C" { +#endif + #include "crc_cfg.h" +#include "typedef.h" #ifdef CRCLIB_USING_CRC16 -#if (!defined(CRC16_USING_CONST_TABLE) || ((CRC16_POLY != 0xA001) && (CRC16_POLY != 0x8408))) -/* +#if (!defined(CRC16_USING_CONST_TABLE) || \ + ((CRC16_POLY != 0xA001) && (CRC16_POLY != 0x8408))) +/* * @brief cyclic initialize crc table * @param none * @retval none @@ -23,24 +28,25 @@ void crc16_table_init(void); #endif -/* +/* * @brief cyclic calculation crc check value * @param init_val - initial value * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ -u16 crc16_cyc_cal(u16 init_val, u8 *pdata, u32 len); +uint16_t crc16_cyc_cal(uint16_t init_val, uint8_t *pdata, uint32_t len); -/* +/* * @brief calculation crc check value, initial is 0xFFFF * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ -u16 crc16_cal(u8 *pdata, u32 len); +uint16_t crc16_cal(uint8_t *pdata, uint32_t len); #endif - +#ifdef __cplusplus +} +#endif #endif - diff --git a/inc/crc16_ex.h b/inc/crc16_ex.h index 8fce2ae..4160a98 100644 --- a/inc/crc16_ex.h +++ b/inc/crc16_ex.h @@ -9,18 +9,22 @@ #ifndef __CRC16_EX_H__ #define __CRC16_EX_H__ -#include "typedef.h" +#ifdef __cplusplus +extern "C" { +#endif + #include "crc_cfg.h" +#include "typedef.h" #ifdef CRCLIB_USING_CRC16_EX -#define CRC16_EX_INIT_VAL 0xFFFF +#define CRC16_EX_INIT_VAL 0xFFFF -typedef struct{ - u16 table[256]; -}crc16_inst_t; +typedef struct { + u16 table[256]; +} crc16_inst_t; -/* +/* * @brief initialize crc instance * @param hinst - instance handle * @param poly - polynomial of crc @@ -28,25 +32,27 @@ typedef struct{ */ void crc16_ex_init(crc16_inst_t *hinst, u16 poly); -/* +/* * @brief cyclic calculation crc check value * @param hinst - instance handle * @param init_val - initial value * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ u16 crc16_ex_cyc_cal(crc16_inst_t *hinst, u16 init_val, u8 *pdata, u32 len); -/* +/* * @brief calculation crc check value, initial is 0xFFFF * @param hinst - instance handle * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ u16 crc16_ex_cal(crc16_inst_t *hinst, u8 *pdata, u32 len); +#endif +#ifdef __cplusplus +} #endif #endif - diff --git a/inc/crc16_ex_sample.h b/inc/crc16_ex_sample.h index be8a490..c43ffdf 100644 --- a/inc/crc16_ex_sample.h +++ b/inc/crc16_ex_sample.h @@ -9,19 +9,27 @@ #ifndef __CRC16_EX_SAMPLE_H__ #define __CRC16_EX_SAMPLE_H__ +#ifdef __cplusplus +extern "C" { +#endif + #include #ifdef CRCLIB_USING_CRC16_EX #ifdef CRCLIB_USING_CRC16_EX_SAMPLE -#define CRC16_EX_SAMPLE_POLY 0xA001 +#define CRC16_EX_SAMPLE_POLY 0xA001 extern crc16_inst_t crc16_ex_sample_inst; -#define CRC16_EX_SAMPLE_CYC_CAL(init, pd, len) crc16_ex_cyc_cal(&crc16_ex_sample_inst, init, pd, len) -#define CRC16_EX_SAMPLE_CAL(pd, len) crc16_ex_cal(&crc16_ex_sample_inst, pd, len) +#define CRC16_EX_SAMPLE_CYC_CAL(init, pd, len) \ + crc16_ex_cyc_cal(&crc16_ex_sample_inst, init, pd, len) +#define CRC16_EX_SAMPLE_CAL(pd, len) \ + crc16_ex_cal(&crc16_ex_sample_inst, pd, len) #endif #endif +#ifdef __cplusplus +} +#endif #endif - diff --git a/inc/crc32.h b/inc/crc32.h index 92bd64d..5caa22e 100644 --- a/inc/crc32.h +++ b/inc/crc32.h @@ -9,13 +9,18 @@ #ifndef __CRC32_H__ #define __CRC32_H__ -#include "typedef.h" +#ifdef __cplusplus +extern "C" { +#endif + #include "crc_cfg.h" +#include "typedef.h" #ifdef CRCLIB_USING_CRC32 -#if (!defined(CRC32_USING_CONST_TABLE) || ((CRC32_POLY != 0xEDB88320) && (CRC32_POLY != 0x82F63B78))) -/* +#if (!defined(CRC32_USING_CONST_TABLE) || \ + ((CRC32_POLY != 0xEDB88320) && (CRC32_POLY != 0x82F63B78))) +/* * @brief cyclic initialize crc table * @param none * @retval none @@ -23,24 +28,25 @@ void crc32_table_init(void); #endif -/* +/* * @brief cyclic calculation crc check value * @param init_val - initial value * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ -u32 crc32_cyc_cal(u32 init_val, u8 *pdata, u32 len); +uint32_t crc32_cyc_cal(uint32_t init_val, uint8_t *pduint32_t, uint32_t len); -/* +/* * @brief calculation crc check value, initial is CRC32_INIT_VAL * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ -u32 crc32_cal(u8 *pdata, u32 len); +uint32_t crc32_cal(uint8_t *pduint32_t, uint32_t len); #endif - +#ifdef __cplusplus +} +#endif #endif - diff --git a/inc/crc32_ex.h b/inc/crc32_ex.h index ff931eb..f741443 100644 --- a/inc/crc32_ex.h +++ b/inc/crc32_ex.h @@ -9,18 +9,22 @@ #ifndef __CRC32_EX_H__ #define __CRC32_EX_H__ -#include "typedef.h" +#ifdef __cplusplus +extern "C" { +#endif + #include "crc_cfg.h" +#include "typedef.h" #ifdef CRCLIB_USING_CRC32_EX -#define CRC32_EX_INIT_VAL 0xFFFFFFFF +#define CRC32_EX_INIT_VAL 0xFFFFFFFF -typedef struct{ - u32 table[256]; -}crc32_inst_t; +typedef struct { + u32 table[256]; +} crc32_inst_t; -/* +/* * @brief initialize crc instance * @param hinst - instance handle * @param poly - polynomial of crc @@ -28,25 +32,27 @@ typedef struct{ */ void crc32_ex_init(crc32_inst_t *hinst, u32 poly); -/* +/* * @brief cyclic calculation crc check value * @param hinst - instance handle * @param init_val - initial value * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ u32 crc32_ex_cyc_cal(crc32_inst_t *hinst, u32 init_val, u8 *pdata, u32 len); -/* +/* * @brief calculation crc check value, initial is 0xFFFFFFFF * @param hinst - instance handle * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ u32 crc32_ex_cal(crc32_inst_t *hinst, u8 *pdata, u32 len); +#endif +#ifdef __cplusplus +} #endif #endif - diff --git a/inc/crc8.h b/inc/crc8.h index ab82a18..c053dfd 100644 --- a/inc/crc8.h +++ b/inc/crc8.h @@ -9,13 +9,18 @@ #ifndef __CRC8_H__ #define __CRC8_H__ -#include +#ifdef __cplusplus +extern "C" { +#endif + #include "crc_cfg.h" +#include #ifdef CRCLIB_USING_CRC8 -#if (!defined(CRC8_USING_CONST_TABLE) || ((CRC8_POLY != 0x8C) && (CRC8_POLY != 0xD9))) -/* +#if (!defined(CRC8_USING_CONST_TABLE) || \ + ((CRC8_POLY != 0x8C) && (CRC8_POLY != 0xD9))) +/* * @brief cyclic initialize crc table * @param none * @retval none @@ -23,24 +28,25 @@ void crc8_table_init(void); #endif -/* +/* * @brief cyclic calculation crc check value * @param init_val - initial value * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ -u8 crc8_cyc_cal(u8 init_val, u8 *pdata, u32 len); +uint8_t crc8_cyc_cal(uint8_t init_val, uint8_t *pdata, uint32_t len); -/* +/* * @brief calculation crc check value, initial is CRC8_INIT_VOL * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ -u8 crc8_cal(u8 *pdata, u32 len); +uint8_t crc8_cal(uint8_t *pdata, uint32_t len); #endif - +#ifdef __cplusplus +} +#endif #endif - diff --git a/inc/crc8_ex.h b/inc/crc8_ex.h index 133f941..0a77f39 100644 --- a/inc/crc8_ex.h +++ b/inc/crc8_ex.h @@ -9,18 +9,22 @@ #ifndef __CRC8_EX_H__ #define __CRC8_EX_H__ -#include "typedef.h" +#ifdef __cplusplus +extern "C" { +#endif + #include "crc_cfg.h" +#include "typedef.h" #ifdef CRCLIB_USING_CRC8_EX -#define CRC8_EX_INIT_VAL 0xFF +#define CRC8_EX_INIT_VAL 0xFF -typedef struct{ - u8 table[256]; -}crc8_inst_t; +typedef struct { + u8 table[256]; +} crc8_inst_t; -/* +/* * @brief initialize crc instance * @param hinst - instance handle * @param poly - polynomial of crc @@ -28,25 +32,27 @@ typedef struct{ */ void crc8_ex_init(crc8_inst_t *hinst, u8 poly); -/* +/* * @brief cyclic calculation crc check value * @param hinst - instance handle * @param init_val - initial value * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ u8 crc8_ex_cyc_cal(crc8_inst_t *hinst, u8 init_val, u8 *pdata, u32 len); -/* +/* * @brief calculation crc check value, initial is 0xFFFF * @param hinst - instance handle * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ u8 crc8_ex_cal(crc8_inst_t *hinst, u8 *pdata, u32 len); +#endif +#ifdef __cplusplus +} #endif #endif - diff --git a/inc/crc_cfg.h b/inc/crc_cfg.h index 4511d44..b120729 100644 --- a/inc/crc_cfg.h +++ b/inc/crc_cfg.h @@ -9,50 +9,62 @@ #ifndef __CRC_CFG_H__ #define __CRC_CFG_H__ +#ifdef __cplusplus +extern "C" { +#endif + #include -//#define CRCLIB_USING_CRC8 -//#define CRCLIB_USING_CRC16 -//#define CRCLIB_USING_CRC32 -//#define CRCLIB_USING_CRC8_EX -//#define CRCLIB_USING_CRC16_EX -//#define CRCLIB_USING_CRC32_EX -//#define CRCLIB_USING_CRC16_EX_SAMPLE -//#define CRCLIB_USING_CRC_HW -//#define CRCLIB_USING_CRC_HW_SAMPLE +// #define CRCLIB_USING_CRC8 +// #define CRCLIB_USING_CRC16 +// #define CRCLIB_USING_CRC32 +// #define CRCLIB_USING_CRC8_EX +// #define CRCLIB_USING_CRC16_EX +// #define CRCLIB_USING_CRC32_EX +// #define CRCLIB_USING_CRC16_EX_SAMPLE +// #define CRCLIB_USING_CRC_HW +// #define CRCLIB_USING_CRC_HW_SAMPLE #ifdef CRCLIB_USING_CRC8 -//#define CRC8_USING_CONST_TABLE //using const table in flash memory, nodefined using table in ram +// #define CRC8_USING_CONST_TABLE //using const table in flash memory, nodefined +// using table in ram #ifndef CRC8_POLY -#define CRC8_POLY 0x8C //crc8 polynome, Poly = x8+x5+x4+1 -//#define CRC8_POLY 0xD9 //crc8 polynome, Poly = x8+x7+x4+x3+x+1 +#define CRC8_POLY 0x8C // crc8 polynome, Poly = x8+x5+x4+1 +// #define CRC8_POLY 0xD9 //crc8 polynome, Poly = x8+x7+x4+x3+x+1 #endif #ifndef CRC8_INIT_VAL -#define CRC8_INIT_VAL 0xFF +#define CRC8_INIT_VAL 0xFF #endif #endif #ifdef CRCLIB_USING_CRC16 -//#define CRC16_USING_CONST_TABLE //using const table in flash memory, nodefined using table in ram +// #define CRC16_USING_CONST_TABLE //using const table in flash memory, +// nodefined using table in ram #ifndef CRC16_POLY -#define CRC16_POLY 0xA001 //crc16 polynome, Poly = x16+x15+x2+1 (IBM,SDLC) -//#define CRC16_POLY 0x8408 //crc16 polynome, Poly = x16+x12+x5+1 (CCITT,ISO,HDLC,ITUX25,PPP-FCS) +#define CRC16_POLY 0xA001 // crc16 polynome, Poly = x16+x15+x2+1 (IBM,SDLC) +// #define CRC16_POLY 0x8408 //crc16 polynome, Poly = x16+x12+x5+1 +// (CCITT,ISO,HDLC,ITUX25,PPP-FCS) #endif #ifndef CRC16_INIT_VAL -#define CRC16_INIT_VAL 0xFFFF +#define CRC16_INIT_VAL 0xFFFF #endif #endif #ifdef CRCLIB_USING_CRC32 -//#define CRC32_USING_CONST_TABLE //using const table in flash memory, nodefined using table in ram +// #define CRC32_USING_CONST_TABLE //using const table in flash memory, +// nodefined using table in ram #ifndef CRC32_POLY -#define CRC32_POLY 0xEDB88320 //crc32 polynome, Poly = x32+x26+x23+...+x2+x+1 (ZIP,RAR,IEEE,LAN/FDDI,PPP-FCS) -//#define CRC32_POLY 0x82F63B78 //crc32 polynome, Poly = x32+x28+x27+...+x8+x6+1 (SCTP) +#define CRC32_POLY \ + 0xEDB88320 // crc32 polynome, Poly = x32+x26+x23+...+x2+x+1 + // (ZIP,RAR,IEEE,LAN/FDDI,PPP-FCS) +// #define CRC32_POLY 0x82F63B78 //crc32 polynome, Poly = +// x32+x28+x27+...+x8+x6+1 (SCTP) #endif #ifndef CRC32_INIT_VAL -#define CRC32_INIT_VAL 0xFFFFFFFF +#define CRC32_INIT_VAL 0xFFFFFFFF #endif #endif - +#ifdef __cplusplus +} +#endif #endif - diff --git a/inc/crc_hw_hc32.h b/inc/crc_hw_hc32.h index 1aa44d7..7249ffa 100644 --- a/inc/crc_hw_hc32.h +++ b/inc/crc_hw_hc32.h @@ -9,8 +9,12 @@ #ifndef __CRC_HW_HC32_H__ #define __CRC_HW_HC32_H__ -#include "typedef.h" +#ifdef __cplusplus +extern "C" { +#endif + #include "crc_cfg.h" +#include "typedef.h" #ifdef CRCLIB_USING_CRC_HW @@ -19,15 +23,15 @@ #ifdef CHIP_FAMILY_HC32 #if defined(CHIP_NAME_HC32F460) -#define CRC_HW_INIT_VAL 0xFFFFFFFF -#define CRC_RTOS_DELAY(x) rt_thread_delay(x) +#define CRC_HW_INIT_VAL 0xFFFFFFFF +#define CRC_RTOS_DELAY(x) rt_thread_delay(x) -typedef struct{ - u32 size; - u32 poly; -}crc_hw_inst_t; +typedef struct { + u32 size; + u32 poly; +} crc_hw_inst_t; -/* +/* * @brief initialize crc instance * @param hinst - instance handle * @param poly - polynomial of crc @@ -35,27 +39,29 @@ typedef struct{ */ void crc_hw_init(crc_hw_inst_t *hinst, u32 poly); -/* +/* * @brief cyclic calculation crc check value * @param hinst - instance handle * @param init_val - initial value * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ u32 crc_hw_cyc_cal(crc_hw_inst_t *hinst, u32 init_val, u8 *pdata, u32 len); -/* +/* * @brief calculation crc check value, initial is CRC_HW_INIT_VAL * @param hinst - instance handle * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ u32 crc_hw_cal(crc_hw_inst_t *hinst, u8 *pdata, u32 len); #endif #endif #endif +#ifdef __cplusplus +} +#endif #endif - diff --git a/inc/crc_hw_n32.h b/inc/crc_hw_n32.h index c496760..1bfbbdb 100644 --- a/inc/crc_hw_n32.h +++ b/inc/crc_hw_n32.h @@ -9,8 +9,12 @@ #ifndef __CRC_HW_N32_H__ #define __CRC_HW_N32_H__ -#include "typedef.h" +#ifdef __cplusplus +extern "C" { +#endif + #include "crc_cfg.h" +#include "typedef.h" #ifdef CRCLIB_USING_CRC_HW @@ -18,15 +22,15 @@ #ifdef CHIP_FAMILY_N32 -#define CRC_HW_INIT_VAL 0xFFFFFFFF -#define CRC_RTOS_DELAY(x) rt_thread_delay(x) +#define CRC_HW_INIT_VAL 0xFFFFFFFF +#define CRC_RTOS_DELAY(x) rt_thread_delay(x) -typedef struct{ - u32 size; - u32 poly; -}crc_hw_inst_t; +typedef struct { + u32 size; + u32 poly; +} crc_hw_inst_t; -/* +/* * @brief initialize crc instance * @param hinst - instance handle * @param poly - polynomial of crc @@ -34,26 +38,28 @@ typedef struct{ */ void crc_hw_init(crc_hw_inst_t *hinst, u32 poly); -/* +/* * @brief cyclic calculation crc check value * @param hinst - instance handle * @param init_val - initial value * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ u32 crc_hw_cyc_cal(crc_hw_inst_t *hinst, u32 init_val, u8 *pdata, u32 len); -/* +/* * @brief calculation crc check value, initial is CRC_HW_INIT_VAL * @param hinst - instance handle * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ u32 crc_hw_cal(crc_hw_inst_t *hinst, u8 *pdata, u32 len); #endif #endif +#ifdef __cplusplus +} +#endif #endif - diff --git a/inc/crc_hw_sample.h b/inc/crc_hw_sample.h index 7db3301..2537030 100644 --- a/inc/crc_hw_sample.h +++ b/inc/crc_hw_sample.h @@ -9,34 +9,44 @@ #ifndef __CRC_HW_SAMPLE_H__ #define __CRC_HW_SAMPLE_H__ +#ifdef __cplusplus +extern "C" { +#endif + #include #ifdef CRCLIB_USING_CRC_HW #ifdef CRCLIB_USING_CRC_HW_SAMPLE -#define CRC8_HW_SAMPLE_POLY 0x8C +#define CRC8_HW_SAMPLE_POLY 0x8C extern crc_hw_inst_t crc8_hw_sample_inst; -#define CRC8_HW_SAMPLE_CYC_CAL(init, pd, len) (u8)crc_hw_cyc_cal(&crc8_hw_sample_inst, init, pd, len) -#define CRC8_HW_SAMPLE_CAL(pd, len) (u8)crc_hw_cal(&crc8_hw_sample_inst, pd, len) +#define CRC8_HW_SAMPLE_CYC_CAL(init, pd, len) \ + (u8) crc_hw_cyc_cal(&crc8_hw_sample_inst, init, pd, len) +#define CRC8_HW_SAMPLE_CAL(pd, len) \ + (u8) crc_hw_cal(&crc8_hw_sample_inst, pd, len) -#define CRC16_HW_SAMPLE_POLY 0xA001 +#define CRC16_HW_SAMPLE_POLY 0xA001 extern crc_hw_inst_t crc16_hw_sample_inst; -#define CRC16_HW_SAMPLE_CYC_CAL(init, pd, len) (u16)crc_hw_cyc_cal(&crc16_hw_sample_inst, init, pd, len) -#define CRC16_HW_SAMPLE_CAL(pd, len) (u16)crc_hw_cal(&crc16_hw_sample_inst, pd, len) - +#define CRC16_HW_SAMPLE_CYC_CAL(init, pd, len) \ + (u16) crc_hw_cyc_cal(&crc16_hw_sample_inst, init, pd, len) +#define CRC16_HW_SAMPLE_CAL(pd, len) \ + (u16) crc_hw_cal(&crc16_hw_sample_inst, pd, len) -#define CRC32_HW_SAMPLE_POLY 0xEDB88320 +#define CRC32_HW_SAMPLE_POLY 0xEDB88320 extern crc_hw_inst_t crc32_hw_sample_inst; -#define CRC32_HW_SAMPLE_CYC_CAL(init, pd, len) crc_hw_cyc_cal(&crc32_hw_sample_inst, init, pd, len) -#define CRC32_HW_SAMPLE_CAL(pd, len) crc_hw_cal(&crc32_hw_sample_inst, pd, len) +#define CRC32_HW_SAMPLE_CYC_CAL(init, pd, len) \ + crc_hw_cyc_cal(&crc32_hw_sample_inst, init, pd, len) +#define CRC32_HW_SAMPLE_CAL(pd, len) crc_hw_cal(&crc32_hw_sample_inst, pd, len) #endif #endif +#ifdef __cplusplus +} +#endif #endif - diff --git a/inc/crc_hw_stm32.h b/inc/crc_hw_stm32.h index bdc6d5a..c152608 100644 --- a/inc/crc_hw_stm32.h +++ b/inc/crc_hw_stm32.h @@ -9,8 +9,12 @@ #ifndef __CRC_HW_STM32_H__ #define __CRC_HW_STM32_H__ -#include "typedef.h" +#ifdef __cplusplus +extern "C" { +#endif + #include "crc_cfg.h" +#include "typedef.h" #ifdef CRCLIB_USING_CRC_HW @@ -18,15 +22,15 @@ #ifdef CHIP_FAMILY_STM32 -#define CRC_HW_INIT_VAL 0xFFFFFFFF -#define CRC_RTOS_DELAY(x) rt_thread_delay(x) +#define CRC_HW_INIT_VAL 0xFFFFFFFF +#define CRC_RTOS_DELAY(x) rt_thread_delay(x) -typedef struct{ - u32 size; - u32 poly; -}crc_hw_inst_t; +typedef struct { + u32 size; + u32 poly; +} crc_hw_inst_t; -/* +/* * @brief initialize crc instance * @param hinst - instance handle * @param poly - polynomial of crc @@ -34,26 +38,28 @@ typedef struct{ */ void crc_hw_init(crc_hw_inst_t *hinst, u32 poly); -/* +/* * @brief cyclic calculation crc check value * @param hinst - instance handle * @param init_val - initial value * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ u32 crc_hw_cyc_cal(crc_hw_inst_t *hinst, u32 init_val, u8 *pdata, u32 len); -/* +/* * @brief calculation crc check value, initial is CRC_HW_INIT_VAL * @param hinst - instance handle * @param pdata - datas pointer * @param len - datas len - * @retval calculated result + * @retval calculated result */ u32 crc_hw_cal(crc_hw_inst_t *hinst, u8 *pdata, u32 len); #endif #endif +#ifdef __cplusplus +} +#endif #endif - diff --git a/inc/typedef.h b/inc/typedef.h index 2fd683a..5f6741e 100644 --- a/inc/typedef.h +++ b/inc/typedef.h @@ -8,37 +8,14 @@ //--------------------------------------- #ifndef __TYPEDEF_H__ #define __TYPEDEF_H__ -//--------------------------------------- -typedef signed char s8; -typedef signed short s16; -typedef signed long s32; -typedef signed long long s64; - -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned long u32; -typedef unsigned long long u64; - -typedef float f32; -typedef double f64; -#ifndef bool -typedef enum {FALSE = 0, TRUE = !FALSE} bool; +#ifdef __cplusplus +extern "C" { #endif -typedef void (*PHOOK_t)(void); +#include -#ifndef NULL -#define NULL (void *)0 +#ifdef __cplusplus +} #endif - -#ifndef true -#define true 1 #endif - -#ifndef false -#define false 0 -#endif -//--------------------------------------- -#endif - diff --git a/readme.md b/readme.md index 10f544f..9caaa38 100644 --- a/readme.md +++ b/readme.md @@ -207,30 +207,28 @@ crclib package 遵循 LGPLv2.1 许可,详见 `LICENSE` 文件。 ### 2.3配置参数说明 -| 参数宏 | 说明 | -| ---- | ---- | -| CRCLIB_USING_CRC8 | 使用8位CRC -| CRCLIB_USING_CRC16 | 使用16位CRC -| CRCLIB_USING_CRC32 | 使用32位CRC -| CRC8_USING_CONST_TABLE | 使用8位CRC常量校验表 -| CRC8_POLY | 8位CRC校验多项式 -| CRC8_INIT_VAL | 8位CRC校验初始值 -| CRC16_USING_CONST_TABLE | 使用16位CRC常量校验表 -| CRC16_POLY | 16位CRC校验多项式 -| CRC16_INIT_VAL | 16位CRC校验初始值 -| CRC32_USING_CONST_TABLE | 使用32位CRC常量校验表 -| CRC32_POLY | 32位CRC校验多项式 -| CRC32_INIT_VAL | 32位CRC校验初始值 -| CRCLIB_USING_CRC8_EX | 使用8位CRC扩展模块 -| CRCLIB_USING_CRC16_EX | 使用16位CRC扩展模块 -| CRCLIB_USING_CRC32_EX | 使用32位CRC扩展模块 -| CRCLIB_USING_CRC16_EX_SAMPLE | 使用16位CRC扩展模块使用示例 -| CRCLIB_USING_CRC_HW | 使用硬件CRC模块 -| CRCLIB_USING_CRC_HW_SAMPLE | 使用硬件CRC模块使用示例 +| 参数宏 | 说明 | +| ---------------------------- | --------------------------- | +| CRCLIB_USING_CRC8 | 使用8位CRC | +| CRCLIB_USING_CRC16 | 使用16位CRC | +| CRCLIB_USING_CRC32 | 使用32位CRC | +| CRC8_USING_CONST_TABLE | 使用8位CRC常量校验表 | +| CRC8_POLY | 8位CRC校验多项式 | +| CRC8_INIT_VAL | 8位CRC校验初始值 | +| CRC16_USING_CONST_TABLE | 使用16位CRC常量校验表 | +| CRC16_POLY | 16位CRC校验多项式 | +| CRC16_INIT_VAL | 16位CRC校验初始值 | +| CRC32_USING_CONST_TABLE | 使用32位CRC常量校验表 | +| CRC32_POLY | 32位CRC校验多项式 | +| CRC32_INIT_VAL | 32位CRC校验初始值 | +| CRCLIB_USING_CRC8_EX | 使用8位CRC扩展模块 | +| CRCLIB_USING_CRC16_EX | 使用16位CRC扩展模块 | +| CRCLIB_USING_CRC32_EX | 使用32位CRC扩展模块 | +| CRCLIB_USING_CRC16_EX_SAMPLE | 使用16位CRC扩展模块使用示例 | +| CRCLIB_USING_CRC_HW | 使用硬件CRC模块 | +| CRCLIB_USING_CRC_HW_SAMPLE | 使用硬件CRC模块使用示例 | ## 3. 联系方式 * 维护:qiyongzhong * 主页:https://gitee.com/qiyongzhong0/crclib - - diff --git a/src/crc16.c b/src/crc16.c index 04c6b6b..5bb57e7 100644 --- a/src/crc16.c +++ b/src/crc16.c @@ -11,120 +11,105 @@ #ifdef CRCLIB_USING_CRC16 #if (defined(CRC16_USING_CONST_TABLE) && (CRC16_POLY == 0xA001)) -static const u16 crc16_table[] = { - 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, - 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, - 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, - 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, - 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, - 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41, - 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, - 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040, - 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, - 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, - 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, - 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840, - 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, - 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40, - 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, - 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041, - 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, - 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441, - 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, - 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, - 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, - 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40, - 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, - 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041, - 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, - 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440, - 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, - 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841, - 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, - 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41, - 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, - 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040 -}; +static const uint16_t crc16_table[] = { + 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, 0xC601, + 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, 0xCC01, 0x0CC0, + 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 0x0A00, 0xCAC1, 0xCB81, + 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, 0xD801, 0x18C0, 0x1980, 0xD941, + 0x1B00, 0xDBC1, 0xDA81, 0x1A40, 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, + 0x1DC0, 0x1C80, 0xDC41, 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, + 0x1680, 0xD641, 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, + 0x1040, 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, + 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, 0x3C00, + 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, 0xFA01, 0x3AC0, + 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840, 0x2800, 0xE8C1, 0xE981, + 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, 0xEE01, 0x2EC0, 0x2F80, 0xEF41, + 0x2D00, 0xEDC1, 0xEC81, 0x2C40, 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, + 0xE7C1, 0xE681, 0x2640, 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, + 0x2080, 0xE041, 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, + 0x6240, 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441, + 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, 0xAA01, + 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, 0x7800, 0xB8C1, + 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, 0xBE01, 0x7EC0, 0x7F80, + 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40, 0xB401, 0x74C0, 0x7580, 0xB541, + 0x7700, 0xB7C1, 0xB681, 0x7640, 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, + 0x71C0, 0x7080, 0xB041, 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, + 0x5280, 0x9241, 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, + 0x5440, 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, + 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841, 0x8801, + 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, 0x4E00, 0x8EC1, + 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41, 0x4400, 0x84C1, 0x8581, + 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341, + 0x4100, 0x81C1, 0x8081, 0x4040}; #elif (defined(CRC16_USING_CONST_TABLE) && (CRC16_POLY == 0x8408)) static const u16 crc16_table[] = { - 0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF, - 0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7, - 0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E, - 0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876, - 0x2102, 0x308B, 0x0210, 0x1399, 0x6726, 0x76AF, 0x4434, 0x55BD, - 0xAD4A, 0xBCC3, 0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5, - 0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E, 0x54B5, 0x453C, - 0xBDCB, 0xAC42, 0x9ED9, 0x8F50, 0xFBEF, 0xEA66, 0xD8FD, 0xC974, - 0x4204, 0x538D, 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB, - 0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1, 0xAB7A, 0xBAF3, - 0x5285, 0x430C, 0x7197, 0x601E, 0x14A1, 0x0528, 0x37B3, 0x263A, - 0xDECD, 0xCF44, 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72, - 0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB, 0x0630, 0x17B9, - 0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5, 0xA96A, 0xB8E3, 0x8A78, 0x9BF1, - 0x7387, 0x620E, 0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738, - 0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862, 0x9AF9, 0x8B70, - 0x8408, 0x9581, 0xA71A, 0xB693, 0xC22C, 0xD3A5, 0xE13E, 0xF0B7, - 0x0840, 0x19C9, 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF, - 0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324, 0xF1BF, 0xE036, - 0x18C1, 0x0948, 0x3BD3, 0x2A5A, 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E, - 0xA50A, 0xB483, 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5, - 0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF, 0x4C74, 0x5DFD, - 0xB58B, 0xA402, 0x9699, 0x8710, 0xF3AF, 0xE226, 0xD0BD, 0xC134, - 0x39C3, 0x284A, 0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C, - 0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1, 0xA33A, 0xB2B3, - 0x4A44, 0x5BCD, 0x6956, 0x78DF, 0x0C60, 0x1DE9, 0x2F72, 0x3EFB, - 0xD68D, 0xC704, 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232, - 0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68, 0x3FF3, 0x2E7A, - 0xE70E, 0xF687, 0xC41C, 0xD595, 0xA12A, 0xB0A3, 0x8238, 0x93B1, - 0x6B46, 0x7ACF, 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9, - 0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330, - 0x7BC7, 0x6A4E, 0x58D5, 0x495C, 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78 -}; + 0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF, 0x8C48, + 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7, 0x1081, 0x0108, + 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E, 0x9CC9, 0x8D40, 0xBFDB, + 0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876, 0x2102, 0x308B, 0x0210, 0x1399, + 0x6726, 0x76AF, 0x4434, 0x55BD, 0xAD4A, 0xBCC3, 0x8E58, 0x9FD1, 0xEB6E, + 0xFAE7, 0xC87C, 0xD9F5, 0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E, + 0x54B5, 0x453C, 0xBDCB, 0xAC42, 0x9ED9, 0x8F50, 0xFBEF, 0xEA66, 0xD8FD, + 0xC974, 0x4204, 0x538D, 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB, + 0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1, 0xAB7A, 0xBAF3, 0x5285, + 0x430C, 0x7197, 0x601E, 0x14A1, 0x0528, 0x37B3, 0x263A, 0xDECD, 0xCF44, + 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72, 0x6306, 0x728F, 0x4014, + 0x519D, 0x2522, 0x34AB, 0x0630, 0x17B9, 0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5, + 0xA96A, 0xB8E3, 0x8A78, 0x9BF1, 0x7387, 0x620E, 0x5095, 0x411C, 0x35A3, + 0x242A, 0x16B1, 0x0738, 0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862, + 0x9AF9, 0x8B70, 0x8408, 0x9581, 0xA71A, 0xB693, 0xC22C, 0xD3A5, 0xE13E, + 0xF0B7, 0x0840, 0x19C9, 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF, + 0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324, 0xF1BF, 0xE036, 0x18C1, + 0x0948, 0x3BD3, 0x2A5A, 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E, 0xA50A, 0xB483, + 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5, 0x2942, 0x38CB, 0x0A50, + 0x1BD9, 0x6F66, 0x7EEF, 0x4C74, 0x5DFD, 0xB58B, 0xA402, 0x9699, 0x8710, + 0xF3AF, 0xE226, 0xD0BD, 0xC134, 0x39C3, 0x284A, 0x1AD1, 0x0B58, 0x7FE7, + 0x6E6E, 0x5CF5, 0x4D7C, 0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1, + 0xA33A, 0xB2B3, 0x4A44, 0x5BCD, 0x6956, 0x78DF, 0x0C60, 0x1DE9, 0x2F72, + 0x3EFB, 0xD68D, 0xC704, 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232, + 0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68, 0x3FF3, 0x2E7A, 0xE70E, + 0xF687, 0xC41C, 0xD595, 0xA12A, 0xB0A3, 0x8238, 0x93B1, 0x6B46, 0x7ACF, + 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9, 0xF78F, 0xE606, 0xD49D, + 0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330, 0x7BC7, 0x6A4E, 0x58D5, 0x495C, + 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78}; #else static u16 crc16_table[256]; -void crc16_table_init(void) -{ - u32 i, j; - u16 c; - u16 poly = CRC16_POLY; - - for (i = 0; i < 256; i++) - { - c = i; - - for (j = 0; j < 8; j++) - { - if(c & 0x0001) - c = poly ^ (c >> 1); - else - c >>= 1; - } - - crc16_table[i] = c; +void crc16_table_init(void) { + u32 i, j; + u16 c; + u16 poly = CRC16_POLY; + + for (i = 0; i < 256; i++) { + c = i; + + for (j = 0; j < 8; j++) { + if (c & 0x0001) + c = poly ^ (c >> 1); + else + c >>= 1; } + + crc16_table[i] = c; + } } INIT_BOARD_EXPORT(crc16_table_init); #endif -u16 crc16_cyc_cal(u16 init_val, u8 *pdata, u32 len) -{ - register u32 i; - register u16 crc16; - register u8 idx; - - crc16 = init_val; - for(i=0; i>8) ^ crc16_table[idx]; - } - - return(crc16); +uint16_t crc16_cyc_cal(uint16_t init_val, uint8_t *pdata, uint32_t len) { + register uint32_t i; + register uint16_t crc16; + register uint8_t idx; + + crc16 = init_val; + for (i = 0; i < len; i++) { + idx = ((uint8_t)crc16) ^ (*pdata++); + crc16 = (crc16 >> 8) ^ crc16_table[idx]; + } + + return (crc16); } -u16 crc16_cal(u8 *pdata,u32 len) -{ - return(crc16_cyc_cal(CRC16_INIT_VAL, pdata, len) ^ CRC16_INIT_VAL); +uint16_t crc16_cal(uint8_t *pdata, uint32_t len) { + return (crc16_cyc_cal(CRC16_INIT_VAL, pdata, len) ^ CRC16_INIT_VAL); } #endif - diff --git a/src/crc16_ex.c b/src/crc16_ex.c index b850246..54d352d 100644 --- a/src/crc16_ex.c +++ b/src/crc16_ex.c @@ -5,51 +5,45 @@ * Date Author Notes * 2021-03-12 qiyongzhong first version */ - + #include "crc16_ex.h" #ifdef CRCLIB_USING_CRC16_EX -void crc16_ex_init(crc16_inst_t *hinst, u16 poly) -{ - u32 i, j; - u16 c; - - for (i = 0; i < 256; i++) - { - c = i; +void crc16_ex_init(crc16_inst_t *hinst, u16 poly) { + u32 i, j; + u16 c; - for (j = 0; j < 8; j++) - { - if(c & 0x0001) - c = poly ^ (c >> 1); - else - c >>= 1; - } + for (i = 0; i < 256; i++) { + c = i; - hinst->table[i] = c; + for (j = 0; j < 8; j++) { + if (c & 0x0001) + c = poly ^ (c >> 1); + else + c >>= 1; } + + hinst->table[i] = c; + } } -u16 crc16_ex_cyc_cal(crc16_inst_t *hinst, u16 init_val, u8 *pdata, u32 len) -{ - register u32 i; - register u16 crc16; - register u8 idx; - - crc16 = init_val; - for(i=0; i>8) ^ hinst->table[idx]; - } +u16 crc16_ex_cyc_cal(crc16_inst_t *hinst, u16 init_val, u8 *pdata, u32 len) { + register u32 i; + register u16 crc16; + register u8 idx; + + crc16 = init_val; + for (i = 0; i < len; i++) { + idx = ((u8)crc16) ^ (*pdata++); + crc16 = (crc16 >> 8) ^ hinst->table[idx]; + } - return(crc16); + return (crc16); } -u16 crc16_ex_cal(crc16_inst_t *hinst, u8 *pdata, u32 len) -{ - return(crc16_ex_cyc_cal(hinst, CRC16_EX_INIT_VAL, pdata, len) ^ CRC16_EX_INIT_VAL); +u16 crc16_ex_cal(crc16_inst_t *hinst, u8 *pdata, u32 len) { + return (crc16_ex_cyc_cal(hinst, CRC16_EX_INIT_VAL, pdata, len) ^ + CRC16_EX_INIT_VAL); } #endif - diff --git a/src/crc16_ex_sample.c b/src/crc16_ex_sample.c index d182f3b..868400b 100644 --- a/src/crc16_ex_sample.c +++ b/src/crc16_ex_sample.c @@ -6,42 +6,38 @@ * 2020-03-15 qiyongzhong first version */ -#include #include +#include #ifdef CRCLIB_USING_CRC16_EX #ifdef CRCLIB_USING_CRC16_EX_SAMPLE crc16_inst_t crc16_ex_sample_inst; -static int crc16_ex_sample_init(void) -{ - crc16_ex_init(&crc16_ex_sample_inst, CRC16_EX_SAMPLE_POLY); - return(0); +static int crc16_ex_sample_init(void) { + crc16_ex_init(&crc16_ex_sample_inst, CRC16_EX_SAMPLE_POLY); + return (0); } INIT_BOARD_EXPORT(crc16_ex_sample_init); #ifdef CRCLIB_USING_CRC16 #if (CRC16_EX_SAMPLE_POLY == CRC16_POLY) -static void crc16_ex_test(void) -{ - u8 data[128]; - - for (int i=0; i> 1); - else - c >>= 1; - } - - crc32_table[i] = c; +void crc32_table_init(void) { + u32 i, j; + u32 c; + u32 poly = CRC32_POLY; + + for (i = 0; i < 256; i++) { + c = i; + + for (j = 0; j < 8; j++) { + if (c & 0x00000001) + c = poly ^ (c >> 1); + else + c >>= 1; } + + crc32_table[i] = c; + } } INIT_BOARD_EXPORT(crc32_table_init); #endif -u32 crc32_cyc_cal(u32 init_val, u8 *pdata, u32 len) -{ - register u32 i; - register u32 crc32; - register u8 idx; - - crc32 = init_val; - for(i=0; i>8) ^ crc32_table[idx]; - } - - return(crc32); +uint32_t crc32_cyc_cal(uint32_t init_val, uint8_t *pdata, uint32_t len) { + register uint32_t i; + register uint32_t crc32; + register uint8_t idx; + + crc32 = init_val; + for (i = 0; i < len; i++) { + idx = ((uint8_t)crc32) ^ (*pdata++); + crc32 = (crc32 >> 8) ^ crc32_table[idx]; + } + + return (crc32); } -u32 crc32_cal(u8 *pdata, u32 len) -{ - return(crc32_cyc_cal(CRC32_INIT_VAL, pdata, len) ^ CRC32_INIT_VAL); +uint32_t crc32_cal(uint8_t *pdata, uint32_t len) { + return (crc32_cyc_cal(CRC32_INIT_VAL, pdata, len) ^ CRC32_INIT_VAL); } #endif - diff --git a/src/crc32_ex.c b/src/crc32_ex.c index 37779e3..a91baf6 100644 --- a/src/crc32_ex.c +++ b/src/crc32_ex.c @@ -5,51 +5,45 @@ * Date Author Notes * 2021-03-12 qiyongzhong first version */ - + #include "crc32_ex.h" #ifdef CRCLIB_USING_CRC32_EX -void crc32_ex_init(crc32_inst_t *hinst, u32 poly) -{ - u32 i, j; - u32 c; - - for (i = 0; i < 256; i++) - { - c = i; +void crc32_ex_init(crc32_inst_t *hinst, u32 poly) { + u32 i, j; + u32 c; - for (j = 0; j < 8; j++) - { - if(c & 0x00000001) - c = poly ^ (c >> 1); - else - c >>= 1; - } + for (i = 0; i < 256; i++) { + c = i; - hinst->table[i] = c; + for (j = 0; j < 8; j++) { + if (c & 0x00000001) + c = poly ^ (c >> 1); + else + c >>= 1; } + + hinst->table[i] = c; + } } -u32 crc32_ex_cyc_cal(crc32_inst_t *hinst, u32 init_val, u8 *pdata, u32 len) -{ - register u32 i; - register u32 crc32; - register u8 idx; - - crc32 = init_val; - for(i=0; i>8) ^ hinst->table[idx]; - } +u32 crc32_ex_cyc_cal(crc32_inst_t *hinst, u32 init_val, u8 *pdata, u32 len) { + register u32 i; + register u32 crc32; + register u8 idx; + + crc32 = init_val; + for (i = 0; i < len; i++) { + idx = ((u8)crc32) ^ (*pdata++); + crc32 = (crc32 >> 8) ^ hinst->table[idx]; + } - return(crc32); + return (crc32); } -u32 crc32_ex_cal(crc32_inst_t *hinst, u8 *pdata, u32 len) -{ - return(crc32_ex_cyc_cal(hinst, CRC32_EX_INIT_VAL, pdata, len) ^ CRC32_EX_INIT_VAL); +u32 crc32_ex_cal(crc32_inst_t *hinst, u8 *pdata, u32 len) { + return (crc32_ex_cyc_cal(hinst, CRC32_EX_INIT_VAL, pdata, len) ^ + CRC32_EX_INIT_VAL); } #endif - diff --git a/src/crc8.c b/src/crc8.c index 12ec8ff..6c731ec 100644 --- a/src/crc8.c +++ b/src/crc8.c @@ -11,121 +11,92 @@ #ifdef CRCLIB_USING_CRC8 #if (defined(CRC8_USING_CONST_TABLE) && (CRC8_POLY == 0x8C)) -static const u8 crc8_table[] = { - 0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83, - 0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41, - 0x9D, 0xC3, 0x21, 0x7F, 0xFC, 0xA2, 0x40, 0x1E, - 0x5F, 0x01, 0xE3, 0xBD, 0x3E, 0x60, 0x82, 0xDC, - 0x23, 0x7D, 0x9F, 0xC1, 0x42, 0x1C, 0xFE, 0xA0, - 0xE1, 0xBF, 0x5D, 0x03, 0x80, 0xDE, 0x3C, 0x62, - 0xBE, 0xE0, 0x02, 0x5C, 0xDF, 0x81, 0x63, 0x3D, - 0x7C, 0x22, 0xC0, 0x9E, 0x1D, 0x43, 0xA1, 0xFF, - 0x46, 0x18, 0xFA, 0xA4, 0x27, 0x79, 0x9B, 0xC5, - 0x84, 0xDA, 0x38, 0x66, 0xE5, 0xBB, 0x59, 0x07, - 0xDB, 0x85, 0x67, 0x39, 0xBA, 0xE4, 0x06, 0x58, - 0x19, 0x47, 0xA5, 0xFB, 0x78, 0x26, 0xC4, 0x9A, - 0x65, 0x3B, 0xD9, 0x87, 0x04, 0x5A, 0xB8, 0xE6, - 0xA7, 0xF9, 0x1B, 0x45, 0xC6, 0x98, 0x7A, 0x24, - 0xF8, 0xA6, 0x44, 0x1A, 0x99, 0xC7, 0x25, 0x7B, - 0x3A, 0x64, 0x86, 0xD8, 0x5B, 0x05, 0xE7, 0xB9, - 0x8C, 0xD2, 0x30, 0x6E, 0xED, 0xB3, 0x51, 0x0F, - 0x4E, 0x10, 0xF2, 0xAC, 0x2F, 0x71, 0x93, 0xCD, - 0x11, 0x4F, 0xAD, 0xF3, 0x70, 0x2E, 0xCC, 0x92, - 0xD3, 0x8D, 0x6F, 0x31, 0xB2, 0xEC, 0x0E, 0x50, - 0xAF, 0xF1, 0x13, 0x4D, 0xCE, 0x90, 0x72, 0x2C, - 0x6D, 0x33, 0xD1, 0x8F, 0x0C, 0x52, 0xB0, 0xEE, - 0x32, 0x6C, 0x8E, 0xD0, 0x53, 0x0D, 0xEF, 0xB1, - 0xF0, 0xAE, 0x4C, 0x12, 0x91, 0xCF, 0x2D, 0x73, - 0xCA, 0x94, 0x76, 0x28, 0xAB, 0xF5, 0x17, 0x49, - 0x08, 0x56, 0xB4, 0xEA, 0x69, 0x37, 0xD5, 0x8B, - 0x57, 0x09, 0xEB, 0xB5, 0x36, 0x68, 0x8A, 0xD4, - 0x95, 0xCB, 0x29, 0x77, 0xF4, 0xAA, 0x48, 0x16, - 0xE9, 0xB7, 0x55, 0x0B, 0x88, 0xD6, 0x34, 0x6A, - 0x2B, 0x75, 0x97, 0xC9, 0x4A, 0x14, 0xF6, 0xA8, - 0x74, 0x2A, 0xC8, 0x96, 0x15, 0x4B, 0xA9, 0xF7, - 0xB6, 0xE8, 0x0A, 0x54, 0xD7, 0x89, 0x6B, 0x35 -}; +static const uint8_t crc8_table[] = { + 0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83, 0xC2, 0x9C, 0x7E, 0x20, + 0xA3, 0xFD, 0x1F, 0x41, 0x9D, 0xC3, 0x21, 0x7F, 0xFC, 0xA2, 0x40, 0x1E, + 0x5F, 0x01, 0xE3, 0xBD, 0x3E, 0x60, 0x82, 0xDC, 0x23, 0x7D, 0x9F, 0xC1, + 0x42, 0x1C, 0xFE, 0xA0, 0xE1, 0xBF, 0x5D, 0x03, 0x80, 0xDE, 0x3C, 0x62, + 0xBE, 0xE0, 0x02, 0x5C, 0xDF, 0x81, 0x63, 0x3D, 0x7C, 0x22, 0xC0, 0x9E, + 0x1D, 0x43, 0xA1, 0xFF, 0x46, 0x18, 0xFA, 0xA4, 0x27, 0x79, 0x9B, 0xC5, + 0x84, 0xDA, 0x38, 0x66, 0xE5, 0xBB, 0x59, 0x07, 0xDB, 0x85, 0x67, 0x39, + 0xBA, 0xE4, 0x06, 0x58, 0x19, 0x47, 0xA5, 0xFB, 0x78, 0x26, 0xC4, 0x9A, + 0x65, 0x3B, 0xD9, 0x87, 0x04, 0x5A, 0xB8, 0xE6, 0xA7, 0xF9, 0x1B, 0x45, + 0xC6, 0x98, 0x7A, 0x24, 0xF8, 0xA6, 0x44, 0x1A, 0x99, 0xC7, 0x25, 0x7B, + 0x3A, 0x64, 0x86, 0xD8, 0x5B, 0x05, 0xE7, 0xB9, 0x8C, 0xD2, 0x30, 0x6E, + 0xED, 0xB3, 0x51, 0x0F, 0x4E, 0x10, 0xF2, 0xAC, 0x2F, 0x71, 0x93, 0xCD, + 0x11, 0x4F, 0xAD, 0xF3, 0x70, 0x2E, 0xCC, 0x92, 0xD3, 0x8D, 0x6F, 0x31, + 0xB2, 0xEC, 0x0E, 0x50, 0xAF, 0xF1, 0x13, 0x4D, 0xCE, 0x90, 0x72, 0x2C, + 0x6D, 0x33, 0xD1, 0x8F, 0x0C, 0x52, 0xB0, 0xEE, 0x32, 0x6C, 0x8E, 0xD0, + 0x53, 0x0D, 0xEF, 0xB1, 0xF0, 0xAE, 0x4C, 0x12, 0x91, 0xCF, 0x2D, 0x73, + 0xCA, 0x94, 0x76, 0x28, 0xAB, 0xF5, 0x17, 0x49, 0x08, 0x56, 0xB4, 0xEA, + 0x69, 0x37, 0xD5, 0x8B, 0x57, 0x09, 0xEB, 0xB5, 0x36, 0x68, 0x8A, 0xD4, + 0x95, 0xCB, 0x29, 0x77, 0xF4, 0xAA, 0x48, 0x16, 0xE9, 0xB7, 0x55, 0x0B, + 0x88, 0xD6, 0x34, 0x6A, 0x2B, 0x75, 0x97, 0xC9, 0x4A, 0x14, 0xF6, 0xA8, + 0x74, 0x2A, 0xC8, 0x96, 0x15, 0x4B, 0xA9, 0xF7, 0xB6, 0xE8, 0x0A, 0x54, + 0xD7, 0x89, 0x6B, 0x35}; #elif (defined(CRC8_USING_CONST_TABLE) && (CRC8_POLY == 0xD9)) static const u8 crc8_table[] = { - 0x00, 0xD0, 0x13, 0xC3, 0x26, 0xF6, 0x35, 0xE5, - 0x4C, 0x9C, 0x5F, 0x8F, 0x6A, 0xBA, 0x79, 0xA9, - 0x98, 0x48, 0x8B, 0x5B, 0xBE, 0x6E, 0xAD, 0x7D, - 0xD4, 0x04, 0xC7, 0x17, 0xF2, 0x22, 0xE1, 0x31, - 0x83, 0x53, 0x90, 0x40, 0xA5, 0x75, 0xB6, 0x66, - 0xCF, 0x1F, 0xDC, 0x0C, 0xE9, 0x39, 0xFA, 0x2A, - 0x1B, 0xCB, 0x08, 0xD8, 0x3D, 0xED, 0x2E, 0xFE, - 0x57, 0x87, 0x44, 0x94, 0x71, 0xA1, 0x62, 0xB2, - 0xB5, 0x65, 0xA6, 0x76, 0x93, 0x43, 0x80, 0x50, - 0xF9, 0x29, 0xEA, 0x3A, 0xDF, 0x0F, 0xCC, 0x1C, - 0x2D, 0xFD, 0x3E, 0xEE, 0x0B, 0xDB, 0x18, 0xC8, - 0x61, 0xB1, 0x72, 0xA2, 0x47, 0x97, 0x54, 0x84, - 0x36, 0xE6, 0x25, 0xF5, 0x10, 0xC0, 0x03, 0xD3, - 0x7A, 0xAA, 0x69, 0xB9, 0x5C, 0x8C, 0x4F, 0x9F, - 0xAE, 0x7E, 0xBD, 0x6D, 0x88, 0x58, 0x9B, 0x4B, - 0xE2, 0x32, 0xF1, 0x21, 0xC4, 0x14, 0xD7, 0x07, - 0xD9, 0x09, 0xCA, 0x1A, 0xFF, 0x2F, 0xEC, 0x3C, - 0x95, 0x45, 0x86, 0x56, 0xB3, 0x63, 0xA0, 0x70, - 0x41, 0x91, 0x52, 0x82, 0x67, 0xB7, 0x74, 0xA4, - 0x0D, 0xDD, 0x1E, 0xCE, 0x2B, 0xFB, 0x38, 0xE8, - 0x5A, 0x8A, 0x49, 0x99, 0x7C, 0xAC, 0x6F, 0xBF, - 0x16, 0xC6, 0x05, 0xD5, 0x30, 0xE0, 0x23, 0xF3, - 0xC2, 0x12, 0xD1, 0x01, 0xE4, 0x34, 0xF7, 0x27, - 0x8E, 0x5E, 0x9D, 0x4D, 0xA8, 0x78, 0xBB, 0x6B, - 0x6C, 0xBC, 0x7F, 0xAF, 0x4A, 0x9A, 0x59, 0x89, - 0x20, 0xF0, 0x33, 0xE3, 0x06, 0xD6, 0x15, 0xC5, - 0xF4, 0x24, 0xE7, 0x37, 0xD2, 0x02, 0xC1, 0x11, - 0xB8, 0x68, 0xAB, 0x7B, 0x9E, 0x4E, 0x8D, 0x5D, - 0xEF, 0x3F, 0xFC, 0x2C, 0xC9, 0x19, 0xDA, 0x0A, - 0xA3, 0x73, 0xB0, 0x60, 0x85, 0x55, 0x96, 0x46, - 0x77, 0xA7, 0x64, 0xB4, 0x51, 0x81, 0x42, 0x92, - 0x3B, 0xEB, 0x28, 0xF8, 0x1D, 0xCD, 0x0E, 0xDE -}; + 0x00, 0xD0, 0x13, 0xC3, 0x26, 0xF6, 0x35, 0xE5, 0x4C, 0x9C, 0x5F, 0x8F, + 0x6A, 0xBA, 0x79, 0xA9, 0x98, 0x48, 0x8B, 0x5B, 0xBE, 0x6E, 0xAD, 0x7D, + 0xD4, 0x04, 0xC7, 0x17, 0xF2, 0x22, 0xE1, 0x31, 0x83, 0x53, 0x90, 0x40, + 0xA5, 0x75, 0xB6, 0x66, 0xCF, 0x1F, 0xDC, 0x0C, 0xE9, 0x39, 0xFA, 0x2A, + 0x1B, 0xCB, 0x08, 0xD8, 0x3D, 0xED, 0x2E, 0xFE, 0x57, 0x87, 0x44, 0x94, + 0x71, 0xA1, 0x62, 0xB2, 0xB5, 0x65, 0xA6, 0x76, 0x93, 0x43, 0x80, 0x50, + 0xF9, 0x29, 0xEA, 0x3A, 0xDF, 0x0F, 0xCC, 0x1C, 0x2D, 0xFD, 0x3E, 0xEE, + 0x0B, 0xDB, 0x18, 0xC8, 0x61, 0xB1, 0x72, 0xA2, 0x47, 0x97, 0x54, 0x84, + 0x36, 0xE6, 0x25, 0xF5, 0x10, 0xC0, 0x03, 0xD3, 0x7A, 0xAA, 0x69, 0xB9, + 0x5C, 0x8C, 0x4F, 0x9F, 0xAE, 0x7E, 0xBD, 0x6D, 0x88, 0x58, 0x9B, 0x4B, + 0xE2, 0x32, 0xF1, 0x21, 0xC4, 0x14, 0xD7, 0x07, 0xD9, 0x09, 0xCA, 0x1A, + 0xFF, 0x2F, 0xEC, 0x3C, 0x95, 0x45, 0x86, 0x56, 0xB3, 0x63, 0xA0, 0x70, + 0x41, 0x91, 0x52, 0x82, 0x67, 0xB7, 0x74, 0xA4, 0x0D, 0xDD, 0x1E, 0xCE, + 0x2B, 0xFB, 0x38, 0xE8, 0x5A, 0x8A, 0x49, 0x99, 0x7C, 0xAC, 0x6F, 0xBF, + 0x16, 0xC6, 0x05, 0xD5, 0x30, 0xE0, 0x23, 0xF3, 0xC2, 0x12, 0xD1, 0x01, + 0xE4, 0x34, 0xF7, 0x27, 0x8E, 0x5E, 0x9D, 0x4D, 0xA8, 0x78, 0xBB, 0x6B, + 0x6C, 0xBC, 0x7F, 0xAF, 0x4A, 0x9A, 0x59, 0x89, 0x20, 0xF0, 0x33, 0xE3, + 0x06, 0xD6, 0x15, 0xC5, 0xF4, 0x24, 0xE7, 0x37, 0xD2, 0x02, 0xC1, 0x11, + 0xB8, 0x68, 0xAB, 0x7B, 0x9E, 0x4E, 0x8D, 0x5D, 0xEF, 0x3F, 0xFC, 0x2C, + 0xC9, 0x19, 0xDA, 0x0A, 0xA3, 0x73, 0xB0, 0x60, 0x85, 0x55, 0x96, 0x46, + 0x77, 0xA7, 0x64, 0xB4, 0x51, 0x81, 0x42, 0x92, 0x3B, 0xEB, 0x28, 0xF8, + 0x1D, 0xCD, 0x0E, 0xDE}; #else static u8 crc8_table[256]; -void crc8_table_init(void) -{ - u32 i, j; - u8 c; - u8 poly = CRC8_POLY; - - for (i = 0; i < 256; i++) - { - c = i; - - for (j = 0; j < 8; j++) - { - if(c & 0x01) - c = poly ^ (c >> 1); - else - c >>= 1; - } - - crc8_table[i] = c; +void crc8_table_init(void) { + u32 i, j; + u8 c; + u8 poly = CRC8_POLY; + + for (i = 0; i < 256; i++) { + c = i; + + for (j = 0; j < 8; j++) { + if (c & 0x01) + c = poly ^ (c >> 1); + else + c >>= 1; } + + crc8_table[i] = c; + } } INIT_BOARD_EXPORT(crc8_table_init); #endif -u8 crc8_cyc_cal(u8 init_val, u8 *pdata, u32 len) -{ - register u32 i; - register u8 crc8; - register u8 idx; - - crc8 = init_val; - for(i=0; i> 1); - else - c >>= 1; - } + for (i = 0; i < 256; i++) { + c = i; - hinst->table[i] = c; + for (j = 0; j < 8; j++) { + if (c & 0x01) + c = poly ^ (c >> 1); + else + c >>= 1; } + + hinst->table[i] = c; + } } -u8 crc8_ex_cyc_cal(crc8_inst_t *hinst, u8 init_val, u8 *pdata, u32 len) -{ - register u32 i; - register u8 crc8; - register u8 idx; - - crc8 = init_val; - for(i=0; i>8) ^ hinst->table[idx]; - } +u8 crc8_ex_cyc_cal(crc8_inst_t *hinst, u8 init_val, u8 *pdata, u32 len) { + register u32 i; + register u8 crc8; + register u8 idx; + + crc8 = init_val; + for (i = 0; i < len; i++) { + idx = ((u8)crc8) ^ (*pdata++); + crc8 = (crc8 >> 8) ^ hinst->table[idx]; + } - return(crc8); + return (crc8); } -u8 crc8_ex_cal(crc8_inst_t *hinst, u8 *pdata, u32 len) -{ - return(crc8_ex_cyc_cal(hinst, CRC8_EX_INIT_VAL, pdata, len) ^ CRC8_EX_INIT_VAL); +u8 crc8_ex_cal(crc8_inst_t *hinst, u8 *pdata, u32 len) { + return (crc8_ex_cyc_cal(hinst, CRC8_EX_INIT_VAL, pdata, len) ^ + CRC8_EX_INIT_VAL); } #endif - diff --git a/src/crc_hw_hc32.c b/src/crc_hw_hc32.c index d7b8077..946b2f2 100644 --- a/src/crc_hw_hc32.c +++ b/src/crc_hw_hc32.c @@ -22,48 +22,48 @@ #if defined(CHIP_NAME_HC32F460) static bool crc_is_busy = false; -void crc_hw_init(crc_hw_inst_t *hinst, u32 poly) -{ - if (poly == 0x8408)//CRC16 only support poly = x16+x12+x5+1 (CCITT,ISO,HDLC,ITUX25,PPP-FCS) - { - hinst->size = (0<<1); - hinst->poly = poly; - return; - } - if (poly == 0xEDB88320)//CRC32 only support poly = x32+x26+x23+...+x2+x+1 (ZIP,RAR,IEEE,LAN/FDDI,PPP-FCS) - { - hinst->size = (1<<1); - hinst->poly = poly; - return; - } - LOG_E("The polynomial is not support. The polynomial must is 0x8408 or 0xEDB88320"); +void crc_hw_init(crc_hw_inst_t *hinst, u32 poly) { + if (poly == 0x8408) // CRC16 only support poly = x16+x12+x5+1 + // (CCITT,ISO,HDLC,ITUX25,PPP-FCS) + { + hinst->size = (0 << 1); + hinst->poly = poly; + return; + } + if (poly == 0xEDB88320) // CRC32 only support poly = x32+x26+x23+...+x2+x+1 + // (ZIP,RAR,IEEE,LAN/FDDI,PPP-FCS) + { + hinst->size = (1 << 1); + hinst->poly = poly; + return; + } + LOG_E("The polynomial is not support. The polynomial must is 0x8408 or " + "0xEDB88320"); } -u32 crc_hw_cyc_cal(crc_hw_inst_t *hinst, u32 init_val, u8 *pdata, u32 len) -{ - while(crc_is_busy)CRC_RTOS_DELAY(1); - crc_is_busy = true; +u32 crc_hw_cyc_cal(crc_hw_inst_t *hinst, u32 init_val, u8 *pdata, u32 len) { + while (crc_is_busy) + CRC_RTOS_DELAY(1); + crc_is_busy = true; - PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_CRC, Enable); + PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_CRC, Enable); - M4_CRC->CR = (hinst->size | (1<<2) | (1<<3) | (0<<4)); - M4_CRC->RESLT = init_val; - - for(int i=0; iDAT0)) = pdata[i]; - } - u32 rst = M4_CRC->RESLT; + M4_CRC->CR = (hinst->size | (1 << 2) | (1 << 3) | (0 << 4)); + M4_CRC->RESLT = init_val; - PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_CRC, Disable); - crc_is_busy = false; - - return(rst); + for (int i = 0; i < len; i++) { + *((u8 *)&(M4_CRC->DAT0)) = pdata[i]; + } + u32 rst = M4_CRC->RESLT; + + PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_CRC, Disable); + crc_is_busy = false; + + return (rst); } -u32 crc_hw_cal(crc_hw_inst_t *hinst, u8 *pdata, u32 len) -{ - return(crc_hw_cyc_cal(hinst, CRC_HW_INIT_VAL, pdata, len) ^ CRC_HW_INIT_VAL); +u32 crc_hw_cal(crc_hw_inst_t *hinst, u8 *pdata, u32 len) { + return (crc_hw_cyc_cal(hinst, CRC_HW_INIT_VAL, pdata, len) ^ CRC_HW_INIT_VAL); } #endif diff --git a/src/crc_hw_n32.c b/src/crc_hw_n32.c index 320a60b..a237f13 100644 --- a/src/crc_hw_n32.c +++ b/src/crc_hw_n32.c @@ -15,86 +15,82 @@ #define DBG_LVL DBG_INFO #include #include -#if (defined(CHIP_SERIES_N32G452) || defined(CHIP_SERIES_N32G455) || defined(CHIP_SERIES_N32G457)) +#if (defined(CHIP_SERIES_N32G452) || defined(CHIP_SERIES_N32G455) || \ + defined(CHIP_SERIES_N32G457)) #include #endif -#if (defined(CHIP_SERIES_N32G452) || defined(CHIP_SERIES_N32G455) || defined(CHIP_SERIES_N32G457)) +#if (defined(CHIP_SERIES_N32G452) || defined(CHIP_SERIES_N32G455) || \ + defined(CHIP_SERIES_N32G457)) static bool crc16_is_busy = false; static bool crc32_is_busy = false; -void crc_hw_init(crc_hw_inst_t *hinst, u32 poly) -{ - if (poly == 0xA001)//CRC16 only support poly = x16+x15+x2+1 (IBM,SDLC) - { - hinst->size = 16; - hinst->poly = poly; - return; - } - if (poly == 0xEDB88320)//CRC32 only support poly = x32+x26+x23+...+x2+x+1 (ZIP,RAR,IEEE,LAN/FDDI,PPP-FCS) - { - hinst->size = 32; - hinst->poly = poly; - return; - } - LOG_E("The polynomial is not support. The polynomial must is 0xA001 or 0xEDB88320"); +void crc_hw_init(crc_hw_inst_t *hinst, u32 poly) { + if (poly == 0xA001) // CRC16 only support poly = x16+x15+x2+1 (IBM,SDLC) + { + hinst->size = 16; + hinst->poly = poly; + return; + } + if (poly == 0xEDB88320) // CRC32 only support poly = x32+x26+x23+...+x2+x+1 + // (ZIP,RAR,IEEE,LAN/FDDI,PPP-FCS) + { + hinst->size = 32; + hinst->poly = poly; + return; + } + LOG_E("The polynomial is not support. The polynomial must is 0xA001 or " + "0xEDB88320"); } -u32 crc_hw_cyc_cal(crc_hw_inst_t *hinst, u32 init_val, u8 *pdata, u32 len) -{ - u32 rst; +u32 crc_hw_cyc_cal(crc_hw_inst_t *hinst, u32 init_val, u8 *pdata, u32 len) { + u32 rst; - RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_CRC, ENABLE); + RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_CRC, ENABLE); - if (hinst->size == 16) - { - while(crc16_is_busy)CRC_RTOS_DELAY(1); - crc16_is_busy = true; + if (hinst->size == 16) { + while (crc16_is_busy) + CRC_RTOS_DELAY(1); + crc16_is_busy = true; - CRC->CRC16CTRL = ((1<<1) | (1<<2)); - CRC->CRC16D = init_val; - for(int i=0; iCRC16DAT = pdata[i]; - } - rst = (__RBIT(CRC->CRC16D) >> 16); + CRC->CRC16CTRL = ((1 << 1) | (1 << 2)); + CRC->CRC16D = init_val; + for (int i = 0; i < len; i++) { + CRC->CRC16DAT = pdata[i]; + } + rst = (__RBIT(CRC->CRC16D) >> 16); - crc16_is_busy = false; + crc16_is_busy = false; + } else { + if ((len % 4) != 0) { + LOG_E("the length must be a multiple of 4."); + return (-1); } - else - { - if ((len % 4) != 0) - { - LOG_E("the length must be a multiple of 4."); - return(-1); - } - - while(crc32_is_busy)CRC_RTOS_DELAY(1); - crc32_is_busy = true; - - if (init_val == 0xFFFFFFFF) - { - CRC->CRC32CTRL = (1<<0); - } - u32 td32; - for(int i=0; iCRC32DAT = __RBIT(td32); - pdata += 4; - } - rst = __RBIT(CRC->CRC32DAT); - - crc32_is_busy = false; + + while (crc32_is_busy) + CRC_RTOS_DELAY(1); + crc32_is_busy = true; + + if (init_val == 0xFFFFFFFF) { + CRC->CRC32CTRL = (1 << 0); + } + u32 td32; + for (int i = 0; i < len / 4; i++) { + memcpy((u8 *)&td32, (u8 *)pdata, 4); + CRC->CRC32DAT = __RBIT(td32); + pdata += 4; } + rst = __RBIT(CRC->CRC32DAT); + + crc32_is_busy = false; + } - return(rst); + return (rst); } -u32 crc_hw_cal(crc_hw_inst_t *hinst, u8 *pdata, u32 len) -{ - return(crc_hw_cyc_cal(hinst, CRC_HW_INIT_VAL, pdata, len) ^ CRC_HW_INIT_VAL); +u32 crc_hw_cal(crc_hw_inst_t *hinst, u8 *pdata, u32 len) { + return (crc_hw_cyc_cal(hinst, CRC_HW_INIT_VAL, pdata, len) ^ CRC_HW_INIT_VAL); } #endif diff --git a/src/crc_hw_sample.c b/src/crc_hw_sample.c index e7b4388..86d35c5 100644 --- a/src/crc_hw_sample.c +++ b/src/crc_hw_sample.c @@ -6,42 +6,38 @@ * 2022-03-22 qiyongzhong first version */ -#include #include +#include #ifdef CRCLIB_USING_CRC_HW #ifdef CRCLIB_USING_CRC_HW_SAMPLE crc_hw_inst_t crc8_hw_sample_inst; -static int crc8_hw_sample_init(void) -{ - crc_hw_init(&crc8_hw_sample_inst, CRC8_HW_SAMPLE_POLY); - return(0); +static int crc8_hw_sample_init(void) { + crc_hw_init(&crc8_hw_sample_inst, CRC8_HW_SAMPLE_POLY); + return (0); } INIT_BOARD_EXPORT(crc8_hw_sample_init); #ifdef CRCLIB_USING_CRC8 #if (CRC8_HW_SAMPLE_POLY == CRC8_POLY) -static void crc8_hw_test(void) -{ - u8 data[128]; - - for (int i=0; isize = CRC_POLYLENGTH_8B; - hinst->poly = (__RBIT(poly) >> 24); - } - else if (poly <= 0xFFFF) - { - hinst->size = CRC_POLYLENGTH_16B; - hinst->poly = (__RBIT(poly) >> 16); - } - else - { - hinst->size = CRC_POLYLENGTH_32B; - hinst->poly = __RBIT(poly); - } +void crc_hw_init(crc_hw_inst_t *hinst, u32 poly) { + if (poly <= 0xFF) { + hinst->size = CRC_POLYLENGTH_8B; + hinst->poly = (__RBIT(poly) >> 24); + } else if (poly <= 0xFFFF) { + hinst->size = CRC_POLYLENGTH_16B; + hinst->poly = (__RBIT(poly) >> 16); + } else { + hinst->size = CRC_POLYLENGTH_32B; + hinst->poly = __RBIT(poly); + } } -u32 crc_hw_cyc_cal(crc_hw_inst_t *hinst, u32 init_val, u8 *pdata, u32 len) -{ - while(crc_is_busy)CRC_RTOS_DELAY(1); - crc_is_busy = true; - - __HAL_RCC_CRC_CLK_ENABLE(); +u32 crc_hw_cyc_cal(crc_hw_inst_t *hinst, u32 init_val, u8 *pdata, u32 len) { + while (crc_is_busy) + CRC_RTOS_DELAY(1); + crc_is_busy = true; - CRC->POL = hinst->poly; - CRC->INIT = init_val; - CRC->CR = ((1<<0) | (hinst->size) | (1<<5) | (1<<7)); + __HAL_RCC_CRC_CLK_ENABLE(); - for(int i=0; iDR)) = pdata[i]; - } - u32 rst = CRC->DR; - - __HAL_RCC_CRC_CLK_DISABLE(); - crc_is_busy = false; - - return(rst); + CRC->POL = hinst->poly; + CRC->INIT = init_val; + CRC->CR = ((1 << 0) | (hinst->size) | (1 << 5) | (1 << 7)); + + for (int i = 0; i < len; i++) { + *((u8 *)&(CRC->DR)) = pdata[i]; + } + u32 rst = CRC->DR; + + __HAL_RCC_CRC_CLK_DISABLE(); + crc_is_busy = false; + + return (rst); } -u32 crc_hw_cal(crc_hw_inst_t *hinst, u8 *pdata, u32 len) -{ - return(crc_hw_cyc_cal(hinst, CRC_HW_INIT_VAL, pdata, len) ^ CRC_HW_INIT_VAL); +u32 crc_hw_cal(crc_hw_inst_t *hinst, u8 *pdata, u32 len) { + return (crc_hw_cyc_cal(hinst, CRC_HW_INIT_VAL, pdata, len) ^ CRC_HW_INIT_VAL); } #endif