Skip to content

Commit c7af97d

Browse files
committed
examples: zephyr_example: refactor
Update Zephyr port to better use dts and available subsystems Add shell commands to control target SoC flash. Signed-off-by: Marek Matej <marek.matej@espressif.com>
1 parent 7790b6d commit c7af97d

25 files changed

Lines changed: 1129 additions & 219 deletions

examples/zephyr_example/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ zephyr_library_sources(
1313

1414
zephyr_library_include_directories(
1515
../common
16+
../../zephyr/subsys/esp_loader
1617
)
1718

19+
zephyr_linker_sources(SECTIONS ../../zephyr/subsys/esp_loader/sections-rom.ld)
20+
1821
# Needed for example_common
1922
add_compile_definitions(SERIAL_FLASHER_INTERFACE_UART)
2023

2124
# Convert binary files to C source files using bin2array.cmake
22-
include(${CMAKE_SOURCE_DIR}/../common/bin2array.cmake)
25+
include(${CMAKE_SOURCE_DIR}/../../zephyr/subsys/esp_loader/bin_images_sections.cmake)
2326
create_resources(${CMAKE_SOURCE_DIR}/target-firmware ${CMAKE_BINARY_DIR}/target_firmware_data.c)
2427
set_property(SOURCE ${CMAKE_BINARY_DIR}/target_firmware_data.c PROPERTY GENERATED 1)
2528

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2026 Espressif Systems (Shanghai) Co., Ltd.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/
8+
{
9+
esp_loader0: esp_loader {
10+
compatible = "espressif,esp-loader";
11+
uart = <&uart1>;
12+
default-baudrate = <115200>;
13+
higher-baudrate = <230400>;
14+
reset-gpios = <&gpio0 25 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
15+
boot-gpios = <&gpio0 26 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
16+
sync-timeout-ms = <100>;
17+
num-trials = <10>;
18+
};
19+
};
20+
21+
&pinctrl {
22+
uart1_example: uart1_example {
23+
group1 {
24+
pinmux = <UART1_TX_GPIO4>;
25+
output-high;
26+
};
27+
group2 {
28+
pinmux = <UART1_RX_GPIO5>;
29+
bias-pull-up;
30+
};
31+
};
32+
};
33+
34+
&uart1 {
35+
pinctrl-0 = <&uart1_example>;
36+
pinctrl-names = "default";
37+
current-speed = <115200>;
38+
status = "okay";
39+
};
40+
41+
&esp_loader0 {
42+
status = "okay";
43+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2026 Espressif Systems (Shanghai) Co., Ltd.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/
8+
{
9+
esp_loader0: esp_loader {
10+
compatible = "espressif,esp-loader";
11+
uart = <&uart1>;
12+
default-baudrate = <115200>;
13+
higher-baudrate = <230400>;
14+
reset-gpios = <&gpio0 7 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
15+
boot-gpios = <&gpio0 8 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>;
16+
sync-timeout-ms = <100>;
17+
num-trials = <10>;
18+
};
19+
};
20+
21+
&pinctrl {
22+
uart1_example: uart1_example {
23+
group1 {
24+
pinmux = <UART1_TX_GPIO18>;
25+
output-high;
26+
};
27+
group2 {
28+
pinmux = <UART1_RX_GPIO17>;
29+
bias-pull-up;
30+
};
31+
};
32+
};
33+
34+
&uart1 {
35+
pinctrl-0 = <&uart1_example>;
36+
pinctrl-names = "default";
37+
current-speed = <115200>;
38+
status = "okay";
39+
};
40+
41+
&esp_loader0 {
42+
status = "okay";
43+
};

examples/zephyr_example/prj.conf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# nothing here
21
CONFIG_CONSOLE=y
32
CONFIG_CONSOLE_GETCHAR=y
4-
CONFIG_NEWLIB_LIBC=y
3+
CONFIG_ESP_SERIAL_FLASHER=y

examples/zephyr_example/socs/esp32_procpu.overlay

Lines changed: 0 additions & 42 deletions
This file was deleted.

examples/zephyr_example/src/main.c

Lines changed: 28 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,90 +16,58 @@
1616
#include <zephyr/drivers/uart.h>
1717
#include <zephyr/sys/util.h>
1818

19-
#include <stdio.h>
20-
#include <string.h>
2119
#include <zephyr_port.h>
2220
#include <esp_loader.h>
21+
#include <esp_loader_io.h>
2322
#include "example_common.h"
23+
#include "bin_image.h"
2424

25-
// Embedded binary files using bin2array.cmake
26-
extern const uint8_t bootloader_bin[];
27-
extern const uint32_t bootloader_bin_size;
28-
extern const uint8_t bootloader_bin_md5[];
29-
extern const uint8_t partition_table_bin[];
30-
extern const uint32_t partition_table_bin_size;
31-
extern const uint8_t partition_table_bin_md5[];
32-
extern const uint8_t app_bin[];
33-
extern const uint32_t app_bin_size;
34-
extern const uint8_t app_bin_md5[];
35-
36-
#define HIGHER_BAUDRATE 230400
37-
#define DEFAULT_BAUDRATE 115200
38-
39-
/* Get UART DTS entry used as flash interface */
40-
static const struct device *esp_uart_dev = DEVICE_DT_GET(DT_ALIAS(uart));
41-
/* Get GPIO pin connected to the ESP's enable pin. */
42-
static const struct gpio_dt_spec esp_enable_spec = GPIO_DT_SPEC_GET(DT_ALIAS(en), gpios);
43-
/* Get GPIO pin connected to the ESP's boot pin. */
44-
static const struct gpio_dt_spec esp_boot_spec = GPIO_DT_SPEC_GET(DT_ALIAS(boot), gpios);
25+
#define ESF_NODE DT_NODELABEL(esp_loader0)
26+
static const struct device *uart = DEVICE_DT_GET(DT_PHANDLE(ESF_NODE, uart));
27+
static uint32_t default_baudrate = DT_PROP(ESF_NODE, default_baudrate);
28+
static uint32_t higher_baudrate = DT_PROP(ESF_NODE, higher_baudrate);
4529

4630
int main(void)
4731
{
48-
49-
const loader_zephyr_config_t config = {
50-
.uart_dev = esp_uart_dev,
51-
.enable_spec = esp_enable_spec,
52-
.boot_spec = esp_boot_spec
53-
};
54-
5532
printk("Running ESP Flasher from Zephyr\r\n");
5633

57-
if (!device_is_ready(esp_uart_dev)) {
58-
printk("ESP UART not ready");
59-
return -ENODEV;
34+
if (connect_to_target(higher_baudrate) != ESP_LOADER_SUCCESS) {
35+
printf("Failed to connect target!\n");
36+
return -1;
6037
}
6138

62-
if (!device_is_ready(esp_boot_spec.port)) {
63-
printk("ESP boot GPIO not ready");
64-
return -ENODEV;
65-
}
39+
target_chip_t chip = esp_loader_get_target();
40+
uint32_t bootloader_addr = get_bootloader_address(chip);
6641

67-
if (!device_is_ready(esp_enable_spec.port)) {
68-
printk("ESP enable GPIO not ready");
69-
return -ENODEV;
70-
}
71-
72-
gpio_pin_configure_dt(&esp_boot_spec, GPIO_OUTPUT_ACTIVE);
73-
gpio_pin_configure_dt(&esp_enable_spec, GPIO_OUTPUT_INACTIVE);
42+
printf("Chip id: %d\n", chip);
43+
printf("bootloader offset: 0x%x\n", bootloader_addr);
44+
printk("Flashing all available images...\n");
7445

75-
if (loader_port_zephyr_init(&config) != ESP_LOADER_SUCCESS) {
76-
printk("ESP loader init failed");
77-
return -EIO;
46+
STRUCT_SECTION_FOREACH(bin_image, t) {
47+
flash_binary(t->data, t->size, t->offset);
7848
}
7949

80-
if (connect_to_target(HIGHER_BAUDRATE) == ESP_LOADER_SUCCESS) {
81-
printk("Loading bootloader...\n");
82-
target_chip_t chip = esp_loader_get_target();
83-
uint32_t bootloader_addr = get_bootloader_address(chip);
84-
flash_binary(bootloader_bin, bootloader_bin_size, bootloader_addr);
85-
printk("Loading partition table...\n");
86-
flash_binary(partition_table_bin, partition_table_bin_size, PARTITION_TABLE_ADDRESS);
87-
printk("Loading app...\n");
88-
flash_binary(app_bin, app_bin_size, APPLICATION_ADDRESS);
89-
}
50+
// Disabling UART interrupts allows target to reset properly
51+
uart_irq_tx_disable(uart);
52+
uart_irq_rx_disable(uart);
9053

9154
esp_loader_reset_target();
55+
// Delay for skipping the boot message of the targets
56+
k_msleep(500);
57+
58+
// Enabling UART interrupts to read the target messages
59+
uart_irq_tx_enable(uart);
60+
uart_irq_rx_enable(uart);
9261

93-
if (loader_port_change_transmission_rate(DEFAULT_BAUDRATE) == ESP_LOADER_SUCCESS) {
94-
// Delay for skipping the boot message of the targets
95-
k_msleep(500);
62+
if (loader_port_change_transmission_rate(default_baudrate) ==
63+
ESP_LOADER_SUCCESS) {
9664

9765
printk("********************************************\n");
9866
printk("*** Logs below are print from slave .... ***\n");
9967
printk("********************************************\n");
10068
while (1) {
10169
uint8_t c;
102-
if (uart_poll_in(esp_uart_dev, &c) == 0) {
70+
if (uart_poll_in(uart, &c) == 0) {
10371
printk("%c", c);
10472
}
10573
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Target Firmware
22

3-
Place the following ESP32 firmware binaries in this directory:
3+
Place all necessary ESP32 firmware binaries in this directory and use the followig file name:
44

5-
- `bootloader.bin` - ESP32 bootloader binary
6-
- `partition-table.bin` - Partition table configuration
7-
- `app.bin` - Main application binary
5+
- `bootloader_0x0.bin` - ESP32 bootloader binary flashed at offset 0x0
6+
- `partition-table_0x8000.bin` - Partition table configuration at offset 0x8000
7+
- `app_0x10000.bin` - Main application binary at offset 0x10000
88

99
These binaries are automatically generated during CI builds or can be manually copied from your ESP-IDF build output.

0 commit comments

Comments
 (0)