ESP32应用平台实战 像手机一样安装和切换固件应用Building an ESP32 Application Platform: Installing and Switching Firmware Apps Like a Smartphone


AI free token/AI免费积分

ESP32应用平台实战 像手机一样安装和切换固件应用

本文来源:http://www.lpqz.cn/news/150354
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

很多刚玩到ESP32的朋友都问过一个问题这东西能不能像手机一样开机一个桌面里面一堆应用图标点哪个跑哪个想换功能直接“装个App”就行我最早觉得这就是天方夜谭因为单片机的传统玩法就是一次烧一个固件换个功能就得重新连线、重新擦除、重新烧录体验和手机差了十万八千里。但后来我认认真真把ESP32的分区表、OTA机制、文件系统这些东西捋了一遍发现“像手机一样安装应用”并不是不能做——我做了一个能在ESP32上跑的小型应用平台这篇文章就把完整思路、架构、代码和踩过的坑都写出来。先说清楚这个平台能做什么开机后进入一个Launcher它会把已安装的应用列出来你通过按钮、屏幕或者串口选择一个应用系统就会重启并进入对应应用应用里也能执行“返回平台”一键切回应用列表。安装过程也做了简化和回滚保护应用包可以通过串口、WiFi或者SD卡分发体验上已经非常接近手机装App的那套流程。这个方案特别适合做多固件演示机、教

学硬件、展会样机以及任何需要在一块板子上无缝切换多套功能的场景。1. 为什么ESP32天生不适合“装应用”我的平台又是怎么绕过去的1.1 先搞清楚一个前提ESP32的“应用”不等于手机AppESP32本质上还是一颗单片机。它没有MMU内存管理单元不跑完整Linux跑的是FreeRTOS这种RTOS。在不支持虚拟内存的情况下进程隔离、动态链接这些手机系统的基本能力统统没有你不可能把一个编译好的任意ELF文件丢进去“执行”——代码的链接地址需要在编译时就固定下来运行时跳转到一段任意地址的指令大概率直接崩。所以最开始我给自己定了一条规定不要试图把ESP32模拟成一台手机那是给自己挖坑。手机安装应用的本质是什么是系统内核具备“加载新进程代码并调度”的能力。ESP32不具备这个能力但我们可以换一条思路——把“应用”理解为一个独立的固件镜像放在一个独立的分区里由一个引导管理器负责切换启动。这正是ESP

-IDF官方OTA机制一直在做的事我们只是把它产品化、体验化。1.2 三条可行路径以及我为什么最终选了分区跳转我实际评估过三条路路径A多分区固件跳转。每个应用单独编译成固件烧录到独立的分区。系统启动时先运行Launcher我放在factory分区Launcher根据用户选择通过写OTAData的方式指定下一次启动的分区然后重启。优点是不损失性能每个应用都能用满ESP32的CPU和外设权限缺点是应用之间不能同时运行切换需要一次重启。对小型嵌入式产品来说这个缺点完全可以接受。路径B脚本类应用。把MicroPython或自定义脚本解析器作为固定底座应用只是存放在文件系统里的脚本文件。优点是“安装”就是写文件非常轻缺点是性能差、依赖脚本库复杂的驱动逻辑写起来很别扭。路径C单固件“软应用”。把所有功能编进一个固件用配置项或标志位决定当前进入哪套逻辑。优点是实现最快缺点是每加一个应用就要重新编译整个固

件应用间无法隔离一个模块出Bug可能拖垮全局。我最终选了路径A作为平台主干。理由有三第一它直接复用ESP-IDF的OTA/启动机制稳定性有保障第二应用的开发方式就是普通的ESP-IDF工程开发者不需要引入任何脚本语言第三应用崩溃不会影响Launcher因为Launcher和应用是两套互相独立的固件复位后永远是Launcher先接管控制权。1.3 一个朴素的产品定义这个平台不是一个操作系统而是一个最小可用应用管理器。我把它拆成四部分Launcher引导器、应用分区每个应用一个、应用清单记录安装信息、安装通道负责把应用包写入分区和清单。这个定义贯穿了后面所有设计遇到问题先问“这一步是引导器干的还是安装器干的还是应用自己干的”事情就会清晰很多。2. 平台架构分区表、引导器和应用清单三者如何协作2.1 分区表所有设计的第一步在ESP32上Flash里装什么、每块多大、用于什么类型全部由分区表决定。

分区表是所有设计的起点画错后面全崩。我选用的是4MB Flash的ESP32-S3 DevKitC做测试分区表大致规划分区名类型子类型偏移大小用途nvsdatanvs0x90000x6000系统参数、返回标志otadatadataota0xf0000x2000记录下次启动哪个App分区factoryappfactory0x100000x100000Launcher平台底座app1appota_10x1100000x100000示例应用1app2appota_20x2100000x100000示例应用2storagedataspiffs0x3100000xe0000应用清单、上传包暂存区这里有三个细节要强调otadata分区必须存在并且大小至少是0x2000。ESP-IDF的bootloader通过读otadata来判断本次要启动哪个分区。它本质上是“开机时读门口那块指示牌”。所有偏移都按0

x1000064KB对齐。我见过很多人随手把偏移写成0x1c000这种“看起来没问题”的地址结果在跳转时遇到奇怪问题后面坑位专门讲。应用分区用ota_1、ota_2而不是custom子类型。原因在于esp_ota_set_boot_partition()这个关键接口只认OTA槽位自定义子类型的分区它直接拒绝。2.2 引导器负责“开机看到什么”Launcher就是我放在factory分区的固件它干的事情很简单扫描所有APP类型分区读取storage里保存的应用清单把“已安装应用”展示出来然后等用户选择。Launcher本身不需要复杂UI。我的第一版只用一个按钮和一个OLED屏按钮切换应用名称长按确认启动。后来加了LVGL版本但核心逻辑完全一样。最关键的一点是Launcher不能“启动”应用它只能设置下一次启动的分区然后重启。这个“复位级切换”和手机App的进程级切换有着本质区别但用户并不关心

底层区别只要体验顺畅就行。// 引导器核心把目标分区设为下次启动分区 static esp_err_t launch_app(const esp_partition_t* app_partition) { esp_err_t err esp_ota_set_boot_partition(app_partition); if (err ! ESP_OK) { ESP_LOGE(launcher, set boot partition failed: %s, esp_err_to_name(err)); return err; } esp_restart(); return ESP_OK; // 不会走到这里 }2.3 应用清单让ESP32认识你的应用应用清单我使用一个简单的文本格式文件manifest.bin存在storage分区。之所以不用JSON是因为我前期用cJSON解析时踩过内存不

足、解析失败的坑嵌入式场景下“每行一个键值对”的极简格式反而最稳。magicESPA format_version1 app_idled_demo nameLED Demo partitionapp1 app_version0.1 crc320x9e28b930字段含义app_id是应用的全局唯一标识partition是它安装到哪个分区必须和分区表里的label一致crc32是固件二进制内容的哈希用于校验其余是展示信息。Launcher启动时逐行读取只认magicESPA开头的文件其他一律视为无效清单。这样做的好处是清单和固件分离卸载应用只需要删掉清单里的条目不需要写任何代码。2.4 应用间的“切换协议”跳转与返回平台化有条底线应用和Launcher之间要有一套松耦合的切换协议。我不希望应用必须链接Launcher的库才能返回平台所以协议控制在“两块Flash区域”层面Launcher要启

动应用时调用esp_ota_set_boot_partition(应用分区)然后esp_restart()应用要返回平台时做同样的事只是目标换成factory分区。用文字描述完整流程上电 - bootloader读otadata - 发现指向factory - 加载LauncherLauncher读manifest - OLED/串口显示应用列表用户选择“LED Demo” - Launcher写otadata指向app1 - 重启bootloader读otadata - 发现指向app1 - 加载LED Demo固件LED Demo里一键“返回平台” - 写otadata指向factory - 重启循环。这个协议看起来简单的令人发指但正是“复位级切换”的魅力应用根本不需要知道Launcher怎么运行Launcher也不需要了解应用内部逻辑。两者唯一的交流媒介就是一块otadata和一小段

NVS标志位。3. “安装”到底在干什么应用包格式与三条分发通道3.1 安装的本质很多人的第一反应是问了“用什么命令安装”其实安装的本质特别朴素把应用固件写入对应的分区然后在storage里写入一条清单记录。和手机安装APK的“解压资源注册组件安全校验”相比嵌入式平台的安装过程几乎就是文件搬运。难点反而在可靠性上写入一半断电怎么办固件坏了却把otadata切过去怎么办所以我设计了一个**.espapp应用包格式**借鉴了APK最简单的部分字段长度说明魔数4字节ESPA格式版本2字节当前为1分区名16字节目标分区label不足补零固件长度4字节后续固件数据字节数CRC324字节对固件数据算CRC32固件数据变长app.bin原样数据安装器拿到一个.espapp文件后先解析头部校验CRC然后按“边收边写”的方式把固件流式写入目标分区的备用区全部写完再切otadata。这个“先写、再验、后切换”

的顺序是整个平台的命根子。3.2 通道一烧录期安装开发阶段最常用开发应用时最直接的方法就是不用安装器直接用烧录工具把应用固件写到对应分区。我平时用esptool.pyesptool.py --chip esp32s3 -p /dev/ttyACM0 write_flash 0x110000 app1.bin注意这里的0x110000必须和分区表里app1的偏移完全一致。如果用了图形化的Flash Download Tool同样要手动填对地址。这个阶段的“安装失败”大多是因为地址填错或者Flash加密/安全启动没关闭导致的校验失败。3.3 通道二局域网无线安装产品化体验的核心既然叫“安装应用”纯靠USB线总觉得不够味。我在Launcher里做了一个简易的SoftAPHTTP安装模式按某个GPIO进入安装模式ESP32打开一个名为ESPApp的热点手机浏览器访问192.168.4.1选择.es

papp文件上传然后HTTP服务器流式写入目标分区。这里有个内存层面的关键点ESP32的可用RAM只有几百KB不能把整个应用包读进内存再写Flash。必须用esp_partition_write()按块写每收到4KB就刷一次Flash。我当时第一版就是偷懒把整个包读进heap2MB的应用直接把系统跑崩了后来改成流式写入才稳。WiFi配网也是实际体验的分水岭建议用wifi_provisioning组件而不是自己写网页配网。3.4 通道三SD卡或文件系统安装现场演示最省心如果设备支持SD卡或者你愿意把storage留出一块路径最省心的自动化安装方式是把.espapp文件丢进/install目录然后Launcher在开机时扫描这个目录自动安装并记录日志。我在一次展会演示机项目里就用这种方式现场人员只需要拿SD卡插一下设备就自动更新应用列表完全不需要电脑和专业操作经验。3.5 安装器的回滚与断电保

护再好的“安装体验”遇上一次安装到一半拔电就可能前功尽弃。我用的回滚策略是双区待切换新应用先写入备用分区比如当前app1在用就写app2对应的空间全部写完并重新计算CRC确认和包头一致把otadata切到新分区同时在新分区对应的NVS区域写一个boot_ok0标志重启后如果新应用在10秒内上报boot_ok1则安装正式完成如果10秒超时Launcher侧超时看门狗自动把otadata切回上一个可用分区并重启。这其实就是PC上“系统还原点”的极简版。虽然不能覆盖所有意外但对付“传输一半拔电”和“新固件一启动就崩”这两类最常见问题已经够了。4. 从零跑起一套ESP32应用平台的具体操作记录4.1 准备硬件与环境我用的是ESP32-S3 DevKitC4MB Flash版本其实ESP32、ESP32-C3同样适用只是分区大小要跟着调。开发环境直接用ESP-IDF v5.x安装和配置参考官方教程这

一步没什么特别的。需要额外装的是Python环境用来跑打包脚本和esptool。环境准备好后先整体规划分区表再按Launcher和应用两套工程分别开发。强烈建议把两个工程分开目录管理因为它们的编译目标完全不同混在一个工程里会非常痛苦。4.2 分区表配置示例在Launcher工程里新建partitions_4mb.csv内容就是我前面画的那张表注意由于我给Flash加密相关组件留了启动空间实际编译时需要在menuconfig中指定分区表文件路径# Name, Type, SubType, Offset, Size, Flags nvs, data, nvs, 0x9000, 0x6000, otadata, data, ota, 0xf000, 0x2000, factory, app, factory, 0x10000, 0x100000, app1, app, ota_1, 0x1100

00, 0x100000, app2, app, ota_2, 0x210000, 0x100000, storage, data, spiffs, 0x310000, 0xe0000,写完分区表后我的习惯是先用命令核对一下idf.py partition-table --print这个命令会把实际落盘地址打出来对照一下CSV里的偏移是不是和工具解析一致。很多所谓的“玄学启动失败”其实在那一步就能拦住。4.3 编写引导器核心代码Launcher的代码不复杂核心就三件事挂载storage分区、扫描应用分区、写otadata并重启。#include esp_ota_ops.h #include esp_partition.h #include esp_littlefs.h static const char* MANIFEST_PATH /storage/manifest.bin; // 读

Building an ESP32 Application Platform: Installing and Switching Firmware Apps Like a Smartphone

本文来源:http://www.lpqz.cn/news/150354
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

Many people who are new to the ESP32 have asked the same question: can this thing work like a phone — boot into a desktop full of app icons, tap one to run it, and swap in a new feature simply by 'installing an App'? At first I thought this was pure fantasy, because the traditional way with microcontrollers is to flash one firmware at a time; changing a function means rewiring, re-erasing, and re-flashing — an experience light years away from a smartphone. But later I carefully worked through the ESP32's partition table, OTA mechanism, and file system, and discovered that 'installing apps like a phone' is not impossible at all — I built a small application platform that runs on the ESP32, and this article lays out the complete thinking, architecture, code, and pitfalls I ran into. First, let me be clear about what this platform can do: after boot you enter a Launcher that lists the installed applications; you select one via a button, a screen, or the serial port, and the system reboots into the corresponding app. Inside an app you can also execute 'return to platform' to switch back to the app list with one press. The installation process is simplified and protected by rollback: app packages can be distributed via serial, WiFi, or SD card, making the experience already very close to the phone-app installation flow. This approach is especially well suited for multi-firmware demo machines, for

teaching hardware, trade-show prototypes, and any scenario that needs to switch seamlessly between multiple sets of functions on a single board. 1. Why the ESP32 is naturally unsuited to 'installing apps', and how my platform works around it. 1.1 First, one premise: an ESP32 'app' is not the same as a phone app. The ESP32 is still essentially a microcontroller. It has no MMU (memory management unit), does not run a full Linux, and runs an RTOS such as FreeRTOS. Without virtual memory support, the basic capabilities of a phone OS — process isolation and dynamic linking — are simply absent; you cannot drop an arbitrary compiled ELF file in and 'execute' it, because the code's link addresses must be fixed at compile time, and jumping to instructions at an arbitrary address at runtime will most likely crash outright. So from the very start I set myself one rule: don't try to turn the ESP32 into a phone — that's digging your own grave. What is the essence of installing an app on a phone? It is the kernel's ability to 'load a new process's code and schedule it'. The ESP32 lacks this ability, but we can take a different route: treat an 'app' as a standalone firmware image placed in its own partition, with a boot manager responsible for switching which one starts. This is exactly what ESP

-IDF's official OTA mechanism has been doing all along; we have merely productized it and polished the experience. 1.2 Three viable paths, and why I finally chose partition jumping. I actually evaluated three paths. Path A: multi-partition firmware jumping. Each app is compiled separately into firmware and flashed into its own partition. At startup the system runs the Launcher first — I put it in the factory partition — and the Launcher, based on the user's selection, specifies the partition to boot next by writing the OTAData, then reboots. The advantage is no performance loss: each app can use the full CPU and peripheral privileges of the ESP32. The drawback is that apps cannot run simultaneously and switching requires a reboot. For small embedded products this drawback is perfectly acceptable. Path B: script-based apps. Use MicroPython or a custom script interpreter as a fixed base, with apps merely script files stored in the file system. The advantage is that 'installation' is just writing a file — extremely lightweight. The drawbacks are poor performance, reliance on script libraries, and awkwardness in writing complex driver logic. Path C: single-firmware 'soft apps'. Compile all features into one firmware and use configuration items or flags to decide which logic to enter. The advantage is the fastest implementation; the drawback is that every new app requires recompiling the entire firmware

and apps cannot be isolated from one another — a bug in one module can bring down the whole system. I ultimately chose Path A as the backbone of the platform, for three reasons. First, it directly reuses ESP-IDF's OTA/boot mechanism, so stability is assured. Second, developing an app is just an ordinary ESP-IDF project — developers don't need to bring in any scripting language. Third, an app crash cannot affect the Launcher, because the Launcher and the app are two mutually independent firmware images; after a reset the Launcher always takes control first. 1.3 A modest product definition: this platform is not an operating system but a minimal viable application manager. I split it into four parts: the Launcher (boot manager), application partitions (one per app), the application manifest (recording installation information), and the installation channel (responsible for writing app packages into partitions and the manifest). This definition runs through all subsequent design decisions: when you hit a problem, just ask 'is this step the boot manager's job, the installer's job, or the app's own job?' and things become much clearer. 2. Platform architecture: how the partition table, boot manager, and application manifest cooperate. 2.1 The partition table, the first step of all design: on the ESP32, what goes into Flash, how big each block is, and what type it is are all determined by the partition table.

The partition table is the starting point of all design; draw it wrong and everything after it collapses. I used an ESP32-S3 DevKitC with 4 MB Flash for testing, and the partition table was roughly planned as follows — name, type, subtype, offset, size, purpose: nvs, data, nvs, 0x9000, 0x6000, system parameters and return flags; otadata, data, ota, 0xf000, 0x2000, recording which App partition to boot next; factory, app, factory, 0x10000, 0x100000, the Launcher platform base; app1, app, ota_1, 0x110000, 0x100000, sample app 1; app2, app, ota_2, 0x210000, 0x100000, sample app 2; storage, data, spiffs, 0x310000, 0xe0000, application manifest and staging area for uploaded packages. Three details deserve emphasis here. The otadata partition must exist and be at least 0x2000 in size. The ESP-IDF bootloader reads otadata to decide which partition to boot; in essence it is 'reading the signboard by the door at startup'. All offsets are aligned to 0

x10000 (64 KB). I have seen many people casually write an offset like 0x1c000 — an address that 'looks fine' — only to run into strange problems at jump time; I'll cover that pitfall in a dedicated section later. Use ota_1 and ota_2 — not the custom subtype — for application partitions: the reason is that the key API esp_ota_set_boot_partition() only recognizes OTA slots and flatly rejects partitions with a custom subtype. 2.2 The boot manager decides 'what you see at startup'. The Launcher is the firmware I place in the factory partition, and its job is simple: scan all APP-type partitions, read the application manifest stored in storage, display the 'installed applications', and then wait for the user to choose. The Launcher itself needs no complex UI. My first version used just one button and one OLED screen: the button cycled through app names and a long press confirmed the launch. I later added an LVGL version, but the core logic was identical. The most important point is that the Launcher cannot 'start' an app — it can only set the partition to boot next and then reboot. This 'reset-level switch' is fundamentally different from a phone app's process-level switch, but users don't care about

the underlying difference as long as the experience is smooth. // Boot manager core: set the target partition as the next boot partition static esp_err_t launch_app(const esp_partition_t* app_partition) { esp_err_t err esp_ota_set_boot_partition(app_partition); if (err ! ESP_OK) { ESP_LOGE(launcher, set boot partition failed: %s, esp_err_to_name(err)); return err; } esp_restart(); return ESP_OK; // never reaches here }2.3 The application manifest: letting the ESP32 know your apps. I used a simple text-format file for the application manifest, manifest.bin, stored in the storage partition. The reason I didn't use JSON is that early on I got burned by cJSON parsing — insufficient

memory and parsing failures; in embedded scenarios, an ultra-minimal 'one key-value pair per line' format is actually the most robust. magicESPA format_version1 app_idled_demo nameLED Demo partitionapp1 app_version0.1 crc320x9e28b930 The fields mean: app_id is the app's globally unique identifier; partition is which partition it is installed into and must match the label in the partition table; crc32 is a hash of the firmware binary content used for verification; the rest is display information. At startup the Launcher reads line by line and only accepts files beginning with 'magicESPA'; everything else is treated as an invalid manifest. The benefit of this approach is that the manifest is separated from the firmware — uninstalling an app only requires deleting its entry from the manifest, with no code changes at all. 2.4 The 'switching protocol' between apps: jumping and returning to the platform. There is one bottom line in platformization: there must be a loosely coupled switching protocol between the apps and the Launcher. I didn't want apps to have to link against the Launcher's library just to return to the platform, so the protocol is kept at the level of 'two Flash regions'. When the Launcher needs to launch

an app, it calls esp_ota_set_boot_partition(app partition) and then esp_restart(); when the app wants to return to the platform it does the same thing, only with factory as the target. Describing the complete flow in words: power on — bootloader reads otadata — finds it points to factory — loads the Launcher; the Launcher reads the manifest — OLED/serial shows the app list; the user selects 'LED Demo' — the Launcher writes otadata pointing to app1 — reboot; bootloader reads otadata — finds it points to app1 — loads the LED Demo firmware; inside LED Demo, one press of 'return to platform' — writes otadata pointing to factory — reboot — and the cycle repeats. This protocol looks ridiculously simple, but that is precisely the charm of 'reset-level switching': the app doesn't need to know how the Launcher runs, and the Launcher doesn't need to understand the app's internal logic. The only medium of exchange between the two is a block of otadata and a short

NVS flag. 3. What 'installation' actually does: app package format and three distribution channels. 3.1 The essence of installation. Many people's first reaction is to ask 'which command do I use to install?' In fact the essence of installation is very plain: write the app firmware into the corresponding partition, then write a manifest record in storage. Compared with a phone installing an APK — 'unpack resources, register components, security checks' — the installation process on an embedded platform is little more than moving files. The real difficulty lies in reliability: what if power is cut halfway through the write? What if the firmware is corrupted but otadata has already been switched over? So I designed a .espapp application package format, borrowing the simplest parts of APK. Field / length / description: magic number, 4 bytes, ESPA; format version, 2 bytes, currently 1; partition name, 16 bytes, target partition label, zero-padded if short; firmware length, 4 bytes, number of bytes of firmware data that follow; CRC32, 4 bytes, CRC32 computed over the firmware data; firmware data, variable length, raw app.bin data. After the installer receives a .espapp file, it first parses the header and verifies the CRC, then streams the firmware into the spare area of the target partition in a 'write as you receive' fashion, and only switches otadata after everything has been written. This sequence of 'write first, verify, then switch'

is the lifeblood of the entire platform. 3.2 Channel one: installation at flash time, the most common in development. When developing an app, the most direct method is to skip the installer and use a flashing tool to write the app firmware straight into the corresponding partition. I normally use esptool.py: esptool.py --chip esp32s3 -p /dev/ttyACM0 write_flash 0x110000 app1.bin. Note that the 0x110000 here must exactly match the app1 offset in the partition table. If you use the graphical Flash Download Tool, you must likewise enter the correct address manually. 'Installation failures' at this stage are mostly caused by a wrong address or by verification failures because Flash encryption / secure boot were not disabled. 3.3 Channel two: wireless installation over the LAN, the heart of the productized experience. If you call it 'installing an app', relying purely on a USB cable somehow feels incomplete. I built a simplified SoftAP + HTTP installation mode into the Launcher: enter installation mode by pressing a certain GPIO, the ESP32 opens a hotspot named ESPApp, a phone browser visits 192.168.4.1, selects a .es

papp file to upload, and the HTTP server streams it into the target partition. There is a key memory-level point here: the ESP32 has only a few hundred KB of available RAM, so you cannot read the whole app package into memory and then write it to Flash. You must use esp_partition_write() to write block by block, flushing to Flash every 4 KB received. My first version lazily read the entire package into the heap — a 2 MB app crashed the system outright — and it only became stable after I switched to streaming writes. WiFi provisioning is also a watershed for the actual experience: I recommend using the wifi_provisioning component rather than writing your own web-based provisioning page. 3.4 Channel three: SD card or file-system installation, the least hassle for on-site demos. If the device supports an SD card, or if you're willing to set aside part of storage for a path, the easiest automated installation method is to drop the .espapp file into the /install directory and have the Launcher scan that directory at boot, install automatically, and log the result. I used exactly this approach in a trade-show demo machine project: on-site staff only needed to plug in an SD card and the device would automatically update its app list — no computer or specialized know-how required. 3.5 The installer's rollback and power-loss pro-

tection: however good the 'installation experience' is, a power cut halfway through an install can undo all your work. The rollback strategy I use is dual-bank: to switch to a new app, first write it into the spare partition — for example, if app1 is currently in use, write into the space for app2 — then, once everything is written, recompute the CRC and confirm it matches the header, switch otadata to the new partition, and at the same time write a 'boot_ok 0' flag in the NVS region corresponding to the new partition. After reboot, if the new app reports 'boot_ok 1' within 10 seconds, the installation is officially complete; if 10 seconds time out, a watchdog on the Launcher side automatically switches otadata back to the last usable partition and reboots. This is essentially a minimal version of a PC 'system restore point'. It cannot cover every accident, but it is enough to deal with the two most common problems: 'power pulled midway through transfer' and 'new firmware crashes immediately on startup'. 4. A concrete, from-scratch record of getting an ESP32 application platform running. 4.1 Preparing hardware and environment. I used an ESP32-S3 DevKitC with 4 MB Flash; ESP32 and ESP32-C3 work just as well, though the partition sizes need to be adjusted accordingly. For the development environment I used ESP-IDF v5.x — installation and configuration follow the official tutorial, and there's nothing special about this

step. The only extra thing needed is a Python environment, to run the packaging script and esptool. Once the environment is ready, plan the partition table as a whole first, then develop the two projects — Launcher and app — separately. I strongly recommend managing the two projects in separate directories, because their build targets are completely different and mixing them into one project is a nightmare. 4.2 Partition table configuration example. In the Launcher project, create partitions_4mb.csv with the content being the table I sketched earlier. Note that because I reserved boot space for Flash-encryption-related components, in the actual build you need to specify the partition table file path in menuconfig: # Name, Type, SubType, Offset, Size, Flags nvs, data, nvs, 0x9000, 0x6000, otadata, data, ota, 0xf000, 0x2000, factory, app, factory, 0x10000, 0x100000, app1, app, ota_1, 0x1100

00, 0x100000, app2, app, ota_2, 0x210000, 0x100000, storage, data, spiffs, 0x310000, 0xe0000. After writing the partition table, my habit is to first verify it with the command idf.py partition-table --print, which prints the actual on-disk addresses so you can check that the offsets in the CSV match what the tool parsed. Many so-called 'mystical boot failures' can be caught right at that step. 4.3 Writing the boot manager's core code. The Launcher's code is not complicated; the core is three things: mount the storage partition, scan the app partitions, and write otadata and reboot. #include esp_ota_ops.h #include esp_partition.h #include esp_littlefs.h static const char* MANIFEST_PATH /storage/manifest.bin; // read

暂无评论,快来发表第一条评论吧!No comments yet. Be the first!

📮 需求咨询📮 Inquiry