diff options
| author | nerwalt <[email protected]> | 2024-07-01 10:03:24 -0600 |
|---|---|---|
| committer | nerwalt <[email protected]> | 2024-07-01 10:03:24 -0600 |
| commit | 98263ac220102a60c1f7cd3a882acafbb59b53f1 (patch) | |
| tree | 74166685aa25a5ef8a859ac966dfac0e071513da /examples | |
| parent | e9bbfb349c15a39e8de4bf1831de0224a3d2bff0 (diff) | |
| parent | 3c6bf3a31a951fcea31e39390ba4f0f073144933 (diff) | |
Merge branch 'main' into nrf9151
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rp/src/bin/i2c_async_embassy.rs | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/examples/rp/src/bin/i2c_async_embassy.rs b/examples/rp/src/bin/i2c_async_embassy.rs new file mode 100644 index 000000000..a65b71b9f --- /dev/null +++ b/examples/rp/src/bin/i2c_async_embassy.rs | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | //! This example shows how to communicate asynchronous using i2c with external chip. | ||
| 2 | //! | ||
| 3 | //! It's using embassy's functions directly instead of traits from embedded_hal_async::i2c::I2c. | ||
| 4 | //! While most of i2c devices are addressed using 7 bits, an extension allows 10 bits too. | ||
| 5 | |||
| 6 | #![no_std] | ||
| 7 | #![no_main] | ||
| 8 | |||
| 9 | use defmt::*; | ||
| 10 | use embassy_rp::i2c::InterruptHandler; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | // Our anonymous hypotetical temperature sensor could be: | ||
| 14 | // a 12-bit sensor, with 100ms startup time, range of -40*C - 125*C, and precision 0.25*C | ||
| 15 | // It requires no configuration or calibration, works with all i2c bus speeds, | ||
| 16 | // never stretches clock or does anything complicated. Replies with one u16. | ||
| 17 | // It requires only one write to take it out of suspend mode, and stays on. | ||
| 18 | // Often result would be just on 12 bits, but here we'll simplify it to 16. | ||
| 19 | |||
| 20 | enum UncomplicatedSensorId { | ||
| 21 | A(UncomplicatedSensorU8), | ||
| 22 | B(UncomplicatedSensorU16), | ||
| 23 | } | ||
| 24 | enum UncomplicatedSensorU8 { | ||
| 25 | First = 0x48, | ||
| 26 | } | ||
| 27 | enum UncomplicatedSensorU16 { | ||
| 28 | Other = 0x0049, | ||
| 29 | } | ||
| 30 | |||
| 31 | impl Into<u16> for UncomplicatedSensorU16 { | ||
| 32 | fn into(self) -> u16 { | ||
| 33 | self as u16 | ||
| 34 | } | ||
| 35 | } | ||
| 36 | impl Into<u16> for UncomplicatedSensorU8 { | ||
| 37 | fn into(self) -> u16 { | ||
| 38 | 0x48 | ||
| 39 | } | ||
| 40 | } | ||
| 41 | impl From<UncomplicatedSensorId> for u16 { | ||
| 42 | fn from(t: UncomplicatedSensorId) -> Self { | ||
| 43 | match t { | ||
| 44 | UncomplicatedSensorId::A(x) => x.into(), | ||
| 45 | UncomplicatedSensorId::B(x) => x.into(), | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | embassy_rp::bind_interrupts!(struct Irqs { | ||
| 51 | I2C1_IRQ => InterruptHandler<embassy_rp::peripherals::I2C1>; | ||
| 52 | }); | ||
| 53 | |||
| 54 | #[embassy_executor::main] | ||
| 55 | async fn main(_task_spawner: embassy_executor::Spawner) { | ||
| 56 | let p = embassy_rp::init(Default::default()); | ||
| 57 | let sda = p.PIN_14; | ||
| 58 | let scl = p.PIN_15; | ||
| 59 | let config = embassy_rp::i2c::Config::default(); | ||
| 60 | let mut bus = embassy_rp::i2c::I2c::new_async(p.I2C1, scl, sda, Irqs, config); | ||
| 61 | |||
| 62 | const WAKEYWAKEY: u16 = 0xBABE; | ||
| 63 | let mut result: [u8; 2] = [0, 0]; | ||
| 64 | // wait for sensors to initialize | ||
| 65 | embassy_time::Timer::after(embassy_time::Duration::from_millis(100)).await; | ||
| 66 | |||
| 67 | let _res_1 = bus | ||
| 68 | .write_async(UncomplicatedSensorU8::First, WAKEYWAKEY.to_be_bytes()) | ||
| 69 | .await; | ||
| 70 | let _res_2 = bus | ||
| 71 | .write_async(UncomplicatedSensorU16::Other, WAKEYWAKEY.to_be_bytes()) | ||
| 72 | .await; | ||
| 73 | |||
| 74 | loop { | ||
| 75 | let s1 = UncomplicatedSensorId::A(UncomplicatedSensorU8::First); | ||
| 76 | let s2 = UncomplicatedSensorId::B(UncomplicatedSensorU16::Other); | ||
| 77 | let sensors = [s1, s2]; | ||
| 78 | for sensor in sensors { | ||
| 79 | if bus.read_async(sensor, &mut result).await.is_ok() { | ||
| 80 | info!("Result {}", u16::from_be_bytes(result.into())); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | embassy_time::Timer::after(embassy_time::Duration::from_millis(200)).await; | ||
| 84 | } | ||
| 85 | } | ||
