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/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/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 1e95b08ad..2bf2a0de1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -244,8 +244,32 @@ 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 + Wire1.beginTransmission(PCF8563_RTC); + if (Wire1.endTransmission() == 0){ + rtc_found = PCF8563_RTC; + DEBUG_MSG("PCF8563 RTC found\n"); + } +#endif + // We need to scan here to decide if we have a screen for nodeDB.init() - scanI2Cdevice(true); + scanI2Cdevice(); + +#ifdef HAS_SDCARD + setupSDCard(); +#endif + #ifdef RAK4630 // scanEInkDevice(); #endif @@ -280,17 +304,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(false); + // scanI2Cdevice(); // fixed screen override? if (config.display.oled != Config_DisplayConfig_OledType_OLED_AUTO) diff --git a/src/mesh/RadioInterface.cpp b/src/mesh/RadioInterface.cpp index 4d24b40c4..0c68b59a2 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 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; 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 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 +