diff options
| author | Mathias <[email protected]> | 2022-10-24 12:14:26 +0200 |
|---|---|---|
| committer | Mathias <[email protected]> | 2022-10-24 12:14:26 +0200 |
| commit | 8d809c96ecf2fabf77f0fb72f2a59acd18306bf2 (patch) | |
| tree | da3e28e491bbaadbc448b9a021291e2164b7531e /examples/rp/src/bin | |
| parent | 7152031229da19005e5b0d52c8c72d359d3e0daa (diff) | |
| parent | ce1cba761c2942b7faa27f4098487c6468784729 (diff) | |
Merge branch 'master' of https://github.com/embassy-rs/embassy into embassy-rp/flash
Diffstat (limited to 'examples/rp/src/bin')
| -rw-r--r-- | examples/rp/src/bin/i2c_async.rs | 102 | ||||
| -rw-r--r-- | examples/rp/src/bin/i2c_blocking.rs | 70 | ||||
| -rw-r--r-- | examples/rp/src/bin/spi_display.rs | 12 |
3 files changed, 178 insertions, 6 deletions
diff --git a/examples/rp/src/bin/i2c_async.rs b/examples/rp/src/bin/i2c_async.rs new file mode 100644 index 000000000..d1a2e3cd7 --- /dev/null +++ b/examples/rp/src/bin/i2c_async.rs | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_rp::i2c::{self, Config}; | ||
| 8 | use embassy_rp::interrupt; | ||
| 9 | use embassy_time::{Duration, Timer}; | ||
| 10 | use embedded_hal_async::i2c::I2c; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | #[allow(dead_code)] | ||
| 14 | mod mcp23017 { | ||
| 15 | pub const ADDR: u8 = 0x20; // default addr | ||
| 16 | |||
| 17 | macro_rules! mcpregs { | ||
| 18 | ($($name:ident : $val:expr),* $(,)?) => { | ||
| 19 | $( | ||
| 20 | pub const $name: u8 = $val; | ||
| 21 | )* | ||
| 22 | |||
| 23 | pub fn regname(reg: u8) -> &'static str { | ||
| 24 | match reg { | ||
| 25 | $( | ||
| 26 | $val => stringify!($name), | ||
| 27 | )* | ||
| 28 | _ => panic!("bad reg"), | ||
| 29 | } | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | // These are correct for IOCON.BANK=0 | ||
| 35 | mcpregs! { | ||
| 36 | IODIRA: 0x00, | ||
| 37 | IPOLA: 0x02, | ||
| 38 | GPINTENA: 0x04, | ||
| 39 | DEFVALA: 0x06, | ||
| 40 | INTCONA: 0x08, | ||
| 41 | IOCONA: 0x0A, | ||
| 42 | GPPUA: 0x0C, | ||
| 43 | INTFA: 0x0E, | ||
| 44 | INTCAPA: 0x10, | ||
| 45 | GPIOA: 0x12, | ||
| 46 | OLATA: 0x14, | ||
| 47 | IODIRB: 0x01, | ||
| 48 | IPOLB: 0x03, | ||
| 49 | GPINTENB: 0x05, | ||
| 50 | DEFVALB: 0x07, | ||
| 51 | INTCONB: 0x09, | ||
| 52 | IOCONB: 0x0B, | ||
| 53 | GPPUB: 0x0D, | ||
| 54 | INTFB: 0x0F, | ||
| 55 | INTCAPB: 0x11, | ||
| 56 | GPIOB: 0x13, | ||
| 57 | OLATB: 0x15, | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | #[embassy_executor::main] | ||
| 62 | async fn main(_spawner: Spawner) { | ||
| 63 | let p = embassy_rp::init(Default::default()); | ||
| 64 | |||
| 65 | let sda = p.PIN_14; | ||
| 66 | let scl = p.PIN_15; | ||
| 67 | let irq = interrupt::take!(I2C1_IRQ); | ||
| 68 | |||
| 69 | info!("set up i2c "); | ||
| 70 | let mut i2c = i2c::I2c::new_async(p.I2C1, scl, sda, irq, Config::default()); | ||
| 71 | |||
| 72 | use mcp23017::*; | ||
| 73 | |||
| 74 | info!("init mcp23017 config for IxpandO"); | ||
| 75 | // init - a outputs, b inputs | ||
| 76 | i2c.write(ADDR, &[IODIRA, 0x00]).await.unwrap(); | ||
| 77 | i2c.write(ADDR, &[IODIRB, 0xff]).await.unwrap(); | ||
| 78 | i2c.write(ADDR, &[GPPUB, 0xff]).await.unwrap(); // pullups | ||
| 79 | |||
| 80 | let mut val = 1; | ||
| 81 | loop { | ||
| 82 | let mut portb = [0]; | ||
| 83 | |||
| 84 | i2c.write_read(mcp23017::ADDR, &[GPIOB], &mut portb).await.unwrap(); | ||
| 85 | info!("portb = {:02x}", portb[0]); | ||
| 86 | i2c.write(mcp23017::ADDR, &[GPIOA, val | portb[0]]).await.unwrap(); | ||
| 87 | val = val.rotate_left(1); | ||
| 88 | |||
| 89 | // get a register dump | ||
| 90 | info!("getting register dump"); | ||
| 91 | let mut regs = [0; 22]; | ||
| 92 | i2c.write_read(ADDR, &[0], &mut regs).await.unwrap(); | ||
| 93 | // always get the regdump but only display it if portb'0 is set | ||
| 94 | if portb[0] & 1 != 0 { | ||
| 95 | for (idx, reg) in regs.into_iter().enumerate() { | ||
| 96 | info!("{} => {:02x}", regname(idx as u8), reg); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | Timer::after(Duration::from_millis(100)).await; | ||
| 101 | } | ||
| 102 | } | ||
diff --git a/examples/rp/src/bin/i2c_blocking.rs b/examples/rp/src/bin/i2c_blocking.rs new file mode 100644 index 000000000..7623e33c8 --- /dev/null +++ b/examples/rp/src/bin/i2c_blocking.rs | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_rp::i2c::{self, Config}; | ||
| 8 | use embassy_time::{Duration, Timer}; | ||
| 9 | use embedded_hal_1::i2c::I2c; | ||
| 10 | use {defmt_rtt as _, panic_probe as _}; | ||
| 11 | |||
| 12 | #[allow(dead_code)] | ||
| 13 | mod 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] | ||
| 41 | async 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 | } | ||
diff --git a/examples/rp/src/bin/spi_display.rs b/examples/rp/src/bin/spi_display.rs index 23cd4355e..778cad3fa 100644 --- a/examples/rp/src/bin/spi_display.rs +++ b/examples/rp/src/bin/spi_display.rs | |||
| @@ -108,9 +108,9 @@ mod shared_spi { | |||
| 108 | use core::cell::RefCell; | 108 | use core::cell::RefCell; |
| 109 | use core::fmt::Debug; | 109 | use core::fmt::Debug; |
| 110 | 110 | ||
| 111 | use embedded_hal_1::digital::blocking::OutputPin; | 111 | use embedded_hal_1::digital::OutputPin; |
| 112 | use embedded_hal_1::spi; | 112 | use embedded_hal_1::spi; |
| 113 | use embedded_hal_1::spi::blocking::SpiDevice; | 113 | use embedded_hal_1::spi::SpiDevice; |
| 114 | 114 | ||
| 115 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] | 115 | #[derive(Copy, Clone, Eq, PartialEq, Debug)] |
| 116 | pub enum SpiDeviceWithCsError<BUS, CS> { | 116 | pub enum SpiDeviceWithCsError<BUS, CS> { |
| @@ -153,7 +153,7 @@ mod shared_spi { | |||
| 153 | 153 | ||
| 154 | impl<'a, BUS, CS> SpiDevice for SpiDeviceWithCs<'a, BUS, CS> | 154 | impl<'a, BUS, CS> SpiDevice for SpiDeviceWithCs<'a, BUS, CS> |
| 155 | where | 155 | where |
| 156 | BUS: spi::blocking::SpiBusFlush, | 156 | BUS: spi::SpiBusFlush, |
| 157 | CS: OutputPin, | 157 | CS: OutputPin, |
| 158 | { | 158 | { |
| 159 | type Bus = BUS; | 159 | type Bus = BUS; |
| @@ -182,7 +182,7 @@ mod shared_spi { | |||
| 182 | 182 | ||
| 183 | /// Driver for the XPT2046 resistive touchscreen sensor | 183 | /// Driver for the XPT2046 resistive touchscreen sensor |
| 184 | mod touch { | 184 | mod touch { |
| 185 | use embedded_hal_1::spi::blocking::{SpiBus, SpiBusRead, SpiBusWrite, SpiDevice}; | 185 | use embedded_hal_1::spi::{SpiBus, SpiBusRead, SpiBusWrite, SpiDevice}; |
| 186 | 186 | ||
| 187 | struct Calibration { | 187 | struct Calibration { |
| 188 | x1: i32, | 188 | x1: i32, |
| @@ -246,8 +246,8 @@ mod touch { | |||
| 246 | 246 | ||
| 247 | mod my_display_interface { | 247 | mod my_display_interface { |
| 248 | use display_interface::{DataFormat, DisplayError, WriteOnlyDataCommand}; | 248 | use display_interface::{DataFormat, DisplayError, WriteOnlyDataCommand}; |
| 249 | use embedded_hal_1::digital::blocking::OutputPin; | 249 | use embedded_hal_1::digital::OutputPin; |
| 250 | use embedded_hal_1::spi::blocking::{SpiBusWrite, SpiDevice}; | 250 | use embedded_hal_1::spi::{SpiBusWrite, SpiDevice}; |
| 251 | 251 | ||
| 252 | /// SPI display interface. | 252 | /// SPI display interface. |
| 253 | /// | 253 | /// |
