diff --git a/src/generic/crc16_ccitt.c b/src/generic/crc16_ccitt.c new file mode 100644 index 0000000..87d08ca --- /dev/null +++ b/src/generic/crc16_ccitt.c @@ -0,0 +1,22 @@ +// Code for crc16_ccitt +// +// Copyright (C) 2016 Kevin O'Connor +// +// This file may be distributed under the terms of the GNU GPLv3 license. + +#include "misc.h" // crc16_ccitt + +// Implement the standard crc "ccitt" algorithm on the given buffer +uint16_t +crc16_ccitt(uint8_t *buf, uint_fast8_t len) +{ + uint16_t crc = 0xffff; + while (len--) { + uint8_t data = *buf++; + data ^= crc & 0xff; + data ^= data << 4; + crc = ((((uint16_t)data << 8) | (crc >> 8)) ^ (uint8_t)(data >> 4) + ^ ((uint16_t)data << 3)); + } + return crc; +} diff --git a/src/generic/misc.h b/src/generic/misc.h index c56b403..a19df60 100644 --- a/src/generic/misc.h +++ b/src/generic/misc.h @@ -18,4 +18,6 @@ uint8_t timer_is_before(uint32_t time1, uint32_t time2); uint32_t timer_read_time(void); void udelay(uint32_t usecs); +uint16_t crc16_ccitt(uint8_t *buf, uint_fast8_t len); + #endif // misc.h diff --git a/src/stm32/Makefile b/src/stm32/Makefile index 159c02a..e66d725 100644 --- a/src/stm32/Makefile +++ b/src/stm32/Makefile @@ -25,7 +25,7 @@ $(OUT)canboot.elf: $(OUT)src/generic/armcm_link.ld # Add source files src-y += stm32/gpio.c stm32/flash.c stm32/clockline.c -src-y += generic/armcm_boot.c generic/armcm_irq.c +src-y += generic/armcm_boot.c generic/armcm_irq.c generic/crc16_ccitt.c src-$(CONFIG_MACH_STM32F0) += ../lib/stm32f0/system_stm32f0xx.c src-$(CONFIG_MACH_STM32F0) += stm32/stm32f0.c stm32/stm32f0_timer.c src-$(CONFIG_MACH_STM32F0) += stm32/gpioperiph.c