diff options
| author | kalkyl <[email protected]> | 2022-11-13 22:15:19 +0100 |
|---|---|---|
| committer | kalkyl <[email protected]> | 2022-11-13 22:15:19 +0100 |
| commit | eba42cb5f4c4dc1be54c27729325e982d85fc8b0 (patch) | |
| tree | 1d99f0e9ea675e67743d839008d75f0682e4e855 /examples | |
| parent | d05979c7085675c33615700f6590b1543ed69323 (diff) | |
embassy-nrf: Add TWIS module
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf/src/bin/twis.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/examples/nrf/src/bin/twis.rs b/examples/nrf/src/bin/twis.rs new file mode 100644 index 000000000..f85173c08 --- /dev/null +++ b/examples/nrf/src/bin/twis.rs | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | //! TWIS example | ||
| 2 | |||
| 3 | #![no_std] | ||
| 4 | #![no_main] | ||
| 5 | #![feature(type_alias_impl_trait)] | ||
| 6 | |||
| 7 | use defmt::*; | ||
| 8 | use embassy_executor::Spawner; | ||
| 9 | use embassy_nrf::interrupt; | ||
| 10 | use embassy_nrf::twis::{self, Command, Twis}; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | #[embassy_executor::main] | ||
| 14 | async fn main(_spawner: Spawner) { | ||
| 15 | let p = embassy_nrf::init(Default::default()); | ||
| 16 | |||
| 17 | let irq = interrupt::take!(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); | ||
| 18 | let mut config = twis::Config::default(); | ||
| 19 | // Set i2c address | ||
| 20 | config.addr0 = 0x55; | ||
| 21 | let mut i2c = Twis::new(p.TWISPI0, irq, p.P0_03, p.P0_04, config); | ||
| 22 | |||
| 23 | info!("Listening..."); | ||
| 24 | loop { | ||
| 25 | let mut buf = [0u8; 16]; | ||
| 26 | let tx_buf = [1, 2, 3, 4, 5, 6, 7, 8]; | ||
| 27 | match i2c.listen(&mut buf).await { | ||
| 28 | Ok(Command::Read) => { | ||
| 29 | info!("Got READ command. Writing back data:\n{:?}\n", tx_buf); | ||
| 30 | if let Err(e) = i2c.write(&tx_buf).await { | ||
| 31 | error!("{:?}", e); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | Ok(Command::Write(n)) => info!("Got WRITE command with data:\n{:?}\n", buf[..n]), | ||
| 35 | Ok(Command::WriteRead(n)) => { | ||
| 36 | info!("Got WRITE/READ command with data:\n{:?}", buf[..n]); | ||
| 37 | info!("Writing back data:\n{:?}\n", tx_buf); | ||
| 38 | if let Err(e) = i2c.write(&tx_buf).await { | ||
| 39 | error!("{:?}", e); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | Err(e) => error!("{:?}", e), | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
