From 13e635b74eed27adb20cee7f437c64a8addcdc45 Mon Sep 17 00:00:00 2001 From: lewishe Date: Mon, 10 Oct 2022 11:50:18 +0800 Subject: [PATCH 1/9] Add judgment on t-beam-s3-core to scanI2Cdevice in main.cpp --- src/main.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index c3f892bce..c9ed47818 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -234,7 +234,11 @@ void setup() #endif // We need to scan here to decide if we have a screen for nodeDB.init() - scanI2Cdevice(); + // In T-Beam-S3-core, the I2C device cannot be scanned before power initialization, otherwise the device will be stuck + if ((HW_VENDOR != HardwareModel_LILYGO_TBEAM_S3_CORE)) { + scanI2Cdevice(); + } + #ifdef RAK4630 // scanEInkDevice(); #endif From 30a87e31457f9bf80ab2c842c303b50f8b766c23 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Thu, 10 Nov 2022 07:26:28 -0600 Subject: [PATCH 2/9] Switch to a smaller sample of devices for cppcheck --- .github/workflows/main_matrix.yml | 9 --------- protobufs | 2 +- src/mesh/generated/mesh.pb.h | 4 ++-- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/.github/workflows/main_matrix.yml b/.github/workflows/main_matrix.yml index 62a2477bf..15a372f29 100644 --- a/.github/workflows/main_matrix.yml +++ b/.github/workflows/main_matrix.yml @@ -23,26 +23,17 @@ jobs: matrix: include: - board: rak11200 - - board: tlora-v2 - board: tlora-v1 - - board: tlora_v1_3 - board: tlora-v2-1-1.6 - board: tbeam - - board: heltec-v1 - - board: heltec-v2.0 - board: heltec-v2.1 - - board: tbeam0.7 - board: meshtastic-diy-v1 - - board: meshtastic-dr-dev - board: rak4631 - - board: rak4631_eink - board: t-echo - board: nano-g1 - board: station-g1 - - board: m5stack-core - board: m5stack-coreink - board: tbeam-s3-core - - board: feather_diy # - board: pico runs-on: ubuntu-latest diff --git a/protobufs b/protobufs index c82c15aac..5b892e4c1 160000 --- a/protobufs +++ b/protobufs @@ -1 +1 @@ -Subproject commit c82c15aac71b9134d96c03dbe319916739cc8314 +Subproject commit 5b892e4c196f8620f2009cdef219eb2c237cc636 diff --git a/src/mesh/generated/mesh.pb.h b/src/mesh/generated/mesh.pb.h index c1c2bdd7b..8e73a0972 100644 --- a/src/mesh/generated/mesh.pb.h +++ b/src/mesh/generated/mesh.pb.h @@ -501,7 +501,7 @@ typedef PB_BYTES_ARRAY_T(256) MeshPacket_encrypted_t; typedef struct _MeshPacket { /* The sending node number. Note: Our crypto implementation uses this field as well. - See [crypto](/docs/about/overview/encryption) for details. + See [crypto](/docs/overview/encryption) for details. FIXME - really should be fixed32 instead, this encoding only hurts the ble link though. */ uint32_t from; /* The (immediatSee Priority description for more details.y should be fixed32 instead, this encoding only @@ -529,7 +529,7 @@ typedef struct _MeshPacket { needs to be unique for a few minutes (long enough to last for the length of any ACK or the completion of a mesh broadcast flood). Note: Our crypto implementation uses this id as well. - See [crypto](/docs/about/overview/encryption) for details. + See [crypto](/docs/overview/encryption) for details. FIXME - really should be fixed32 instead, this encoding only hurts the ble link though. */ uint32_t id; From 3737252d39a5ca8ab7bbca4040846a97fbb4da9c Mon Sep 17 00:00:00 2001 From: puzzled-pancake <78745145+puzzled-pancake@users.noreply.github.com> Date: Sat, 12 Nov 2022 17:55:28 +1300 Subject: [PATCH 3/9] Update NZ_865 to 36dBm Updated NZ_865 to 36dBm as per: https://rrf.rsm.govt.nz/smart-web/smart/page/-smart/domain/licence/LicenceSummary.wdk?id=219752 6dBW = 36dBm/4watt --- src/mesh/RadioInterface.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/RadioInterface.cpp b/src/mesh/RadioInterface.cpp index 97dd66fae..832877dbd 100644 --- a/src/mesh/RadioInterface.cpp +++ b/src/mesh/RadioInterface.cpp @@ -89,7 +89,7 @@ const RegionInfo regions[] = { https://rrf.rsm.govt.nz/smart-web/smart/page/-smart/domain/licence/LicenceSummary.wdk?id=219752 https://iotalliance.org.nz/wp-content/uploads/sites/4/2019/05/IoT-Spectrum-in-NZ-Briefing-Paper.pdf */ - RDEF(NZ_865, 864.0f, 868.0f, 100, 0, 0, true, false, false), + RDEF(NZ_865, 864.0f, 868.0f, 100, 0, 36, true, false, false), /* https://lora-alliance.org/wp-content/uploads/2020/11/lorawan_regional_parameters_v1.0.3reva_0.pdf From 68ccebafbf0e1f67658e3280985bfc8cbbeda174 Mon Sep 17 00:00:00 2001 From: lewishe Date: Sat, 12 Nov 2022 14:38:48 +0800 Subject: [PATCH 4/9] Add low-speed crystal initialization for ESP32S3 --- src/platform/esp32/main-esp32.cpp | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/platform/esp32/main-esp32.cpp b/src/platform/esp32/main-esp32.cpp index 9e8990d97..c5c493997 100644 --- a/src/platform/esp32/main-esp32.cpp +++ b/src/platform/esp32/main-esp32.cpp @@ -14,6 +14,7 @@ #include #include #include +#include "soc/rtc.h" NimbleBluetooth *nimbleBluetooth; @@ -36,6 +37,44 @@ void setBluetoothEnable(bool on) { } } +#ifdef HAS_32768HZ +#define CALIBRATE_ONE(cali_clk) calibrate_one(cali_clk, #cali_clk) + +static uint32_t calibrate_one(rtc_cal_sel_t cal_clk, const char *name) +{ + const uint32_t cal_count = 1000; + const float factor = (1 << 19) * 1000.0f; + uint32_t cali_val; + for (int i = 0; i < 5; ++i) { + cali_val = rtc_clk_cal(cal_clk, cal_count); + } + return cali_val; +} + +void enableSlowCLK() +{ + rtc_clk_32k_enable(true); + + CALIBRATE_ONE(RTC_CAL_RTC_MUX); + uint32_t cal_32k = CALIBRATE_ONE(RTC_CAL_32K_XTAL); + + if (cal_32k == 0) { + DEBUG_MSG("32K XTAL OSC has not started up\n"); + } else { + rtc_clk_slow_freq_set(RTC_SLOW_FREQ_32K_XTAL); + DEBUG_MSG("Switching RTC Source to 32.768Khz succeeded, using 32K XTAL\n"); + CALIBRATE_ONE(RTC_CAL_RTC_MUX); + CALIBRATE_ONE(RTC_CAL_32K_XTAL); + } + CALIBRATE_ONE(RTC_CAL_RTC_MUX); + CALIBRATE_ONE(RTC_CAL_32K_XTAL); + if (rtc_clk_slow_freq_get() != RTC_SLOW_FREQ_32K_XTAL) { + DEBUG_MSG("Warning: Failed to switch 32K XTAL RTC source to 32.768Khz !!! \n"); return; + } +} +#endif + + void esp32Setup() { uint32_t seed = esp_random(); @@ -83,6 +122,10 @@ void esp32Setup() res = esp_task_wdt_add(NULL); assert(res == ESP_OK); + +#ifdef HAS_32768HZ + enableSlowCLK(); +#endif } #if 0 From b68a026627eadf0dd47afee156bfae914b4fc779 Mon Sep 17 00:00:00 2001 From: lewishe Date: Sat, 12 Nov 2022 14:39:42 +0800 Subject: [PATCH 5/9] Update tbeam-s3 variant mapping --- variants/tbeam-s3-core/pins_arduino.h | 18 +++++++++++++----- variants/tbeam-s3-core/variant.h | 19 +++++++++++-------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/variants/tbeam-s3-core/pins_arduino.h b/variants/tbeam-s3-core/pins_arduino.h index 50c993f3a..5030d1d23 100644 --- a/variants/tbeam-s3-core/pins_arduino.h +++ b/variants/tbeam-s3-core/pins_arduino.h @@ -17,20 +17,28 @@ static const uint8_t TX = 43; static const uint8_t RX = 44; +// The default Wire will be mapped to PMU and RTC static const uint8_t SDA = 42; static const uint8_t SCL = 41; +// Default SPI will be mapped to Radio static const uint8_t SS = 10; static const uint8_t MOSI = 11; static const uint8_t MISO = 13; static const uint8_t SCK = 12; -#define SDMMC_CMD (35) -#define SDMMC_CLK (36) -#define SDMMC_DATA (37) +// Another SPI bus shares SD card and QMI8653 inertial measurement sensor +#define SPI_MOSI (35) +#define SPI_SCK (36) +#define SPI_MISO (37) +#define SPI_CS (47) +#define IMU_CS (34) -#define ACCEL_INT1 (34) -#define ACCEL_INT2 (33) +#define SDCARD_CS SPI_CS +#define IMU_INT (33) +// #define PMU_IRQ (40) #define RTC_INT (14) + + #endif /* Pins_Arduino_h */ diff --git a/variants/tbeam-s3-core/variant.h b/variants/tbeam-s3-core/variant.h index 63387c00a..9aaba6010 100644 --- a/variants/tbeam-s3-core/variant.h +++ b/variants/tbeam-s3-core/variant.h @@ -1,20 +1,18 @@ // #define BUTTON_NEED_PULLUP // if set we need to turn on the internal CPU pullup during sleep -#define I2C_SDA1 42 //Used for PMU management -#define I2C_SCL1 41 //Used for PMU management +#define I2C_SDA1 42 //Used for PMU management and PCF8563 +#define I2C_SCL1 41 //Used for PMU management and PCF8563 -#define I2C_SDA 17 //For sensors and screens -#define I2C_SCL 18 //For sensors and screens +#define I2C_SDA 17 //For QMC6310 sensors and screens +#define I2C_SCL 18 //For QMC6310 sensors and screens #define BUTTON_PIN 0 // The middle button GPIO on the T-Beam S3 //#define BUTTON_PIN_ALT 13 // Alternate GPIO for an external button if needed. Does anyone use this? It is not documented anywhere. // #define EXT_NOTIFY_OUT 13 // Default pin to use for Ext Notify Module. -#define BUTTON1_PIN 47 //Additional keys #define LED_INVERTED 1 -// #define LED_PIN 4 // Newer tbeams (1.1) have an extra led on GPIO4 // TTGO uses a common pinout for their SX1262 vs RF95 modules - both can be enabled and we will probe at runtime for RF95 and if // not found then probe for SX1262 @@ -42,8 +40,9 @@ // #define PMU_IRQ 40 #define HAS_AXP2101 -// Specify the PMU as Wire1. In the t-beam-s3-core, the PMU enjoys a separate bus +// Specify the PMU as Wire1. In the t-beam-s3 core, PCF8563 and PMU share the bus #define PMU_USE_WIRE1 +#define RTC_USE_WIRE1 #define RF95_SCK 12 #define RF95_MISO 13 @@ -56,10 +55,14 @@ #define GPS_1PPS_PIN 6 -#define HAS_SDCARD //Have 3-bit SDMMC interface SD card slot +#define HAS_SDCARD //Have SPI interface SD card slot +#define SDCARD_USE_SPI1 // PCF8563 RTC Module // #define PCF8563_RTC 0x51 //Putting definitions in variant. h does not compile correctly #define HAS_RTC 1 +//has 32768 Hz crystal +#define HAS_32768HZ + From cd95d0865fad4d5de887995664f6e52d0f412986 Mon Sep 17 00:00:00 2001 From: lewishe Date: Sat, 12 Nov 2022 15:12:53 +0800 Subject: [PATCH 6/9] Repair the sharing of Wire1 between RTC and PMU in tbeams3 --- src/gps/RTC.cpp | 10 +++++++++- src/main.cpp | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/gps/RTC.cpp b/src/gps/RTC.cpp index 9053f5b2b..e04f903ee 100644 --- a/src/gps/RTC.cpp +++ b/src/gps/RTC.cpp @@ -44,7 +44,11 @@ void readFromRTC() if(rtc_found == PCF8563_RTC) { uint32_t now = millis(); PCF8563_Class rtc; +#ifdef RTC_USE_WIRE1 + rtc.begin(Wire1); +#else rtc.begin(); +#endif auto tc = rtc.getDateTime(); tm t; t.tm_year = tc.year - 1900; @@ -110,7 +114,11 @@ bool perhapsSetRTC(RTCQuality q, const struct timeval *tv) #elif defined(PCF8563_RTC) if(rtc_found == PCF8563_RTC) { PCF8563_Class rtc; - rtc.begin(); +#ifdef RTC_USE_WIRE1 + rtc.begin(Wire1); +#else + rtc.begin(); +#endif tm *t = localtime(&tv->tv_sec); rtc.setDateTime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); DEBUG_MSG("PCF8563_RTC setDateTime %02d-%02d-%02d %02d:%02d:%02d %ld\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, tv->tv_sec); diff --git a/src/main.cpp b/src/main.cpp index d4d76cc3b..f9a475af0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -246,6 +246,13 @@ void setup() // In T-Beam-S3-core, the I2C device cannot be scanned before power initialization, otherwise the device will be stuck if ((HW_VENDOR != HardwareModel_LILYGO_TBEAM_S3_CORE)) { scanI2Cdevice(); + }else{ + // PCF8563 RTC in tbeam-s3 uses Wire1 to share I2C bus + Wire1.beginTransmission(PCF8563_RTC); + if (Wire1.endTransmission() == 0){ + rtc_found = PCF8563_RTC; + DEBUG_MSG("PCF8563 RTC found\n"); + } } #ifdef RAK4630 From 61ebdb336723a26a3159d42ac8aef9c748dd4c6b Mon Sep 17 00:00:00 2001 From: lewishe Date: Sat, 12 Nov 2022 15:14:13 +0800 Subject: [PATCH 7/9] Add SD card initialization --- src/FSCommon.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/FSCommon.h | 1 + src/main.cpp | 4 ++++ 3 files changed, 53 insertions(+) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index a4fa3c3ae..56e551047 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -1,6 +1,18 @@ #include "configuration.h" #include "FSCommon.h" +#ifdef HAS_SDCARD +#include +#include + + +#ifdef SDCARD_USE_SPI1 +SPIClass SPI1(HSPI); +#define SDHandler SPI1 +#endif + + +#endif //HAS_SDCARD bool copyFile(const char* from, const char* to) { @@ -169,3 +181,39 @@ void fsInit() listDir("/", 10); #endif } + + +void setupSDCard() +{ +#ifdef HAS_SDCARD + SDHandler.begin(SPI_SCK, SPI_MISO, SPI_MOSI); + + if (!SD.begin(SDCARD_CS, SDHandler)) { + DEBUG_MSG("No SD_MMC card detected\n"); + return ; + } + uint8_t cardType = SD.cardType(); + if (cardType == CARD_NONE) { + DEBUG_MSG("No SD_MMC card attached\n"); + return ; + } + DEBUG_MSG("SD_MMC Card Type: "); + if (cardType == CARD_MMC) { + DEBUG_MSG("MMC\n"); + } else if (cardType == CARD_SD) { + DEBUG_MSG("SDSC\n"); + } else if (cardType == CARD_SDHC) { + DEBUG_MSG("SDHC\n"); + } else { + DEBUG_MSG("UNKNOWN\n"); + } + + uint64_t cardSize = SD.cardSize() / (1024 * 1024); + DEBUG_MSG("SD Card Size: %lluMB\n", cardSize); + DEBUG_MSG("Total space: %llu MB\n", SD.totalBytes() / (1024 * 1024)); + DEBUG_MSG("Used space: %llu MB\n", SD.usedBytes() / (1024 * 1024)); +#endif +} + + + diff --git a/src/FSCommon.h b/src/FSCommon.h index 841fa5adb..e304a455b 100644 --- a/src/FSCommon.h +++ b/src/FSCommon.h @@ -44,3 +44,4 @@ bool copyFile(const char* from, const char* to); bool renameFile(const char* pathFrom, const char* pathTo); void listDir(const char * dirname, uint8_t levels, boolean del); void rmDir(const char * dirname); +void setupSDCard(); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f9a475af0..993cf2106 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -254,6 +254,10 @@ void setup() DEBUG_MSG("PCF8563 RTC found\n"); } } + +#ifdef HAS_SDCARD + setupSDCard(); +#endif #ifdef RAK4630 // scanEInkDevice(); From 43f769ebac98a67221ab5e1c2e3b5230ffb65d62 Mon Sep 17 00:00:00 2001 From: lewishe Date: Sat, 12 Nov 2022 16:01:30 +0800 Subject: [PATCH 8/9] Resolve compilation conflicts --- src/main.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 843cdaa44..543568f09 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -246,18 +246,19 @@ void setup() digitalWrite(PIN_3V3_EN, 1); #endif - // We need to scan here to decide if we have a screen for nodeDB.init() +#ifdef LILYGO_TBEAM_S3_CORE // In T-Beam-S3-core, the I2C device cannot be scanned before power initialization, otherwise the device will be stuck - if ((HW_VENDOR != HardwareModel_LILYGO_TBEAM_S3_CORE)) { - scanI2Cdevice(); - }else{ - // PCF8563 RTC in tbeam-s3 uses Wire1 to share I2C bus - Wire1.beginTransmission(PCF8563_RTC); - if (Wire1.endTransmission() == 0){ - rtc_found = PCF8563_RTC; - DEBUG_MSG("PCF8563 RTC found\n"); - } + // PCF8563 RTC in tbeam-s3 uses Wire1 to share I2C bus + Wire1.beginTransmission(PCF8563_RTC); + if (Wire1.endTransmission() == 0){ + rtc_found = PCF8563_RTC; + DEBUG_MSG("PCF8563 RTC found\n"); } +#else + // We need to scan here to decide if we have a screen for nodeDB.init() + scanI2Cdevice(); +#endif + #ifdef HAS_SDCARD setupSDCard(); From 60fdf9fcb275e8529387b87f0752bd9448f81e92 Mon Sep 17 00:00:00 2001 From: lewis Date: Sat, 12 Nov 2022 20:09:25 +0800 Subject: [PATCH 9/9] Place PMU initialization before scanI2Cdevice --- src/main.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 543568f09..0f7761b10 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -246,6 +246,15 @@ void setup() digitalWrite(PIN_3V3_EN, 1); #endif + + // Currently only the tbeam has a PMU + // PMU initialization needs to be placed before scanI2Cdevice + power = new Power(); + power->setStatusHandler(powerStatus); + powerStatus->observe(&power->newStatus); + power->setup(); // Must be after status handler is installed, so that handler gets notified of the initial configuration + + #ifdef LILYGO_TBEAM_S3_CORE // In T-Beam-S3-core, the I2C device cannot be scanned before power initialization, otherwise the device will be stuck // PCF8563 RTC in tbeam-s3 uses Wire1 to share I2C bus @@ -254,11 +263,10 @@ void setup() rtc_found = PCF8563_RTC; DEBUG_MSG("PCF8563 RTC found\n"); } -#else - // We need to scan here to decide if we have a screen for nodeDB.init() - scanI2Cdevice(); #endif + // We need to scan here to decide if we have a screen for nodeDB.init() + scanI2Cdevice(); #ifdef HAS_SDCARD setupSDCard(); @@ -298,17 +306,12 @@ void setup() playStartMelody(); - // Currently only the tbeam has a PMU - power = new Power(); - power->setStatusHandler(powerStatus); - powerStatus->observe(&power->newStatus); - power->setup(); // Must be after status handler is installed, so that handler gets notified of the initial configuration /* * Repeat the scanning for I2C devices after power initialization or look for 'latecomers'. * Boards with an PMU need to be powered on to correctly scan to the device address, such as t-beam-s3-core */ - scanI2Cdevice(); + // scanI2Cdevice(); // fixed screen override? if (config.display.oled != Config_DisplayConfig_OledType_OLED_AUTO)