You can chat about native SDK questions and issues here.

User avatar
By aht2000
#83146 Hello,
I wrote the following code using SDK 3.1 to setup ESP8266 E12 to act an access point. It starts normally, but once I try to connect to it from Windows 10, it crashes. I captured the attached output from the serial port.

I erased the 4MB using esp_tool erase_flash and even writing 1MB blank bin to the start of eack 1MB flash.
just to make sure that I have a clean start.

I write the blank and init (v08) to:
c:/Espressif/utils/ESP8266/esptool.exe -p COM3 write_flash -ff 40m -fm qio -fs 32m 0x3fc000 c:/Espressif/ESP8266_SDK/bin/esp_init_data_default.bin 0x3fd000 c:/Espressif/ESP8266_SDK/bin/blank.bin

I write the programs to:
c:/Espressif/utils/ESP8266/esptool.exe -p COM3 -b 230400 write_flash -ff 40m -fm qio -fs 32m 0x00000 firmware/eagle.flash.bin 0x10000 firmware/eagle.irom0text.bin


Output of RealTerm serial monitor. These are are extracts, not in specific order:
In baudrate 74880
rf cal sector: 1019
freq trace enable 0

In baudrate 9600
Fatal exception 28Fatal exception 28(LoadProhibitedCause):
epc1=0x4000debe, epc2=0x00000000, epc3=0x00000000, excvaddr=0x0000001c, depc=0x0
0000000

Fatal exception 0(IllegalInstructionCause):
epc1=0x402211e0, epc2=0x00000000, epc3=0x00000000, excvaddr=0x0000001c, depc=0x0
0000000

I tried several power supplies with 100uf and 100nf decoupling caps near Vcc and GND.

Following is my code:
Code: Select all/*
   setup ESP8266 as a softAP. Still crashes when trying to connect to the softAP
*/

#include <ets_sys.h>
#include "user_interface.h"
#include <osapi.h>
#include <gpio.h>
#include <mem.h>
#include "driver/uart.h"

#define SPI_FLASH_SIZE_MAP  4      // ESP8266-12E 4MB Flash !!

#define SYSTEM_PARTITION_OTA_SIZE                     0x6A000
#define SYSTEM_PARTITION_OTA_2_ADDR                     0x81000
#define SYSTEM_PARTITION_RF_CAL_ADDR                  0x3fb000
#define SYSTEM_PARTITION_PHY_DATA_ADDR                  0x3fc000
#define SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR            0x3fd000

static const partition_item_t at_partition_table[] = {
    { SYSTEM_PARTITION_BOOTLOADER,                   0x0,                                     0x1000},
    { SYSTEM_PARTITION_OTA_1,                     0x1000,                                  SYSTEM_PARTITION_OTA_SIZE},
    { SYSTEM_PARTITION_OTA_2,                     SYSTEM_PARTITION_OTA_2_ADDR,                   SYSTEM_PARTITION_OTA_SIZE},
    { SYSTEM_PARTITION_RF_CAL,                    SYSTEM_PARTITION_RF_CAL_ADDR,                   0x1000},
    { SYSTEM_PARTITION_PHY_DATA,                   SYSTEM_PARTITION_PHY_DATA_ADDR,                0x1000},
    { SYSTEM_PARTITION_SYSTEM_PARAMETER,             SYSTEM_PARTITION_SYSTEM_PARAMETER_ADDR,          0x3000},
};

/******************************************************************************
 * FunctionName : user_rf_cal_sector_set
 * Description  : SDK just reversed 4 sectors, used for rf init data and paramters.
 *                We add this function to force users to set rf cal sector, since
 *                we don't know which sector is free in user's application.
 *                sector map for last several sectors : ABBBCDDD
 *                A : rf cal
 *                B : at parameters
 *                C : rf init data
 *                D : sdk parameters
 * Parameters   : none
 * Returns      : rf cal sector
*******************************************************************************/
uint32 ICACHE_FLASH_ATTR user_rf_cal_sector_set(void)
{
    enum flash_size_map size_map = system_get_flash_size_map();
    uint32 rf_cal_sec = 0;

    switch (size_map) {
        case FLASH_SIZE_4M_MAP_256_256:
            rf_cal_sec = 128 - 8;
            break;

        case FLASH_SIZE_8M_MAP_512_512:
            rf_cal_sec = 256 - 5;
            break;

        case FLASH_SIZE_16M_MAP_512_512:
        case FLASH_SIZE_16M_MAP_1024_1024:
            rf_cal_sec = 512 - 5;
            break;

        case FLASH_SIZE_32M_MAP_512_512:
        case FLASH_SIZE_32M_MAP_1024_1024:
            rf_cal_sec = 1024 - 5;
            break;

        default:
            rf_cal_sec = 0;
            break;
    }

    return rf_cal_sec;
}

void ICACHE_FLASH_ATTR user_rf_pre_init(void) {
}

void ICACHE_FLASH_ATTR user_pre_init(void) {
    if(!system_partition_table_regist(at_partition_table, sizeof(at_partition_table)/sizeof(at_partition_table[0]),SPI_FLASH_SIZE_MAP)) {
      os_printf("system_partition_table_regist fail\r\n");
      while(1);
   }
}

LOCAL void ICACHE_FLASH_ATTR set_ap_config (os_event_t *e){
   struct softap_config ap;
   os_printf ("\r\nIn OS Task\r\n");
   wifi_softap_get_config(&ap);                  // Get config first.
   os_memset(ap.ssid, 0, 32);
   os_memset(ap.password, 0, 64);
   os_memcpy(ap.ssid, "softAP", 6);
   os_memcpy(ap.password, "", 10);
//   ap.authmode = AUTH_WPA2_PSK;
   ap.authmode = AUTH_OPEN;
   ap.ssid_len = 0;                              // or its actual length
   ap.max_connection = 1;                        // Max no. of stations permissible?
   wifi_softap_set_config (&ap);                 // Update ESP8266 softap config!
}

LOCAL void ICACHE_FLASH_ATTR sdk_init_done_cb (void){
   unsigned char res;
   os_event_t *testQueue;
   res = wifi_set_opmode_current (SOFTAP_MODE);  // Make sure ESP8266 is in SoftAP mode!
   os_printf ("\r\nSet op mode returned: %d\r\n", res);
   testQueue = (os_event_t *)os_malloc(sizeof(os_event_t)*4);
   system_os_task (set_ap_config, USER_TASK_PRIO_1, testQueue, 4);
   system_os_post(USER_TASK_PRIO_1, 0, 0);
//   ap_server_setup (AP_PORT);                     // Continue with server setup, etc
}

void ICACHE_FLASH_ATTR user_init(void) {
   uart_init(BIT_RATE_9600, BIT_RATE_9600);   // uart0_br, uart1_br
   system_init_done_cb(sdk_init_done_cb);
}


Any idea what I could be doing wrong?
User avatar
By Mike Nix
#83305 Hi, Do you get a stack dump when it crashes?
If so, is it unusually large and contain a large block of "feefeffe" ?

I have been having a problem where the SDK appears to randomly allocate over 3K of stack - up to 3600 bytes in some cases) which is basically suicide on a 4K stack. In some cases I've seen over 5500 bytes of stack used!
As far as I can tell its related to the network code, and can happen even if your own code does nothing at all if the wifi has been previously configured.

I've been able to reliably reproduce this by putting the wifi in station mode, with sleep mode MODEM (the default) and connecting to my AP. My code then does nothing else at all, but with debugging on an exception 0 occurs within seconds of "pm open,type:2"

Disabling WiFi stops the code from crashing.

I discovered this using the Arduino IDE and put together some test code in that, but looking into the stack dumps leads me to believe this is actually a bug in the SDK, probably in a network related interrupt handler based on the position of the large unused region and the fact that it then calls the ethernet_input() code in the Arduino library every time.
User avatar
By davydnorris
#83327 You don't use user_rf_cal_sector_set() for the later 3.x SDKs - it's been replaced by the partition table, so that may cause grief.

I think, however, that the problem may be the ap.ssid_len setting - I always set that to the actual length of the AP name.

I also assume that you have an actual AP password and you've just deleted it in your code, otherwise the line with os_memcpy(ap.password, "", 10); is dodgy and will definitely be running over a memory location somewhere.

A few other things:
- you don't need to use a task queue to set the AP - you can do that directly in the init_done callback.
- you may want to use wifi_softap_set_config_current(), as the function you're using will write to flash each time and eventually wear it out.
User avatar
By quackmore
#83334 don't think user_rf_cal_sector_set() could be a problem, I'm using SDK 3.0, I have it in my code together with the partition table and everything works fine

softap_config.ssid_len==0 is not a problem too, in that case SSID is checked till a termination character is found

I don't like os_memcpy(ap.password, "", 10)
but in case you are lucky it will end up with an empty password

another problem could be this one:
when you first initialize the ap_config you use wifi_softap_get_config (without checking the result)
that will get the current ap configuration (isn't it empty?) not the one stored in flash (where nonos_sdk should have a default one)
then you don't initialize the channel, the ssid_hidden and the beacon
not sure what the result will be
furthermore you then save that configuration to flash ...

other than fixing the code
it could be worth it to clear the whole flash and reset the default values