summaryrefslogtreecommitdiffstats
path: root/ble
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2019-08-12 20:37:34 -0400
committerBrian Cully <bjc@kublai.com>2019-08-13 15:33:41 -0400
commit26609a6882d393eb86644f3472e401ccb63c0b21 (patch)
tree099cb67a04145dc156899cba000a23408953d4e5 /ble
parentb0e0946840155b506dfe175999bef735f45f0908 (diff)
downloadbleusb-26609a6882d393eb86644f3472e401ccb63c0b21.tar.gz
bleusb-26609a6882d393eb86644f3472e401ccb63c0b21.zip
Get I²C basics working.
Diffstat (limited to 'ble')
-rw-r--r--ble/src/i2c.rs43
-rw-r--r--ble/src/main.rs24
2 files changed, 65 insertions, 2 deletions
diff --git a/ble/src/i2c.rs b/ble/src/i2c.rs
new file mode 100644
index 0000000..13f5a5a
--- /dev/null
+++ b/ble/src/i2c.rs
@@ -0,0 +1,43 @@
+use log::{error, info, trace};
+use nrf52840_hal::twis::{self, Twis, TwisInterrupt};
+
+pub fn setup<T>(mut twis: Twis<T>) -> 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::Write);
+ twis.enable_interrupt(TwisInterrupt::Read);
+
+ let mut buf: [u8; 255] = [0; 255];
+ move || handler(&mut twis, &mut buf)
+}
+
+fn handler<T>(twis: &mut Twis<T>, buf: &mut [u8])
+where
+ T: twis::Instance,
+{
+ if twis.get_event_triggered(TwisInterrupt::Stopped, true) {
+ 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::Write, true) {
+ trace!("i²c write");
+ match twis.read(buf) {
+ Ok(len) => info!("buf {}: {:?}", len, &buf[..len]),
+ Err(e) => error!("{:?}", e),
+ }
+ } else if twis.get_event_triggered(TwisInterrupt::Read, true) {
+ trace!("i²c read");
+ } else {
+ trace!("i²c unknown");
+ }
+}
diff --git a/ble/src/main.rs b/ble/src/main.rs
index 4430e98..2da6de9 100644
--- a/ble/src/main.rs
+++ b/ble/src/main.rs
@@ -3,6 +3,7 @@
#![no_std]
#![no_main]
+mod i2c;
mod logger;
mod macros;
mod rtc;
@@ -21,12 +22,15 @@ use nrf52840_mdk_bsp::{
hal::{
gpio::{Floating, Input, Level, Output, Pin, PushPull},
target::{interrupt, Interrupt, UARTE0},
+ twis::{self, Twis},
uarte::{self, Baudrate as UartBaudrate, Parity as UartParity, Uarte},
Clocks, Rtc,
},
Board,
};
+const I2C_ADDR: u8 = 4;
+
// TODO:
// * set up serial reader for trinket
// * set up i²c interface for keyboard reports
@@ -49,6 +53,14 @@ fn main() -> ! {
let mut nvic = nrf52.NVIC;
let mut rtc_handler = rtc::setup(Rtc::new(nrf52.RTC0), Clocks::new(nrf52.CLOCK));
+ let mut twis_handler = i2c::setup(Twis::new(
+ I2C_ADDR,
+ nrf52.TWIS0,
+ twis::Pins {
+ scl: nrf52.pins.P0_06.into_floating_input().degrade(),
+ sda: nrf52.pins.P0_07.into_floating_input().degrade(),
+ },
+ ));
let txp = nrf52
.pins
@@ -61,6 +73,8 @@ fn main() -> ! {
HANDLERS.with_overrides(|hs| {
hs.register(0, &mut rtc_handler);
nvic.enable(Interrupt::RTC0);
+ hs.register(1, &mut twis_handler);
+ nvic.enable(Interrupt::SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0);
info!("Bootstrap complete.");
@@ -74,6 +88,7 @@ fn main() -> ! {
trace!(".");
write!(uarte1, "!").expect("uarte1 write");
}
+
wfi();
}
});
@@ -87,8 +102,8 @@ where
Uarte::new(
uarte,
uarte::Pins {
- txd: tx.into_push_pull_output(Level::High),
- rxd: rx.into_floating_input(),
+ txd: tx,
+ rxd: rx,
cts: None,
rts: None,
},
@@ -112,3 +127,8 @@ fn HardFault(ef: &ExceptionFrame) -> ! {
fn RTC0() {
HANDLERS.call(0);
}
+
+#[interrupt]
+fn SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0() {
+ HANDLERS.call(1);
+}