Matchboxscope / Omniscope-MultiCamViewer

0 stars 0 forks source link

Connecting Cameras Via I2C or SPI? #2

Open beniroquai opened 5 months ago

beniroquai commented 5 months ago

This is the SD card adapter pinout from the XIAO Sense:

image

This is the pinout of the microsd card image

So we can use the following wiring to realize I2C:

VSS - GND
VDD - 3V3
D8 - CLK
D10 - CMD
D9 - DO
SD_CS - D21?

From: https://forum.seeedstudio.com/t/using-sd-mmc-library-on-seeed-xiao-esp32s3-sense/271615/12

On ESP32-S3, SDMMC peripheral is connected to GPIO pins using GPIO matrix. This allows arbitrary GPIOs to be used to connect an SD card or MMC. The GPIOs can be configured with the following commands:

#define SD_MMC_CLK 7
#define SD_MMC_CMD 9
#define SD_MMC_D0 8
setPins(int clk, int cmd, int d0))
setPins(int clk, int cmd, int d0, int d1, int d2, int d3))
setPins(7,9,8))

So we can use e.g. D8, D9 for I2C:

#include "driver/i2c.h"
#include "esp_camera.h"

// Initialize I2C as master
void i2c_master_init() {
    int i2c_master_port = I2C_NUM_0;
    i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = D8,
        .scl_io_num = D9,
        .master.clk_speed = 400000, // 400kHz
        .pullup_en = GPIO_PULLUP_ENABLE,
    };
    i2c_param_config(i2c_master_port, &conf);
    i2c_driver_install(i2c_master_port, conf.mode, 0, 0, 0);
}

// Example function to write camera data to a slave device
void i2c_write_image(uint8_t slave_addr, camera_fb_t *fb) {
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, ( slave_addr << 1 ) | I2C_MASTER_WRITE, true);
    i2c_master_write(cmd, fb->buf, fb->len, true);
    i2c_master_stop(cmd);
    i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_PERIOD_MS);
    i2c_cmd_link_delete(cmd);
}

void app_main() {
    i2c_master_init();

    // Assume camera initialization is done elsewhere

    while (1) {
        camera_fb_t *fb = esp_camera_fb_get();
        if (!fb) {
            printf("Camera capture failed");
        } else {
            i2c_write_image(SLAVE_ADDR, fb); // Define SLAVE_ADDR accordingly
            esp_camera_fb_return(fb);
        }

        vTaskDelay(pdMS_TO_TICKS(2000)); // Delay for demonstration
    }
}
beniroquai commented 5 months ago
image
beniroquai commented 5 months ago

Alternatively, perhaps we could use SPI instead?

ESP32-S3 GPIO microSD Card Slot
GPIO21 CS
D8 / A8 / Qt7 / GPIO7 SCK
D9 / A9 / Qt8 / GPIO8 MISO
D10 / A10 / Qt9 / GPIO9 MOSI

SRC: https://wiki.seeedstudio.com/xiao_esp32s3_sense_filesystem/#card-slot-circuit-design-for-expansion-boards

image

Any thoughts @KillerInk 😇

Some problems I identify with I2C:

Current state (I2C):

Master: (Reader) Master https://github.com/Matchboxscope/Omniscope-MultiCamViewer/blob/I2C_Test/ESP32/OmniscopeFW/I2CServer/I2CServer.ino

Slave: (Sender) https://github.com/Matchboxscope/Omniscope-MultiCamViewer/blob/I2C_Test/ESP32/OmniscopeFW/main/main.ino

How it could look like using SPI:

image
beniroquai commented 5 months ago

Seems like someone did it already: https://github.com/PowerBroker2/SerialTransfer/issues/70

beniroquai commented 5 months ago

SPI seems to be working. And it's really fast!

Master: https://github.com/Matchboxscope/Omniscope-MultiCamViewer/blob/SPI_Test/ESP32/OmniscopeFW/SPIServer/SPIServer.ino

Slave: https://github.com/Matchboxscope/Omniscope-MultiCamViewer/blob/SPI_Test/ESP32/OmniscopeFW/main/main.ino

Here, I connect an ESP32 to the Xiao via the micro-sd card slot image

beniroquai commented 5 months ago

Something's not right:


SP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x4202bb22
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbe4
load:0x403cc700,len:0x2a38
entry 0x403c98d4
[   109][I][esp32-hal-psram.c:96] psramInit(): PSRAM enabled
PSRAM FOUND
[  2306][D][ESP32DMASPISlave.h:112] spi_slave_task(): [ESP32DMASPISlave] spi_slave_task start
start spi slave
Original frame size: 8930
8601135
Padded frame size: 8932
8592187
initialize tx/rx buffers
execute transaction in the background
8592187
[  2308][D][ESP32DMASPISlave.h:140] spi_slave_task(): [ESP32DMASPISlave] new transaction request received (size = 1)
Guru Meditation Error: Core  1 panic'ed (Double exception). 

Core  1 register dump:
PC      : 0x4037b01a  PS      : 0x00040136  A0      : 0x8037842f  A1      : 0x3fcecef0  
A2      : 0x00040136  A3      : 0x00040026  A4      : 0x00000001  A5      : 0x00000051  
A6      : 0x02c95188  A7      : 0x00ffffff  A8      : 0x3fcecfb0  A9      : 0x3fcea04c  
A10     : 0x00000054  A11     : 0x00000001  A12     : 0x00000000  A13     : 0x00005004  
A14     : 0x3fcf04d4  A15     : 0x00000001  SAR     : 0x00000004  EXCCAUSE: 0x00000002  
EXCVADDR: 0x3fcea04c  LBEG    : 0x400556d5  LEND    : 0x400556e5  LCOUNT  : 0xfffffff7  

Backtrace: 0x4037b017:0x3fcecef0 0x4037842c:0x00000080 |<-CORRUPTED

  #0  0x4037b017:0x3fcecef0 in _xt_context_save at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/freertos/port/xtensa/xtensa_context.S:194
  #1  0x4037842c:0x00000080 in lock_init_generic at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/newlib/locks.c:73

ELF file SHA256: 6126fb6d3597542a

Rebooting...
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x4202bb22
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbe4
load:0x403cc700,len:0x2a38
entry 0x403c98d4
[   109][I
beniroquai commented 5 months ago

This I do not understand. On the Xiao it seems to be working, but anyway, the ESP reboots: https://github.com/hideakitai/ESP32DMASPI/issues/7