ESP32 的 Flash 可以划分出多个应用分区(如 ota_0、ota_1,甚至更多)。OTA 机制的核心是一个叫 otadata 的小数据分区,它记录了“下一次启动应该从哪个应用分区加载程序”。
切换不同应用的思路是:
- 准备多个应用:把不同 Arduino 程序编译成多个 .bin 文件,分别烧录到不同的 OTA 分区里。
- 控制启动分区:在代码中调用 ESP-IDF 的底层函数 esp_ota_set_boot_partition(),把想运行的那个应用分区设置为“下次启动目标”,然后重启(esp_restart())。这样设备重启后,Bootloader 就会加载指定的那个应用了。
arduino的分区表
partitions.csv:
1 2 3 4 5 6 7 8
| nvs, data, nvs, 0x9000, 0x5000, otadata, data, ota, 0xe000, 0x2000, app0, app, ota_0, 0x10000, 0x300000, app1, app, ota_1, 0x310000,0x300000, spiffs, data, spiffs, 0x610000,0x9E0000, coredump, data, coredump,0xFF0000,0x10000,
|
arduino应用通常刷进app0分区,app1作ota使用。
在这里,把第二个应用刷进app1分区。
刷机方法:
1
| esptool --chip esp32s3 --port COM3 --baud 460800 write_flash 0x310000 eReader.ino.bin
|
COM3是esp32s3连接端口,按实际写。
在C:\Users\用户名\AppData\Local\arduino\sketches\75916FF70044A6E79BDE29D86B809A7F 这样的目录可以找到arduino编译好的bin文件,只要和ino同名的.bin。
esptool的安装:
模块化
app_switch.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| #ifndef APP_SWITCH_H #define APP_SWITCH_H
#include <Arduino.h> #include "esp_ota_ops.h" #include "esp_partition.h" #include "esp_system.h"
#ifndef APP_SWITCH_PIN_BOOT #define APP_SWITCH_PIN_BOOT 0 #endif #ifndef APP_SWITCH_PIN_WHEEL #define APP_SWITCH_PIN_WHEEL 5 #endif #ifndef APP_SWITCH_HOLD_MS #define APP_SWITCH_HOLD_MS 3000 #endif #ifndef APP_SWITCH_DEBUG #define APP_SWITCH_DEBUG 1 #endif
static uint32_t _appSwitchPressStart = 0;
inline bool appSwitchPartitionHasApp(const esp_partition_t *p) { if (!p) return false; uint8_t magic = 0; if (esp_partition_read(p, 0, &magic, 1) != ESP_OK) return false; return magic == 0xE9; }
inline void appSwitchTryOther() { const esp_partition_t *ota0 = esp_partition_find_first( ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_0, NULL); const esp_partition_t *ota1 = esp_partition_find_first( ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL);
if (!ota0 || !ota1) { #if APP_SWITCH_DEBUG Serial.println("[app_switch] 缺少 OTA 分区"); #endif return; } if (!appSwitchPartitionHasApp(ota0) || !appSwitchPartitionHasApp(ota1)) { #if APP_SWITCH_DEBUG Serial.println("[app_switch] 两个应用槽都需有效固件,取消切换"); #endif return; }
const esp_partition_t *running = esp_ota_get_running_partition(); const esp_partition_t *target = (running->subtype == ESP_PARTITION_SUBTYPE_APP_OTA_0) ? ota1 : ota0;
#if APP_SWITCH_DEBUG Serial.printf("[app_switch] 切换 %s -> %s\n", running->label, target->label); #endif
if (esp_ota_set_boot_partition(target) != ESP_OK) { #if APP_SWITCH_DEBUG Serial.println("[app_switch] 设置启动分区失败"); #endif return; }
esp_restart(); }
inline void appSwitchPoll() { bool both = (digitalRead(APP_SWITCH_PIN_BOOT) == LOW) && (digitalRead(APP_SWITCH_PIN_WHEEL) == LOW);
if (!both) { _appSwitchPressStart = 0; } else { if (_appSwitchPressStart == 0) _appSwitchPressStart = millis(); if (millis() - _appSwitchPressStart >= APP_SWITCH_HOLD_MS) { _appSwitchPressStart = 0; appSwitchTryOther(); } } }
inline void appSwitchBegin() { pinMode(APP_SWITCH_PIN_BOOT, INPUT_PULLUP); pinMode(APP_SWITCH_PIN_WHEEL, INPUT_PULLUP); #if APP_SWITCH_DEBUG const esp_partition_t *running = esp_ota_get_running_partition(); Serial.printf("[app_switch] 运行在 %s (subtype=0x%02x)\n", running->label, running->subtype); #endif }
#endif
|
使用方法
在上面的.h文件里就有说明(为了方便使用,每一个模块化文件都在头部放置使用说明)。
在程序运行时,同时长按拨轮和BOOT键5秒就会在app0和app1里的两个应用之间切换。
如果长按BOOT还有别的用途,比如配网,就把配网的超时改长,超过应用切换的时间即可。