[ESP32-CAM] SPI Bus issue

jackyang
Posts: 4
Joined: Fri Jul 18, 2025 5:15 am

[ESP32-CAM] SPI Bus issue

Postby jackyang » Fri Jul 18, 2025 6:43 am

抱歉,似乎沒有 ESP32-CAM 相關版?


我正在使用: ESP32-CAM Module , 原先預計搭配 `DFRobot CAN Shield` , 藉 SPI bus 溝通.
後來 即使換用 外接MCP2515Module亦然 .
Image

用 Ardiono IDE v2.3.6 , 選 `AI Thinker ESP32-CAM` module .

雖然 [utl=https://shop.mirotek.com.tw/iot/iot-start-2/]這裡說[/url] 可以直接選擇 `ESP32 Wrover Module`, 但是 我的IDE選單中 有 `AI Thinker ESP32-CAM` module 選項, 我想 選 `AI Thinker ESP32-CAM` 應該比較不會採坑 吧?


我參考: viewtopic.php?t=10325 做法 修改: `DFRobot_MCP2515.cpp` 檔中的 `begin()` function :

Original:

Code: Select all

uint8_t DFRobot_MCP2515::begin(uint8_t speedset) {
    pinMode(_csPin, OUTPUT);
    digitalWrite(_csPin, HIGH);

    SPI.begin();	 // !!!! 

    mcpReset();

    uint8_t res;
    res = mcpInit(speedset);
    if (res == MCP2515_OK) return CAN_OK;
    else return CAN_FAILINIT;
}
使其於 呼叫 `SPI.begin()` 時 設定相關 SI/SO等參數 :

Code: Select all

uint8_t DFRobot_MCP2515::begin(uint8_t speedset, uint8_t SCK_PIN, uint8_t MISO_PIN, uint8_t MOSI_PIN, uint8_t SS_PIN) {
    pinMode(_csPin, OUTPUT);
    digitalWrite(_csPin, HIGH);

        SPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN);                 // Init SPI bus !!!!
    mcpReset();

    uint8_t res;
    res = mcpInit(speedset);
    if (res == MCP2515_OK) return CAN_OK;
    else return CAN_FAILINIT;
}

不知為何 使用 GPIO 16, 會造成不斷重啟
所以換成 使用 GOIO 2 :

Code: Select all

#define SPI_RST_PIN 2    // 16:=CRASH  !??
#define SPI_SCK_PIN 14   
#define SPI_SS_PIN 15    
#define SPI_MOSI_PIN 13 
#define SPI_MISO_PIN 12 
---

打開 core debug level 到 verbose , 發現下列訊息 :

即使前面顯示 init SPI 成功 :
`12:07:40.972 -> [ 936][ I][SPI.cpp:113] begin(): YG.SPI: _spi_num=3(VSPI), sck=14, so=12, si=13, ss=15`

但是 後面 SPI 仍會被 不知明原因 關閉 :
`12:07:41.008 -> [ 979][ D][esp32-hal-spi.c:221] spiDetachBus(): Stopping SPI bus[3]/4: sck=-1, so=12, si=-1, ss=-1, clock=10227713.`
(MISO 可以正常設定,其他的卻都是 -1 ?)

, 檔案 `c:/Users/<User>/AppData/Local/Arduino15/packages/esp32/hardware/esp32/3.3.0-alpha1/cores/esp32/esp32-hal-spi.c`
中的 `static bool spiDetachBus(void *bus)` function :

Code: Select all

static bool spiDetachBus(void *bus) {
  uint8_t spi_num = (int)bus - 1;
  spi_t *spi = &_spi_bus_array[spi_num];

  if (spi->dev->clock.val == 0) {
    log_d("SPI bus already stopped");
    return true;
  } else if (spi->sck == -1 || (spi->miso == -1 && spi->mosi == -1)) {
    //Original: log_d("Stopping SPI bus"); 
    log_d("Stopping SPI bus[%d]/%d: sck=%d, so=%d, si=%d, ss=%d,  clock=%d.",   spi_num, (sizeof(_spi_bus_array)/sizeof(spi_t)),
                                          spi->sck , spi->miso, spi->mosi, spi->ss, spi->dev->clock.val );
    spiStopBus(spi);

    spiDetachSCK(spi);
    spiDetachMISO(spi);
    spiDetachMOSI(spi);
    spiDetachSS(spi);
    spi = NULL;
    return true;
  }
  return true;
}
@@"?

開機紀錄 :

Code: Select all

12:07:40.876 -> [   821][V][esp32-hal-uart.c:814] uartSetRxFIFOFull(): UART0 RX FIFO Full value set to 120 from a requested value of 120
12:07:40.876 -> 
12:07:40.876 -> [   835][V][NetworkEvents.cpp:117] _checkForEvent(): Network Event: 101 - WIFI_READY
12:07:40.909 -> [   897][V][STA.cpp:186] _onStaEvent(): STA Started
12:07:40.909 -> [   898][V][NetworkEvents.cpp:117] _checkForEvent(): Network Event: 110 - STA_START
12:07:40.909 -> [   899][V][STA.cpp:110] _onStaArduinoEvent(): Arduino STA Event: 110 - STA_START
12:07:40.941 -> [   911][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x40181738
12:07:40.941 -> [   913][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 2 successfully set to type GPIO (1) with bus 0x3
12:07:40.941 -> [   914][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x40181738
12:07:40.941 -> [   915][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 14 successfully set to type GPIO (1) with bus 0xf
12:07:40.941 -> [   917][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x40181738
12:07:40.941 -> [   918][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 15 successfully set to type GPIO (1) with bus 0x10
12:07:40.941 -> [   920][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x40181738
12:07:40.941 -> [   921][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 13 successfully set to type GPIO (1) with bus 0xe
12:07:40.941 -> [   922][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x40181738
12:07:40.941 -> [   924][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 12 successfully set to type GPIO (1) with bus 0xd
12:07:40.941 -> [   925][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type SPI_MASTER_SCK (37) successfully set to 0x400e1904
12:07:40.941 -> [   927][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type SPI_MASTER_MISO (38) successfully set to 0x400e19f4
12:07:40.941 -> [   928][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type SPI_MASTER_MOSI (39) successfully set to 0x400e1ae8
12:07:40.941 -> [   930][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type SPI_MASTER_SS (40) successfully set to 0x400e1be8
12:07:40.941 -> [   932][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 14 successfully set to type SPI_MASTER_SCK (37) with bus 0x4
12:07:40.941 -> [   933][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 12 successfully set to type SPI_MASTER_MISO (38) with bus 0x4
12:07:40.941 -> [   935][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 13 successfully set to type SPI_MASTER_MOSI (39) with bus 0x4
12:07:40.972 -> [   936][I][SPI.cpp:113] begin():  YG.SPI: _spi_num=3(VSPI), sck=14, so=12, si=13, ss=15
12:07:40.972 -> [   957][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x40181738
12:07:40.972 -> [   959][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 4 successfully set to type GPIO (1) with bus 0x5
12:07:40.972 -> [   960][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x40181738
12:07:40.972 -> [   962][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 33 successfully set to type GPIO (1) with bus 0x22
12:07:40.972 -> This is esp32 chip with 2 CPU core(s), Model=1, WiFi/BTBLE. 
12:07:40.972 -> silicon revision v3.0, 4MB external flash
12:07:40.972 -> Minimum free heap size: 4356708 bytes
12:07:40.972 -> ESP32 Chip model = ESP32-D0WD-V3 Rev 300
12:07:40.972 -> This chip has 2 cores
12:07:40.972 -> Chip ID: 8949660
12:07:40.972 -> [   965][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x40181738
12:07:41.008 -> [   967][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 13 successfully set to type INIT (0) with bus 0x0
12:07:41.008 -> [   969][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 13 successfully set to type INIT (0) with bus 0x0
12:07:41.008 -> [   970][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 13 successfully set to type GPIO (1) with bus 0xe
12:07:41.008 -> [   971][V][esp32-hal-periman.c:235] perimanSetBusDeinit(): Deinit function for type GPIO (1) successfully set to 0x40181738
12:07:41.008 -> [   973][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 14 successfully set to type INIT (0) with bus 0x0
12:07:41.008 -> [   974][I][esp32-hal-spi.c:217] spiDetachBus(): YG.SPI: spiDetachBus(): 0: sck=-1, so=-1, si=-1, ss=-1
12:07:41.008 -> [   975][I][esp32-hal-spi.c:218] spiDetachBus(): YG.SPI: spiDetachBus(): 1: sck=-1, so=-1, si=-1, ss=-1
12:07:41.008 -> [   977][I][esp32-hal-spi.c:219] spiDetachBus(): YG.SPI: spiDetachBus(): 2: sck=-1, so=-1, si=-1, ss=-1
12:07:41.008 -> [   978][I][esp32-hal-spi.c:220] spiDetachBus(): YG.SPI: spiDetachBus(): 3: sck=-1, so=12, si=-1, ss=-1
12:07:41.008 -> [   979][D][esp32-hal-spi.c:221] spiDetachBus(): Stopping SPI bus[3]/4: sck=-1, so=12, si=-1, ss=-1,  clock=10227713.
12:07:41.008 -> [   981][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 12 successfully set to type INIT (0) with bus 0x0
12:07:41.008 -> [   982][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 14 successfully set to type INIT (0) with bus 0x0
12:07:41.008 -> [   983][V][esp32-hal-periman.c:160] perimanSetPinBus(): Pin 14 successfully set to type GPIO (1) with bus 0xf
12:07:41.008 -> [  1001][V][STA.cpp:206] _onStaEvent(): STA Connected: SSID: .hh, BSSID: 08:1f:71:25:c6:8a, Channel: 11, Auth: WPA2_PSK
12:07:41.043 -> [  1002][V][NetworkEvents.cpp:117] _checkForEvent(): Network Event: 112 - STA_CONNECTED
12:07:41.043 -> [  1004][V][STA.cpp:110] _onStaArduinoEvent(): Arduino STA Event: 112 - STA_CONNECTED
12:07:41.191 -> Setup QRCode Reader
12:07:41.224 -> Begin on Core 1
12:07:41.224 ->  SSID: '.hh' : WiFi connecting0(0)[  2018][V][NetworkInterface.cpp:78] _onIpEvent(): sta Got New IP: 192.168.137.69 MASK: 255.255.255.0 GW: 192.168.137.1
12:07:42.070 -> [  2020][V][NetworkEvents.cpp:117] _checkForEvent(): Network Event: 115 - STA_GOT_IP
12:07:42.070 -> [  2021][V][STA.cpp:110] _onStaArduinoEvent(): Arduino STA Event: 115 - STA_GOT_IP
12:07:42.070 -> [  2022][V][STA.cpp:171] _onStaArduinoEvent(): STA IP: 192.168.137.69, MASK: 255.255.255.0, GW: 192.168.137.1
12:07:42.236 -> (3)
12:07:42.805 -> WiFi connected
12:07:42.805 -> [  2783][I][app_httpd.cpp:822] startCameraServer(): Starting web server on port: '80'
12:07:42.805 -> [  2787][I][app_httpd.cpp:839] startCameraServer(): Starting stream server on port: '81'
12:07:42.805 -> ESP32 Chip model = ESP32-D0WD-V3 Rev 300
12:07:42.805 -> This chip has 2 cores
12:07:42.805 -> Chip ID: 8949660
12:07:42.805 -> Flash Size: 4194304 bytes
12:07:42.805 -> PSRAM Size: 4194304 bytes
12:07:42.805 -> Free Memory: 100824 bytes
12:07:42.805 -> Total heap: 309948 bytes
12:07:42.805 -> Free PSRAM: 4027884 bytes
12:07:42.805 -> Camera Ready! Use 'http://192.168.137.69' to connect
12:07:42.805 -> =========== After Setup Start ============
12:07:42.805 -> INTERNAL Memory Info:
12:07:42.805 -> ------------------------------------------
12:07:42.805 ->   Total Size        :   309948 B ( 302.7 KB)
12:07:42.805 ->   Free Bytes        :   100824 B (  98.5 KB)
12:07:42.805 ->   Allocated Bytes   :   197508 B ( 192.9 KB)
12:07:42.805 ->   Minimum Free Bytes:   100824 B (  98.5 KB)
12:07:42.805 ->   Largest Free Block:    61428 B (  60.0 KB)
12:07:42.805 -> ------------------------------------------
12:07:42.858 -> SPIRAM Memory Info:
12:07:42.858 -> ------------------------------------------
12:07:42.858 ->   Total Size        :  4194304 B (4096.0 KB)
12:07:42.858 ->   Free Bytes        :  4027884 B (3933.5 KB)
12:07:42.858 ->   Allocated Bytes   :   164000 B ( 160.2 KB)
12:07:42.858 ->   Minimum Free Bytes:  4027884 B (3933.5 KB)
12:07:42.858 ->   Largest Free Block:  3997684 B (3904.0 KB)
12:07:42.858 -> ------------------------------------------
12:07:42.858 -> GPIO Info:
12:07:42.858 -> ------------------------------------------
12:07:42.858 ->   GPIO : BUS_TYPE[bus/unit][chan]
12:07:42.858 ->   --------------------------------------  
12:07:42.858 ->      1 : UART_TX[0]
12:07:42.858 ->      2 : GPIO
12:07:42.858 ->      3 : UART_RX[0]
12:07:42.858 ->      4 : GPIO
12:07:42.858 ->     13 : GPIO
12:07:42.858 ->     14 : GPIO
12:07:42.858 ->     15 : GPIO
12:07:42.858 ->     33 : GPIO
12:07:42.858 -> ============ After Setup End =============
在此先感謝提出任何相關意見的朋友

jackyang
Posts: 4
Joined: Fri Jul 18, 2025 5:15 am

Re: [ESP32-CAM] SPI Bus issue

Postby jackyang » Mon Jul 21, 2025 5:18 am

FYR

ESP32 CAM 連接 SPI 示波器 圖
Image

Android Uno 連接 SPI 示波器 圖
Image

Who is online

Users browsing this forum: No registered users and 0 guests