2021-05-26 18:15:37 +02:00
|
|
|
//! Example on how to read a 24C/24LC i2c eeprom with low power consumption.
|
|
|
|
//! The eeprom is read every 1 second, while ensuring lowest possible power while
|
|
|
|
//! sleeping between reads.
|
|
|
|
//!
|
|
|
|
//! Connect SDA to P0.03, SCL to P0.04
|
|
|
|
|
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use core::mem;
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2021-11-16 12:13:43 +01:00
|
|
|
use defmt::*;
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_executor::Spawner;
|
2021-05-26 18:15:37 +02:00
|
|
|
use embassy_nrf::twim::{self, Twim};
|
2023-03-05 22:12:34 +01:00
|
|
|
use embassy_nrf::{bind_interrupts, peripherals};
|
2023-10-15 01:57:25 +02:00
|
|
|
use embassy_time::Timer;
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2022-04-02 04:35:06 +02:00
|
|
|
|
2021-05-26 18:15:37 +02:00
|
|
|
const ADDRESS: u8 = 0x50;
|
|
|
|
|
2023-03-05 22:12:34 +01:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0 => twim::InterruptHandler<peripherals::TWISPI0>;
|
|
|
|
});
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::main]
|
2022-08-17 18:49:55 +02:00
|
|
|
async fn main(_p: Spawner) {
|
|
|
|
let mut p = embassy_nrf::init(Default::default());
|
2021-05-26 18:15:37 +02:00
|
|
|
info!("Started!");
|
|
|
|
|
|
|
|
loop {
|
|
|
|
info!("Initializing TWI...");
|
|
|
|
let config = twim::Config::default();
|
|
|
|
|
|
|
|
// Create the TWIM instance with borrowed singletons, so they're not consumed.
|
2023-03-05 22:12:34 +01:00
|
|
|
let mut twi = Twim::new(&mut p.TWISPI0, Irqs, &mut p.P0_03, &mut p.P0_04, config);
|
2021-05-26 18:15:37 +02:00
|
|
|
|
|
|
|
info!("Reading...");
|
|
|
|
|
|
|
|
let mut buf = [0u8; 16];
|
2022-01-13 20:47:28 +01:00
|
|
|
unwrap!(twi.blocking_write_read(ADDRESS, &mut [0x00], &mut buf));
|
2021-05-26 18:15:37 +02:00
|
|
|
|
|
|
|
info!("Read: {=[u8]:x}", buf);
|
|
|
|
|
|
|
|
// Drop the TWIM instance. This disables the peripehral and deconfigures the pins.
|
|
|
|
// This clears the borrow on the singletons, so they can now be used again.
|
|
|
|
mem::drop(twi);
|
|
|
|
|
|
|
|
// Sleep for 1 second. The executor ensures the core sleeps with a WFE when it has nothing to do.
|
|
|
|
// During this sleep, the nRF chip should only use ~3uA
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_secs(1).await;
|
2021-05-26 18:15:37 +02:00
|
|
|
}
|
|
|
|
}
|