From ba3088e373daa256f0be5a7a82b9be721fbf3fa6 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 21 Nov 2018 22:40:27 +0100 Subject: added RadioHead library --- .gitignore | 2 ++ contrib/Makefile | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index af8d6f1..b336bc7 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,8 @@ contrib/FastLED-* contrib/PJON-* contrib/BMP180-* contrib/vusb-* +contrib/RadioHead-* +contrib/RadioHead/ contrib/.teensy-loader.prepared contrib/teensy_loader_cli contrib/.micronucleus.prepared diff --git a/contrib/Makefile b/contrib/Makefile index 05fe264..0da0e95 100644 --- a/contrib/Makefile +++ b/contrib/Makefile @@ -26,8 +26,10 @@ LUFA_VERSION := 151115 FASTLED_VERSION := 3.1.0 PJON_VERSION := 3.0 VUSB_VERSION := 20121206 +RADIOHEAD_VERSION := 1.89 + +all: download-lufa download-fastled download-pjon download-vusb download-radiohead update-teensy-loader update-micronucleus -all: download-lufa download-fastled download-pjon download-vusb update-teensy-loader update-micronucleus download-lufa: LUFA-${LUFA_VERSION}.zip @@ -35,6 +37,7 @@ LUFA-${LUFA_VERSION}.zip: wget "http://www.github.com/abcminiuser/lufa/archive/$@" -O "$@" unzip $@ + download-fastled: FastLED-${FASTLED_VERSION}.zip FastLED-${FASTLED_VERSION}.zip: @@ -49,12 +52,21 @@ PJON-${PJON_VERSION}.zip: wget "https://github.com/gioblu/PJON/archive/${PJON_VERSION}.zip" -O "$@" unzip $@ + download-vusb: vusb-${VUSB_VERSION}.zip vusb-${VUSB_VERSION}.zip: wget "https://www.obdev.at/downloads/vusb/vusb-${VUSB_VERSION}.zip" -O "$@" unzip $@ + +download-radiohead: RadioHead-${RADIOHEAD_VERSION}.zip + +RadioHead-${RADIOHEAD_VERSION}.zip: + wget "http://www.airspayce.com/mikem/arduino/RadioHead/$@" -O "$@" + unzip $@ + + .teensy-loader.prepared: git clone https://github.com/PaulStoffregen/teensy_loader_cli.git touch $@ @@ -62,6 +74,7 @@ vusb-${VUSB_VERSION}.zip: update-teensy-loader: .teensy-loader.prepared cd teensy_loader_cli; git pull; make + .micronucleus.prepared: git clone https://github.com/micronucleus/micronucleus/ touch $@ -- cgit v1.2.3 From 8e811ef77b42ef1d62db2b19b642daac2acb8665 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 22 Nov 2018 00:42:10 +0100 Subject: added basic environment for RadioHead lib - not working yet --- include.mk | 31 ++++++++++++++++++++++++++++++- lib/SPI.h | 37 +++++++++++++++++++++++++++++++++++++ lora-test/Makefile | 34 ++++++++++++++++++++++++++++++++++ lora-test/lora-test.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ radiohead.mk | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 lib/SPI.h create mode 100644 lora-test/Makefile create mode 100644 lora-test/lora-test.cpp create mode 100644 radiohead.mk diff --git a/include.mk b/include.mk index 66daa10..ffdab72 100644 --- a/include.mk +++ b/include.mk @@ -38,7 +38,7 @@ CXX_OBJ_LIB := $(CXX_LIBS:%=lib-%.o) CXX_SRC_LIB := $(CXX_LIBS:%=$(LIB_DIR)/%.cpp) CXX_DEP_LIB := $(CXX_LIBS:%=lib-%.d) -.PHONY: prepare clean clean-external distclean clean-lufa clean-fastled clean-pjon clean-vusb program erase flash reset run +.PHONY: prepare clean clean-external distclean clean-lufa clean-fastled clean-pjon clean-vusb clean-radiohead program erase flash reset run ELFFILE := $(NAME).elf HEXFILE := $(NAME).hex @@ -79,6 +79,12 @@ CFLAGS += -DUSES_VUSB CFLAGS += $(VUSB_OPTS) endif +ifdef RADIOHEAD_PATH +CXXFLAGS += -I$(RADIOHEAD_PATH) +CXXFLAGS += -DRH_PLATFORM=RH_PLATFORM_GENERIC_AVR8 +CXXFLAGS += $(RADIOHEAD_OPTS) +endif + prepare: $(EXTERNAL_LIBS:%=build-%) clean-external: $(EXTERNAL_LIBS:%=clean-%) @@ -176,6 +182,29 @@ clean-vusb: @echo "****************************************************" +build-radiohead: libradiohead.a + +libradiohead.a: Makefile + @echo "" + @echo "****************************************************" + @echo "building external RadioHead lib ($(RADIOHEAD_PATH))" + @echo "" + make -f $(SPREADAVR_PATH)/radiohead.mk libradiohead.a + make -f $(SPREADAVR_PATH)/radiohead.mk clean + @echo "" + @echo "****************************************************" + +clean-radiohead: + @echo "" + @echo "****************************************************" + @echo "cleaning external RadioHead lib ($(RADIOHEAD_PATH))" + @echo "" + make -f $(SPREADAVR_PATH)/radiohead.mk clean + rm -f libradiohead.a + @echo "" + @echo "****************************************************" + + ## project-specific objects %.d: %.c Makefile @set -e; rm -f $@; \ diff --git a/lib/SPI.h b/lib/SPI.h new file mode 100644 index 0000000..6b3264e --- /dev/null +++ b/lib/SPI.h @@ -0,0 +1,37 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2013-2018 Christian Pointner + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * spreadspace avr utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with spreadspace avr utils. If not, see . + */ + +#ifndef SPREADAVR_SPI_h_INCLUDED +#define SPREADAVR_SPI_h_INCLUDED + +#ifdef __cplusplus +extern "C" { +#endif + + // TODO: for now this is just a placeholder to make so we + // can compile RadioHead + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lora-test/Makefile b/lora-test/Makefile new file mode 100644 index 0000000..8b2dc08 --- /dev/null +++ b/lora-test/Makefile @@ -0,0 +1,34 @@ +## +## spreadspace avr utils +## +## +## Copyright (C) 2013-2018 Christian Pointner +## +## This file is part of spreadspace avr utils. +## +## spreadspace avr utils is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## any later version. +## +## spreadspace avr utils is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with spreadspace avr utils. If not, see . +## + +NAME := lora-test +BOARD_TYPE := elecrow32u4lora +CXX_OBJ := $(NAME).o +LIBS := util led +EXTERNAL_LIBS := radiohead +SPREADAVR_PATH := .. + +RADIOHEAD_PATH := $(SPREADAVR_PATH)/contrib/RadioHead + +RADIOHEAD_DRIVER := RF95 + +include $(SPREADAVR_PATH)/include.mk diff --git a/lora-test/lora-test.cpp b/lora-test/lora-test.cpp new file mode 100644 index 0000000..665e762 --- /dev/null +++ b/lora-test/lora-test.cpp @@ -0,0 +1,45 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2013-2016 Christian Pointner + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * spreadspace avr utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with spreadspace avr utils. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "util.h" +#include "led.h" +#include "RadioHead.h" + +int main(void) +{ + MCUSR &= ~(1 << WDRF); + wdt_disable(); + + cpu_init(); + led_init(); + sei(); + + for(;;) { + // for now we don't do anything here... + } +} diff --git a/radiohead.mk b/radiohead.mk new file mode 100644 index 0000000..f0aabca --- /dev/null +++ b/radiohead.mk @@ -0,0 +1,42 @@ +## +## spreadspace avr utils +## +## +## Copyright (C) 2013-2018 Christian Pointner +## +## This file is part of spreadspace avr utils. +## +## spreadspace avr utils is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## any later version. +## +## spreadspace avr utils is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with spreadspace avr utils. If not, see . +## + +include $(SPREADAVR_PATH)/defines.mk + +RADIOHEAD_COMPONENTS := RH_$(RADIOHEAD_DRIVER) # TODO: added other components dependent on driver... + +SRC:=$(foreach COMP,$(RADIOHEAD_COMPONENTS),$(RADIOHEAD_PATH)/$(COMP).cpp) + +CXXFLAGS += -I$(RADIOHEAD_PATH) +CXXFLAGS += -DRH_PLATFORM=RH_PLATFORM_GENERIC_AVR8 +CXXFLAGS += $(RADIOHEAD_OPTS) + +OBJ = $(SRC:%.cpp=%.o) + +libradiohead.a: $(OBJ) + $(AR) $@ $(OBJ) + +%.o: %.cpp + $(CXX) -c $(CXXFLAGS) $< -o $@ + +clean: + rm -f $(SRC:%.cpp=%.o) -- cgit v1.2.3 From f8eee14729012f620fb7448334eaef46f1964198 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 22 Nov 2018 02:42:45 +0100 Subject: some more tests - still does not compile --- contrib/Makefile | 1 + contrib/radiohead.patch | 26 ++++++++++++++++++++++++ lib/SPI.h | 17 ++++++++-------- lib/arduino-minimal.h | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ lora-test/lora-test.cpp | 8 +++++++- radiohead.mk | 3 ++- 6 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 contrib/radiohead.patch create mode 100644 lib/arduino-minimal.h diff --git a/contrib/Makefile b/contrib/Makefile index 0da0e95..9d606bb 100644 --- a/contrib/Makefile +++ b/contrib/Makefile @@ -65,6 +65,7 @@ download-radiohead: RadioHead-${RADIOHEAD_VERSION}.zip RadioHead-${RADIOHEAD_VERSION}.zip: wget "http://www.airspayce.com/mikem/arduino/RadioHead/$@" -O "$@" unzip $@ + cd RadioHead; patch -p1 < ../radiohead.patch .teensy-loader.prepared: diff --git a/contrib/radiohead.patch b/contrib/radiohead.patch new file mode 100644 index 0000000..e844a0a --- /dev/null +++ b/contrib/radiohead.patch @@ -0,0 +1,26 @@ +diff -Nur RadioHead.orig/RadioHead.h RadioHead/RadioHead.h +--- RadioHead.orig/RadioHead.h 2018-11-15 02:40:23.000000000 +0100 ++++ RadioHead/RadioHead.h 2018-11-22 02:28:56.058105167 +0100 +@@ -317,8 +317,6 @@ + cd /tmp + mkdir RadioHead + cd RadioHead +-cp /usr/local/projects/arduino/libraries/RadioHead/*.h . +-cp /usr/local/projects/arduino/libraries/RadioHead/*.cpp . + cp /usr/local/projects/arduino/libraries/RadioHead/examples/cc110/cc110_client/cc110_client.pde application.cpp + \endcode + - Edit application.cpp and comment out any \#include so it looks like: +@@ -1283,11 +1281,13 @@ + #elif (RH_PLATFORM == RH_PLATFORM_GENERIC_AVR8) + #include + #include ++ #include + #include + #include + #include + #define RH_HAVE_HARDWARE_SPI + #include ++ #include + + // For Steve Childress port to ARM M4 w/CMSIS with STM's Hardware Abstraction lib. + // See ArduinoWorkarounds.h (not supplied) diff --git a/lib/SPI.h b/lib/SPI.h index 6b3264e..a770999 100644 --- a/lib/SPI.h +++ b/lib/SPI.h @@ -23,15 +23,16 @@ #ifndef SPREADAVR_SPI_h_INCLUDED #define SPREADAVR_SPI_h_INCLUDED -#ifdef __cplusplus -extern "C" { -#endif - - // TODO: for now this is just a placeholder to make so we + // TODO: for now this is just a placeholder to make it so we // can compile RadioHead -#ifdef __cplusplus -} -#endif +class SPIClass { // AVR +public: + static void begin(); + static void end(); + static uint8_t transfer(uint8_t data); +}; + +extern SPIClass SPI; #endif diff --git a/lib/arduino-minimal.h b/lib/arduino-minimal.h new file mode 100644 index 0000000..64eb867 --- /dev/null +++ b/lib/arduino-minimal.h @@ -0,0 +1,54 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2013-2018 Christian Pointner + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * spreadspace avr utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with spreadspace avr utils. If not, see . + */ + +#ifndef SPREADAVR_arduino_minimal_h_INCLUDED +#define SPREADAVR_arduino_minimal_h_INCLUDED + +#define HIGH 0x1 +#define LOW 0x0 + +#define INPUT 0x0 +#define OUTPUT 0x1 +#define INPUT_PULLUP 0x2 + +#define CHANGE 1 +#define FALLING 2 +#define RISING 3 + +unsigned long millis(void); +void delay(unsigned long); + +void pinMode(uint8_t, uint8_t); +void digitalWrite(uint8_t, uint8_t); +// int digitalRead(uint8_t); +// int analogRead(uint8_t); +// void analogReference(uint8_t mode); +// void analogWrite(uint8_t, int); + +void attachInterrupt(uint8_t, void (*)(void), int mode); +//void detachInterrupt(uint8_t); + +// WMath prototypes +long random(long); +long random(long, long); + +#endif diff --git a/lora-test/lora-test.cpp b/lora-test/lora-test.cpp index 665e762..aecb0a6 100644 --- a/lora-test/lora-test.cpp +++ b/lora-test/lora-test.cpp @@ -28,7 +28,11 @@ #include "util.h" #include "led.h" + #include "RadioHead.h" +#include "RH_RF95.h" + +RH_RF95 rf95; int main(void) { @@ -39,7 +43,9 @@ int main(void) led_init(); sei(); + rf95.init(); + rf95.setFrequency(868.0); for(;;) { - // for now we don't do anything here... + // for now we don't do anything here... } } diff --git a/radiohead.mk b/radiohead.mk index f0aabca..94d1de3 100644 --- a/radiohead.mk +++ b/radiohead.mk @@ -22,7 +22,8 @@ include $(SPREADAVR_PATH)/defines.mk -RADIOHEAD_COMPONENTS := RH_$(RADIOHEAD_DRIVER) # TODO: added other components dependent on driver... + # TODO: add other components dependent on driver... +RADIOHEAD_COMPONENTS := RH_$(RADIOHEAD_DRIVER) RHGenericDriver RHSPIDriver RHHardwareSPI RHGenericSPI SRC:=$(foreach COMP,$(RADIOHEAD_COMPONENTS),$(RADIOHEAD_PATH)/$(COMP).cpp) -- cgit v1.2.3 From 3bbc176867450337bccd019c2725ed83efeb5916 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 22 Nov 2018 23:35:34 +0100 Subject: added makro USES_RADIOHEAD if the library is used --- include.mk | 1 + 1 file changed, 1 insertion(+) diff --git a/include.mk b/include.mk index ffdab72..cff83b7 100644 --- a/include.mk +++ b/include.mk @@ -81,6 +81,7 @@ endif ifdef RADIOHEAD_PATH CXXFLAGS += -I$(RADIOHEAD_PATH) +CXXFLAGS += -DUSES_RADIOHEAD CXXFLAGS += -DRH_PLATFORM=RH_PLATFORM_GENERIC_AVR8 CXXFLAGS += $(RADIOHEAD_OPTS) endif -- cgit v1.2.3 From fd6a9db2391daa75c20a77f18c0ee935e7e1d39d Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 23 Nov 2018 00:32:50 +0100 Subject: fix radio dep problem -- still now working, but closer ;) --- lib/arduino-minimal.h | 8 ++++++++ radiohead.mk | 11 ++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/arduino-minimal.h b/lib/arduino-minimal.h index 64eb867..fcac3ab 100644 --- a/lib/arduino-minimal.h +++ b/lib/arduino-minimal.h @@ -23,6 +23,10 @@ #ifndef SPREADAVR_arduino_minimal_h_INCLUDED #define SPREADAVR_arduino_minimal_h_INCLUDED +#ifdef __cplusplus +extern "C"{ +#endif + #define HIGH 0x1 #define LOW 0x0 @@ -47,6 +51,10 @@ void digitalWrite(uint8_t, uint8_t); void attachInterrupt(uint8_t, void (*)(void), int mode); //void detachInterrupt(uint8_t); +#ifdef __cplusplus +} // extern "C" +#endif + // WMath prototypes long random(long); long random(long, long); diff --git a/radiohead.mk b/radiohead.mk index 94d1de3..d4a969e 100644 --- a/radiohead.mk +++ b/radiohead.mk @@ -26,18 +26,23 @@ include $(SPREADAVR_PATH)/defines.mk RADIOHEAD_COMPONENTS := RH_$(RADIOHEAD_DRIVER) RHGenericDriver RHSPIDriver RHHardwareSPI RHGenericSPI SRC:=$(foreach COMP,$(RADIOHEAD_COMPONENTS),$(RADIOHEAD_PATH)/$(COMP).cpp) +DEPLIBS := arduino-stub CXXFLAGS += -I$(RADIOHEAD_PATH) CXXFLAGS += -DRH_PLATFORM=RH_PLATFORM_GENERIC_AVR8 CXXFLAGS += $(RADIOHEAD_OPTS) OBJ = $(SRC:%.cpp=%.o) +OBJ_DEPLIB = $(DEPLIBS:%=deplib-radiohead--%.o) -libradiohead.a: $(OBJ) - $(AR) $@ $(OBJ) +libradiohead.a: $(OBJ) $(OBJ_DEPLIB) + $(AR) $@ $(OBJ) $(OBJ_DEPLIB) %.o: %.cpp $(CXX) -c $(CXXFLAGS) $< -o $@ +deplib-radiohead--%.o: $(LIB_DIR)/%.cpp + $(CXX) $(CXXFLAGS) -c $< -o $@ + clean: - rm -f $(SRC:%.cpp=%.o) + rm -f $(SRC:%.cpp=%.o) $(OBJ_DEPLIB) -- cgit v1.2.3 From 1595002fcefa092d16d6367826d7843e7c5c0edd Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 23 Nov 2018 01:55:10 +0100 Subject: extended ardiuno stub --- contrib/radiohead.patch | 4 +- lib/Arduino.h | 27 +++++- lib/arduino-minimal.h | 62 ------------ lib/arduino-stub.cpp | 248 +++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 274 insertions(+), 67 deletions(-) delete mode 100644 lib/arduino-minimal.h diff --git a/contrib/radiohead.patch b/contrib/radiohead.patch index e844a0a..4f9939f 100644 --- a/contrib/radiohead.patch +++ b/contrib/radiohead.patch @@ -1,6 +1,6 @@ diff -Nur RadioHead.orig/RadioHead.h RadioHead/RadioHead.h --- RadioHead.orig/RadioHead.h 2018-11-15 02:40:23.000000000 +0100 -+++ RadioHead/RadioHead.h 2018-11-22 02:28:56.058105167 +0100 ++++ RadioHead/RadioHead.h 2018-11-23 01:50:55.920760834 +0100 @@ -317,8 +317,6 @@ cd /tmp mkdir RadioHead @@ -20,7 +20,7 @@ diff -Nur RadioHead.orig/RadioHead.h RadioHead/RadioHead.h #include #define RH_HAVE_HARDWARE_SPI #include -+ #include ++ #include // For Steve Childress port to ARM M4 w/CMSIS with STM's Hardware Abstraction lib. // See ArduinoWorkarounds.h (not supplied) diff --git a/lib/Arduino.h b/lib/Arduino.h index 3698392..ac7031d 100644 --- a/lib/Arduino.h +++ b/lib/Arduino.h @@ -143,8 +143,8 @@ void delayMicroseconds(unsigned int us); // void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val); // uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder); -// void attachInterrupt(uint8_t, void (*)(void), int mode); -// void detachInterrupt(uint8_t); +void attachInterrupt(uint8_t, void (*)(void), int mode); +void detachInterrupt(uint8_t); // Get the bit location within the hardware port of the given virtual pin. // This comes from the pins_*.c file for the active board configuration. @@ -215,6 +215,29 @@ extern const uint8_t PROGMEM digital_pin_to_timer_PGM[]; #define TIMER5B 17 #define TIMER5C 18 + +#define EXTERNAL_INT_0 0 +#define EXTERNAL_INT_1 1 +#define EXTERNAL_INT_2 2 +#define EXTERNAL_INT_3 3 +#define EXTERNAL_INT_4 4 +#define EXTERNAL_INT_5 5 +#define EXTERNAL_INT_6 6 +#define EXTERNAL_INT_7 7 + +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega128RFA1__) || defined(__AVR_ATmega256RFR2__) || \ + defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega8U2__) +#define EXTERNAL_NUM_INTERRUPTS 8 +#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) +#define EXTERNAL_NUM_INTERRUPTS 3 +#elif defined(__AVR_ATmega32U4__) +#define EXTERNAL_NUM_INTERRUPTS 5 +#else +#define EXTERNAL_NUM_INTERRUPTS 2 +#endif + +typedef void (*voidFuncPtr)(void); + #ifdef __cplusplus } // extern "C" #endif diff --git a/lib/arduino-minimal.h b/lib/arduino-minimal.h deleted file mode 100644 index fcac3ab..0000000 --- a/lib/arduino-minimal.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * spreadspace avr utils - * - * - * Copyright (C) 2013-2018 Christian Pointner - * - * This file is part of spreadspace avr utils. - * - * spreadspace avr utils is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * spreadspace avr utils is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with spreadspace avr utils. If not, see . - */ - -#ifndef SPREADAVR_arduino_minimal_h_INCLUDED -#define SPREADAVR_arduino_minimal_h_INCLUDED - -#ifdef __cplusplus -extern "C"{ -#endif - -#define HIGH 0x1 -#define LOW 0x0 - -#define INPUT 0x0 -#define OUTPUT 0x1 -#define INPUT_PULLUP 0x2 - -#define CHANGE 1 -#define FALLING 2 -#define RISING 3 - -unsigned long millis(void); -void delay(unsigned long); - -void pinMode(uint8_t, uint8_t); -void digitalWrite(uint8_t, uint8_t); -// int digitalRead(uint8_t); -// int analogRead(uint8_t); -// void analogReference(uint8_t mode); -// void analogWrite(uint8_t, int); - -void attachInterrupt(uint8_t, void (*)(void), int mode); -//void detachInterrupt(uint8_t); - -#ifdef __cplusplus -} // extern "C" -#endif - -// WMath prototypes -long random(long); -long random(long, long); - -#endif diff --git a/lib/arduino-stub.cpp b/lib/arduino-stub.cpp index d848e8d..c130a45 100644 --- a/lib/arduino-stub.cpp +++ b/lib/arduino-stub.cpp @@ -329,7 +329,7 @@ void init() * real cooperative scheduler. */ static void __empty() { - // Empty + // Empty } void yield(void) __attribute__ ((weak, alias("__empty"))); @@ -761,6 +761,252 @@ void analogWrite(uint8_t pin, int val) } } +// ****************** +// this is from Arduino's WInterrupts.c + +static void nothing(void) { +} + +static volatile voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS] = { +#if EXTERNAL_NUM_INTERRUPTS > 8 + #warning There are more than 8 external interrupts. Some callbacks may not be initialized. + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 7 + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 6 + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 5 + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 4 + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 3 + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 2 + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 1 + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 0 + nothing, +#endif +}; +// volatile static voidFuncPtr twiIntFunc; + +void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) { + if(interruptNum < EXTERNAL_NUM_INTERRUPTS) { + intFunc[interruptNum] = userFunc; + + // Configure the interrupt mode (trigger on low input, any change, rising + // edge, or falling edge). The mode constants were chosen to correspond + // to the configuration bits in the hardware register, so we simply shift + // the mode into place. + + // Enable the interrupt. + + switch (interruptNum) { +#if defined(__AVR_ATmega32U4__) + // I hate doing this, but the register assignment differs between the 1280/2560 + // and the 32U4. Since avrlib defines registers PCMSK1 and PCMSK2 that aren't + // even present on the 32U4 this is the only way to distinguish between them. + case 0: + EICRA = (EICRA & ~((1< Date: Fri, 23 Nov 2018 13:35:37 +0100 Subject: radiohead compiles and links now ... still quite hacky and needs testing --- contrib/radiohead.patch | 9 +- lib/Arduino-SPI.cpp | 201 ++++++++++++++++++++++++++++++ lib/Arduino-SPI.h | 324 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/SPI.h | 38 ------ lib/arduino-stub.cpp | 20 ++- radiohead.mk | 2 +- 6 files changed, 550 insertions(+), 44 deletions(-) create mode 100644 lib/Arduino-SPI.cpp create mode 100644 lib/Arduino-SPI.h delete mode 100644 lib/SPI.h diff --git a/contrib/radiohead.patch b/contrib/radiohead.patch index 4f9939f..3b154ee 100644 --- a/contrib/radiohead.patch +++ b/contrib/radiohead.patch @@ -1,6 +1,6 @@ -diff -Nur RadioHead.orig/RadioHead.h RadioHead/RadioHead.h ---- RadioHead.orig/RadioHead.h 2018-11-15 02:40:23.000000000 +0100 -+++ RadioHead/RadioHead.h 2018-11-23 01:50:55.920760834 +0100 +diff -Nur RadioHead-orig/RadioHead.h RadioHead/RadioHead.h +--- RadioHead-orig/RadioHead.h 2018-11-15 11:40:24.000000000 +0100 ++++ RadioHead/RadioHead.h 2018-11-23 13:20:02.014133482 +0100 @@ -317,8 +317,6 @@ cd /tmp mkdir RadioHead @@ -19,8 +19,9 @@ diff -Nur RadioHead.orig/RadioHead.h RadioHead/RadioHead.h #include #include #define RH_HAVE_HARDWARE_SPI - #include +- #include + #include ++ #include // For Steve Childress port to ARM M4 w/CMSIS with STM's Hardware Abstraction lib. // See ArduinoWorkarounds.h (not supplied) diff --git a/lib/Arduino-SPI.cpp b/lib/Arduino-SPI.cpp new file mode 100644 index 0000000..0a7ac91 --- /dev/null +++ b/lib/Arduino-SPI.cpp @@ -0,0 +1,201 @@ +/* + * Copyright (c) 2010 by Cristian Maglie + * Copyright (c) 2014 by Paul Stoffregen (Transaction API) + * Copyright (c) 2014 by Matthijs Kooijman (SPISettings AVR) + * Copyright (c) 2014 by Andrew J. Kroll (atomicity fixes) + * SPI Master library for arduino. + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of either the GNU General Public License version 2 + * or the GNU Lesser General Public License version 2.1, both as + * published by the Free Software Foundation. + */ + +#include "Arduino-SPI.h" + +SPIClass SPI; + +uint8_t SPIClass::initialized = 0; +uint8_t SPIClass::interruptMode = 0; +uint8_t SPIClass::interruptMask = 0; +uint8_t SPIClass::interruptSave = 0; +#ifdef SPI_TRANSACTION_MISMATCH_LED +uint8_t SPIClass::inTransactionFlag = 0; +#endif + +void SPIClass::begin() +{ + uint8_t sreg = SREG; + noInterrupts(); // Protect from a scheduler and prevent transactionBegin + if (!initialized) { + // Set SS to high so a connected chip will be "deselected" by default + uint8_t port = digitalPinToPort(SS); + uint8_t bit = digitalPinToBitMask(SS); + volatile uint8_t *reg = portModeRegister(port); + + // if the SS pin is not already configured as an output + // then set it high (to enable the internal pull-up resistor) + if(!(*reg & bit)){ + digitalWrite(SS, HIGH); + } + + // When the SS pin is set as OUTPUT, it can be used as + // a general purpose output port (it doesn't influence + // SPI operations). + pinMode(SS, OUTPUT); + + // Warning: if the SS pin ever becomes a LOW INPUT then SPI + // automatically switches to Slave, so the data direction of + // the SS pin MUST be kept as OUTPUT. + SPCR |= _BV(MSTR); + SPCR |= _BV(SPE); + + // Set direction register for SCK and MOSI pin. + // MISO pin automatically overrides to INPUT. + // By doing this AFTER enabling SPI, we avoid accidentally + // clocking in a single bit since the lines go directly + // from "input" to SPI control. + // http://code.google.com/p/arduino/issues/detail?id=888 + pinMode(SCK, OUTPUT); + pinMode(MOSI, OUTPUT); + } + initialized++; // reference count + SREG = sreg; +} + +void SPIClass::end() { + uint8_t sreg = SREG; + noInterrupts(); // Protect from a scheduler and prevent transactionBegin + // Decrease the reference counter + if (initialized) + initialized--; + // If there are no more references disable SPI + if (!initialized) { + SPCR &= ~_BV(SPE); + interruptMode = 0; + #ifdef SPI_TRANSACTION_MISMATCH_LED + inTransactionFlag = 0; + #endif + } + SREG = sreg; +} + +// mapping of interrupt numbers to bits within SPI_AVR_EIMSK +#if defined(__AVR_ATmega32U4__) + #define SPI_INT0_MASK (1< + * Copyright (c) 2014 by Paul Stoffregen (Transaction API) + * Copyright (c) 2014 by Matthijs Kooijman (SPISettings AVR) + * Copyright (c) 2014 by Andrew J. Kroll (atomicity fixes) + * SPI Master library for arduino. + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of either the GNU General Public License version 2 + * or the GNU Lesser General Public License version 2.1, both as + * published by the Free Software Foundation. + */ + +#ifndef _SPI_H_INCLUDED +#define _SPI_H_INCLUDED + +#include + +// SPI_HAS_TRANSACTION means SPI has beginTransaction(), endTransaction(), +// usingInterrupt(), and SPISetting(clock, bitOrder, dataMode) +#define SPI_HAS_TRANSACTION 1 + +// SPI_HAS_NOTUSINGINTERRUPT means that SPI has notUsingInterrupt() method +#define SPI_HAS_NOTUSINGINTERRUPT 1 + +// SPI_ATOMIC_VERSION means that SPI has atomicity fixes and what version. +// This way when there is a bug fix you can check this define to alert users +// of your code if it uses better version of this library. +// This also implies everything that SPI_HAS_TRANSACTION as documented above is +// available too. +#define SPI_ATOMIC_VERSION 1 + +// Uncomment this line to add detection of mismatched begin/end transactions. +// A mismatch occurs if other libraries fail to use SPI.endTransaction() for +// each SPI.beginTransaction(). Connect an LED to this pin. The LED will turn +// on if any mismatch is ever detected. +//#define SPI_TRANSACTION_MISMATCH_LED 5 + +#ifndef LSBFIRST +#define LSBFIRST 0 +#endif +#ifndef MSBFIRST +#define MSBFIRST 1 +#endif + +#define SPI_CLOCK_DIV4 0x00 +#define SPI_CLOCK_DIV16 0x01 +#define SPI_CLOCK_DIV64 0x02 +#define SPI_CLOCK_DIV128 0x03 +#define SPI_CLOCK_DIV2 0x04 +#define SPI_CLOCK_DIV8 0x05 +#define SPI_CLOCK_DIV32 0x06 + +#define SPI_MODE0 0x00 +#define SPI_MODE1 0x04 +#define SPI_MODE2 0x08 +#define SPI_MODE3 0x0C + +#define SPI_MODE_MASK 0x0C // CPOL = bit 3, CPHA = bit 2 on SPCR +#define SPI_CLOCK_MASK 0x03 // SPR1 = bit 1, SPR0 = bit 0 on SPCR +#define SPI_2XCLOCK_MASK 0x01 // SPI2X = bit 0 on SPSR + +// define SPI_AVR_EIMSK for AVR boards with external interrupt pins +#if defined(EIMSK) + #define SPI_AVR_EIMSK EIMSK +#elif defined(GICR) + #define SPI_AVR_EIMSK GICR +#elif defined(GIMSK) + #define SPI_AVR_EIMSK GIMSK +#endif + +class SPISettings { +public: + SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) { + if (__builtin_constant_p(clock)) { + init_AlwaysInline(clock, bitOrder, dataMode); + } else { + init_MightInline(clock, bitOrder, dataMode); + } + } + SPISettings() { + init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0); + } +private: + void init_MightInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) { + init_AlwaysInline(clock, bitOrder, dataMode); + } + void init_AlwaysInline(uint32_t clock, uint8_t bitOrder, uint8_t dataMode) + __attribute__((__always_inline__)) { + // Clock settings are defined as follows. Note that this shows SPI2X + // inverted, so the bits form increasing numbers. Also note that + // fosc/64 appears twice + // SPR1 SPR0 ~SPI2X Freq + // 0 0 0 fosc/2 + // 0 0 1 fosc/4 + // 0 1 0 fosc/8 + // 0 1 1 fosc/16 + // 1 0 0 fosc/32 + // 1 0 1 fosc/64 + // 1 1 0 fosc/64 + // 1 1 1 fosc/128 + + // We find the fastest clock that is less than or equal to the + // given clock rate. The clock divider that results in clock_setting + // is 2 ^^ (clock_div + 1). If nothing is slow enough, we'll use the + // slowest (128 == 2 ^^ 7, so clock_div = 6). + uint8_t clockDiv; + + // When the clock is known at compiletime, use this if-then-else + // cascade, which the compiler knows how to completely optimize + // away. When clock is not known, use a loop instead, which generates + // shorter code. + if (__builtin_constant_p(clock)) { + if (clock >= F_CPU / 2) { + clockDiv = 0; + } else if (clock >= F_CPU / 4) { + clockDiv = 1; + } else if (clock >= F_CPU / 8) { + clockDiv = 2; + } else if (clock >= F_CPU / 16) { + clockDiv = 3; + } else if (clock >= F_CPU / 32) { + clockDiv = 4; + } else if (clock >= F_CPU / 64) { + clockDiv = 5; + } else { + clockDiv = 6; + } + } else { + uint32_t clockSetting = F_CPU / 2; + clockDiv = 0; + while (clockDiv < 6 && clock < clockSetting) { + clockSetting /= 2; + clockDiv++; + } + } + + // Compensate for the duplicate fosc/64 + if (clockDiv == 6) + clockDiv = 7; + + // Invert the SPI2X bit + clockDiv ^= 0x1; + + // Pack into the SPISettings class + spcr = _BV(SPE) | _BV(MSTR) | ((bitOrder == LSBFIRST) ? _BV(DORD) : 0) | + (dataMode & SPI_MODE_MASK) | ((clockDiv >> 1) & SPI_CLOCK_MASK); + spsr = clockDiv & SPI_2XCLOCK_MASK; + } + uint8_t spcr; + uint8_t spsr; + friend class SPIClass; +}; + + +class SPIClass { +public: + // Initialize the SPI library + static void begin(); + + // If SPI is used from within an interrupt, this function registers + // that interrupt with the SPI library, so beginTransaction() can + // prevent conflicts. The input interruptNumber is the number used + // with attachInterrupt. If SPI is used from a different interrupt + // (eg, a timer), interruptNumber should be 255. + static void usingInterrupt(uint8_t interruptNumber); + // And this does the opposite. + static void notUsingInterrupt(uint8_t interruptNumber); + // Note: the usingInterrupt and notUsingInterrupt functions should + // not to be called from ISR context or inside a transaction. + // For details see: + // https://github.com/arduino/Arduino/pull/2381 + // https://github.com/arduino/Arduino/pull/2449 + + // Before using SPI.transfer() or asserting chip select pins, + // this function is used to gain exclusive access to the SPI bus + // and configure the correct settings. + inline static void beginTransaction(SPISettings settings) { + if (interruptMode > 0) { + uint8_t sreg = SREG; + noInterrupts(); + + #ifdef SPI_AVR_EIMSK + if (interruptMode == 1) { + interruptSave = SPI_AVR_EIMSK; + SPI_AVR_EIMSK &= ~interruptMask; + SREG = sreg; + } else + #endif + { + interruptSave = sreg; + } + } + + #ifdef SPI_TRANSACTION_MISMATCH_LED + if (inTransactionFlag) { + pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT); + digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH); + } + inTransactionFlag = 1; + #endif + + SPCR = settings.spcr; + SPSR = settings.spsr; + } + + // Write to the SPI bus (MOSI pin) and also receive (MISO pin) + inline static uint8_t transfer(uint8_t data) { + SPDR = data; + /* + * The following NOP introduces a small delay that can prevent the wait + * loop form iterating when running at the maximum speed. This gives + * about 10% more speed, even if it seems counter-intuitive. At lower + * speeds it is unnoticed. + */ + asm volatile("nop"); + while (!(SPSR & _BV(SPIF))) ; // wait + return SPDR; + } + inline static uint16_t transfer16(uint16_t data) { + union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } in, out; + in.val = data; + if (!(SPCR & _BV(DORD))) { + SPDR = in.msb; + asm volatile("nop"); // See transfer(uint8_t) function + while (!(SPSR & _BV(SPIF))) ; + out.msb = SPDR; + SPDR = in.lsb; + asm volatile("nop"); + while (!(SPSR & _BV(SPIF))) ; + out.lsb = SPDR; + } else { + SPDR = in.lsb; + asm volatile("nop"); + while (!(SPSR & _BV(SPIF))) ; + out.lsb = SPDR; + SPDR = in.msb; + asm volatile("nop"); + while (!(SPSR & _BV(SPIF))) ; + out.msb = SPDR; + } + return out.val; + } + inline static void transfer(void *buf, size_t count) { + if (count == 0) return; + uint8_t *p = (uint8_t *)buf; + SPDR = *p; + while (--count > 0) { + uint8_t out = *(p + 1); + while (!(SPSR & _BV(SPIF))) ; + uint8_t in = SPDR; + SPDR = out; + *p++ = in; + } + while (!(SPSR & _BV(SPIF))) ; + *p = SPDR; + } + // After performing a group of transfers and releasing the chip select + // signal, this function allows others to access the SPI bus + inline static void endTransaction(void) { + #ifdef SPI_TRANSACTION_MISMATCH_LED + if (!inTransactionFlag) { + pinMode(SPI_TRANSACTION_MISMATCH_LED, OUTPUT); + digitalWrite(SPI_TRANSACTION_MISMATCH_LED, HIGH); + } + inTransactionFlag = 0; + #endif + + if (interruptMode > 0) { + #ifdef SPI_AVR_EIMSK + uint8_t sreg = SREG; + #endif + noInterrupts(); + #ifdef SPI_AVR_EIMSK + if (interruptMode == 1) { + SPI_AVR_EIMSK = interruptSave; + SREG = sreg; + } else + #endif + { + SREG = interruptSave; + } + } + } + + // Disable the SPI bus + static void end(); + + // This function is deprecated. New applications should use + // beginTransaction() to configure SPI settings. + inline static void setBitOrder(uint8_t bitOrder) { + if (bitOrder == LSBFIRST) SPCR |= _BV(DORD); + else SPCR &= ~(_BV(DORD)); + } + // This function is deprecated. New applications should use + // beginTransaction() to configure SPI settings. + inline static void setDataMode(uint8_t dataMode) { + SPCR = (SPCR & ~SPI_MODE_MASK) | dataMode; + } + // This function is deprecated. New applications should use + // beginTransaction() to configure SPI settings. + inline static void setClockDivider(uint8_t clockDiv) { + SPCR = (SPCR & ~SPI_CLOCK_MASK) | (clockDiv & SPI_CLOCK_MASK); + SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((clockDiv >> 2) & SPI_2XCLOCK_MASK); + } + // These undocumented functions should not be used. SPI.transfer() + // polls the hardware flag which is automatically cleared as the + // AVR responds to SPI's interrupt + inline static void attachInterrupt() { SPCR |= _BV(SPIE); } + inline static void detachInterrupt() { SPCR &= ~_BV(SPIE); } + +private: + static uint8_t initialized; + static uint8_t interruptMode; // 0=none, 1=mask, 2=global + static uint8_t interruptMask; // which interrupts to mask + static uint8_t interruptSave; // temp storage, to restore state + #ifdef SPI_TRANSACTION_MISMATCH_LED + static uint8_t inTransactionFlag; + #endif +}; + +extern SPIClass SPI; + +#endif diff --git a/lib/SPI.h b/lib/SPI.h deleted file mode 100644 index a770999..0000000 --- a/lib/SPI.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * spreadspace avr utils - * - * - * Copyright (C) 2013-2018 Christian Pointner - * - * This file is part of spreadspace avr utils. - * - * spreadspace avr utils is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * spreadspace avr utils is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with spreadspace avr utils. If not, see . - */ - -#ifndef SPREADAVR_SPI_h_INCLUDED -#define SPREADAVR_SPI_h_INCLUDED - - // TODO: for now this is just a placeholder to make it so we - // can compile RadioHead - -class SPIClass { // AVR -public: - static void begin(); - static void end(); - static uint8_t transfer(uint8_t data); -}; - -extern SPIClass SPI; - -#endif diff --git a/lib/arduino-stub.cpp b/lib/arduino-stub.cpp index c130a45..39bd9cc 100644 --- a/lib/arduino-stub.cpp +++ b/lib/arduino-stub.cpp @@ -22,6 +22,7 @@ #define ARDUINO_MAIN #include "Arduino.h" +#include "stdlib.h" int atexit(void (* /*func*/ )()) { return 0; } @@ -1042,4 +1043,21 @@ long map(long x, long in_min, long in_max, long out_min, long out_max) unsigned int makeWord(unsigned int w) { return w; } unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; } -// end WMath.cpp + +// ****************** +// this is from Arduino's abi.cpp + +extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__)); +extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__)); + +void __cxa_pure_virtual(void) { + // We might want to write some diagnostics to uart in this case + //std::terminate(); + abort(); +} + +void __cxa_deleted_virtual(void) { + // We might want to write some diagnostics to uart in this case + //std::terminate(); + abort(); +} diff --git a/radiohead.mk b/radiohead.mk index d4a969e..16e13e1 100644 --- a/radiohead.mk +++ b/radiohead.mk @@ -26,7 +26,7 @@ include $(SPREADAVR_PATH)/defines.mk RADIOHEAD_COMPONENTS := RH_$(RADIOHEAD_DRIVER) RHGenericDriver RHSPIDriver RHHardwareSPI RHGenericSPI SRC:=$(foreach COMP,$(RADIOHEAD_COMPONENTS),$(RADIOHEAD_PATH)/$(COMP).cpp) -DEPLIBS := arduino-stub +DEPLIBS := Arduino-SPI arduino-stub CXXFLAGS += -I$(RADIOHEAD_PATH) CXXFLAGS += -DRH_PLATFORM=RH_PLATFORM_GENERIC_AVR8 -- cgit v1.2.3 From 3de6c7256b270ec2076f65eb115c261bb90352c0 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 23 Nov 2018 14:07:28 +0100 Subject: added ubsio to lora-test and renamed it to usb-lora --- lora-test/Makefile | 34 --------------- lora-test/lora-test.cpp | 51 ---------------------- usb-lora/Makefile | 50 ++++++++++++++++++++++ usb-lora/usb-lora.cpp | 110 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+), 85 deletions(-) delete mode 100644 lora-test/Makefile delete mode 100644 lora-test/lora-test.cpp create mode 100644 usb-lora/Makefile create mode 100644 usb-lora/usb-lora.cpp diff --git a/lora-test/Makefile b/lora-test/Makefile deleted file mode 100644 index 8b2dc08..0000000 --- a/lora-test/Makefile +++ /dev/null @@ -1,34 +0,0 @@ -## -## spreadspace avr utils -## -## -## Copyright (C) 2013-2018 Christian Pointner -## -## This file is part of spreadspace avr utils. -## -## spreadspace avr utils is free software: you can redistribute it and/or modify -## it under the terms of the GNU General Public License as published by -## the Free Software Foundation, either version 3 of the License, or -## any later version. -## -## spreadspace avr utils is distributed in the hope that it will be useful, -## but WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -## GNU General Public License for more details. -## -## You should have received a copy of the GNU General Public License -## along with spreadspace avr utils. If not, see . -## - -NAME := lora-test -BOARD_TYPE := elecrow32u4lora -CXX_OBJ := $(NAME).o -LIBS := util led -EXTERNAL_LIBS := radiohead -SPREADAVR_PATH := .. - -RADIOHEAD_PATH := $(SPREADAVR_PATH)/contrib/RadioHead - -RADIOHEAD_DRIVER := RF95 - -include $(SPREADAVR_PATH)/include.mk diff --git a/lora-test/lora-test.cpp b/lora-test/lora-test.cpp deleted file mode 100644 index aecb0a6..0000000 --- a/lora-test/lora-test.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - * spreadspace avr utils - * - * - * Copyright (C) 2013-2016 Christian Pointner - * - * This file is part of spreadspace avr utils. - * - * spreadspace avr utils is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * any later version. - * - * spreadspace avr utils is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with spreadspace avr utils. If not, see . - */ - -#include -#include -#include -#include -#include - -#include "util.h" -#include "led.h" - -#include "RadioHead.h" -#include "RH_RF95.h" - -RH_RF95 rf95; - -int main(void) -{ - MCUSR &= ~(1 << WDRF); - wdt_disable(); - - cpu_init(); - led_init(); - sei(); - - rf95.init(); - rf95.setFrequency(868.0); - for(;;) { - // for now we don't do anything here... - } -} diff --git a/usb-lora/Makefile b/usb-lora/Makefile new file mode 100644 index 0000000..07e3a9a --- /dev/null +++ b/usb-lora/Makefile @@ -0,0 +1,50 @@ +## +## spreadspace avr utils +## +## +## Copyright (C) 2013-2018 Christian Pointner +## +## This file is part of spreadspace avr utils. +## +## spreadspace avr utils is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## any later version. +## +## spreadspace avr utils is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with spreadspace avr utils. If not, see . +## + +NAME := usb-lora +BOARD_TYPE := elecrow32u4lora +CXX_OBJ := $(NAME).o +LIBS := util led lufa-descriptor-usbserial usbio +EXTERNAL_LIBS := lufa radiohead +SPREADAVR_PATH := .. + +LUFA_PATH := $(SPREADAVR_PATH)/contrib/lufa-LUFA-151115 +LUFA_OPTS = -D USB_DEVICE_ONLY +LUFA_OPTS += -D DEVICE_STATE_AS_GPIOR=0 +LUFA_OPTS += -D ORDERED_EP_CONFIG +LUFA_OPTS += -D FIXED_CONTROL_ENDPOINT_SIZE=8 +LUFA_OPTS += -D FIXED_NUM_CONFIGURATIONS=1 +LUFA_OPTS += -D USE_FLASH_DESCRIPTORS +LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)" +LUFA_OPTS += -D INTERRUPT_CONTROL_ENDPOINT + +LUFA_OPTS += -D USB_MANUFACTURER="L\"equinox\"" +LUFA_OPTS += -D USB_PRODUCT="L\"spreadspace usb-lora example\"" + +LUFA_COMPONENTS := USB USBCLASS + + +RADIOHEAD_PATH := $(SPREADAVR_PATH)/contrib/RadioHead + +RADIOHEAD_DRIVER := RF95 + +include $(SPREADAVR_PATH)/include.mk diff --git a/usb-lora/usb-lora.cpp b/usb-lora/usb-lora.cpp new file mode 100644 index 0000000..c0ad3ae --- /dev/null +++ b/usb-lora/usb-lora.cpp @@ -0,0 +1,110 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2013-2016 Christian Pointner + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * spreadspace avr utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with spreadspace avr utils. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "util.h" +#include "led.h" +#include "usbio.h" + +#include "RadioHead.h" +#include "RH_RF95.h" + +RH_RF95 lora; + +void recv_lora_msg() +{ + uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; + uint8_t len = sizeof(buf); + + led_on(); + if(!lora.recv(buf, &len)) { + printf("lora.recv(): failed\r\n"); + led_off(); + return; + } + + printf("lora.recv() got message: SNR = %d, data = ", lora.lastSNR()); + for(uint8_t i = 0; i < len; ++i) printf("%s0x%02X", (i==0) ? "" : " ", buf[i]); + printf("\r\n"); + led_off(); +} + +void send_lora_msg() +{ + uint8_t data[] = "spreadspace.org/avr-utils usb-lora test using radiohead library"; + + led2_on(); + if(!lora.send(data, sizeof(data))) { + printf("lora.send(): failed\r\n"); + led2_off(); + return; + } + lora.waitPacketSent(); + printf("lora.send(): success\r\n"); + led2_off(); +} + +void handle_cmd(uint8_t cmd) +{ + switch(cmd) { + case 's': send_lora_msg(); break; + case 'r': reset2bootloader(); break; + default: printf("error\r\n"); return; + } + printf("ok\r\n"); +} + +int main(void) +{ + MCUSR &= ~(1 << WDRF); + wdt_disable(); + + cpu_init(); + led_init(); + usbio_init(); + sei(); + + lora.init(); + lora.setFrequency(868.0); + + for(;;) { + if(lora.available()) { + recv_lora_msg(); + } + + int16_t BytesReceived = usbio_bytes_received(); + while(BytesReceived > 0) { + int ReceivedByte = fgetc(stdin); + if(ReceivedByte != EOF) { + handle_cmd(ReceivedByte); + } + BytesReceived--; + } + + usbio_task(); + } +} -- cgit v1.2.3 From a3832be0ae169755506db1ca639ea394c57fe6e4 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 23 Nov 2018 14:22:55 +0100 Subject: improved handling for __cxa_.._virtual() functions --- lib/arduino-stub.cpp | 20 -------------------- lib/stdc++-minimal.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ radiohead.mk | 2 +- 3 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 lib/stdc++-minimal.cpp diff --git a/lib/arduino-stub.cpp b/lib/arduino-stub.cpp index 39bd9cc..201962f 100644 --- a/lib/arduino-stub.cpp +++ b/lib/arduino-stub.cpp @@ -22,7 +22,6 @@ #define ARDUINO_MAIN #include "Arduino.h" -#include "stdlib.h" int atexit(void (* /*func*/ )()) { return 0; } @@ -1042,22 +1041,3 @@ long map(long x, long in_min, long in_max, long out_min, long out_max) unsigned int makeWord(unsigned int w) { return w; } unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; } - - -// ****************** -// this is from Arduino's abi.cpp - -extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__)); -extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__)); - -void __cxa_pure_virtual(void) { - // We might want to write some diagnostics to uart in this case - //std::terminate(); - abort(); -} - -void __cxa_deleted_virtual(void) { - // We might want to write some diagnostics to uart in this case - //std::terminate(); - abort(); -} diff --git a/lib/stdc++-minimal.cpp b/lib/stdc++-minimal.cpp new file mode 100644 index 0000000..50b1947 --- /dev/null +++ b/lib/stdc++-minimal.cpp @@ -0,0 +1,41 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2014-2018 Christian Pointner + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * spreadspace avr utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with spreadspace avr utils. If not, see . + */ + +#include "stdlib.h" +#include "stdio.h" + +extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__)); +extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__)); + +void __cxa_pure_virtual(void) { + // this only works with serialio, it won't work with usbio + // because we would need to call usbio_task() ... + printf("PANIC: __cxa_pure_virtual() got called ..."); + for(;;); +} + +void __cxa_deleted_virtual(void) { + // this only works with serialio, it won't work with usbio + // because we would need to call usbio_task() ... + printf("PANIC: __cxa_deleted_virtual() got called ..."); + for(;;); +} diff --git a/radiohead.mk b/radiohead.mk index 16e13e1..cef06b8 100644 --- a/radiohead.mk +++ b/radiohead.mk @@ -26,7 +26,7 @@ include $(SPREADAVR_PATH)/defines.mk RADIOHEAD_COMPONENTS := RH_$(RADIOHEAD_DRIVER) RHGenericDriver RHSPIDriver RHHardwareSPI RHGenericSPI SRC:=$(foreach COMP,$(RADIOHEAD_COMPONENTS),$(RADIOHEAD_PATH)/$(COMP).cpp) -DEPLIBS := Arduino-SPI arduino-stub +DEPLIBS := Arduino-SPI arduino-stub stdc++-minimal CXXFLAGS += -I$(RADIOHEAD_PATH) CXXFLAGS += -DRH_PLATFORM=RH_PLATFORM_GENERIC_AVR8 -- cgit v1.2.3 From 95931f7e6ef5431191309e0c247e293bf40afce8 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 23 Nov 2018 15:17:10 +0100 Subject: fix whitespaces --- usb-lora/usb-lora.cpp | 56 +++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/usb-lora/usb-lora.cpp b/usb-lora/usb-lora.cpp index c0ad3ae..3d48031 100644 --- a/usb-lora/usb-lora.cpp +++ b/usb-lora/usb-lora.cpp @@ -37,35 +37,35 @@ RH_RF95 lora; void recv_lora_msg() { - uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; - uint8_t len = sizeof(buf); - - led_on(); - if(!lora.recv(buf, &len)) { - printf("lora.recv(): failed\r\n"); - led_off(); - return; - } - - printf("lora.recv() got message: SNR = %d, data = ", lora.lastSNR()); - for(uint8_t i = 0; i < len; ++i) printf("%s0x%02X", (i==0) ? "" : " ", buf[i]); - printf("\r\n"); - led_off(); + uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; + uint8_t len = sizeof(buf); + + led_on(); + if(!lora.recv(buf, &len)) { + printf("lora.recv(): failed\r\n"); + led_off(); + return; + } + + printf("lora.recv() got message: SNR = %d, data = ", lora.lastSNR()); + for(uint8_t i = 0; i < len; ++i) printf("%s0x%02X", (i==0) ? "" : " ", buf[i]); + printf("\r\n"); + led_off(); } void send_lora_msg() { - uint8_t data[] = "spreadspace.org/avr-utils usb-lora test using radiohead library"; - - led2_on(); - if(!lora.send(data, sizeof(data))) { - printf("lora.send(): failed\r\n"); - led2_off(); - return; - } - lora.waitPacketSent(); - printf("lora.send(): success\r\n"); - led2_off(); + uint8_t data[] = "spreadspace.org/avr-utils usb-lora test using radiohead library"; + + led2_on(); + if(!lora.send(data, sizeof(data))) { + printf("lora.send(): failed\r\n"); + led2_off(); + return; + } + lora.waitPacketSent(); + printf("lora.send(): success\r\n"); + led2_off(); } void handle_cmd(uint8_t cmd) @@ -92,9 +92,9 @@ int main(void) lora.setFrequency(868.0); for(;;) { - if(lora.available()) { - recv_lora_msg(); - } + if(lora.available()) { + recv_lora_msg(); + } int16_t BytesReceived = usbio_bytes_received(); while(BytesReceived > 0) { -- cgit v1.2.3 From 4ef807724561efbb2c7864db498a53b1dcdcf5c1 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 24 Nov 2018 03:22:20 +0100 Subject: fix lora.init() and did some cleanup --- lib/stdc++-minimal.cpp | 4 ++-- radiohead.mk | 2 +- usb-lora/usb-lora.cpp | 16 ++++++++-------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/stdc++-minimal.cpp b/lib/stdc++-minimal.cpp index 50b1947..c3b0559 100644 --- a/lib/stdc++-minimal.cpp +++ b/lib/stdc++-minimal.cpp @@ -29,13 +29,13 @@ extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__)); void __cxa_pure_virtual(void) { // this only works with serialio, it won't work with usbio // because we would need to call usbio_task() ... - printf("PANIC: __cxa_pure_virtual() got called ..."); + printf("PANIC: __cxa_pure_virtual() got called ...\r\n"); for(;;); } void __cxa_deleted_virtual(void) { // this only works with serialio, it won't work with usbio // because we would need to call usbio_task() ... - printf("PANIC: __cxa_deleted_virtual() got called ..."); + printf("PANIC: __cxa_deleted_virtual() got called ...\r\n"); for(;;); } diff --git a/radiohead.mk b/radiohead.mk index cef06b8..6352d5e 100644 --- a/radiohead.mk +++ b/radiohead.mk @@ -23,7 +23,7 @@ include $(SPREADAVR_PATH)/defines.mk # TODO: add other components dependent on driver... -RADIOHEAD_COMPONENTS := RH_$(RADIOHEAD_DRIVER) RHGenericDriver RHSPIDriver RHHardwareSPI RHGenericSPI +RADIOHEAD_COMPONENTS := RH_$(RADIOHEAD_DRIVER) RHSPIDriver RHGenericDriver RHGenericSPI RHHardwareSPI SRC:=$(foreach COMP,$(RADIOHEAD_COMPONENTS),$(RADIOHEAD_PATH)/$(COMP).cpp) DEPLIBS := Arduino-SPI arduino-stub stdc++-minimal diff --git a/usb-lora/usb-lora.cpp b/usb-lora/usb-lora.cpp index 3d48031..d56946f 100644 --- a/usb-lora/usb-lora.cpp +++ b/usb-lora/usb-lora.cpp @@ -40,42 +40,41 @@ void recv_lora_msg() uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; uint8_t len = sizeof(buf); - led_on(); + led2_on(); if(!lora.recv(buf, &len)) { printf("lora.recv(): failed\r\n"); - led_off(); + led2_off(); return; } printf("lora.recv() got message: SNR = %d, data = ", lora.lastSNR()); for(uint8_t i = 0; i < len; ++i) printf("%s0x%02X", (i==0) ? "" : " ", buf[i]); printf("\r\n"); - led_off(); + led2_off(); } void send_lora_msg() { uint8_t data[] = "spreadspace.org/avr-utils usb-lora test using radiohead library"; + printf("lora: sending message with %d bytes\r\n", sizeof(data)); - led2_on(); + led_on(); if(!lora.send(data, sizeof(data))) { printf("lora.send(): failed\r\n"); - led2_off(); + led_off(); return; } lora.waitPacketSent(); printf("lora.send(): success\r\n"); - led2_off(); + led_off(); } void handle_cmd(uint8_t cmd) { switch(cmd) { case 's': send_lora_msg(); break; - case 'r': reset2bootloader(); break; default: printf("error\r\n"); return; } - printf("ok\r\n"); } int main(void) @@ -86,6 +85,7 @@ int main(void) cpu_init(); led_init(); usbio_init(); + arduino_init(); sei(); lora.init(); -- cgit v1.2.3 From 49e6624d3817c87d71265790d774e0d34a47b8b4 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 24 Nov 2018 03:50:02 +0100 Subject: usb-lora example works now - but is not stable yet... --- contrib/radiohead.patch | 5 +++-- lib/arduino-stub.cpp | 35 +++++++++++++++++++++++++++++++++++ usb-lora/usb-lora.cpp | 12 +++++++----- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/contrib/radiohead.patch b/contrib/radiohead.patch index 3b154ee..a7040a6 100644 --- a/contrib/radiohead.patch +++ b/contrib/radiohead.patch @@ -1,6 +1,6 @@ diff -Nur RadioHead-orig/RadioHead.h RadioHead/RadioHead.h --- RadioHead-orig/RadioHead.h 2018-11-15 11:40:24.000000000 +0100 -+++ RadioHead/RadioHead.h 2018-11-23 13:20:02.014133482 +0100 ++++ RadioHead/RadioHead.h 2018-11-24 03:40:03.395279378 +0100 @@ -317,8 +317,6 @@ cd /tmp mkdir RadioHead @@ -10,7 +10,7 @@ diff -Nur RadioHead-orig/RadioHead.h RadioHead/RadioHead.h cp /usr/local/projects/arduino/libraries/RadioHead/examples/cc110/cc110_client/cc110_client.pde application.cpp \endcode - Edit application.cpp and comment out any \#include so it looks like: -@@ -1283,11 +1281,13 @@ +@@ -1283,11 +1281,14 @@ #elif (RH_PLATFORM == RH_PLATFORM_GENERIC_AVR8) #include #include @@ -22,6 +22,7 @@ diff -Nur RadioHead-orig/RadioHead.h RadioHead/RadioHead.h - #include + #include + #include ++ #define RH_ATTACHINTERRUPT_TAKES_PIN_NUMBER // For Steve Childress port to ARM M4 w/CMSIS with STM's Hardware Abstraction lib. // See ArduinoWorkarounds.h (not supplied) diff --git a/lib/arduino-stub.cpp b/lib/arduino-stub.cpp index 201962f..38bc168 100644 --- a/lib/arduino-stub.cpp +++ b/lib/arduino-stub.cpp @@ -1006,6 +1006,41 @@ void detachInterrupt(uint8_t interruptNum) { } } +#define IMPLEMENT_ISR(vect, interrupt) \ + ISR(vect) { \ + intFunc[interrupt](); \ + } + +#if defined(__AVR_ATmega32U4__) + +IMPLEMENT_ISR(INT0_vect, EXTERNAL_INT_0) +IMPLEMENT_ISR(INT1_vect, EXTERNAL_INT_1) +IMPLEMENT_ISR(INT2_vect, EXTERNAL_INT_2) +IMPLEMENT_ISR(INT3_vect, EXTERNAL_INT_3) +IMPLEMENT_ISR(INT6_vect, EXTERNAL_INT_4) + +#elif defined(EICRA) && defined(EICRB) + +IMPLEMENT_ISR(INT0_vect, EXTERNAL_INT_2) +IMPLEMENT_ISR(INT1_vect, EXTERNAL_INT_3) +IMPLEMENT_ISR(INT2_vect, EXTERNAL_INT_4) +IMPLEMENT_ISR(INT3_vect, EXTERNAL_INT_5) +IMPLEMENT_ISR(INT4_vect, EXTERNAL_INT_0) +IMPLEMENT_ISR(INT5_vect, EXTERNAL_INT_1) +IMPLEMENT_ISR(INT6_vect, EXTERNAL_INT_6) +IMPLEMENT_ISR(INT7_vect, EXTERNAL_INT_7) + +#else + +IMPLEMENT_ISR(INT0_vect, EXTERNAL_INT_0) +IMPLEMENT_ISR(INT1_vect, EXTERNAL_INT_1) + +#if defined(EICRA) && defined(ISC20) +IMPLEMENT_ISR(INT2_vect, EXTERNAL_INT_2) +#endif + +#endif + // ****************** // this is from Arduino's WMath.cpp diff --git a/usb-lora/usb-lora.cpp b/usb-lora/usb-lora.cpp index d56946f..561f543 100644 --- a/usb-lora/usb-lora.cpp +++ b/usb-lora/usb-lora.cpp @@ -37,8 +37,8 @@ RH_RF95 lora; void recv_lora_msg() { - uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; - uint8_t len = sizeof(buf); + uint8_t buf[RH_RF95_MAX_MESSAGE_LEN+1]; + uint8_t len = sizeof(buf)-1; led2_on(); if(!lora.recv(buf, &len)) { @@ -46,10 +46,12 @@ void recv_lora_msg() led2_off(); return; } + buf[len] = 0; - printf("lora.recv() got message: SNR = %d, data = ", lora.lastSNR()); - for(uint8_t i = 0; i < len; ++i) printf("%s0x%02X", (i==0) ? "" : " ", buf[i]); - printf("\r\n"); + // printf("lora.recv() got message: SNR = %d, data = ", lora.lastSNR()); + // for(uint8_t i = 0; i < len; ++i) printf("%s0x%02X", (i==0) ? "" : " ", buf[i]); + // printf("\r\n"); + printf("lora.recv() got message: SNR = %d, data = %s\r\n", lora.lastSNR(), buf); led2_off(); } -- cgit v1.2.3 From 33f599bdb87e0604d2e3905b835c5f23b5e3b877 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 28 Nov 2018 18:11:29 +0100 Subject: added supported for arduino crypot lib --- .gitignore | 2 ++ contrib/Makefile | 18 ++++++++++++++++-- crypto.mk | 44 ++++++++++++++++++++++++++++++++++++++++++++ include.mk | 31 ++++++++++++++++++++++++++++++- lib/stdc++-minimal.cpp | 12 ++++++++++++ 5 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 crypto.mk diff --git a/.gitignore b/.gitignore index b336bc7..2d3a6f2 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,8 @@ contrib/BMP180-* contrib/vusb-* contrib/RadioHead-* contrib/RadioHead/ +contrib/.crypto.prepared +contrib/crypto contrib/.teensy-loader.prepared contrib/teensy_loader_cli contrib/.micronucleus.prepared diff --git a/contrib/Makefile b/contrib/Makefile index 9d606bb..eb86305 100644 --- a/contrib/Makefile +++ b/contrib/Makefile @@ -28,8 +28,8 @@ PJON_VERSION := 3.0 VUSB_VERSION := 20121206 RADIOHEAD_VERSION := 1.89 -all: download-lufa download-fastled download-pjon download-vusb download-radiohead update-teensy-loader update-micronucleus - +all: download-lufa download-fastled download-pjon download-vusb download-radiohead clone-crypto clone-teensy-loader clone-micronucleus +update-all: update-crypto update-teensy-loader update-micronucleus download-lufa: LUFA-${LUFA_VERSION}.zip @@ -68,10 +68,22 @@ RadioHead-${RADIOHEAD_VERSION}.zip: cd RadioHead; patch -p1 < ../radiohead.patch +.crypto.prepared: + git clone https://github.com/rweather/arduinolibs.git crypto + touch $@ + +clone-crypto: .crypto.prepared + +update-crypto: .crypto.prepared + cd crypto; git pull + + .teensy-loader.prepared: git clone https://github.com/PaulStoffregen/teensy_loader_cli.git touch $@ +clone-teensy-loader: .teensy-loader.prepared + update-teensy-loader: .teensy-loader.prepared cd teensy_loader_cli; git pull; make @@ -80,5 +92,7 @@ update-teensy-loader: .teensy-loader.prepared git clone https://github.com/micronucleus/micronucleus/ touch $@ +clone-micronucleus: .micronucleus.prepared + update-micronucleus: .micronucleus.prepared cd micronucleus/commandline; git pull; make diff --git a/crypto.mk b/crypto.mk new file mode 100644 index 0000000..f35102d --- /dev/null +++ b/crypto.mk @@ -0,0 +1,44 @@ +## +## spreadspace avr utils +## +## +## Copyright (C) 2013-2016 Christian Pointner +## +## This file is part of spreadspace avr utils. +## +## spreadspace avr utils is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## any later version. +## +## spreadspace avr utils is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with spreadspace avr utils. If not, see . +## + +include $(SPREADAVR_PATH)/defines.mk + +SRC:=$(wildcard $(CRYPTO_PATH)/libraries/Crypto/*.cpp) +DEPLIBS := stdc++-minimal + +CXXFLAGS += -I$(CRYPTO_PATH)/libraries/Crypto +CXXFLAGS += $(CRYPTO_OPTS) + +OBJ = $(SRC:%.cpp=%.o) +OBJ_DEPLIB = $(DEPLIBS:%=deplib-crypto--%.o) + +libcrypto.a: $(OBJ) $(OBJ_DEPLIB) + $(AR) $@ $(OBJ) $(OBJ_DEPLIB) + +%.o: %.cpp + $(CXX) -c $(CXXFLAGS) $< -o $@ + +deplib-crypto--%.o: $(LIB_DIR)/%.cpp + $(CXX) $(CXXFLAGS) -c $< -o $@ + +clean: + rm -f $(SRC:%.cpp=%.o) $(OBJ_DEPLIB) diff --git a/include.mk b/include.mk index d47827c..b1e3e0d 100644 --- a/include.mk +++ b/include.mk @@ -41,7 +41,7 @@ CXX_OBJ_LIB := $(CXX_LIBS:%=lib-%.o) CXX_SRC_LIB := $(CXX_LIBS:%=$(LIB_DIR)/%.cpp) CXX_DEP_LIB := $(CXX_LIBS:%=lib-%.d) -.PHONY: prepare clean clean-external distclean clean-lufa clean-fastled clean-pjon clean-vusb clean-radiohead program erase flash reset run +.PHONY: prepare clean clean-external distclean clean-lufa clean-fastled clean-pjon clean-vusb clean-radiohead clean-crypto program erase flash reset run ELFFILE := $(NAME).elf HEXFILE := $(NAME).hex @@ -89,6 +89,12 @@ CXXFLAGS += -DRH_PLATFORM=RH_PLATFORM_GENERIC_AVR8 CXXFLAGS += $(RADIOHEAD_OPTS) endif +ifdef CRYPTO_PATH +CXXFLAGS += -I$(CRYPTO_PATH)/libraries/Crypto +CXXFLAGS += -DUSES_CRYPTO +CXXFLAGS += $(CRYPTO_OPTS) +endif + prepare: $(EXTERNAL_LIBS:%=build-%) clean-external: $(EXTERNAL_LIBS:%=clean-%) @@ -209,6 +215,29 @@ clean-radiohead: @echo "****************************************************" +build-crypto: libcrypto.a + +libcrypto.a: Makefile + @echo "" + @echo "****************************************************" + @echo "building external crypto lib ($(CRYPTO_PATH))" + @echo "" + make -f $(SPREADAVR_PATH)/crypto.mk libcrypto.a + make -f $(SPREADAVR_PATH)/crypto.mk clean + @echo "" + @echo "****************************************************" + +clean-crypto: + @echo "" + @echo "****************************************************" + @echo "cleaning external crypto lib ($(CRYPTO_PATH))" + @echo "" + make -f $(SPREADAVR_PATH)/crypto.mk clean + rm -f libcrypto.a + @echo "" + @echo "****************************************************" + + ## project-specific objects %.d: %.c Makefile @set -e; rm -f $@; \ diff --git a/lib/stdc++-minimal.cpp b/lib/stdc++-minimal.cpp index c3b0559..982719b 100644 --- a/lib/stdc++-minimal.cpp +++ b/lib/stdc++-minimal.cpp @@ -39,3 +39,15 @@ void __cxa_deleted_virtual(void) { printf("PANIC: __cxa_deleted_virtual() got called ...\r\n"); for(;;); } + +void * operator new(size_t n) +{ + void * const p = malloc(n); + // handle p == 0 + return p; +} + +void operator delete(void * p) // or delete(void *, std::size_t) +{ + free(p); +} -- cgit v1.2.3 From c29aa83128d1762dcbd00e90aeaecaed367f4a6e Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 28 Nov 2018 18:13:53 +0100 Subject: added usb-crypto example --- usb-crypto/Makefile | 51 +++++++++++++++++++ usb-crypto/usb-crypto.cpp | 123 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 usb-crypto/Makefile create mode 100644 usb-crypto/usb-crypto.cpp diff --git a/usb-crypto/Makefile b/usb-crypto/Makefile new file mode 100644 index 0000000..d6e6444 --- /dev/null +++ b/usb-crypto/Makefile @@ -0,0 +1,51 @@ +## +## spreadspace avr utils +## +## +## Copyright (C) 2013-2016 Christian Pointner +## +## This file is part of spreadspace avr utils. +## +## spreadspace avr utils is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## any later version. +## +## spreadspace avr utils is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with spreadspace avr utils. If not, see . +## + +NAME := usb-crypto +BOARD_TYPE := teensy2 +CXX_OBJ := $(NAME).o +LIBS := util led lufa-descriptor-usbserial usbio +EXTERNAL_LIBS := lufa crypto +SPREADAVR_PATH := .. +RESET_FUNC := $(SPREADAVR_PATH)/tools/reset_lufa_cdc +RESET_PARAM := 'r' + +LUFA_PATH := $(SPREADAVR_PATH)/contrib/lufa-LUFA-151115 +LUFA_OPTS = -D USB_DEVICE_ONLY +LUFA_OPTS += -D DEVICE_STATE_AS_GPIOR=0 +LUFA_OPTS += -D ORDERED_EP_CONFIG +LUFA_OPTS += -D FIXED_CONTROL_ENDPOINT_SIZE=8 +LUFA_OPTS += -D FIXED_NUM_CONFIGURATIONS=1 +LUFA_OPTS += -D USE_FLASH_DESCRIPTORS +LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)" +LUFA_OPTS += -D INTERRUPT_CONTROL_ENDPOINT + +LUFA_OPTS += -D USB_MANUFACTURER="L\"equinox\"" +LUFA_OPTS += -D USB_PRODUCT="L\"spreadspace usb-crypto example\"" + +LUFA_COMPONENTS := USB USBCLASS + + +CRYPTO_PATH := $(SPREADAVR_PATH)/contrib/crypto + + +include $(SPREADAVR_PATH)/include.mk diff --git a/usb-crypto/usb-crypto.cpp b/usb-crypto/usb-crypto.cpp new file mode 100644 index 0000000..777b939 --- /dev/null +++ b/usb-crypto/usb-crypto.cpp @@ -0,0 +1,123 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2013-2016 Christian Pointner + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * spreadspace avr utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with spreadspace avr utils. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +#include "util.h" +#include "led.h" +#include "usbio.h" + +#include +#include + +ChaChaPoly cipher; + +void print_hex_dump(const uint8_t* data, size_t len) { + for(size_t i=0; i 0) { + int ReceivedByte = fgetc(stdin); + if(ReceivedByte != EOF) { + handle_cmd(ReceivedByte); + } + BytesReceived--; + } + + usbio_task(); + } +} -- cgit v1.2.3 From cd63149204c36b677c3175da8aab16ae43966b92 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 28 Nov 2018 20:47:51 +0100 Subject: rename external lib crypto to rweather-crypto --- .gitignore | 4 ++-- contrib/Makefile | 14 +++++++------- crypto.mk | 44 -------------------------------------------- include.mk | 28 ++++++++++++++-------------- rweather-crypto.mk | 44 ++++++++++++++++++++++++++++++++++++++++++++ usb-crypto/Makefile | 4 ++-- 6 files changed, 69 insertions(+), 69 deletions(-) delete mode 100644 crypto.mk create mode 100644 rweather-crypto.mk diff --git a/.gitignore b/.gitignore index 2d3a6f2..69a5435 100644 --- a/.gitignore +++ b/.gitignore @@ -13,8 +13,8 @@ contrib/BMP180-* contrib/vusb-* contrib/RadioHead-* contrib/RadioHead/ -contrib/.crypto.prepared -contrib/crypto +contrib/.rweather-crypto.prepared +contrib/rweather-crypto contrib/.teensy-loader.prepared contrib/teensy_loader_cli contrib/.micronucleus.prepared diff --git a/contrib/Makefile b/contrib/Makefile index eb86305..37eba4f 100644 --- a/contrib/Makefile +++ b/contrib/Makefile @@ -28,8 +28,8 @@ PJON_VERSION := 3.0 VUSB_VERSION := 20121206 RADIOHEAD_VERSION := 1.89 -all: download-lufa download-fastled download-pjon download-vusb download-radiohead clone-crypto clone-teensy-loader clone-micronucleus -update-all: update-crypto update-teensy-loader update-micronucleus +all: download-lufa download-fastled download-pjon download-vusb download-radiohead clone-rweather-crypto clone-teensy-loader clone-micronucleus +update-all: update-rweather-crypto update-teensy-loader update-micronucleus download-lufa: LUFA-${LUFA_VERSION}.zip @@ -68,14 +68,14 @@ RadioHead-${RADIOHEAD_VERSION}.zip: cd RadioHead; patch -p1 < ../radiohead.patch -.crypto.prepared: - git clone https://github.com/rweather/arduinolibs.git crypto +.rweather-crypto.prepared: + git clone https://github.com/rweather/arduinolibs.git rweather-crypto touch $@ -clone-crypto: .crypto.prepared +clone-rweather-crypto: .rweather-crypto.prepared -update-crypto: .crypto.prepared - cd crypto; git pull +update-rweather-crypto: .rweather-crypto.prepared + cd rweather-crypto; git pull .teensy-loader.prepared: diff --git a/crypto.mk b/crypto.mk deleted file mode 100644 index f35102d..0000000 --- a/crypto.mk +++ /dev/null @@ -1,44 +0,0 @@ -## -## spreadspace avr utils -## -## -## Copyright (C) 2013-2016 Christian Pointner -## -## This file is part of spreadspace avr utils. -## -## spreadspace avr utils is free software: you can redistribute it and/or modify -## it under the terms of the GNU General Public License as published by -## the Free Software Foundation, either version 3 of the License, or -## any later version. -## -## spreadspace avr utils is distributed in the hope that it will be useful, -## but WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -## GNU General Public License for more details. -## -## You should have received a copy of the GNU General Public License -## along with spreadspace avr utils. If not, see . -## - -include $(SPREADAVR_PATH)/defines.mk - -SRC:=$(wildcard $(CRYPTO_PATH)/libraries/Crypto/*.cpp) -DEPLIBS := stdc++-minimal - -CXXFLAGS += -I$(CRYPTO_PATH)/libraries/Crypto -CXXFLAGS += $(CRYPTO_OPTS) - -OBJ = $(SRC:%.cpp=%.o) -OBJ_DEPLIB = $(DEPLIBS:%=deplib-crypto--%.o) - -libcrypto.a: $(OBJ) $(OBJ_DEPLIB) - $(AR) $@ $(OBJ) $(OBJ_DEPLIB) - -%.o: %.cpp - $(CXX) -c $(CXXFLAGS) $< -o $@ - -deplib-crypto--%.o: $(LIB_DIR)/%.cpp - $(CXX) $(CXXFLAGS) -c $< -o $@ - -clean: - rm -f $(SRC:%.cpp=%.o) $(OBJ_DEPLIB) diff --git a/include.mk b/include.mk index b1e3e0d..5c14178 100644 --- a/include.mk +++ b/include.mk @@ -41,7 +41,7 @@ CXX_OBJ_LIB := $(CXX_LIBS:%=lib-%.o) CXX_SRC_LIB := $(CXX_LIBS:%=$(LIB_DIR)/%.cpp) CXX_DEP_LIB := $(CXX_LIBS:%=lib-%.d) -.PHONY: prepare clean clean-external distclean clean-lufa clean-fastled clean-pjon clean-vusb clean-radiohead clean-crypto program erase flash reset run +.PHONY: prepare clean clean-external distclean clean-lufa clean-fastled clean-pjon clean-vusb clean-radiohead clean-rweather-crypto program erase flash reset run ELFFILE := $(NAME).elf HEXFILE := $(NAME).hex @@ -89,10 +89,10 @@ CXXFLAGS += -DRH_PLATFORM=RH_PLATFORM_GENERIC_AVR8 CXXFLAGS += $(RADIOHEAD_OPTS) endif -ifdef CRYPTO_PATH -CXXFLAGS += -I$(CRYPTO_PATH)/libraries/Crypto -CXXFLAGS += -DUSES_CRYPTO -CXXFLAGS += $(CRYPTO_OPTS) +ifdef RWEATHER_CRYPTO_PATH +CXXFLAGS += -I$(RWEATHER_CRYPTO_PATH)/libraries/Crypto +CXXFLAGS += -DUSES_RWEATHER_CRYPTO +CXXFLAGS += $(RWEATHER_CRYPTO_OPTS) endif prepare: $(EXTERNAL_LIBS:%=build-%) @@ -215,25 +215,25 @@ clean-radiohead: @echo "****************************************************" -build-crypto: libcrypto.a +build-rweather-crypto: librweather-crypto.a -libcrypto.a: Makefile +librweather-crypto.a: Makefile @echo "" @echo "****************************************************" - @echo "building external crypto lib ($(CRYPTO_PATH))" + @echo "building external rweather/crypto lib ($(RWEATHER_CRYPTO_PATH))" @echo "" - make -f $(SPREADAVR_PATH)/crypto.mk libcrypto.a - make -f $(SPREADAVR_PATH)/crypto.mk clean + make -f $(SPREADAVR_PATH)/rweather-crypto.mk librweather-crypto.a + make -f $(SPREADAVR_PATH)/rweather-crypto.mk clean @echo "" @echo "****************************************************" -clean-crypto: +clean-rweather-crypto: @echo "" @echo "****************************************************" - @echo "cleaning external crypto lib ($(CRYPTO_PATH))" + @echo "cleaning external rweather/crypto lib ($(RWEATHER_CRYPTO_PATH))" @echo "" - make -f $(SPREADAVR_PATH)/crypto.mk clean - rm -f libcrypto.a + make -f $(SPREADAVR_PATH)/rweather-crypto.mk clean + rm -f librweather-crypto.a @echo "" @echo "****************************************************" diff --git a/rweather-crypto.mk b/rweather-crypto.mk new file mode 100644 index 0000000..1d39ecc --- /dev/null +++ b/rweather-crypto.mk @@ -0,0 +1,44 @@ +## +## spreadspace avr utils +## +## +## Copyright (C) 2013-2016 Christian Pointner +## +## This file is part of spreadspace avr utils. +## +## spreadspace avr utils is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## any later version. +## +## spreadspace avr utils is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with spreadspace avr utils. If not, see . +## + +include $(SPREADAVR_PATH)/defines.mk + +SRC:=$(wildcard $(RWEATHER_CRYPTO_PATH)/libraries/Crypto/*.cpp) +DEPLIBS := stdc++-minimal + +CXXFLAGS += -I$(RWEATHER_CRYPTO_PATH)/libraries/Crypto +CXXFLAGS += $(RWEATHER_CRYPTO_OPTS) + +OBJ = $(SRC:%.cpp=%.o) +OBJ_DEPLIB = $(DEPLIBS:%=deplib-rweather-crypto--%.o) + +librweather-crypto.a: $(OBJ) $(OBJ_DEPLIB) + $(AR) $@ $(OBJ) $(OBJ_DEPLIB) + +%.o: %.cpp + $(CXX) -c $(CXXFLAGS) $< -o $@ + +deplib-rweather-crypto--%.o: $(LIB_DIR)/%.cpp + $(CXX) $(CXXFLAGS) -c $< -o $@ + +clean: + rm -f $(SRC:%.cpp=%.o) $(OBJ_DEPLIB) diff --git a/usb-crypto/Makefile b/usb-crypto/Makefile index d6e6444..bae846a 100644 --- a/usb-crypto/Makefile +++ b/usb-crypto/Makefile @@ -24,7 +24,7 @@ NAME := usb-crypto BOARD_TYPE := teensy2 CXX_OBJ := $(NAME).o LIBS := util led lufa-descriptor-usbserial usbio -EXTERNAL_LIBS := lufa crypto +EXTERNAL_LIBS := lufa rweather-crypto SPREADAVR_PATH := .. RESET_FUNC := $(SPREADAVR_PATH)/tools/reset_lufa_cdc RESET_PARAM := 'r' @@ -45,7 +45,7 @@ LUFA_OPTS += -D USB_PRODUCT="L\"spreadspace usb-crypto example\"" LUFA_COMPONENTS := USB USBCLASS -CRYPTO_PATH := $(SPREADAVR_PATH)/contrib/crypto +RWEATHER_CRYPTO_PATH := $(SPREADAVR_PATH)/contrib/rweather-crypto include $(SPREADAVR_PATH)/include.mk -- cgit v1.2.3 From 753f1318c69bdb56145db1a783e54593ad729f72 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 28 Nov 2018 20:50:54 +0100 Subject: added decrypt example in python --- usb-crypto/decrypt.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 usb-crypto/decrypt.py diff --git a/usb-crypto/decrypt.py b/usb-crypto/decrypt.py new file mode 100755 index 0000000..7f7151a --- /dev/null +++ b/usb-crypto/decrypt.py @@ -0,0 +1,36 @@ +#!/usr/bin/python +# +# spreadspace avr utils +# +# +# Copyright (C) 2013-2016 Christian Pointner +# +# This file is part of spreadspace avr utils. +# +# spreadspace avr utils is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# any later version. +# +# spreadspace avr utils is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with spreadspace avr utils. If not, see . +# + +import binascii +from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305 + +hdr = "hello world!" +body_enc = binascii.unhexlify("5535BB923FB4BDCA401D3AE05C85FF314BD22423FE339CD00259551AECA534FABBEB16B39CC9AC14DF") + +key = binascii.unhexlify("7043b69bde20446661ba579e83fda0830e3e61c95b5ac8deeb79973ba0df02d8") +iv = binascii.unhexlify("6fac1c6a94a5788761cf9ecd") + +cipher = ChaCha20Poly1305(key) +msg = cipher.decrypt(iv, body_enc, hdr) + +print(str(msg)) -- cgit v1.2.3 From 2154d2e53d716a21346a072db3ea2da2544bbc96 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 28 Nov 2018 21:40:43 +0100 Subject: added decryption example --- usb-crypto/usb-crypto.cpp | 58 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/usb-crypto/usb-crypto.cpp b/usb-crypto/usb-crypto.cpp index 777b939..0f7a9be 100644 --- a/usb-crypto/usb-crypto.cpp +++ b/usb-crypto/usb-crypto.cpp @@ -44,8 +44,11 @@ void print_hex_dump(const uint8_t* data, size_t len) { printf("\r\n"); } -void encrypt() { +void run_crypto() +{ + printf("running encrypt/decrypt test using ChaCha20Poly1305:\r\n"); + printf("\r\nencrypting...\r\n"); uint8_t hdr[] = "hello world!"; size_t hdr_len = sizeof(hdr)-1; uint8_t body[] = "this is a secret message."; @@ -59,6 +62,12 @@ void encrypt() { uint8_t iv[] = {0x6f, 0xac, 0x1c, 0x6a, 0x94, 0xa5, 0x78, 0x87, 0x61, 0xcf, 0x9e, 0xcd}; + uint8_t buf[256]; + uint8_t tag[16]; + memset(buf, 0, sizeof(buf)); + memset(tag, 0, sizeof(tag)); + + led_on(); cipher.clear(); if(!cipher.setKey(key, sizeof(key))) { printf("failed to set key\r\n"); @@ -68,34 +77,59 @@ void encrypt() { printf("failed to set iv\r\n"); return; } + led_off(); - uint8_t buf[256]; - uint8_t tag[16]; - memset(buf, 0, sizeof(buf)); - memset(tag, 0, sizeof(tag)); - + led_on(); cipher.addAuthData(hdr, hdr_len); cipher.encrypt(buf, body, body_len); cipher.computeTag(tag, sizeof(tag)); + led_off(); printf("encrypted data (%d bytes):\r\n", body_len); print_hex_dump(buf, body_len); printf("\r\n"); printf("auth tag (%d bytes):\r\n", sizeof(tag)); print_hex_dump(tag, sizeof(tag)); + + printf("\r\ndecrypting...\r\n"); + memcpy(body, buf, body_len); + memset(buf, 0, sizeof(buf)); + + led_on(); + cipher.clear(); + if(!cipher.setKey(key, sizeof(key))) { + printf("failed to set key\r\n"); + return; + } + if(!cipher.setIV(iv, sizeof(iv))) { + printf("failed to set iv\r\n"); + return; + } + led_off(); + + led_on(); + cipher.addAuthData(hdr, hdr_len); + cipher.decrypt(buf, body, body_len); + bool ct = cipher.checkTag(tag, sizeof(tag)); + led_off(); + + if(!ct) { + printf("auth tag mismatch!\r\n"); + return; + } else { + printf("auth tag correct!\r\n"); + } + printf("decrypted body: '%s'\r\n", buf); } void handle_cmd(uint8_t cmd) { switch(cmd) { - case 'e': encrypt(); return; - case '0': led_off(); break; - case '1': led_on(); break; - case 't': led_toggle(); break; + case 'c': run_crypto(); break; case 'r': reset2bootloader(); break; - default: printf("error\r\n"); return; + default: printf("unknown command"); break; } - printf("ok\r\n"); + printf("\r\n"); } int main(void) -- cgit v1.2.3 From 21d747167d1091985438f6a2c954cdc895031c07 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 29 Nov 2018 00:03:23 +0100 Subject: radiohead with crypto support --- contrib/radiohead.patch | 279 +++++++++++++++++++++++++++++++++++- include.mk | 3 + radiohead.mk | 13 +- usb-lora-crypto/Makefile | 52 +++++++ usb-lora-crypto/usb-lora-crypto.cpp | 118 +++++++++++++++ 5 files changed, 461 insertions(+), 4 deletions(-) create mode 100644 usb-lora-crypto/Makefile create mode 100644 usb-lora-crypto/usb-lora-crypto.cpp diff --git a/contrib/radiohead.patch b/contrib/radiohead.patch index a7040a6..1636f91 100644 --- a/contrib/radiohead.patch +++ b/contrib/radiohead.patch @@ -1,6 +1,6 @@ diff -Nur RadioHead-orig/RadioHead.h RadioHead/RadioHead.h --- RadioHead-orig/RadioHead.h 2018-11-15 11:40:24.000000000 +0100 -+++ RadioHead/RadioHead.h 2018-11-24 03:40:03.395279378 +0100 ++++ RadioHead/RadioHead.h 2018-11-28 23:58:44.998151593 +0100 @@ -317,8 +317,6 @@ cd /tmp mkdir RadioHead @@ -26,3 +26,280 @@ diff -Nur RadioHead-orig/RadioHead.h RadioHead/RadioHead.h // For Steve Childress port to ARM M4 w/CMSIS with STM's Hardware Abstraction lib. // See ArduinoWorkarounds.h (not supplied) +diff -Nur RadioHead-orig/RHAEADDriver.cpp RadioHead/RHAEADDriver.cpp +--- RadioHead-orig/RHAEADDriver.cpp 1970-01-01 01:00:00.000000000 +0100 ++++ RadioHead/RHAEADDriver.cpp 2018-11-28 23:59:30.373774012 +0100 +@@ -0,0 +1,48 @@ ++// RHAEADDriver.cpp ++// ++// Author: Philippe.Rochat'at'gmail.com ++// Contributed to the RadioHead project by the author ++// $Id: RHAEADDriver.cpp,v 1.4 2018/09/23 23:54:01 mikem Exp $ ++ ++#include ++#ifdef RH_ENABLE_ENCRYPTION_MODULE ++ ++RHAEADDriver::RHAEADDriver(RHGenericDriver& driver, AuthenticatedCipher& aead) ++ : _driver(driver), ++ _aead(aead) ++{ ++ _buffer = (uint8_t *)calloc(_driver.maxMessageLength(), sizeof(uint8_t)); ++} ++ ++bool RHAEADDriver::recv(uint8_t* buf, uint8_t* len) ++{ ++ bool status = _driver.recv(_buffer, len); ++ if (status && buf && len) { ++ // TODO: implement this ++ } ++ return status; ++} ++ ++bool RHAEADDriver::send(const uint8_t* data, uint8_t len) ++{ ++ if (len > maxMessageLength()) ++ return false; ++ ++ bool status = true; ++ if (len == 0) // PassThru ++ return _driver.send(data, len); ++ ++ uint8_t out_len = 0; ++ // TODO: implement this ++ return _driver.send(_buffer, out_len); ++} ++ ++uint8_t RHAEADDriver::maxMessageLength() ++{ ++ int driver_len = _driver.maxMessageLength(); ++ // IV? ++ driver_len -= _aead.tagSize(); ++ return driver_len; ++} ++ ++#endif +diff -Nur RadioHead-orig/RHAEADDriver.h RadioHead/RHAEADDriver.h +--- RadioHead-orig/RHAEADDriver.h 1970-01-01 01:00:00.000000000 +0100 ++++ RadioHead/RHAEADDriver.h 2018-11-28 23:59:09.441948151 +0100 +@@ -0,0 +1,221 @@ ++// RHAEADDriver.h ++ ++// Generic encryption and authentication layer that could use any driver ++// But will encrypt and authenticate all data. ++// Requires the Arduinolibs/Crypto library: ++// https://github.com/rweather/arduinolibs ++// ++// Author: Christain Pointner equinox'at'spreadspace.org ++// Contributed to the RadioHead project by the author ++ ++#ifndef RHAEADDriver_h ++#define RHAEADDriver_h ++ ++#include ++#ifdef RH_ENABLE_ENCRYPTION_MODULE ++#include ++ ++///////////////////////////////////////////////////////////////////// ++/// \class RHAEADDriver RHAEADDriver ++/// \brief Virtual Driver to encrypt and authenticate data. Can be used with any other RadioHead driver. ++/// ++/// This driver acts as a wrapper for any other RadioHead driver, adding encryption and authentication of ++/// messages that are passed to and from the actual radio driver. Only the message payload is encrypted but ++/// the to/from address or flags is part of the authenticated portion. Any of the authenticated-ciphers ++/// supported by ArduinoLibs Cryptographic Library http://rweather.github.io/arduinolibs/crypto.html may be used. ++/// ++/// For successful communications, both sender and receiver must use the same cipher and the same key. ++/// ++/// In order to enable this module you must uncomment #define RH_ENABLE_AEAD_MODULE at the bottom of RadioHead.h ++/// But ensure you have installed the Crypto directory from arduinolibs first: ++/// http://rweather.github.io/arduinolibs/index.html ++ ++class RHAEADDriver : public RHGenericDriver ++{ ++public: ++ /// Constructor. ++ /// Adds a ciphering layer to messages sent and received by the actual transport driver. ++ /// \param[in] driver The RadioHead driver to use to transport messages. ++ /// \param[in] blockcipher The blockcipher (from arduinolibs) that crypt/decrypt data. Ensure that ++ /// the blockcipher has had its key set before sending or receiving messages. ++ RHAEADDriver(RHGenericDriver& driver, AuthenticatedCipher& aead); ++ ++ /// Calls the real driver's init() ++ /// \return The value returned from the driver init() method; ++ virtual bool init() { return _driver.init();}; ++ ++ /// Tests whether a new message is available ++ /// from the Driver. ++ /// On most drivers, this will also put the Driver into RHModeRx mode until ++ /// a message is actually received by the transport, when it wil be returned to RHModeIdle. ++ /// This can be called multiple times in a timeout loop ++ /// \return true if a new, complete, error-free uncollected message is available to be retreived by recv() ++ virtual bool available() { return _driver.available();}; ++ ++ /// Turns the receiver on if it not already on. ++ /// If there is a valid message available, copy it to buf and return true ++ /// else return false. ++ /// If a message is copied, *len is set to the length (Caution, 0 length messages are permitted). ++ /// You should be sure to call this function frequently enough to not miss any messages ++ /// It is recommended that you call it in your main loop. ++ /// \param[in] buf Location to copy the received message ++ /// \param[in,out] len Pointer to available space in buf. Set to the actual number of octets copied. ++ /// \return true if a valid message was copied to buf ++ virtual bool recv(uint8_t* buf, uint8_t* len); ++ ++ /// Waits until any previous transmit packet is finished being transmitted with waitPacketSent(). ++ /// Then optionally waits for Channel Activity Detection (CAD) ++ /// to show the channnel is clear (if the radio supports CAD) by calling waitCAD(). ++ /// Then loads a message into the transmitter and starts the transmitter. Note that a message length ++ /// of 0 is permitted. ++ /// \param[in] data Array of data to be sent ++ /// \param[in] len Number of bytes of data to send ++ /// specify the maximum time in ms to wait. If 0 (the default) do not wait for CAD before transmitting. ++ /// \return true if the message length was valid and it was correctly queued for transmit. Return false ++ /// if CAD was requested and the CAD timeout timed out before clear channel was detected. ++ virtual bool send(const uint8_t* data, uint8_t len); ++ ++ /// Returns the maximum message length ++ /// available in this Driver, which depends on the maximum length supported by the underlying transport driver. ++ /// \return The maximum legal message length ++ virtual uint8_t maxMessageLength(); ++ ++ /// Blocks until the transmitter ++ /// is no longer transmitting. ++ virtual bool waitPacketSent() { return _driver.waitPacketSent();} ; ++ ++ /// Blocks until the transmitter is no longer transmitting. ++ /// or until the timeout occuers, whichever happens first ++ /// \param[in] timeout Maximum time to wait in milliseconds. ++ /// \return true if the radio completed transmission within the timeout period. False if it timed out. ++ virtual bool waitPacketSent(uint16_t timeout) {return _driver.waitPacketSent(timeout);} ; ++ ++ /// Starts the receiver and blocks until a received message is available or a timeout ++ /// \param[in] timeout Maximum time to wait in milliseconds. ++ /// \return true if a message is available ++ virtual bool waitAvailableTimeout(uint16_t timeout) {return _driver.waitAvailableTimeout(timeout);}; ++ ++ /// Calls the waitCAD method in the driver ++ /// \return The return value from teh drivers waitCAD() method ++ virtual bool waitCAD() { return _driver.waitCAD();}; ++ ++ /// Sets the Channel Activity Detection timeout in milliseconds to be used by waitCAD(). ++ /// The default is 0, which means do not wait for CAD detection. ++ /// CAD detection depends on support for isChannelActive() by your particular radio. ++ void setCADTimeout(unsigned long cad_timeout) {_driver.setCADTimeout(cad_timeout);}; ++ ++ /// Determine if the currently selected radio channel is active. ++ /// This is expected to be subclassed by specific radios to implement their Channel Activity Detection ++ /// if supported. If the radio does not support CAD, returns true immediately. If a RadioHead radio ++ /// supports isChannelActive() it will be documented in the radio specific documentation. ++ /// This is called automatically by waitCAD(). ++ /// \return true if the radio-specific CAD (as returned by override of isChannelActive()) shows the ++ /// current radio channel as active, else false. If there is no radio-specific CAD, returns false. ++ virtual bool isChannelActive() { return _driver.isChannelActive();}; ++ ++ /// Sets the address of this node. Defaults to 0xFF. Subclasses or the user may want to change this. ++ /// This will be used to test the adddress in incoming messages. In non-promiscuous mode, ++ /// only messages with a TO header the same as thisAddress or the broadcast addess (0xFF) will be accepted. ++ /// In promiscuous mode, all messages will be accepted regardless of the TO header. ++ /// In a conventional multinode system, all nodes will have a unique address ++ /// (which you could store in EEPROM). ++ /// You would normally set the header FROM address to be the same as thisAddress (though you dont have to, ++ /// allowing the possibilty of address spoofing). ++ /// \param[in] thisAddress The address of this node. ++ virtual void setThisAddress(uint8_t thisAddress) { _driver.setThisAddress(thisAddress);}; ++ ++ /// Sets the TO header to be sent in all subsequent messages ++ /// \param[in] to The new TO header value ++ virtual void setHeaderTo(uint8_t to){ _driver.setHeaderTo(to);}; ++ ++ /// Sets the FROM header to be sent in all subsequent messages ++ /// \param[in] from The new FROM header value ++ virtual void setHeaderFrom(uint8_t from){ _driver.setHeaderFrom(from);}; ++ ++ /// Sets the ID header to be sent in all subsequent messages ++ /// \param[in] id The new ID header value ++ virtual void setHeaderId(uint8_t id){ _driver.setHeaderId(id);}; ++ ++ /// Sets and clears bits in the FLAGS header to be sent in all subsequent messages ++ /// First it clears he FLAGS according to the clear argument, then sets the flags according to the ++ /// set argument. The default for clear always clears the application specific flags. ++ /// \param[in] set bitmask of bits to be set. Flags are cleared with the clear mask before being set. ++ /// \param[in] clear bitmask of flags to clear. Defaults to RH_FLAGS_APPLICATION_SPECIFIC ++ /// which clears the application specific flags, resulting in new application specific flags ++ /// identical to the set. ++ virtual void setHeaderFlags(uint8_t set, uint8_t clear = RH_FLAGS_APPLICATION_SPECIFIC) { _driver.setHeaderFlags(set, clear);}; ++ ++ /// Tells the receiver to accept messages with any TO address, not just messages ++ /// addressed to thisAddress or the broadcast address ++ /// \param[in] promiscuous true if you wish to receive messages with any TO address ++ virtual void setPromiscuous(bool promiscuous){ _driver.setPromiscuous(promiscuous);}; ++ ++ /// Returns the TO header of the last received message ++ /// \return The TO header ++ virtual uint8_t headerTo() { return _driver.headerTo();}; ++ ++ /// Returns the FROM header of the last received message ++ /// \return The FROM header ++ virtual uint8_t headerFrom() { return _driver.headerFrom();}; ++ ++ /// Returns the ID header of the last received message ++ /// \return The ID header ++ virtual uint8_t headerId() { return _driver.headerId();}; ++ ++ /// Returns the FLAGS header of the last received message ++ /// \return The FLAGS header ++ virtual uint8_t headerFlags() { return _driver.headerFlags();}; ++ ++ /// Returns the most recent RSSI (Receiver Signal Strength Indicator). ++ /// Usually it is the RSSI of the last received message, which is measured when the preamble is received. ++ /// If you called readRssi() more recently, it will return that more recent value. ++ /// \return The most recent RSSI measurement in dBm. ++ int16_t lastRssi() { return _driver.lastRssi();}; ++ ++ /// Returns the operating mode of the library. ++ /// \return the current mode, one of RF69_MODE_* ++ RHMode mode() { return _driver.mode();}; ++ ++ /// Sets the operating mode of the transport. ++ void setMode(RHMode mode) { _driver.setMode(mode);}; ++ ++ /// Sets the transport hardware into low-power sleep mode ++ /// (if supported). May be overridden by specific drivers to initialte sleep mode. ++ /// If successful, the transport will stay in sleep mode until woken by ++ /// changing mode it idle, transmit or receive (eg by calling send(), recv(), available() etc) ++ /// \return true if sleep mode is supported by transport hardware and the RadioHead driver, and if sleep mode ++ /// was successfully entered. If sleep mode is not suported, return false. ++ virtual bool sleep() { return _driver.sleep();}; ++ ++ /// Returns the count of the number of bad received packets (ie packets with bad lengths, checksum etc) ++ /// which were rejected and not delivered to the application. ++ /// Caution: not all drivers can correctly report this count. Some underlying hardware only report ++ /// good packets. ++ /// \return The number of bad packets received. ++ virtual uint16_t rxBad() { return _driver.rxBad();}; ++ ++ /// Returns the count of the number of ++ /// good received packets ++ /// \return The number of good packets received. ++ virtual uint16_t rxGood() { return _driver.rxGood();}; ++ ++ /// Returns the count of the number of ++ /// packets successfully transmitted (though not necessarily received by the destination) ++ /// \return The number of packets successfully transmitted ++ virtual uint16_t txGood() { return _driver.txGood();}; ++ ++private: ++ /// The underlying transport river we are to use ++ RHGenericDriver& _driver; ++ ++ /// The AuthenticatedCipher we are to use for encrypting/decrypting ++ AuthenticatedCipher& _aead; ++ ++ /// Buffer to store encrypted/decrypted message ++ uint8_t* _buffer; ++}; ++ ++ ++ ++#endif ++#endif diff --git a/include.mk b/include.mk index 5c14178..d0f526e 100644 --- a/include.mk +++ b/include.mk @@ -86,6 +86,9 @@ ifdef RADIOHEAD_PATH CXXFLAGS += -I$(RADIOHEAD_PATH) CXXFLAGS += -DUSES_RADIOHEAD CXXFLAGS += -DRH_PLATFORM=RH_PLATFORM_GENERIC_AVR8 +ifdef RWEATHER_CRYPTO_PATH +CXXFLAGS += -DRH_ENABLE_ENCRYPTION_MODULE +endif CXXFLAGS += $(RADIOHEAD_OPTS) endif diff --git a/radiohead.mk b/radiohead.mk index 6352d5e..55fe943 100644 --- a/radiohead.mk +++ b/radiohead.mk @@ -25,13 +25,20 @@ include $(SPREADAVR_PATH)/defines.mk # TODO: add other components dependent on driver... RADIOHEAD_COMPONENTS := RH_$(RADIOHEAD_DRIVER) RHSPIDriver RHGenericDriver RHGenericSPI RHHardwareSPI -SRC:=$(foreach COMP,$(RADIOHEAD_COMPONENTS),$(RADIOHEAD_PATH)/$(COMP).cpp) -DEPLIBS := Arduino-SPI arduino-stub stdc++-minimal - CXXFLAGS += -I$(RADIOHEAD_PATH) CXXFLAGS += -DRH_PLATFORM=RH_PLATFORM_GENERIC_AVR8 CXXFLAGS += $(RADIOHEAD_OPTS) +ifdef RWEATHER_CRYPTO_PATH +CXXFLAGS += -DRH_ENABLE_ENCRYPTION_MODULE +CXXFLAGS += -I$(RWEATHER_CRYPTO_PATH)/libraries/Crypto +CXXFLAGS += $(RWEATHER_CRYPTO_OPTS) +RADIOHEAD_COMPONENTS += RHAEADDriver # RHEncryptedDriver +endif + +SRC:=$(foreach COMP,$(RADIOHEAD_COMPONENTS),$(RADIOHEAD_PATH)/$(COMP).cpp) +DEPLIBS := Arduino-SPI arduino-stub stdc++-minimal + OBJ = $(SRC:%.cpp=%.o) OBJ_DEPLIB = $(DEPLIBS:%=deplib-radiohead--%.o) diff --git a/usb-lora-crypto/Makefile b/usb-lora-crypto/Makefile new file mode 100644 index 0000000..0b61c94 --- /dev/null +++ b/usb-lora-crypto/Makefile @@ -0,0 +1,52 @@ +## +## spreadspace avr utils +## +## +## Copyright (C) 2013-2018 Christian Pointner +## +## This file is part of spreadspace avr utils. +## +## spreadspace avr utils is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## any later version. +## +## spreadspace avr utils is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with spreadspace avr utils. If not, see . +## + +NAME := usb-lora-crypto +BOARD_TYPE := elecrow32u4lora +CXX_OBJ := $(NAME).o +LIBS := util led lufa-descriptor-usbserial usbio +EXTERNAL_LIBS := lufa radiohead rweather-crypto +SPREADAVR_PATH := .. + +LUFA_PATH := $(SPREADAVR_PATH)/contrib/lufa-LUFA-151115 +LUFA_OPTS = -D USB_DEVICE_ONLY +LUFA_OPTS += -D DEVICE_STATE_AS_GPIOR=0 +LUFA_OPTS += -D ORDERED_EP_CONFIG +LUFA_OPTS += -D FIXED_CONTROL_ENDPOINT_SIZE=8 +LUFA_OPTS += -D FIXED_NUM_CONFIGURATIONS=1 +LUFA_OPTS += -D USE_FLASH_DESCRIPTORS +LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)" +LUFA_OPTS += -D INTERRUPT_CONTROL_ENDPOINT + +LUFA_OPTS += -D USB_MANUFACTURER="L\"equinox\"" +LUFA_OPTS += -D USB_PRODUCT="L\"spreadspace usb-lora-crypto example\"" + +LUFA_COMPONENTS := USB USBCLASS + + +RADIOHEAD_PATH := $(SPREADAVR_PATH)/contrib/RadioHead + +RADIOHEAD_DRIVER := RF95 + +RWEATHER_CRYPTO_PATH := $(SPREADAVR_PATH)/contrib/rweather-crypto + +include $(SPREADAVR_PATH)/include.mk diff --git a/usb-lora-crypto/usb-lora-crypto.cpp b/usb-lora-crypto/usb-lora-crypto.cpp new file mode 100644 index 0000000..3af1dcb --- /dev/null +++ b/usb-lora-crypto/usb-lora-crypto.cpp @@ -0,0 +1,118 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2013-2016 Christian Pointner + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * spreadspace avr utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with spreadspace avr utils. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "util.h" +#include "led.h" +#include "usbio.h" + +#include "RadioHead.h" +#include "RH_RF95.h" +#include +#include +#include "RHAEADDriver.h" + +ChaChaPoly cipher; +RH_RF95 lora; +RHAEADDriver aead(lora, cipher); + +void recv_aead_msg() +{ + uint8_t buf[RH_RF95_MAX_MESSAGE_LEN+1]; + uint8_t len = sizeof(buf)-1; + + led2_on(); + if(!aead.recv(buf, &len)) { + printf("aead.recv(): failed\r\n"); + led2_off(); + return; + } + buf[len] = 0; + + printf("aead.recv() got message: RSSI = %d, data = ", aead.lastRssi()); + for(uint8_t i = 0; i < len; ++i) printf("%s0x%02X", (i==0) ? "" : " ", buf[i]); + printf("\r\n"); + // printf("aead.recv() got message: RSSI = %d, data = %s\r\n", aead.lastRssi(), buf); + led2_off(); +} + +void send_aead_msg() +{ + uint8_t data[] = "spreadspace.org/avr-utils usb-aead test using radiohead library"; + printf("aead: sending message with %d bytes\r\n", sizeof(data)); + + led_on(); + if(!aead.send(data, sizeof(data))) { + printf("aead.send(): failed\r\n"); + led_off(); + return; + } + aead.waitPacketSent(); + printf("aead.send(): success\r\n"); + led_off(); +} + +void handle_cmd(uint8_t cmd) +{ + switch(cmd) { + case 's': send_aead_msg(); break; + default: printf("error\r\n"); return; + } +} + +int main(void) +{ + MCUSR &= ~(1 << WDRF); + wdt_disable(); + + cpu_init(); + led_init(); + usbio_init(); + arduino_init(); + sei(); + + lora.init(); + lora.setFrequency(868.0); + aead.init(); + + for(;;) { + if(aead.available()) { + recv_aead_msg(); + } + + int16_t BytesReceived = usbio_bytes_received(); + while(BytesReceived > 0) { + int ReceivedByte = fgetc(stdin); + if(ReceivedByte != EOF) { + handle_cmd(ReceivedByte); + } + BytesReceived--; + } + + usbio_task(); + } +} -- cgit v1.2.3 From 1d819db348caaed63b9b308a7154fa8b77fe3cdc Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 29 Nov 2018 02:34:43 +0100 Subject: remove -fpack-struct since this does nothing on AVR anyway (8bit platform) --- defines.mk | 1 - 1 file changed, 1 deletion(-) diff --git a/defines.mk b/defines.mk index cec3dd5..3cb82db 100644 --- a/defines.mk +++ b/defines.mk @@ -285,7 +285,6 @@ COMMON += -funsigned-char COMMON += -funsigned-bitfields COMMON += -fdata-sections COMMON += -ffunction-sections -COMMON += -fpack-struct COMMON += -fshort-enums COMMON += -Wall COMMON += -I$(LIB_DIR) -- cgit v1.2.3 From 39f70772e05892ba97f78e6a375ab4a6b1930ccc Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 29 Nov 2018 22:40:15 +0100 Subject: added feather32u4lora board --- defines.mk | 13 +++++++++++++ lib/led.c | 4 ++-- usb-lora/Makefile | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/defines.mk b/defines.mk index 3cb82db..f04496f 100644 --- a/defines.mk +++ b/defines.mk @@ -145,6 +145,19 @@ ifeq ($(BOARD_TYPE),feather32u4) PROG_TYPE := avr109 AVRDUDE_PORT := /dev/ttyACM0 LUFA_BOARD = NONE + ARDUINO_PINS = leonardo +endif +ifeq ($(BOARD_TYPE),feather32u4lora) + MCU := atmega32u4 + ARCH = AVR8 + F_CPU := 8000000 + F_USB = $(F_CPU) + PROG := avrdude + UPLOAD_RATE := 57600 + PROG_TYPE := avr109 + AVRDUDE_PORT := /dev/ttyACM0 + LUFA_BOARD = NONE + ARDUINO_PINS = leonardo endif ifeq ($(BOARD_TYPE),elecrow32u4lora) MCU := atmega32u4 diff --git a/lib/led.c b/lib/led.c index 192eb68..3949623 100644 --- a/lib/led.c +++ b/lib/led.c @@ -29,7 +29,7 @@ #define NUM_LEDS 2 #elif defined(__BOARD_teensy1__) || defined(__BOARD_teensy1pp__) || defined(__BOARD_teensy2__) || defined(__BOARD_teensy2pp__) || \ defined(__BOARD_arduinoNano__) || defined(__BOARD_arduinoNG__) || defined(__BOARD_arduino2009v2__) || defined(__BOARD_arduino2009__) || \ - defined(__BOARD_arduino10000__) || defined(__BOARD_arduinoUno__) || defined(__BOARD_feather32u4__) || \ + defined(__BOARD_arduino10000__) || defined(__BOARD_arduinoUno__) || defined(__BOARD_feather32u4__) || defined(__BOARD_feather32u4lora__) || \ defined(__BOARD_hhd70dongle__) || defined(__BOARD_rda1846dongle__) || defined(__BOARD_culV3__) || \ defined(__BOARD_slowpandongle1__) || defined(__BOARD_slowpandongle2__) || defined(__BOARD_teenstep__) || \ defined(__BOARD_rhmixxx__) || defined(__BOARD_digispark__) || defined(__BOARD_robotdynMega2560__) @@ -62,7 +62,7 @@ #define LED2_PINNUM 6 #endif -#if defined(__BOARD_feather32u4__) +#if defined(__BOARD_feather32u4__) || defined(__BOARD_feather32u4lora__) #define LED_PORT PORTC #define LED_DDR DDRC #define LED_PINNUM 7 diff --git a/usb-lora/Makefile b/usb-lora/Makefile index 07e3a9a..8e2c751 100644 --- a/usb-lora/Makefile +++ b/usb-lora/Makefile @@ -21,7 +21,7 @@ ## NAME := usb-lora -BOARD_TYPE := elecrow32u4lora +BOARD_TYPE := feather32u4lora CXX_OBJ := $(NAME).o LIBS := util led lufa-descriptor-usbserial usbio EXTERNAL_LIBS := lufa radiohead -- cgit v1.2.3