From c541fa2d7101a29692d50c0d15ae8ed9bc80c1f6 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Wed, 17 Sep 2025 16:08:44 +0800 Subject: [PATCH] Added SX1280 ranging example, using open source code, accuracy is not guaranteed --- .../SX1280/Ranging_Master/Ranging_Master.ino | 213 +++++++++++++++++ .../SX1280/Ranging_Master/boards.h | 216 ++++++++++++++++++ .../SX1280/Ranging_Master/utilities.h | 47 ++++ .../SX1280/Ranging_Slave/Ranging_Slave.ino | 119 ++++++++++ .../SX1280/Ranging_Slave/boards.h | 216 ++++++++++++++++++ .../SX1280/Ranging_Slave/utilities.h | 47 ++++ platformio.ini | 5 + 7 files changed, 863 insertions(+) create mode 100644 examples/RadioLibExamples/SX1280/Ranging_Master/Ranging_Master.ino create mode 100644 examples/RadioLibExamples/SX1280/Ranging_Master/boards.h create mode 100644 examples/RadioLibExamples/SX1280/Ranging_Master/utilities.h create mode 100644 examples/RadioLibExamples/SX1280/Ranging_Slave/Ranging_Slave.ino create mode 100644 examples/RadioLibExamples/SX1280/Ranging_Slave/boards.h create mode 100644 examples/RadioLibExamples/SX1280/Ranging_Slave/utilities.h diff --git a/examples/RadioLibExamples/SX1280/Ranging_Master/Ranging_Master.ino b/examples/RadioLibExamples/SX1280/Ranging_Master/Ranging_Master.ino new file mode 100644 index 0000000..5fd0895 --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging_Master/Ranging_Master.ino @@ -0,0 +1,213 @@ + +#include "Arduino.h" +#include +#include +#include "boards.h" + +#define LORA_DEVICE DEVICE_SX1280 //we need to define the device we are using + +// Using open source code, no guarantee of accuracy +#define T3_S3_V1_2_SX1280_PA + +//******* Setup LoRa Parameters Here ! *************** +const uint32_t Frequency = 2445000000; //frequency of transmissions in hz +const int32_t Offset = 0; //offset frequency in hz for calibration purposes +const uint8_t Bandwidth = LORA_BW_0800; //LoRa bandwidth +const uint8_t SpreadingFactor = LORA_SF8; //LoRa spreading factor +const uint8_t CodeRate = LORA_CR_4_5; //LoRa coding rate +const uint16_t Calibration = 11350; //Manual Ranging calibrarion value + + +#ifdef T3_S3_V1_2_SX1280_PA +const int8_t RangingTXPower = 3; //Transmit power used !!Cannot be greater than 3dbm!! +#else +const int8_t RangingTXPower = 13; //Transmit power used !!Cannot be greater than 3dbm!! +#endif + +const uint32_t RangingAddress = 16; //must match address in recever + +const uint16_t waittimemS = 10000; //wait this long in mS for packet before assuming timeout +const uint16_t TXtimeoutmS = 5000; //ranging TX timeout in mS +const uint16_t packet_delaymS = 0; //forced extra delay in mS between ranging requests +const uint16_t rangeingcount = 10; //number of times ranging is cqarried out for each distance measurment +float distance_adjustment = 1.0000; //adjustment factor to calculated distance + + +#define ENABLEOLED //enable this define to use display +#define ENABLEDISPLAY //enable this define to use display + +SX128XLT LT; + +uint16_t rangeing_errors, rangeings_valid, rangeing_results; +uint16_t IrqStatus; +uint32_t endwaitmS, range_result_sum, range_result_average; +float distance, distance_sum, distance_average; +bool ranging_error; +int32_t range_result; +int16_t RangingRSSI; + +void led_Flash(uint16_t flashes, uint16_t delaymS); + +void setup() +{ + pinMode(BOARD_LED, OUTPUT); //setup pin as output for indicator LED + led_Flash(4, 125); //two quick LED flashes to indicate program start + + initBoard(); + delay(100); + + Serial.println(F("Ranging Master Starting, Using open source code, no guarantee of accuracy")); + + + SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN); + + if (LT.begin(RADIO_CS_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN, RADIO_DIO1_PIN, RADIO_RX_PIN, RADIO_TX_PIN, LORA_DEVICE)) { + Serial.println(F("Device found")); + led_Flash(2, 125); + delay(1000); + } else { + Serial.println(F("No device responding")); + u8g2->clearBuffer(); + u8g2->drawStr(0, 12, "No device responding"); + u8g2->sendBuffer(); + while (1) { + led_Flash(50, 50); //long fast speed flash indicates device error + } + } + + LT.setupRanging(Frequency, Offset, SpreadingFactor, Bandwidth, CodeRate, RangingAddress, RANGING_MASTER); + LT.setRangingCalibration(Calibration); //override automatic lookup of calibration value from library table + + Serial.println(); + LT.printModemSettings(); //reads and prints the configured LoRa settings, useful check + Serial.println(); + LT.printOperatingSettings(); //reads and prints the configured operating settings, useful check + Serial.println(); + Serial.println(); + LT.printRegisters(0x900, 0x9FF); //print contents of device registers, normally 0x900 to 0x9FF + Serial.println(); + Serial.println(); + +#ifdef ENABLEDISPLAY + if (u8g2) { + Serial.println("Display Enabled"); + u8g2->setFont(u8g2_font_unifont_t_chinese2); // use chinese2 for all the glyphs of "你好世界" + u8g2->setFontDirection(0); + char buf[256]; + u8g2->clearBuffer(); + u8g2->drawStr(0, 12, "Ranging RAW Ready"); + snprintf(buf, sizeof(buf), "Power:%.d dBm", RangingTXPower); + u8g2->drawStr(0, 12 * 2, buf); + snprintf(buf, sizeof(buf), "Cal: %d ", Calibration); + u8g2->drawStr(0, 12 * 3, buf); + snprintf(buf, sizeof(buf), "Adjust: %d ", distance_adjustment); + u8g2->sendBuffer(); + } +#endif + + Serial.print(F("Address ")); + Serial.println(RangingAddress); + Serial.print(F("CalibrationValue ")); + Serial.println(LT.getSetCalibrationValue()); + Serial.println(F("Ranging master RAW ready")); + delay(2000); +} + +void loop() +{ + uint8_t index; + distance_sum = 0; + range_result_sum = 0; + rangeing_results = 0; //count of valid results in each loop + + for (index = 1; index <= rangeingcount; index++) { + + LT.transmitRanging(RangingAddress, TXtimeoutmS, RangingTXPower, WAIT_TX); + IrqStatus = LT.readIrqStatus(); + + if (IrqStatus & IRQ_RANGING_MASTER_RESULT_VALID) { + rangeing_results++; + rangeings_valid++; + digitalWrite(BOARD_LED, HIGH); + Serial.print(F("Valid")); + range_result = LT.getRangingResultRegValue(RANGING_RESULT_RAW); + Serial.print(F(",Register,")); + Serial.print(range_result); + + if (range_result > 800000) { + range_result = 0; + } + range_result_sum = range_result_sum + range_result; + + distance = LT.getRangingDistance(RANGING_RESULT_RAW, range_result, distance_adjustment); + distance_sum = distance_sum + distance; + + Serial.print(F(",Distance,")); + Serial.print(distance, 1); + RangingRSSI = LT.getRangingRSSI(); + digitalWrite(BOARD_LED, LOW); + } else { + rangeing_errors++; + distance = 0; + range_result = 0; + Serial.print(F("NotValid")); + Serial.print(F(",Irq,")); + Serial.print(IrqStatus, HEX); + } + delay(packet_delaymS); + + if (index == rangeingcount) { + if (rangeing_results > 0) { + range_result_average = range_result_sum / rangeing_results; + distance_average = distance_sum / rangeing_results; + } else { + range_result_average = 0; + distance_average = 0.0; + RangingRSSI = 0; + } + + Serial.print(F(",TotalValid,")); + Serial.print(rangeings_valid); + Serial.print(F(",TotalErrors,")); + Serial.print(rangeing_errors); + Serial.print(F(",AverageRAWResult,")); + Serial.print(range_result_average); + Serial.print(F(",AverageDistance,")); + Serial.print(distance_average, 1); + +#ifdef ENABLEDISPLAY + if (u8g2) { + u8g2->clearBuffer(); + char buf[256]; + u8g2->drawStr(0, 12, "Rang_Master"); + snprintf(buf, sizeof(buf), "Distance:%.2f m", distance_average); + u8g2->drawStr(0, 12 * 2, buf); + snprintf(buf, sizeof(buf), "RSSI: %d dBm", RangingRSSI); + u8g2->drawStr(0, 12 * 3, buf); + // snprintf(buf, sizeof(buf), "OK: %d ", rangeings_valid); + // u8g2->drawStr(0, 12 * 4, buf); + // snprintf(buf, sizeof(buf), "Err: %d ", rangeing_errors); + // u8g2->drawStr(0, 12 * 5, buf); + u8g2->sendBuffer(); + } + +#endif + delay(2000); + + } + Serial.println(); + } + +} + +void led_Flash(uint16_t flashes, uint16_t delaymS) +{ + uint16_t index; + + for (index = 1; index <= flashes; index++) { + digitalWrite(BOARD_LED, HIGH); + delay(delaymS); + digitalWrite(BOARD_LED, LOW); + delay(delaymS); + } +} diff --git a/examples/RadioLibExamples/SX1280/Ranging_Master/boards.h b/examples/RadioLibExamples/SX1280/Ranging_Master/boards.h new file mode 100644 index 0000000..671bda0 --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging_Master/boards.h @@ -0,0 +1,216 @@ +#include +#include +#include +#include +#include "utilities.h" + +#ifdef HAS_SDCARD +#include +#include +#endif + +#ifdef HAS_DISPLAY +#include +U8G2_SSD1306_128X64_NONAME_F_HW_I2C *u8g2 = nullptr; +#endif + +Ticker ledTicker; +#if defined(LILYGO_TBeam_V1_X) +#include +AXP20X_Class PMU; + +bool initPMU() +{ + if (PMU.begin(Wire, AXP192_SLAVE_ADDRESS) == AXP_FAIL) { + return false; + } + /* + * The charging indicator can be turned on or off + * * * */ + // PMU.setChgLEDMode(LED_BLINK_4HZ); + + /* + * The default ESP32 power supply has been turned on, + * no need to set, please do not set it, if it is turned off, + * it will not be able to program + * + * PMU.setDCDC3Voltage(3300); + * PMU.setPowerOutPut(AXP192_DCDC3, AXP202_ON); + * + * * * */ + + /* + * Turn off unused power sources to save power + * **/ + + PMU.setPowerOutPut(AXP192_DCDC1, AXP202_OFF); + PMU.setPowerOutPut(AXP192_DCDC2, AXP202_OFF); + PMU.setPowerOutPut(AXP192_LDO2, AXP202_OFF); + PMU.setPowerOutPut(AXP192_LDO3, AXP202_OFF); + PMU.setPowerOutPut(AXP192_EXTEN, AXP202_OFF); + + /* + * Set the power of LoRa and GPS module to 3.3V + **/ + PMU.setLDO2Voltage(3300); //LoRa VDD + PMU.setLDO3Voltage(3300); //GPS VDD + PMU.setDCDC1Voltage(3300); //3.3V Pin next to 21 and 22 is controlled by DCDC1 + + PMU.setPowerOutPut(AXP192_DCDC1, AXP202_ON); + PMU.setPowerOutPut(AXP192_LDO2, AXP202_ON); + PMU.setPowerOutPut(AXP192_LDO3, AXP202_ON); + + pinMode(PMU_IRQ, INPUT_PULLUP); + attachInterrupt(PMU_IRQ, [] { + // pmu_irq = true; + }, FALLING); + + PMU.adc1Enable(AXP202_VBUS_VOL_ADC1 | + AXP202_VBUS_CUR_ADC1 | + AXP202_BATT_CUR_ADC1 | + AXP202_BATT_VOL_ADC1, + AXP202_ON); + + PMU.enableIRQ(AXP202_VBUS_REMOVED_IRQ | + AXP202_VBUS_CONNECT_IRQ | + AXP202_BATT_REMOVED_IRQ | + AXP202_BATT_CONNECT_IRQ, + AXP202_ON); + PMU.clearIRQ(); + + return true; +} + +void disablePeripherals() +{ + PMU.setPowerOutPut(AXP192_DCDC1, AXP202_OFF); + PMU.setPowerOutPut(AXP192_LDO2, AXP202_OFF); + PMU.setPowerOutPut(AXP192_LDO3, AXP202_OFF); +} +#else +#define initPMU() +#define disablePeripherals() +#endif + +SPIClass SDSPI(HSPI); + + +void initBoard() +{ + Serial.begin(115200); + Serial.println("initBoard"); + SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN); + Wire.begin(I2C_SDA, I2C_SCL); + +#ifdef LILYGO_T3_S3_V1_0 + pinMode(RADIO_TX_PIN, OUTPUT); + pinMode(RADIO_RX_PIN, OUTPUT); + digitalWrite(RADIO_TX_PIN, LOW); + digitalWrite(RADIO_RX_PIN, HIGH); +#endif + +#ifdef HAS_GPS + Serial1.begin(GPS_BAUD_RATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN); +#endif + +#if OLED_RST + pinMode(OLED_RST, OUTPUT); + digitalWrite(OLED_RST, HIGH); delay(20); + digitalWrite(OLED_RST, LOW); delay(20); + digitalWrite(OLED_RST, HIGH); delay(20); +#endif + + initPMU(); + + +#ifdef BOARD_LED + /* + * T-BeamV1.0, V1.1 LED defaults to low level as trun on, + * so it needs to be forced to pull up + * * * * */ +#if LED_ON == LOW + gpio_hold_dis(GPIO_NUM_4); +#endif + pinMode(BOARD_LED, OUTPUT); + ledTicker.attach_ms(500, []() { + static bool level; + digitalWrite(BOARD_LED, level); + level = !level; + }); +#endif + + +#ifdef HAS_DISPLAY + Wire.beginTransmission(0x3C); + if (Wire.endTransmission() == 0) { + Serial.println("Started OLED"); + u8g2 = new U8G2_SSD1306_128X64_NONAME_F_HW_I2C(U8G2_R0, U8X8_PIN_NONE); + u8g2->begin(); + u8g2->clearBuffer(); + u8g2->setFlipMode(0); + u8g2->setFontMode(1); // Transparent + u8g2->setDrawColor(1); + u8g2->setFontDirection(0); + u8g2->firstPage(); + do { + u8g2->setFont(u8g2_font_inb19_mr); + u8g2->drawStr(0, 30, "LilyGo"); + u8g2->drawHLine(2, 35, 47); + u8g2->drawHLine(3, 36, 47); + u8g2->drawVLine(45, 32, 12); + u8g2->drawVLine(46, 33, 12); + u8g2->setFont(u8g2_font_inb19_mf); + u8g2->drawStr(58, 60, "LoRa"); + } while ( u8g2->nextPage() ); + u8g2->sendBuffer(); + u8g2->setFont(u8g2_font_fur11_tf); + delay(3000); + } +#endif + + +#ifdef HAS_SDCARD + if (u8g2) { + u8g2->setFont(u8g2_font_ncenB08_tr); + } + pinMode(SDCARD_MISO, INPUT_PULLUP); + SDSPI.begin(SDCARD_SCLK, SDCARD_MISO, SDCARD_MOSI, SDCARD_CS); + if (u8g2) { + u8g2->clearBuffer(); + } + + if (!SD.begin(SDCARD_CS, SDSPI)) { + + Serial.println("setupSDCard FAIL"); + if (u8g2) { + do { + u8g2->setCursor(0, 16); + u8g2->println( "SDCard FAILED");; + } while ( u8g2->nextPage() ); + } + + } else { + uint32_t cardSize = SD.cardSize() / (1024 * 1024); + if (u8g2) { + do { + u8g2->setCursor(0, 16); + u8g2->print( "SDCard:");; + u8g2->print(cardSize / 1024.0);; + u8g2->println(" GB");; + } while ( u8g2->nextPage() ); + } + + Serial.print("setupSDCard PASS . SIZE = "); + Serial.print(cardSize / 1024.0); + Serial.println(" GB"); + } + if (u8g2) { + u8g2->sendBuffer(); + } + delay(3000); +#endif + + +} + + diff --git a/examples/RadioLibExamples/SX1280/Ranging_Master/utilities.h b/examples/RadioLibExamples/SX1280/Ranging_Master/utilities.h new file mode 100644 index 0000000..3c05e13 --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging_Master/utilities.h @@ -0,0 +1,47 @@ + +#pragma once + +/* +* This sample program only supports SX1280 +* */ + +#define I2C_SDA 18 +#define I2C_SCL 17 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 3 +#define RADIO_MOSI_PIN 6 +#define RADIO_CS_PIN 7 +#define RADIO_DIO1_PIN 9 +#define RADIO_DIO2_PIN 33 +#define RADIO_DIO3_PIN 34 +#define RADIO_RST_PIN 8 +#define RADIO_BUSY_PIN 36 + +#define RADIO_RX_PIN 10 +#define RADIO_TX_PIN 21 + +#define SDCARD_MOSI 11 +#define SDCARD_MISO 2 +#define SDCARD_SCLK 14 +#define SDCARD_CS 13 + +#define BOARD_LED 37 +#define LED_ON HIGH + +#define BAT_ADC_PIN 1 +#define BUTTON_PIN 0 + +#define HAS_SDCARD +#define HAS_DISPLAY + + + + + + + + + + diff --git a/examples/RadioLibExamples/SX1280/Ranging_Slave/Ranging_Slave.ino b/examples/RadioLibExamples/SX1280/Ranging_Slave/Ranging_Slave.ino new file mode 100644 index 0000000..994b0d7 --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging_Slave/Ranging_Slave.ino @@ -0,0 +1,119 @@ +#include +#include +#include "boards.h" + +// Using open source code, no guarantee of accuracy +#define T3_S3_V1_2_SX1280_PA + +#define LORA_DEVICE DEVICE_SX1280 //we need to define the device we are using +//******* Setup LoRa Parameters Here ! *************** +//LoRa Modem Parameters +const uint32_t Frequency = 2445000000; //frequency of transmissions in hz +const int32_t Offset = 0; //offset frequency in hz for calibration purposes +const uint8_t Bandwidth = LORA_BW_0800; //LoRa bandwidth +const uint8_t SpreadingFactor = LORA_SF8; //LoRa spreading factor +const uint8_t CodeRate = LORA_CR_4_5; //LoRa coding rate +const uint16_t Calibration = 11350; //Manual Ranging calibration value + +#ifdef T3_S3_V1_2_SX1280_PA +const int8_t TXpower = 3; //Transmit power used !!Cannot be greater than 3dbm!! +#else +const int8_t TXpower = 13; //Transmit power used !!Cannot be greater than 3dbm!! +#endif +const uint32_t RangingAddress = 16; //must match address in master + +const uint16_t rangingRXTimeoutmS = 0xFFFF; //ranging RX timeout in mS + + +SX128XLT LT; + +uint32_t endwaitmS; +uint16_t IrqStatus; +uint32_t response_sent; + +void led_Flash(unsigned int flashes, unsigned int delaymS); + +void setup() +{ + Serial.begin(115200); //setup Serial console ouput + + Serial.println("Ranging Slave Starting , Using open source code, no guarantee of accuracy"); + + pinMode(BOARD_LED, OUTPUT); + led_Flash(2, 125); + + initBoard(); + delay(100); + + SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN); + + if (LT.begin(RADIO_CS_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN, RADIO_DIO1_PIN, RADIO_RX_PIN, RADIO_TX_PIN, LORA_DEVICE)) { + Serial.println(F("Device found")); + led_Flash(2, 125); + delay(1000); + } else { + Serial.println(F("No device responding")); + while (1) { + led_Flash(50, 50); //long fast speed flash indicates device error + } + } + + //The function call list below shows the complete setup for the LoRa device for ranging using the information + LT.setupRanging(Frequency, Offset, SpreadingFactor, Bandwidth, CodeRate, RangingAddress, RANGING_SLAVE); + + LT.setRangingCalibration(11350); //override automatic lookup of calibration value from library table + + Serial.print(F("Calibration,")); + Serial.println(LT.getSetCalibrationValue()); //reads the calibratuion value currently set + delay(2000); + + if (u8g2) { + u8g2->clearBuffer(); + u8g2->drawStr(0, 12, "Rang_Slave"); + u8g2->sendBuffer(); + } +} + +char buf[256]; +void loop() +{ + LT.receiveRanging(RangingAddress, 0, TXpower, NO_WAIT); + + endwaitmS = millis() + rangingRXTimeoutmS; + + while (!digitalRead(RADIO_DIO1_PIN) && (millis() <= endwaitmS)); //wait for Ranging valid or timeout + + if (millis() >= endwaitmS) { + Serial.println("Error - Ranging Receive Timeout!!"); + led_Flash(2, 100); //single flash to indicate timeout + } else { + IrqStatus = LT.readIrqStatus(); + digitalWrite(BOARD_LED, HIGH); + + if (IrqStatus & IRQ_RANGING_SLAVE_RESPONSE_DONE) { + response_sent++; + Serial.print(response_sent); + Serial.print(" Response sent"); + } else { + Serial.print("Slave error,"); + Serial.print(",Irq,"); + Serial.print(IrqStatus, HEX); + LT.printIrqStatus(); + } + digitalWrite(BOARD_LED, LOW); + Serial.println(); + } + +} + +void led_Flash(unsigned int flashes, unsigned int delaymS) +{ + //flash LED to show board is alive + unsigned int index; + for (index = 1; index <= flashes; index++) { + digitalWrite(BOARD_LED, HIGH); + delay(delaymS); + digitalWrite(BOARD_LED, LOW); + delay(delaymS); + } +} diff --git a/examples/RadioLibExamples/SX1280/Ranging_Slave/boards.h b/examples/RadioLibExamples/SX1280/Ranging_Slave/boards.h new file mode 100644 index 0000000..671bda0 --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging_Slave/boards.h @@ -0,0 +1,216 @@ +#include +#include +#include +#include +#include "utilities.h" + +#ifdef HAS_SDCARD +#include +#include +#endif + +#ifdef HAS_DISPLAY +#include +U8G2_SSD1306_128X64_NONAME_F_HW_I2C *u8g2 = nullptr; +#endif + +Ticker ledTicker; +#if defined(LILYGO_TBeam_V1_X) +#include +AXP20X_Class PMU; + +bool initPMU() +{ + if (PMU.begin(Wire, AXP192_SLAVE_ADDRESS) == AXP_FAIL) { + return false; + } + /* + * The charging indicator can be turned on or off + * * * */ + // PMU.setChgLEDMode(LED_BLINK_4HZ); + + /* + * The default ESP32 power supply has been turned on, + * no need to set, please do not set it, if it is turned off, + * it will not be able to program + * + * PMU.setDCDC3Voltage(3300); + * PMU.setPowerOutPut(AXP192_DCDC3, AXP202_ON); + * + * * * */ + + /* + * Turn off unused power sources to save power + * **/ + + PMU.setPowerOutPut(AXP192_DCDC1, AXP202_OFF); + PMU.setPowerOutPut(AXP192_DCDC2, AXP202_OFF); + PMU.setPowerOutPut(AXP192_LDO2, AXP202_OFF); + PMU.setPowerOutPut(AXP192_LDO3, AXP202_OFF); + PMU.setPowerOutPut(AXP192_EXTEN, AXP202_OFF); + + /* + * Set the power of LoRa and GPS module to 3.3V + **/ + PMU.setLDO2Voltage(3300); //LoRa VDD + PMU.setLDO3Voltage(3300); //GPS VDD + PMU.setDCDC1Voltage(3300); //3.3V Pin next to 21 and 22 is controlled by DCDC1 + + PMU.setPowerOutPut(AXP192_DCDC1, AXP202_ON); + PMU.setPowerOutPut(AXP192_LDO2, AXP202_ON); + PMU.setPowerOutPut(AXP192_LDO3, AXP202_ON); + + pinMode(PMU_IRQ, INPUT_PULLUP); + attachInterrupt(PMU_IRQ, [] { + // pmu_irq = true; + }, FALLING); + + PMU.adc1Enable(AXP202_VBUS_VOL_ADC1 | + AXP202_VBUS_CUR_ADC1 | + AXP202_BATT_CUR_ADC1 | + AXP202_BATT_VOL_ADC1, + AXP202_ON); + + PMU.enableIRQ(AXP202_VBUS_REMOVED_IRQ | + AXP202_VBUS_CONNECT_IRQ | + AXP202_BATT_REMOVED_IRQ | + AXP202_BATT_CONNECT_IRQ, + AXP202_ON); + PMU.clearIRQ(); + + return true; +} + +void disablePeripherals() +{ + PMU.setPowerOutPut(AXP192_DCDC1, AXP202_OFF); + PMU.setPowerOutPut(AXP192_LDO2, AXP202_OFF); + PMU.setPowerOutPut(AXP192_LDO3, AXP202_OFF); +} +#else +#define initPMU() +#define disablePeripherals() +#endif + +SPIClass SDSPI(HSPI); + + +void initBoard() +{ + Serial.begin(115200); + Serial.println("initBoard"); + SPI.begin(RADIO_SCLK_PIN, RADIO_MISO_PIN, RADIO_MOSI_PIN); + Wire.begin(I2C_SDA, I2C_SCL); + +#ifdef LILYGO_T3_S3_V1_0 + pinMode(RADIO_TX_PIN, OUTPUT); + pinMode(RADIO_RX_PIN, OUTPUT); + digitalWrite(RADIO_TX_PIN, LOW); + digitalWrite(RADIO_RX_PIN, HIGH); +#endif + +#ifdef HAS_GPS + Serial1.begin(GPS_BAUD_RATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN); +#endif + +#if OLED_RST + pinMode(OLED_RST, OUTPUT); + digitalWrite(OLED_RST, HIGH); delay(20); + digitalWrite(OLED_RST, LOW); delay(20); + digitalWrite(OLED_RST, HIGH); delay(20); +#endif + + initPMU(); + + +#ifdef BOARD_LED + /* + * T-BeamV1.0, V1.1 LED defaults to low level as trun on, + * so it needs to be forced to pull up + * * * * */ +#if LED_ON == LOW + gpio_hold_dis(GPIO_NUM_4); +#endif + pinMode(BOARD_LED, OUTPUT); + ledTicker.attach_ms(500, []() { + static bool level; + digitalWrite(BOARD_LED, level); + level = !level; + }); +#endif + + +#ifdef HAS_DISPLAY + Wire.beginTransmission(0x3C); + if (Wire.endTransmission() == 0) { + Serial.println("Started OLED"); + u8g2 = new U8G2_SSD1306_128X64_NONAME_F_HW_I2C(U8G2_R0, U8X8_PIN_NONE); + u8g2->begin(); + u8g2->clearBuffer(); + u8g2->setFlipMode(0); + u8g2->setFontMode(1); // Transparent + u8g2->setDrawColor(1); + u8g2->setFontDirection(0); + u8g2->firstPage(); + do { + u8g2->setFont(u8g2_font_inb19_mr); + u8g2->drawStr(0, 30, "LilyGo"); + u8g2->drawHLine(2, 35, 47); + u8g2->drawHLine(3, 36, 47); + u8g2->drawVLine(45, 32, 12); + u8g2->drawVLine(46, 33, 12); + u8g2->setFont(u8g2_font_inb19_mf); + u8g2->drawStr(58, 60, "LoRa"); + } while ( u8g2->nextPage() ); + u8g2->sendBuffer(); + u8g2->setFont(u8g2_font_fur11_tf); + delay(3000); + } +#endif + + +#ifdef HAS_SDCARD + if (u8g2) { + u8g2->setFont(u8g2_font_ncenB08_tr); + } + pinMode(SDCARD_MISO, INPUT_PULLUP); + SDSPI.begin(SDCARD_SCLK, SDCARD_MISO, SDCARD_MOSI, SDCARD_CS); + if (u8g2) { + u8g2->clearBuffer(); + } + + if (!SD.begin(SDCARD_CS, SDSPI)) { + + Serial.println("setupSDCard FAIL"); + if (u8g2) { + do { + u8g2->setCursor(0, 16); + u8g2->println( "SDCard FAILED");; + } while ( u8g2->nextPage() ); + } + + } else { + uint32_t cardSize = SD.cardSize() / (1024 * 1024); + if (u8g2) { + do { + u8g2->setCursor(0, 16); + u8g2->print( "SDCard:");; + u8g2->print(cardSize / 1024.0);; + u8g2->println(" GB");; + } while ( u8g2->nextPage() ); + } + + Serial.print("setupSDCard PASS . SIZE = "); + Serial.print(cardSize / 1024.0); + Serial.println(" GB"); + } + if (u8g2) { + u8g2->sendBuffer(); + } + delay(3000); +#endif + + +} + + diff --git a/examples/RadioLibExamples/SX1280/Ranging_Slave/utilities.h b/examples/RadioLibExamples/SX1280/Ranging_Slave/utilities.h new file mode 100644 index 0000000..3c05e13 --- /dev/null +++ b/examples/RadioLibExamples/SX1280/Ranging_Slave/utilities.h @@ -0,0 +1,47 @@ + +#pragma once + +/* +* This sample program only supports SX1280 +* */ + +#define I2C_SDA 18 +#define I2C_SCL 17 +#define OLED_RST UNUSE_PIN + +#define RADIO_SCLK_PIN 5 +#define RADIO_MISO_PIN 3 +#define RADIO_MOSI_PIN 6 +#define RADIO_CS_PIN 7 +#define RADIO_DIO1_PIN 9 +#define RADIO_DIO2_PIN 33 +#define RADIO_DIO3_PIN 34 +#define RADIO_RST_PIN 8 +#define RADIO_BUSY_PIN 36 + +#define RADIO_RX_PIN 10 +#define RADIO_TX_PIN 21 + +#define SDCARD_MOSI 11 +#define SDCARD_MISO 2 +#define SDCARD_SCLK 14 +#define SDCARD_CS 13 + +#define BOARD_LED 37 +#define LED_ON HIGH + +#define BAT_ADC_PIN 1 +#define BUTTON_PIN 0 + +#define HAS_SDCARD +#define HAS_DISPLAY + + + + + + + + + + diff --git a/platformio.ini b/platformio.ini index 9fa1463..f5ab488 100644 --- a/platformio.ini +++ b/platformio.ini @@ -89,6 +89,11 @@ ; src_dir = examples/RadioLibExamples/Transmit_Interrupt ; src_dir = examples/RadioLibExamples/SX1280_Ranging +; ! SX1280 Ranging examples just only support T3_S3_V1_2_SX1280 or T3_S3_V1_2_SX1280_PA +; ! Using open source code, no guarantee of accuracy +; src_dir = examples/RadioLibExamples/SX1280/Ranging_Master +; src_dir = examples/RadioLibExamples/SX1280/Ranging_Slave + ; ! LMIC_Library_OTTA only support SX1276 radio model , other model and T3_V1_6_SX1276_TCXO not support ; src_dir = examples/LoRaWAN/LMIC_Library_OTTA