commit ec11499cb445ee3e99404b2396b502e47b9f2b8d Author: Jens Ahrensfeld Date: Sat Jul 19 07:44:42 2014 +0000 Initial import git-svn-id: http://moon:8086/svn/software/trunk/libsrc/crc@1 b431acfa-c32f-4a4a-93f1-934dc6c82436 diff --git a/crc.c b/crc.c new file mode 100755 index 0000000..f734e06 --- /dev/null +++ b/crc.c @@ -0,0 +1,46 @@ +/* CRC16.C */ + +/* Developed 1990-1997 by Rob Swindell; PO Box 501, Yorba Linda, CA 92885 */ + +/* 16-bit CRC routines */ + +#include + +/****************************************************************************/ +/* Updates 16-bit "rcrc" with character 'ch' */ +/****************************************************************************/ +uint16_t Upd_crc16(uint8_t ch, uint16_t poly, uint16_t crc) +{ + uint16_t i, cy; + uint8_t nch = ch; + + for (i=0; i<8; i++) + { + cy = crc & 0x8000; + crc <<= 1; + if (nch & 0x80) + crc |= 1; + + nch<<=1; + + if (cy) + crc ^= poly; + } + return crc; +} + +/****************************************************************************/ +/* Returns 16-crc of string (not counting terminating NULL) */ +/****************************************************************************/ +uint16_t Crc16(uint16_t poly, uint16_t crc, uint8_t *pBuf, uint32_t len) +{ + while(len) + { + crc = Upd_crc16(*pBuf, poly, crc); + pBuf++; + len--; + + } + + return crc; +} diff --git a/crc.h b/crc.h new file mode 100755 index 0000000..f2086d6 --- /dev/null +++ b/crc.h @@ -0,0 +1,19 @@ +// -------------------------------------------------------------- +#ifndef CRC_H +#define CRC_H + +#include +// -------------------------------------------------------------- +#ifdef __cplusplus +extern "C" { +#endif + +uint16_t Upd_crc16(uint8_t ch, uint16_t poly, uint16_t crc); +uint16_t Crc16(uint16_t poly, uint16_t crc, uint8_t *pBuf, uint32_t len); + +#ifdef __cplusplus +} +#endif // __cplusplus + +// -------------------------------------------------------------- +#endif // CRC_H