aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJeremy Fitzhardinge <[email protected]>2022-09-29 02:01:58 -0700
committerJeremy Fitzhardinge <[email protected]>2022-10-01 01:29:10 -0700
commit9f77dbf5ae442c1cac0c652b4ef25bf1c82ed9d4 (patch)
tree2637dacc6ff9bbbaabea282bc5288e3e153d04de /examples
parentaabc02506bcbd762552660a0b45cda6e15787cab (diff)
rp i2c: blocking example
i2c example talking to mcp23017 i2c gpio expander.
Diffstat (limited to 'examples')
-rw-r--r--examples/rp/src/bin/i2c.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/examples/rp/src/bin/i2c.rs b/examples/rp/src/bin/i2c.rs
new file mode 100644
index 000000000..a5991d0da
--- /dev/null
+++ b/examples/rp/src/bin/i2c.rs
@@ -0,0 +1,70 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_rp::i2c::{self, Config};
8use embassy_time::{Duration, Timer};
9use embedded_hal_1::i2c::blocking::I2c;
10use {defmt_rtt as _, panic_probe as _};
11
12#[allow(dead_code)]
13mod mcp23017 {
14 pub const ADDR: u8 = 0x20; // default addr
15
16 pub const IODIRA: u8 = 0x00;
17 pub const IPOLA: u8 = 0x02;
18 pub const GPINTENA: u8 = 0x04;
19 pub const DEFVALA: u8 = 0x06;
20 pub const INTCONA: u8 = 0x08;
21 pub const IOCONA: u8 = 0x0A;
22 pub const GPPUA: u8 = 0x0C;
23 pub const INTFA: u8 = 0x0E;
24 pub const INTCAPA: u8 = 0x10;
25 pub const GPIOA: u8 = 0x12;
26 pub const OLATA: u8 = 0x14;
27 pub const IODIRB: u8 = 0x01;
28 pub const IPOLB: u8 = 0x03;
29 pub const GPINTENB: u8 = 0x05;
30 pub const DEFVALB: u8 = 0x07;
31 pub const INTCONB: u8 = 0x09;
32 pub const IOCONB: u8 = 0x0B;
33 pub const GPPUB: u8 = 0x0D;
34 pub const INTFB: u8 = 0x0F;
35 pub const INTCAPB: u8 = 0x11;
36 pub const GPIOB: u8 = 0x13;
37 pub const OLATB: u8 = 0x15;
38}
39
40#[embassy_executor::main]
41async fn main(_spawner: Spawner) {
42 let p = embassy_rp::init(Default::default());
43
44 let sda = p.PIN_14;
45 let scl = p.PIN_15;
46
47 info!("set up i2c ");
48 let mut i2c = i2c::I2c::new_blocking(p.I2C1, scl, sda, Config::default());
49
50 use mcp23017::*;
51
52 info!("init mcp23017 config for IxpandO");
53 // init - a outputs, b inputs
54 i2c.write(ADDR, &[IODIRA, 0x00]).unwrap();
55 i2c.write(ADDR, &[IODIRB, 0xff]).unwrap();
56 i2c.write(ADDR, &[GPPUB, 0xff]).unwrap(); // pullups
57
58 let mut val = 0xaa;
59 loop {
60 let mut portb = [0];
61
62 i2c.write(mcp23017::ADDR, &[GPIOA, val]).unwrap();
63 i2c.write_read(mcp23017::ADDR, &[GPIOB], &mut portb).unwrap();
64
65 info!("portb = {:02x}", portb[0]);
66 val = !val;
67
68 Timer::after(Duration::from_secs(1)).await;
69 }
70}