May 11, 2018

Analysis of the ESP32 IDF and makefile

I am quite happy that the ESP32 IDF is driven by a Makefile, but of course I am curious. To start with, what is the "monitor" that serves as a terminal emulator? Make itself has options we can use to get it to tell us more. The "-d" options requests debug information, but that is generally much more than we need.

Look at make monitor

The "-n" option usually seems to do exactly what I want and reveals for "make monitor" that this is what is going on:
python /opt/esp32/esp-idf/tools/idf_monitor.py --baud 115200 --port "/dev/ttyUSB2" --toolchain-prefix "xtensa-esp32-elf-" --make "make" /u1/Projects/Esp32/hello_world/build/hello-world.elf

The monitor is a 620 line python file.
Why not just use picocom as I always do?
The hint is the option to read the ELF file.
Why would a mere terminal emulator need to do that?
Apparently it offers some curious support in terms of
looking up addresses and even launching gdb.
More to learn about.



Look at make flash

The command to actually flash the chip is easy to get and is shown to us as part of the normal build process:

To flash all build output, run 'make flash' or:
python /opt/esp32/esp-idf/components/esptool_py/esptool/esptool.py --chip esp32 --port /dev/ttyUSB2 --baud 115200 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 40m --flash_size detect 0x1000 /u1/Projects/Esp32/hello_world/build/bootloader/bootloader.bin 0x10000 /u1/Projects/Esp32/hello_world/build/hello-world.bin 0x8000 /u1/Projects/Esp32/hello_world/build/partitions_singleapp.bin

Look at make menuconfig

In the case of "make menuconfig", just the "-n" option does not tell the story, so we resort to "-d" and wade through countless screens of output. Some interesting tidbits are as follows:
Reading makefile '/opt/esp32/esp-idf/make/project.mk'
Reading makefile '/opt/esp32/esp-idf/make/common.mk'
Reading makefile '/opt/esp32/esp-idf/make/project_config.mk'
File 'menuconfig' does not exist.
Considering target file '/opt/esp32/esp-idf/tools/kconfig/mconf'.
MENUCONFIG
So, "make menuconfig" is sneaky and more digging will be required. One way, give the above printout of "MENUCONFIG" is this:
cd /opt/esp32/esp-idf
grep -r MENUCONFIG .
This reveals that the file ./make/project_config.mk is the one of interest. This is a mere 101 line makefile (albeit with lots of dependencies). The line in question is:
$(summary) MENUCONFIG
Some examination of the Makefile leads to the following directory which contains source for several programs (mconf and conf) and merits further study.
/opt/esp32/esp-idf/tools/kconfig

The blink demo gives a nice example though of a connection between menuconfig and our code. The GPIO that is used to blink the LED is configured by menuconfig and discovered as the setting of the macro CONFIG_BLINK_GPIO. The gets set in the file sdkconfig.h, which as it turns out is hiding in the directory blink/build/include/sdkconfig.h and is pulled into the program in the usual way:

#include "sdkconfig.h"

A peek at the supplied Makefile

It consists of two lines, as follows:
PROJECT_NAME := hello-world
include $(IDF_PATH)/make/project.mk
project.mk is a 579 line monster that in turn includes other Makefiles.
Feedback? Questions? Drop me a line!

Tom's Computer Info / tom@mmto.org