diff options
Diffstat (limited to 'examples/nrf54l15/src/bin/twis.rs')
| -rw-r--r-- | examples/nrf54l15/src/bin/twis.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/examples/nrf54l15/src/bin/twis.rs b/examples/nrf54l15/src/bin/twis.rs new file mode 100644 index 000000000..34c04aee3 --- /dev/null +++ b/examples/nrf54l15/src/bin/twis.rs | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | //! TWIS example | ||
| 2 | |||
| 3 | #![no_std] | ||
| 4 | #![no_main] | ||
| 5 | |||
| 6 | use defmt::*; | ||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_nrf::twis::{self, Command, Twis}; | ||
| 9 | use embassy_nrf::{bind_interrupts, peripherals}; | ||
| 10 | use {defmt_rtt as _, panic_probe as _}; | ||
| 11 | |||
| 12 | bind_interrupts!(struct Irqs { | ||
| 13 | SERIAL20 => twis::InterruptHandler<peripherals::SERIAL20>; | ||
| 14 | }); | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | let p = embassy_nrf::init(Default::default()); | ||
| 19 | |||
| 20 | let mut config = twis::Config::default(); | ||
| 21 | config.address0 = 0x55; // Set i2c address | ||
| 22 | let mut i2c = Twis::new(p.SERIAL20, Irqs, p.P0_03, p.P0_04, config); | ||
| 23 | |||
| 24 | info!("Listening..."); | ||
| 25 | loop { | ||
| 26 | let response = [1, 2, 3, 4, 5, 6, 7, 8]; | ||
| 27 | // This buffer is used if the i2c master performs a Write or WriteRead | ||
| 28 | let mut buf = [0u8; 16]; | ||
| 29 | match i2c.listen(&mut buf).await { | ||
| 30 | Ok(Command::Read) => { | ||
| 31 | info!("Got READ command. Respond with data:\n{:?}\n", response); | ||
| 32 | if let Err(e) = i2c.respond_to_read(&response).await { | ||
| 33 | error!("{:?}", e); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | Ok(Command::Write(n)) => info!("Got WRITE command with data:\n{:?}\n", buf[..n]), | ||
| 37 | Ok(Command::WriteRead(n)) => { | ||
| 38 | info!("Got WRITE/READ command with data:\n{:?}", buf[..n]); | ||
| 39 | info!("Respond with data:\n{:?}\n", response); | ||
| 40 | if let Err(e) = i2c.respond_to_read(&response).await { | ||
| 41 | error!("{:?}", e); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | Err(e) => error!("{:?}", e), | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
