new ota logic
This commit is contained in:
@@ -10,7 +10,7 @@ use crate::hal::little_fs2storage_adapter::LittleFs2Filesystem;
|
||||
use crate::hal::shared_flash::MutexFlashStorage;
|
||||
use alloc::string::ToString;
|
||||
use alloc::sync::Arc;
|
||||
use alloc::{format, string::String, vec, vec::Vec};
|
||||
use alloc::{format, string::String, vec::Vec};
|
||||
use core::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use core::str::FromStr;
|
||||
use core::sync::atomic::Ordering;
|
||||
@@ -21,11 +21,6 @@ use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::mutex::{Mutex, MutexGuard};
|
||||
use embassy_sync::once_lock::OnceLock;
|
||||
use embassy_time::{Duration, Timer, WithTimeout};
|
||||
use embedded_storage::nor_flash::{check_erase, NorFlash, ReadNorFlash};
|
||||
use esp_bootloader_esp_idf::ota::OtaImageState::Valid;
|
||||
use esp_bootloader_esp_idf::ota::{Ota, OtaImageState};
|
||||
use esp_bootloader_esp_idf::partitions::{AppPartitionSubType, FlashRegion};
|
||||
use esp_hal::Blocking;
|
||||
use esp_hal::gpio::{Input, RtcPinWithResistors};
|
||||
use esp_hal::rng::Rng;
|
||||
use esp_hal::rtc_cntl::{
|
||||
@@ -34,6 +29,8 @@ use esp_hal::rtc_cntl::{
|
||||
};
|
||||
use esp_hal::system::software_reset;
|
||||
use esp_hal::uart::Uart;
|
||||
use esp_hal::Blocking;
|
||||
use esp_hal_ota::Ota;
|
||||
use esp_println::println;
|
||||
use esp_radio::wifi::{
|
||||
AccessPointConfig, AccessPointInfo, AuthMethod, ClientConfig, ModeConfig, ScanConfig,
|
||||
@@ -129,12 +126,7 @@ pub struct Esp<'a> {
|
||||
// RTC-capable GPIO used as external wake source (store the raw peripheral)
|
||||
pub wake_gpio1: esp_hal::peripherals::GPIO1<'static>,
|
||||
pub uart0: Uart<'a, Blocking>,
|
||||
|
||||
pub ota: Ota<'static, MutexFlashStorage>,
|
||||
pub ota_target: &'static mut FlashRegion<'static, MutexFlashStorage>,
|
||||
pub current: AppPartitionSubType,
|
||||
pub slot0_state: OtaImageState,
|
||||
pub slot1_state: OtaImageState,
|
||||
pub ota: &'static mut Ota<&'static mut MutexFlashStorage>,
|
||||
}
|
||||
|
||||
// SAFETY: On this target we never move Esp across OS threads; the firmware runs single-core
|
||||
@@ -155,7 +147,6 @@ macro_rules! mk_static {
|
||||
}
|
||||
|
||||
impl Esp<'_> {
|
||||
|
||||
pub(crate) async fn read_serial_line(&mut self) -> FatResult<Option<alloc::string::String>> {
|
||||
let mut buf = [0u8; 1];
|
||||
let mut line = String::new();
|
||||
@@ -171,7 +162,7 @@ impl Esp<'_> {
|
||||
}
|
||||
line.push(c);
|
||||
}
|
||||
Err(error ) => {
|
||||
Err(error) => {
|
||||
if line.is_empty() {
|
||||
return Ok(None);
|
||||
} else {
|
||||
@@ -250,50 +241,6 @@ impl Esp<'_> {
|
||||
Ok((buf, read))
|
||||
}
|
||||
|
||||
pub(crate) async fn write_ota(&mut self, offset: u32, buf: &[u8]) -> Result<(), FatError> {
|
||||
let _ = check_erase(self.ota_target, offset, offset + 4096);
|
||||
self.ota_target.erase(offset, offset + 4096)?;
|
||||
|
||||
let mut temp = vec![0; buf.len()];
|
||||
let read_back = temp.as_mut_slice();
|
||||
//change to nor flash, align writes!
|
||||
self.ota_target.write(offset, buf)?;
|
||||
self.ota_target.read(offset, read_back)?;
|
||||
if buf != read_back {
|
||||
info!("Expected {buf:?} but got {read_back:?}");
|
||||
bail!(
|
||||
"Flash error, read back does not match write buffer at offset {:x}",
|
||||
offset
|
||||
)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn finalize_ota(&mut self) -> Result<(), FatError> {
|
||||
let current = self.ota.current_app_partition()?;
|
||||
if self.ota.current_ota_state()? != Valid {
|
||||
info!("Validating current slot {current:?} as it was able to ota");
|
||||
self.ota.set_current_ota_state(Valid)?;
|
||||
}
|
||||
let next = match current {
|
||||
AppPartitionSubType::Ota0 => AppPartitionSubType::Ota1,
|
||||
AppPartitionSubType::Ota1 => AppPartitionSubType::Ota0,
|
||||
_ => {
|
||||
bail!("Invalid current slot {current:?} for ota");
|
||||
}
|
||||
};
|
||||
self.ota.set_current_app_partition(next)?;
|
||||
info!("switched slot");
|
||||
self.ota.set_current_ota_state(OtaImageState::New)?;
|
||||
info!("switched state for new partition");
|
||||
let state_new = self.ota.current_ota_state()?;
|
||||
info!("state on new partition now {state_new:?}");
|
||||
//determine nextslot crc
|
||||
|
||||
self.set_restart_to_conf(true);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn mode_override_pressed(&mut self) -> bool {
|
||||
self.boot_button.is_low()
|
||||
}
|
||||
@@ -594,14 +541,7 @@ impl Esp<'_> {
|
||||
duration_in_ms: u64,
|
||||
mut rtc: MutexGuard<CriticalSectionRawMutex, Rtc>,
|
||||
) -> ! {
|
||||
// Mark the current OTA image as valid if we reached here while in pending verify.
|
||||
if let Ok(cur) = self.ota.current_ota_state() {
|
||||
if cur == OtaImageState::PendingVerify {
|
||||
self.ota
|
||||
.set_current_ota_state(Valid)
|
||||
.expect("Could not set image to valid");
|
||||
}
|
||||
}
|
||||
//TODO HERE Mark the current OTA image as valid if we reached here while in pending verify.
|
||||
|
||||
if duration_in_ms == 0 {
|
||||
software_reset();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use esp_hal::uart::Config as UartConfig;
|
||||
use lib_bms_protocol::BmsReadable;
|
||||
use esp_hal::uart::{Config as UartConfig};
|
||||
pub(crate) mod battery;
|
||||
// mod can_api; // replaced by external canapi crate
|
||||
pub mod esp;
|
||||
@@ -9,7 +9,6 @@ mod shared_flash;
|
||||
mod v4_hal;
|
||||
mod water;
|
||||
|
||||
use lib_bms_protocol::ProtocolVersion;
|
||||
use crate::alloc::string::ToString;
|
||||
use crate::hal::rtc::{DS3231Module, RTCModuleInteraction};
|
||||
use esp_hal::peripherals::Peripherals;
|
||||
@@ -36,6 +35,7 @@ use esp_hal::peripherals::GPIO6;
|
||||
use esp_hal::peripherals::GPIO7;
|
||||
use esp_hal::peripherals::GPIO8;
|
||||
use esp_hal::peripherals::TWAI0;
|
||||
use lib_bms_protocol::ProtocolVersion;
|
||||
|
||||
use crate::{
|
||||
bail,
|
||||
@@ -65,24 +65,20 @@ use eeprom24x::{Eeprom24x, SlaveAddr, Storage};
|
||||
use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice;
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::blocking_mutex::CriticalSectionMutex;
|
||||
use esp_bootloader_esp_idf::partitions::{
|
||||
AppPartitionSubType, DataPartitionSubType, FlashRegion, PartitionEntry,
|
||||
};
|
||||
use esp_bootloader_esp_idf::partitions::{DataPartitionSubType, FlashRegion, PartitionEntry};
|
||||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::gpio::{Input, InputConfig, Pull};
|
||||
use measurements::{Current, Voltage};
|
||||
|
||||
use crate::fat_error::{ContextExt, FatError, FatResult};
|
||||
use crate::hal::battery::{WCHI2CSlave};
|
||||
use crate::fat_error::{FatError, FatResult};
|
||||
use crate::hal::battery::WCHI2CSlave;
|
||||
use crate::hal::little_fs2storage_adapter::LittleFs2Filesystem;
|
||||
use crate::hal::water::TankSensor;
|
||||
use crate::log::LOG_ACCESS;
|
||||
use embassy_sync::mutex::Mutex;
|
||||
use embassy_sync::once_lock::OnceLock;
|
||||
use embedded_storage::ReadStorage;
|
||||
use esp_alloc as _;
|
||||
use esp_backtrace as _;
|
||||
use esp_bootloader_esp_idf::ota::{Ota, OtaImageState};
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::i2c::master::{BusTimeout, Config, I2c};
|
||||
use esp_hal::interrupt::software::SoftwareInterruptControl;
|
||||
@@ -93,8 +89,9 @@ use esp_hal::rtc_cntl::{Rtc, SocResetReason};
|
||||
use esp_hal::system::reset_reason;
|
||||
use esp_hal::time::Rate;
|
||||
use esp_hal::timer::timg::TimerGroup;
|
||||
use esp_hal::Blocking;
|
||||
use esp_hal::uart::Uart;
|
||||
use esp_hal::Blocking;
|
||||
use esp_hal_ota::Ota;
|
||||
use esp_radio::{init, Controller};
|
||||
use esp_storage::FlashStorage;
|
||||
use littlefs2::fs::{Allocation, Filesystem as lfs2Filesystem};
|
||||
@@ -138,12 +135,6 @@ pub struct HAL<'a> {
|
||||
pub board_hal: Box<dyn BoardInteraction<'a> + Send>,
|
||||
}
|
||||
|
||||
pub struct DetectionRequest {
|
||||
pub sensorsa: [Sensor; PLANT_COUNT],
|
||||
pub sensorsb: [Sensor; PLANT_COUNT],
|
||||
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
pub trait BoardInteraction<'a> {
|
||||
fn get_tank_sensor(&mut self) -> Result<&mut TankSensor<'a>, FatError>;
|
||||
@@ -169,7 +160,7 @@ pub trait BoardInteraction<'a> {
|
||||
async fn can_power(&mut self, state: bool) -> FatResult<()>;
|
||||
|
||||
// Return JSON string with autodetected sensors per plant. Default: not supported.
|
||||
async fn detect_sensors(&mut self, request: Detection) -> FatResult<Detection> {
|
||||
async fn detect_sensors(&mut self, _request: Detection) -> FatResult<Detection> {
|
||||
bail!("Autodetection is only available on v4 HAL with CAN bus");
|
||||
}
|
||||
|
||||
@@ -324,51 +315,15 @@ impl PlantHal {
|
||||
let pt =
|
||||
esp_bootloader_esp_idf::partitions::read_partition_table(flash_storage, tablebuffer)?;
|
||||
|
||||
let ota_data = mk_static!(
|
||||
PartitionEntry,
|
||||
pt.find_partition(esp_bootloader_esp_idf::partitions::PartitionType::Data(
|
||||
DataPartitionSubType::Ota,
|
||||
))?
|
||||
.expect("No OTA data partition found")
|
||||
);
|
||||
|
||||
let ota_data = mk_static!(
|
||||
FlashRegion<MutexFlashStorage>,
|
||||
ota_data.as_embedded_storage(flash_storage_2)
|
||||
);
|
||||
|
||||
let state_0 = ota_state(AppPartitionSubType::Ota0, ota_data);
|
||||
let state_1 = ota_state(AppPartitionSubType::Ota1, ota_data);
|
||||
let mut ota = Ota::new(ota_data, 2)?;
|
||||
let running = get_current_slot_and_fix_ota_data(&mut ota, state_0, state_1)?;
|
||||
let target = next_partition(running)?;
|
||||
|
||||
info!("Currently running OTA slot: {running:?}");
|
||||
info!("Slot0 state: {state_0:?}");
|
||||
info!("Slot1 state: {state_1:?}");
|
||||
|
||||
//get current_state and next_state here!
|
||||
let ota_target = match target {
|
||||
AppPartitionSubType::Ota0 => pt
|
||||
.find_partition(esp_bootloader_esp_idf::partitions::PartitionType::App(
|
||||
AppPartitionSubType::Ota0,
|
||||
))?
|
||||
.context("Partition table invalid no ota0")?,
|
||||
AppPartitionSubType::Ota1 => pt
|
||||
.find_partition(esp_bootloader_esp_idf::partitions::PartitionType::App(
|
||||
AppPartitionSubType::Ota1,
|
||||
))?
|
||||
.context("Partition table invalid no ota1")?,
|
||||
_ => {
|
||||
bail!("Invalid target partition");
|
||||
}
|
||||
};
|
||||
|
||||
let ota_target = mk_static!(PartitionEntry, ota_target);
|
||||
let ota_target = mk_static!(
|
||||
FlashRegion<MutexFlashStorage>,
|
||||
ota_target.as_embedded_storage(flash_storage)
|
||||
let ota = mk_static!(
|
||||
Ota<&mut MutexFlashStorage>,
|
||||
Ota::new(flash_storage_2).unwrap()
|
||||
);
|
||||
info!("Ota initialized");
|
||||
let selected = ota.get_currently_booted_partition();
|
||||
let ota_state = ota.get_ota_image_state();
|
||||
info!("Currently running partition slot: {selected:?}");
|
||||
info!("Slot state: {ota_state:?}");
|
||||
|
||||
let data_partition = pt
|
||||
.find_partition(esp_bootloader_esp_idf::partitions::PartitionType::Data(
|
||||
@@ -401,8 +356,8 @@ impl PlantHal {
|
||||
lfs2Filesystem::mount(alloc, lfs2filesystem).expect("Could not mount lfs2 filesystem"),
|
||||
));
|
||||
|
||||
let uart0 = Uart::new(peripherals.UART0, UartConfig::default())
|
||||
.map_err(|_| FatError::String {
|
||||
let uart0 =
|
||||
Uart::new(peripherals.UART0, UartConfig::default()).map_err(|_| FatError::String {
|
||||
error: "Uart creation failed".to_string(),
|
||||
})?;
|
||||
|
||||
@@ -417,11 +372,7 @@ impl PlantHal {
|
||||
boot_button,
|
||||
wake_gpio1,
|
||||
ota,
|
||||
ota_target,
|
||||
current: running,
|
||||
slot0_state: state_0,
|
||||
slot1_state: state_1,
|
||||
uart0
|
||||
uart0,
|
||||
};
|
||||
|
||||
//init,reset rtc memory depending on cause
|
||||
@@ -553,7 +504,7 @@ impl PlantHal {
|
||||
//BoardVersion::V4 => {
|
||||
v4_hal::create_v4(free_pins, esp, config, battery_interaction, rtc_module)
|
||||
.await?;
|
||||
//}
|
||||
//}
|
||||
//};
|
||||
|
||||
HAL { board_hal }
|
||||
@@ -571,9 +522,14 @@ impl PlantHal {
|
||||
)
|
||||
.await;
|
||||
HAL {
|
||||
board_hal: v4_hal::create_v4(free_pins, esp, PlantControllerConfig::default(),
|
||||
Box::new(NoBatteryMonitor {}), rtc_module)
|
||||
.await?
|
||||
board_hal: v4_hal::create_v4(
|
||||
free_pins,
|
||||
esp,
|
||||
PlantControllerConfig::default(),
|
||||
Box::new(NoBatteryMonitor {}),
|
||||
rtc_module,
|
||||
)
|
||||
.await?,
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -582,87 +538,6 @@ impl PlantHal {
|
||||
}
|
||||
}
|
||||
|
||||
fn ota_state(
|
||||
slot: AppPartitionSubType,
|
||||
ota_data: &mut FlashRegion<MutexFlashStorage>,
|
||||
) -> OtaImageState {
|
||||
// Read and log OTA states for both slots before constructing Ota
|
||||
// Each OTA select entry is 32 bytes: [seq:4][label:20][state:4][crc:4]
|
||||
// Offsets within the OTA data partition: slot0 @ 0x0000, slot1 @ 0x1000
|
||||
let mut slot_buf = [0u8; 32];
|
||||
if slot == AppPartitionSubType::Ota0 {
|
||||
let _ = ReadStorage::read(ota_data, 0x0000, &mut slot_buf);
|
||||
} else {
|
||||
let _ = ReadStorage::read(ota_data, 0x1000, &mut slot_buf);
|
||||
}
|
||||
let raw_state = u32::from_le_bytes(slot_buf[24..28].try_into().unwrap_or([0xff; 4]));
|
||||
|
||||
OtaImageState::try_from(raw_state).unwrap_or(OtaImageState::Undefined)
|
||||
}
|
||||
|
||||
fn get_current_slot_and_fix_ota_data(
|
||||
ota: &mut Ota<MutexFlashStorage>,
|
||||
state0: OtaImageState,
|
||||
state1: OtaImageState,
|
||||
) -> Result<AppPartitionSubType, FatError> {
|
||||
let state = ota.current_ota_state().unwrap_or_default();
|
||||
let swap = match state {
|
||||
OtaImageState::Invalid => true,
|
||||
OtaImageState::Aborted => true,
|
||||
OtaImageState::Undefined => {
|
||||
info!("Undefined image in current slot, bootloader wrong?");
|
||||
false
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
let current = ota.current_app_partition()?;
|
||||
if swap {
|
||||
let other = match current {
|
||||
AppPartitionSubType::Ota0 => state1,
|
||||
AppPartitionSubType::Ota1 => state0,
|
||||
_ => OtaImageState::Invalid,
|
||||
};
|
||||
|
||||
match other {
|
||||
OtaImageState::Invalid => {
|
||||
bail!(
|
||||
"cannot recover slot, as both slots in invalid state {:?} {:?} {:?}",
|
||||
current,
|
||||
state0,
|
||||
state1
|
||||
);
|
||||
}
|
||||
OtaImageState::Aborted => {
|
||||
bail!(
|
||||
"cannot recover slot, as both slots in invalid state {:?} {:?} {:?}",
|
||||
current,
|
||||
state0,
|
||||
state1
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
info!("Current slot has state {state:?} other state has {other:?} swapping");
|
||||
let next = next_partition(current)?;
|
||||
ota.set_current_app_partition(next)?;
|
||||
|
||||
//we actually booted other slot, than partition table assumes
|
||||
return Ok(ota.current_app_partition()?);
|
||||
};
|
||||
Ok(current)
|
||||
}
|
||||
|
||||
pub fn next_partition(current: AppPartitionSubType) -> FatResult<AppPartitionSubType> {
|
||||
let next = match current {
|
||||
AppPartitionSubType::Ota0 => AppPartitionSubType::Ota1,
|
||||
AppPartitionSubType::Ota1 => AppPartitionSubType::Ota0,
|
||||
_ => {
|
||||
bail!("Current slot is not ota0 or ota1");
|
||||
}
|
||||
};
|
||||
Ok(next)
|
||||
}
|
||||
|
||||
pub async fn esp_time() -> DateTime<Utc> {
|
||||
let guard = TIME_ACCESS.get().await.lock().await;
|
||||
DateTime::from_timestamp_micros(guard.current_time_us() as i64).unwrap()
|
||||
@@ -699,4 +574,3 @@ pub struct DetectionSensorResult {
|
||||
sensor_a: bool,
|
||||
sensor_b: bool,
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ use core::cell::RefCell;
|
||||
use core::ops::{Deref, DerefMut};
|
||||
use embassy_sync::blocking_mutex::CriticalSectionMutex;
|
||||
use embedded_storage::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
|
||||
use embedded_storage::ReadStorage;
|
||||
use embedded_storage::{ReadStorage, Storage};
|
||||
use esp_storage::{FlashStorage, FlashStorageError};
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -11,6 +11,19 @@ pub struct MutexFlashStorage {
|
||||
pub(crate) inner: Arc<CriticalSectionMutex<RefCell<FlashStorage<'static>>>>,
|
||||
}
|
||||
|
||||
impl Storage for MutexFlashStorage {
|
||||
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
||||
self.inner
|
||||
.lock(|f| NorFlash::write(f.borrow_mut().deref_mut(), offset, bytes))
|
||||
}
|
||||
}
|
||||
|
||||
impl Storage for &mut MutexFlashStorage {
|
||||
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
||||
Storage::write(&mut (**self), offset, bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl ReadStorage for MutexFlashStorage {
|
||||
type Error = FlashStorageError;
|
||||
|
||||
@@ -25,9 +38,29 @@ impl ReadStorage for MutexFlashStorage {
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_storage::Storage for MutexFlashStorage {
|
||||
fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
|
||||
NorFlash::write(self, offset, bytes)
|
||||
impl ReadStorage for &mut MutexFlashStorage {
|
||||
type Error = FlashStorageError;
|
||||
|
||||
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), FlashStorageError> {
|
||||
ReadStorage::read(&mut (**self), offset, bytes)
|
||||
}
|
||||
|
||||
fn capacity(&self) -> usize {
|
||||
ReadStorage::capacity(&(**self))
|
||||
}
|
||||
}
|
||||
|
||||
impl ReadStorage for &MutexFlashStorage {
|
||||
type Error = FlashStorageError;
|
||||
|
||||
fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), FlashStorageError> {
|
||||
self.inner
|
||||
.lock(|f| ReadStorage::read(f.borrow_mut().deref_mut(), offset, bytes))
|
||||
}
|
||||
|
||||
fn capacity(&self) -> usize {
|
||||
self.inner
|
||||
.lock(|f| ReadStorage::capacity(f.borrow().deref()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +68,10 @@ impl ErrorType for MutexFlashStorage {
|
||||
type Error = FlashStorageError;
|
||||
}
|
||||
|
||||
impl ErrorType for &MutexFlashStorage {
|
||||
type Error = FlashStorageError;
|
||||
}
|
||||
|
||||
impl ReadNorFlash for MutexFlashStorage {
|
||||
const READ_SIZE: usize = 1;
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ use crate::hal::{
|
||||
};
|
||||
use crate::log::{LogMessage, LOG_ACCESS};
|
||||
use alloc::boxed::Box;
|
||||
use alloc::string::{ToString};
|
||||
use alloc::string::ToString;
|
||||
use async_trait::async_trait;
|
||||
use canapi::id::{classify, plant_id, MessageKind, IDENTIFY_CMD_OFFSET};
|
||||
use canapi::SensorSlot;
|
||||
@@ -23,7 +23,6 @@ use esp_hal::gpio::{Flex, Input, InputConfig, Level, Output, OutputConfig, Pull}
|
||||
use esp_hal::i2c::master::I2c;
|
||||
use esp_hal::twai::{EspTwaiError, EspTwaiFrame, StandardId, Twai, TwaiConfiguration, TwaiMode};
|
||||
use esp_hal::{twai, Async, Blocking};
|
||||
use esp_println::println;
|
||||
use ina219::address::{Address, Pin};
|
||||
use ina219::calibration::UnCalibrated;
|
||||
use ina219::configuration::{Configuration, OperatingMode, Resolution};
|
||||
@@ -133,10 +132,9 @@ pub struct V4<'a> {
|
||||
|
||||
extra1: Output<'a>,
|
||||
extra2: Output<'a>,
|
||||
twai_config: Option<TwaiConfiguration<'static, Blocking>>
|
||||
twai_config: Option<TwaiConfiguration<'static, Blocking>>,
|
||||
}
|
||||
|
||||
|
||||
pub(crate) async fn create_v4(
|
||||
peripherals: FreePeripherals<'static>,
|
||||
esp: Esp<'static>,
|
||||
@@ -261,12 +259,11 @@ pub(crate) async fn create_v4(
|
||||
extra1,
|
||||
extra2,
|
||||
can_power,
|
||||
twai_config
|
||||
twai_config,
|
||||
};
|
||||
Ok(Box::new(v))
|
||||
}
|
||||
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
fn get_tank_sensor(&mut self) -> Result<&mut TankSensor<'a>, FatError> {
|
||||
@@ -393,11 +390,18 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
if !detect {
|
||||
continue;
|
||||
}
|
||||
let target =
|
||||
StandardId::new(plant_id(IDENTIFY_CMD_OFFSET, sensor.into(), (plant +1) as u16))
|
||||
.context(">> Could not create address for sensor! (plant: {}) <<")?;
|
||||
let target = StandardId::new(plant_id(
|
||||
IDENTIFY_CMD_OFFSET,
|
||||
sensor.into(),
|
||||
(plant + 1) as u16,
|
||||
))
|
||||
.context(">> Could not create address for sensor! (plant: {}) <<")?;
|
||||
let can_buffer = [0_u8; 0];
|
||||
info!("Sending test message to plant {} sensor {sensor:?} with id {}", plant +1, target.as_raw());
|
||||
info!(
|
||||
"Sending test message to plant {} sensor {sensor:?} with id {}",
|
||||
plant + 1,
|
||||
target.as_raw()
|
||||
);
|
||||
if let Some(frame) = EspTwaiFrame::new(target, &can_buffer) {
|
||||
// Try a few times; we intentionally ignore rx here and rely on stub logic
|
||||
let resu = twai
|
||||
@@ -405,12 +409,12 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
.with_timeout(Duration::from_millis(3000))
|
||||
.await;
|
||||
match resu {
|
||||
Ok(_) => {
|
||||
}
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
info!(
|
||||
"Error sending test message to plant {} sensor {sensor:?}: {err:?}", plant +1
|
||||
);
|
||||
"Error sending test message to plant {} sensor {sensor:?}: {err:?}",
|
||||
plant + 1
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -424,20 +428,17 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
.with_timeout(Duration::from_millis(3000))
|
||||
.await;
|
||||
|
||||
|
||||
let config = twai.stop().into_blocking();
|
||||
self.twai_config.replace(config);
|
||||
|
||||
self.can_power.set_low();
|
||||
|
||||
|
||||
let result = moistures.into();
|
||||
|
||||
info!("Autodetection result: {result:?}");
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
|
||||
async fn general_fault(&mut self, enable: bool) {
|
||||
hold_disable(23);
|
||||
self.general_fault.set_level(enable.into());
|
||||
@@ -480,7 +481,7 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
.await
|
||||
.log(LogMessage::TestSensor, a, b, &(plant+1).to_string(), "")
|
||||
.log(LogMessage::TestSensor, a, b, &(plant + 1).to_string(), "")
|
||||
.await;
|
||||
}
|
||||
Timer::after_millis(10).await;
|
||||
@@ -509,7 +510,6 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async fn wait_for_can_measurements(
|
||||
as_async: &mut Twai<'_, Async>,
|
||||
moistures: &mut Moistures,
|
||||
@@ -536,10 +536,12 @@ async fn wait_for_can_measurements(
|
||||
let frequency = u32::from_be_bytes(bytes);
|
||||
match sensor {
|
||||
SensorSlot::A => {
|
||||
moistures.sensor_a_hz[plant-1] = Some(frequency as f32);
|
||||
moistures.sensor_a_hz[plant - 1] =
|
||||
Some(frequency as f32);
|
||||
}
|
||||
SensorSlot::B => {
|
||||
moistures.sensor_b_hz[plant-1] = Some(frequency as f32);
|
||||
moistures.sensor_b_hz[plant - 1] =
|
||||
Some(frequency as f32);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user