Skip to content

Factory App Core — Entry Point (`factory_app_core_factory_app_entry`)

Factory App Core — Entry Point (factory_app_core_factory_app_entry)

Section titled “Factory App Core — Entry Point (factory_app_core_factory_app_entry)”

This module is the ESP-IDF application entry point for the factory recovery firmware. It implements the single exported symbol required by ESP-IDF — app_main — across all five actively-supported board variants of the factory_app_core group. Its responsibility is to:

  1. Restore OTA partition bookkeeping from a flash backup (before any other work).
  2. Bring up display and input hardware in the correct board-specific order.
  3. Detect available flash partitions and SD card payload files.
  4. Build and render the recovery menu.
  5. Run an infinite event loop that accepts input from physical buttons, a rotary encoder, and the touchscreen.

The module exists as five near-identical per-board source files that compile into independent binaries. Differences are confined to the LCD driver call, screen geometry constants, and — on two boards — extra bus/expander init steps imposed by hardware sharing constraints.

Board source fileDisplay driverFactory entry method
boards/esp32s3_8048s070c/Factory/main/main.cEK9716 (RGB parallel, 800 × 480)Software [ESP444]FACTORY command
boards/esp32s3_bzm_tft35_gt911/Factory/main/main.cST7796 (SPI, 320 × 480)Software [ESP444]FACTORY command
boards/esp32s3_hmi43v3/Factory/main/main.cRM68120 (i80 parallel)Software [ESP444]FACTORY command
boards/esp32s3_zx3d50ce02s_usrc_4832/Factory/main/main.cST7796 (i80 parallel)Software [ESP444]FACTORY command
boards/pibot_pendant_v1_0/Factory/main/main.cILI9341 (SPI, 240 × 320)Custom bootloader hook (button held at power-on) or software command

factory_app_core_factory_app_entry is a leaf of the factory_core sub-tree. All higher-level context is in factory_core.md and factory_app.md. The sibling sub-modules that app_main delegates to at runtime are:

Sibling module documentationResponsibility
factory_menu_system.mdMenu data model, navigation, rendering
factory_update_actions.mdFirmware/resources flash, OTA partition selection
factory_visual_feedback.mdProgress bar, flash screen, result display
factory_snapshot.mdDebug screen-capture to SD card
factory_input_dispatch.mdUnified button/touch/encoder dispatch
factory_hardware_drivers.mdBoard LCD, touch, buttons, encoder, buzzer drivers
factory_graphics.mdLow-level pixel drawing (gfx_*)
factory_sdcard.mdSD mount / unmount / file presence probe
factory_logging.mdLog silencing utilities

Sibling Modules

Main Event Loop

Hardware Init - ordered

factory_app_core_factory_app_entry - app_main

app_main

restore_otadata_from_backup

Hardware Init

Partition Detection

Menu Construction

Main Event Loop

factory_log_silence_sd_stack

I2C bus + TCA9554 IO expander

hmi43v3 only

Display Driver Init

Backlight Enable

gfx_init

buttons_init

encoder_init

touch_init

buzzer_init

snapshot_check()

if ENABLE_SNAPSHOT

encoder_read

button_wait_press(100 ms)

touch_read

menu_move

dispatch_button

touch_hint_hit_test

factory_input_dispatch

factory_menu_system

factory_update_actions


How the device reaches app_main in the factory partition differs between the pibot board and all others:

All other boards

Main firmware running

User sends

[ESP444]FACTORY command

esp444.cpp:

1. Back up otadata → 0xB000

2. Erase otadata

3. Set boot → factory

4. esp_restart()

app_main - factory partition

pibot_pendant_v1_0

Power on

+ button held

Custom bootloader hook

bootloader_before_init in hooks.c

backup_and_erase_otadata()

at offset 0xB000

Set boot → factory partition

esp_restart()

app_main - factory partition

Key invariant: In both paths, the caller backs up the current otadata to flash offset 0xB000 before switching the boot pointer to factory. restore_otadata_from_backup() inside app_main is symmetric to both paths.


The first action of app_main — before display, before any user interaction — is restore_otadata_from_backup(). This restores the ESP-IDF OTA slot selector so that power-cycling from inside recovery returns to the correct previously-active application partition, not to the factory partition.

0x00000 Bootloader
...
0x0B000 ◄── OTADATA_BACKUP_OFFSET (4 KB sector, below partition table)
[0x00 – 0x1F] OTA Entry 1 (32 bytes)
[0x20 – 0x3F] OTA Entry 2 (32 bytes)
[0x40 – 0x43] BACKUP_MAGIC = 0xAA55AA55
...
0x0C000 Partition table
0x0D000 NVS
...
0x10000 ◄── OTADATA_OFFSET (active otadata — two 4 KB sectors)
0x11000

⚠ CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED=y required. Offset 0xB000 is below the first normal partition. ESP-IDF aborts writes there by default. The factory app is an intentionally privileged recovery tool.

No

Yes

Yes

No

app_main starts

esp_flash_read magic

at 0xB040

magic ==

0xAA55AA55?

No valid backup - skip restore

Read 32-byte Entry1 from 0xB000

Read 32-byte Entry2 from 0xB020

Both entries

all 0xFF?

Skip write - flash already erased

Erase 0x10000 sector

Erase 0x11000 sector

Write Entry1 → 0x10000 if not empty

Write Entry2 → 0x11000 if not empty

Otadata restored

Erase backup sector 0xB000

prevent retrigger

Continue hardware init

OTA Constants (identical across all five boards)

Section titled “OTA Constants (identical across all five boards)”
ConstantValuePurpose
OTADATA_OFFSET0x10000Active otadata flash address
OTADATA_SECTOR_SIZE0x10004 KB sector size
OTADATA_ENTRY_SIZE32Bytes per OTA slot descriptor
OTADATA_BACKUP_OFFSET0xB000Backup sector address
BACKUP_MAGIC_OFFSET0x40Offset of magic word within backup sector
BACKUP_MAGIC0xAA55AA55Sentinel confirming a valid backup exists

These constants must match exactly the values used in the main firmware’s esp444.cpp (all boards except pibot) or hooks.c (pibot).


app_main initializes hardware in strict order. Deviating from this order causes invisible failures (blank display, unresponsive touch).

Buttons / Encoder / Touch / Buzzergfx layerBoard Display Driveresp_flash / OTA APIfactory_logapp_mainFreeRTOSButtons / Encoder / Touch / Buzzergfx layerBoard Display Driveresp_flash / OTA APIfactory_logapp_mainFreeRTOSBoard-specific extras may run here - see next sectionapp_main() invokedfactory_log_silence_sd_stack()restore_otadata_from_backup()done (backup found or absent - both safe)<board>_init()ESP_OK or abort()<board>_backlight(true)gfx_init()buttons_init()encoder_init()touch_init()buzzer_init()partition detect + menu build + draw_menu()enter infinite event loop

Two boards impose additional ordering constraints due to hardware sharing:

All other boards

_init()

Display init

_backlight(true)

touch_init()

esp32s3_zx3d50ce02s_usrc_4832

st7796_i80_init()

Panel reset pulse issued here

GPIO TFT_LED high

Plain-GPIO backlight

touch_init()

MUST follow display:

shared TFT_RST_PIN

esp32s3_hmi43v3

bus_i2c_init()

Shared I2C bus

io_tca9554_configure()

TCA9554 controls:

• LCD backlight bit

• Touch controller reset bit

rm68120_init()

Display init

touch_init()

I2C bus already up

hmi43v3 rationale: The TCA9554 IO expander controls both the LCD backlight enable bit and the touch controller’s hardware reset line. The expander must be configured before display init (or the display is invisible) and before touch_init() (or the touch IC stays in reset).

zx3d50ce02s_usrc_4832 rationale: The display panel and the FT6336U touch controller share a single physical TFT_RST_PIN. The st7796_i80_init() routine issues a reset pulse that simultaneously releases the touch IC from reset. Calling touch_init() before display init would conflict with this reset sequence.


No

Yes

ESP-IDF calls app_main

factory_log_silence_sd_stack

restore_otadata_from_backup

board display init

board backlight on

Init returned

ESP_OK?

esp3d_log_e error

vTaskDelay 200 ms

abort()

gfx_init

buttons_init

encoder_init

touch_init

buzzer_init

esp_partition_find_first app1

→ has_app1 flag

Build menu_items array

probe_sd_files()

→ sd_has_fw / sd_has_res

draw_menu

gpio_config GPIO_NUM_0

if ENABLE_SNAPSHOT

Main event loop


After hardware init, app_main detects the flash layout and builds the menu accordingly:

Yes

No

Yes

No

Start menu build

Add: Boot app0 - always

app1 partition

exists?

Add: Boot app1

Add: Flash firmware SD → app0 - always

app1 exists?

Add: Flash firmware SD → app1

Add: Flash UI resources SD - always

probe_sd_files()

Mount SD, check for esp3dfw.bin

and ui_resources.bin, unmount

draw_menu

show FW / RES indicators in header

probe_sd_files() mounts the SD card, tests file existence, then immediately unmounts. The results (sd_has_fw, sd_has_res) drive the “FW” and “RES” visual indicators in the menu header line. SD file naming conventions are documented in factory_sdcard.md.


After initialization, app_main enters an infinite polling loop. No additional RTOS tasks are created; the entire factory app runs within the single app_main task.

Yes

No

Yes

No

Yes

No

No

Yes

No

Yes

while (1)

ENABLE_SNAPSHOT

defined?

snapshot_check()

GPIO0 / BOOT button debounce

+ capture to SD

encoder_read()

non-blocking

enc > 0?

menu_move(-1) scroll up

enc < 0?

menu_move(+1) scroll down

button_wait_press(100 ms)

blocking with timeout

dispatch_button(btn)

touch_read()

non-blocking

pressed &&

!touch_was_pressed?

touch_was_pressed = touch.pressed

touch_hint_hit_test(x, y)

vbtn != BTN_NONE?

draw_button_hint_pressed(vbtn)

buzzer_beep_short()

vTaskDelay 80 ms

draw_button_hints()

dispatch_button(vbtn)

All three input sources resolve to dispatch_button(button_id_t):

Input sourceConditionMaps to
Physical BTN_1pressmenu_move(-1) — scroll up
Physical BTN_2pressmenu_move(+1) — scroll down
Physical BTN_3pressexecute_selected_action() — confirm
Encoder CW stepencoder_read() > 0menu_move(-1)
Encoder CCW stepencoder_read() < 0menu_move(+1)
Touch BTN_1 zonetapmenu_move(-1)
Touch BTN_2 zonetapmenu_move(+1)
Touch BTN_3 zonetapexecute_selected_action()

On ESP32-S3 boards without physical hardware, BUTTON_n_PIN and ENCODER_n_PIN are GPIO_NUM_NC. The corresponding init and read calls silently no-op; touch is the only operational input.

For dispatch implementation details see factory_input_dispatch.md.

A rising-edge detector prevents repeat-triggering while a finger is held:

touch_point_t touch = touch_read();
if (touch.pressed && !touch_was_pressed) {
/* rising edge only — handle once per press */
button_id_t vbtn = touch_hint_hit_test(touch.x, touch.y);
if (vbtn != BTN_NONE) {
draw_button_hint_pressed(vbtn);
buzzer_beep_short();
vTaskDelay(pdMS_TO_TICKS(80));
draw_button_hints();
dispatch_button(vbtn);
}
}
touch_was_pressed = touch.pressed;

The 80 ms pause between draw_button_hint_pressed and dispatch_button provides visible press feedback before executing the action.

The three virtual button zones cover the bottom of the screen. The hit-test uses different column-boundary strategies depending on canvas width:

Canvas widthStrategy
≤ 320 px (portrait)Simple thirds: x < W/3 → BTN_1, x < 2W/3 → BTN_2, else BTN_3
≥ 480 px (landscape / large)Midpoint boundaries between the actual rendered circle-center X coordinates — prevents zone mismapping on wide screens where a simple third-split does not align with the rendered icons

When ENABLE_SNAPSHOT is defined, app_main configures GPIO_NUM_0 (BOOT button) as an additional input. Pressing it at any point captures the full screen to a numbered .raw file on the SD card. This is a QA/debug feature; production builds leave ENABLE_SNAPSHOT undefined.

Yes

No

GPIO_NUM_0 pressed

snapshot_check()

snapshot_take()

sdcard_mount()

snap_find_next_number()

scan snap000.raw … snap999.raw

gfx_snapshot_begin(filepath)

Currently

flashing?

draw_flashing_screen()

draw_progress(last %)

draw_menu()

gfx_snapshot_end()

close file, unmount

For full detail see factory_snapshot.md.


PathMeaning
/sdcard/esp3dfw.binFirmware image to flash
/sdcard/esp3dfw.okRenamed from .bin after successful flash
/sdcard/esp3dfw.badRenamed from .bin after failed flash
/sdcard/ui_resources.binUI resources partition image
/sdcard/ui_resources.okRenamed after successful flash
/sdcard/ui_resources.badRenamed after failed flash
/sdcard/snap000.raw …Debug screen snapshots (ENABLE_SNAPSHOT builds only)

When adding a new board:

  1. Copy the closest existing main.c (match on display bus type: SPI, i80, or RGB).
  2. Replace the display include and init call (<board>_init(), <board>_backlight()).
  3. Check reset sharing: if display and touch share a reset pin, touch_init() must follow display init (see zx3d50ce02s_usrc_4832).
  4. Check IO expander: if any expander gates backlight or touch reset, add bus + expander init before display init (see hmi43v3).
  5. Keep OTADATA_BACKUP_OFFSET in sync with the value in the main firmware’s esp444.cpp (or hooks.c for bootloader-based boards). Recalculate if the new board’s bootloader extends past 0xB000.
  6. Ensure the factory sdkconfig sets CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED=y.
  7. Scale layout constants (MENU_START_Y, MENU_ITEM_H, FONT_WIDTH, BTN_CIRCLE_R, touch column boundaries) for the target resolution. In-source comments in the existing files document the scaling ratios used.

hmi43v3 only

factory_app_core_factory_app_entry

app_main()

ESP-IDF

esp_flash · esp_ota_ops

esp_partition · esp_system

factory_logging

factory_log_silence_sd_stack

Board display driver

factory_hardware_drivers

factory_graphics

gfx_init

factory_buttons

buttons_init / button_wait_press

factory_encoder

encoder_init / encoder_read

factory_touch

touch_init / touch_read

factory_buzzer

buzzer_init / buzzer_beep_short

factory_menu_system

draw_menu / menu_move

factory_update_actions

execute_selected_action

factory_visual_feedback

draw_flashing_screen / draw_progress

factory_snapshot

snapshot_check

factory_input_dispatch

dispatch_button / touch_hint_hit_test

esp_lcd I2C bus

bus_i2c_init

TCA9554 IO expander

io_tca9554_configure