From b0f5c3e9b66c45e1c56c27cf10e78bfd0364aa0a Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Fri, 8 Jul 2011 00:10:53 +0200 Subject: [PATCH] add openbeacon-compatible crc (CCITT-16) --- firmware/Makefile | 3 ++- firmware/basic/basic.h | 4 ++++ firmware/basic/crc.c | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 firmware/basic/crc.c diff --git a/firmware/Makefile b/firmware/Makefile index 65dbcf0..af2f8ef 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -8,7 +8,7 @@ OBJS = main.o VPATH += OBJS += OBJS += basic/basic.o basic/reinvoke_isp.o basic/delayms.o basic/voltage.o -OBJS += basic/keyin.o basic/uuid.o +OBJS += basic/keyin.o basic/uuid.o basic/crc.o LIBS += core/libcore.a lcd/liblcd.a applications/libapp.a filesystem/libfat.a usb/libusb.a funk/libfunk.a ########################################################################## @@ -106,3 +106,4 @@ $(OUTFILE).elf: $(OBJS) $(SYS_OBJS) $(LIBS) $(LPCFIX) $(LD_TEMP) .PHONY: $(LD_TEMP) lcd/liblcd.a applications/libapp.a filesystem/libfat.a usb/libusb.a + diff --git a/firmware/basic/basic.h b/firmware/basic/basic.h index 4bd93ea..ba942af 100644 --- a/firmware/basic/basic.h +++ b/firmware/basic/basic.h @@ -152,4 +152,8 @@ uint16_t GetUUID16(void); // for core/iap/iap.c (no official definition) void iap_entry(uint32_t param_tab[], uint32_t result_tab[]); +// crc.c +uint16_t crc16(char * buf, int len); + + #endif diff --git a/firmware/basic/crc.c b/firmware/basic/crc.c new file mode 100644 index 0000000..9846888 --- /dev/null +++ b/firmware/basic/crc.c @@ -0,0 +1,24 @@ +#include "basic.h" + +// Calculate the CRC for transmitted and received data using +// the CCITT 16bit algorithm (X^16 + X^12 + X^5 + 1). + +uint16_t crc16(char * buf, int len){ + unsigned int crc=0xffff; + + for(int i=0;i> 8) | (crc << 8); + crc ^= buf[i]; + crc ^= (unsigned char)(crc & 0xff) >> 4; + crc ^= (crc << 8) << 4; + crc ^= ((crc & 0xff) << 4) << 1; + }; + return crc; +}; + +/* Note: + It is best not to alter this code. For example, (crc<<8)<<4 does + not generate the same code as crc<<12. Although the result of the + computation is the same, the latter generates much more code and + executes slower. + */