diff --git a/variants/wio_wm1110/WioWM1110Board.cpp b/variants/wio_wm1110/WioWM1110Board.cpp new file mode 100644 index 000000000..ca3638b34 --- /dev/null +++ b/variants/wio_wm1110/WioWM1110Board.cpp @@ -0,0 +1,75 @@ +#ifdef WIO_WM1110 + +#include +#include +#include + +#include "WioWM1110Board.h" + +static BLEDfu bledfu; + +static void connect_callback(uint16_t conn_handle) { + (void)conn_handle; + MESH_DEBUG_PRINTLN("BLE client connected"); +} + +static void disconnect_callback(uint16_t conn_handle, uint8_t reason) { + (void)conn_handle; + (void)reason; + + MESH_DEBUG_PRINTLN("BLE client disconnected"); +} + +void WioWM1110Board::begin() { + startup_reason = BD_STARTUP_NORMAL; + + sd_power_mode_set(NRF_POWER_MODE_LOWPWR); + NRF_POWER->DCDCEN = 1; + + pinMode(BATTERY_PIN, INPUT); + pinMode(LED_GREEN, OUTPUT); + pinMode(LED_RED, OUTPUT); + pinMode(SENSOR_POWER_PIN, OUTPUT); + + digitalWrite(LED_GREEN, HIGH); + digitalWrite(LED_RED, LOW); + digitalWrite(SENSOR_POWER_PIN, LOW); + + Serial1.begin(115200); + +#if defined(PIN_WIRE_SDA) && defined(PIN_WIRE_SCL) + Wire.setPins(PIN_WIRE_SDA, PIN_WIRE_SCL); +#endif + + Wire.begin(); + + delay(10); +} + +bool WioWM1110Board::startOTAUpdate(const char *id, char reply[]) { + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); + Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16); + + Bluefruit.begin(1, 0); + Bluefruit.setTxPower(4); + Bluefruit.setName("WM1110_OTA"); + + Bluefruit.Periph.setConnectCallback(connect_callback); + Bluefruit.Periph.setDisconnectCallback(disconnect_callback); + + bledfu.begin(); + + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + Bluefruit.Advertising.addName(); + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); + Bluefruit.Advertising.setFastTimeout(30); + Bluefruit.Advertising.start(0); + + strcpy(reply, "OK - started"); + return true; +} + +#endif + diff --git a/variants/wio_wm1110/WioWM1110Board.h b/variants/wio_wm1110/WioWM1110Board.h new file mode 100644 index 000000000..823acbc7d --- /dev/null +++ b/variants/wio_wm1110/WioWM1110Board.h @@ -0,0 +1,58 @@ +#pragma once + +#include +#include + +#ifdef WIO_WM1110 + +#ifdef Serial + #undef Serial +#endif +#define Serial Serial1 + +class WioWM1110Board : public mesh::MainBoard { +protected: + uint8_t startup_reason; + +public: + void begin(); + uint8_t getStartupReason() const override { return startup_reason; } + +#if defined(LED_GREEN) + void onBeforeTransmit() override { + digitalWrite(LED_RED, HIGH); + } + void onAfterTransmit() override { + digitalWrite(LED_RED, LOW); + } +#endif + + uint16_t getBattMilliVolts() override { + int adcvalue = 0; + analogReadResolution(12); + analogReference(AR_INTERNAL_3_0); + delay(10); + adcvalue = analogRead(BATTERY_PIN); + return (adcvalue * ADC_MULTIPLIER * AREF_VOLTAGE * 1000.0) / 4096.0; + } + + const char* getManufacturerName() const override { + return "Seeed Wio WM1110"; + } + + void reboot() override { + NVIC_SystemReset(); + } + + bool startOTAUpdate(const char* id, char reply[]) override; + + void enableSensorPower(bool enable) { + digitalWrite(SENSOR_POWER_PIN, enable ? HIGH : LOW); + if (enable) { + delay(100); + } + } +}; + +#endif + diff --git a/variants/wio_wm1110/platformio.ini b/variants/wio_wm1110/platformio.ini new file mode 100644 index 000000000..ec65e706b --- /dev/null +++ b/variants/wio_wm1110/platformio.ini @@ -0,0 +1,86 @@ +[wio_wm1110] +extends = nrf52_base +board = seeed-xiao-afruitnrf52-nrf52840 +board_build.ldscript = boards/nrf52840_s140_v7.ld +build_flags = ${nrf52_base.build_flags} + ${sensor_base.build_flags} + -I lib/nrf52/s140_nrf52_7.3.0_API/include + -I lib/nrf52/s140_nrf52_7.3.0_API/include/nrf52 + -I variants/wio_wm1110 + -D NRF52_PLATFORM + -D WIO_WM1110 +; -D MESH_DEBUG=1 + -D RADIO_CLASS=CustomLR1110 + -D WRAPPER_CLASS=CustomLR1110Wrapper + -D LORA_TX_POWER=22 + -D RX_BOOSTED_GAIN=true + -D P_LORA_DIO_1=40 + -D P_LORA_RESET=42 + -D P_LORA_BUSY=43 + -D P_LORA_NSS=44 + -D P_LORA_SCLK=45 + -D P_LORA_MOSI=46 + -D P_LORA_MISO=47 + -D LR11X0_DIO_AS_RF_SWITCH=true + -D LR11X0_DIO3_TCXO_VOLTAGE=1.8 + -D RF_SWITCH_TABLE + -D ENV_INCLUDE_GPS=0 +build_src_filter = ${nrf52_base.build_src_filter} + + + + + +<../variants/wio_wm1110> +debug_tool = jlink +upload_protocol = jlink +lib_deps = ${nrf52_base.lib_deps} + ${sensor_base.lib_deps} + adafruit/Adafruit LIS3DH @ ^1.2.4 + adafruit/Adafruit SHT4x Library @ ^1.0.4 + +[env:wio_wm1110_repeater] +extends = wio_wm1110 +build_flags = + ${wio_wm1110.build_flags} + -D ADVERT_NAME='"WM1110 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 = ${wio_wm1110.build_src_filter} + +<../examples/simple_repeater/*.cpp> + +[env:wio_wm1110_room_server] +extends = wio_wm1110 +build_flags = + ${wio_wm1110.build_flags} + -D ADVERT_NAME='"WM1110 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 = ${wio_wm1110.build_src_filter} + +<../examples/simple_room_server/*.cpp> + +[env:wio_wm1110_companion_radio_ble] +extends = wio_wm1110 +board_build.ldscript = boards/nrf52840_s140_v7_extrafs.ld +board_upload.maximum_size = 708608 +build_flags = + ${wio_wm1110.build_flags} + -D MAX_CONTACTS=350 + -D MAX_GROUP_CHANNELS=40 + -D BLE_PIN_CODE=123456 + -D QSPIFLASH=1 + -D OFFLINE_QUEUE_SIZE=256 +; -D BLE_DEBUG_LOGGING=1 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${wio_wm1110.build_src_filter} + + + +<../examples/companion_radio/*.cpp> +lib_deps = + ${wio_wm1110.lib_deps} + densaugeo/base64 @ ~1.4.0 diff --git a/variants/wio_wm1110/target.cpp b/variants/wio_wm1110/target.cpp new file mode 100644 index 000000000..c659d708b --- /dev/null +++ b/variants/wio_wm1110/target.cpp @@ -0,0 +1,92 @@ +#include +#include "target.h" +#include + +WioWM1110Board board; + +RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, SPI); + +WRAPPER_CLASS radio_driver(radio, board); + +VolatileRTCClock rtc_clock; +EnvironmentSensorManager sensors; + +#ifndef LORA_CR + #define LORA_CR 5 +#endif + +#ifdef RF_SWITCH_TABLE +static const uint32_t rfswitch_dios[Module::RFSWITCH_MAX_PINS] = { + RADIOLIB_LR11X0_DIO5, + RADIOLIB_LR11X0_DIO6, + RADIOLIB_LR11X0_DIO7, + RADIOLIB_LR11X0_DIO8, + RADIOLIB_NC +}; + +static const Module::RfSwitchMode_t rfswitch_table[] = { + // mode DIO5 DIO6 DIO7 DIO8 + { LR11x0::MODE_STBY, {LOW, LOW, LOW, LOW }}, + { LR11x0::MODE_RX, {HIGH, LOW, LOW, HIGH }}, + { LR11x0::MODE_TX, {HIGH, HIGH, LOW, HIGH }}, + { LR11x0::MODE_TX_HP, {LOW, HIGH, LOW, HIGH }}, + { LR11x0::MODE_TX_HF, {LOW, LOW, LOW, LOW }}, + { LR11x0::MODE_GNSS, {LOW, LOW, HIGH, LOW }}, + { LR11x0::MODE_WIFI, {LOW, LOW, LOW, LOW }}, + END_OF_MODE_TABLE, +}; +#endif + +bool radio_init() { + board.enableSensorPower(true); + +#ifdef LR11X0_DIO3_TCXO_VOLTAGE + float tcxo = LR11X0_DIO3_TCXO_VOLTAGE; +#else + float tcxo = 1.8f; +#endif + + SPI.setPins(P_LORA_MISO, P_LORA_SCLK, P_LORA_MOSI); + SPI.begin(); + + int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_LR11X0_LORA_SYNC_WORD_PRIVATE, LORA_TX_POWER, 16, tcxo); + if (status != RADIOLIB_ERR_NONE) { + Serial.print("ERROR: radio init failed: "); + Serial.println(status); + return false; // fail + } + + radio.setCRC(2); + radio.explicitHeader(); + +#ifdef RF_SWITCH_TABLE + radio.setRfSwitchTable(rfswitch_dios, rfswitch_table); +#endif + +#ifdef RX_BOOSTED_GAIN + radio.setRxBoostedGainMode(RX_BOOSTED_GAIN); +#endif + + return true; // success +} + +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/wio_wm1110/target.h b/variants/wio_wm1110/target.h new file mode 100644 index 000000000..9bd4a22b8 --- /dev/null +++ b/variants/wio_wm1110/target.h @@ -0,0 +1,21 @@ +#pragma once + +#define RADIOLIB_STATIC_ONLY 1 +#include +#include +#include "WioWM1110Board.h" +#include +#include +#include + +extern WioWM1110Board board; +extern WRAPPER_CLASS radio_driver; +extern VolatileRTCClock rtc_clock; +extern EnvironmentSensorManager sensors; + +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(); + diff --git a/variants/wio_wm1110/variant.cpp b/variants/wio_wm1110/variant.cpp new file mode 100644 index 000000000..9691a304a --- /dev/null +++ b/variants/wio_wm1110/variant.cpp @@ -0,0 +1,92 @@ +/* + * variant.cpp - Seeed Wio WM1110 Dev Board + * Pin mapping for nRF52840 + */ + +#include "variant.h" +#include "wiring_constants.h" +#include "wiring_digital.h" + +const uint32_t g_ADigitalPinMap[PINS_COUNT + 1] = +{ + 0, // P0.00 + 1, // P0.01 + 2, // P0.02, AIN0, SENSOR_AIN_0 + 3, // P0.03, AIN1, SENSOR_AIN_1 + 4, // P0.04, AIN2, SENSOR_AIN_2 + 5, // P0.05, AIN3, SENSOR_AIN_3 + 6, // P0.06, PIN_SERIAL2_RX, SENSOR_RXD + 7, // P0.07, SENSOR_POWER_PIN + 8, // P0.08, PIN_SERIAL2_TX, SENSOR_TXD + 9, // P0.09 + 10, // P0.10 + 11, // P0.11, LIS3DH_INT_PIN_1, SENSOR_INT_1 + 12, // P0.12, LIS3DH_INT_PIN_2, SENSOR_INT_2 + 13, // P0.13, LED_GREEN, USER_LED_G + 14, // P0.14, LED_RED, USER_LED_R + 15, // P0.15 + 16, // P0.16 + 17, // P0.17 + 18, // P0.18 + 19, // P0.19 + 20, // P0.20 + 21, // P0.21 + 22, // P0.22, PIN_SERIAL1_RX, DEBUG_RX_PIN + 23, // P0.23 + 24, // P0.24, PIN_SERIAL1_TX, DEBUG_TX_PIN + 25, // P0.25 + 26, // P0.26, PIN_WIRE_SCL, SENSOR_SCL + 27, // P0.27, PIN_WIRE_SDA, SENSOR_SDA + 28, // P0.28, AIN4, SENSOR_AIN_4 + 29, // P0.29, AIN5, SENSOR_AIN_5 + 30, // P0.30, AIN6, SENSOR_AIN_6 + 31, // P0.31, AIN7, SENSOR_AIN_7, BATTERY_PIN + 32, // P1.00 + 33, // P1.01 + 34, // P1.02 + 35, // P1.03 + 36, // P1.04 + 37, // P1.05, LR1110_GNSS_ANT_PIN + 38, // P1.06 + 39, // P1.07 + 40, // P1.08, LORA_DIO_1, LR1110_IRQ_PIN + 41, // P1.09 + 42, // P1.10, LORA_RESET, LR1110_NRESET_PIN + 43, // P1.11, LORA_BUSY, LR1110_BUSY_PIN + 44, // P1.12, PIN_SPI_NSS, LR1110_SPI_NSS_PIN + 45, // P1.13, PIN_SPI_SCK, LR1110_SPI_SCK_PIN + 46, // P1.14, PIN_SPI_MOSI, LR1110_SPI_MOSI_PIN + 47, // P1.15, PIN_SPI_MISO, LR1110_SPI_MISO_PIN + 255, // NRFX_SPIM_PIN_NOT_USED +}; + +void initVariant() +{ + // All pins output HIGH by default. + // https://github.com/Seeed-Studio/Adafruit_nRF52_Arduino/blob/fab7d30a997a1dfeef9d1d59bfb549adda73815a/cores/nRF5/wiring.c#L65-L69 + + // Set analog input pins + pinMode(BATTERY_PIN, INPUT); + pinMode(SENSOR_AIN_0, INPUT); + pinMode(SENSOR_AIN_1, INPUT); + pinMode(SENSOR_AIN_2, INPUT); + pinMode(SENSOR_AIN_3, INPUT); + pinMode(SENSOR_AIN_4, INPUT); + pinMode(SENSOR_AIN_5, INPUT); + pinMode(SENSOR_AIN_6, INPUT); + + // Sensor interrupts as inputs + pinMode(LIS3DH_INT_PIN_1, INPUT); + pinMode(LIS3DH_INT_PIN_2, INPUT); + + // Set output pins + pinMode(LED_GREEN, OUTPUT); + pinMode(LED_RED, OUTPUT); + pinMode(SENSOR_POWER_PIN, OUTPUT); + + // Initialize outputs to safe states + digitalWrite(LED_GREEN, HIGH); // Power indicator LED on + digitalWrite(LED_RED, LOW); + digitalWrite(SENSOR_POWER_PIN, LOW); // Sensors powered off initially +} + diff --git a/variants/wio_wm1110/variant.h b/variants/wio_wm1110/variant.h new file mode 100644 index 000000000..cc72c3288 --- /dev/null +++ b/variants/wio_wm1110/variant.h @@ -0,0 +1,145 @@ +/* + * variant.h - Seeed Wio WM1110 Dev Board + * nRF52840 + LR1110 (LoRa + GNSS + WiFi Scanner) + */ + +#pragma once + +#include "WVariant.h" + +//////////////////////////////////////////////////////////////////////////////// +// Low frequency clock source + +#define USE_LFXO // 32.768 kHz crystal oscillator +#define VARIANT_MCK (64000000ul) + +//////////////////////////////////////////////////////////////////////////////// +// Power + +#define BATTERY_PIN (31) // AIN7 +#define BATTERY_IMMUTABLE +#define ADC_MULTIPLIER (2.0F) + +#define ADC_RESOLUTION (14) +#define BATTERY_SENSE_RES (12) + +#define AREF_VOLTAGE (3.0) + +//////////////////////////////////////////////////////////////////////////////// +// Number of pins + +#define PINS_COUNT (48) +#define NUM_DIGITAL_PINS (48) +#define NUM_ANALOG_INPUTS (8) +#define NUM_ANALOG_OUTPUTS (0) + +//////////////////////////////////////////////////////////////////////////////// +// UART pin definition + +#define PIN_SERIAL1_RX (22) +#define PIN_SERIAL1_TX (24) + +#define PIN_SERIAL2_RX (6) +#define PIN_SERIAL2_TX (8) + +//////////////////////////////////////////////////////////////////////////////// +// I2C pin definition + +#define HAS_WIRE (1) +#define WIRE_INTERFACES_COUNT (1) + +#define PIN_WIRE_SDA (27) +#define PIN_WIRE_SCL (26) +#define I2C_NO_RESCAN + +#define SENSOR_POWER_PIN (7) + +#define HAS_LIS3DH (1) +#define LIS3DH_INT_PIN_1 (11) +#define LIS3DH_INT_PIN_2 (12) + +#define HAS_SHT41 (1) + +//////////////////////////////////////////////////////////////////////////////// +// SPI pin definition + +#define SPI_INTERFACES_COUNT (1) + +#define PIN_SPI_MISO (47) +#define PIN_SPI_MOSI (46) +#define PIN_SPI_SCK (45) +#define PIN_SPI_NSS (44) + +//////////////////////////////////////////////////////////////////////////////// +// Builtin LEDs + +#define LED_BUILTIN (13) +#define LED_GREEN (13) +#define LED_RED (14) +#define LED_BLUE LED_RED +#define LED_PIN LED_GREEN + +#define LED_STATE_ON HIGH + +//////////////////////////////////////////////////////////////////////////////// +// Builtin buttons + +#define PIN_BUTTON1 (-1) +#define BUTTON_PIN PIN_BUTTON1 + +//////////////////////////////////////////////////////////////////////////////// +// LR1110 LoRa Radio + GNSS + WiFi + +#define LORA_DIO_1 (40) // P1.8 - LR1110_IRQ_PIN +#define LORA_NSS (PIN_SPI_NSS) // P1.12 +#define LORA_RESET (42) // P1.10 - LR1110_NRESET_PIN +#define LORA_BUSY (43) // P1.11 - LR1110_BUSY_PIN +#define LORA_SCLK (PIN_SPI_SCK) // P1.13 +#define LORA_MISO (PIN_SPI_MISO) // P1.15 +#define LORA_MOSI (PIN_SPI_MOSI) // P1.14 +#define LORA_CS PIN_SPI_NSS // P1.12 + +// LR1110 specific settings +#define LR11X0_DIO_AS_RF_SWITCH true +#define LR11X0_DIO3_TCXO_VOLTAGE 1.8 +#define LR1110_GNSS_ANT_PIN (37) // P1.5 + +// Pin aliases for LR1110 driver compatibility +#define LR1110_IRQ_PIN LORA_DIO_1 +#define LR1110_NRESET_PIN LORA_RESET +#define LR1110_BUSY_PIN LORA_BUSY +#define LR1110_SPI_NSS_PIN LORA_CS +#define LR1110_SPI_SCK_PIN LORA_SCLK +#define LR1110_SPI_MOSI_PIN LORA_MOSI +#define LR1110_SPI_MISO_PIN LORA_MISO + +//////////////////////////////////////////////////////////////////////////////// +// Analog Input Pins + +#define SENSOR_AIN_0 (2) +#define SENSOR_AIN_1 (3) +#define SENSOR_AIN_2 (4) +#define SENSOR_AIN_3 (5) +#define SENSOR_AIN_4 (28) +#define SENSOR_AIN_5 (29) +#define SENSOR_AIN_6 (30) +#define SENSOR_AIN_7 (31) + +static const uint8_t A0 = SENSOR_AIN_0; +static const uint8_t A1 = SENSOR_AIN_1; +static const uint8_t A2 = SENSOR_AIN_2; +static const uint8_t A3 = SENSOR_AIN_3; +static const uint8_t A4 = SENSOR_AIN_4; +static const uint8_t A5 = SENSOR_AIN_5; +static const uint8_t A6 = SENSOR_AIN_6; +static const uint8_t A7 = SENSOR_AIN_7; + +//////////////////////////////////////////////////////////////////////////////// +// GPS/GNSS + +#define HAS_GPS 0 +#define PIN_GPS_TX (-1) +#define PIN_GPS_RX (-1) +#define GPS_EN (-1) +#define GPS_RESET (-1) +