add: implement UART-based serial configuration handling and improve error handling in charge indicator updates
This commit is contained in:
@@ -25,6 +25,7 @@ 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::{
|
||||
@@ -32,6 +33,7 @@ use esp_hal::rtc_cntl::{
|
||||
Rtc,
|
||||
};
|
||||
use esp_hal::system::software_reset;
|
||||
use esp_hal::uart::Uart;
|
||||
use esp_println::println;
|
||||
use esp_radio::wifi::{
|
||||
AccessPointConfig, AccessPointInfo, AuthMethod, ClientConfig, ModeConfig, ScanConfig,
|
||||
@@ -39,7 +41,7 @@ use esp_radio::wifi::{
|
||||
};
|
||||
use littlefs2::fs::Filesystem;
|
||||
use littlefs2_core::{FileType, PathBuf, SeekFrom};
|
||||
use log::{info, warn};
|
||||
use log::{error, info, warn};
|
||||
use mcutie::{
|
||||
Error, McutieBuilder, McutieReceiver, McutieTask, MqttMessage, PublishDisplay, Publishable,
|
||||
QoS, Topic,
|
||||
@@ -126,6 +128,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>,
|
||||
@@ -152,6 +155,37 @@ 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();
|
||||
loop {
|
||||
match self.uart0.read_buffered(&mut buf) {
|
||||
Ok(read) => {
|
||||
if (read == 0) {
|
||||
return Ok(None);
|
||||
}
|
||||
let c = buf[0] as char;
|
||||
if c == '\n' {
|
||||
return Ok(Some(line));
|
||||
}
|
||||
line.push(c);
|
||||
}
|
||||
Err(error ) => {
|
||||
if line.is_empty() {
|
||||
return Ok(None);
|
||||
} else {
|
||||
error!("Error reading serial line: {error:?}");
|
||||
// If we already have some data, we should probably wait a bit or just return what we have?
|
||||
// But the protocol expects a full line or message.
|
||||
// For simplicity in config mode, we can block here or just return None if nothing is there yet.
|
||||
// However, if we started receiving, we should probably finish or timeout.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) async fn delete_file(&self, filename: String) -> FatResult<()> {
|
||||
let file = PathBuf::try_from(filename.as_str())?;
|
||||
let access = self.fs.lock().await;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
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;
|
||||
@@ -49,6 +50,7 @@ use crate::{
|
||||
};
|
||||
use alloc::boxed::Box;
|
||||
use alloc::format;
|
||||
use alloc::string::String;
|
||||
use alloc::sync::Arc;
|
||||
use async_trait::async_trait;
|
||||
use bincode::{Decode, Encode};
|
||||
@@ -94,6 +96,7 @@ 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_radio::{init, Controller};
|
||||
use esp_storage::FlashStorage;
|
||||
use littlefs2::fs::{Allocation, Filesystem as lfs2Filesystem};
|
||||
@@ -272,10 +275,6 @@ impl PlantHal {
|
||||
let pcnt_module = Pcnt::new(peripherals.PCNT);
|
||||
|
||||
let free_pins = FreePeripherals {
|
||||
// can: peripherals.can,
|
||||
// adc1: peripherals.adc1,
|
||||
// pcnt0: peripherals.pcnt0,
|
||||
// pcnt1: peripherals.pcnt1,
|
||||
gpio0: peripherals.GPIO0,
|
||||
gpio2: peripherals.GPIO2,
|
||||
gpio3: peripherals.GPIO3,
|
||||
@@ -397,6 +396,11 @@ impl PlantHal {
|
||||
lfs2Filesystem::mount(alloc, lfs2filesystem).expect("Could not mount lfs2 filesystem"),
|
||||
));
|
||||
|
||||
let uart0 = Uart::new(peripherals.UART0, UartConfig::default())
|
||||
.map_err(|_| FatError::String {
|
||||
error: "Uart creation failed".to_string(),
|
||||
})?;
|
||||
|
||||
let ap = interfaces.ap;
|
||||
let sta = interfaces.sta;
|
||||
let mut esp = Esp {
|
||||
@@ -412,6 +416,7 @@ impl PlantHal {
|
||||
current: running,
|
||||
slot0_state: state_0,
|
||||
slot1_state: state_1,
|
||||
uart0
|
||||
};
|
||||
|
||||
//init,reset rtc memory depending on cause
|
||||
|
||||
@@ -11,7 +11,8 @@ use crate::hal::{
|
||||
};
|
||||
use crate::log::{LogMessage, LOG_ACCESS};
|
||||
use alloc::boxed::Box;
|
||||
use alloc::string::ToString;
|
||||
use alloc::string::{String, ToString};
|
||||
use alloc::vec;
|
||||
use async_trait::async_trait;
|
||||
use canapi::id::{classify, plant_id, MessageKind, IDENTIFY_CMD_OFFSET};
|
||||
use canapi::SensorSlot;
|
||||
@@ -130,6 +131,7 @@ pub struct V4<'a> {
|
||||
>,
|
||||
twai_config: Option<TwaiConfiguration<'static, Blocking>>,
|
||||
can_power: Output<'static>,
|
||||
|
||||
extra1: Output<'a>,
|
||||
extra2: Output<'a>,
|
||||
}
|
||||
@@ -254,7 +256,7 @@ pub(crate) async fn create_v4(
|
||||
charger,
|
||||
extra1,
|
||||
extra2,
|
||||
can_power,
|
||||
can_power
|
||||
};
|
||||
Ok(Box::new(v))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user