ESP32 – Hello world reinvented

In previous post I have described Hello World example coming from ESP_IDF environment. Now let’s build Hello World from the scratch.

Template application

First of all let’s grab template application. It is available on GitHub under espressif project.

cd ~/esp
git clone --recursive https://github.com/espressif/esp-idf-template.git

Rename esp-idf-template directory to hello_world2.

mv esp-idf-template hello_world2
cd hello_world2

Edit Makefile and change value of PROJECT_NAME variable to hello_world2.

Main files of the projects are located in main directory, additional components will be located in components by default. So let’s got to main and remove source file main.c. This file contains example how to connect to wireless network and will be interesting in future. Create the easiest existing example hello_world2.c as below. Name of the file does not matter but one of source files must contain void app_main(void) function:

#include <stdio.h>

void app_main(void)
{
  printf("Hello world\n");
}

Remember to run make menuconfig to set serial flasher connection and then flash and monitor. At the end of the results you will see something like:

clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0008,len:8
load:0x3fff0010,len:3424
load:0x40078000,len:10388
ho 0 tail 12 room 4
load:0x40080000,len:252
0x40080000: _iram_start at ??:?
entry 0x40080034
0x40080034: _iram_start at ??:?
Hello world

It works. Now as general rule tasks should be created in form of endless loop and this peace of code hasn’t got loop. But task can delete themselves when finished. But where this task from app_main is deleted? When you quickly look into cpu_start.c file from esp-idf/components/esp32 folder you will see that right after app_main ends the task is deleted:

static void main_task(void* args)
{
    // Now that the application is about to start, disable boot watchdogs
    REG_CLR_BIT(TIMG_WDTCONFIG0_REG(0), TIMG_WDT_FLASHBOOT_MOD_EN_S);
    REG_CLR_BIT(RTC_CNTL_WDTCONFIG0_REG, RTC_CNTL_WDT_FLASHBOOT_MOD_EN);
    //Enable allocation in region where the startup stacks were located.
    heap_alloc_enable_nonos_stack_tag();
    app_main();
    vTaskDelete(NULL);
}

/Rook