summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-10-02 18:58:49 +0000
committerGitHub <noreply@github.com>2022-10-02 18:58:49 +0000
commit73208d524843ca451b4cbfdb06e35f1b85290f4c (patch)
tree930620eeda74db9eaf2f7c601c927248d1ddce0e
parentaabc02506bcbd762552660a0b45cda6e15787cab (diff)
parentc96581879cf054d9e4fec9272e5cecb165844cbb (diff)
downloadembassy-73208d524843ca451b4cbfdb06e35f1b85290f4c.zip
Merge #993
993: rp i2c: blocking example r=Dirbaio a=jsgf i2c example talking to mcp23017 i2c gpio expander. Co-authored-by: Jeremy Fitzhardinge <jeremy@goop.org>
-rw-r--r--examples/rp/Cargo.toml2
-rw-r--r--examples/rp/src/bin/i2c_blocking.rs70
2 files changed, 71 insertions, 1 deletions
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml
index 3c8f923e..1bb6432e 100644
--- a/examples/rp/Cargo.toml
+++ b/examples/rp/Cargo.toml
@@ -26,7 +26,7 @@ st7789 = "0.6.1"
display-interface = "0.4.1"
byte-slice-cast = { version = "1.2.0", default-features = false }
-embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" }
+embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.9" }
embedded-hal-async = { version = "0.1.0-alpha.1" }
embedded-io = { version = "0.3.0", features = ["async", "defmt"] }
static_cell = "1.0.0"
diff --git a/examples/rp/src/bin/i2c_blocking.rs b/examples/rp/src/bin/i2c_blocking.rs
new file mode 100644
index 00000000..7623e33c
--- /dev/null
+++ b/examples/rp/src/bin/i2c_blocking.rs
@@ -0,0 +1,70 @@
+#![no_std]
+#![no_main]
+#![feature(type_alias_impl_trait)]
+
+use defmt::*;
+use embassy_executor::Spawner;
+use embassy_rp::i2c::{self, Config};
+use embassy_time::{Duration, Timer};
+use embedded_hal_1::i2c::I2c;
+use {defmt_rtt as _, panic_probe as _};
+
+#[allow(dead_code)]
+mod mcp23017 {
+ pub const ADDR: u8 = 0x20; // default addr
+
+ pub const IODIRA: u8 = 0x00;
+ pub const IPOLA: u8 = 0x02;
+ pub const GPINTENA: u8 = 0x04;
+ pub const DEFVALA: u8 = 0x06;
+ pub const INTCONA: u8 = 0x08;
+ pub const IOCONA: u8 = 0x0A;
+ pub const GPPUA: u8 = 0x0C;
+ pub const INTFA: u8 = 0x0E;
+ pub const INTCAPA: u8 = 0x10;
+ pub const GPIOA: u8 = 0x12;
+ pub const OLATA: u8 = 0x14;
+ pub const IODIRB: u8 = 0x01;
+ pub const IPOLB: u8 = 0x03;
+ pub const GPINTENB: u8 = 0x05;
+ pub const DEFVALB: u8 = 0x07;
+ pub const INTCONB: u8 = 0x09;
+ pub const IOCONB: u8 = 0x0B;
+ pub const GPPUB: u8 = 0x0D;
+ pub const INTFB: u8 = 0x0F;
+ pub const INTCAPB: u8 = 0x11;
+ pub const GPIOB: u8 = 0x13;
+ pub const OLATB: u8 = 0x15;
+}
+
+#[embassy_executor::main]
+async fn main(_spawner: Spawner) {
+ let p = embassy_rp::init(Default::default());
+
+ let sda = p.PIN_14;
+ let scl = p.PIN_15;
+
+ info!("set up i2c ");
+ let mut i2c = i2c::I2c::new_blocking(p.I2C1, scl, sda, Config::default());
+
+ use mcp23017::*;
+
+ info!("init mcp23017 config for IxpandO");
+ // init - a outputs, b inputs
+ i2c.write(ADDR, &[IODIRA, 0x00]).unwrap();
+ i2c.write(ADDR, &[IODIRB, 0xff]).unwrap();
+ i2c.write(ADDR, &[GPPUB, 0xff]).unwrap(); // pullups
+
+ let mut val = 0xaa;
+ loop {
+ let mut portb = [0];
+
+ i2c.write(mcp23017::ADDR, &[GPIOA, val]).unwrap();
+ i2c.write_read(mcp23017::ADDR, &[GPIOB], &mut portb).unwrap();
+
+ info!("portb = {:02x}", portb[0]);
+ val = !val;
+
+ Timer::after(Duration::from_secs(1)).await;
+ }
+}