diff --git a/Makefile b/Makefile index 160251c3..5fbf908f 100644 --- a/Makefile +++ b/Makefile @@ -26,13 +26,13 @@ PYTHON=python # Source files src-y=sched.c command.c stepper.c basecmd.c gpiocmds.c spicmds.c endstop.c -DIRS=src src/avr src/simulator lib/pjrc_usb_serial +DIRS=src src/avr src/simulator src/generic lib/pjrc_usb_serial # Default compiler flags cc-option=$(shell if test -z "`$(1) $(2) -S -o /dev/null -xc /dev/null 2>&1`" \ ; then echo "$(2)"; else echo "$(3)"; fi ;) -CFLAGS-y := -I$(OUT) -Isrc -Os -MD -g \ +CFLAGS-y := -I$(OUT) -Isrc -I$(OUT)board-generic/ -Os -MD -g \ -Wall -Wold-style-definition $(call cc-option,$(CC),-Wtype-limits,) \ -ffunction-sections -fdata-sections CFLAGS-y += -flto -fwhole-program @@ -72,6 +72,8 @@ $(OUT)board-link: $(KCONFIG_CONFIG) @echo " Creating symbolic link $(OUT)board" $(Q)touch $@ $(Q)ln -Tsf $(PWD)/src/$(CONFIG_BOARD_DIRECTORY) $(OUT)board + $(Q)mkdir -p $(OUT)board-generic + $(Q)ln -Tsf $(PWD)/src/generic $(OUT)board-generic/board $(OUT)declfunc.lds: src/declfunc.lds.S @echo " Precompiling $@" diff --git a/src/avr/Makefile b/src/avr/Makefile index c5e5dba1..0e51ed7f 100644 --- a/src/avr/Makefile +++ b/src/avr/Makefile @@ -7,7 +7,7 @@ CFLAGS-y += -mmcu=$(CONFIG_MCU) -DF_CPU=$(CONFIG_CLOCK_FREQ) LDFLAGS-y += -Wl,--relax # Add avr source files -src-y += avr/main.c avr/timer.c avr/gpio.c avr/alloc.c +src-y += avr/main.c avr/timer.c avr/gpio.c avr/misc.c src-$(CONFIG_AVR_WATCHDOG) += avr/watchdog.c src-$(CONFIG_AVR_USBSERIAL) += avr/usbserial.c ../lib/pjrc_usb_serial/usb_serial.c src-$(CONFIG_AVR_SERIAL) += avr/serial.c diff --git a/src/avr/alloc.c b/src/avr/misc.c similarity index 62% rename from src/avr/alloc.c rename to src/avr/misc.c index aaa671bc..682ef29d 100644 --- a/src/avr/alloc.c +++ b/src/avr/misc.c @@ -1,4 +1,4 @@ -// AVR allocation checking code. +// AVR miscellaneous platform code // // Copyright (C) 2016 Kevin O'Connor // @@ -6,10 +6,22 @@ #include // AVR_STACK_POINTER_REG #include // __malloc_heap_end +#include // _crc_ccitt_update #include "autoconf.h" // CONFIG_AVR_STACK_SIZE +#include "board/misc.h" // alloc_maxsize #include "compiler.h" // ALIGN -#include "misc.h" // alloc_maxsize +// Optimized crc16_ccitt for the avr processor +uint16_t +crc16_ccitt(char *buf, uint8_t len) +{ + uint16_t crc = 0xFFFF; + while (len--) + crc = _crc_ccitt_update(crc, *buf++); + return crc; +} + +// Return the maximum allocation size that can succeed up to 'reqsize' size_t alloc_maxsize(size_t reqsize) { diff --git a/src/avr/misc.h b/src/avr/misc.h deleted file mode 100644 index 244cd5bc..00000000 --- a/src/avr/misc.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef __AVR_MISC_H -#define __AVR_MISC_H - -#include -#include - -// alloc.c -size_t alloc_maxsize(size_t reqsize); - -// console.c -char *console_get_input(uint8_t *plen); -void console_pop_input(uint8_t len); -char *console_get_output(uint8_t len); -void console_push_output(uint8_t len); - -// Optimized crc16_ccitt for the avr processor -#define HAVE_OPTIMIZED_CRC 1 -static inline uint16_t _crc16_ccitt(char *buf, uint8_t len) { - uint16_t crc = 0xFFFF; - while (len--) - crc = _crc_ccitt_update(crc, *buf++); - return crc; -} - -#endif // misc.h diff --git a/src/avr/serial.c b/src/avr/serial.c index c73890dd..fc71cf0e 100644 --- a/src/avr/serial.c +++ b/src/avr/serial.c @@ -7,9 +7,9 @@ #include // USART0_RX_vect #include // memmove #include "autoconf.h" // CONFIG_SERIAL_BAUD +#include "board/misc.h" // console_get_input #include "sched.h" // DECL_INIT #include "irq.h" // irq_save -#include "misc.h" // console_get_input #define SERIAL_BUFFER_SIZE 96 static char receive_buf[SERIAL_BUFFER_SIZE]; diff --git a/src/command.c b/src/command.c index 8608fbdf..f11bfcc8 100644 --- a/src/command.c +++ b/src/command.c @@ -10,7 +10,7 @@ #include // strtod #include // strcasecmp #include "board/irq.h" // irq_disable -#include "board/misc.h" // HAVE_OPTIMIZED_CRC +#include "board/misc.h" // crc16_ccitt #include "board/pgm.h" // READP #include "command.h" // output_P #include "sched.h" // DECL_TASK @@ -35,23 +35,6 @@ static uint8_t next_sequence = MESSAGE_DEST; * Binary message parsing ****************************************************************/ -// Implement the standard crc "ccitt" algorithm on the given buffer -static uint16_t -crc16_ccitt(char *buf, uint8_t len) -{ - if (HAVE_OPTIMIZED_CRC) - return _crc16_ccitt(buf, 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; -} - // Encode an integer as a variable length quantity (vlq) static char * encode_int(char *p, uint32_t v) diff --git a/src/generic/alloc.c b/src/generic/alloc.c new file mode 100644 index 00000000..826e4f9e --- /dev/null +++ b/src/generic/alloc.c @@ -0,0 +1,13 @@ +// Dummy implementation for alloc commands +// +// Copyright (C) 2016 Kevin O'Connor +// +// This file may be distributed under the terms of the GNU GPLv3 license. + +#include "misc.h" + +// Return the maximum allocation size that can succeed up to 'reqsize' +size_t alloc_maxsize(size_t reqsize) +{ + return reqsize; +} diff --git a/src/generic/crc16_ccitt.c b/src/generic/crc16_ccitt.c new file mode 100644 index 00000000..8347bb83 --- /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(char *buf, uint8_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 new file mode 100644 index 00000000..9b2986be --- /dev/null +++ b/src/generic/misc.h @@ -0,0 +1,16 @@ +#ifndef __GENERIC_MISC_H +#define __GENERIC_MISC_H + +#include // size_t +#include // uint8_t + +char *console_get_input(uint8_t *plen); +void console_pop_input(uint8_t len); +char *console_get_output(uint8_t len); +void console_push_output(uint8_t len); + +size_t alloc_maxsize(size_t reqsize); + +uint16_t crc16_ccitt(char *buf, uint8_t len); + +#endif // misc.h diff --git a/src/simulator/Makefile b/src/simulator/Makefile index 0dbcdab4..ed250768 100644 --- a/src/simulator/Makefile +++ b/src/simulator/Makefile @@ -1,3 +1,4 @@ # Additional simulator build rules src-y += simulator/main.c simulator/gpio.c +src-y += generic/crc16_ccitt.c generic/alloc.c diff --git a/src/simulator/misc.h b/src/simulator/misc.h deleted file mode 100644 index c279794e..00000000 --- a/src/simulator/misc.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef __SIMU_MISC_H -#define __SIMU_MISC_H - -#include - -// main.c -char *console_get_input(uint8_t *plen); -void console_pop_input(uint8_t len); -char *console_get_output(uint8_t len); -void console_push_output(uint8_t len); - -static inline size_t alloc_maxsize(size_t reqsize) { - return reqsize; -} - -#define HAVE_OPTIMIZED_CRC 0 -static inline uint16_t _crc16_ccitt(char *buf, uint8_t len) { - return 0; -} - -#endif // misc.h