ESP-32 S3 Cannot initialize multiple spi devices (host_id not initialized)

shawny1234
Posts: 4
Joined: Tue Nov 14, 2023 5:21 pm

ESP-32 S3 Cannot initialize multiple spi devices (host_id not initialized)

Postby shawny1234 » Tue Nov 14, 2023 5:39 pm

Hi,
When i'm trying to initialize multiple devices, i get the error:
spi_master: spi_master_init_driver(200): host_id not initialized
ESP_ERROR_CHECK failed: esp_err_t 0x103 (ESP_ERR_INVALID_STATE) at 0x4037d06c
file: "src/board/board_spi.cpp" line 35
func: void NFC_SPI_Init()
expression: ret


Unsure what's wrong in my code, i tried modifying the host values but i still get an error or a black screen / rfid reader not working,
any help might be appreciated:



lgfx setup:
  1. class LGFX : public lgfx::LGFX_Device
  2. {
  3.     lgfx::Panel_ILI9488  _panel_instance;
  4.     lgfx::Bus_SPI       _bus_instance;
  5.     lgfx::Light_PWM     _light_instance;
  6.     lgfx::Touch_GT911   _touch_instance;
  7.   public:
  8.     LGFX(void)
  9.     {
  10.       {
  11.         auto cfg = _bus_instance.config();
  12.         cfg.spi_host         = SPI2_HOST;
  13.         cfg.spi_mode         = 0;
  14.         cfg.freq_write       = 40000000;
  15.         cfg.freq_read        = 16000000;
  16.         cfg.spi_3wire        = true;
  17.         cfg.use_lock         = true;
  18.         cfg.dma_channel      = SPI2_HOST; /*SPI_DMA_CH_AUTO;*/
  19.         cfg.pin_sclk         = 18;
  20.         cfg.pin_mosi         = 13;
  21.         cfg.pin_miso         = 19;
  22.         cfg.pin_dc           = 16;
  23.  
  24.         _bus_instance.config(cfg);
  25.         _panel_instance.setBus(&_bus_instance);
  26.       }
  27.  
  28.       {
  29.         auto cfg = _panel_instance.config();
  30.  
  31.         cfg.pin_cs           =    15;
  32.         cfg.pin_rst          =    -1;
  33.         cfg.pin_busy         =    -1;
  34.  
  35.         cfg.panel_width      =   320;
  36.         cfg.panel_height     =   480;
  37.         cfg.offset_x         =     0;
  38.         cfg.offset_y         =     0;
  39.         cfg.offset_rotation  =     0;
  40.         cfg.dummy_read_pixel =     8;
  41.         cfg.dummy_read_bits  =     1;
  42.         cfg.readable         =  true;
  43.         cfg.invert           = false;
  44.         cfg.rgb_order        = false;
  45.         cfg.dlen_16bit       = false;
  46.         cfg.bus_shared       = false;
  47.  
  48.         _panel_instance.config(cfg);
  49.       }
  50.  
  51.       {
  52.         auto cfg = _light_instance.config();
  53.  
  54.         cfg.pin_bl = 21;
  55.         cfg.invert = false;
  56.         cfg.freq   = 44100;
  57.         cfg.pwm_channel = 7;
  58.  
  59.         _light_instance.config(cfg);
  60.         _panel_instance.setLight(&_light_instance);
  61.       }
  62.  
  63.       {
  64.         auto cfg = _touch_instance.config();
  65.         cfg.pin_scl  = GPIO_NUM_35;
  66.         cfg.pin_sda  = GPIO_NUM_36;
  67.         cfg.pin_int  = GPIO_NUM_37;
  68.         cfg.i2c_addr = 0x5D;
  69.         cfg.i2c_port = I2C_NUM_0;
  70.         cfg.freq     = 400000;
  71.         cfg.x_min    =  14;
  72.         cfg.x_max    = 310;
  73.         cfg.y_min    =   5;
  74.         cfg.y_max    = 448;
  75.         cfg.offset_rotation = 0;
  76.         cfg.bus_shared = false;
  77.  
  78.         _touch_instance.config(cfg);
  79.         _panel_instance.setTouch(&_touch_instance);
  80.       }
  81.  
  82.       setPanel(&_panel_instance);
  83.     }
  84. };
main: The gui init lgvl with the lgfx config
  1. void setup() {
  2.   Serial.begin(115200);
  3.  
  4.   esp_err_t ret = nvs_flash_init();
  5.   if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  6.     ESP_ERROR_CHECK(nvs_flash_erase());
  7.     ret = nvs_flash_init();
  8.   }
  9.  
  10.   ESP_ERROR_CHECK(ret);
  11. xTaskCreatePinnedToCore(gui_task, "gui", 4091*2, NULL, 1, NULL, 1);
  12.   vTaskDelay(200/ portTICK_PERIOD_MS);
  13.   NFC_Init();
  14. }
  15.  
  16.  
  17.  
  18. void loop()
  19. {
  20.   MFRC522_ReadCardSerialNo(card);
  21.  
  22.   if (card[0] != 0 || card[1] != 0 || card[2] != 0 || card[3] != 0) {
  23.     Serial.println(card[0]);
  24.     Serial.println(card[1]);
  25.     Serial.println(card[2]);
  26.     Serial.println(card[3]);
  27.   }
  28.   vTaskDelay(1000/ portTICK_PERIOD_MS);
  29.   //lv_timer_handler();
  30.  // vTaskDelay(pdMS_TO_TICKS(TASK_SLEEP_PERIOD_MS));
  31.  
  32.   //unsigned long currentMillis = millis();
  33.  
  34.  // unsigned long elapsedMillis = currentMillis - StartOfInterval;
  35. }
  36.  
NFC:
  1. void NFC_Init(void) {
  2.     NFC_GPIO_Init();
  3.     NFC_SPI_Init();
  4.     MFRC522_Init();
  5.     CreateNfcCooldownTimer();
  6. }
  7. void NFC_GPIO_Init(void) {
  8.     gpio_pad_select_gpio(NFC_RST_GPIO_PIN);
  9.     gpio_set_direction(NFC_RST_GPIO_PIN, GPIO_MODE_OUTPUT);
  10.  
  11.     NFC_GPIO_Write(NFC_RST_HIGH);
  12. }
  13.  
  14. void NFC_SPI_Init(void) {
  15.    
  16.     esp_err_t ret;
  17.  
  18.     /*spi_bus_config_t spiBusConfig;
  19.  
  20.     spiBusConfig.miso_io_num = NFC_SPI_MISO_PIN;
  21.     spiBusConfig.mosi_io_num = NFC_SPI_MOSI_PIN;
  22.     spiBusConfig.sclk_io_num = NFC_SPI_SCLK_PIN;
  23.     spiBusConfig.quadwp_io_num = -1,
  24.     spiBusConfig.quadhd_io_num = -1,
  25.     spiBusConfig.max_transfer_sz = 64*8;*/
  26.    
  27.    
  28.  
  29.     spi_device_interface_config_t spiDeviceConfig;
  30.  
  31.     spiDeviceConfig.clock_speed_hz = SPI_MASTER_FREQ_10M;
  32.     spiDeviceConfig.mode = 0;
  33.     spiDeviceConfig.spics_io_num = NFC_SPI_CS_PIN;
  34.     spiDeviceConfig.queue_size = 7;
  35.    
  36.  
  37.   //  ret = spi_bus_initialize(SPI2_HOST, &spiBusConfig, SPI3_HOST);
  38.   //  ESP_ERROR_CHECK(ret);
  39.     ret = spi_bus_add_device(SPI3_HOST, &spiDeviceConfig, &s_spiHandle);
  40.     ESP_ERROR_CHECK(ret);
  41.  
  42.     gpio_pad_select_gpio(NFC_SPI_CS_PIN);
  43.     gpio_set_direction(NFC_SPI_CS_PIN, GPIO_MODE_OUTPUT);
  44. }
  45.  

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-32 S3 Cannot initialize multiple spi devices (host_id not initialized)

Postby ESP_Sprite » Wed Nov 15, 2023 6:21 am

Why did you comment out spi_bus_initialize?

shawny1234
Posts: 4
Joined: Tue Nov 14, 2023 5:21 pm

Re: ESP-32 S3 Cannot initialize multiple spi devices (host_id not initialized)

Postby shawny1234 » Wed Nov 15, 2023 10:52 am

Because it's initialized in the lgfx config and i'm unsure if i should initialize it twice, should I ?

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-32 S3 Cannot initialize multiple spi devices (host_id not initialized)

Postby ESP_Sprite » Thu Nov 16, 2023 2:33 am

If I read your code correctly, it's not: you're using SPI2_HOST there, not SPI3_HOST. You either need to move your devices to the same SPI host (if your devices share SCK/MISO/MOSI lines) or initialize each SPI host separately (if they do not).

shawny1234
Posts: 4
Joined: Tue Nov 14, 2023 5:21 pm

Re: ESP-32 S3 Cannot initialize multiple spi devices (host_id not initialized)

Postby shawny1234 » Thu Nov 16, 2023 9:27 am

I"m unsure about a second initialization, when i uncomment the spi_bus initialize with this code:

Code: Select all

void NFC_SPI_Init(void) {
   
    esp_err_t ret;

    spi_bus_config_t spiBusConfig;

    spiBusConfig.miso_io_num = NFC_SPI_MISO_PIN;
    spiBusConfig.mosi_io_num = NFC_SPI_MOSI_PIN;
    spiBusConfig.sclk_io_num = NFC_SPI_SCLK_PIN;
    spiBusConfig.quadwp_io_num = -1,
    spiBusConfig.quadhd_io_num = -1,
    spiBusConfig.max_transfer_sz = 64*8;
    
    

    spi_device_interface_config_t spiDeviceConfig;

    spiDeviceConfig.clock_speed_hz = SPI_MASTER_FREQ_10M;
    spiDeviceConfig.mode = 0;
    spiDeviceConfig.spics_io_num = NFC_SPI_CS_PIN;
    spiDeviceConfig.queue_size = 7;
   

    ret = spi_bus_initialize(SPI3_HOST, &spiBusConfig, SPI_DMA_CH_AUTO); 
    ESP_ERROR_CHECK(ret);
    ret = spi_bus_add_device(SPI3_HOST, &spiDeviceConfig, &s_spiHandle);
    ESP_ERROR_CHECK(ret);

    gpio_pad_select_gpio(NFC_SPI_CS_PIN);
    gpio_set_direction(NFC_SPI_CS_PIN, GPIO_MODE_OUTPUT);
}

class LGFX : public lgfx::LGFX_Device
{
    lgfx::Panel_ILI9488  _panel_instance;
    lgfx::Bus_SPI       _bus_instance;
    lgfx::Light_PWM     _light_instance;
    lgfx::Touch_GT911   _touch_instance;
  public:
    LGFX(void)
    {
      {
        auto cfg = _bus_instance.config();
        cfg.spi_host         = SPI3_HOST; 
        cfg.spi_mode         = 0;
        cfg.freq_write       = 40000000;
        cfg.freq_read        = 16000000;
        cfg.spi_3wire        = true;
        cfg.use_lock         = true;
        cfg.dma_channel      = SPI_DMA_CH_AUTO;
        cfg.pin_sclk         = 18;
        cfg.pin_mosi         = 13;
        cfg.pin_miso         = 19;
        cfg.pin_dc           = 16;

        _bus_instance.config(cfg);
        _panel_instance.setBus(&_bus_instance);
      }
I get the error spi: spi_bus_initialize(756): SPI bus already initialized.
ESP_ERROR_CHECK failed: esp_err_t 0x103 (ESP_ERR_INVALID_STATE) at 0x4037d06c
file: "src/board/board_spi.cpp" line 33
func: void NFC_SPI_Init()
expression: ret

abort() was called at PC 0x4037d06f on core 1

When i comment it, there are no error message but nothing is displayed and when i comment the NFC_Init() func the display is working fine. I'm wrong somewhere with the bus usage but don't know where

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-32 S3 Cannot initialize multiple spi devices (host_id not initialized)

Postby ESP_Sprite » Fri Nov 17, 2023 1:22 am

I think you need to track down where SPI2 is initialized, then. It doesn't appear to happen anywhere in the code you posted. EDIT: SPI2 instead of SPI3.

shawny1234
Posts: 4
Joined: Tue Nov 14, 2023 5:21 pm

Re: ESP-32 S3 Cannot initialize multiple spi devices (host_id not initialized)

Postby shawny1234 » Fri Nov 17, 2023 7:21 am

You mean by declaring a spiclass and storing it somewhere ? I effectively didn't try that but that wouldn't be compatible with the way of using the lgfx lib right ?

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-32 S3 Cannot initialize multiple spi devices (host_id not initialized)

Postby ESP_Sprite » Sun Nov 19, 2023 7:02 am

No, I mean by going through all your code and see where it initializes (or even touches) the SPI2 host. Something seems to be doing that, making your init fail as the SPI2 host already is inited. EDIT: I meant SPI2, not SPI3.

Who is online

Users browsing this forum: No registered users and 113 guests