From e7c72c5c6ad0ca5b225e40b260c7f9bd1114d08a Mon Sep 17 00:00:00 2001 From: Rastislav Vysoky Date: Fri, 23 Jan 2026 22:26:24 +0100 Subject: [PATCH 1/2] initial port of rak3112 --- variants/rak3112/RAK3112Board.h | 97 ++++++++++++++ variants/rak3112/platformio.ini | 223 ++++++++++++++++++++++++++++++++ variants/rak3112/target.cpp | 60 +++++++++ variants/rak3112/target.h | 30 +++++ 4 files changed, 410 insertions(+) create mode 100644 variants/rak3112/RAK3112Board.h create mode 100644 variants/rak3112/platformio.ini create mode 100644 variants/rak3112/target.cpp create mode 100644 variants/rak3112/target.h diff --git a/variants/rak3112/RAK3112Board.h b/variants/rak3112/RAK3112Board.h new file mode 100644 index 00000000..8dddbefd --- /dev/null +++ b/variants/rak3112/RAK3112Board.h @@ -0,0 +1,97 @@ +#pragma once + +#include +#include +#include + +// built-ins +#ifndef PIN_VBAT_READ + #define PIN_VBAT_READ 1 +#endif +#ifndef PIN_ADC_CTRL + #define PIN_ADC_CTRL 36 +#endif +#define PIN_ADC_CTRL_ACTIVE LOW +#define PIN_ADC_CTRL_INACTIVE HIGH + +#include + +class RAK3112Board : public ESP32Board { +private: + bool adc_active_state; + +public: + RefCountedDigitalPin periph_power; + + RAK3112Board() : periph_power(PIN_VEXT_EN) { } + + void begin() { + ESP32Board::begin(); + + // Auto-detect correct ADC_CTRL pin polarity (different for boards >3.2) + pinMode(PIN_ADC_CTRL, INPUT); + adc_active_state = !digitalRead(PIN_ADC_CTRL); + + pinMode(PIN_ADC_CTRL, OUTPUT); + digitalWrite(PIN_ADC_CTRL, !adc_active_state); // Initially inactive + + periph_power.begin(); + + esp_reset_reason_t reason = esp_reset_reason(); + if (reason == ESP_RST_DEEPSLEEP) { + long wakeup_source = esp_sleep_get_ext1_wakeup_status(); + if (wakeup_source & (1 << P_LORA_DIO_1)) { // received a LoRa packet (while in deep sleep) + startup_reason = BD_STARTUP_RX_PACKET; + } + + rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS); + rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1); + } + } + + void enterDeepSleep(uint32_t secs, int pin_wake_btn = -1) { + esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); + + // Make sure the DIO1 and NSS GPIOs are hold on required levels during deep sleep + rtc_gpio_set_direction((gpio_num_t)P_LORA_DIO_1, RTC_GPIO_MODE_INPUT_ONLY); + rtc_gpio_pulldown_en((gpio_num_t)P_LORA_DIO_1); + + rtc_gpio_hold_en((gpio_num_t)P_LORA_NSS); + + if (pin_wake_btn < 0) { + esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet + } else { + esp_sleep_enable_ext1_wakeup( (1L << P_LORA_DIO_1) | (1L << pin_wake_btn), ESP_EXT1_WAKEUP_ANY_HIGH); // wake up on: recv LoRa packet OR wake btn + } + + if (secs > 0) { + esp_sleep_enable_timer_wakeup(secs * 1000000); + } + + // Finally set ESP32 into sleep + esp_deep_sleep_start(); // CPU halts here and never returns! + } + + void powerOff() override { + enterDeepSleep(0); + } + + uint16_t getBattMilliVolts() override { + analogReadResolution(10); + digitalWrite(PIN_ADC_CTRL, adc_active_state); + + uint32_t raw = 0; + for (int i = 0; i < 8; i++) { + raw += analogRead(PIN_VBAT_READ); + } + raw = raw / 8; + + digitalWrite(PIN_ADC_CTRL, !adc_active_state); + + return (5.42 * (3.3 / 1024.0) * raw) * 1000; + } + + const char* getManufacturerName() const override { + return "RAK 3112"; + } +}; diff --git a/variants/rak3112/platformio.ini b/variants/rak3112/platformio.ini new file mode 100644 index 00000000..29ebdff2 --- /dev/null +++ b/variants/rak3112/platformio.ini @@ -0,0 +1,223 @@ +[rak3112] +extends = esp32_base +board = esp32-s3-devkitc-1 +build_flags = + ${esp32_base.build_flags} + ${sensor_base.build_flags} + -I variants/rak3112 + -D RAK_3112=1 + -D ESP32_CPU_FREQ=80 + -D P_LORA_DIO_1=47 + -D P_LORA_NSS=7 + -D P_LORA_RESET=8 + -D P_LORA_BUSY=48 + -D P_LORA_SCLK=5 + -D P_LORA_MISO=3 + -D P_LORA_MOSI=6 + -D RADIO_CLASS=CustomSX1262 + -D WRAPPER_CLASS=CustomSX1262Wrapper + -D LORA_TX_POWER=22 + -D P_LORA_TX_LED=46 + -D PIN_BOARD_SDA=9 + -D PIN_BOARD_SCL=40 + -D PIN_USER_BTN=-1 + -D PIN_VEXT_EN=14 + -D SX126X_DIO2_AS_RF_SWITCH=true + -D SX126X_DIO3_TCXO_VOLTAGE=1.8 + -D SX126X_CURRENT_LIMIT=140 + -D SX126X_RX_BOOSTED_GAIN=1 + -D PIN_GPS_RX=43 + -D PIN_GPS_TX=44 +; -D PIN_GPS_EN=26 +build_src_filter = ${esp32_base.build_src_filter} + +<../variants/rak3112> + + +lib_deps = + ${esp32_base.lib_deps} + ${sensor_base.lib_deps} + +[env:RAK3112_repeater] +extends = rak3112 +build_flags = + ${rak3112.build_flags} + -D DISPLAY_CLASS=SSD1306Display + -D ADVERT_NAME='"RAK3112 Repeater"' + -D ADVERT_LAT=0.0 + -D ADVERT_LON=0.0 + -D ADMIN_PASSWORD='"password"' + -D MAX_NEIGHBOURS=50 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${rak3112.build_src_filter} + + + +<../examples/simple_repeater> +lib_deps = + ${rak3112.lib_deps} + ${esp32_ota.lib_deps} + bakercp/CRC32 @ ^2.0.0 + +[env:RAK3112_repeater_bridge_rs232] +extends = rak3112 +build_flags = + ${rak3112.build_flags} + -D DISPLAY_CLASS=SSD1306Display + -D ADVERT_NAME='"RS232 Bridge"' + -D ADVERT_LAT=0.0 + -D ADVERT_LON=0.0 + -D ADMIN_PASSWORD='"password"' + -D MAX_NEIGHBOURS=50 + -D WITH_RS232_BRIDGE=Serial2 + -D WITH_RS232_BRIDGE_RX=5 + -D WITH_RS232_BRIDGE_TX=6 +; -D BRIDGE_DEBUG=1 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${rak3112.build_src_filter} + + + + + +<../examples/simple_repeater> +lib_deps = + ${rak3112.lib_deps} + ${esp32_ota.lib_deps} + +[env:RAK3112_repeater_bridge_espnow] +extends = rak3112 +build_flags = + ${rak3112.build_flags} + -D DISPLAY_CLASS=SSD1306Display + -D ADVERT_NAME='"ESPNow Bridge"' + -D ADVERT_LAT=0.0 + -D ADVERT_LON=0.0 + -D ADMIN_PASSWORD='"password"' + -D MAX_NEIGHBOURS=50 + -D WITH_ESPNOW_BRIDGE=1 +; -D BRIDGE_DEBUG=1 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${rak3112.build_src_filter} + + + + + +<../examples/simple_repeater> +lib_deps = + ${rak3112.lib_deps} + ${esp32_ota.lib_deps} + +[env:RAK3112_room_server] +extends = rak3112 +build_flags = + ${rak3112.build_flags} + -D DISPLAY_CLASS=SSD1306Display + -D ADVERT_NAME='"RAK3112 Room"' + -D ADVERT_LAT=0.0 + -D ADVERT_LON=0.0 + -D ADMIN_PASSWORD='"password"' + -D ROOM_PASSWORD='"hello"' +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${rak3112.build_src_filter} + + + +<../examples/simple_room_server> +lib_deps = + ${rak3112.lib_deps} + ${esp32_ota.lib_deps} + +[env:RAK3112_terminal_chat] +extends = rak3112 +build_flags = + ${rak3112.build_flags} + -D MAX_CONTACTS=350 + -D MAX_GROUP_CHANNELS=1 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${rak3112.build_src_filter} + +<../examples/simple_secure_chat/main.cpp> +lib_deps = + ${rak3112.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:RAK3112_companion_radio_usb] +extends = rak3112 +build_flags = + ${rak3112.build_flags} + -I examples/companion_radio/ui-new + -D MAX_CONTACTS=350 + -D MAX_GROUP_CHANNELS=40 + -D DISPLAY_CLASS=SSD1306Display +; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1 +; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1 +build_src_filter = ${rak3112.build_src_filter} + + + + + +<../examples/companion_radio/*.cpp> + +<../examples/companion_radio/ui-new/*.cpp> +lib_deps = + ${rak3112.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:RAK3112_companion_radio_ble] +extends = rak3112 +build_flags = + ${rak3112.build_flags} + -I examples/companion_radio/ui-new + -D MAX_CONTACTS=350 + -D MAX_GROUP_CHANNELS=40 + -D DISPLAY_CLASS=SSD1306Display + -D BLE_PIN_CODE=123456 ; dynamic, random PIN + -D AUTO_SHUTDOWN_MILLIVOLTS=3400 + -D BLE_DEBUG_LOGGING=1 + -D OFFLINE_QUEUE_SIZE=256 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${rak3112.build_src_filter} + + + + + + + +<../examples/companion_radio/*.cpp> + +<../examples/companion_radio/ui-new/*.cpp> +lib_deps = + ${rak3112.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:RAK3112_companion_radio_wifi] +extends = rak3112 +build_flags = + ${rak3112.build_flags} + -I examples/companion_radio/ui-new + -D MAX_CONTACTS=350 + -D MAX_GROUP_CHANNELS=40 + -D DISPLAY_CLASS=SSD1306Display + -D WIFI_DEBUG_LOGGING=1 + -D WIFI_SSID='"myssid"' + -D WIFI_PWD='"mypwd"' + -D OFFLINE_QUEUE_SIZE=256 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${rak3112.build_src_filter} + + + + + + + +<../examples/companion_radio/*.cpp> + +<../examples/companion_radio/ui-new/*.cpp> +lib_deps = + ${rak3112.lib_deps} + densaugeo/base64 @ ~1.4.0 + +[env:RAK3112_sensor] +extends = rak3112 +build_flags = + ${rak3112.build_flags} + -D ADVERT_NAME='"RAK3112 v3 Sensor"' + -D ADVERT_LAT=0.0 + -D ADVERT_LON=0.0 + -D ADMIN_PASSWORD='"password"' + -D ENV_PIN_SDA=33 + -D ENV_PIN_SCL=34 + -D DISPLAY_CLASS=SSD1306Display +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${rak3112.build_src_filter} + + + +<../examples/simple_sensor> +lib_deps = + ${rak3112.lib_deps} + ${esp32_ota.lib_deps} diff --git a/variants/rak3112/target.cpp b/variants/rak3112/target.cpp new file mode 100644 index 00000000..634573b8 --- /dev/null +++ b/variants/rak3112/target.cpp @@ -0,0 +1,60 @@ +#include +#include "target.h" + +RAK3112Board board; + +#if defined(P_LORA_SCLK) + static SPIClass spi; + RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, spi); +#else + RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY); +#endif + +WRAPPER_CLASS radio_driver(radio, board); + +ESP32RTCClock fallback_clock; +AutoDiscoverRTCClock rtc_clock(fallback_clock); + +#if ENV_INCLUDE_GPS + #include + MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1); + EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); +#else + EnvironmentSensorManager sensors; +#endif + +#ifdef DISPLAY_CLASS + DISPLAY_CLASS display; + MomentaryButton user_btn(PIN_USER_BTN, 1000, true); +#endif + +bool radio_init() { + fallback_clock.begin(); + rtc_clock.begin(Wire); + +#if defined(P_LORA_SCLK) + return radio.std_init(&spi); +#else + return radio.std_init(); +#endif +} + +uint32_t radio_get_rng_seed() { + return radio.random(0x7FFFFFFF); +} + +void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) { + radio.setFrequency(freq); + radio.setSpreadingFactor(sf); + radio.setBandwidth(bw); + radio.setCodingRate(cr); +} + +void radio_set_tx_power(uint8_t dbm) { + radio.setOutputPower(dbm); +} + +mesh::LocalIdentity radio_new_identity() { + RadioNoiseListener rng(radio); + return mesh::LocalIdentity(&rng); // create new random identity +} diff --git a/variants/rak3112/target.h b/variants/rak3112/target.h new file mode 100644 index 00000000..eae90900 --- /dev/null +++ b/variants/rak3112/target.h @@ -0,0 +1,30 @@ +#pragma once + +#define RADIOLIB_STATIC_ONLY 1 +#include +#include +#include +#include +#include +#include +#include +#ifdef DISPLAY_CLASS + #include + #include +#endif + +extern RAK3112Board board; +extern WRAPPER_CLASS radio_driver; +extern AutoDiscoverRTCClock rtc_clock; +extern EnvironmentSensorManager sensors; + +#ifdef DISPLAY_CLASS + extern DISPLAY_CLASS display; + extern MomentaryButton user_btn; +#endif + +bool radio_init(); +uint32_t radio_get_rng_seed(); +void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr); +void radio_set_tx_power(uint8_t dbm); +mesh::LocalIdentity radio_new_identity(); From 266f6ee8560083de8805a87228691edb9c2f159a Mon Sep 17 00:00:00 2001 From: Rastislav Vysoky Date: Fri, 23 Jan 2026 22:41:47 +0100 Subject: [PATCH 2/2] fixed battery measurement --- variants/rak3112/RAK3112Board.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/variants/rak3112/RAK3112Board.h b/variants/rak3112/RAK3112Board.h index 8dddbefd..8ba3197c 100644 --- a/variants/rak3112/RAK3112Board.h +++ b/variants/rak3112/RAK3112Board.h @@ -13,6 +13,8 @@ #endif #define PIN_ADC_CTRL_ACTIVE LOW #define PIN_ADC_CTRL_INACTIVE HIGH +#define ADC_MULTIPLIER (3 * 1.73 * 1.187 * 1000) +#define BATTERY_SAMPLES 8 #include @@ -77,18 +79,15 @@ public: } uint16_t getBattMilliVolts() override { - analogReadResolution(10); - digitalWrite(PIN_ADC_CTRL, adc_active_state); + analogReadResolution(12); uint32_t raw = 0; - for (int i = 0; i < 8; i++) { + for (int i = 0; i < BATTERY_SAMPLES; i++) { raw += analogRead(PIN_VBAT_READ); } - raw = raw / 8; + raw = raw / BATTERY_SAMPLES; - digitalWrite(PIN_ADC_CTRL, !adc_active_state); - - return (5.42 * (3.3 / 1024.0) * raw) * 1000; + return (ADC_MULTIPLIER * raw) / 4096; } const char* getManufacturerName() const override {