commit b9d02b07b57c200da8b160b02ddc0416f247101f
parent b58453f9e706dc865af1fa0f16c13d28cb1b2b23
Author: Daniel Barlow <dan@telent.net>
Date: Mon, 19 Jan 2026 21:09:58 +0000
OTA support (replace stub)
Diffstat:
2 files changed, 29 insertions(+), 9 deletions(-)
diff --git a/ecu-esp32/src/main.rs b/ecu-esp32/src/main.rs
@@ -66,7 +66,7 @@ async fn main(s : Spawner) {
);
join(
- crate::wifi::start_wifi(&s, peripherals.WIFI, &mut ecu),
+ crate::wifi::start_wifi(&s, peripherals.WIFI, peripherals.FLASH, &mut ecu),
async {
loop {
Timer::after_millis(1000).await;
diff --git a/ecu-esp32/src/wifi.rs b/ecu-esp32/src/wifi.rs
@@ -33,6 +33,8 @@ use esp_radio::{
WifiStaState,
},
};
+use esp_hal_ota::Ota;
+use esp_storage::FlashStorage;
use embassy_net::Stack;
@@ -89,6 +91,7 @@ fn dns_records(my_ip_address: [u8;4], serial: Option<usize>) -> Vec<mdns::Resour
pub async fn start_wifi(spawner: &Spawner,
wifi_peripheral: esp_hal::peripherals::WIFI<'static>,
+ flash_peripheral: esp_hal::peripherals::FLASH<'static>,
ecu: &mut crate::ecu::Ecu<'_>) {
@@ -135,10 +138,11 @@ pub async fn start_wifi(spawner: &Spawner,
}
let mut serial = None;
+
join(
join(
crate::streamer::loop_udp_thing(stack, ecu),
- run_tcp_handler(stack),
+ run_tcp_handler(stack, flash_peripheral),
),
async {
loop {
@@ -231,16 +235,30 @@ async fn read_all(socket: &mut embassy_net::tcp::TcpSocket<'_>,
n
}
-async fn write_ota(sock: &mut embassy_net::tcp::TcpSocket<'_>, len: usize) {
+async fn write_ota(ota : &mut Ota<FlashStorage<'_>>,
+ sock: &mut embassy_net::tcp::TcpSocket<'_>,
+ len: u32) {
let mut buf = [0u8; 4096];
- // let mut ota = esp_ota::OtaUpdate::begin()?;
+
+ ota.ota_begin(len, 0);
+
let mut len = len;
while len > 0 {
match sock.read(&mut buf).await {
Ok(chunksize) => {
- info!("{} bytes to write to flash", chunksize);
- // ota.write(&buf[0..chunksize])?;
- len = len - chunksize;
+ info!("writing {} bytes, {} remaining", chunksize, len);
+ let res = ota.ota_write_chunk(&buf[0..chunksize]);
+ if res == Ok(true) {
+ info!("eof on write, remaining bytes = {} ", len);
+
+ if ota.ota_flush(false, false).is_ok() { // ignore crc, no rollback
+ Timer::after_millis(1000).await;
+ esp_hal::system::software_reset();
+ }
+ }
+ // let progress = (ota.get_ota_progress() * 100.0) as u8;
+ // log::info!("progress: {}%", progress);
+ len = len - (chunksize as u32);
},
Err(x) => {
warn!("error reading ota image: {:?}", x);
@@ -260,10 +278,12 @@ fn register_session_key(key : &[u8]) {
}
-async fn run_tcp_handler(stack : Stack<'_>) {
+async fn run_tcp_handler(stack : Stack<'_>, flash_peripheral: esp_hal::peripherals::FLASH<'static>) {
let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096];
let mut buf = [0u8; 68];
+ let flash = FlashStorage::new(flash_peripheral);
+ let mut ota = Ota::new(flash).unwrap();
loop {
let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
@@ -287,7 +307,7 @@ async fn run_tcp_handler(stack : Stack<'_>) {
let _n = read_all(&mut socket, &mut len_bytes[..], 4).await;
let len = u32::from_be_bytes(len_bytes);
info!("reading {} bytes", len);
- write_ota(&mut socket, len as usize).await;
+ write_ota(&mut ota, &mut socket, len).await;
info!("write {:?}", socket.write(b"BYE\n").await);
socket.flush().await;
socket.close();