Crash after SPI slave initialization (PSRAM enabled)

TheOrangeCap
Posts: 2
Joined: Mon Apr 14, 2025 4:48 pm

Crash after SPI slave initialization (PSRAM enabled)

Postby TheOrangeCap » Mon Apr 14, 2025 5:17 pm

Hello!
I am new to programming with the esp_idf framework and have a problem that has been bugging me for a while now.

Context:
The board is ESP32-CAM. The esp is used as an SPI peripheral that captures an image on command and then transfers it to the SPI master. Everything seemed to go well in the beginning. I made some bigger changes, and suddenly the init stage did not work anymore.

It seems that after initializing the spi_slave, the esp runs for a few cycles and then crashes:

Code: Select all

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.

Core  0 register dump:
PC       : 0x400d70e0  PS       : 0x00060930  A0       : 0x8008825c  A1       : 0x3ffb7740
--- 0x400d70e0: esp_vfs_write at /anonymized/esp/v5.4.1/esp-idf/components/vfs/vfs.c:945

A2       : 0x3ffafa28  A3       : 0x00000009  A4       : 0x3ffaed68  A5       : 0x0000002a
A6       : 0x3ffb7ba0  A7       : 0x3ffaecb0  A8       : 0x3f8008cc  A9       : 0xffffffff
A10      : 0x00000000  A11      : 0x00000000  A12      : 0x00060520  A13      : 0x00060523
A14      : 0x40086e38  A15      : 0x3ffaecb0  SAR      : 0x00000004  EXCCAUSE: 0x0000001c
--- 0x40086e38: spi_intr at /anonymized/esp/v5.4.1/esp-idf/components/esp_driver_spi/src/gpspi/spi_slave.c:587

EXCVADDR: 0xffffffff  LBEG     : 0x400884f1  LEND     : 0x40088501  LCOUNT   : 0xfffffffc
--- 0x400884f1: strlen at /builds/idf/crosstool-NG/.build/xtensa-esp-elf/src/newlib/newlib/libc/machine/xtensa/strlen.S:84
--- 0x40088501: strlen at /builds/idf/crosstool-NG/.build/xtensa-esp-elf/src/newlib/newlib/libc/machine/xtensa/strlen.S:96



Backtrace: 0x400d70dd:0x3ffb7740 0x40088259:0x3ffb7760 0x400876d9:0x3ffb7780 0x4008772e:0x3ffb77a0 0x40087c49:0x3ffb77c0 0x400f09df:0x3ffb77f0 0x400f03e6:0x3ffb7810 0x400ecedd:0x3ffb7b30 0x400f80c5:0x3ffb7b60 0x40090311:0x3ffb7b90 0x400daa85:0x3ffb7be0 0x400da9d9:0x3ffb7c50 0x400f78ac:0x3ffb7c70 0x4008a93d:0x3ffb7ca0
--- 0x400d70dd: esp_vfs_write at /anonymized/esp/v5.4.1/esp-idf/components/vfs/vfs.c:942
--- 0x40088259: __swrite at /builds/idf/crosstool-NG/.build/xtensa-esp-elf/src/newlib/newlib/libc/stdio/stdio.c:94
--- 0x400876d9: __sflush_r at /builds/idf/crosstool-NG/.build/xtensa-esp-elf/src/newlib/newlib/libc/stdio/fflush.c:224
--- 0x4008772e: _fflush_r at /builds/idf/crosstool-NG/.build/xtensa-esp-elf/src/newlib/newlib/libc/stdio/fflush.c:278
--- 0x40087c49: __sfvwrite_r at /builds/idf/crosstool-NG/.build/xtensa-esp-elf/src/newlib/newlib/libc/stdio/fvwrite.c:251
--- 0x400f09df: __sprint_r at /builds/idf/crosstool-NG/.build/xtensa-esp-elf/src/newlib/newlib/libc/stdio/vfprintf.c:429
---  (inlined by) __sprint_r at /builds/idf/crosstool-NG/.build/xtensa-esp-elf/src/newlib/newlib/libc/stdio/vfprintf.c:399
--- 0x400f03e6: _vfprintf_r at /builds/idf/crosstool-NG/.build/xtensa-esp-elf/src/newlib/newlib/libc/stdio/vfprintf.c:1774 (discriminator 1)
--- 0x400ecedd: vprintf at /builds/idf/crosstool-NG/.build/xtensa-esp-elf/src/newlib/newlib/libc/stdio/vprintf.c:34
--- 0x400f80c5: esp_log_writev at /anonymized/esp/v5.4.1/esp-idf/components/log/src/os/log_write.c:34
--- 0x40090311: esp_log_write at /anonymized/esp/v5.4.1/esp-idf/components/log/src/os/log_write.c:44
--- 0x400daa85: spiSlaveHandler_init at /anonymized/main/SpiSlaveHandler.c:38 (discriminator 1)
--- 0x400da9d9: app_main at /anonymized/main/main.c:66
--- 0x400f78ac: main_task at /anonymized/esp/v5.4.1/esp-idf/components/freertos/app_startup.c:208
--- 0x4008a93d: vPortTaskWrapper at /anonymized/esp/v5.4.1/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:139


It seems that the crash is always at a ESP_LOG instruction. I tried to mitigate that by increasing the main task stack size. Didn't work. And I want to emphasize that this only happens if the spi init function executes.

This is my main function:

Code: Select all

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "esp_camera.h"

#include "SpiSlaveHandler.h"
#include "cameraHandler.h"

#define LED_BUILTIN GPIO_NUM_25

#define CAM_PIN_PWDN -1 
#define CAM_PIN_RESET -1
#define CAM_PIN_XCLK 15
#define CAM_PIN_SIOD 22
#define CAM_PIN_SIOC 23

#define CAM_PIN_D7 39
#define CAM_PIN_D6 34
#define CAM_PIN_D5 33
#define CAM_PIN_D4 27
#define CAM_PIN_D3 12
#define CAM_PIN_D2 35
#define CAM_PIN_D1 14
#define CAM_PIN_D0 2
#define CAM_PIN_VSYNC 18
#define CAM_PIN_HREF 36
#define CAM_PIN_PCLK 26

static camera_config_t cam_config = {
    .pin_pwdn = CAM_PIN_PWDN,
    .pin_reset = CAM_PIN_RESET,
    .pin_xclk = CAM_PIN_XCLK,
    .pin_sccb_sda = CAM_PIN_SIOD,
    .pin_sccb_scl = CAM_PIN_SIOC,

    .pin_d7 = CAM_PIN_D7,
    .pin_d6 = CAM_PIN_D6,
    .pin_d5 = CAM_PIN_D5,
    .pin_d4 = CAM_PIN_D4,
    .pin_d3 = CAM_PIN_D3,
    .pin_d2 = CAM_PIN_D2,
    .pin_d1 = CAM_PIN_D1,
    .pin_d0 = CAM_PIN_D0,
    .pin_vsync = CAM_PIN_VSYNC,
    .pin_href = CAM_PIN_HREF,
    .pin_pclk = CAM_PIN_PCLK,

    .xclk_freq_hz = 20000000,
    .ledc_timer = LEDC_TIMER_0,
    .ledc_channel = LEDC_CHANNEL_0,

    .pixel_format = PIXFORMAT_JPEG,
    .frame_size = FRAMESIZE_UXGA,

    .jpeg_quality = 12,
    .fb_count = 1,
    .fb_location = CAMERA_FB_IN_PSRAM,
    .grab_mode = CAMERA_GRAB_WHEN_EMPTY,
};


void app_main(void)
{
    cameraHandler_init(&cam_config);
    spiSlaveHandler_init(4,16,17,19);// (19,17,32,25);

    gpio_set_direction(LED_BUILTIN, GPIO_MODE_OUTPUT);
    gpio_set_level(LED_BUILTIN, 0);
    while(1)
    {
        ESP_LOGI("TEST","THIS IS A TEST");
        gpio_set_level(LED_BUILTIN, 0);
        vTaskDelay(pdMS_TO_TICKS(500));
        gpio_set_level(LED_BUILTIN, 1);
        vTaskDelay(pdMS_TO_TICKS(500));
    } 
        
}





The program crash only happens if spiSlaveHandler_init(...) gets executed:

Code: Select all

static const char* TAG_SPI = "SPI Handler";

esp_err_t spiSlaveHandler_init(uint8_t miso, uint8_t mosi, uint8_t sclk, uint8_t cs)
{
    gpio_set_pull_mode(mosi, GPIO_PULLUP_ONLY);
    gpio_set_pull_mode(sclk, GPIO_PULLUP_ONLY);
    gpio_set_pull_mode(cs, GPIO_PULLUP_ONLY);

    spi_bus_config_t busConfig = {
        .miso_io_num = miso,
        .mosi_io_num = mosi,
        .sclk_io_num = sclk,
        .quadhd_io_num = -1,
        .quadwp_io_num = -1,
        .flags = 0,
    };

    spi_slave_interface_config_t slaveConfig = {
        .mode = 0,
        .spics_io_num = cs,
        .queue_size = 3,
        .flags = 0,
        .post_setup_cb = NULL,
        .post_trans_cb = NULL,
    };

    esp_err_t ret = spi_slave_initialize(SPI2_HOST, &busConfig, &slaveConfig, SPI_DMA_DISABLED);

    if(ret == ESP_OK)
        ESP_LOGI(TAG_SPI, "SPI bus initialized");
    else
        ESP_LOGE(TAG_SPI, "SPI bus failed to initialize");

    return ret;
}
I would appreciate if someone can help me, I am kind of stuck here. Could it be that the board is fried/broken?

EDIT:
I noticed that the SPI initialization leads to undefined behaviour, because I enabled PSRAM. Which SPI controller does the PSRAM use? I thought it would share the controller the flash memory uses.

I already tried VSPI and HSPI.

Sprite
Espressif staff
Espressif staff
Posts: 10650
Joined: Thu Nov 26, 2015 4:08 am

Re: Crash after SPI slave initialization (PSRAM enabled)

Postby Sprite » Tue Apr 15, 2025 12:07 am

Are you sure your SPI pins don't collide with the pins in use by PSRAM?

TheOrangeCap
Posts: 2
Joined: Mon Apr 14, 2025 4:48 pm

Re: Crash after SPI slave initialization (PSRAM enabled)

Postby TheOrangeCap » Tue Apr 15, 2025 8:32 am

Thanks! That was the problem.

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Google [Bot], Qwantbot and 2 guests