diff --git a/code/include/defines.h b/code/include/defines.h index e41178957..dc47df0f6 100644 --- a/code/include/defines.h +++ b/code/include/defines.h @@ -267,6 +267,42 @@ #define USE_PWM_LEDFLASH // if __LEDGLOBAL is defined, a global variable is used for LED control, otherwise locally and each time a new +#elif defined(BOARD_ESP32CAM_S014TF) // ESP32Cam S014TF PIN Map + #define __SD_USE_SPI_MODE__ // only SPI mode is supported on this board + + #define GPIO_SDCARD_CLK GPIO_NUM_4 + #define GPIO_SDCARD_CMD GPIO_NUM_21 + #define GPIO_SDCARD_D0 GPIO_NUM_13 + #define GPIO_SDCARD_D1 GPIO_NUM_NC + #define GPIO_SDCARD_D2 GPIO_NUM_NC + #define GPIO_SDCARD_D3 GPIO_NUM_19 + + #define CAM_PIN_PWDN GPIO_NUM_NC + #define CAM_PIN_RESET GPIO_NUM_5 + #define CAM_PIN_XCLK GPIO_NUM_15 + #define CAM_PIN_SIOD GPIO_NUM_22 + #define CAM_PIN_SIOC GPIO_NUM_23 + + #define CAM_PIN_D7 GPIO_NUM_39 + #define CAM_PIN_D6 GPIO_NUM_34 + #define CAM_PIN_D5 GPIO_NUM_33 + #define CAM_PIN_D4 GPIO_NUM_27 + #define CAM_PIN_D3 GPIO_NUM_12 + #define CAM_PIN_D2 GPIO_NUM_35 + #define CAM_PIN_D1 GPIO_NUM_14 + #define CAM_PIN_D0 GPIO_NUM_2 + #define CAM_PIN_VSYNC GPIO_NUM_18 + #define CAM_PIN_HREF GPIO_NUM_36 + #define CAM_PIN_PCLK GPIO_NUM_26 + + //Statusled + ClassControllCamera + #define BLINK_GPIO GPIO_NUM_25 // PIN for red board LED + + //ClassControllCamera + // NOTE: GPIOs 38 and 37 are inputs only + #define FLASH_GPIO GPIO_NUM_32 // PIN for flashlight LED + #define USE_PWM_LEDFLASH // if __LEDGLOBAL is defined, a global variable is used for LED control, otherwise locally and each time a new + #elif defined(BOARD_ESP32CAM_AITHINKER) // ESP32Cam (AiThinker) PIN Map // SD card (operated with SDMMC peripheral) //------------------------------------------------- diff --git a/code/main/main.cpp b/code/main/main.cpp index 0c494ef92..7b8ae62c3 100644 --- a/code/main/main.cpp +++ b/code/main/main.cpp @@ -105,16 +105,23 @@ bool Init_NVS_SDCard() ret = nvs_flash_init(); } +#ifdef __SD_USE_SPI_MODE__ + ESP_LOGD(TAG, "Using SDSPI peripheral"); + // By default, SD card frequency is initialized to SDMMC_FREQ_DEFAULT (20MHz) + // For setting a specific frequency, use host.max_freq_khz (range 400kHz - 20MHz for SDSPI) + // Example: for fixed frequency of 10MHz, use host.max_freq_khz = 10000; + sdmmc_host_t host = SDSPI_HOST_DEFAULT(); +#else ESP_LOGD(TAG, "Using SDMMC peripheral"); sdmmc_host_t host = SDMMC_HOST_DEFAULT(); - host.max_freq_khz = SDMMC_FREQ_HIGHSPEED; +#endif // __SD_USE_SPI_MODE__ // For SoCs where the SD power can be supplied both via an internal or external (e.g. on-board LDO) power supply. // When using specific IO pins (which can be used for ultra high-speed SDMMC) to connect to the SD card // and the internal LDO power supply, we need to initialize the power supply first. #if SD_PWR_CTRL_LDO_INTERNAL_IO sd_pwr_ctrl_ldo_config_t ldo_config = { - .ldo_chan_id = CONFIG_EXAMPLE_SD_PWR_CTRL_LDO_IO_ID, + .ldo_chan_id = SD_PWR_CTRL_LDO_IO_ID, }; sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL; @@ -127,6 +134,32 @@ bool Init_NVS_SDCard() host.pwr_ctrl_handle = pwr_ctrl_handle; #endif +#ifdef __SD_USE_SPI_MODE__ + spi_bus_config_t bus_cfg = + { + .mosi_io_num = GPIO_SDCARD_CMD, + .miso_io_num = GPIO_SDCARD_D0, + .sclk_io_num = GPIO_SDCARD_CLK, + .quadwp_io_num = -1, + .quadhd_io_num = -1, + .max_transfer_sz = 4000, + }; + + ret = spi_bus_initialize((spi_host_device_t)host.slot, &bus_cfg, SDSPI_DEFAULT_DMA); + if (ret != ESP_OK) + { + ESP_LOGE(TAG, "Failed to initialize SPI bus."); + return false; + } + + // This initializes the slot without card detect (CD) and write protect (WP) signals. + // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals. + sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT(); + slot_config.gpio_cs = GPIO_SDCARD_D3; + slot_config.host_id = (spi_host_device_t)host.slot; + +#else + // This initializes the slot without card detect (CD) and write protect (WP) signals. // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals. #ifdef CONFIG_SOC_SDMMC_USE_GPIO_MATRIX @@ -138,14 +171,14 @@ bool Init_NVS_SDCard() sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); #endif - // Set bus width to use: -#ifdef __SD_USE_ONE_LINE_MODE__ - slot_config.width = 1; #ifdef CONFIG_SOC_SDMMC_USE_GPIO_MATRIX slot_config.clk = GPIO_SDCARD_CLK; slot_config.cmd = GPIO_SDCARD_CMD; slot_config.d0 = GPIO_SDCARD_D0; #endif // end CONFIG_SOC_SDMMC_USE_GPIO_MATRIX + // Set bus width to use: +#ifdef __SD_USE_ONE_LINE_MODE__ + slot_config.width = 1; #else // else __SD_USE_ONE_LINE_MODE__ slot_config.width = 4; #ifdef CONFIG_SOC_SDMMC_USE_GPIO_MATRIX @@ -167,6 +200,8 @@ bool Init_NVS_SDCard() // Um diese Probleme zu kompensieren, wird der PullUp manuel gesetzt. gpio_set_pull_mode(GPIO_SDCARD_D3, GPIO_PULLUP_ONLY); // HS2_D3 +#endif // __SD_USE_SPI_MODE__ + // Options for mounting the filesystem. // If format_if_mount_failed is set to true, SD card will be partitioned and // formatted in case when mounting fails. @@ -175,11 +210,15 @@ bool Init_NVS_SDCard() .max_files = 12, // previously -> 2022-09-21: 5, 2023-01-02: 7 .allocation_unit_size = 0, // 0 = auto .disk_status_check_enable = 0, + .use_one_fat = false, // Whether to use only one FAT table (reduce memory usage), but decrease reliability of file system in case of power failure. }; sdmmc_card_t* card; const char mount_point[] = MOUNT_POINT; +#ifdef __SD_USE_SPI_MODE__ + ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card); +#else // Use settings defined above to initialize SD card and mount FAT filesystem. // Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function. // Please check its source code and implement error recovery when developing @@ -189,6 +228,7 @@ bool Init_NVS_SDCard() #else ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card); #endif +#endif // __SD_USE_SPI_MODE__ if (ret != ESP_OK) { if (ret == ESP_FAIL) { diff --git a/code/platformio.ini b/code/platformio.ini index d622f420a..76d9e3de6 100644 --- a/code/platformio.ini +++ b/code/platformio.ini @@ -72,6 +72,30 @@ build_flags = board_build.partitions = partitions.csv monitor_speed = 115200 +; M5stack ESP32cam env +[env:esp32cam-s014tf] +extends = common:esp32-idf +board = esp32cam +framework = espidf +build_flags = + ; ### common imported : + ${common:esp32-idf.build_flags} + ${flags:runtime.build_flags} + ; ### Sofware options : (can be set in defines.h) + -D BOARD_ESP32CAM_S014TF + -D ENABLE_MQTT + ;-D MQTT_PROTOCOL_311 + -D MQTT_ENABLE_SSL + ;-D MQTT_ENABLE_WS + ;-D MQTT_ENABLE_WSS + -D MQTT_SUPPORTED_FEATURE_SKIP_CRT_CMN_NAME_CHECK + ;-D MQTT_SUPPORTED_FEATURE_CRT_CMN_NAME + ;-D MQTT_SUPPORTED_FEATURE_CLIENT_KEY_PASSWORD + -D ENABLE_INFLUXDB + -D ENABLE_WEBHOOK + -D ENABLE_SOFTAP +board_build.partitions = partitions.csv +monitor_speed = 115200 ; full standalone dev mode ; As sample, the board is nod32s instead of esp32cam (do not change nothing in fact :)