diff options
-rw-r--r-- | ble/src/i2c.rs | 45 |
1 files changed, 29 insertions, 16 deletions
diff --git a/ble/src/i2c.rs b/ble/src/i2c.rs index 13f5a5a..0f7fe5e 100644 --- a/ble/src/i2c.rs +++ b/ble/src/i2c.rs @@ -1,23 +1,28 @@ -use log::{error, info, trace}; +use log::{error, trace}; use nrf52840_hal::twis::{self, Twis, TwisInterrupt}; +use starb::{Reader, RingBuffer, Writer}; -pub fn setup<T>(mut twis: Twis<T>) -> impl FnMut() +static mut RB: RingBuffer<u8> = RingBuffer::new(0); + +pub fn setup<T>(mut twis: Twis<T>) -> (Reader<'static, u8>, impl FnMut()) where T: twis::Instance, { // TODO: we only care about `Write` and `Stopped`. twis.enable_interrupt(TwisInterrupt::Stopped); twis.enable_interrupt(TwisInterrupt::Error); - twis.enable_interrupt(TwisInterrupt::RxStarted); - twis.enable_interrupt(TwisInterrupt::TxStarted); + //twis.enable_interrupt(TwisInterrupt::RxStarted); + //twis.enable_interrupt(TwisInterrupt::TxStarted); twis.enable_interrupt(TwisInterrupt::Write); - twis.enable_interrupt(TwisInterrupt::Read); + //twis.enable_interrupt(TwisInterrupt::Read); + + let (rbr, mut rbw) = unsafe { RB.split() }; - let mut buf: [u8; 255] = [0; 255]; - move || handler(&mut twis, &mut buf) + let cl = move || handler(&mut twis, &mut rbw); + (rbr, cl) } -fn handler<T>(twis: &mut Twis<T>, buf: &mut [u8]) +fn handler<T>(twis: &mut Twis<T>, writer: &mut Writer<u8>) where T: twis::Instance, { @@ -25,18 +30,26 @@ where trace!("i²c stopped: {}", twis.amount()); } else if twis.get_event_triggered(TwisInterrupt::Error, true) { trace!("i²c error"); - } else if twis.get_event_triggered(TwisInterrupt::RxStarted, true) { - trace!("i²c rxstarted"); - } else if twis.get_event_triggered(TwisInterrupt::TxStarted, true) { - trace!("i²c txstarted"); + //} else if twis.get_event_triggered(TwisInterrupt::RxStarted, true) { + // trace!("i²c rxstarted"); + //} else if twis.get_event_triggered(TwisInterrupt::TxStarted, true) { + // trace!("i²c txstarted"); } else if twis.get_event_triggered(TwisInterrupt::Write, true) { trace!("i²c write"); - match twis.read(buf) { - Ok(len) => info!("buf {}: {:?}", len, &buf[..len]), + let mut buf: [u8; 255] = [0; 255]; + match twis.read(&mut buf) { + Ok(len) => { + for b in &buf[0..len] { + // Ignore unshift errors for now, as I don't think + // there's much we can do about it. + writer.unshift(*b).ok(); + } + trace!("write done"); + } Err(e) => error!("{:?}", e), } - } else if twis.get_event_triggered(TwisInterrupt::Read, true) { - trace!("i²c read"); + //} else if twis.get_event_triggered(TwisInterrupt::Read, true) { + //trace!("i²c read"); } else { trace!("i²c unknown"); } |