From 6f62a5394b1d3d419751548dfeff97aed3844bba Mon Sep 17 00:00:00 2001
From: Mark Qvist <mark@unsigned.io>
Date: Sun, 11 Feb 2024 18:27:47 +0100
Subject: [PATCH] Reworked board defines, fixed RSSI and waterfall bugs for
 SX1262.

---
 Boards.h   | 392 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 Config.h   | 318 -------------------------------------------
 Makefile   |   8 +-
 ROM.h      |   1 +
 sx126x.cpp | 158 ++++++++++++---------
 sx126x.h   |   2 +-
 sx127x.cpp |  33 ++---
 sx127x.h   |   6 +-
 8 files changed, 465 insertions(+), 453 deletions(-)

diff --git a/Boards.h b/Boards.h
index ffce6e9..63cb2ae 100644
--- a/Boards.h
+++ b/Boards.h
@@ -13,49 +13,373 @@
 // You should have received a copy of the GNU General Public License
 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
-#import "Modem.h"
+#include "Modem.h"
 
 #ifndef BOARDS_H
   #define BOARDS_H
+
+  #define PLATFORM_AVR   0x90
+  #define PLATFORM_ESP32 0x80
+  #define PLATFORM_NRF52 0x70
+
+  #define MCU_1284P 0x91
+  #define MCU_2560  0x92
+  #define MCU_ESP32 0x81
+  #define MCU_NRF52 0x71
+
+  #define BOARD_RNODE         0x31
+  #define BOARD_HMBRW         0x32
+  #define BOARD_TBEAM         0x33
+  #define BOARD_HUZZAH32      0x34
+  #define BOARD_GENERIC_ESP32 0x35
+  #define BOARD_LORA32_V2_0   0x36
+  #define BOARD_LORA32_V2_1   0x37
+  #define BOARD_LORA32_V1_0   0x39
+  #define BOARD_HELTEC32_V2   0x38
+  #define BOARD_RNODE_NG_20   0x40
+  #define BOARD_RNODE_NG_21   0x41
+  #define BOARD_RNODE_NG_22   0x42
+  #define BOARD_GENERIC_NRF52 0x50
+  #define BOARD_RAK4630       0x51
+
+  #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
+  #elif defined(NRF52840_XXAA)
+    #define PLATFORM PLATFORM_NRF52
+    #define MCU_VARIANT MCU_NRF52
+  #else
+      #error "The firmware cannot be compiled for the selected MCU variant"
+  #endif
+
+  #ifndef MODEM
+    #if BOARD_MODEL == BOARD_RAK4630
+      #define MODEM SX1262
+    #elif BOARD_MODEL == BOARD_GENERIC_NRF52
+      #define MODEM SX1262
+    #else
+      #define MODEM SX1276
+    #endif
+  #endif
+
+  #define HAS_DISPLAY false
+  #define HAS_BLUETOOTH false
+  #define HAS_TCXO false
+  #define HAS_PMU false
+  #define HAS_NP false
+  #define HAS_EEPROM false
+
+  #if defined(ENABLE_TCXO)
+      #define HAS_TCXO true
+  #endif
+
+  #if MCU_VARIANT == MCU_1284P
+    const int pin_cs = 4;
+    const int pin_reset = 3;
+    const int pin_dio = 2;
+    const int pin_led_rx = 12;
+    const int pin_led_tx = 13;
+
+    #define BOARD_MODEL BOARD_RNODE
+    #define HAS_EEPROM true
+    #define CONFIG_UART_BUFFER_SIZE 6144
+    #define CONFIG_QUEUE_SIZE 6144
+    #define CONFIG_QUEUE_MAX_LENGTH 200
+    #define EEPROM_SIZE 4096
+    #define EEPROM_OFFSET EEPROM_SIZE-EEPROM_RESERVED
   
-    #define PLATFORM_AVR   0x90
-    #define PLATFORM_ESP32 0x80
-    #define PLATFORM_NRF52 0x70
+  #elif MCU_VARIANT == MCU_2560
+    const int pin_cs = 5;
+    const int pin_reset = 4;
+    const int pin_dio = 2;
+    const int pin_led_rx = 12;
+    const int pin_led_tx = 13;
 
-    #define MCU_1284P 0x91
-    #define MCU_2560  0x92
-    #define MCU_ESP32 0x81
-    #define MCU_NRF52 0x71
+    #define BOARD_MODEL BOARD_HMBRW
+    #define HAS_EEPROM true
+    #define CONFIG_UART_BUFFER_SIZE 768
+    #define CONFIG_QUEUE_SIZE 5120
+    #define CONFIG_QUEUE_MAX_LENGTH 24
+    #define EEPROM_SIZE 4096
+    #define EEPROM_OFFSET EEPROM_SIZE-EEPROM_RESERVED
 
-    #define BOARD_RNODE         0x31
-    #define BOARD_HMBRW         0x32
-    #define BOARD_TBEAM         0x33
-    #define BOARD_HUZZAH32      0x34
-    #define BOARD_GENERIC_ESP32 0x35
-    #define BOARD_LORA32_V2_0   0x36
-    #define BOARD_LORA32_V2_1   0x37
-    #define BOARD_LORA32_V1_0   0x39
-    #define BOARD_HELTEC32_V2   0x38
-    #define BOARD_RNODE_NG_20   0x40
-    #define BOARD_RNODE_NG_21   0x41
-    #define BOARD_RNODE_NG_22   0x42
-    #define BOARD_GENERIC_NRF52 0x50
-    #define BOARD_RAK4630       0x51
+  #elif MCU_VARIANT == MCU_ESP32
 
-    #ifndef MODEM
-      #if BOARD_MODEL == BOARD_RAK4630
-        #define MODEM SX1262
-      #elif BOARD_MODEL == BOARD_RNODE_NG_22
-        #define MODEM SX1262
-        const int pin_cs = 7;
-        const int pin_sclk = 5;
-        const int pin_mosi = 6;
-        const int pin_miso = 3;
-      #elif BOARD_MODEL == BOARD_GENERIC_NRF52
-        #define MODEM SX1262
+    // Board models for ESP32 based builds are
+    // defined by the build target in the makefile.
+    // If you are not using make to compile this
+    // firmware, you can manually define model here.
+    //
+    // #define BOARD_MODEL BOARD_GENERIC_ESP32
+    #define CONFIG_UART_BUFFER_SIZE 6144
+    #define CONFIG_QUEUE_SIZE 6144
+    #define CONFIG_QUEUE_MAX_LENGTH 200
+
+    #define EEPROM_SIZE 1024
+    #define EEPROM_OFFSET EEPROM_SIZE-EEPROM_RESERVED
+
+    #define GPS_BAUD_RATE 9600
+    #define PIN_GPS_TX 12
+    #define PIN_GPS_RX 34
+
+    #if BOARD_MODEL == BOARD_GENERIC_ESP32
+      #define HAS_BLUETOOTH true
+      #define HAS_CONSOLE true
+      #define HAS_EEPROM true
+      const int pin_cs = 4;
+      const int pin_reset = 36;
+      const int pin_dio = 39;
+      const int pin_led_rx = 14;
+      const int pin_led_tx = 32;
+
+    #elif BOARD_MODEL == BOARD_TBEAM
+      #define HAS_DISPLAY true
+      #define HAS_PMU true
+      #define HAS_BLUETOOTH true
+      #define HAS_CONSOLE true
+      #define HAS_SD false
+      #define HAS_EEPROM true
+      #define I2C_SDA 21
+      #define I2C_SCL 22
+      #define PMU_IRQ 35
+      const int pin_cs = 18;
+      const int pin_reset = 23;
+      const int pin_led_rx = 2;
+      const int pin_led_tx = 4;
+
+      #if MODEM == SX1262
+        #define HAS_TCXO true
+        #define HAS_BUSY true
+        #define DIO2_AS_RF_SWITCH true
+        const int pin_busy = 32;
+        const int pin_dio = 33;
+        const int pin_tcxo_enable = -1;
       #else
-        #define MODEM SX1276
+        const int pin_dio = 26;
       #endif
+
+    #elif BOARD_MODEL == BOARD_HUZZAH32
+      #define HAS_BLUETOOTH true
+      #define HAS_CONSOLE true
+      #define HAS_EEPROM true
+      const int pin_cs = 4;
+      const int pin_reset = 36;
+      const int pin_dio = 39;
+      const int pin_led_rx = 14;
+      const int pin_led_tx = 32;
+
+    #elif BOARD_MODEL == BOARD_LORA32_V1_0
+      #define HAS_DISPLAY true
+      #define HAS_BLUETOOTH true
+      #define HAS_CONSOLE true
+      #define HAS_EEPROM true
+      const int pin_cs = 18;
+      const int pin_reset = 14;
+      const int pin_dio = 26;
+      #if defined(EXTERNAL_LEDS)
+        const int pin_led_rx = 25;
+        const int pin_led_tx = 2;
+      #else
+        const int pin_led_rx = 2;
+        const int pin_led_tx = 2;
+      #endif
+
+    #elif BOARD_MODEL == BOARD_LORA32_V2_0
+      #define HAS_DISPLAY true
+      #define HAS_BLUETOOTH true
+      #define HAS_CONSOLE true
+      #define HAS_EEPROM true
+      const int pin_cs = 18;
+      const int pin_reset = 12;
+      const int pin_dio = 26;
+      #if defined(EXTERNAL_LEDS)
+        const int pin_led_rx = 2;
+        const int pin_led_tx = 0;
+      #else
+        const int pin_led_rx = 22;
+        const int pin_led_tx = 22;
+      #endif
+
+    #elif BOARD_MODEL == BOARD_LORA32_V2_1
+      #define HAS_DISPLAY true
+      #define HAS_BLUETOOTH true
+      #define HAS_PMU true
+      #define HAS_CONSOLE true
+      #define HAS_EEPROM true
+      const int pin_cs = 18;
+      const int pin_reset = 23;
+      const int pin_dio = 26;
+      #if HAS_TCXO == true
+        const int pin_tcxo_enable = 33;
+      #endif
+      #if defined(EXTERNAL_LEDS)
+        const int pin_led_rx = 15;
+        const int pin_led_tx = 4;
+      #else
+        const int pin_led_rx = 25;
+        const int pin_led_tx = 25;
+      #endif
+
+    #elif BOARD_MODEL == BOARD_HELTEC32_V2
+      #define HAS_DISPLAY true
+      #define HAS_BLUETOOTH true
+      #define HAS_CONSOLE true
+      #define HAS_EEPROM true
+      const int pin_cs = 18;
+      const int pin_reset = 23;
+      const int pin_dio = 26;
+      #if defined(EXTERNAL_LEDS)
+        const int pin_led_rx = 36;
+        const int pin_led_tx = 37;
+      #else
+        const int pin_led_rx = 25;
+        const int pin_led_tx = 25;
+      #endif
+
+    #elif BOARD_MODEL == BOARD_RNODE_NG_20
+      #define HAS_DISPLAY true
+      #define HAS_BLUETOOTH true
+      #define HAS_NP true
+      #define HAS_CONSOLE true
+      #define HAS_EEPROM true
+      const int pin_cs = 18;
+      const int pin_reset = 12;
+      const int pin_dio = 26;
+      const int pin_np = 4;
+      #if HAS_NP == false
+        #if defined(EXTERNAL_LEDS)
+          const int pin_led_rx = 2;
+          const int pin_led_tx = 0;
+        #else
+          const int pin_led_rx = 22;
+          const int pin_led_tx = 22;
+        #endif
+      #endif
+
+    #elif BOARD_MODEL == BOARD_RNODE_NG_21
+      #define HAS_DISPLAY true
+      #define HAS_BLUETOOTH true
+      #define HAS_CONSOLE true
+      #define HAS_PMU true
+      #define HAS_NP true
+      #define HAS_SD false
+      #define HAS_EEPROM true
+      const int pin_cs = 18;
+      const int pin_reset = 23;
+      const int pin_dio = 26;
+      const int pin_np = 12;
+      const int pin_dac = 25;
+      const int pin_adc = 34;
+      const int SD_MISO = 2;
+      const int SD_MOSI = 15;
+      const int SD_CLK = 14;
+      const int SD_CS = 13;
+      #if HAS_NP == false
+        #if defined(EXTERNAL_LEDS)
+          const int pin_led_rx = 12;
+          const int pin_led_tx = 4;
+        #else
+          const int pin_led_rx = 25;
+          const int pin_led_tx = 25;
+        #endif
+      #endif
+
+    #elif BOARD_MODEL == BOARD_RNODE_NG_22
+      #define IS_ESP32S3 true
+      #define MODEM SX1262
+      #define HAS_DISPLAY true
+      #define HAS_BLE true
+      #define HAS_BLUETOOTH false // TODO: Implement
+      #define HAS_CONSOLE false   // TODO: Implement
+      #define HAS_PMU true
+      #define HAS_NP true
+      #define HAS_SD false
+      #define HAS_EEPROM true
+      #define HAS_RF_SWITCH_RX_TX true
+      #define HAS_BUSY true
+      const int pin_cs = 7;
+      const int pin_sclk = 5;
+      const int pin_mosi = 6;
+      const int pin_miso = 3;
+      const int pin_reset = 8;
+      const int pin_rxen = 21;
+      const int pin_dio = 33;
+      const int pin_busy = 34;
+      const int pin_np = 38;
+      const int pin_dac = 25;
+      const int pin_adc = 1;
+      const int SD_MISO = 2;
+      const int SD_MOSI = 11;
+      const int SD_CLK = 14;
+      const int SD_CS = 13;
+      #if HAS_NP == false
+        #if defined(EXTERNAL_LEDS)
+          const int pin_led_rx = 37;
+          const int pin_led_tx = 37;
+        #else
+          const int pin_led_rx = 37;
+          const int pin_led_tx = 37;
+        #endif
+      #endif
+
+    #else
+      #error An unsupported ESP32 board was selected. Cannot compile RNode firmware.
+    #endif
+  
+  #elif MCU_VARIANT == MCU_NRF52
+    #if BOARD_MODEL == BOARD_RAK4630
+      #define HAS_EEPROM false
+      #define HAS_DISPLAY false // set for debugging
+      #define HAS_BLUETOOTH true
+      #define HAS_CONSOLE false
+      #define HAS_PMU false
+      #define HAS_NP false
+      #define HAS_SD false
+      #define HAS_TCXO true
+      #define HAS_RF_SWITCH_RX_TX true
+      #define HAS_BUSY true
+      #define CONFIG_UART_BUFFER_SIZE 6144
+      #define CONFIG_QUEUE_SIZE 6144
+      #define CONFIG_QUEUE_MAX_LENGTH 200
+      #define EEPROM_SIZE 200
+      #define EEPROM_OFFSET EEPROM_SIZE-EEPROM_RESERVED
+
+      // following pins are for the sx1262
+      const int pin_rxen = 37;
+      const int pin_reset = 38;
+      const int pin_cs = 42;
+      const int pin_sclk = 43;
+      const int pin_mosi = 44;
+      const int pin_miso = 45;
+      const int pin_busy = 46;
+      const int pin_dio = 47;
+      const int pin_led_rx = LED_BLUE;
+      const int pin_led_tx = LED_GREEN;
+      const int pin_tcxo_enable = -1;
+
+    #else
+      #error An unsupported nRF board was selected. Cannot compile RNode firmware.
     #endif
 
+  #endif
+
+  #ifndef HAS_RF_SWITCH_RX_TX
+    const int pin_rxen = -1;
+    const int pin_txen = -1;
+  #endif
+
+  #ifndef HAS_BUSY
+    const int pin_busy = -1;
+  #endif
+
+  #ifndef DIO2_AS_RF_SWITCH
+    #define DIO2_AS_RF_SWITCH false
+  #endif
+
 #endif
diff --git a/Config.h b/Config.h
index 49c49af..b411c44 100644
--- a/Config.h
+++ b/Config.h
@@ -15,7 +15,6 @@
 
 #include "ROM.h"
 #include "Boards.h"
-#include "Modem.h"
 
 #ifndef CONFIG_H
 	#define CONFIG_H
@@ -46,333 +45,16 @@
 	bool console_active = false;
 	bool modem_installed = false;
 
-	#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
-    #elif defined(NRF52840_XXAA)
-        #define PLATFORM PLATFORM_NRF52
-        #define MCU_VARIANT MCU_NRF52
-	#else
-	    #error "The firmware cannot be compiled for the selected MCU variant"
-	#endif
-
 	#define MTU   	   508
 	#define SINGLE_MTU 255
 	#define HEADER_L   1
 	#define MIN_L	   1
-
 	#define CMD_L      64
 
-	// MCU dependent configuration parameters
-
-    #define HAS_DISPLAY false
-    #define HAS_BLUETOOTH false
-    #define HAS_TCXO false
-    #define HAS_PMU false
-    #define HAS_NP false
-    #define HAS_EEPROM false
-
-    #if defined(ENABLE_TCXO)
-        #define HAS_TCXO true
-    #endif
-
-	#if MCU_VARIANT == MCU_1284P
-		const int pin_cs = 4;
-		const int pin_reset = 3;
-		const int pin_dio = 2;
-		const int pin_led_rx = 12;
-		const int pin_led_tx = 13;
-
-		#define BOARD_MODEL BOARD_RNODE
-
-        #define HAS_EEPROM true
-
-		#define CONFIG_UART_BUFFER_SIZE 6144
-		#define CONFIG_QUEUE_SIZE 6144
-		#define CONFIG_QUEUE_MAX_LENGTH 200
-
-		#define EEPROM_SIZE 4096
-		#define EEPROM_OFFSET EEPROM_SIZE-EEPROM_RESERVED
-	
-	#elif MCU_VARIANT == MCU_2560
-		const int pin_cs = 5;
-		const int pin_reset = 4;
-		const int pin_dio = 2;
-		const int pin_led_rx = 12;
-		const int pin_led_tx = 13;
-
-		#define BOARD_MODEL BOARD_HMBRW
-
-        #define HAS_EEPROM true
-
-		#define CONFIG_UART_BUFFER_SIZE 768
-		#define CONFIG_QUEUE_SIZE 5120
-		#define CONFIG_QUEUE_MAX_LENGTH 24
-
-		#define EEPROM_SIZE 4096
-		#define EEPROM_OFFSET EEPROM_SIZE-EEPROM_RESERVED
-
-	#elif MCU_VARIANT == MCU_ESP32
-
-		// Board models for ESP32 based builds are
-		// defined by the build target in the makefile.
-		// If you are not using make to compile this
-		// firmware, you can manually define model here.
-		//
-		// #define BOARD_MODEL BOARD_GENERIC_ESP32
-		#define CONFIG_UART_BUFFER_SIZE 6144
-		#define CONFIG_QUEUE_SIZE 6144
-		#define CONFIG_QUEUE_MAX_LENGTH 200
-
-		#define EEPROM_SIZE 1024
-		#define EEPROM_OFFSET EEPROM_SIZE-EEPROM_RESERVED
-
-		#define GPS_BAUD_RATE 9600
-		#define PIN_GPS_TX 12
-		#define PIN_GPS_RX 34
-
-		#if BOARD_MODEL == BOARD_GENERIC_ESP32
-			const int pin_cs = 4;
-			const int pin_reset = 36;
-			const int pin_dio = 39;
-			const int pin_led_rx = 14;
-			const int pin_led_tx = 32;
-            #define HAS_BLUETOOTH true
-            #define HAS_CONSOLE true
-            #define HAS_EEPROM true
-		#elif BOARD_MODEL == BOARD_TBEAM
-			const int pin_cs = 18;
-			const int pin_reset = 23;
-			const int pin_dio = 26;
-			const int pin_led_rx = 2;
-			const int pin_led_tx = 4;
-            #define HAS_DISPLAY true
-            #define HAS_PMU true
-            #define HAS_BLUETOOTH true
-            #define HAS_CONSOLE true
-            #define HAS_SD false
-            #define HAS_EEPROM true
-            #if MODEM == SX1262
-            	// TODO: Figure out how on earth the SX1262 T-Beams are actually connected
-            	// #define HAS_RF_SWITCH_RX_TX true
-	            // const int pin_rxen = 32;
-            	#define HAS_BUSY true
-				const int pin_busy = 32;
-            #endif
-		#elif BOARD_MODEL == BOARD_HUZZAH32
-			const int pin_cs = 4;
-			const int pin_reset = 36;
-			const int pin_dio = 39;
-			const int pin_led_rx = 14;
-			const int pin_led_tx = 32;
-			#define HAS_BLUETOOTH true
-            #define HAS_CONSOLE true
-            #define HAS_EEPROM true
-		#elif BOARD_MODEL == BOARD_LORA32_V1_0
-			const int pin_cs = 18;
-			const int pin_reset = 14;
-			const int pin_dio = 26;
-			#if defined(EXTERNAL_LEDS)
-				const int pin_led_rx = 25;
-				const int pin_led_tx = 2;
-			#else
-				const int pin_led_rx = 2;
-				const int pin_led_tx = 2;
-			#endif
-            #define HAS_DISPLAY true
-            #define HAS_BLUETOOTH true
-            #define HAS_CONSOLE true
-            #define HAS_EEPROM true
-		#elif BOARD_MODEL == BOARD_LORA32_V2_0
-			const int pin_cs = 18;
-			const int pin_reset = 12;
-			const int pin_dio = 26;
-			#if defined(EXTERNAL_LEDS)
-				const int pin_led_rx = 2;
-				const int pin_led_tx = 0;
-			#else
-				const int pin_led_rx = 22;
-				const int pin_led_tx = 22;
-			#endif
-            #define HAS_DISPLAY true
-            #define HAS_BLUETOOTH true
-            #define HAS_CONSOLE true
-            #define HAS_EEPROM true
-		#elif BOARD_MODEL == BOARD_LORA32_V2_1
-			const int pin_cs = 18;
-			const int pin_reset = 23;
-			const int pin_dio = 26;
-			#if HAS_TCXO == true
-				const int pin_tcxo_enable = 33;
-			#endif
-			#if defined(EXTERNAL_LEDS)
-				const int pin_led_rx = 15;
-				const int pin_led_tx = 4;
-			#else
-				const int pin_led_rx = 25;
-				const int pin_led_tx = 25;
-			#endif
-            #define HAS_DISPLAY true
-            #define HAS_BLUETOOTH true
-            #define HAS_PMU true
-            #define HAS_CONSOLE true
-            #define HAS_EEPROM true
-		#elif BOARD_MODEL == BOARD_HELTEC32_V2
-			const int pin_cs = 18;
-			const int pin_reset = 23;
-			const int pin_dio = 26;
-			#if defined(EXTERNAL_LEDS)
-				const int pin_led_rx = 36;
-				const int pin_led_tx = 37;
-			#else
-				const int pin_led_rx = 25;
-				const int pin_led_tx = 25;
-			#endif
-            #define HAS_DISPLAY true
-			#define HAS_BLUETOOTH true
-            #define HAS_CONSOLE true
-            #define HAS_EEPROM true
-		#elif BOARD_MODEL == BOARD_RNODE_NG_20
-            #define HAS_DISPLAY true
-            #define HAS_BLUETOOTH true
-			#define HAS_NP true
-            #define HAS_CONSOLE true
-            #define HAS_EEPROM true
-			const int pin_cs = 18;
-			const int pin_reset = 12;
-			const int pin_dio = 26;
-			const int pin_np = 4;
-			#if HAS_NP == false
-				#if defined(EXTERNAL_LEDS)
-					const int pin_led_rx = 2;
-					const int pin_led_tx = 0;
-				#else
-					const int pin_led_rx = 22;
-					const int pin_led_tx = 22;
-				#endif
-			#endif
-		#elif BOARD_MODEL == BOARD_RNODE_NG_21
-            #define HAS_DISPLAY true
-            #define HAS_BLUETOOTH true
-			#define HAS_CONSOLE true
-            #define HAS_PMU true
-			#define HAS_NP true
-			#define HAS_SD false
-            #define HAS_EEPROM true
-			const int pin_cs = 18;
-			const int pin_reset = 23;
-			const int pin_dio = 26;
-			const int pin_np = 12;
-			const int pin_dac = 25;
-			const int pin_adc = 34;
-			const int SD_MISO = 2;
-			const int SD_MOSI = 15;
-			const int SD_CLK = 14;
-			const int SD_CS = 13;
-			#if HAS_NP == false
-				#if defined(EXTERNAL_LEDS)
-					const int pin_led_rx = 12;
-					const int pin_led_tx = 4;
-				#else
-					const int pin_led_rx = 25;
-					const int pin_led_tx = 25;
-				#endif
-			#endif
-        #elif BOARD_MODEL == BOARD_RNODE_NG_22
-			#define IS_ESP32S3 true
-            #define HAS_DISPLAY true
-			#define HAS_BLE true
-            #define HAS_BLUETOOTH false // TODO: Implement
-			#define HAS_CONSOLE false   // TODO: Implement
-            #define HAS_PMU true
-			#define HAS_NP true
-			#define HAS_SD false
-            #define HAS_EEPROM true
-            #define HAS_RF_SWITCH_RX_TX true
-            #define HAS_BUSY true
-			const int pin_reset = 8;
-            const int pin_rxen = 21;
-			const int pin_dio = 33;
-			const int pin_busy = 34;
-
-			const int pin_np = 38;
-			const int pin_dac = 25;
-			const int pin_adc = 1;
-			const int SD_MISO = 2;
-			const int SD_MOSI = 11;
-			const int SD_CLK = 14;
-			const int SD_CS = 13;
-			#if HAS_NP == false
-				#if defined(EXTERNAL_LEDS)
-					const int pin_led_rx = 37;
-					const int pin_led_tx = 37;
-				#else
-					const int pin_led_rx = 37;
-					const int pin_led_tx = 37;
-				#endif
-			#endif
-        #endif
-    #elif PLATFORM == PLATFORM_NRF52
-        #if BOARD_MODEL == BOARD_RAK4630
-            #define HAS_EEPROM false
-            #define HAS_DISPLAY false // set for debugging
-            #define HAS_BLUETOOTH true
-            #define HAS_CONSOLE false
-            #define HAS_PMU false
-            #define HAS_NP false
-            #define HAS_SD false
-            #define HAS_TCXO true
-            #define HAS_RF_SWITCH_RX_TX true
-            #define HAS_BUSY true
-            #define CONFIG_UART_BUFFER_SIZE 6144
-            #define CONFIG_QUEUE_SIZE 6144
-            #define CONFIG_QUEUE_MAX_LENGTH 200
-            #define EEPROM_SIZE 200
-            //#define EEPROM_OFFSET EEPROM_SIZE+0xED000-EEPROM_RESERVED
-            #define EEPROM_OFFSET EEPROM_SIZE-EEPROM_RESERVED
-
-            // following pins are for the sx1262
-            const int pin_rxen = 37;
-            const int pin_reset = 38;
-            const int pin_cs = 42;
-            const int pin_sclk = 43;
-            const int pin_mosi = 44;
-            const int pin_miso = 45;
-            const int pin_busy = 46;
-            const int pin_dio = 47;
-            const int pin_led_rx = LED_BLUE;
-            const int pin_led_tx = LED_GREEN;
-            const int pin_tcxo_enable = -1;
-        #endif
-	#else
-		#error An unsupported board was selected. Cannot compile RNode firmware.
-	#endif
-
     bool mw_radio_online = false;
 
-	#if BOARD_MODEL == BOARD_TBEAM
-		#define I2C_SDA 21
-		#define I2C_SCL 22
-		#define PMU_IRQ 35
-	#endif
-
 	#define eeprom_addr(a) (a+EEPROM_OFFSET)
 
-    #ifndef HAS_RF_SWITCH_RX_TX
-        const int pin_rxen = -1;
-        const int pin_txen = -1;
-    #endif
-
-    #ifndef HAS_BUSY
-        const int pin_busy = -1;
-    #endif
-
     #if (MODEM == SX1262 || MODEM == SX1280) && defined(NRF52840_XXAA)
         SPIClass spiModem(NRF_SPIM2, pin_miso, pin_sclk, pin_mosi);
     #endif
diff --git a/Makefile b/Makefile
index 3ad2fbe..067e291 100644
--- a/Makefile
+++ b/Makefile
@@ -115,11 +115,11 @@ upload-mega2560:
 	arduino-cli upload -p /dev/ttyACM0 --fqbn arduino:avr:mega
 
 upload-tbeam:
-	arduino-cli upload -p /dev/ttyACM0 --fqbn esp32:esp32:t-beam
+	arduino-cli upload -p /dev/ttyACM1 --fqbn esp32:esp32:t-beam
 	@sleep 1
-	rnodeconf /dev/ttyACM0 --firmware-hash $$(./partition_hashes ./build/esp32.esp32.t-beam/RNode_Firmware.ino.bin)
-	@sleep 3
-	python ./Release/esptool/esptool.py --chip esp32 --port /dev/ttyACM0 --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size 4MB 0x210000 ./Release/console_image.bin
+	rnodeconf /dev/ttyACM1 --firmware-hash $$(./partition_hashes ./build/esp32.esp32.t-beam/RNode_Firmware.ino.bin)
+#	@sleep 3
+#	python ./Release/esptool/esptool.py --chip esp32 --port /dev/ttyACM0 --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size 4MB 0x210000 ./Release/console_image.bin
 
 upload-lora32_v10:
 	arduino-cli upload -p /dev/ttyUSB0 --fqbn esp32:esp32:ttgo-lora32
diff --git a/ROM.h b/ROM.h
index 78e8b08..df4dd08 100644
--- a/ROM.h
+++ b/ROM.h
@@ -72,4 +72,5 @@
 	#define BT_ENABLE_BYTE 0x73
 
 	#define EEPROM_RESERVED 200
+
 #endif
diff --git a/sx126x.cpp b/sx126x.cpp
index 6389a76..d2b2f3b 100644
--- a/sx126x.cpp
+++ b/sx126x.cpp
@@ -4,9 +4,11 @@
 // Modifications and additions copyright 2023 by Mark Qvist
 // Obviously still under the MIT license.
 
-#include "sx126x.h"
 #include "Boards.h"
 
+#if MODEM == SX1262
+#include "sx126x.h"
+
 #define MCU_1284P 0x91
 #define MCU_2560  0x92
 #define MCU_ESP32 0x81
@@ -67,6 +69,7 @@
 #define IRQ_TX_DONE_MASK_6X         0x01
 #define IRQ_RX_DONE_MASK_6X         0x02
 #define IRQ_HEADER_DET_MASK_6X      0x10
+#define IRQ_PREAMBLE_DET_MASK_6X    0x04
 #define IRQ_PAYLOAD_CRC_ERROR_MASK_6X 0x40
 
 #define MODE_LONG_RANGE_MODE_6X     0x01
@@ -81,8 +84,16 @@
 #define REG_RANDOM_GEN_6X         0x0819
 
 #define MODE_TCXO_3_3V_6X           0x07
+#define MODE_TCXO_3_0V_6X           0x06
+#define MODE_TCXO_2_7V_6X           0x06
+#define MODE_TCXO_2_4V_6X           0x06
+#define MODE_TCXO_2_2V_6X           0x03
+#define MODE_TCXO_1_8V_6X           0x02
+#define MODE_TCXO_1_7V_6X           0x01
+#define MODE_TCXO_1_6V_6X           0x00
+
+#define SYNC_WORD_6X              0x1424
 
-#define IRQ_PREAMBLE_DET_MASK_6X    0x04
 #define XTAL_FREQ_6X (double)32000000
 #define FREQ_DIV_6X (double)pow(2.0, 25.0)
 #define FREQ_STEP_6X (double)(XTAL_FREQ_6X / FREQ_DIV_6X)
@@ -126,34 +137,28 @@ bool sx126x::preInit() {
   // set SS high
   digitalWrite(_ss, HIGH);
   
-  Serial.println("SPI INIT");
   #if BOARD_MODEL == BOARD_RNODE_NG_22
     SPI.begin(pin_sclk, pin_miso, pin_mosi, pin_cs);
   #else
     SPI.begin();
   #endif
-  Serial.println("DONE");
 
   // check version (retry for up to 2 seconds)
   long start = millis();
   uint8_t syncmsb;
   uint8_t synclsb;
-  Serial.println("TRYING REGISTER READ");
   while (((millis() - start) < 2000) && (millis() >= start)) {
       syncmsb = readRegister(REG_SYNC_WORD_MSB_6X);
       synclsb = readRegister(REG_SYNC_WORD_LSB_6X);
       if ( uint16_t(syncmsb << 8 | synclsb) == 0x1424 || uint16_t(syncmsb << 8 | synclsb) == 0x4434) {
-          Serial.println("CORRECT VALUE RETURNED");
           break;
       }
       delay(100);
   }
   if ( uint16_t(syncmsb << 8 | synclsb) != 0x1424 && uint16_t(syncmsb << 8 | synclsb) != 0x4434) {
-      Serial.println("REG READ FAILED");
       return false;
   }
 
-  Serial.println("MODEM PREINIT SUCCESS");
   _preinit_done = true;
   return true;
 }
@@ -193,10 +198,9 @@ uint8_t ISR_VECT sx126x::singleTransfer(uint8_t opcode, uint16_t address, uint8_
 
 void sx126x::rxAntEnable()
 {
-        uint8_t byte = 0x01;
-        // enable dio2 rf switch
-        executeOpcode(OP_DIO2_RF_CTRL_6X, &byte, 1); 
-        digitalWrite(_rxen, HIGH);
+  if (_rxen != -1) {
+    digitalWrite(_rxen, HIGH);
+  }
 }
 
 void sx126x::loraMode() {
@@ -361,35 +365,48 @@ int sx126x::begin(long frequency)
     }
   }
 
-      //#if HAS_TCXO
-          // turn TCXO on
-          enableTCXO();
-      //#endif
-      loraMode();
-      idle();
-      // cannot access registers in sleep mode on sx1262, set to idle instead
-      if (_rxen != -1) {
-          pinMode(_rxen, OUTPUT);
-          rxAntEnable();
-      }
-      // calibrate RC64k, RC13M, PLL, ADC and image
-      uint8_t calibrate = 0x7F; 
-      executeOpcode(OP_CALIBRATE_6X, &calibrate, 1);
+  loraMode();
+  // cannot access registers in sleep mode on sx1262, set to idle instead
+  idle();
 
-      setFrequency(frequency);
+  #if HAS_TCXO
+    enableTCXO();
+  #endif
 
-      // set output power to 2 dBm
-      setTxPower(2);
+  if (_rxen != -1) {
+      pinMode(_rxen, OUTPUT);
+  }
 
-      // set LNA boost
-      writeRegister(REG_LNA_6X, 0x96);
+  #if DIO2_AS_RF_SWITCH
+    // enable dio2 rf switch
+    uint8_t byte = 0x01;
+    executeOpcode(OP_DIO2_RF_CTRL_6X, &byte, 1);
+  #endif
 
-      // set base addresses
-      uint8_t basebuf[2] = {0};
-      executeOpcode(OP_BUFFER_BASE_ADDR_6X, basebuf, 2);
+  rxAntEnable();
 
-      setModulationParams(_sf, _bw, _cr, _ldro);
-      setPacketParams(_preambleLength, _implicitHeaderMode, _payloadLength, _crcMode);
+  // Set sync word
+  setSyncWord(SYNC_WORD_6X);
+
+  // calibrate RC64k, RC13M, PLL, ADC and image
+  uint8_t calibrate = 0x7F; 
+  executeOpcode(OP_CALIBRATE_6X, &calibrate, 1);
+
+  setFrequency(frequency);
+
+  // set output power to 2 dBm
+  setTxPower(2);
+  enableCrc();
+
+  // set LNA boost
+  writeRegister(REG_LNA_6X, 0x96);
+
+  // set base addresses
+  uint8_t basebuf[2] = {0};
+  executeOpcode(OP_BUFFER_BASE_ADDR_6X, basebuf, 2);
+
+  setModulationParams(_sf, _bw, _cr, _ldro);
+  setPacketParams(_preambleLength, _implicitHeaderMode, _payloadLength, _crcMode);
 
   return 1;
 }
@@ -416,9 +433,9 @@ int sx126x::beginPacket(int implicitHeader)
     explicitHeaderMode();
   }
 
-      _payloadLength = 0;
-      _fifo_tx_addr_ptr = 0;
-      setPacketParams(_preambleLength, _implicitHeaderMode, _payloadLength, _crcMode);
+  _payloadLength = 0;
+  _fifo_tx_addr_ptr = 0;
+  setPacketParams(_preambleLength, _implicitHeaderMode, _payloadLength, _crcMode);
 
   return 1;
 }
@@ -459,23 +476,18 @@ uint8_t sx126x::modemStatus() {
     // imitate the register status from the sx1276 / 78
     uint8_t buf[2] = {0};
 
-
     executeOpcodeRead(OP_GET_IRQ_STATUS_6X, buf, 2);
-
     uint8_t clearbuf[2] = {0};
-
     uint8_t byte = 0x00;
 
-    if (buf[1] & IRQ_PREAMBLE_DET_MASK_6X != 0) {
-        byte = byte | 0x01 | 0x04;
-        // clear register after reading
-        clearbuf[1] = IRQ_PREAMBLE_DET_MASK_6X; 
+    if ((buf[1] & IRQ_PREAMBLE_DET_MASK_6X) != 0) {
+      byte = byte | 0x01 | 0x04;
+      // clear register after reading
+      clearbuf[1] = IRQ_PREAMBLE_DET_MASK_6X;
     }
 
-    if (buf[1] & IRQ_HEADER_DET_MASK_6X != 0) {
-        byte = byte | 0x02 | 0x04;
-        // clear register after reading
-        clearbuf[1] = clearbuf[1] | IRQ_HEADER_DET_MASK_6X; 
+    if ((buf[1] & IRQ_HEADER_DET_MASK_6X) != 0) {
+      byte = byte | 0x02 | 0x04;
     }
 
     executeOpcode(OP_CLEAR_IRQ_STATUS_6X, clearbuf, 2);
@@ -494,7 +506,7 @@ int ISR_VECT sx126x::currentRssi() {
     uint8_t byte = 0;
     executeOpcodeRead(OP_CURRENT_RSSI_6X, &byte, 1);
     int rssi = -(int(byte)) / 2;
-    return rssi - RSSI_OFFSET;
+    return rssi;
 }
 
 uint8_t sx126x::packetRssiRaw() {
@@ -508,7 +520,7 @@ int ISR_VECT sx126x::packetRssi() {
     uint8_t buf[3] = {0};
     executeOpcodeRead(OP_PACKET_STATUS_6X, buf, 3);
     int pkt_rssi = -buf[0] / 2;
-    return pkt_rssi - RSSI_OFFSET;
+    return pkt_rssi;
 }
 
 uint8_t ISR_VECT sx126x::packetSnrRaw() {
@@ -653,20 +665,18 @@ void sx126x::receive(int size)
     if (_rxen != -1) {
         rxAntEnable();
     }
+
     uint8_t mode[3] = {0xFF, 0xFF, 0xFF}; // continuous mode
     executeOpcode(OP_RX_6X, mode, 3);
 }
 
 void sx126x::idle()
 {
-      //#if HAS_TCXO
-          // STDBY_XOSC
-          uint8_t byte = 0x01;
-      //#else
-      //    // STDBY_RC
-      //    uint8_t byte = 0x00;
-      //#endif
-      executeOpcode(OP_STANDBY_6X, &byte, 1); 
+  // STDBY_XOSC
+  uint8_t byte = 0x01;
+  // STDBY_RC
+  // uint8_t byte = 0x00;
+  executeOpcode(OP_STANDBY_6X, &byte, 1); 
 }
 
 void sx126x::sleep()
@@ -677,7 +687,12 @@ void sx126x::sleep()
 
 void sx126x::enableTCXO() {
     // only tested for RAK4630, voltage may be different on other platforms
-    uint8_t buf[4] = {MODE_TCXO_3_3V_6X, 0x00, 0x00, 0xFF};
+    #if BOARD_MODEL == BOARD_RAK4630
+      uint8_t buf[4] = {MODE_TCXO_3_3V_6X, 0x00, 0x00, 0xFF};
+    #elif BOARD_MODEL == BOARD_TBEAM
+      uint8_t buf[4] = {MODE_TCXO_1_8V_6X, 0x00, 0x00, 0xFF};
+    #endif
+
     executeOpcode(OP_DIO3_TCXO_CTRL_6X, buf, 4);
 }
 
@@ -756,7 +771,7 @@ void sx126x::setSpreadingFactor(int sf)
 
   _sf = sf;
 
-      setModulationParams(sf, _bw, _cr, _ldro);
+  setModulationParams(sf, _bw, _cr, _ldro);
   handleLowDataRate();
 }
 
@@ -834,13 +849,17 @@ void sx126x::setCodingRate4(int denominator)
 
 void sx126x::setPreambleLength(long length)
 {
-    setPacketParams(length, _implicitHeaderMode, _payloadLength, _crcMode);
+  _preambleLength = length;
+  setPacketParams(length, _implicitHeaderMode, _payloadLength, _crcMode);
 }
 
-void sx126x::setSyncWord(int sw)
+void sx126x::setSyncWord(uint16_t sw)
 {
-    writeRegister(REG_SYNC_WORD_MSB_6X, sw & 0xFF00);
-    writeRegister(REG_SYNC_WORD_LSB_6X, sw & 0x00FF);
+  // TODO: Fix
+    // writeRegister(REG_SYNC_WORD_MSB_6X, (sw & 0xFF00) >> 8);
+    // writeRegister(REG_SYNC_WORD_LSB_6X, sw & 0x00FF);
+    writeRegister(REG_SYNC_WORD_MSB_6X, 0x14);
+    writeRegister(REG_SYNC_WORD_LSB_6X, 0x24);
 }
 
 void sx126x::enableCrc()
@@ -921,6 +940,11 @@ void ISR_VECT sx126x::handleDio0Rise()
             _onReceive(packetLength);
         }
     }
+    // else {
+    //   Serial.println("CRCE");
+    //   Serial.println(buf[0]);
+    //   Serial.println(buf[1]);
+    // }
 }
 
 void ISR_VECT sx126x::onDio0Rise()
@@ -929,3 +953,5 @@ void ISR_VECT sx126x::onDio0Rise()
 }
 
 sx126x sx126x_modem;
+
+#endif
\ No newline at end of file
diff --git a/sx126x.h b/sx126x.h
index a08b11c..d20f713 100644
--- a/sx126x.h
+++ b/sx126x.h
@@ -68,7 +68,7 @@ public:
   void setSignalBandwidth(long sbw);
   void setCodingRate4(int denominator);
   void setPreambleLength(long length);
-  void setSyncWord(int sw);
+  void setSyncWord(uint16_t sw);
   uint8_t modemStatus();
   void enableCrc();
   void disableCrc();
diff --git a/sx127x.cpp b/sx127x.cpp
index 403808d..0fa8f5e 100644
--- a/sx127x.cpp
+++ b/sx127x.cpp
@@ -4,30 +4,10 @@
 // Modifications and additions copyright 2023 by Mark Qvist
 // Obviously still under the MIT license.
 
-#include "sx127x.h"
 #include "Boards.h"
 
-#define MCU_1284P 0x91
-#define MCU_2560  0x92
-#define MCU_ESP32 0x81
-#define MCU_NRF52 0x71
-#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
-#elif defined(NRF52840_XXAA)
-  #define PLATFORM PLATFORM_NRF52
-  #define MCU_VARIANT MCU_NRF52
-#endif
-
-#ifndef MCU_VARIANT
-  #error No MCU variant defined, cannot compile
-#endif
+#if MODEM == SX1276
+#include "sx127x.h"
 
 #if MCU_VARIANT == MCU_ESP32
   #if MCU_VARIANT == MCU_ESP32 and !defined(CONFIG_IDF_TARGET_ESP32S3)
@@ -96,6 +76,7 @@
 extern SPIClass SPI;
 
 #define MAX_PKT_LENGTH           255
+#define SYNC_WORD_7X            0x12
 
 sx127x::sx127x() :
   _spiSettings(8E6, MSBFIRST, SPI_MODE0),
@@ -200,10 +181,10 @@ int sx127x::begin(long frequency)
   // set auto AGC
   writeRegister(REG_MODEM_CONFIG_3_7X, 0x04);
 
-  // set output power to 2 dBm
+  setSyncWord(SYNC_WORD_7X);
+  enableCrc();
   setTxPower(2);
 
-  // put in standby mode
   idle();
 
   return 1;
@@ -602,7 +583,7 @@ void sx127x::setPreambleLength(long length)
   writeRegister(REG_PREAMBLE_LSB_7X, (uint8_t)(length >> 0));
 }
 
-void sx127x::setSyncWord(int sw)
+void sx127x::setSyncWord(uint8_t sw)
 {
     writeRegister(REG_SYNC_WORD_7X, sw);
 }
@@ -692,3 +673,5 @@ void ISR_VECT sx127x::onDio0Rise()
 }
 
 sx127x sx127x_modem;
+
+#endif
\ No newline at end of file
diff --git a/sx127x.h b/sx127x.h
index dca01c0..b12002f 100644
--- a/sx127x.h
+++ b/sx127x.h
@@ -66,17 +66,13 @@ public:
   void setSignalBandwidth(long sbw);
   void setCodingRate4(int denominator);
   void setPreambleLength(long length);
-  void setSyncWord(int sw);
+  void setSyncWord(uint8_t sw);
   uint8_t modemStatus();
   void enableCrc();
   void disableCrc();
   void enableTCXO();
   void disableTCXO();
 
-  // deprecated
-  void crc() { enableCrc(); }
-  void noCrc() { disableCrc(); }
-
   byte random();
 
   void setPins(int ss = LORA_DEFAULT_SS_PIN, int reset = LORA_DEFAULT_RESET_PIN, int dio0 = LORA_DEFAULT_DIO0_PIN, int busy = LORA_DEFAULT_BUSY_PIN);