From 22454c95c7711a11632e9fb9483f6de8a0d36d10 Mon Sep 17 00:00:00 2001
From: Mark Trevor Birss <markbirss@gmail.com>
Date: Thu, 29 Aug 2024 23:17:44 +0200
Subject: [PATCH] [BOARD] Add Minewsemi MS24SF1 nRF52840 SX1262 Module
 (SoftDevice 7.3.0) (#4584)

* Update architecture.h

* Add files via upload

* Add files via upload

* Update variant.h

* Update variant.h

* Update variant.cpp

* Update variant.cpp

* Update variant.cpp
---
 boards/ms24sf1.json               |  58 +++++++++++++
 src/platform/nrf52/architecture.h |   2 +
 variants/MS24SF1/platformio.ini   |  15 ++++
 variants/MS24SF1/variant.cpp      |  30 +++++++
 variants/MS24SF1/variant.h        | 139 ++++++++++++++++++++++++++++++
 5 files changed, 244 insertions(+)
 create mode 100644 boards/ms24sf1.json
 create mode 100644 variants/MS24SF1/platformio.ini
 create mode 100644 variants/MS24SF1/variant.cpp
 create mode 100644 variants/MS24SF1/variant.h

diff --git a/boards/ms24sf1.json b/boards/ms24sf1.json
new file mode 100644
index 000000000..4e28507da
--- /dev/null
+++ b/boards/ms24sf1.json
@@ -0,0 +1,58 @@
+{
+  "build": {
+    "arduino": {
+      "ldscript": "nrf52840_s140_v7.ld"
+    },
+    "core": "nRF5",
+    "cpu": "cortex-m4",
+    "extra_flags": "-DARDUINO_WIO_WM1110 -DNRF52840_XXAA",
+    "f_cpu": "64000000L",
+    "hwids": [
+      ["0x239A", "0x8029"],
+      ["0x239A", "0x0029"],
+      ["0x239A", "0x002A"],
+      ["0x239A", "0x802A"]
+    ],
+    "usb_product": "MS24SF1-BOOT",
+    "mcu": "nrf52840",
+    "variant": "MINEWSEMI_MS24SF1_SX1262",
+    "bsp": {
+      "name": "adafruit"
+    },
+    "softdevice": {
+      "sd_flags": "-DS140",
+      "sd_name": "s140",
+      "sd_version": "7.3.0",
+      "sd_fwid": "0x0123"
+    },
+    "bootloader": {
+      "settings_addr": "0xFF000"
+    }
+  },
+  "connectivity": ["bluetooth"],
+  "debug": {
+    "jlink_device": "nRF52840_xxAA",
+    "svd_path": "nrf52840.svd"
+  },
+  "frameworks": ["arduino"],
+  "name": "MINEWSEMI_MS24SF1_SX1262",
+  "upload": {
+    "maximum_ram_size": 248832,
+    "maximum_size": 815104,
+    "speed": 115200,
+    "protocol": "nrfutil",
+    "protocols": [
+      "jlink",
+      "nrfjprog",
+      "nrfutil",
+      "stlink",
+      "cmsis-dap",
+      "blackmagic"
+    ],
+    "use_1200bps_touch": true,
+    "require_upload_port": true,
+    "wait_for_upload_port": true
+  },
+  "url": "https://en.minewsemi.com/lora-module/nrf52840-sx1262-ms24sf1",
+  "vendor": "Minesemi"
+}
diff --git a/src/platform/nrf52/architecture.h b/src/platform/nrf52/architecture.h
index 834ff6f0c..5a04dd6a7 100644
--- a/src/platform/nrf52/architecture.h
+++ b/src/platform/nrf52/architecture.h
@@ -67,6 +67,8 @@
 #define HW_VENDOR meshtastic_HardwareModel_TRACKER_T1000_E
 #elif defined(ME25LS01_4Y10TD)
 #define HW_VENDOR meshtastic_HardwareModel_ME25LS01_4Y10TD
+#elif defined(MS24SF1)
+#define HW_VENDOR meshtastic_HardwareModel_MS24SF1
 #elif defined(PRIVATE_HW) || defined(FEATHER_DIY)
 #define HW_VENDOR meshtastic_HardwareModel_PRIVATE_HW
 #else
diff --git a/variants/MS24SF1/platformio.ini b/variants/MS24SF1/platformio.ini
new file mode 100644
index 000000000..5cbd078d0
--- /dev/null
+++ b/variants/MS24SF1/platformio.ini
@@ -0,0 +1,15 @@
+[env:ms24sf1]
+extends = nrf52840_base
+board = ms24sf1
+board_level = extra
+; platform = https://github.com/maxgerhardt/platform-nordicnrf52#cac6fcf943a41accd2aeb4f3659ae297a73f422e
+build_flags = ${nrf52840_base.build_flags} -Ivariants/MS24SF1 -Isrc/platform/nrf52/softdevice -Isrc/platform/nrf52/softdevice/nrf52
+  -L "${platformio.libdeps_dir}/${this.__env__}/bsec2/src/cortex-m4/fpv4-sp-d16-hard"
+  -DGPS_POWER_TOGGLE ; comment this line to disable triple press function on the user button to turn off gps entirely.
+board_build.ldscript = src/platform/nrf52/nrf52840_s140_v7.ld
+build_src_filter = ${nrf52_base.build_src_filter} +<../variants/MS24SF1>
+lib_deps =
+  ${nrf52840_base.lib_deps}
+; If not set we will default to uploading over serial (first it forces bootloader entry by talking 1200bps to cdcacm)
+upload_protocol = nrfutil
+upload_port = /dev/ttyACM1
diff --git a/variants/MS24SF1/variant.cpp b/variants/MS24SF1/variant.cpp
new file mode 100644
index 000000000..d1d79c8ce
--- /dev/null
+++ b/variants/MS24SF1/variant.cpp
@@ -0,0 +1,30 @@
+/*
+  Copyright (c) 2014-2015 Arduino LLC.  All right reserved.
+  Copyright (c) 2016 Sandeep Mistry All right reserved.
+  Copyright (c) 2018, Adafruit Industries (adafruit.com)
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+  This library 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 Lesser General Public License for more details.
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include "variant.h"
+#include "nrf.h"
+#include "wiring_constants.h"
+#include "wiring_digital.h"
+
+const uint32_t g_ADigitalPinMap[] = {
+    // P0
+    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
+
+    // P1
+    32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47};
+
+void initVariant() {}
diff --git a/variants/MS24SF1/variant.h b/variants/MS24SF1/variant.h
new file mode 100644
index 000000000..d26dcebc2
--- /dev/null
+++ b/variants/MS24SF1/variant.h
@@ -0,0 +1,139 @@
+/*
+  Copyright (c) 2014-2015 Arduino LLC.  All right reserved.
+  Copyright (c) 2016 Sandeep Mistry All right reserved.
+  Copyright (c) 2018, Adafruit Industries (adafruit.com)
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+  This library 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 Lesser General Public License for more details.
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#ifndef _VARIANT_MINEWSEMI_MS24SF1_
+#define _VARIANT_MINEWSEMI_MS24SF1_
+
+#define ME25LS01
+
+/** Master clock frequency */
+#define VARIANT_MCK (64000000ul)
+
+#define USE_LFXO // Board uses 32khz crystal for LF
+
+/*----------------------------------------------------------------------------
+ *        Headers
+ *----------------------------------------------------------------------------*/
+
+#include "WVariant.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
+
+// Number of pins defined in PinDescription array
+#define PINS_COUNT (48)
+#define NUM_DIGITAL_PINS (48)
+#define NUM_ANALOG_INPUTS (6)
+#define NUM_ANALOG_OUTPUTS (0)
+
+// Use the native nrf52 usb power detection
+#define NRF_APM
+
+#define PIN_3V3_EN (32 + 5) //-1
+#define PIN_3V3_ACC_EN -1
+
+#define PIN_LED1 (-1)
+
+#define LED_PIN PIN_LED1
+#define LED_BUILTIN -1
+
+#define LED_BLUE -1
+#define LED_STATE_ON 1 // State when LED is lit
+
+#define BUTTON_PIN (-1)
+#define BUTTON_NEED_PULLUP
+
+#define HAS_WIRE 1
+
+#define WIRE_INTERFACES_COUNT 1
+
+#define PIN_WIRE_SDA (0 + 29) // P0.15
+#define PIN_WIRE_SCL (0 + 30) // P0.17
+
+/*
+ * Serial interfaces
+ */
+#define PIN_SERIAL1_RX (-1) // P0.14
+#define PIN_SERIAL1_TX (-1) // P0.13
+
+#define PIN_SERIAL2_RX (-1) // P0.17
+#define PIN_SERIAL2_TX (-1) // P0.16
+
+/*
+ * SPI Interfaces
+ */
+#define SPI_INTERFACES_COUNT 1 // 2
+
+#define PIN_SPI_MISO (0 + 17) // MISO      P0.17
+#define PIN_SPI_MOSI (0 + 20) // MOSI      P0.20
+#define PIN_SPI_SCK (0 + 21)  // SCK       P0.21
+
+// #define PIN_SPI1_MISO (-1) //
+// #define PIN_SPI1_MOSI (10) // EPD_MOSI  P0.10
+// #define PIN_SPI1_SCK (9)   // EPD_SCLK  P0.09
+
+static const uint8_t SS = (0 + 22); // LORA_CS   P0.22
+static const uint8_t MOSI = PIN_SPI_MOSI;
+static const uint8_t MISO = PIN_SPI_MISO;
+static const uint8_t SCK = PIN_SPI_SCK;
+
+// MINEWSEMI nRF52840+SX1262 MS24SF1 (NRF82540 with integrated SX1262)
+#define USE_SX1262
+#define SX126X_CS (0 + 22)    // LORA_CS     P0.22
+#define SX126X_DIO1 (0 + 16)  // DIO1        P0.16
+#define SX126X_BUSY (0 + 19)  // LORA_BUSY   P0.19
+#define SX126X_RESET (0 + 12) // LORA_RESET  P0.12
+#define SX126X_TXEN (32 + 4)  // TXEN        P1.04
+#define SX126X_RXEN (32 + 2)  // RXEN        P1.02
+
+// DIO2 controlls an antenna switch and the TCXO voltage is controlled by DIO3
+#define SX126X_DIO2_AS_RF_SWITCH
+
+#define HAS_GPS 0
+
+#define PIN_GPS_EN -1
+#define GPS_EN_ACTIVE HIGH
+#define PIN_GPS_RESET -1
+#define GPS_VRTC_EN -1
+#define GPS_SLEEP_INT -1
+#define GPS_RTC_INT -1
+#define GPS_RESETB_OUT -1
+
+#define BATTERY_PIN -1
+#define ADC_MULTIPLIER (2.0F)
+
+#define ADC_RESOLUTION 14
+#define BATTERY_SENSE_RESOLUTION_BITS 12
+
+#undef AREF_VOLTAGE
+#define AREF_VOLTAGE 3.0
+#define VBAT_AR_INTERNAL AR_INTERNAL_3_0
+
+// Buzzer
+// #define PIN_BUZZER (0 + 25)
+
+#ifdef __cplusplus
+}
+#endif
+
+/*----------------------------------------------------------------------------
+ *        Arduino objects - C++ only
+ *----------------------------------------------------------------------------*/
+
+#endif // _VARIANT_MINEWSEMI_MS24SF1_