espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.48k stars 7.26k forks source link

esp-eye board camera timeout (IDFGH-13229) #14164

Closed leehakgun closed 2 months ago

leehakgun commented 2 months ago

Answers checklist.

IDF version.

v5.2.2

Operating System used.

Windows

How did you build your project?

VS Code IDE

If you are using Windows, please specify command line type.

None

What is the expected behavior?

A function that measures the voice dB and captures the image when it exceeds a certain dB and outputs it to the web server

What is the actual behavior?

현재 i2s_channel_read 이 부분에서 제대로 작동하지 않아 버그가 발생하고 있습니다,

Steps to reproduce.

include

include

include "freertos/FreeRTOS.h"

include "freertos/task.h"

include "driver/gpio.h"

include "esp_camera.h"

include "esp_http_server.h"

include "esp_timer.h"

include "connect_wifi.h"

include "esp_log.h"

include

include "driver/i2s_std.h"

include "esp_heap_caps.h"

// 카메라 핀 설정

define PWDN_GPIO_NUM -1

define RESET_GPIO_NUM -1

define XCLK_GPIO_NUM 4

define SIOD_GPIO_NUM 18

define SIOC_GPIO_NUM 23

define Y9_GPIO_NUM 36

define Y8_GPIO_NUM 37

define Y7_GPIO_NUM 38

define Y6_GPIO_NUM 39

define Y5_GPIO_NUM 35

define Y4_GPIO_NUM 14

define Y3_GPIO_NUM 13

define Y2_GPIO_NUM 34

define VSYNC_GPIO_NUM 5

define HREF_GPIO_NUM 27

define PCLK_GPIO_NUM 25

static const char *TAG = "esp32-cam Webserver";

camera_fb_t *last_fb = NULL; bool capture_flag = false; static i2s_chan_handle_t rx_chan;

static void print_memory_status() { ESP_LOGI(TAG, "Free heap: %d", (unsigned int)esp_get_free_heap_size()); ESP_LOGI(TAG, "Free internal heap: %d", (unsigned int)heap_caps_get_free_size(MALLOC_CAP_INTERNAL)); ESP_LOGI(TAG, "Free PSRAM: %d", (unsigned int)heap_caps_get_free_size(MALLOC_CAP_SPIRAM)); }

static esp_err_t init_camera(void) { print_memory_status();

camera_config_t camera_config = {
    .ledc_channel = LEDC_CHANNEL_0,
    .ledc_timer = LEDC_TIMER_0,
    .pin_d0 = Y2_GPIO_NUM,
    .pin_d1 = Y3_GPIO_NUM,
    .pin_d2 = Y4_GPIO_NUM,
    .pin_d3 = Y5_GPIO_NUM,
    .pin_d4 = Y6_GPIO_NUM,
    .pin_d5 = Y7_GPIO_NUM,
    .pin_d6 = Y8_GPIO_NUM,
    .pin_d7 = Y9_GPIO_NUM,
    .pin_xclk = XCLK_GPIO_NUM,
    .pin_pclk = PCLK_GPIO_NUM,
    .pin_vsync = VSYNC_GPIO_NUM,
    .pin_href = HREF_GPIO_NUM,
    .pin_sscb_sda = SIOD_GPIO_NUM,
    .pin_sscb_scl = SIOC_GPIO_NUM,
    .pin_pwdn = PWDN_GPIO_NUM,
    .pin_reset = RESET_GPIO_NUM,
    .xclk_freq_hz = 8000000,
    .pixel_format = PIXFORMAT_JPEG,
    .frame_size = FRAMESIZE_QVGA,
    .jpeg_quality = 10,
    .fb_count = 2,
    .grab_mode = CAMERA_GRAB_LATEST,
};
esp_err_t err = esp_camera_init(&camera_config);
if (err != ESP_OK)
{
    ESP_LOGE(TAG, "Camera init failed with error 0x%x", err);
    return err;
}
return ESP_OK;

}

define I2S_NUM 0

define SAMPLE_RATE 44100

define SAMPLE_BITS I2S_BITS_PER_SAMPLE_16BIT

define I2S_BUF_LEN 512

static void init_i2s() { i2s_chan_config_t rx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER); ESP_ERROR_CHECK(i2s_new_channel(&rx_chan_cfg, NULL, &rx_chan));

i2s_std_config_t rx_std_cfg = {
    .clk_cfg  = I2S_STD_CLK_DEFAULT_CONFIG(16000),
    .slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_STEREO),
    .gpio_cfg = {
        .mclk = I2S_GPIO_UNUSED,    
        .bclk = 26,
        .ws   = 32,
        .dout = I2S_GPIO_UNUSED,
        .din  = 33,
        .invert_flags = {
            .mclk_inv = false,
            .bclk_inv = false,
            .ws_inv   = false,
        },
    },
};
rx_std_cfg.slot_cfg.slot_bit_width = I2S_SLOT_BIT_WIDTH_32BIT;
rx_std_cfg.slot_cfg.slot_mask = I2S_STD_SLOT_LEFT; 

ESP_LOGI(TAG, "Initializing I2S RX channel...");
ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_chan, &rx_std_cfg));
ESP_ERROR_CHECK(i2s_channel_enable(rx_chan));
ESP_LOGI(TAG, "I2S initialized successfully: RX channel handle: %p", (void *)rx_chan);

}

void measure_decibel() { ESP_LOGI(TAG, "Reading I2S data with RX channel handle: %p", (void )rx_chan); int16_t i2s_buffer = (int16_t )heap_caps_malloc(I2S_BUF_LEN sizeof(int16_t), MALLOC_CAP_DMA); if (!i2s_buffer) { ESP_LOGE(TAG, "Failed to allocate I2S buffer"); return; }

size_t bytes_read = 0;
ESP_LOGI(TAG, "Before i2s_channel_read...");
esp_err_t ret = i2s_channel_read(rx_chan, i2s_buffer, I2S_BUF_LEN * sizeof(uint8_t), &bytes_read, 1000 / portTICK_PERIOD_MS);
ESP_LOGI(TAG, "After i2s_channel_read...");
if (ret != ESP_OK) {
    ESP_LOGE(TAG, "I2S read error: %s", esp_err_to_name(ret));
    ESP_LOGE(TAG, "Error details: ret=%d, bytes_read=%d", ret, bytes_read);
    heap_caps_free(i2s_buffer);
    return;
}

ESP_LOGI(TAG, "Processing I2S data");
uint32_t sum_squares = 0;
for (int i = 0; i < bytes_read / sizeof(int16_t); ++i) {
    sum_squares += i2s_buffer[i] * i2s_buffer[i];
}
float rms = sqrt((float)sum_squares / (bytes_read / sizeof(int16_t)));
float decibels = 20 * log10(rms);

printf("RMS: %f dB: %f\n", rms, decibels);
if (decibels >= 45) {
    ESP_LOGI(TAG, "dB over");
    capture_flag = true;
}

heap_caps_free(i2s_buffer);

}

void capture_image_task(void *pvParameters) { while (true) { if (capture_flag) { capture_flag = false;

        camera_fb_t *fb = esp_camera_fb_get();
        if (fb)
        {
            if (last_fb)
            {
                esp_camera_fb_return(last_fb);
            }
            last_fb = fb;
            ESP_LOGI(TAG, "Image captured");
        }
        else
        {
            ESP_LOGE(TAG, "Failed to capture image, reinitializing camera");
            esp_camera_deinit();
            init_camera();
            vTaskDelay(pdMS_TO_TICKS(3000));
        }
    }

    ESP_LOGI(TAG, "Starting to measure decibel");
    measure_decibel();
    ESP_LOGI(TAG, "Decibel measurement done");

    vTaskDelay(pdMS_TO_TICKS(1500));
}

}

void app_main() { esp_err_t err = 0; if (!wifi_connect_status) { err = init_camera(); if (err != ESP_OK) { ESP_LOGE(TAG, "Camera init failed: %s", esp_err_to_name(err)); return; } init_i2s(); ESP_LOGI(TAG, "ESP32 CAM Web Server is up and running\n");

    xTaskCreatePinnedToCore(capture_image_task, "capture_image_task", 8192, NULL, 5, NULL, 0);
}

esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
    ESP_ERROR_CHECK(nvs_flash_erase());
    ret = nvs_flash_init();
}

}

Build or installation Logs.

I (26) boot: ESP-IDF v5.0 2nd stage bootloader
I (26) boot: compile time 16:34:06
I (27) boot: chip revision: v1.0
I (29) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (36) boot.esp32: SPI Speed      : 40MHz
I (41) boot.esp32: SPI Mode       : DIO
I (45) boot.esp32: SPI Flash Size : 2MB
I (50) boot: Enabling RNG early entropy source...
I (55) boot: Partition Table:
I (59) boot: ## Label            Usage          Type ST Offset   Length
I (66) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (74) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (81) boot:  2 factory          factory app      00 00 00010000 00100000
I (89) boot: End of partition table
I (93) boot_comm: chip revision: 1, min. application chip revision: 0
I (100) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=13948h ( 80200) map
I (137) esp_image: segment 1: paddr=00023970 vaddr=3ffb0000 size=03c3ch ( 15420) load
I (144) esp_image: segment 2: paddr=000275b4 vaddr=40080000 size=08a64h ( 35428) load
I (159) esp_image: segment 3: paddr=00030020 vaddr=400d0020 size=2ff20h (196384) map
I (230) esp_image: segment 4: paddr=0005ff48 vaddr=40088a64 size=0ae00h ( 44544) load
I (248) esp_image: segment 5: paddr=0006ad50 vaddr=50000000 size=00010h (    16) load
I (259) boot: Loaded app from partition at offset 0x10000
I (259) boot: Disabling RNG early entropy source...
I (271) quad_psram: This chip is ESP32-D0WD
I (273) esp_psram: Found 8MB PSRAM device
I (273) esp_psram: Speed: 40MHz
I (275) esp_psram: PSRAM initialized, cache is in low/high (2-core) mode.
W (283) esp_psram: Virtual address not enough for PSRAM, map as much as we can. 4MB is mapped
I (292) cpu_start: Pro cpu up.
I (295) cpu_start: Starting app cpu, entry point is 0x400816bc
0x400816bc: call_start_cpu1 at C:/Users/admin/esp/v5.0/esp-idf/components/esp_system/port/cpu_start.c:142

I (0) cpu_start: App cpu up.
I (1205) esp_psram: SPI SRAM memory test OK
I (1213) cpu_start: Pro cpu start user code
I (1213) cpu_start: cpu freq: 160000000 Hz
I (1213) cpu_start: Application information:
I (1216) cpu_start: Project name:     camera_example
I (1222) cpu_start: App version:      1
I (1226) cpu_start: Compile time:     Jul 11 2024 16:33:25
I (1233) cpu_start: ELF file SHA256:  977b1eb605455202...
I (1239) cpu_start: ESP-IDF:          v5.0
I (1244) heap_init: Initializing. RAM available for dynamic allocation:
I (1251) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (1257) heap_init: At 3FFB46E0 len 0002B920 (174 KiB): DRAM
I (1263) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (1270) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (1276) heap_init: At 40093864 len 0000C79C (49 KiB): IRAM
I (1283) esp_psram: Adding pool of 4096K of PSRAM memory to heap allocator        
I (1291) spi_flash: detected chip: generic
I (1295) spi_flash: flash io: dio
W (1299) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (1313) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (1323) esp_psram: Reserving pool of 32K of internal memory for DMA/internal allocations
I (1333) esp32-cam Webserver: Free heap: 4446856
I (1333) esp32-cam Webserver: Free internal heap: 334155
I (1343) esp32-cam Webserver: Free PSRAM: 4192152
I (1353) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:2
I (1363) cam_hal: cam init ok
I (1363) sccb: pin_sda 18 pin_scl 23
I (1363) sccb: sccb_i2c_port=1
I (1383) camera: Detected camera at address=0x30
I (1383) camera: Detected OV2640 camera
I (1383) camera: Camera PID=0x26 VER=0x42 MIDL=0x7f MIDH=0xa2
I (1433) cam_hal: buffer_size: 32768, half_buffer_size: 4096, node_buffer_size: 2048, node_cnt: 16, total_cnt: 3
I (1433) cam_hal: Allocating 15360 Byte frame buffer in PSRAM
I (1443) cam_hal: Allocating 15360 Byte frame buffer in PSRAM
I (1443) cam_hal: cam config ok
I (1453) ov2640: Set PLL: clk_2x: 0, clk_div: 0, pclk_auto: 0, pclk_div: 8        
I (1503) esp32-cam Webserver: Initializing I2S RX channel...
I (1503) esp32-cam Webserver: I2S initialized successfully: RX channel handle: 0x3f808154
I (1503) esp32-cam Webserver: ESP32 CAM Web Server is up and running

I (1513) esp32-cam Webserver: Starting to measure decibel
I (1523) esp32-cam Webserver: Reading I2S data with RX channel handle: 0x3f808154
I (1523) esp32-cam Webserver: Before i2s_channel_read...
I (1633) esp32-cam Webserver: After i2s_channel_read...
E (1633) esp32-cam Webserver: I2S read error: ESP_ERR_TIMEOUT
E (1633) esp32-cam Webserver: Error details: ret=263, bytes_read=0
I (1633) esp32-cam Webserver: Decibel measurement done
W (1753) cam_hal: NO-SOI
W (1803) cam_hal: NO-SOI
W (1853) cam_hal: NO-SOI
W (1903) cam_hal: NO-SOI
W (1953) cam_hal: NO-SOI
W (2003) cam_hal: NO-SOI
W (2053) cam_hal: NO-SOI
W (2103) cam_hal: NO-SOI
W (2153) cam_hal: NO-SOI
W (2203) cam_hal: NO-SOI
W (2253) cam_hal: NO-SOI
W (2303) cam_hal: NO-SOI
W (2353) cam_hal: NO-SOI
W (2403) cam_hal: NO-SOI
W (2453) cam_hal: NO-SOI
W (2503) cam_hal: NO-SOI

More Information.

No response

Sandra-lol commented 2 months ago

Hi, we've several suggestions for your reference:

1)May use examples/camera from esp-iot-solution repo, to validate that the devboard and camera sensor are working fine. 2)DVP camera on esp32-eye is implemented with I2S0 as default, so if you're using I2S, better to avoid I2S0 when camera is enabled.

Have a nice day~

leehakgun commented 2 months ago

I thought it wouldn't matter if I set auto mode, but it wasn't. Thanks to you, the problem has been solved.