|
| 1 | +#![no_std] |
| 2 | +#![no_main] |
| 3 | + |
| 4 | +use defmt_rtt as _; |
| 5 | +use panic_probe as _; |
| 6 | + |
| 7 | +use bsp::hal; |
| 8 | +use bsp::pac; |
| 9 | +use feather_m0 as bsp; |
| 10 | +use fugit::MillisDuration; |
| 11 | +use hal::ehal_async::i2c::I2c; |
| 12 | +use hal::{ |
| 13 | + clock::GenericClockController, |
| 14 | + dmac::{DmaController, PriorityLevel}, |
| 15 | + prelude::*, |
| 16 | + sercom::{i2c, Sercom3}, |
| 17 | +}; |
| 18 | +use rtic_monotonics::systick::Systick; |
| 19 | + |
| 20 | +atsamd_hal::bind_interrupts!(struct Irqs { |
| 21 | + SERCOM3 => atsamd_hal::sercom::i2c::InterruptHandler<Sercom3>; |
| 22 | + DMAC => atsamd_hal::dmac::InterruptHandler; |
| 23 | +}); |
| 24 | + |
| 25 | +#[embassy_executor::main] |
| 26 | +async fn main(_s: embassy_executor::Spawner) { |
| 27 | + let mut peripherals = pac::Peripherals::take().unwrap(); |
| 28 | + let _core = pac::CorePeripherals::take().unwrap(); |
| 29 | + |
| 30 | + let mut clocks = GenericClockController::with_external_32kosc( |
| 31 | + peripherals.gclk, |
| 32 | + &mut peripherals.pm, |
| 33 | + &mut peripherals.sysctrl, |
| 34 | + &mut peripherals.nvmctrl, |
| 35 | + ); |
| 36 | + |
| 37 | + let pins = bsp::Pins::new(peripherals.port); |
| 38 | + |
| 39 | + // Take SDA and SCL |
| 40 | + let (sda, scl) = (pins.sda, pins.scl); |
| 41 | + let i2c_sercom = bsp::periph_alias!(peripherals.i2c_sercom); |
| 42 | + |
| 43 | + // Initialize DMA Controller |
| 44 | + let dmac = DmaController::init(peripherals.dmac, &mut peripherals.pm); |
| 45 | + |
| 46 | + // Turn dmac into an async controller |
| 47 | + let mut dmac = dmac.into_future(Irqs); |
| 48 | + // Get individual handles to DMA channels |
| 49 | + let channels = dmac.split(); |
| 50 | + |
| 51 | + // Initialize DMA Channel 0 |
| 52 | + let channel0 = channels.0.init(PriorityLevel::Lvl0); |
| 53 | + |
| 54 | + let gclk0 = clocks.gclk0(); |
| 55 | + let sercom3_clock = &clocks.sercom3_core(&gclk0).unwrap(); |
| 56 | + let pads = i2c::Pads::new(sda, scl); |
| 57 | + let mut i2c = i2c::Config::new(&peripherals.pm, i2c_sercom, pads, sercom3_clock.freq()) |
| 58 | + .baud(100.kHz()) |
| 59 | + .enable() |
| 60 | + .into_future(Irqs) |
| 61 | + .with_dma_channel(channel0); |
| 62 | + |
| 63 | + loop { |
| 64 | + defmt::info!("Sending 0x00 to I2C device..."); |
| 65 | + // This test is based on the BMP388 barometer. Feel free to use any I2C |
| 66 | + // peripheral you have on hand. |
| 67 | + i2c.write(0x76, &[0x00]).await.unwrap(); |
| 68 | + |
| 69 | + let mut buffer = [0xff; 4]; |
| 70 | + i2c.read(0x76, &mut buffer).await.unwrap(); |
| 71 | + defmt::info!("Read buffer: {:#x}", buffer); |
| 72 | + Systick::delay(MillisDuration::<u32>::from_ticks(500).convert()).await; |
| 73 | + } |
| 74 | +} |
0 commit comments