Page 1 of 1

esp_wifi_init() fails on ESP32-S2 WROVER; works on WROOM? (SOLVED)

Posted: Wed Feb 17, 2021 3:19 am
by adamwilt
I have WI-FI STA and AP code working properly on an ESP32-S2 WROOM, but when I run it on a WROVER with the external RAM enabled, esp_wifi_init() fails with ESP_ERR_NO_MEM.

The startup code for AP mode:

Code: Select all

    ESP_ERROR_CHECK(esp_event_loop_create_default());
    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
                                                        ESP_EVENT_ANY_ID,
                                                        &event_handler,
                                                        NULL,
                                                        NULL));

    if (!_initialized) {
        ESP_LOGI(TAG, "esp_netif_init()");
        ESP_ERROR_CHECK(esp_netif_init());
    } // if !_initialized
       
    _espNetif = esp_netif_create_default_wifi_ap();

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg)); // THIS IS THE LINE WHERE IT FAILS!
    
    wifi_country_t country = { "US", 1, 11, 0, WIFI_COUNTRY_POLICY_AUTO };
    ESP_ERROR_CHECK(esp_wifi_set_country(&country));
    
    // etc: set mode, set config, start, start mdns, start http server...
Failure mode on a WROVER (an ESP32-S2-Saola-R1) with external RAM enabled in menuconfig:
I (20035) wifi_init: rx ba win: 6
I (20035) wifi_init: tcpip mbox: 32
I (20035) wifi_init: udp mbox: 6
I (20035) wifi_init: tcp mbox: 6
I (20035) wifi_init: tcp tx win: 5744
I (20045) wifi_init: tcp rx win: 5744
I (20045) wifi_init: tcp mss: 1440
I (20055) wifi_init: WiFi IRAM OP enabled
I (20055) wifi_init: WiFi RX IRAM OP enabled
ESP_ERROR_CHECK failed: esp_err_t 0x101 (ESP_ERR_NO_MEM) at 0x4002ba70
0x4002ba70: _esp_error_check_failed at /Users/adamwilt/esp/esp-idf/components/esp_common/src/esp_err.c:41

file: "../components/dcr_wifi/dcr_wifi.c" line 250
func: wifi_ap_start
expression: esp_wifi_init(&cfg)

abort() was called at PC 0x4002ba73 on core 0
If I run on a WROOM (ESP32-S2-Saola-M1), or I disable "Support for external, SPI-connected RAM" in menuconfig on the WROVER, Wi-Fi initializes and runs:
I (19664) wifi_init: rx ba win: 6
I (19674) wifi_init: tcpip mbox: 32
I (19674) wifi_init: udp mbox: 6
I (19674) wifi_init: tcp mbox: 6
I (19684) wifi_init: tcp tx win: 5744
I (19684) wifi_init: tcp rx win: 5744
I (19694) wifi_init: tcp mss: 1440
I (19694) wifi_init: WiFi IRAM OP enabled
I (19704) wifi_init: WiFi RX IRAM OP enabled
I (19704) wifi:set country: cc=US schan=1 nchan=11 policy=0

I (19714) phy_init: phy_version 1300,2887b9c,Dec 16 2020
I (19844) wifi:mode : softAP (7c:df:a1:05:d6:5b)
I (19844) wifi:Total power save buffer number: 8
I (19844) wifi:Init max length of beacon: 752/752
I (19844) wifi:Init max length of beacon: 752/752
I (19854) dcr_wifi: eventHandler ignoring event
I (19864) dcr_wifi: wifi_init_softap finished. SSID:DCR_II password: channel:10
I (19864) dcr_wifi: Starting mDNS service...
What do I need to do differently to init Wi-Fi on a WROVER with external RAM?

Re: esp_wifi_init() fails on ESP32-S2 WROVER; works on WROOM?

Posted: Wed Feb 17, 2021 1:38 pm
by chegewara
The logs suggesting you are short on memory. It is working w/o PSRAM and fails with it because there is some additional cache in heap when you are using PSRAM.

Re: esp_wifi_init() fails on ESP32-S2 WROVER; works on WROOM?

Posted: Wed Feb 17, 2021 6:53 pm
by adamwilt
Yes, I'm short on RAM (the ESP_ERR_NO_MEM is a bit of a clue, grin). The question is how, or why?

There is nothing in my code's memory allocation that's different between using external RAM and not. The only thing different between these two cases is that I enable or disable "Support for external, SPI-connected RAM" in menuconfig, and rebuild. Without external RAM, the free heap reported by esp_get_minimum_free_heap_size() at startup is 119,896 bytes, and with external RAM it's 2,178,483 bytes, about 10x more.

So why, when I have 10x more RAM and my code is unchanged, am I seeing ESP_ERR_NO_MEM? What's changing inside the system's memory allocation that's not only preventing esp_wifi_init() from taking advantage of that 10x more memory, but preventing it from using memory it was able to use before, and what (if anything) can I do to fix it? Where is this "additional cache" coming from?

Re: esp_wifi_init() fails on ESP32-S2 WROVER; works on WROOM?

Posted: Wed Feb 17, 2021 7:59 pm
by chegewara
You can try this option in menuconfig for PSRAM:

Code: Select all

Make RAM allocatable using malloc() as well
Then you will have this option, you try with, but dont have to:

Code: Select all

Try to allocate memories of WiFi and LWIP in SPIRAM firstly. If failed, allocate internal memory

Re: esp_wifi_init() fails on ESP32-S2 WROVER; works on WROOM?

Posted: Wed Feb 17, 2021 9:40 pm
by adamwilt
Thank you! I already had "Make RAM allocatable using malloc() as well", but not

Code: Select all

Try to allocate memories of WiFi and LWIP in SPIRAM firstly. If failed, allocate internal memory
That did the trick! I'm up and running again and all is well with the world. :-)
Many thanks!