Creating your own main() with ESP8266 core

This may be making things more complex than they need to be but I am curious for learning purposes...
I am trying to brainstorm how I would go about making my own main() function for esp8266 related boards I am working with just to test out. I am aware for AVR based boards that the main() function being used that defines setup() and loop() is located here /arduino/hardware/arduino/avr/cores/arduino/main.cpp
and looks like:

int main(void)
{
	init();

	initVariant();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

Seeing that every arduino program follows the same generic setup() and loop() I was intrigued when I saw how different it is when working with the ESP8266.
I was curious how I could edit the setup() and loop() for the ESP8266 main file I believe to be located in this file /.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/core_esp8266_main.cpp
and the portion of the code I interpreted as the "main" in this case was this:

static void loop_wrapper() {
    static bool setup_done = false;
    preloop_update_frequency();
    if(!setup_done) {
        setup();
        setup_done = true;
    }
    loop();
    loop_end();
    if (serialEventRun) {
        serialEventRun();
    }
    esp_schedule();
}

Does anyone have any ideas on how I could potentially modify this or my existing code to allows for the use of main() and not be set to using setup() and loop()?

Interesting question and I don't have the answer.

Your topic has been moved to a more suitable location on the forum.

1 Like

you can't create your own main() with esp8266 even without Arduino. main() is in the Espressif SDK

1 Like

Looks like the first function in the ESP8266 core is "user_init()", also in core_esp8266_main.cpp. It is the function that sets up a 'task' that runs loop_wrapper():

extern "C" void user_init(void) {
    struct rst_info *rtc_info_ptr = system_get_rst_info();
    memcpy((void *) &resetInfo, (void *) rtc_info_ptr, sizeof(resetInfo));

    uart_div_modify(0, UART_CLK_FREQ / (115200));

    init(); // in core_esp8266_wiring.c, inits hw regs and sdk timer

    initVariant();

    experimental::initFlashQuirks(); // Chip specific flash init.

    cont_init(g_pcont);

#if defined(DEBUG_ESP_HWDT) || defined(DEBUG_ESP_HWDT_NOEXTRA4K)
    debug_hwdt_init();
#endif

#if defined(UMM_HEAP_EXTERNAL)
    install_vm_exception_handler();
#endif

#if defined(NON32XFER_HANDLER) || defined(MMU_IRAM_HEAP)
    install_non32xfer_exception_handler();
#endif

#if defined(MMU_IRAM_HEAP)
    umm_init_iram();
#endif
    preinit(); // Prior to C++ Dynamic Init (not related to above init() ). Meant to be user redefinable.
    __disableWiFiAtBootTime(); // default weak function disables WiFi

    ets_task(loop_task,
        LOOP_TASK_PRIORITY, s_loop_queue,
        LOOP_QUEUE_SIZE);

    system_init_done_cb(&init_done);
}

It isn't called from anywhere in the Arduino core but is referenced in a bunch of pre-compiled libraries:

Binary file tools/sdk/lib/NONOSDK22x_191105/libwps.a matches
Binary file tools/sdk/lib/NONOSDK22x_191105/libsmartconfig.a matches
Binary file tools/sdk/lib/NONOSDK22x_191105/libmain.a matches
Binary file tools/sdk/lib/NONOSDK22x_191024/libwps.a matches
Binary file tools/sdk/lib/NONOSDK22x_191024/libsmartconfig.a matches
Binary file tools/sdk/lib/NONOSDK22x_191024/libmain.a matches
Binary file tools/sdk/lib/NONOSDK22x_190313/libwps.a matches
Binary file tools/sdk/lib/NONOSDK22x_190313/libsmartconfig.a matches
Binary file tools/sdk/lib/NONOSDK22x_190313/libmain.a matches
Binary file tools/sdk/lib/NONOSDK221/libwps.a matches
Binary file tools/sdk/lib/NONOSDK221/libsmartconfig.a matches
Binary file tools/sdk/lib/NONOSDK221/libmain.a matches
Binary file tools/sdk/lib/NONOSDK22x_190703/libwps.a matches
Binary file tools/sdk/lib/NONOSDK22x_190703/libsmartconfig.a matches
Binary file tools/sdk/lib/NONOSDK22x_190703/libmain.a matches
Binary file tools/sdk/lib/NONOSDK3V0/libwps.a matches
Binary file tools/sdk/lib/NONOSDK3V0/libsmartconfig.a matches
Binary file tools/sdk/lib/NONOSDK3V0/libmain.a matches
Binary file tools/sdk/lib/NONOSDK22x_191122/libwps.a matches
Binary file tools/sdk/lib/NONOSDK22x_191122/libsmartconfig.a matches
Binary file tools/sdk/lib/NONOSDK22x_191122/libmain.a matches

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.