|
| 1 | +//! OTA Update Example |
| 2 | +//! |
| 3 | +//! This shows the basics of dealing with partitions and changing the active partition. |
| 4 | +//! For simplicity it will flash an application image embedded into the binary. |
| 5 | +//! In a real world application you can get the image via HTTP(S), UART or from an sd-card etc. |
| 6 | +//! |
| 7 | +//! Adjust the target and the chip in the following commands according to the chip used! |
| 8 | +//! |
| 9 | +//! - `cargo xtask build examples examples esp32 --example=gpio_interrupt` |
| 10 | +//! - `espflash save-image --chip=esp32 examples/target/xtensa-esp32-none-elf/release/gpio_interrupt examples/target/ota_image` |
| 11 | +//! - `cargo xtask build examples examples esp32 --example=ota_update` |
| 12 | +//! - `espflash save-image --chip=esp32 examples/target/xtensa-esp32-none-elf/release/ota_update examples/target/ota_image` |
| 13 | +//! - erase whole flash via `espflash erase-flash` (this is to make sure otadata is cleared and no code is flashed to any partition) |
| 14 | +//! - run via `cargo xtask run example examples esp32 --example=ota_update` |
| 15 | +//! |
| 16 | +//! On first boot notice the firmware partition gets booted ("Loaded app from partition at offset 0x10000"). |
| 17 | +//! Press the BOOT button, once finished press the RESET button. |
| 18 | +//! |
| 19 | +//! Notice OTA0 gets booted ("Loaded app from partition at offset 0x110000"). |
| 20 | +//! |
| 21 | +//! Once again press BOOT, when finished press RESET. |
| 22 | +//! You will see the `gpio_interrupt` example gets booted from OTA1 ("Loaded app from partition at offset 0x210000") |
| 23 | +//! |
| 24 | +//! See https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/ota.html |
| 25 | +
|
| 26 | +//% FEATURES: esp-storage esp-hal/unstable |
| 27 | +//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3 |
| 28 | + |
| 29 | +#![no_std] |
| 30 | +#![no_main] |
| 31 | + |
| 32 | +use embedded_storage::Storage; |
| 33 | +use esp_backtrace as _; |
| 34 | +use esp_bootloader_esp_idf::{ |
| 35 | + ota::Slot, |
| 36 | + partitions::{self, AppPartitionSubType, DataPartitionSubType}, |
| 37 | +}; |
| 38 | +use esp_hal::{ |
| 39 | + gpio::{Input, InputConfig, Pull}, |
| 40 | + main, |
| 41 | +}; |
| 42 | +use esp_println::println; |
| 43 | + |
| 44 | +esp_bootloader_esp_idf::esp_app_desc!(); |
| 45 | + |
| 46 | +static OTA_IMAGE: &[u8] = include_bytes!("../../target/ota_image"); |
| 47 | + |
| 48 | +#[main] |
| 49 | +fn main() -> ! { |
| 50 | + let peripherals = esp_hal::init(esp_hal::Config::default()); |
| 51 | + |
| 52 | + let mut storage = esp_storage::FlashStorage::new(); |
| 53 | + |
| 54 | + let mut buffer = [0u8; esp_bootloader_esp_idf::partitions::PARTITION_TABLE_MAX_LEN]; |
| 55 | + let pt = esp_bootloader_esp_idf::partitions::read_partition_table(&mut storage, &mut buffer) |
| 56 | + .unwrap(); |
| 57 | + |
| 58 | + // List all partitions - this is just FYI |
| 59 | + for i in 0..pt.len() { |
| 60 | + println!("{:?}", pt.get_partition(i)); |
| 61 | + } |
| 62 | + |
| 63 | + // Find the OTA-data partition and show the currently active partition |
| 64 | + let ota_part = pt |
| 65 | + .find_partition(esp_bootloader_esp_idf::partitions::PartitionType::Data( |
| 66 | + DataPartitionSubType::Ota, |
| 67 | + )) |
| 68 | + .unwrap() |
| 69 | + .unwrap(); |
| 70 | + let mut ota_part = ota_part.as_embedded_storage(&mut storage); |
| 71 | + println!("Found ota data"); |
| 72 | + |
| 73 | + let mut ota = esp_bootloader_esp_idf::ota::Ota::new(&mut ota_part).unwrap(); |
| 74 | + let current = ota.current_slot().unwrap(); |
| 75 | + println!( |
| 76 | + "current image state {:?} (only relevant if the bootloader was built with auto-rollback support)", |
| 77 | + ota.current_ota_state() |
| 78 | + ); |
| 79 | + println!("current {:?} - next {:?}", current, current.next()); |
| 80 | + |
| 81 | + // Mark the current slot as VALID - this is only needed if the bootloader was built with auto-rollback support. |
| 82 | + // The default pre-compiled bootloader in espflash is NOT. |
| 83 | + if ota.current_slot().unwrap() != Slot::None |
| 84 | + && (ota.current_ota_state().unwrap() == esp_bootloader_esp_idf::ota::OtaImageState::New |
| 85 | + || ota.current_ota_state().unwrap() |
| 86 | + == esp_bootloader_esp_idf::ota::OtaImageState::PendingVerify) |
| 87 | + { |
| 88 | + println!("Changed state to VALID"); |
| 89 | + ota.set_current_ota_state(esp_bootloader_esp_idf::ota::OtaImageState::Valid) |
| 90 | + .unwrap(); |
| 91 | + } |
| 92 | + |
| 93 | + cfg_if::cfg_if! { |
| 94 | + if #[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))] { |
| 95 | + let button = peripherals.GPIO0; |
| 96 | + } else { |
| 97 | + let button = peripherals.GPIO9; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + let boot_button = Input::new(button, InputConfig::default().with_pull(Pull::Up)); |
| 102 | + |
| 103 | + println!("Press boot button to flash and switch to the next OTA slot"); |
| 104 | + let mut done = false; |
| 105 | + loop { |
| 106 | + if boot_button.is_low() && !done { |
| 107 | + done = true; |
| 108 | + |
| 109 | + let next_slot = current.next(); |
| 110 | + |
| 111 | + println!("Flashing image to {:?}", next_slot); |
| 112 | + |
| 113 | + // find the target app partition |
| 114 | + let next_app_partition = match next_slot { |
| 115 | + Slot::None => { |
| 116 | + // None is FACTORY if present, OTA0 otherwise |
| 117 | + pt.find_partition(partitions::PartitionType::App(AppPartitionSubType::Factory)) |
| 118 | + .or_else(|_| { |
| 119 | + pt.find_partition(partitions::PartitionType::App( |
| 120 | + AppPartitionSubType::Ota0, |
| 121 | + )) |
| 122 | + }) |
| 123 | + .unwrap() |
| 124 | + } |
| 125 | + Slot::Slot0 => pt |
| 126 | + .find_partition(partitions::PartitionType::App(AppPartitionSubType::Ota0)) |
| 127 | + .unwrap(), |
| 128 | + Slot::Slot1 => pt |
| 129 | + .find_partition(partitions::PartitionType::App(AppPartitionSubType::Ota1)) |
| 130 | + .unwrap(), |
| 131 | + } |
| 132 | + .unwrap(); |
| 133 | + println!("Found partition: {:?}", next_app_partition); |
| 134 | + let mut next_app_partition = next_app_partition.as_embedded_storage(&mut storage); |
| 135 | + |
| 136 | + // write to the app partition |
| 137 | + for (sector, chunk) in OTA_IMAGE.chunks(4096).enumerate() { |
| 138 | + println!("Writing sector {sector}..."); |
| 139 | + next_app_partition |
| 140 | + .write((sector * 4096) as u32, chunk) |
| 141 | + .unwrap(); |
| 142 | + } |
| 143 | + |
| 144 | + println!("Changing OTA slot and setting the state to NEW"); |
| 145 | + let ota_part = pt |
| 146 | + .find_partition(esp_bootloader_esp_idf::partitions::PartitionType::Data( |
| 147 | + DataPartitionSubType::Ota, |
| 148 | + )) |
| 149 | + .unwrap() |
| 150 | + .unwrap(); |
| 151 | + let mut ota_part = ota_part.as_embedded_storage(&mut storage); |
| 152 | + let mut ota = esp_bootloader_esp_idf::ota::Ota::new(&mut ota_part).unwrap(); |
| 153 | + ota.set_current_slot(next_slot).unwrap(); |
| 154 | + ota.set_current_ota_state(esp_bootloader_esp_idf::ota::OtaImageState::New) |
| 155 | + .unwrap(); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments