diff --git a/boards/heltec_meshpocket.json b/boards/heltec_meshpocket.json new file mode 100644 index 000000000..a35387857 --- /dev/null +++ b/boards/heltec_meshpocket.json @@ -0,0 +1,54 @@ +{ + "build": { + "arduino": { + "ldscript": "nrf52840_s140_v6.ld" + }, + "core": "nRF5", + "cpu": "cortex-m4", + "extra_flags": "-DNRF52840_XXAA", + "f_cpu": "64000000L", + "hwids": [ + ["0x239A", "0x4405"], + ["0x239A", "0x0029"], + ["0x239A", "0x002A"] + ], + "usb_product": "HT-n5262", + "mcu": "nrf52840", + "variant": "heltec_mesh_pocket", + "variants_dir": "variants", + "bsp": { + "name": "adafruit" + }, + "softdevice": { + "sd_flags": "-DS140", + "sd_name": "s140", + "sd_version": "6.1.1", + "sd_fwid": "0x00B6" + }, + "bootloader": { + "settings_addr": "0xFF000" + } + }, + "connectivity": ["bluetooth"], + "debug": { + "jlink_device": "nRF52840_xxAA", + "onboard_tools": ["jlink"], + "svd_path": "nrf52840.svd", + "openocd_target": "nrf52840-mdk-rs" + }, + "frameworks": ["arduino"], + "name": "Heltec nrf (Adafruit BSP)", + "upload": { + "maximum_ram_size": 248832, + "maximum_size": 815104, + "speed": 115200, + "protocol": "nrfutil", + "protocols": ["jlink", "nrfjprog", "nrfutil", "stlink"], + "use_1200bps_touch": true, + "require_upload_port": true, + "wait_for_upload_port": true + }, + "url": "https://heltec.org/project/meshpocket/", + "vendor": "Heltec" + } + \ No newline at end of file diff --git a/src/helpers/ui/SSD1680Display.cpp b/src/helpers/ui/SSD1680Display.cpp new file mode 100644 index 000000000..27d1902ab --- /dev/null +++ b/src/helpers/ui/SSD1680Display.cpp @@ -0,0 +1,66 @@ +#include "SSD1680Display.h" + +bool SSD1680Display::begin() { + display.begin(true); + return true; +} + +// eInk displays are usually kept in deep sleep +// This means that drawing reinitializes them anyways. +void SSD1680Display::turnOn() { +} + +void SSD1680Display::turnOff() { +} + +void SSD1680Display::clear() { + display.clearDisplay(); +} + +void SSD1680Display::startFrame(Color bkg) { + display.clearBuffer(); + _color = EPD_BLACK; + display.setTextColor(_color); + display.setTextSize(1); + display.cp437(true); // Use full 256 char 'Code Page 437' font +} + +void SSD1680Display::setTextSize(int sz) { + display.setTextSize(sz); +} + +void SSD1680Display::setColor(Color c) { + _color = (c != 0) ? EPD_BLACK : EPD_WHITE; + display.setTextColor(_color); +} + +void SSD1680Display::setCursor(int x, int y) { + display.setCursor(x, y); +} + +void SSD1680Display::print(const char* str) { + display.print(str); +} + +void SSD1680Display::fillRect(int x, int y, int w, int h) { + display.fillRect(x, y, w, h, _color); +} + +void SSD1680Display::drawRect(int x, int y, int w, int h) { + display.drawRect(x, y, w, h, _color); +} + +void SSD1680Display::drawXbm(int x, int y, const uint8_t* bits, int w, int h) { + display.drawBitmap(x, y, bits, w, h, EPD_BLACK); +} + +uint16_t SSD1680Display::getTextWidth(const char* str) { + int16_t x1, y1; + uint16_t w, h; + display.getTextBounds(str, 0, 0, &x1, &y1, &w, &h); + return w; +} + +void SSD1680Display::endFrame() { + display.display(); +} diff --git a/src/helpers/ui/SSD1680Display.h b/src/helpers/ui/SSD1680Display.h new file mode 100644 index 000000000..5c73fc42d --- /dev/null +++ b/src/helpers/ui/SSD1680Display.h @@ -0,0 +1,32 @@ +#pragma once + +#include "DisplayDriver.h" +#include + + +class SSD1680Display : public DisplayDriver { + Adafruit_SSD1680 display; + bool _display_on; + uint32_t _last_full_update; + uint8_t _partial_update_count; + uint8_t _color; + +public: + SSD1680Display() : DisplayDriver(250, 122), display(250, 122, PIN_DISPLAY_DC, PIN_DISPLAY_RST, PIN_DISPLAY_CS, -1, PIN_DISPLAY_BUSY, &SPI1) { _display_on = false; _last_full_update = 0; _partial_update_count = -1;}; + bool begin(); + + bool isOn() override { return true; } + void turnOn() override; + void turnOff() override; + void clear() override; + void startFrame(Color bkg = DARK) override; + void setTextSize(int sz) override; + void setColor(Color c) override; + void setCursor(int x, int y) override; + void print(const char* str) override; + void fillRect(int x, int y, int w, int h) override; + void drawRect(int x, int y, int w, int h) override; + void drawXbm(int x, int y, const uint8_t* bits, int w, int h) override; + uint16_t getTextWidth(const char* str) override; + void endFrame() override; +}; diff --git a/variants/heltec_meshpocket/MeshPocket.cpp b/variants/heltec_meshpocket/MeshPocket.cpp new file mode 100644 index 000000000..04d2cb4a4 --- /dev/null +++ b/variants/heltec_meshpocket/MeshPocket.cpp @@ -0,0 +1,35 @@ +#include +#include "MeshPocket.h" + +#include +#include + +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 HeltecMeshPocket::begin() { + // for future use, sub-classes SHOULD call this from their begin() + startup_reason = BD_STARTUP_NORMAL; + Serial.begin(9600); + pinMode(PIN_VBAT_READ, INPUT); + + pinMode(PIN_USER_BTN, INPUT); + +#ifdef P_LORA_TX_LED + pinMode(P_LORA_TX_LED, OUTPUT); + digitalWrite(P_LORA_TX_LED, HIGH); +#endif +} diff --git a/variants/heltec_meshpocket/MeshPocket.h b/variants/heltec_meshpocket/MeshPocket.h new file mode 100644 index 000000000..1b054fe1c --- /dev/null +++ b/variants/heltec_meshpocket/MeshPocket.h @@ -0,0 +1,55 @@ +#pragma once + +#include +#include + +#define SX126X_DIO2_AS_RF_SWITCH true +#define SX126X_DIO3_TCXO_VOLTAGE 1.8 + +// built-ins +#define PIN_VBAT_READ 29 +#define PIN_BAT_CTL 34 +#define MV_LSB (3000.0F / 4096.0F) // 12-bit ADC with 3.0V input range + +class HeltecMeshPocket : public mesh::MainBoard { +protected: + uint8_t startup_reason; + +public: + void begin(); + uint8_t getStartupReason() const override { return startup_reason; } + +#if defined(P_LORA_TX_LED) + void onBeforeTransmit() override { + digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED on + } + void onAfterTransmit() override { + digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED off + } +#endif + + uint16_t getBattMilliVolts() override { + int adcvalue = 0; + analogReadResolution(12); + analogReference(AR_INTERNAL_3_0); + pinMode(PIN_BAT_CTL, OUTPUT); // battery adc can be read only ctrl pin set to high + pinMode(PIN_VBAT_READ, INPUT); + digitalWrite(PIN_BAT_CTL, 1); + + delay(10); + adcvalue = analogRead(PIN_VBAT_READ); + digitalWrite(PIN_BAT_CTL, 0); + + return (uint16_t)((float)adcvalue * MV_LSB * 4.9); + } + + const char* getManufacturerName() const override { + return "Heltec MeshPocket"; + } + + void reboot() override { + NVIC_SystemReset(); + } + + bool startOTAUpdate(const char* id, char reply[]) override; +}; \ No newline at end of file diff --git a/variants/heltec_meshpocket/platformio.ini b/variants/heltec_meshpocket/platformio.ini new file mode 100644 index 000000000..cb0d5950b --- /dev/null +++ b/variants/heltec_meshpocket/platformio.ini @@ -0,0 +1,42 @@ +[Heltec_meshpocket] +extends = nrf52840_base +board = heltec_meshpocket +board_build.ldscript = boards/nrf52840_s140_v6.ld +build_flags = ${nrf52840_base.build_flags} + -I src/helpers/nrf52 + -I lib/nrf52/s140_nrf52_6.1.1_API/include + -I lib/nrf52/s140_nrf52_6.1.1_API/include/nrf52 + -I variants/heltec_meshpocket + -I src/helpers/ui + -D HELTEC_MESHPOCKET + -D P_LORA_TX_LED=13 + -D RADIO_CLASS=CustomSX1262 + -D WRAPPER_CLASS=CustomSX1262Wrapper + -D LORA_TX_POWER=22 + -D SX126X_CURRENT_LIMIT=140 + -D SX126X_RX_BOOSTED_GAIN=1 +build_src_filter = ${nrf52840_base.build_src_filter} + + + + + +<../variants/heltec_meshpocket> +lib_deps = + ${nrf52840_base.lib_deps} + adafruit/Adafruit EPD @ 4.6.1 +debug_tool = jlink +upload_protocol = nrfutil + +[env:Heltec_MeshPocket_companion_radio_ble] +extends = Heltec_meshpocket +build_flags = + ${Heltec_meshpocket.build_flags} + -D MAX_CONTACTS=100 + -D MAX_GROUP_CHANNELS=8 + -D BLE_PIN_CODE=123456 ; dynamic, random PIN + -D OFFLINE_QUEUE_SIZE=256 + -D DISPLAY_CLASS=SSD1680Display +build_src_filter = ${Heltec_meshpocket.build_src_filter} + + + +<../examples/companion_radio> +lib_deps = + ${Heltec_meshpocket.lib_deps} + densaugeo/base64 @ ~1.4.0 \ No newline at end of file diff --git a/variants/heltec_meshpocket/target.cpp b/variants/heltec_meshpocket/target.cpp new file mode 100644 index 000000000..fced5ae07 --- /dev/null +++ b/variants/heltec_meshpocket/target.cpp @@ -0,0 +1,67 @@ +#include +#include "target.h" +#include + +HeltecMeshPocket 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); + +SSD1680Display display; +MeshPocketSensorManager sensors = MeshPocketSensorManager(); + +VolatileRTCClock fallback_clock; +AutoDiscoverRTCClock rtc_clock(fallback_clock); + +bool radio_init() { + return radio.std_init(&SPI); +} + +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 +} + +bool MeshPocketSensorManager::begin() { + return true; +} + +void MeshPocketSensorManager::loop() { + +} + +bool MeshPocketSensorManager::querySensors(uint8_t requester_permission, CayenneLPP& telemetry) { + return true; +} + +int MeshPocketSensorManager::getNumSettings() const { + return 0; +} + +const char* MeshPocketSensorManager::getSettingName(int i) const { + return NULL; +} + +const char* MeshPocketSensorManager::getSettingValue(int i) const { + return NULL; +} + +bool MeshPocketSensorManager::setSettingValue(const char* name, const char* value) { + return false; +} diff --git a/variants/heltec_meshpocket/target.h b/variants/heltec_meshpocket/target.h new file mode 100644 index 000000000..277c928ed --- /dev/null +++ b/variants/heltec_meshpocket/target.h @@ -0,0 +1,37 @@ +#pragma once + +#define RADIOLIB_STATIC_ONLY 1 +#include +#include +#include +#include "MeshPocket.h" +#include +#include + +extern HeltecMeshPocket board; +extern WRAPPER_CLASS radio_driver; +extern AutoDiscoverRTCClock rtc_clock; + + +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(); + +class MeshPocketSensorManager : public SensorManager { +public: + MeshPocketSensorManager() {}; + bool begin() override; + bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry); + void loop(); + int getNumSettings() const override; + const char* getSettingName(int i) const override; + const char* getSettingValue(int i) const override; + bool setSettingValue(const char* name, const char* value) override; +}; + +extern MeshPocketSensorManager sensors; + +#include "helpers/ui/SSD1680Display.h" +extern SSD1680Display display; \ No newline at end of file diff --git a/variants/heltec_meshpocket/variant.cpp b/variants/heltec_meshpocket/variant.cpp new file mode 100644 index 000000000..5c609bba3 --- /dev/null +++ b/variants/heltec_meshpocket/variant.cpp @@ -0,0 +1,12 @@ +#include "variant.h" +#include "nrf.h" +#include "wiring_constants.h" +#include "wiring_digital.h" + +const uint32_t g_ADigitalPinMap[] = { + // P0 - pins 0 and 1 are hardwired for xtal and should never be enabled + 0xff, 0xff, 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}; + diff --git a/variants/heltec_meshpocket/variant.h b/variants/heltec_meshpocket/variant.h new file mode 100644 index 000000000..c2e7f2dee --- /dev/null +++ b/variants/heltec_meshpocket/variant.h @@ -0,0 +1,119 @@ +/* + * variant.h + * MIT License + */ + +#pragma once + +#include "WVariant.h" + +//////////////////////////////////////////////////////////////////////////////// +// Low frequency clock source + +#define USE_LFXO // 32.768 kHz crystal oscillator +#define VARIANT_MCK (64000000ul) + +#define WIRE_INTERFACES_COUNT (1) + +//////////////////////////////////////////////////////////////////////////////// +// Power + +#define BATTERY_PIN (0 + 29) +#define PIN_BAT_CTRL (32 + 2) +#define ADC_MULTIPLIER (4.90F) + +#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 (1) +#define NUM_ANALOG_OUTPUTS (0) + +//////////////////////////////////////////////////////////////////////////////// +// UART pin definition + +#define PIN_SERIAL1_RX (37) +#define PIN_SERIAL1_TX (39) + +#define PIN_SERIAL2_RX (7) +#define PIN_SERIAL2_TX (8) + +//////////////////////////////////////////////////////////////////////////////// +// I2C pin definition + +#define PIN_WIRE_SDA (32+15) +#define PIN_WIRE_SCL (32+13) + +//////////////////////////////////////////////////////////////////////////////// +// SPI pin definition + +#define SPI_INTERFACES_COUNT (2) + +//////////////////////////////////////////////////////////////////////////////// +// Builtin LEDs + +#define LED_BUILTIN (13) +#define PIN_LED LED_BUILTIN +#define LED_RED LED_BUILTIN +#define LED_BLUE (-1) // No blue led, prevents Bluefruit flashing the green LED during advertising +#define PIN_STATUS_LED LED_BUILTIN + +#define LED_STATE_ON LOW + +//////////////////////////////////////////////////////////////////////////////// +// Builtin buttons + +#define PIN_BUTTON1 (32 + 10) +#define BUTTON_PIN PIN_BUTTON1 + +// #define PIN_BUTTON2 (0 + 18) +// #define BUTTON_PIN2 PIN_BUTTON2 + +#define PIN_USER_BTN BUTTON_PIN + +//////////////////////////////////////////////////////////////////////////////// +// Lora + +#define USE_SX1262 +#define SX126X_CS (0 + 26) +#define SX126X_DIO1 (0 + 16) +#define SX126X_BUSY (0 + 15) +#define SX126X_RESET (0 + 12) +#define SX126X_DIO2_AS_RF_SWITCH +#define SX126X_DIO3_TCXO_VOLTAGE 1.8 + +#define PIN_SPI_MISO (32 + 9) +#define PIN_SPI_MOSI (0 + 5) +#define PIN_SPI_SCK (0 + 4) + +#define LORA_CS SX126X_CS +#define P_LORA_DIO_1 SX126X_DIO1 +#define P_LORA_NSS SX126X_CS +#define P_LORA_RESET SX126X_RESET +#define P_LORA_BUSY SX126X_BUSY +#define P_LORA_SCLK PIN_SPI_SCK +#define P_LORA_MISO PIN_SPI_MISO +#define P_LORA_MOSI PIN_SPI_MOSI + + +//////////////////////////////////////////////////////////////////////////////// +// EInk + +#define PIN_DISPLAY_CS (24) +#define PIN_DISPLAY_BUSY (32 + 6) +#define PIN_DISPLAY_DC (31) +#define PIN_DISPLAY_RST (32 + 4) + +#define PIN_SPI1_MISO (-1) +#define PIN_SPI1_MOSI (20) +#define PIN_SPI1_SCK (22) + +#undef HAS_GPS +#define HAS_GPS 0 +#define HAS_RTC 0 \ No newline at end of file