Unify i2cv1 definition with i2cv2

This commit is contained in:
chemicstry 2022-10-24 22:34:10 +03:00
parent 6062978d58
commit 33f75419e5
2 changed files with 30 additions and 6 deletions

View file

@ -1,8 +1,9 @@
use core::marker::PhantomData; use core::marker::PhantomData;
use embassy_embedded_hal::SetConfig; use embassy_embedded_hal::SetConfig;
use embassy_hal_common::into_ref; use embassy_hal_common::{into_ref, PeripheralRef};
use crate::dma::NoDma;
use crate::gpio::sealed::AFType; use crate::gpio::sealed::AFType;
use crate::gpio::Pull; use crate::gpio::Pull;
use crate::i2c::{Error, Instance, SclPin, SdaPin}; use crate::i2c::{Error, Instance, SclPin, SdaPin};
@ -34,19 +35,26 @@ impl State {
} }
} }
pub struct I2c<'d, T: Instance> { pub struct I2c<'d, T: Instance, TXDMA = NoDma, RXDMA = NoDma> {
phantom: PhantomData<&'d mut T>, phantom: PhantomData<&'d mut T>,
#[allow(dead_code)]
tx_dma: PeripheralRef<'d, TXDMA>,
#[allow(dead_code)]
rx_dma: PeripheralRef<'d, RXDMA>,
} }
impl<'d, T: Instance> I2c<'d, T> { impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
pub fn new( pub fn new(
_peri: impl Peripheral<P = T> + 'd, _peri: impl Peripheral<P = T> + 'd,
scl: impl Peripheral<P = impl SclPin<T>> + 'd, scl: impl Peripheral<P = impl SclPin<T>> + 'd,
sda: impl Peripheral<P = impl SdaPin<T>> + 'd, sda: impl Peripheral<P = impl SdaPin<T>> + 'd,
_irq: impl Peripheral<P = T::Interrupt> + 'd,
tx_dma: impl Peripheral<P = TXDMA> + 'd,
rx_dma: impl Peripheral<P = RXDMA> + 'd,
freq: Hertz, freq: Hertz,
config: Config, config: Config,
) -> Self { ) -> Self {
into_ref!(scl, sda); into_ref!(scl, sda, tx_dma, rx_dma);
T::enable(); T::enable();
T::reset(); T::reset();
@ -99,7 +107,11 @@ impl<'d, T: Instance> I2c<'d, T> {
}); });
} }
Self { phantom: PhantomData } Self {
phantom: PhantomData,
tx_dma,
rx_dma,
}
} }
unsafe fn check_and_clear_error_flags(&self) -> Result<i2c::regs::Sr1, Error> { unsafe fn check_and_clear_error_flags(&self) -> Result<i2c::regs::Sr1, Error> {

View file

@ -4,7 +4,9 @@
use defmt::*; use defmt::*;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_stm32::dma::NoDma;
use embassy_stm32::i2c::{Error, I2c, TimeoutI2c}; use embassy_stm32::i2c::{Error, I2c, TimeoutI2c};
use embassy_stm32::interrupt;
use embassy_stm32::time::Hertz; use embassy_stm32::time::Hertz;
use embassy_time::Duration; use embassy_time::Duration;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
@ -17,7 +19,17 @@ async fn main(_spawner: Spawner) -> ! {
info!("Hello world!"); info!("Hello world!");
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let mut i2c = I2c::new(p.I2C2, p.PB10, p.PB11, Hertz(100_000), Default::default()); let irq = interrupt::take!(I2C2_EV);
let mut i2c = I2c::new(
p.I2C2,
p.PB10,
p.PB11,
irq,
NoDma,
NoDma,
Hertz(100_000),
Default::default(),
);
let mut timeout_i2c = TimeoutI2c::new(&mut i2c, Duration::from_millis(1000)); let mut timeout_i2c = TimeoutI2c::new(&mut i2c, Duration::from_millis(1000));
let mut data = [0u8; 1]; let mut data = [0u8; 1];