1
Fork 0

switched northbridge -> southbridge communication to length-data

buffereduart peek does not appear to work, and
coms are very stable with shielded wire
This commit is contained in:
Andy Killorin 2025-02-04 13:06:38 -05:00
parent 76ba58de69
commit a9573dc8bc
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
4 changed files with 36 additions and 43 deletions

View file

@ -37,9 +37,8 @@ impl Encoder<Vec<u8>> for FramedCodec {
type Error = framed::Error;
fn encode(&mut self, item: Vec<u8>, dst: &mut BytesMut) -> Result<(), Self::Error> {
let encoded = self.codec.encode_to_box(&item)?;
dst.extend_from_slice(&encoded);
dst.extend_from_slice(&item.len().to_be_bytes());
dst.extend_from_slice(&item);
Ok(())
}

View file

@ -30,26 +30,33 @@ async fn main() -> Result<()> {
println!("starting");
write.send(Command::Enable).await?;
write.flush().await?;
while let Some(Ok(data)) = read.next().await {
if data.enabled {
write.send(Command::FeedWatchdog).await?;
loop {
//let Some(Ok(data)) = read.next().await else {
// continue;
//};
//if data.enabled {
// write.send(Command::FeedWatchdog).await?;
// write.flush().await?;
//} else {
// println!("enabled southbridge");
// write.send(Command::Enable).await?;
//}
} else {
println!("enabled southbridge");
write.send(Command::Enable).await?;
write.flush().await?;
//dbg!(&data);
//if let Some(data) = data.sensor_data {
// let _ = sensor_sender.send(data);
//}
write.send(dbg!(commands.borrow().clone())).await?;
write.flush().await?;
}
dbg!(&data);
if let Some(data) = data.sensor_data {
let _ = sensor_sender.send(data);
}
write.send(commands.borrow().clone()).await?;
}
Ok(())
}
async fn control(sender: Sender<Command>) -> Result<()> {

View file

@ -13,7 +13,7 @@ embedded-storage = "0.3.1"
cortex-m-rt = "0.7.3"
embassy-executor = { version = "0.7", features = ["arch-cortex-m", "executor-thread", "executor-interrupt"] }
embassy-executor = { version = "0.7", features = ["arch-cortex-m", "executor-thread", "task-arena-size-32768"] }
embassy-sync = { version = "0.6" }
embassy-time = { version = "0.4" }
cortex-m = { version = "0.7.6" }

View file

@ -8,8 +8,7 @@ use embassy_executor::Spawner;
use embassy_rp::{bind_interrupts, block::ImageDef, gpio::{Level, Output}, peripherals::{UART0, UART1, USB}, pwm::{self, Pwm}, uart::{BufferedInterruptHandler, BufferedUart, BufferedUartRx, BufferedUartTx, Config}, usb::Driver};
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
use embassy_time::{with_deadline, with_timeout, Duration, Instant, Timer};
use embedded_io::Write;
use embedded_io_async::{BufRead, Read};
use embedded_io_async::{BufRead, Read, Write};
use framed::{bytes, FRAME_END_SYMBOL};
use log::{error, info, trace, warn};
use nalgebra::clamp;
@ -57,7 +56,7 @@ async fn main(spawner: Spawner) {
drive_conf.compare_b = 15000; // 1.5ms
drive_conf.compare_a = 15000; // 1.5ms
let stopped = drive_conf.clone();
let mut drive_pwm = Pwm::new_output_ab(p.PWM_SLICE0, p.PIN_16, p.PIN_17, drive_conf.clone());
let mut drive_pwm = Pwm::new_output_ab(p.PWM_SLICE0, p.PIN_16, p.PIN_17, stopped.clone());
let config = embassy_rp::i2c::Config::default();
let bus = embassy_rp::i2c::I2c::new_async(p.I2C1, p.PIN_19, p.PIN_18, Irqs, config);
@ -80,19 +79,11 @@ async fn main(spawner: Spawner) {
let enabled = &ENABLED;
let mut enable_watchdog = Instant::now();
let enable_watchdog_time = Duration::from_millis(50);
let enable_watchdog_time = Duration::from_millis(150);
Timer::after_secs(1).await;
info!("ready");
Timer::after_secs(1).await;
loop {
if !enabled.load(Ordering::Acquire) {
// stop all motors
drive_pwm.set_config(&stopped);
}
let command = if enabled.load(Ordering::Acquire) {
let Ok(command) = with_deadline(enable_watchdog + enable_watchdog_time, COMMANDS.receive()).await else {
warn!("no message received");
@ -101,6 +92,8 @@ async fn main(spawner: Spawner) {
};
command
} else {
// stop all motors
//drive_pwm.set_config(&stopped);
info!("waiting for command");
COMMANDS.receive().await
};
@ -129,7 +122,6 @@ async fn main(spawner: Spawner) {
};
info!("Blink");
Timer::after(Duration::from_millis(100)).await;
}
}
@ -137,18 +129,14 @@ async fn main(spawner: Spawner) {
#[embassy_executor::task]
async fn decoder(mut rx: BufferedUartRx<'static, UART1>) {
info!("Reading...");
let mut codec = framed::bytes::Config::default().to_codec();
loop {
let data = rx.fill_buf().await.unwrap();
let Some(end) = data.iter().position(|e| *e == FRAME_END_SYMBOL) else { continue; };
let data = &data[..end];
let mut len = [0;4];
rx.read_exact(&mut len).await.unwrap();
let len = u32::from_be_bytes(len) as usize;
let mut buf = [0u8; 1024];
let Ok(len) = codec.decode_to_slice(data, &mut buf) else {
error!("frame decode fail");
continue;
};
let mut buf = [0;1024];
rx.read_exact(&mut buf[..len]).await.unwrap();
let Ok(data) = postcard::from_bytes::<Command>(&buf[..len]) else {
error!("message decode fail");
@ -158,8 +146,6 @@ async fn decoder(mut rx: BufferedUartRx<'static, UART1>) {
trace!("received {data:?}");
COMMANDS.send(data).await;
rx.consume(end);
}
}
@ -189,9 +175,10 @@ async fn telemetry(mut tx: BufferedUartTx<'static, UART1>, enabled: &'static Ato
continue;
};
if let Err(e) = tx.write_all(&buf[..len]) {
if let Err(e) = tx.write_all(&buf[..len]).await {
error!("transport error {e:?}");
}
tx.flush().await.unwrap();
}
}