Skip to content
Snippets Groups Projects
Commit d66e1a26 authored by Benjamin Koch's avatar Benjamin Koch
Browse files

do not transmit continuously

parent e84cb4f4
No related branches found
No related tags found
No related merge requests found
...@@ -2,9 +2,15 @@ use core::future::*; ...@@ -2,9 +2,15 @@ use core::future::*;
use core::pin::Pin; use core::pin::Pin;
use core::task::*; use core::task::*;
pub enum DontAbortMode {
PanicIfReused,
HangIfReused,
}
pub struct DontAbort<A> { pub struct DontAbort<A> {
a: A, a: A,
done: bool done: bool,
mode: DontAbortMode,
} }
pub struct DontAbortFuture<'a, A> { pub struct DontAbortFuture<'a, A> {
...@@ -12,8 +18,8 @@ pub struct DontAbortFuture<'a, A> { ...@@ -12,8 +18,8 @@ pub struct DontAbortFuture<'a, A> {
} }
impl<A> DontAbort<A> { impl<A> DontAbort<A> {
pub fn new(a: A) -> DontAbort<A> { pub fn new(a: A, mode: DontAbortMode) -> DontAbort<A> {
DontAbort { a, done: false } DontAbort { a, done: false, mode }
} }
#[allow(dead_code)] #[allow(dead_code)]
...@@ -38,7 +44,10 @@ where ...@@ -38,7 +44,10 @@ where
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() }; let this = unsafe { self.get_unchecked_mut() };
if this.inner.done { if this.inner.done {
panic!("Ready future is called again!"); match this.inner.mode {
DontAbortMode::PanicIfReused => panic!("Ready future is called again!"),
DontAbortMode::HangIfReused => return Poll::Pending, // will never finish because we haven't scheduled ourselves again
}
} }
let a = unsafe { Pin::new_unchecked(&mut this.inner.a) }; let a = unsafe { Pin::new_unchecked(&mut this.inner.a) };
if let Poll::Ready(x) = a.poll(cx) { if let Poll::Ready(x) = a.poll(cx) {
......
...@@ -15,6 +15,7 @@ use fixed::traits::ToFixed; ...@@ -15,6 +15,7 @@ use fixed::traits::ToFixed;
use fixed_macro::types::U56F8; use fixed_macro::types::U56F8;
use crate::dont_abort::DontAbort; use crate::dont_abort::DontAbort;
use crate::dont_abort::DontAbortMode::*;
pub struct RS485 { pub struct RS485 {
pio: peripherals::PIO0, pio: peripherals::PIO0,
...@@ -255,7 +256,6 @@ impl RS485 { ...@@ -255,7 +256,6 @@ impl RS485 {
} }
tx_data[i] = x | ((parity&1) << 8); tx_data[i] = x | ((parity&1) << 8);
} }
info!("tx_data: {:?}", tx_data);
let mut dma_in_ref = self.dma_channel.into_ref(); let mut dma_in_ref = self.dma_channel.into_ref();
...@@ -263,8 +263,8 @@ impl RS485 { ...@@ -263,8 +263,8 @@ impl RS485 {
let mut din = [42u32; 9]; let mut din = [42u32; 9];
let mut bit_index = 0; let mut bit_index = 0;
let mut rx_buf = [0; 1]; let mut rx_buf = [0; 1];
let mut rx_future = DontAbort::new(self.rx.read(&mut rx_buf)); let mut rx_future = DontAbort::new(self.rx.read(&mut rx_buf), PanicIfReused);
let mut tx_future = DontAbort::new(sm1.tx().dma_push(dma_tx_ref.reborrow(), &tx_data)); let mut tx_future = DontAbort::new(sm1.tx().dma_push(dma_tx_ref.reborrow(), &tx_data), HangIfReused);
loop { loop {
let x = select4( let x = select4(
irq0.wait(), irq0.wait(),
...@@ -361,20 +361,23 @@ impl RS485 { ...@@ -361,20 +361,23 @@ impl RS485 {
} }
}, },
Either4::Third(_) => { Either4::Third(_) => {
drop(tx_future); //drop(tx_future);
tx_future = DontAbort::new(sm1.tx().dma_push(dma_tx_ref.reborrow(), &tx_data)); //tx_future = DontAbort::new(sm1.tx().dma_push(dma_tx_ref.reborrow(), &tx_data), HangIfReused);
}, },
Either4::Fourth(Either::First(x)) => { Either4::Fourth(Either::First(x)) => {
drop(rx_future); drop(rx_future);
match x { match x {
Result::Ok(()) => { Result::Ok(()) => {
info!("RX {:?}", rx_buf); info!("RX {:?}", rx_buf);
drop(tx_future);
tx_future = DontAbort::new(sm1.tx().dma_push(dma_tx_ref.reborrow(), &tx_data), HangIfReused);
}, },
Result::Err(e) => { Result::Err(e) => {
info!("RX error {:?}", e); info!("RX error {:?}", e);
}, },
} }
rx_future = DontAbort::new(self.rx.read(&mut rx_buf)); rx_future = DontAbort::new(self.rx.read(&mut rx_buf), PanicIfReused);
}, },
_ => { _ => {
}, },
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment