diff --git a/.github/workflows/buil_parallel.yml b/.github/workflows/buil_parallel.yml index 44a637bff..148230ba0 100644 --- a/.github/workflows/buil_parallel.yml +++ b/.github/workflows/buil_parallel.yml @@ -192,6 +192,20 @@ jobs: family: "ESP32-S3", partitions: { bootloader_addr: "0x0" }, } + - { + vendor: "Lilygo", + name: "Lilygo T-Display-S3 (SD_MMC)", + env: "lilygo-t-display-s3-mmc", + family: "ESP32-S3", + partitions: { bootloader_addr: "0x0" }, + } + - { + vendor: "Lilygo", + name: "Lilygo T-Display-S3 Touch (SD_MMC)", + env: "lilygo-t-display-s3-touch-mmc", + family: "ESP32-S3", + partitions: { bootloader_addr: "0x0" }, + } - { vendor: "Smoochiee", name: "Smoochiee-Board", diff --git a/boards/_boards_json/lilygo-t-display-s3.json b/boards/_boards_json/lilygo-t-display-s3.json index 26355fff1..011665b8e 100644 --- a/boards/_boards_json/lilygo-t-display-s3.json +++ b/boards/_boards_json/lilygo-t-display-s3.json @@ -11,6 +11,7 @@ "-DBOARD_HAS_PSRAM", "-DARDUINO_RUNNING_CORE=1", "-DARDUINO_EVENT_RUNNING_CORE=1", + "-DARDUINO_USB_CDC_ON_BOOT", "-DARDUINO_USB_MODE=1" ], "f_cpu": "240000000L", @@ -18,16 +19,8 @@ "flash_mode": "qio", "hwids": [ [ - "0x239A", - "0x811B" - ], - [ - "0x239A", - "0x011B" - ], - [ - "0x239A", - "0x811C" + "0X303A", + "0x1001" ] ], "mcu": "esp32s3", @@ -38,11 +31,7 @@ "wifi" ], "debug": { - "default_tool": "esp-builtin", - "onboard_tools": [ - "esp-builtin" - ], - "openocd_target": "esp32s3.cfg" + "openocd_target": "esp32s3-builtin.cfg" }, "frameworks": [ "arduino", @@ -54,8 +43,8 @@ "maximum_ram_size": 327680, "maximum_size": 16777216, "require_upload_port": true, - "speed": 460800 + "speed": 921600 }, - "url": "https://www.lilygo.cc", - "vendor": "LilyGo" -} \ No newline at end of file + "url": "https://www.lilygo.cc/products/t-display-s3", + "vendor": "LILYGO" +} diff --git a/boards/lilygo-t-display-s3/interface.cpp b/boards/lilygo-t-display-s3/interface.cpp index e8637e177..bf19440dc 100644 --- a/boards/lilygo-t-display-s3/interface.cpp +++ b/boards/lilygo-t-display-s3/interface.cpp @@ -3,10 +3,11 @@ #include #include -// #include -// #define PIN_SD_CMD 13 -// #define PIN_SD_CLK 11 -// #define PIN_SD_D0 12 +#ifdef USE_SD_MMC +#define PIN_SD_CMD 13 +#define PIN_SD_CLK 11 +#define PIN_SD_D0 12 +#endif #ifdef HAS_TOUCH #define TOUCH_MODULES_CST_SELF #include @@ -41,7 +42,9 @@ Button *btn2; void _setup_gpio() { #ifdef HAS_TOUCH - // SD_MMC.setPins(PIN_SD_CLK, PIN_SD_CMD, PIN_SD_D0); +#ifdef USE_SD_MMC + SD.setPins(PIN_SD_CLK, PIN_SD_CMD, PIN_SD_D0); +#endif gpio_hold_dis((gpio_num_t)21); // PIN_TOUCH_RES pinMode(15, OUTPUT); @@ -60,21 +63,19 @@ void _setup_gpio() { .type = BUTTON_TYPE_GPIO, .long_press_time = 600, .short_press_time = 120, - .gpio_button_config = - { - .gpio_num = DW_BTN, - .active_level = 0, - }, + .gpio_button_config = { + .gpio_num = DW_BTN, + .active_level = 0, + }, }; button_config_t bt2 = { .type = BUTTON_TYPE_GPIO, .long_press_time = 600, .short_press_time = 120, - .gpio_button_config = - { - .gpio_num = UP_BTN, - .active_level = 0, - }, + .gpio_button_config = { + .gpio_num = UP_BTN, + .active_level = 0, + }, }; pinMode(SEL_BTN, INPUT_PULLUP); @@ -105,6 +106,8 @@ void _setup_gpio() { bruceConfig.irRx = RXLED; bruceConfig.irTx = LED; + + Serial.begin(115200); } /*************************************************************************************** diff --git a/boards/lilygo-t-display-s3/lilygo-t-display-s3.ini b/boards/lilygo-t-display-s3/lilygo-t-display-s3.ini index d185d26fd..a392eeaea 100644 --- a/boards/lilygo-t-display-s3/lilygo-t-display-s3.ini +++ b/boards/lilygo-t-display-s3/lilygo-t-display-s3.ini @@ -10,32 +10,11 @@ build_flags = -DREDRAW_DELAY=1 # Used to improve navigation on menus for this device -DMIC_SPM1423 - -UARDUINO_USB_CDC_ON_BOOT #Turn off CDC on boot https://github.com/Xinyuan-LilyGO/T-Display-S3 + -DALLOW_ALL_GPIO_FOR_IR_RF=1 ; Set this option to make use of all GPIOs, from 1 to 44 to be chosen, except TFT and SD pins + + ;-UARDUINO_USB_CDC_ON_BOOT #Turn off CDC on boot https://github.com/Xinyuan-LilyGO/T-Display-S3 -DDISABLE_ALL_LIBRARY_WARNINGS - ;Infrared Led default pin and state - -DIR_TX_PINS='{{"Pin 43", 43}, {"Pin 44", 44}}' - -DIR_RX_PINS='{{"Pin 43", 43}, {"Pin 44", 44}}' - - ;Radio Frequency (one pin modules) pin setting - -DRF_TX_PINS='{{"Pin 43", 43}, {"Pin 44", 44}}' - -DRF_RX_PINS='{{"Pin 43", 43}, {"Pin 44", 44}}' - - ;CC1101 support SPI - -DUSE_CC1101_VIA_SPI - -DCC1101_GDO0_PIN=44 - -DCC1101_SS_PIN=43 - -DCC1101_MOSI_PIN=SPI_MOSI_PIN - -DCC1101_SCK_PIN=SPI_SCK_PIN - -DCC1101_MISO_PIN=SPI_MISO_PIN - - ;NRF24 support SPI - -DUSE_NRF24_VIA_SPI - -DNRF24_CE_PIN=3 - -DNRF24_SS_PIN=SPI_SS_PIN - -DNRF24_MOSI_PIN=SPI_MOSI_PIN - -DNRF24_SCK_PIN=SPI_SCK_PIN - -DNRF24_MISO_PIN=SPI_MISO_PIN lib_deps = ${env.lib_deps} fastled/FastLED @3.9.4 @@ -48,3 +27,22 @@ extends = env:lilygo-t-display-s3 build_flags = ${env:lilygo-t-display-s3.build_flags} -DHAS_TOUCH=1 + +[env:lilygo-t-display-s3-mmc] +extends = env:lilygo-t-display-s3 +build_flags = + ${env:lilygo-t-display-s3.build_flags} + -DUSE_SD_MMC=1 +lib_ignore = + SD + + +[env:lilygo-t-display-s3-touch-mmc] +extends = env:lilygo-t-display-s3 +build_flags = + ${env:lilygo-t-display-s3.build_flags} + -DHAS_TOUCH=1 + -DUSE_SD_MMC=1 +lib_ignore = + SD + diff --git a/boards/lilygo-t-display-s3/pins_arduino.h b/boards/lilygo-t-display-s3/pins_arduino.h index a068c0ee8..9d18d4e98 100644 --- a/boards/lilygo-t-display-s3/pins_arduino.h +++ b/boards/lilygo-t-display-s3/pins_arduino.h @@ -7,19 +7,69 @@ // Lite Version // #define LITE_VERSION 1 -// Main I2C Bus +// Main SPI Bus +#ifdef USE_SD_MMC + +// Willy board definitions +#define SPI_SS_PIN 1 +#define SPI_MOSI_PIN 3 +#define SPI_MISO_PIN 2 +#define SPI_SCK_PIN 43 + +#define SDCARD_CS -1 +#define SDCARD_SCK -1 +#define SDCARD_MISO -1 +#define SDCARD_MOSI -1 + +#define USE_CC1101_VIA_SPI +#define CC1101_GDO0_PIN 44 +#define CC1101_SS_PIN 1 +#define CC1101_MOSI_PIN 3 +#define CC1101_SCK_PIN 43 +#define CC1101_MISO_PIN 2 + +#define USE_NRF24_VIA_SPI +#define NRF24_CE_PIN 44 +#define NRF24_SS_PIN 1 +#define NRF24_MOSI_PIN 3 +#define NRF24_SCK_PIN 43 +#define NRF24_MISO_PIN 2 + +#else + #define SPI_SS_PIN 10 #define SPI_MOSI_PIN 11 #define SPI_MISO_PIN 13 #define SPI_SCK_PIN 12 + +#define SDCARD_CS SPI_SS_PIN +#define SDCARD_SCK SPI_SCK_PIN +#define SDCARD_MISO SPI_MISO_PIN +#define SDCARD_MOSI SPI_MOSI_PIN + +#define USE_CC1101_VIA_SPI +#define CC1101_GDO0_PIN 44 +#define CC1101_SS_PIN 43 +#define CC1101_MOSI_PIN SPI_MOSI_PIN +#define CC1101_SCK_PIN SPI_SCK_PIN +#define CC1101_MISO_PIN SPI_MISO_PIN + +#define USE_NRF24_VIA_SPI +#define NRF24_CE_PIN 3 +#define NRF24_SS_PIN SPI_SS_PIN +#define NRF24_MOSI_PIN SPI_MOSI_PIN +#define NRF24_SCK_PIN SPI_SCK_PIN +#define NRF24_MISO_PIN SPI_MISO_PIN +#endif + static const uint8_t SS = SPI_SS_PIN; static const uint8_t MOSI = SPI_MOSI_PIN; static const uint8_t SCK = SPI_MISO_PIN; static const uint8_t MISO = SPI_SCK_PIN; // Set Main I2C Bus -#define GROVE_SDA 44 -#define GROVE_SCL 43 +#define GROVE_SDA 17 +#define GROVE_SCL 16 static const uint8_t SDA = GROVE_SDA; static const uint8_t SCL = GROVE_SCL; @@ -64,8 +114,8 @@ static const uint8_t SCL = GROVE_SCL; #define FG 3 // Serial -#define SERIAL_TX 43 -#define SERIAL_RX 44 +#define SERIAL_TX 16 +#define SERIAL_RX 17 // Battery PIN #define PIN_POWER_ON 15 @@ -81,12 +131,16 @@ static const uint8_t SCL = GROVE_SCL; #define SEL_BTN 16 #define UP_BTN 0 #define DW_BTN 14 -#define BK_BTN 3 #define BTN_ACT LOW // IR pins +#ifdef USE_SD_MMC +#define LED 10 +#define RXLED 44 +#else #define LED 44 #define RXLED 43 +#endif #define LED_ON HIGH #define LED_OFF LOW diff --git a/lib/HAL/SD.h b/lib/HAL/SD.h new file mode 100644 index 000000000..ff0ffd52f --- /dev/null +++ b/lib/HAL/SD.h @@ -0,0 +1,93 @@ +#ifndef __SDM__H +#define __SDM__H +#include + +// #define USE_SD_MMC // test, delete later + +#ifdef USE_SD_MMC +#include "sdkconfig.h" +#include "soc/soc_caps.h" +#ifndef SOC_SDMMC_HOST_SUPPORTED + +#ifdef ARDUINO +#warning The SDMMC library requires a device with an SDIO Host +#endif + +#else +#include "driver/sdmmc_types.h" +#include "sd_defines.h" +#ifndef BOARD_MAX_SDMMC_FREQ +#define BOARD_MAX_SDMMC_FREQ SDMMC_FREQ_HIGHSPEED +#endif +#endif + +#else +#include +#include +#endif + +namespace fs { + +class SDFS : public FS { +#ifdef USE_SD_MMC +protected: + sdmmc_card_t *_card; + int8_t _pin_clk = -1; + int8_t _pin_cmd = -1; + int8_t _pin_d0 = -1; + int8_t _pin_d1 = -1; + int8_t _pin_d2 = -1; + int8_t _pin_d3 = -1; +#ifdef SOC_SDMMC_IO_POWER_EXTERNAL + int8_t _power_channel = -1; +#endif + uint8_t _pdrv = 0xFF; + bool _mode1bit = false; + +private: + static bool sdmmcDetachBus(void *bus_pointer); + +public: + bool setPins(int clk, int cmd, int d0); + bool setPins(int clk, int cmd, int d0, int d1, int d2, int d3); +#ifdef SOC_SDMMC_IO_POWER_EXTERNAL + bool setPowerChannel(int power_channel); +#endif + bool begin( + const char *mountpoint = "/sdcard", bool mode1bit = false, bool format_if_mount_failed = false, + int sdmmc_frequency = BOARD_MAX_SDMMC_FREQ, uint8_t maxOpenFiles = 5 + ); + +#else +protected: + uint8_t _pdrv; + +public: + bool begin( + uint8_t ssPin = SS, SPIClass &spi = SPI, uint32_t frequency = 4000000, const char *mountpoint = "/sd", + uint8_t max_files = 5, bool format_if_empty = false + ); +#endif + + SDFS(FSImplPtr impl); + + void end(); + sdcard_type_t cardType(); + uint64_t cardSize(); + size_t numSectors(); + size_t sectorSize(); + uint64_t totalBytes(); + uint64_t usedBytes(); + bool readRAW(uint8_t *buffer, uint32_t sector); + bool writeRAW(uint8_t *buffer, uint32_t sector); +}; + +} // namespace fs + +extern fs::SDFS SD; + +using namespace fs; +typedef fs::File SDFile; +typedef fs::SDFS SDFileSystemClass; +#define SDFileSystem SD +#endif diff --git a/lib/HAL/sd_card/SD.cpp b/lib/HAL/sd_card/SD.cpp new file mode 100644 index 000000000..e6dcf2946 --- /dev/null +++ b/lib/HAL/sd_card/SD.cpp @@ -0,0 +1,113 @@ +#ifndef USE_SD_MMC +#include "SD.h" +#include "FS.h" +#include "ff.h" +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "sd_diskio.h" +#include "vfs_api.h" + +using namespace fs; + +SDFS::SDFS(FSImplPtr impl) : FS(impl), _pdrv(0xFF) {} + +bool SDFS::begin( + uint8_t ssPin, SPIClass &spi, uint32_t frequency, const char *mountpoint, uint8_t max_files, + bool format_if_empty +) { + if (_pdrv != 0xFF) { return true; } + + spi.begin(); + + _pdrv = sdcard_init(ssPin, &spi, frequency); + if (_pdrv == 0xFF) { return false; } + + if (!sdcard_mount(_pdrv, mountpoint, max_files, format_if_empty)) { + sdcard_unmount(_pdrv); + sdcard_uninit(_pdrv); + _pdrv = 0xFF; + return false; + } + + _impl->mountpoint(mountpoint); + return true; +} + +void SDFS::end() { + if (_pdrv != 0xFF) { + _impl->mountpoint(NULL); + sdcard_unmount(_pdrv); + + sdcard_uninit(_pdrv); + _pdrv = 0xFF; + } +} + +sdcard_type_t SDFS::cardType() { + if (_pdrv == 0xFF) { return CARD_NONE; } + return sdcard_type(_pdrv); +} + +uint64_t SDFS::cardSize() { + if (_pdrv == 0xFF) { return 0; } + size_t sectors = sdcard_num_sectors(_pdrv); + size_t sectorSize = sdcard_sector_size(_pdrv); + return (uint64_t)sectors * sectorSize; +} + +size_t SDFS::numSectors() { + if (_pdrv == 0xFF) { return 0; } + return sdcard_num_sectors(_pdrv); +} + +size_t SDFS::sectorSize() { + if (_pdrv == 0xFF) { return 0; } + return sdcard_sector_size(_pdrv); +} + +uint64_t SDFS::totalBytes() { + FATFS *fsinfo; + DWORD fre_clust; + char drv[3] = {(char)(48 + _pdrv), ':', 0}; + if (f_getfree(drv, &fre_clust, &fsinfo) != 0) return 0; + uint64_t size = ((uint64_t)(fsinfo->csize)) * (fsinfo->n_fatent - 2) +#if _MAX_SS != 512 + * (fsinfo->ssize); +#else + * 512; +#endif + return size; +} + +uint64_t SDFS::usedBytes() { + FATFS *fsinfo; + DWORD fre_clust; + char drv[3] = {(char)(48 + _pdrv), ':', 0}; + if (f_getfree(drv, &fre_clust, &fsinfo) != 0) return 0; + uint64_t size = ((uint64_t)(fsinfo->csize)) * ((fsinfo->n_fatent - 2) - (fsinfo->free_clst)) +#if _MAX_SS != 512 + * (fsinfo->ssize); +#else + * 512; +#endif + return size; +} + +bool SDFS::readRAW(uint8_t *buffer, uint32_t sector) { return sd_read_raw(_pdrv, buffer, sector); } + +bool SDFS::writeRAW(uint8_t *buffer, uint32_t sector) { return sd_write_raw(_pdrv, buffer, sector); } + +SDFS SD = SDFS(FSImplPtr(new VFSImpl())); +#endif diff --git a/lib/HAL/sd_card/SD_MMC.cpp b/lib/HAL/sd_card/SD_MMC.cpp new file mode 100644 index 000000000..57b76aef8 --- /dev/null +++ b/lib/HAL/sd_card/SD_MMC.cpp @@ -0,0 +1,214 @@ +#ifdef USE_SD_MMC +#include "../SD.h" +#include "FS.h" +#include "ff.h" +// Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "SD_MMC.h" +#include "io_pin_remap.h" +#include "pins_arduino.h" +#ifdef SOC_SDMMC_HOST_SUPPORTED +#include "vfs_api.h" + +#include "driver/sdmmc_defs.h" +#include "driver/sdmmc_host.h" +#include "esp_vfs_fat.h" +#include "ff.h" +#include "sdmmc_cmd.h" +#include "soc/sdmmc_pins.h" +#include + +using namespace fs; + +SDFS::SDFS(FSImplPtr impl) : FS(impl), _card(nullptr) { +#if defined(SOC_SDMMC_USE_GPIO_MATRIX) && defined(BOARD_HAS_SDMMC) + _pin_clk = SDMMC_CLK; + _pin_cmd = SDMMC_CMD; + _pin_d0 = SDMMC_D0; +#ifndef BOARD_HAS_1BIT_SDMMC + _pin_d1 = SDMMC_D1; + _pin_d2 = SDMMC_D2; + _pin_d3 = SDMMC_D3; +#endif // BOARD_HAS_1BIT_SDMMC +#endif // defined(SOC_SDMMC_USE_GPIO_MATRIX) && defined(BOARD_HAS_SDMMC) +} + +bool SDFS::setPins(int clk, int cmd, int d0) { + return setPins(clk, cmd, d0, GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC); +} + +bool SDFS::setPins(int clk, int cmd, int d0, int d1, int d2, int d3) { + if (_card != nullptr) { + log_e("SD_MMC.setPins must be called before SD_MMC.begin"); + return false; + } + + // map logical pins to GPIO numbers + clk = digitalPinToGPIONumber(clk); + cmd = digitalPinToGPIONumber(cmd); + d0 = digitalPinToGPIONumber(d0); + d1 = digitalPinToGPIONumber(d1); + d2 = digitalPinToGPIONumber(d2); + d3 = digitalPinToGPIONumber(d3); + +#ifdef SOC_SDMMC_USE_GPIO_MATRIX + // SoC supports SDMMC pin configuration via GPIO matrix. Save the pins for later use in SDFS::begin. + _pin_clk = (int8_t)clk; + _pin_cmd = (int8_t)cmd; + _pin_d0 = (int8_t)d0; + _pin_d1 = (int8_t)d1; + _pin_d2 = (int8_t)d2; + _pin_d3 = (int8_t)d3; + return true; +#elif CONFIG_IDF_TARGET_ESP32 + // ESP32 doesn't support SDMMC pin configuration via GPIO matrix. + // Since SDFS::begin hardcodes the usage of slot 1, only check if + // the pins match slot 1 pins. + bool pins_ok = (clk == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_CLK) && + (cmd == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_CMD) && (d0 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D0) && + (((d1 == -1) && (d2 == -1) && (d3 == -1)) || + ((d1 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D1) && (d2 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D2) && + (d3 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D3))); + if (!pins_ok) { + log_e("SDFS: specified pins are not supported by this chip."); + return false; + } + return true; +#else +#error SoC not supported +#endif +} + +bool SDFS::begin( + const char *mountpoint, bool mode1bit, bool format_if_mount_failed, int sdmmc_frequency, + uint8_t maxOpenFiles +) { + if (_card) { return true; } + // mount + sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); +#ifdef SOC_SDMMC_USE_GPIO_MATRIX + // SoC supports SDMMC pin configuration via GPIO matrix. + // Check that the pins have been set either in the constructor or setPins function. + if (_pin_cmd == -1 || _pin_clk == -1 || _pin_d0 == -1 || + (!mode1bit && (_pin_d1 == -1 || _pin_d2 == -1 || _pin_d3 == -1))) { + log_e("SDFS: some SD pins are not set"); + return false; + } + + slot_config.clk = (gpio_num_t)_pin_clk; + slot_config.cmd = (gpio_num_t)_pin_cmd; + slot_config.d0 = (gpio_num_t)_pin_d0; + slot_config.d1 = (gpio_num_t)_pin_d1; + slot_config.d2 = (gpio_num_t)_pin_d2; + slot_config.d3 = (gpio_num_t)_pin_d3; + slot_config.width = 4; +#endif // SOC_SDMMC_USE_GPIO_MATRIX + sdmmc_host_t host = SDMMC_HOST_DEFAULT(); + host.flags = SDMMC_HOST_FLAG_4BIT; + host.slot = SDMMC_HOST_SLOT_1; + host.max_freq_khz = sdmmc_frequency; +#ifdef BOARD_HAS_1BIT_SDMMC + mode1bit = true; +#endif + if (mode1bit) { + host.flags = SDMMC_HOST_FLAG_1BIT; // use 1-line SD mode + slot_config.width = 1; + } + + esp_vfs_fat_sdmmc_mount_config_t mount_config = { + .format_if_mount_failed = format_if_mount_failed, .max_files = maxOpenFiles, .allocation_unit_size = 0 + }; + + esp_err_t ret = esp_vfs_fat_sdmmc_mount(mountpoint, &host, &slot_config, &mount_config, &_card); + if (ret != ESP_OK) { + if (ret == ESP_FAIL) { + log_e( + "Failed to mount filesystem. If you want the card to be formatted, set " + "format_if_mount_failed = true." + ); + } else if (ret == ESP_ERR_INVALID_STATE) { + _impl->mountpoint(mountpoint); + log_w("SD Already mounted"); + return true; + } else { + log_e( + "Failed to initialize the card (0x%x). Make sure SD card lines have pull-up resistors in " + "place.", + ret + ); + } + _card = NULL; + return false; + } + _impl->mountpoint(mountpoint); + return true; +} + +void SDFS::end() { + if (_card) { + esp_vfs_fat_sdmmc_unmount(); + _impl->mountpoint(NULL); + _card = NULL; + } +} + +sdcard_type_t SDFS::cardType() { + if (!_card) { return CARD_NONE; } + return (_card->ocr & SD_OCR_SDHC_CAP) ? CARD_SDHC : CARD_SD; +} + +uint64_t SDFS::cardSize() { + if (!_card) { return 0; } + return (uint64_t)_card->csd.capacity * _card->csd.sector_size; +} + +uint64_t SDFS::totalBytes() { + FATFS *fsinfo; + DWORD fre_clust; + if (f_getfree("0:", &fre_clust, &fsinfo) != 0) return 0; + uint64_t size = ((uint64_t)(fsinfo->csize)) * (fsinfo->n_fatent - 2) +#if _MAX_SS != 512 + * (fsinfo->ssize); +#else + * 512; +#endif + return size; +} + +uint64_t SDFS::usedBytes() { + FATFS *fsinfo; + DWORD fre_clust; + if (f_getfree("0:", &fre_clust, &fsinfo) != 0) return 0; + uint64_t size = ((uint64_t)(fsinfo->csize)) * ((fsinfo->n_fatent - 2) - (fsinfo->free_clst)) +#if _MAX_SS != 512 + * (fsinfo->ssize); +#else + * 512; +#endif + return size; +} + +// these next 4 functions need to be implemented, doesn't exist in Arduino-esp32 2.0.17 +size_t SDFS::numSectors() { return 0; } + +size_t SDFS::sectorSize() { return 0; } + +bool SDFS::readRAW(uint8_t *buffer, uint32_t sector) { return false; } + +bool SDFS::writeRAW(uint8_t *buffer, uint32_t sector) { return false; } + +SDFS SD = SDFS(FSImplPtr(new VFSImpl())); +#endif /* SOC_SDMMC_HOST_SUPPORTED */ +#endif diff --git a/lib/HAL/sd_defines.h b/lib/HAL/sd_defines.h new file mode 100644 index 000000000..7bedac932 --- /dev/null +++ b/lib/HAL/sd_defines.h @@ -0,0 +1,19 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef _SD_DEFINES_H_ +#define _SD_DEFINES_H_ + +typedef enum { CARD_NONE, CARD_MMC, CARD_SD, CARD_SDHC, CARD_UNKNOWN } sdcard_type_t; + +#endif /* _SD_DISKIO_H_ */ diff --git a/src/core/connect/file_sharing.cpp b/src/core/connect/file_sharing.cpp index 44d0aed9e..25b0e374d 100644 --- a/src/core/connect/file_sharing.cpp +++ b/src/core/connect/file_sharing.cpp @@ -1,6 +1,6 @@ #include "file_sharing.h" #include "core/display.h" - +#include FileSharing::FileSharing() {} void FileSharing::sendFile() { diff --git a/src/core/massStorage.cpp b/src/core/massStorage.cpp index 9c0f9c453..d68b9bd7e 100644 --- a/src/core/massStorage.cpp +++ b/src/core/massStorage.cpp @@ -1,4 +1,4 @@ -#ifdef ARDUINO_USB_MODE +#if defined(ARDUINO_USB_MODE) && !defined(USE_SD_MMC) #include "massStorage.h" #include "core/display.h" diff --git a/src/core/menu_items/FileMenu.cpp b/src/core/menu_items/FileMenu.cpp index 8d93d26c1..ba0d97541 100644 --- a/src/core/menu_items/FileMenu.cpp +++ b/src/core/menu_items/FileMenu.cpp @@ -10,7 +10,7 @@ void FileMenu::optionsMenu() { options.push_back({"SD Card", [=]() { loopSD(SD); }}); options.push_back({"LittleFS", [=]() { loopSD(LittleFS); }}); options.push_back({"WebUI", loopOptionsWebUi}); -#ifdef ARDUINO_USB_MODE +#if defined(ARDUINO_USB_MODE) && !defined(USE_SD_MMC) options.push_back({"Mass Storage", [=]() { MassStorage(); }}); #endif addOptionToMainMenu(); diff --git a/src/core/sd_functions.cpp b/src/core/sd_functions.cpp index a5d9a1467..f0e051cb9 100644 --- a/src/core/sd_functions.cpp +++ b/src/core/sd_functions.cpp @@ -26,10 +26,12 @@ std::vector fileList; ** Description: Start SD Card ***************************************************************************************/ bool setupSdCard() { - if (SDCARD_SCK == -1) { +#ifndef USE_SD_MMC + if (bruceConfig.SDCARD_bus.sck < 0) { sdcardMounted = false; return false; } +#endif // avoid unnecessary remounting if (sdcardMounted) return true; #ifdef USE_TFT_eSPI_TOUCH @@ -39,12 +41,18 @@ bool setupSdCard() { #endif bool result = true; +#ifdef USE_SD_MMC + if (!SD.begin("/sdcard", true)) { + sdcardMounted = false; + result = false; + } +#else if (task) { // Not using InputHandler (SdCard on default &SPI bus) if (!SD.begin(SDCARD_CS)) result = false; } else if (bruceConfig.SDCARD_bus.mosi == (gpio_num_t)TFT_MOSI && bruceConfig.SDCARD_bus.mosi != GPIO_NUM_NC) { // SDCard in the same Bus as TFT, in this case we call the SPI TFT Instance -#if TFT_MOSI > 0 // condition for Headless and 8bit displays (no SPI bus) +#if TFT_MOSI > 0 // condition for Headless and 8bit displays (no SPI bus) if (!SD.begin(SDCARD_CS, tft.getSPIinstance())) result = false; #else goto NEXT; // destination for Headless and 8bit displays (no SPI bus) @@ -57,6 +65,7 @@ bool setupSdCard() { delay(10); if (!SD.begin(SDCARD_CS, sdcardSPI)) result = false; } +#endif if (result == false) { #if defined(ARDUINO_M5STICK_C_PLUS) || defined(ARDUINO_M5STICK_C_PLUS2) diff --git a/src/core/settings.cpp b/src/core/settings.cpp index ab51a0219..f35edeb0a 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -354,9 +354,9 @@ void setRFModuleMenu() { CC_NRF_SPI.begin( bruceConfig.CC1101_bus.sck, bruceConfig.CC1101_bus.miso, bruceConfig.CC1101_bus.mosi ); - initCC1101once(&CC_NRF_SPI - ); // (ARDUINO_M5STICK_C_PLUS) || (ARDUINO_M5STICK_C_PLUS2) and others that doesn´t share SPI with - // other devices (need to change it when Bruce board comes to shore) + initCC1101once(&CC_NRF_SPI); + // (ARDUINO_M5STICK_C_PLUS) || (ARDUINO_M5STICK_C_PLUS2) and others that doesn´t share SPI with + // other devices (need to change it when Bruce board comes to shore) ELECHOUSE_cc1101.setBeginEndLogic(true); } diff --git a/src/core/startup_app.cpp b/src/core/startup_app.cpp index 460496877..65238bfa5 100644 --- a/src/core/startup_app.cpp +++ b/src/core/startup_app.cpp @@ -26,7 +26,7 @@ StartupApp::StartupApp() { _startupApps["Clock"] = []() { runClockLoop(); }; _startupApps["Custom SubGHz"] = []() { sendCustomRF(); }; _startupApps["GPS Tracker"] = []() { GPSTracker(); }; -#ifdef ARDUINO_USB_MODE +#if defined(ARDUINO_USB_MODE) && !defined(USE_SD_MMC) _startupApps["Mass Storage"] = []() { MassStorage(); }; #endif _startupApps["PN532 BLE"] = []() { Pn532ble(); }; diff --git a/src/core/wifi/webInterface.cpp b/src/core/wifi/webInterface.cpp index fafde36a3..524249374 100644 --- a/src/core/wifi/webInterface.cpp +++ b/src/core/wifi/webInterface.cpp @@ -521,7 +521,7 @@ void configureWebServer() { fs::FS *fs = useSD ? (fs::FS *)&SD : (fs::FS *)&LittleFS; String fsType = useSD ? "SD" : "LittleFS"; - if ((useSD && !SD.begin()) || (!useSD && !LittleFS.begin())) { + if ((useSD && !setupSdCard()) || (!useSD && !LittleFS.begin())) { request->send(500, "text/plain", "Failed to initialize file system: " + fsType); return; } diff --git a/src/modules/NRF24/nrf_jammer.cpp b/src/modules/NRF24/nrf_jammer.cpp index c9627b985..023570eea 100644 --- a/src/modules/NRF24/nrf_jammer.cpp +++ b/src/modules/NRF24/nrf_jammer.cpp @@ -10,18 +10,33 @@ ************************************************************************************** */ void nrf_jammer() { RF24 radio(NRF24_CE_PIN, NRF24_SS_PIN); - byte wifi_channels[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // WiFi Channels - byte ble_channels[] = {2, 26, 80}; // BLE Channels - byte bluetooth_channels[] = {32, 34, 46, 48, 50, 52, 0, 1, 2, 4, 6, - 8, 22, 24, 26, 28, 30, 74, 76, 78, 80}; // Bluetooth Channels - byte usb_channels[] = {40, 50, 60}; // USB Wireless Channels - byte video_channels[] = {70, 75, 80}; // Video Streaming Channels - byte rc_channels[] = {1, 3, 5, 7}; // RC Toys/ Drones Channels - byte full_channels[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, - 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, - 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, - 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, - 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100}; // All Channels + // https://www.allaboutcircuits.com/uploads/articles/Bluetooth_and_WLAN_frequencies.jpg + byte wifi_channels[] = { + 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, // Channel 1 + 22, 24, 26, 28, // mid band + 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, // Channel 6 + 46, 48, 50, 52, // mid band + 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, // Channel 11 + }; // WiFi Channels + byte ble_channels[] = {1, 2, 3, 25, 26, 27, 79, 80, 81}; // BLE Advertising Channels: 2, 26 and 80 + byte bluetooth_channels[] = { + // 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, // odds + // 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, // odds + 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, // even + 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80 // even + }; // Bluetooth Channels + byte usb_channels[] = {40, 50, 60}; // USB Wireless Channels + byte video_channels[] = {70, 75, 80}; // Video Streaming Channels + byte rc_channels[] = {1, 3, 5, 7}; // RC Toys/ Drones Channels + byte full_channels[] = { + 1, 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, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, + 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, + 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, + 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100 + //, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, + // 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124 + }; // All Channels if (nrf_start()) { Serial.println("NRF24 turned On"); @@ -55,11 +70,11 @@ void nrf_jammer() { NRF_MODE = "WiFi "; Serial.println("WiFi"); } else if (NRF_MODE_N == 2) { - NRF_MODE = "BLE "; - Serial.println("BLE"); + NRF_MODE = "BLE Adv ch. "; + Serial.println("BLE Advertising"); } else if (NRF_MODE_N == 3) { NRF_MODE = "Bluetooth "; - Serial.println("Bluetooth"); + Serial.println("Bluetooth "); } else if (NRF_MODE_N == 4) { NRF_MODE = "USB "; Serial.println("USB"); @@ -142,6 +157,7 @@ void nrf_channel_jammer() { Serial.println("NRF24 turned On"); int NRF_MODE_N = 1; + int NRF_FREQ = 400 + NRF_MODE_N; int NRF_MODE_N_X = 0; String NRF_MODE = "CH 1 "; @@ -167,6 +183,8 @@ void nrf_channel_jammer() { tft.println("STATUS : ACTIVE"); tft.setCursor(10, 100); tft.print("MODE : " + NRF_MODE + " "); + tft.setCursor(10, 116); + tft.printf("Freq : 2.%03dGhz", NRF_FREQ); Serial.println("CH " + String(NRF_MODE_N)); tft.drawRoundRect(5, 5, tftWidth - 10, tftHeight - 10, 5, bruceConfig.priColor); redraw = false;