diff options
Diffstat (limited to 'examples/nrf54l15/src/bin/twim.rs')
| -rw-r--r-- | examples/nrf54l15/src/bin/twim.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/examples/nrf54l15/src/bin/twim.rs b/examples/nrf54l15/src/bin/twim.rs new file mode 100644 index 000000000..53b85034e --- /dev/null +++ b/examples/nrf54l15/src/bin/twim.rs | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | //! Example on how to read a 24C/24LC i2c eeprom. | ||
| 2 | //! | ||
| 3 | //! Connect SDA to P0.03, SCL to P0.04 | ||
| 4 | |||
| 5 | #![no_std] | ||
| 6 | #![no_main] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_nrf::twim::{self, Twim}; | ||
| 11 | use embassy_nrf::{bind_interrupts, peripherals}; | ||
| 12 | use static_cell::ConstStaticCell; | ||
| 13 | use {defmt_rtt as _, panic_probe as _}; | ||
| 14 | |||
| 15 | const ADDRESS: u8 = 0x18; | ||
| 16 | const WHOAMI: u8 = 0x0F; | ||
| 17 | |||
| 18 | bind_interrupts!(struct Irqs { | ||
| 19 | SERIAL20 => twim::InterruptHandler<peripherals::SERIAL20>; | ||
| 20 | }); | ||
| 21 | |||
| 22 | #[embassy_executor::main] | ||
| 23 | async fn main(_spawner: Spawner) { | ||
| 24 | let p = embassy_nrf::init(Default::default()); | ||
| 25 | info!("Initializing TWI..."); | ||
| 26 | let config = twim::Config::default(); | ||
| 27 | static RAM_BUFFER: ConstStaticCell<[u8; 16]> = ConstStaticCell::new([0; 16]); | ||
| 28 | let mut twi = Twim::new(p.SERIAL20, Irqs, p.P1_13, p.P1_12, config, RAM_BUFFER.take()); | ||
| 29 | |||
| 30 | info!("Reading..."); | ||
| 31 | |||
| 32 | let mut data = [0u8; 1]; | ||
| 33 | match twi.write_read(ADDRESS, &[WHOAMI], &mut data).await { | ||
| 34 | Ok(()) => info!("Whoami: {}", data[0]), | ||
| 35 | Err(e) => error!("I2c Error: {:?}", e), | ||
| 36 | } | ||
| 37 | } | ||
