eculocate

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 7cf21b42f02e5e34933bead7a267b70cbbfd0649
parent a932717f67c60a89ff669f98b7fe2756b39c44eb
Author: filipton <filipton12@gmail.com>
Date:   Fri,  5 Jul 2024 14:00:20 +0200

docs: simplify example in README

Diffstat:
MREADME.md | 51++++++++++++++-------------------------------------
Msrc/lib.rs | 5++++-
2 files changed, 18 insertions(+), 38 deletions(-)

diff --git a/README.md b/README.md @@ -11,54 +11,31 @@ For now only works on esp32s3 (esp32c3 in the near future). - CRC32 verification ## Example -This example uses embassy-net for TcpSocket, as you can see ota doesn't use -async so you can easily use it with smoltcp. +To see real-world example look at `./examples` and `./simple-ota-server` dirs. ```rust -let mut ota_buff = [0; 4096]; -socket - .read(&mut ota_buff[..4]) - .await - .expect("Cannot read firmware size!"); - -let flash_size = u32::from_le_bytes(ota_buff[..4].try_into().unwrap()); - -socket - .read(&mut ota_buff[..4]) - .await - .expect("Cannot read target crc!"); -let target_crc = u32::from_le_bytes(ota_buff[..4].try_into().unwrap()); -log::info!("flash_size: {flash_size}"); -log::info!("target_crc: {target_crc}"); +let flash_size = 1234; // get it from OTA server +let target_crc = 65436743; // get it from OTA server let mut ota = Ota::new(FlashStorage::new()).unwrap(); ota.ota_begin(flash_size, target_crc); -let mut bytes_read = 0; +let mut buf = [0; 4096]; loop { - let res = socket.read(&mut ota_buff).await; - if let Ok(n) = res { - bytes_read += n; - if n == 0 { - break; - } - - let res = ota.ota_write_chunk(&ota_buff[..n]); - if bytes_read % 4096 == 0 { - _ = socket.write(&[0]).await; - } - - if res == Ok(true) { - if ota.ota_flush(true).is_ok() { - // Here just reboot or sth! - } + let n = read_next_chunk(&mut buf); + if n == 0 { + break; + } - break; + let res = ota.ota_write_chunk(&buf[..n]); + if res == Ok(true) { // end of flash + if ota.ota_flush(true).is_ok() { // true if you want to verify crc reading flash + esp_hal::reset::software_reset(); } } - Timer::after_millis(10).await; - log::info!("Progress: {}%", (ota.get_ota_progress() * 100.0) as u8); + let progress = (ota.get_ota_progress() * 100) as u8; + log::info!("progress: {}%", progress); } ``` diff --git a/src/lib.rs b/src/lib.rs @@ -51,7 +51,10 @@ where { pub fn new(mut flash: S) -> Result<Self, ()> { if let Some(pinfo) = Self::read_partitions(&mut flash) { - log::info!("parts: {:?}", &pinfo); + if pinfo.ota_partitions_count < 2 { + log::error!("Not enough OTA partitions! (>= 2)"); + return Err(()); // not enough partitions + } return Ok(Ota { flash,