Quellcode durchsuchen

test commit for aec

Kathy Zhu vor 1 Woche
Ursprung
Commit
2ac2d3af7b

+ 0 - 1
2_way_tochie/2_way_Tochie

@@ -1 +0,0 @@
-Subproject commit f5a2e76b0be3d3db3c7c13f1d74d48f37a5929ff

+ 77 - 14
2_way_tochie/dependencies.lock

@@ -6,7 +6,7 @@ dependencies:
       require: private
       version: '>=5.0'
     source:
-      registry_url: https://components.espressif.com/
+      registry_url: https://components.espressif.com
       type: service
     version: 1.7.19~2
   espressif/dl_fft:
@@ -16,9 +16,37 @@ dependencies:
       require: private
       version: '>=5.0'
     source:
-      registry_url: https://components.espressif.com/
+      registry_url: https://components.espressif.com
       type: service
     version: 0.6.0
+  espressif/esp-dl:
+    component_hash: 0d5dd7fadd04854a82e1404f871e16980002a46dfe9796e52626d3c8d6566ad2
+    dependencies:
+    - name: espressif/dl_fft
+      registry_url: https://components.espressif.com
+      require: private
+      version: '>=0.6.0'
+    - name: espressif/esp_new_jpeg
+      registry_url: https://components.espressif.com
+      require: private
+      version: ^1
+    - name: idf
+      require: private
+      version: '*'
+    source:
+      registry_url: https://components.espressif.com
+      type: service
+    targets:
+    - esp32
+    - esp32c2
+    - esp32c3
+    - esp32c5
+    - esp32c6
+    - esp32p4
+    - esp32s2
+    - esp32s3
+    - esp32s31
+    version: 3.3.9
   espressif/esp-dsp:
     component_hash: 12e44db246517a627bc0fe2b255b8335410c0215c98bfba2df5a8e3edea839ef
     dependencies:
@@ -26,17 +54,37 @@ dependencies:
       require: private
       version: '>=4.2'
     source:
-      registry_url: https://components.espressif.com/
+      registry_url: https://components.espressif.com
       type: service
     version: 1.8.0
   espressif/esp-sr:
-    dependencies: []
+    component_hash: 803a3e2917f5100f6ce5766373d29c269ce32b9c6b33ca948548bc94ad752ef1
+    dependencies:
+    - name: espressif/cjson
+      registry_url: https://components.espressif.com
+      require: private
+      version: ^1.7.19
+    - name: espressif/dl_fft
+      registry_url: https://components.espressif.com
+      require: private
+      version: '>=0.6.0'
+    - name: espressif/esp-dl
+      registry_url: https://components.espressif.com
+      require: private
+      version: '>=3.3.9'
+    - name: espressif/esp-dsp
+      registry_url: https://components.espressif.com
+      require: private
+      version: 1.8.0
+    - name: idf
+      require: private
+      version: '>=5.0'
     source:
-      path: C:\Users\kathy\Downloads\Retry_Test\2_way_tochie\components\espressif__esp-sr
-      type: local
-    version: 2.4.7
+      registry_url: https://components.espressif.com/
+      type: service
+    version: 2.5.0
   espressif/esp_codec_dev:
-    component_hash: 8a58569aed3c6e8d858da5419eba4d54ee905d7709cdbe01c42101c1ed0c5319
+    component_hash: 4779f31a3ba3f9b38aee1afe6ccb9ba8f6108dc3a6e77d39d7d714e9a215371b
     dependencies:
     - name: idf
       require: private
@@ -44,7 +92,7 @@ dependencies:
     source:
       registry_url: https://components.espressif.com/
       type: service
-    version: 1.6.1
+    version: 1.6.2
   espressif/esp_io_expander:
     component_hash: 7b7f91270172e46ca4597e3874f48d8412e280e8c295559f5922ec38656e3e5f
     dependencies:
@@ -69,6 +117,25 @@ dependencies:
       registry_url: https://components.espressif.com/
       type: service
     version: 2.0.2
+  espressif/esp_new_jpeg:
+    component_hash: e1e7e6991193c41ed6c9fc386702aa7821433319e7aa9c3147cd3236310cd69c
+    dependencies: []
+    source:
+      registry_url: https://components.espressif.com
+      type: service
+    targets:
+    - esp32
+    - esp32s2
+    - esp32s3
+    - esp32s31
+    - esp32p4
+    - esp32c2
+    - esp32c3
+    - esp32c5
+    - esp32c6
+    - esp32c61
+    - esp32h4
+    version: 1.0.2
   espressif/led_strip:
     component_hash: 28621486f77229aaf81c71f5e15d6fbf36c2949cf11094e07090593e659e7639
     dependencies:
@@ -84,14 +151,10 @@ dependencies:
       type: idf
     version: 5.3.5
 direct_dependencies:
-- espressif/cjson
-- espressif/dl_fft
-- espressif/esp-dsp
 - espressif/esp-sr
 - espressif/esp_codec_dev
 - espressif/esp_io_expander_tca95xx_16bit
 - espressif/led_strip
-- idf
-manifest_hash: f4940e795292bc577123b38efcacbc725acac754b1567eb35278512dbc4eedf4
+manifest_hash: cc6725c3be0ea7fd83b3ab5f9e00bbc374e48bbe22994003311ddb44aa92bcbe
 target: esp32s3
 version: 2.0.0

+ 257 - 6
2_way_tochie/main/hardeware_driver/bsp_board.c

@@ -13,6 +13,8 @@
 #include "esp_log.h"
 #include "esp_rom_sys.h"
 #include "esp_check.h"
+#include "esp_vfs_fat.h"
+#include "sdmmc_cmd.h"
 #include "driver/i2s_std.h"
 #include "driver/i2s_types.h"
 #include "driver/i2s_pdm.h"
@@ -20,14 +22,22 @@
 
 #include <sys/unistd.h>
 #include <sys/stat.h>
+#include "dirent.h"
+#include "driver/sdmmc_host.h"
+#include <errno.h>
 
 #include "esp_aec.h"
 #include "tca9555_driver.h"
 
+#if ((SOC_SDMMC_HOST_SUPPORTED) && (FUNC_SDMMC_EN))
+#include "driver/sdmmc_host.h"
+#endif /* ((SOC_SDMMC_HOST_SUPPORTED) && (FUNC_SDMMC_EN)) */
+
 
 #define ADC_I2S_CHANNEL 2
 #define ADC_BITS_PER_SAMPLE 32
 
+static sdmmc_card_t *card;
 static const char *TAG = "board";
 static int s_play_sample_rate = 16000;
 static int s_play_channel_format = 1;
@@ -48,6 +58,7 @@ static audio_codec_gpio_if_t *play_gpio_if    = NULL;
 static audio_codec_if_t *play_codec_if        = NULL;
 static esp_codec_dev_handle_t play_dev        = NULL;
 static i2c_master_bus_handle_t i2c_bus_handle = NULL;
+static uint32_t SDCard_Size  = 0;
 static bool s_volume_buttons_task_started = false;
 
 static aec_handle_t *aec = NULL;
@@ -58,7 +69,9 @@ static int16_t *out = NULL;
 
 static esp_err_t bsp_aec_init(void)
 {
-    /* aec_create_from_config() is used instead of aec_create() so the NLP
+    /* Espressif's direct AEC flow: create, query the algorithm frame size,
+     * then allocate aligned signed-16-bit audio buffers for each frame.
+     * aec_create_from_config() is used instead of aec_create() so the NLP
      * (residual echo suppression) level can be pushed to its strongest
      * setting; a longer filter_length also helps cancel longer echo tails. */
     aec_config_t aec_cfg = {
@@ -164,7 +177,7 @@ static esp_err_t i2c_master_init(void)
     }
 
     ESP_LOGI(TAG, "I2C bus initialized successfully");
-    return ESP_OK; 
+    return ESP_OK;  // 返回 ESP_OK 表示成功
 }
 
 
@@ -200,7 +213,20 @@ esp_err_t bsp_codec_adc_init(int sample_rate)
 
     es7210_codec_cfg_t es7210_cfg = {
         .ctrl_if = record_ctrl_if,
-
+        /*
+         * Microphone initialization for the ES7210 ADC codec.
+         * This board supports multiple mic inputs, and the current code enables all
+         * 4 available mic channels:
+         *   MIC1 + MIC2 + MIC3 + MIC4
+         *
+         * For a 2-mic board, the intended setup is typically to enable only the two
+         * physical microphones that are wired to the board, for example:
+         *   .mic_selected = ES7210_SEL_MIC1 | ES7210_SEL_MIC2;
+         *
+         * In other words, the current configuration is not "one mic only"; it is
+         * configured to allow all entries in the codec, even though the app logic only
+         * reads one channel from the resulting stream.
+         */
         .mic_selected =
             ES7210_SEL_MIC1 |
             ES7210_SEL_MIC2 |
@@ -233,7 +259,15 @@ esp_err_t bsp_codec_adc_init(int sample_rate)
 
     esp_codec_dev_sample_info_t fs = {
         .sample_rate = sample_rate,
-
+        /*
+         * Stereo is opened here, which matches a 2-channel mic stream.
+         * If the board is wired as a 2-mic stereo pair, the code can use both
+         * channels by reading left and right samples from the interleaved buffer.
+         * Example:
+         *   // .mic_selected = ES7210_SEL_MIC1 | ES7210_SEL_MIC2;
+         *   // left  = mic_buffer[i * 2 + 0];
+         *   // right = mic_buffer[i * 2 + 1];
+         */
         .channel = 2,
         .bits_per_sample = 16,
     };
@@ -269,6 +303,7 @@ esp_err_t bsp_codec_dac_init(int sample_rate, int channel_format, int bits_per_c
 {
     esp_err_t ret_val = ESP_OK;
 
+    // Do initialize of related interface: data_if, ctrl_if and gpio_if
     audio_codec_i2s_cfg_t i2s_cfg = {
         .port = I2S_NUM_1,
 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
@@ -396,6 +431,7 @@ esp_err_t esp_audio_get_play_vol(int *volume)
     return ESP_OK;
 }
 
+// static esp_err_t bsp_i2s_init(i2s_port_t i2s_num, uint32_t sample_rate, i2s_channel_fmt_t channel_format, i2s_bits_per_chan_t bits_per_chan)
 static esp_err_t bsp_i2s_init(int i2s_num, uint32_t sample_rate, int channel_format, int bits_per_chan)
 {
     esp_err_t ret_val = ESP_OK;
@@ -486,6 +522,7 @@ static esp_err_t bsp_codec_init(
         int dac_channel_format,
         int dac_bits_per_chan)
 {
+    //ESP_LOGE(TAG, "bsp_codec_adc_init START");
     esp_err_t ret;
 
     ret = bsp_codec_adc_init(adc_sample_rate);
@@ -597,12 +634,21 @@ esp_err_t esp_get_feed_data(
         int buffer_len)
 {
 
+    // ESP_LOGE(TAG, "NEW esp_get_feed_data CALLED");
+
     if(record_dev == NULL)
     {
         ESP_LOGE(TAG,"record_dev NULL");
         return ESP_ERR_INVALID_STATE;
     }
 
+
+    // ESP_LOGI(TAG,
+    //          "read buffer=%p size=%d",
+    //          buffer,
+    //          buffer_len);
+
+
     esp_err_t ret;
 
     ret = esp_codec_dev_read(
@@ -610,6 +656,12 @@ esp_err_t esp_get_feed_data(
             buffer,
             buffer_len);
 
+
+    // ESP_LOGI(TAG,
+    //          "codec read ret=%s",
+    //          esp_err_to_name(ret));
+
+
     if(ret != ESP_OK)
     {
         return ret;
@@ -646,7 +698,8 @@ char* esp_get_input_format(void)
 
 static void bsp_volume_buttons_task(void *arg)
 {
-
+    /* K1/K3 are wired through the TCA9555 IO expander, not raw ESP32 GPIOs
+     * (GPIO0 is the BOOT/RESET strapping pin, so it must not be used here). */
     int last_key1 = 1;
     int last_key3 = 1;
 
@@ -702,6 +755,8 @@ static esp_err_t bsp_volume_buttons_init(void)
         return ESP_OK;
     }
 
+    /* K1/K3 input direction is already configured on the TCA9555 expander by
+     * tca9555_driver_init(); no direct GPIO configuration is needed here. */
     BaseType_t task_created = xTaskCreate(bsp_volume_buttons_task, "vol_btns", 4096, NULL, 5, NULL);
     if (task_created != pdPASS) {
         return ESP_FAIL;
@@ -746,7 +801,16 @@ else
 {
     ESP_LOGI(TAG,"bsp_codec_init OK");
 }
-
+    /* Initialize PA */
+     /*gpio_config_t  io_conf;
+     memset(&io_conf, 0, sizeof(io_conf));
+     io_conf.intr_type = GPIO_INTR_DISABLE;
+     io_conf.mode = GPIO_MODE_OUTPUT;
+     io_conf.pin_bit_mask = ((1ULL << GPIO_PWR_CTRL));
+     io_conf.pull_down_en = 0;
+     io_conf.pull_up_en = 0;
+     gpio_config(&io_conf);
+     */
     gpio_set_level(GPIO_PWR_CTRL, 1);
 
     esp_err_t button_ret = bsp_volume_buttons_init();
@@ -757,3 +821,190 @@ else
     return ESP_OK;
 }
 
+esp_err_t esp_sdcard_init(char *mount_point, size_t max_files)
+{
+    if (NULL != card) {
+        return ESP_ERR_INVALID_STATE;
+    }
+
+    /* Check if SD crad is supported */
+    if (!FUNC_SDMMC_EN && !FUNC_SDSPI_EN) {
+        ESP_LOGE(TAG, "SDMMC and SDSPI not supported on this board!");
+        return ESP_ERR_NOT_SUPPORTED;
+    }
+
+    esp_err_t ret_val = ESP_OK;
+
+    /**
+     * @brief 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.
+     *
+     */
+    esp_vfs_fat_sdmmc_mount_config_t mount_config = {
+        .format_if_mount_failed = false,
+        .max_files = max_files,
+        .allocation_unit_size = 16 * 1024
+    };
+
+    /**
+     * @brief Use settings defined above to initialize SD card and mount FAT filesystem.
+     *   Note: esp_vfs_fat_sdmmc/sdspi_mount is all-in-one convenience functions.
+     *   Please check its source code and implement error recovery when developing
+     *   production applications.
+     *
+     */
+    sdmmc_host_t host =
+#if FUNC_SDMMC_EN
+        SDMMC_HOST_DEFAULT();
+#else
+        SDSPI_HOST_DEFAULT();
+    spi_bus_config_t bus_cfg = {
+        .mosi_io_num = GPIO_SDSPI_MOSI,
+        .miso_io_num = GPIO_SDSPI_MISO,
+        .sclk_io_num = GPIO_SDSPI_SCLK,
+        .quadwp_io_num = GPIO_NUM_NC,
+        .quadhd_io_num = GPIO_NUM_NC,
+        .max_transfer_sz = 4000,
+    };
+    ret_val = spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CH_AUTO);
+    if (ret_val != ESP_OK) {
+        ESP_LOGE(TAG, "Failed to initialize bus.");
+        return ret_val;
+    }
+#endif
+
+    /**
+     * @brief 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.
+     *
+     */
+#if FUNC_SDMMC_EN
+    sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
+#else
+    sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
+#endif
+
+#if FUNC_SDMMC_EN
+    /* Config SD data width. 0, 4 or 8. Currently for SD card, 8 bit is not supported. */
+    slot_config.width = SDMMC_BUS_WIDTH;
+
+    /**
+     * @brief On chips where the GPIOs used for SD card can be configured, set them in
+     *   the slot_config structure.
+     *
+     */
+#if SOC_SDMMC_USE_GPIO_MATRIX
+    slot_config.clk = GPIO_SDMMC_CLK;
+    slot_config.cmd = GPIO_SDMMC_CMD;
+    slot_config.d0 = GPIO_SDMMC_D0;
+    slot_config.d1 = GPIO_SDMMC_D1;
+    slot_config.d2 = GPIO_SDMMC_D2;
+    slot_config.d3 = GPIO_SDMMC_D3;
+#endif
+    slot_config.cd = GPIO_SDMMC_DET;
+    slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
+#else
+    slot_config.gpio_cs = GPIO_SDSPI_CS;
+    slot_config.host_id = host.slot;
+#endif
+    /**
+     * @brief Enable internal pullups on enabled pins. The internal pullups
+     *   are insufficient however, please make sure 10k external pullups are
+     *   connected on the bus. This is for debug / example purpose only.
+     */
+
+    /* get FAT filesystem on SD card registered in VFS. */
+    ret_val =
+#if FUNC_SDMMC_EN
+        esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
+#else
+        esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
+#endif
+
+    /* Check for SDMMC mount result. */
+    if (ret_val != ESP_OK) {
+        if (ret_val == ESP_FAIL) {
+            ESP_LOGE(TAG, "Failed to mount filesystem. "
+                     "If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
+        } else {
+            ESP_LOGE(TAG, "Failed to initialize the card (%s). "
+                     "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret_val));
+        }
+        return ret_val;
+    }
+
+    /* Card has been initialized, print its properties. */
+    sdmmc_card_print_info(stdout, card);
+    SDCard_Size = ((uint64_t) card->csd.capacity) * card->csd.sector_size / (1024 * 1024);
+    return ret_val;
+}
+
+esp_err_t esp_sdcard_deinit(char *mount_point)
+{
+    if (NULL == mount_point) {
+        return ESP_ERR_INVALID_STATE;
+    }
+
+    /* Unmount an SD card from the FAT filesystem and release resources acquired */
+    esp_err_t ret_val = esp_vfs_fat_sdcard_unmount(mount_point, card);
+
+    /* Make SD/MMC card information structure pointer NULL */
+    card = NULL;
+
+    return ret_val;
+}
+
+#define MOUNT_POINT "/sdcard"
+
+static const char *SD_TAG = "SD";
+
+
+
+uint16_t Folder_retrieval(const char* directory, const char* fileExtension, char File_Name[][MAX_FILE_NAME_SIZE], uint16_t maxFiles)    
+{
+    DIR *dir = opendir(directory);  
+    if (dir == NULL) {
+        ESP_LOGE(SD_TAG, "Path: <%s> does not exist", directory); 
+        return 0;  
+    }
+
+    uint16_t fileCount = 0;  
+    struct dirent *entry;   
+
+    while ((entry = readdir(dir)) != NULL && fileCount < maxFiles) {
+
+        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
+            continue;
+        }
+
+        const char *dot = strrchr(entry->d_name, '.');  
+        if (dot != NULL && dot != entry->d_name) { 
+
+            if (strcasecmp(dot, fileExtension) == 0) {  
+                strncpy(File_Name[fileCount], entry->d_name, MAX_FILE_NAME_SIZE - 1);
+                File_Name[fileCount][MAX_FILE_NAME_SIZE - 1] = '\0';  
+
+                char filePath[MAX_PATH_SIZE];
+                snprintf(filePath, MAX_PATH_SIZE, "%s/%s", directory, entry->d_name);
+
+                printf("File found: %s\r\n", filePath); 
+                fileCount++;  
+            }
+        }
+        else{
+           
+            // printf("No extension found for file: %s\r\n", entry->d_name);
+        }
+    }
+
+    closedir(dir);  
+
+    if (fileCount > 0) {
+        ESP_LOGI(SD_TAG, "Retrieved %d files with extension '%s'", fileCount, fileExtension);  
+    } else {
+        ESP_LOGW(SD_TAG, "No files with extension '%s' found in directory: %s", fileExtension, directory); 
+    }
+
+    return fileCount; 
+}

+ 45 - 0
2_way_tochie/main/hardeware_driver/bsp_board.h

@@ -20,10 +20,19 @@ extern "C" {
 #define MAX_FILE_NAME_SIZE 100  // Define maximum file name size
 #define MAX_PATH_SIZE 512      // Define a larger size for the full path
 
+/**
+ * @brief ESP32-S3-CAM-OVxxxx I2C GPIO defineation
+ * 
+ */
 #define I2C_NUM         (0)
 #define GPIO_I2C_SCL    (GPIO_NUM_10)
 #define GPIO_I2C_SDA    (GPIO_NUM_11)
 
+/**
+ * @brief ESP32-S3-CAM-OVxxxx SDMMC GPIO defination
+ * 
+ * @note Only avaliable when PMOD connected
+ */
 #define FUNC_SDMMC_EN   (1)
 #define SDMMC_BUS_WIDTH (1)
 #define GPIO_SDMMC_CLK  (GPIO_NUM_40)
@@ -34,6 +43,10 @@ extern "C" {
 #define GPIO_SDMMC_D3   (GPIO_NUM_NC)
 #define GPIO_SDMMC_DET  (GPIO_NUM_NC)
 
+/**
+ * @brief ESP32-S3-CAM-OVxxxx SDSPI GPIO definationv
+ * 
+ */
 #define FUNC_SDSPI_EN       (0)
 #define SDSPI_HOST          (SPI2_HOST)
 #define GPIO_SDSPI_CS       (GPIO_NUM_NC)
@@ -41,6 +54,10 @@ extern "C" {
 #define GPIO_SDSPI_MISO     (GPIO_NUM_NC)
 #define GPIO_SDSPI_MOSI     (GPIO_NUM_NC)
 
+/**
+ * @brief ESP32-S3-CAM-OVxxxx I2S GPIO defination
+ * 
+ */
 #define FUNC_I2S_EN         (1)
 #define GPIO_I2S_LRCK       (GPIO_NUM_14)
 #define GPIO_I2S_MCLK       (GPIO_NUM_12)
@@ -48,6 +65,10 @@ extern "C" {
 #define GPIO_I2S_SDIN       (GPIO_NUM_15)
 #define GPIO_I2S_DOUT       (GPIO_NUM_16)
 
+/**
+ * @brief ESP32-S3-CAM-OVxxxx I2S GPIO defination
+ * 
+ */
 #define FUNC_I2S0_EN         (0)
 #define GPIO_I2S0_LRCK       (GPIO_NUM_NC)
 #define GPIO_I2S0_MCLK       (GPIO_NUM_NC)
@@ -55,13 +76,32 @@ extern "C" {
 #define GPIO_I2S0_SDIN       (GPIO_NUM_NC)
 #define GPIO_I2S0_DOUT       (GPIO_NUM_NC)
 
+/**
+ * @brief record configurations
+ *
+ */
 #define RECORD_VOLUME   (30.0)
 
+/**
+ * @brief player configurations
+ *
+ */
 #define PLAYER_VOLUME   (60)
 
+/**
+ * @brief ESP32-S3-HMI-DevKit power control IO
+ * 
+ * @note Some power control pins might not be listed yet
+ * 
+ */
+//#define FUNC_PWR_CTRL       (1)
 #define GPIO_PWR_CTRL       (-1)
 #define GPIO_PWR_ON_LEVEL   (1)
 
+/* Waveshare ESP32-S3 audio board button mapping.
+ * K1/K2/K3 are wired through the TCA9555 IO expander (not raw ESP32 GPIOs) -
+ * GPIO0 must not be used here since it is the BOOT/RESET strapping pin.
+ * K1 = volume up, K3 = volume down (matches the official Waveshare factory demo). */
 #define KEY_VOLUME_UP_PIN   (1ULL << 9)   /* K1, TCA9555 expander pin 9 */
 #define KEY_VOLUME_DOWN_PIN (1ULL << 11)  /* K3, TCA9555 expander pin 11 */
 #define VOLUME_BUTTON_POLL_MS (50)
@@ -152,6 +192,9 @@ extern "C" {
 
 esp_err_t esp_board_init(uint32_t sample_rate, int channel_format, int bits_per_chan);
 
+esp_err_t esp_sdcard_init(char *mount_point, size_t max_files);
+esp_err_t esp_sdcard_deinit(char *mount_point);
+
 esp_err_t esp_audio_play(const int16_t* data, int length, uint32_t ticks_to_wait);
 int bsp_aec_get_frame_size(void);
 esp_err_t bsp_aec_process_frame(const int16_t *near_end,
@@ -166,6 +209,8 @@ esp_err_t esp_audio_get_play_vol(int *volume);
 
 i2c_master_bus_handle_t esp_ret_i2c_handle(void);
 esp_codec_dev_handle_t esp_ret_play_dev();
+uint32_t Get_SD_Size(void);
+uint16_t Folder_retrieval(const char* directory, const char* fileExtension, char File_Name[][MAX_FILE_NAME_SIZE], uint16_t maxFiles) ;
 
 #ifdef __cplusplus
 }

+ 88 - 7
2_way_tochie/main/sip/sip.c

@@ -21,14 +21,21 @@
 
 #include "bsp_board.h"
 #include "tca9555_driver.h"
-
-#define ESP32_IP "192.168.68.16"
+/*
+#include "esp_audio_enc_default.h"
+#include "esp_audio_enc.h"
+#include "esp_audio_dec_default.h"
+#include "esp_audio_dec.h"
+#include "esp_g711_enc.h"
+#include "esp_g711_dec.h"*/
+
+#define ESP32_IP "192.168.68.67"
 #define RTP_PORT 4000
 
 #define WIFI_SSID "TOCHTECH"
 #define WIFI_PASS "Smarturns2017"
 
-#define ASTERISK_IP "192.168.68.90"
+#define ASTERISK_IP "192.168.68.118"
 #define SIP_PORT 5060
 #define SIP_LOCAL_IP "0.0.0.0"
 #define SIP_LOCAL_PORT 5062
@@ -138,6 +145,8 @@ static bool aec_process_capture(const int16_t *near_end,
                 return false;
             }
 
+            /* AEC runs at 16 kHz. The negotiated PCMU stream is 8 kHz,
+             * so retain every second cleaned sample for RTP encoding. */
             for (int sample = 0; sample < frame_size; sample += 2) {
                 if (aec_output_samples < FAR_END_FIFO_SAMPLES) {
                     aec_output_fifo[aec_output_samples++] = processed[sample];
@@ -432,8 +441,21 @@ static bool sip_extract_rtp_port(const char *msg, uint16_t *out_port)
 
 static bool microphone_read(int16_t *samples, size_t count)
 {
+    // ESP_LOGI(TAG,
+    //          "mic ptr=%p samples=%d bytes=%d",
+    //          samples,
+    //          count,
+    //          count * sizeof(int16_t));
 
     esp_err_t ret = esp_get_feed_data(false, samples, count);
+
+
+
+
+
+    
+
+
     
     if(ret != ESP_OK)
     {
@@ -517,6 +539,11 @@ static void sip_send_rtp_packet(void)
 
 
     static uint32_t call_count = 0;
+/*
+ESP_LOGI(TAG,
+         "sip_send_rtp_packet() #%lu",
+         (unsigned long)call_count++);*/
+
          
     static uint16_t seq = 0;
     static uint32_t timestamp = 0;
@@ -570,6 +597,8 @@ for(int i = 0; i < SAMPLES_PER_PACKET; i++)
     if(value > max)
         max = value;
 }
+/*
+ESP_LOGI("MIC", "max=%d", max);*/
 
 
 // PCM16 -> PCMU (G711 u-law)
@@ -578,6 +607,21 @@ for(int i = 0; i < SAMPLES_PER_PACKET; i++)
     payload[i] = linear2ulaw(mic_pcm16[i]);
 }
 
+
+
+//testing multiple packets
+/*
+ESP_LOGI("MIC",
+         "%6d %6d %6d %6d %6d %6d %6d %6d "
+         "%6d %6d %6d %6d %6d %6d %6d %6d "
+         "%6d %6d %6d %6d",
+         mic_pcm16[0], mic_pcm16[1], mic_pcm16[2], mic_pcm16[3],
+         mic_pcm16[4], mic_pcm16[5], mic_pcm16[6], mic_pcm16[7],
+         mic_pcm16[8], mic_pcm16[9], mic_pcm16[10], mic_pcm16[11],
+         mic_pcm16[12], mic_pcm16[13], mic_pcm16[14], mic_pcm16[15],
+         mic_pcm16[16], mic_pcm16[17], mic_pcm16[18], mic_pcm16[19]);
+*/
+
     uint8_t packet[12 + SAMPLES_PER_PACKET];
 
 
@@ -616,12 +660,21 @@ packet[11] = ssrc & 0xff;
     struct sockaddr_in peer = s_rtp_peer;
 
 peer.sin_port = htons(s_rtp_peer_port);
+/*ESP_LOGI(TAG,
+         "RTP destination IP: %s",
+         inet_ntoa(peer.sin_addr));
+
+ESP_LOGI(TAG,
+         "Sending RTP to %s:%d",
+         inet_ntoa(peer.sin_addr),
+         ntohs(peer.sin_port));*/
 
 esp_netif_ip_info_t ip;
 esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
 
 esp_netif_get_ip_info(netif, &ip);
 
+/*ESP_LOGI(TAG, "Current IP: " IPSTR, IP2STR(&ip.ip));*/
 int ret =
     sendto(
         s_rtp_sock,
@@ -639,9 +692,20 @@ if(ret < 0)
 }
 else
 {
-
+   /* ESP_LOGI(TAG,
+             "RTP sent bytes=%d seq=%d",
+             ret,
+             seq);*/
 }
 
+    /*if(ret > 0)
+    {
+        ESP_LOGI(TAG,
+                 "RTP sent seq=%d",
+                 seq);
+    }*/
+
+
     seq++;
 
     timestamp += SAMPLES_PER_PACKET;
@@ -656,7 +720,7 @@ static void rtp_tx_task(void *pvParameters)
     TickType_t last_wake = xTaskGetTickCount();
 
 while(call_active)
-{
+{/*ESP_LOGI(TAG, "RTP loop");*/
     sip_send_rtp_packet();
 
     vTaskDelayUntil(
@@ -721,6 +785,8 @@ if (src.sin_addr.s_addr == local_ip.ip.addr)
     continue;
 }
 
+// ESP_LOGI(TAG, "Received RTP packet (%d bytes)", n);
+
 if (n <= 12)
 {
     ESP_LOGW(TAG, "Packet too small");
@@ -772,6 +838,13 @@ esp_err_t ret = esp_audio_play(
     payload_size * 4 * sizeof(int16_t),
     portMAX_DELAY);
 
+
+// ESP_LOGI(TAG,
+//          "esp_audio_play() = %s",
+//          esp_err_to_name(ret));
+
+//             ESP_LOGI(TAG, "esp_audio_play() returned %s",
+//              esp_err_to_name(ret));
 }
 
     while (!rtp_tx_done) {
@@ -876,6 +949,7 @@ void sip_register_task(void *pvParameters)
         }
 
         response[len] = '\0';
+        // printf("SIP RESPONSE:\n%s\n", response);
         ESP_LOGI(TAG, "Received SIP response (%d bytes)", len);
 
         if (strstr(response, "SIP/2.0 401")) {
@@ -1010,7 +1084,11 @@ void sip_register_task(void *pvParameters)
         rx_buf[rx_len] = '\0';
         ESP_LOGI(TAG, "\n===== RAW INVITE =====\n%s\n=======================\n", rx_buf);
 
+        // printf("\n========================================\n");
+        // printf("SIP MESSAGE RECEIVED:\n");
+        // printf("%s\n", rx_buf);
         ESP_LOGI(TAG, "Received SIP packet (%d bytes)", rx_len);
+        // printf("========================================\n");
 
         if (strncmp(rx_buf, "REGISTER ", 9) == 0) {
             ESP_LOGI(TAG, "Received REGISTER; sending 200 OK");
@@ -1210,7 +1288,8 @@ for(int i = 0; i < 4; i++)
             rtp_tx_done = true;
         }
     }
-
+//temp comment out
+    //ESP_LOGI(TAG, "rtp task create result=%d", ret);
 }
 else if (!call_active && rtp_tasks_running) {
     ESP_LOGW(TAG, "Previous RTP call is still shutting down");
@@ -1253,7 +1332,9 @@ esp_err_t sip_media_init(void)
     }
 
     tca9555_driver_init();
-
+    /*if (!sip_open_audio_media_codecs()) {
+        return ESP_FAIL;
+    }*/
     s_audio_hw_ready = true;
     ESP_LOGI(TAG, "Audio board initialized for live RTP media");
     return ESP_OK;

+ 0 - 17
2_way_tochie/main/tca9555_driver/tca9555_driver.c

@@ -5,8 +5,6 @@
 
 esp_io_expander_handle_t io_expander = NULL;
 
-#define AUDIO_POWER_MASK (IO_EXPANDER_PIN_NUM_0 | IO_EXPANDER_PIN_NUM_1 | IO_EXPANDER_PIN_NUM_5 | IO_EXPANDER_PIN_NUM_6 | IO_EXPANDER_PIN_NUM_8)
-
 
 void tca9555_driver_init(void)
 {
@@ -26,21 +24,6 @@ void tca9555_driver_init(void)
    }
 }
 
-void tca9555_enable_audio_power(void)
-{
-    if (io_expander == NULL) {
-        ESP_LOGW("TCA9555", "IO expander not initialized, cannot enable audio power");
-        return;
-    }
-
-    esp_err_t ret = esp_io_expander_set_level(io_expander, AUDIO_POWER_MASK, 1);
-    if (ret != ESP_OK) {
-        ESP_LOGW("TCA9555", "Failed to enable audio power rails: %s", esp_err_to_name(ret));
-    } else {
-        ESP_LOGI("TCA9555", "Audio power rails enabled via IO expander");
-    }
-}
-
 
 /********************************************************** Set the EXIO output status **********************************************************/  
 void Set_EXIO(uint32_t Pin,uint8_t State)                  // Sets the level state of the Pin without affecting the other pins(PIN:1~8)

+ 0 - 1
2_way_tochie/main/tca9555_driver/tca9555_driver.h

@@ -9,7 +9,6 @@ extern "C" {
 
 extern esp_io_expander_handle_t io_expander;
 void tca9555_driver_init(void);
-void tca9555_enable_audio_power(void);
 void Set_EXIO(uint32_t Pin,uint8_t State);    
 bool Read_EXIO(uint32_t Pin);