1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00
RIOT/cpu/esp_common/Makefile.include
bors[bot] f7ef90d213
Merge #19074
19074: cpu/esp8266: build the SDK bootloader from source r=benpicco a=gschorcht

### Contribution description

This PR is a takeover of PR #17043, which is rebased to the current master and includes some corrections that became necessary after rebasing.

**Copied from description of PR #17043:**

We had four versions of pre-built bootloaders for the esp8266 with different settings of logging and color logging. These bootloaders were manually built from the SDK and shipped with RIOT-OS source code. However there are more settings that affect the bootloader build that are relevant to the app or final board that uses this bootloader. In particular, flash size and flash speed is important for the bootloader to be able to load an app from a large partition table at the fastest speed supported by the board layout and flash chip.

Another example is the UART baudrate of the logging output from the bootloader. The boot ROM will normally start at a baud rate of 74880 (depending on the crystal installed), so it might make sense to keep the UART output at the same speed so we can debug boot modes and bootloader with the same terminal.

This patch builds the `bootloader.bin` file from the ESP8266 SDK source code. The code is built as a module (`esp8266_bootloader`) which at the moment doesn't generate any object code for the application and only produces a `bootloader.bin` file set to the `BOOTLOADER_BIN` make variable for the `esptool.inc.mk` to flash.

The code needs to be compiled and linked with custom rules defined in the module's Makefile since the `bootloader.bin` is its own separate application.

The `BOOTLOADER_BIN` variable is changed from a path relative to the `$(RIOTCPU)/$(CPU)/bin/` directory to be full path. This makes it easier for applications or board to provide their own bootloader binary if needed.

As a result of building the bootloader from source we fixed the issue of having a large partition table.

### Testing procedure

Use following command to flash the application with STDIO UART baudrate of 115200 baud.
```
BAUD=74880 USEMODULE=esp_log_startup make -C tests/shell BOARD=esp8266-esp-12x flash
```
Connect with a terminal programm of your choice (unfortunatly `picocom` and `socat` don't support a baudrate close to 74880), for example:
```
python -m serial.tools.miniterm /dev/ttyUSB0 74880
```
On reset, the `esp8266-esp-12x` node shows the ROM bootloader log output
```
 ets Jan  8 2013,rst cause:2, boot mode:(3,7) 

load 0x40100000, len 6152, room 16 
tail 8
chksum 0x6f
load 0x3ffe8008, len 24, room 0 
tail 8
chksum 0x86
load 0x3ffe8020, len 3408, room 0 
tail 0
chksum 0x79
```
as well as the second-stage bootloader built by this PR (`ESP-IDF v3.1-51-g913a06a9ac3`) at 74880 baudrate.
```
I (42) boot: ESP-IDF v3.1-51-g913a06a9ac3 2nd stage bootloader
I (42) boot: compile time 11:25:03
I (42) boot: SPI Speed      : 26.7MHz
...
I (151) boot: Loaded app from partition at offset 0x10000
```
The application output is seen as garbage since the `esp8266-esp-12x` uses 115200 as baurate by default.

To see all output at a baudrate of 74880 baud, you can use the following command:
```
CFLAGS='-DSTDIO_UART_BAUDRATE=74880' BAUD=74880 USEMODULE=esp_log_startup make -C tests/shell BOARD=esp8266-esp-12x flash
```

If the application is built without options, the ROOM bootloader output will be 74880 baud and the second stage bootloader and application output will be 115200 baud.

### Issues/PRs references

Fixes issue #16402

Co-authored-by: iosabi <iosabi@protonmail.com>
Co-authored-by: Gunar Schorcht <gunar@schorcht.net>
2023-01-03 00:44:24 +00:00

162 lines
5.0 KiB
Makefile

# Default compile configurations
# FLASH_MODE=[ dout | dio | qout | qio ]
# use flash mode dout by default to keep GPIO9 and GPIO10 free for use
FLASH_MODE ?= dout
# ESP* pseudomodules
PSEUDOMODULES += esp_gdb
PSEUDOMODULES += esp_i2c_sw
PSEUDOMODULES += esp_log_colored
PSEUDOMODULES += esp_log_tagged
PSEUDOMODULES += esp_log_startup
PSEUDOMODULES += esp_qemu
PSEUDOMODULES += esp_spiffs
PSEUDOMODULES += esp_wifi_any
PSEUDOMODULES += esp_wifi_ap
# Common includes
INCLUDES += -I$(RIOTBOARD)/common/$(CPU)/include
INCLUDES += -I$(RIOTCPU)/esp_common
INCLUDES += -I$(RIOTCPU)/esp_common/include
INCLUDES += -I$(RIOTCPU)/esp_common/include/freertos
INCLUDES += -I$(RIOTCPU)/esp_common/vendor/
INCLUDES += -I$(RIOTCPU)/esp_common/vendor/esp
# Flags
CFLAGS += -Wno-unused-parameter
CFLAGS += -Wformat=0
CFLAGS += -fstrict-volatile-bitfields
CFLAGS += -fdata-sections
CFLAFS += -ffunction-sections
CFLAGS += -fzero-initialized-in-bss
ifeq (xtensa,$(CPU_ARCH))
CFLAGS += -mlongcalls -mtext-section-literals
endif
OPTIONAL_CFLAGS_BLACKLIST += -Wformat-overflow
OPTIONAL_CFLAGS_BLACKLIST += -Wformat-truncation
OPTIONAL_CFLAGS_BLACKLIST += -gz
ASFLAGS += --longcalls --text-section-literals
CFLAGS_DBG ?= -ggdb -g3
# override default CFLAGS_OPT in case module esp_gdb is enabled
ifneq (,$(filter esp_gdb,$(USEMODULE)))
CFLAGS_OPT ?= -Og
else
CFLAGS_OPT ?= -Os
endif
CFLAGS += $(CFLAGS_OPT) $(CFLAGS_DBG)
# add -DQEMU for qemu
ifneq (,$(filter esp_qemu,$(USEMODULE)))
CFLAGS += -DQEMU
endif
ifneq (,$(filter esp_freertos_common,$(USEMODULE)))
# thread_t.name required by FreeRTOS adaptation layer and esp_gdbstub
CFLAGS += -DCONFIG_THREAD_NAMES
# thread_t.stack_start required by FreeRTOS adaptation layer
CFLAGS += -DSCHED_TEST_STACK
endif
# use 32 priority levels if any WiFi interface or the ETH interface is used
ifneq (,$(filter esp_wifi_any esp_eth,$(USEMODULE)))
CFLAGS += -DSCHED_PRIO_LEVELS=32
endif
# The threads for handling the ESP hardware have the priorities from 2 to 4.
# The priority of lwIP TCPIP thread should be lower than these priorities.
ifneq (,$(filter lwip,$(USEMODULE)))
CFLAGS += -DTCPIP_THREAD_PRIO=5
endif
# extend CFLAGS by the corresponding CONFIG_FLASHMODE_* defines
ifeq (dout,$(FLASH_MODE))
CFLAGS += -DCONFIG_FLASHMODE_DOUT
CFLAGS += -DCONFIG_ESPTOOLPY_FLASHMODE_DOUT
else ifeq (dio,$(FLASH_MODE))
CFLAGS += -DCONFIG_FLASHMODE_DIO
CFLAGS += -DCONFIG_ESPTOOLPY_FLASHMODE_DIO
else ifeq (qout,$(FLASH_MODE))
CFLAGS += -DCONFIG_FLASHMODE_QOUT
CFLAGS += -DCONFIG_ESPTOOLPY_FLASHMODE_QUOT
else ifeq (qio,$(FLASH_MODE))
CFLAGS += -DCONFIG_FLASHMODE_QIO
CFLAGS += -DCONFIG_ESPTOOLPY_FLASHMODE_QIO
else
$(error Undefined FLASH_MODE, possible values are: dout, dio, qout and qio)
endif
ARCHIVES += -lg -lc
LINKFLAGS += $(CFLAGS_OPT) $(CFLAGS_DBG)
LINKFLAGS += -nostdlib -Wl,-gc-sections -Wl,-static
# use the wrapper functions for calloc to add correct overflow detection missing
# in the newlib's version.
LINKFLAGS += -Wl,-wrap,_calloc_r
ifneq (,$(filter esp_idf_heap,$(USEMODULE)))
LINKFLAGS += -Wl,-wrap,_malloc_r
LINKFLAGS += -Wl,-wrap,_realloc_r
LINKFLAGS += -Wl,-wrap,_free_r
endif
# LINKFLAGS += -Wl,--verbose
# LINKFLAGS += -Wl,--print-gc-sections
# increase the test timeout for file system tests that use the SPI flash drive
ifneq (,$(filter spiffs littlefs,$(USEMODULE)))
RIOT_TEST_TIMEOUT = 20
$(call target-export-variables,test,RIOT_TEST_TIMEOUT)
endif
# All ESP are flashed using esptool
PROGRAMMER ?= esptool
# Add esptool in the list supported programmers
PROGRAMMERS_SUPPORTED += esptool
# The ELFFILE is defined by default in $(RIOTBASE)/Makefile.include but only
# after this file is included, so we need ELFFILE defined earlier.
# This is used to create new make rules in this file (based on FLASHFILE)
# and can't be deferred.
ELFFILE ?= $(BINDIR)/$(APPLICATION).elf
FLASHFILE ?= $(ELFFILE).bin
ESPTOOL ?= $(RIOTTOOLS)/esptools/esptool.py
# This is the binary that ends up programmed in the flash.
$(ELFFILE).bin: $(ELFFILE)
$(Q)$(ESPTOOL) --chip $(FLASH_CHIP) elf2image --flash_mode $(FLASH_MODE) \
--flash_size $(FLASH_SIZE)MB --flash_freq $(FLASH_FREQ) $(FLASH_OPTS) \
-o $@ $<
# Convert .elf and .csv to .bin files at build time, but make them available for
# tests at flash time. These can't be added to FLASHDEPS because they depend on
# on ELFFILE and would trigger a rebuild with "flash-only".
BUILD_FILES += $(FLASHFILE) $(BINDIR)/partitions.bin $(BOOTLOADER_BIN)
TEST_EXTRA_FILES += $(FLASHFILE) $(BINDIR)/partitions.bin $(BOOTLOADER_BIN)
# Default partition table with no OTA. Can be replaced with a custom partition
# table setting PARTITION_TABLE_CSV.
PARTITION_TABLE_CSV ?= $(BINDIR)/partitions.csv
$(BINDIR)/partitions.csv: $(FLASHFILE)
$(Q)printf "\n" > $(BINDIR)/partitions.csv
$(Q)printf "nvs, data, nvs, 0x9000, 0x6000\n" >> $@
$(Q)printf "phy_init, data, phy, 0xf000, 0x1000\n" >> $@
$(Q)printf "factory, app, factory, $(FLASHFILE_POS), " >> $@
$(Q)ls -l $< | awk '{ print $$5 }' >> $@
$(BINDIR)/partitions.bin: $(PARTITION_TABLE_CSV)
$(Q)python3 $(RIOTTOOLS)/esptools/gen_esp32part.py --verify $< $@