From e67b38c005241e4a8a5c55a4fac1d0e706c32e31 Mon Sep 17 00:00:00 2001 From: Adam Novak Date: Sun, 13 Feb 2022 16:58:04 -0500 Subject: [PATCH] Factor out board config detection --- Config.h | 23 ++++++----------------- LoRa.cpp | 20 +------------------- LoRa.h | 14 +++++++++++--- MD5.cpp | 2 +- Makefile | 5 ++++- Platform.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 65 insertions(+), 41 deletions(-) create mode 100644 Platform.h diff --git a/Config.h b/Config.h index f5d6f38..172f6c9 100644 --- a/Config.h +++ b/Config.h @@ -1,20 +1,13 @@ #include "ROM.h" +#include "Platform.h" + #ifndef CONFIG_H #define CONFIG_H #define MAJ_VERS 0x01 #define MIN_VERS 0x1B - #define PLATFORM_AVR 0x90 - #define PLATFORM_ESP32 0x80 - #define PLATFORM_LINUX 0x70 - - #define MCU_1284P 0x91 - #define MCU_2560 0x92 - #define MCU_ESP32 0x81 - #define MCU_LINUX 0x71 - #define BOARD_RNODE 0x31 #define BOARD_HMBRW 0x32 #define BOARD_TBEAM 0x33 @@ -24,24 +17,25 @@ #define BOARD_LORA32_V2_1 0x37 #define BOARD_SPIDEV 0x38 - #define LIBRARY_ARDUINO 0x1 - #define LIBRARY_C 0x2 - #define MODE_HOST 0x11 #define MODE_TNC 0x12 #if defined(__AVR_ATmega1284P__) #define PLATFORM PLATFORM_AVR #define MCU_VARIANT MCU_1284P + #define LIBRARY_TYPE LIBRARY_ARDUINO #elif defined(__AVR_ATmega2560__) #define PLATFORM PLATFORM_AVR #define MCU_VARIANT MCU_2560 + #define LIBRARY_TYPE LIBRARY_ARDUINO #elif defined(ESP32) #define PLATFORM PLATFORM_ESP32 #define MCU_VARIANT MCU_ESP32 + #define LIBRARY_TYPE LIBRARY_ARDUINO #elif defined(__unix__) #define PLATFORM PLATFORM_LINUX #define MCU_VARIANT MCU_LINUX + #define LIBRARY_TYPE LIBRARY_C #else #error "The firmware cannot be compiled for the selected MCU variant" #endif @@ -63,7 +57,6 @@ const int pin_led_tx = 13; #define BOARD_MODEL BOARD_RNODE - #define LIBRARY_TYPE LIBRARY_ARDUINO #define CONFIG_UART_BUFFER_SIZE 6144 #define CONFIG_QUEUE_SIZE 6144 @@ -80,7 +73,6 @@ const int pin_led_tx = 13; #define BOARD_MODEL BOARD_HMBRW - #define LIBRARY_TYPE LIBRARY_ARDUINO #define CONFIG_UART_BUFFER_SIZE 768 #define CONFIG_QUEUE_SIZE 5120 @@ -142,8 +134,6 @@ #error An unsupported board was selected. Cannot compile RNode firmware. #endif - #define LIBRARY_TYPE LIBRARY_ARDUINO - #define CONFIG_UART_BUFFER_SIZE 6144 #define CONFIG_QUEUE_SIZE 6144 #define CONFIG_QUEUE_MAX_LENGTH 200 @@ -162,7 +152,6 @@ const int pin_led_tx = -1; #define BOARD_MODEL BOARD_SPIDEV - #define LIBRARY_TYPE LIBRARY_C #define CONFIG_UART_BUFFER_SIZE 6144 #define CONFIG_QUEUE_SIZE 6144 diff --git a/LoRa.cpp b/LoRa.cpp index 304e2df..df50cfc 100644 --- a/LoRa.cpp +++ b/LoRa.cpp @@ -6,24 +6,6 @@ #include "LoRa.h" -#define MCU_1284P 0x91 -#define MCU_2560 0x92 -#define MCU_ESP32 0x81 -#if defined(__AVR_ATmega1284P__) - #define PLATFORM PLATFORM_AVR - #define MCU_VARIANT MCU_1284P -#elif defined(__AVR_ATmega2560__) - #define PLATFORM PLATFORM_AVR - #define MCU_VARIANT MCU_2560 -#elif defined(ESP32) - #define PLATFORM PLATFORM_ESP32 - #define MCU_VARIANT MCU_ESP32 -#endif - -#ifndef MCU_VARIANT - #error No MCU variant defined, cannot compile -#endif - #if MCU_VARIANT == MCU_ESP32 #include "soc/rtc_wdt.h" #define ISR_VECT IRAM_ATTR @@ -654,4 +636,4 @@ void ISR_VECT LoRaClass::onDio0Rise() LoRa.handleDio0Rise(); } -LoRaClass LoRa; \ No newline at end of file +LoRaClass LoRa; diff --git a/LoRa.h b/LoRa.h index 09bdc54..42bc116 100644 --- a/LoRa.h +++ b/LoRa.h @@ -7,8 +7,16 @@ #ifndef LORA_H #define LORA_H -#include -#include +#include "Platform.h" + +#if LIBRARY_TYPE == LIBRARY_ARDUINO + #include + #include +#elif LIBRARY_TYPE == LIBRARY_C + #include + #include + #include "ArduinoOnLinux/Stream.h" +#endif #define LORA_DEFAULT_SS_PIN 10 #define LORA_DEFAULT_RESET_PIN 9 @@ -103,4 +111,4 @@ private: extern LoRaClass LoRa; -#endif \ No newline at end of file +#endif diff --git a/MD5.cpp b/MD5.cpp index 52317e5..c04efc1 100644 --- a/MD5.cpp +++ b/MD5.cpp @@ -1,7 +1,7 @@ #include "MD5.h" #if LIBRARY_TYPE == LIBRARY_ARDUINO - #include "Arduino.h" + #include #elif LIBRARY_TYPE == LIBRARY_C #include #endif diff --git a/Makefile b/Makefile index 1c2fa08..fda102b 100644 --- a/Makefile +++ b/Makefile @@ -145,6 +145,9 @@ clean: rm -Rf bin rm -Rf obj -obj/MD5.o: MD5.cpp MD5.h Config.h ROM.h prep-linux +obj/MD5.o: MD5.cpp MD5.h Config.h ROM.h Platform.h prep-linux + $(CC) -c -o $@ $< + +obj/LoRa.o: LoRa.cpp LoRa.h Platform.h prep-linux $(CC) -c -o $@ $< diff --git a/Platform.h b/Platform.h new file mode 100644 index 0000000..5a4c58c --- /dev/null +++ b/Platform.h @@ -0,0 +1,42 @@ +#ifndef PLATFORM_H +#define PLATFORM_H + +// Determine the platform, MCU, and C library we are building for. + +#define PLATFORM_AVR 0x90 +#define PLATFORM_ESP32 0x80 +#define PLATFORM_LINUX 0x70 + +#define MCU_1284P 0x91 +#define MCU_2560 0x92 +#define MCU_ESP32 0x81 +#define MCU_LINUX 0x71 + +#define LIBRARY_ARDUINO 0x1 +#define LIBRARY_C 0x2 + +#if defined(__AVR_ATmega1284P__) + #define PLATFORM PLATFORM_AVR + #define MCU_VARIANT MCU_1284P + #define LIBRARY_TYPE LIBRARY_ARDUINO +#elif defined(__AVR_ATmega2560__) + #define PLATFORM PLATFORM_AVR + #define MCU_VARIANT MCU_2560 + #define LIBRARY_TYPE LIBRARY_ARDUINO +#elif defined(ESP32) + #define PLATFORM PLATFORM_ESP32 + #define MCU_VARIANT MCU_ESP32 + #define LIBRARY_TYPE LIBRARY_ARDUINO +#elif defined(__unix__) + #define PLATFORM PLATFORM_LINUX + #define MCU_VARIANT MCU_LINUX + #define LIBRARY_TYPE LIBRARY_C +#else + #error "The firmware cannot be compiled for the selected MCU variant" +#endif + +#ifndef MCU_VARIANT + #error No MCU variant defined, cannot compile +#endif + +#endif