diff --git a/variants/arduino_nesso_n1/ArduinoNessoN1Board.cpp b/variants/arduino_nesso_n1/ArduinoNessoN1Board.cpp new file mode 100644 index 000000000..122f26403 --- /dev/null +++ b/variants/arduino_nesso_n1/ArduinoNessoN1Board.cpp @@ -0,0 +1,80 @@ +#include +#include "target.h" +#include "pins_arduino.h" + +ArduinoNessoN1Board board; + +#if defined(P_LORA_SCLK) + static SPIClass spi(0); + // replace P_LORA_RESET with -1 to indicate RESET is handled outside + RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, -1, P_LORA_BUSY, spi); +#else + RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, -1, P_LORA_BUSY); +#endif + +WRAPPER_CLASS radio_driver(radio, board); + +ESP32RTCClock fallback_clock; +AutoDiscoverRTCClock rtc_clock(fallback_clock); +SensorManager sensors; + +bool radio_init() { + MESH_DEBUG_PRINTLN("radio_init()"); + fallback_clock.begin(); + rtc_clock.begin(Wire); + + // pinMode(LCD_BACKLIGHT, OUTPUT); // LCD_BACKLIGHT + // pinMode(BEEP_PIN, OUTPUT); + + MESH_DEBUG_PRINTLN("set Nesso N1 pin modes..."); + pinMode(LORA_ENABLE, OUTPUT); // RESET + pinMode(LORA_ANTENNA_SWITCH, OUTPUT); // ANTENNA_SWITCH + pinMode(LORA_LNA_ENABLE, OUTPUT); // LNA_ENABLE + + // Toggle reset via expander + MESH_DEBUG_PRINTLN("Enable LoRa..."); + digitalWrite(LORA_ENABLE, LOW); + delay(10); + digitalWrite(LORA_ENABLE, HIGH); + + // Configure antenna switch and LNA + digitalWrite(LORA_ANTENNA_SWITCH, HIGH); // enable antenna switch + digitalWrite(LORA_LNA_ENABLE, HIGH); // enable LNA + + // Enable LCD backlight + // digitalWrite(LCD_BACKLIGHT, HIGH); + // digitalWrite(BEEP_PIN, HIGH); + // delayMicroseconds(1000); + // digitalWrite(LCD_BACKLIGHT, LOW); + // digitalWrite(BEEP_PIN, LOW); + + MESH_DEBUG_PRINTLN("radio.std_init() and return..."); +#if defined(P_LORA_SCLK) + spi.begin(P_LORA_SCLK, P_LORA_MISO, P_LORA_MOSI); + 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/arduino_nesso_n1/ArduinoNessoN1Board.h b/variants/arduino_nesso_n1/ArduinoNessoN1Board.h new file mode 100644 index 000000000..15e191262 --- /dev/null +++ b/variants/arduino_nesso_n1/ArduinoNessoN1Board.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include "pins_arduino.h" + +#define P_LORA_TX_LED LED_BUILTIN // ToDo: doesn't appear to blink on receipt + + + +class ArduinoNessoN1Board : public ESP32Board { +private: + NessoBattery battery; +public: + void begin() { + ESP32Board::begin(); + +#ifdef P_LORA_TX_LED + pinMode(P_LORA_TX_LED, OUTPUT); + digitalWrite(P_LORA_TX_LED, LOW); +#endif + } + +#ifdef P_LORA_TX_LED + void onBeforeTransmit() override { + MESH_DEBUG_PRINTLN("onBeforeTransmit: HIGH LED"); + digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED on + } + void onAfterTransmit() override { + MESH_DEBUG_PRINTLN("onBeforeTransmit: LOW LED"); + digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED off + } +#endif + + const char* getManufacturerName() const override { + return "Arduino Nesso N1"; + } + + uint16_t getBattMilliVolts() override { + return battery.getMilliVoltage(); // ToDo: Needs work - full battery reports 65v 😅 + } +}; + + diff --git a/variants/arduino_nesso_n1/expander.cpp b/variants/arduino_nesso_n1/expander.cpp new file mode 100644 index 000000000..6589c1efc --- /dev/null +++ b/variants/arduino_nesso_n1/expander.cpp @@ -0,0 +1,139 @@ +#define TwoWire TwoWireInternal +#define Wire WireInternal +#define Wire1 WireInternal1 + +#include "pins_arduino.h" +#include "../../libraries/Wire/src/Wire.h" +#include "../../libraries/Wire/src/Wire.cpp" + +static bool wireInitialized = false; + +// From https://www.diodes.com/datasheet/download/PI4IOE5V6408.pdf +static void writeRegister(uint8_t address, uint8_t reg, uint8_t value) { + WireInternal.beginTransmission(address); + WireInternal.write(reg); + WireInternal.write(value); + WireInternal.endTransmission(); +} + +static uint8_t readRegister(uint8_t address, uint8_t reg) { + WireInternal.beginTransmission(address); + WireInternal.write(reg); + WireInternal.endTransmission(false); + WireInternal.requestFrom(address, 1); + return WireInternal.read(); +} + +static void writeBitRegister(uint8_t address, uint8_t reg, uint8_t bit, uint8_t value) { + uint8_t val = readRegister(address, reg); + if (value) { + writeRegister(address, reg, val | (1 << bit)); + } else { + writeRegister(address, reg, val & ~(1 << bit)); + } +} + +static bool readBitRegister(uint8_t address, uint8_t reg, uint8_t bit) { + uint8_t val = readRegister(address, reg); + return ((val & (1 << bit)) > 0); +} + +void pinMode(ExpanderPin pin, uint8_t mode) { + if (!wireInitialized) { + WireInternal.begin(SDA, SCL); + wireInitialized = true; + // reset all registers to default state + writeRegister(pin.address, 0x1, 0x1); + // set all pins as high as default state + writeRegister(pin.address, 0x9, 0xFF); + // interrupt mask to all pins + writeRegister(pin.address, 0x11, 0xFF); + // all input + writeRegister(pin.address, 0x3, 0); + } + writeBitRegister(pin.address, 0x3, pin.pin, mode == OUTPUT); + if (mode == OUTPUT) { + // remove high impedance + writeBitRegister(pin.address, 0x7, pin.pin, false); + } else if (mode == INPUT_PULLUP) { + // set pull-up resistor + writeBitRegister(pin.address, 0xB, pin.pin, true); + writeBitRegister(pin.address, 0xD, pin.pin, true); + } else if (mode == INPUT_PULLDOWN) { + // disable pull-up resistor + writeBitRegister(pin.address, 0xB, pin.pin, true); + writeBitRegister(pin.address, 0xD, pin.pin, false); + } else if (mode == INPUT) { + // disable pull selector resistor + writeBitRegister(pin.address, 0xB, pin.pin, false); + } +} + +void digitalWrite(ExpanderPin pin, uint8_t val) { + if (!wireInitialized) { + WireInternal.begin(SDA, SCL); + wireInitialized = true; + } + writeBitRegister(pin.address, 0x5, pin.pin, val == HIGH); +} + +int digitalRead(ExpanderPin pin) { + if (!wireInitialized) { + WireInternal.begin(SDA, SCL); + wireInitialized = true; + } + return readBitRegister(pin.address, 0xF, pin.pin); +} + +void NessoBattery::enableCharge() { + // AW32001E - address 0x49 + // set CEB bit low (charge enable) + if (!wireInitialized) { + WireInternal.begin(SDA, SCL); + wireInitialized = true; + } + writeBitRegister(0x49, 0x1, 3, false); +} + +float NessoBattery::getVoltage() { + // BQ27220 - address 0x55 + if (!wireInitialized) { + WireInternal.begin(SDA, SCL); + wireInitialized = true; + } + uint16_t voltage = (readRegister(0x55, 0x9) << 8) | readRegister(0x55, 0x8); + return (float)voltage / 1000.0f; +} + +uint16_t NessoBattery::getMilliVoltage() { + // BQ27220 - address 0x55 + if (!wireInitialized) { + WireInternal.begin(SDA, SCL); + wireInitialized = true; + } + uint16_t voltage = (readRegister(0x55, 0x9) << 8) | readRegister(0x55, 0x8); + return voltage; +} + +uint16_t NessoBattery::getChargeLevel() { + // BQ27220 - address 0x55 + if (!wireInitialized) { + WireInternal.begin(SDA, SCL); + wireInitialized = true; + } + uint16_t current_capacity = readRegister(0x55, 0x11) << 8 | readRegister(0x55, 0x10); + uint16_t total_capacity = readRegister(0x55, 0x13) << 8 | readRegister(0x55, 0x12); + return (current_capacity * 100) / total_capacity; +} + +ExpanderPin LORA_LNA_ENABLE(5); +ExpanderPin LORA_ANTENNA_SWITCH(6); +ExpanderPin LORA_ENABLE(7); +ExpanderPin KEY1(0); +ExpanderPin KEY2(1); +ExpanderPin POWEROFF((1 << 8) | 0); +ExpanderPin LCD_RESET((1 << 8) | 1); +ExpanderPin GROVE_POWER_EN((1 << 8) | 2); +ExpanderPin VIN_DETECT((1 << 8) | 5); +ExpanderPin LCD_BACKLIGHT((1 << 8) | 6); +ExpanderPin LED_BUILTIN((1 << 8) | 7); \ No newline at end of file diff --git a/variants/arduino_nesso_n1/pins_arduino.h b/variants/arduino_nesso_n1/pins_arduino.h new file mode 100644 index 000000000..c675613f5 --- /dev/null +++ b/variants/arduino_nesso_n1/pins_arduino.h @@ -0,0 +1,79 @@ +// from https://github.com/espressif/arduino-esp32/pull/11985/files +#ifndef Pins_Arduino_h +#define Pins_Arduino_h + +#include +#include "soc/soc_caps.h" + +#define USB_VID 0x303A +#define USB_PID 0x1001 +#define USB_MANUFACTURER "Arduino" +#define USB_PRODUCT "Nesso N1" +#define USB_SERIAL "" + +static const uint8_t TX = -1; +static const uint8_t RX = -1; + +static const uint8_t SDA = 10; +static const uint8_t SCL = 8; + +static const uint8_t MOSI = 21; +static const uint8_t MISO = 22; +static const uint8_t SCK = 20; +static const uint8_t SS = 23; + +static const uint8_t D1 = 7; +static const uint8_t D2 = 2; +static const uint8_t D3 = 6; + +static const uint8_t IR_TX_PIN = 9; +static const uint8_t BEEP_PIN = 11; + +static const uint8_t GROVE_IO_0 = 5; +static const uint8_t GROVE_IO_1 = 4; + +static const uint8_t LORA_IRQ = 15; +static const uint8_t LORA_CS = 23; +static const uint8_t LORA_BUSY = 19; + +static const uint8_t SYS_IRQ = 3; + +static const uint8_t LCD_CS = 17; +static const uint8_t LCD_RS = 16; + +#if !defined(MAIN_ESP32_HAL_GPIO_H_) && defined(__cplusplus) +/* address: 0x43/0x44 */ +class ExpanderPin { +public: + ExpanderPin(uint16_t _pin) : pin(_pin & 0xFF), address(_pin & 0x100 ? 0x44 : 0x43){}; + uint8_t pin; + uint8_t address; +}; + +class NessoBattery { +public: + NessoBattery(){}; + void enableCharge(); // enable charging + float getVoltage(); // get battery voltage in Volts + uint16_t getMilliVoltage(); // get battery voltage in millivolts + uint16_t getChargeLevel(); // get battery charge level in percents +}; + +extern ExpanderPin LORA_LNA_ENABLE; +extern ExpanderPin LORA_ANTENNA_SWITCH; +extern ExpanderPin LORA_ENABLE; +extern ExpanderPin POWEROFF; +extern ExpanderPin GROVE_POWER_EN; +extern ExpanderPin VIN_DETECT; +extern ExpanderPin LCD_RESET; +extern ExpanderPin LCD_BACKLIGHT; +extern ExpanderPin LED_BUILTIN; +extern ExpanderPin KEY1; +extern ExpanderPin KEY2; + +void pinMode(ExpanderPin pin, uint8_t mode); +void digitalWrite(ExpanderPin pin, uint8_t val); +int digitalRead(ExpanderPin pin); +#endif + +#endif /* Pins_Arduino_h */ \ No newline at end of file diff --git a/variants/arduino_nesso_n1/platformio.ini b/variants/arduino_nesso_n1/platformio.ini new file mode 100644 index 000000000..45122de6b --- /dev/null +++ b/variants/arduino_nesso_n1/platformio.ini @@ -0,0 +1,79 @@ +[Arduino_Nesso_N1] +extends = esp32c6_base +board = esp32-c6-devkitm-1 +board_build.partitions = min_spiffs.csv ; get around 4mb flash limit +build_flags = + ${esp32c6_base.build_flags} + -I variants/arduino_nesso_n1 + ; -D MESH_DEBUG=1 + ; -D ARDUINO=1 + -D ARDUINO_USB_CDC_ON_BOOT=1 + -D ARDUINO_USB_MODE=1 + ; -D P_LORA_TX_LED= ; Only an IR LED, so disabled for now + -D P_LORA_SCLK=20 + -D P_LORA_MISO=22 + -D P_LORA_MOSI=21 + -D P_LORA_NSS=23 ; aka LORA_CS on the Nesso N1 schematic + -D P_LORA_DIO_1=15 ; aka LORA_IRQ on the Nesso N1 schematic + -D P_LORA_BUSY=19 + ; -D P_LORA_RESET=2 ; ToDo: Required, is on an IO expander: E0.P7 (Address 0x43) + -D PIN_BOARD_SDA=10 + -D PIN_BOARD_SCL=8 + ; -D SX126X_RXEN=23 ; ToDo: not sure + -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 RADIO_CLASS=CustomSX1262 + -D WRAPPER_CLASS=CustomSX1262Wrapper + -D LORA_TX_POWER=22 + ; -D DISPLAY_CLASS=SCIndicatorDisplay ; ToDo: Figure out display later + ; -D DISPLAY_LINES=21 ; ToDo: Figure out display later + ; -D LINE_LENGTH=53 ; ToDo: Figure out display later + ; -D UI_ZOOM=3.5 ; ToDo: Figure out display later + ; -D UI_RECENT_LIST_SIZE=9 ; ToDo: Figure out UI later + ; -D UI_SENSORS_PAGE=1 ; ToDo: Figure out UI later + ; -D PIN_USER_BTN=38 ; ToDo: Figure out UI later + ; -D HAS_TOUCH ; ToDo: Figure out UI later + -D DISABLE_WIFI_OTA=1 +build_src_filter = ${esp32c6_base.build_src_filter} + +<../variants/arduino_nesso_n1> + + + + + +[env:Arduino_Nesso_N1_repeater_] +extends = Arduino_Nesso_N1 +build_src_filter = ${Arduino_Nesso_N1.build_src_filter} + +<../examples/simple_repeater/*.cpp> +build_flags = + ${Arduino_Nesso_N1.build_flags} + -D ADVERT_NAME='"Xiao C6 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 +lib_deps = + ${Arduino_Nesso_N1.lib_deps} +; ${esp32_ota.lib_deps} + +[env:Arduino_Nesso_N1_companion_radio_ble_] +extends = Arduino_Nesso_N1 +build_flags = ${Arduino_Nesso_N1.build_flags} + -D MAX_CONTACTS=350 + -D MAX_GROUP_CHANNELS=40 + -D BLE_PIN_CODE=123456 + -D BLE_DEBUG_LOGGING=1 + -D OFFLINE_QUEUE_SIZE=256 + -D ENABLE_PRIVATE_KEY_IMPORT=1 + -D ENABLE_PRIVATE_KEY_EXPORT=1 +; -D MESH_PACKET_LOGGING=1 +; -D MESH_DEBUG=1 +build_src_filter = ${Arduino_Nesso_N1.build_src_filter} + + + - + +<../examples/companion_radio/*.cpp> +lib_deps = + ${Arduino_Nesso_N1.lib_deps} + densaugeo/base64 @ ~1.4.0 diff --git a/variants/arduino_nesso_n1/target.h b/variants/arduino_nesso_n1/target.h new file mode 100644 index 000000000..1aa0b392c --- /dev/null +++ b/variants/arduino_nesso_n1/target.h @@ -0,0 +1,22 @@ +#pragma once + +#define RADIOLIB_STATIC_ONLY 1 +#include +#include +#include "pins_arduino.h" +#include +#include +#include +#include +#include + +extern ArduinoNessoN1Board board; +extern WRAPPER_CLASS radio_driver; +extern AutoDiscoverRTCClock rtc_clock; +extern SensorManager 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();