diff options
| author | chemicstry <[email protected]> | 2022-10-24 22:34:10 +0300 |
|---|---|---|
| committer | chemicstry <[email protected]> | 2022-10-24 22:34:10 +0300 |
| commit | 33f75419e542ef52d7d6a1403c9e3dbfd1c39abe (patch) | |
| tree | ec17137d32ba3215799fb39977e6561fb84b6345 | |
| parent | 6062978d58915e1d0c7db103365f0048f836babc (diff) | |
Unify i2cv1 definition with i2cv2
| -rw-r--r-- | embassy-stm32/src/i2c/v1.rs | 22 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/i2c.rs | 14 |
2 files changed, 30 insertions, 6 deletions
diff --git a/embassy-stm32/src/i2c/v1.rs b/embassy-stm32/src/i2c/v1.rs index 92a898824..f140e2b0d 100644 --- a/embassy-stm32/src/i2c/v1.rs +++ b/embassy-stm32/src/i2c/v1.rs | |||
| @@ -1,8 +1,9 @@ | |||
| 1 | use core::marker::PhantomData; | 1 | use core::marker::PhantomData; |
| 2 | 2 | ||
| 3 | use embassy_embedded_hal::SetConfig; | 3 | use embassy_embedded_hal::SetConfig; |
| 4 | use embassy_hal_common::into_ref; | 4 | use embassy_hal_common::{into_ref, PeripheralRef}; |
| 5 | 5 | ||
| 6 | use crate::dma::NoDma; | ||
| 6 | use crate::gpio::sealed::AFType; | 7 | use crate::gpio::sealed::AFType; |
| 7 | use crate::gpio::Pull; | 8 | use crate::gpio::Pull; |
| 8 | use crate::i2c::{Error, Instance, SclPin, SdaPin}; | 9 | use crate::i2c::{Error, Instance, SclPin, SdaPin}; |
| @@ -34,19 +35,26 @@ impl State { | |||
| 34 | } | 35 | } |
| 35 | } | 36 | } |
| 36 | 37 | ||
| 37 | pub struct I2c<'d, T: Instance> { | 38 | pub struct I2c<'d, T: Instance, TXDMA = NoDma, RXDMA = NoDma> { |
| 38 | phantom: PhantomData<&'d mut T>, | 39 | phantom: PhantomData<&'d mut T>, |
| 40 | #[allow(dead_code)] | ||
| 41 | tx_dma: PeripheralRef<'d, TXDMA>, | ||
| 42 | #[allow(dead_code)] | ||
| 43 | rx_dma: PeripheralRef<'d, RXDMA>, | ||
| 39 | } | 44 | } |
| 40 | 45 | ||
| 41 | impl<'d, T: Instance> I2c<'d, T> { | 46 | impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> { |
| 42 | pub fn new( | 47 | pub fn new( |
| 43 | _peri: impl Peripheral<P = T> + 'd, | 48 | _peri: impl Peripheral<P = T> + 'd, |
| 44 | scl: impl Peripheral<P = impl SclPin<T>> + 'd, | 49 | scl: impl Peripheral<P = impl SclPin<T>> + 'd, |
| 45 | sda: impl Peripheral<P = impl SdaPin<T>> + 'd, | 50 | sda: impl Peripheral<P = impl SdaPin<T>> + 'd, |
| 51 | _irq: impl Peripheral<P = T::Interrupt> + 'd, | ||
| 52 | tx_dma: impl Peripheral<P = TXDMA> + 'd, | ||
| 53 | rx_dma: impl Peripheral<P = RXDMA> + 'd, | ||
| 46 | freq: Hertz, | 54 | freq: Hertz, |
| 47 | config: Config, | 55 | config: Config, |
| 48 | ) -> Self { | 56 | ) -> Self { |
| 49 | into_ref!(scl, sda); | 57 | into_ref!(scl, sda, tx_dma, rx_dma); |
| 50 | 58 | ||
| 51 | T::enable(); | 59 | T::enable(); |
| 52 | T::reset(); | 60 | T::reset(); |
| @@ -99,7 +107,11 @@ impl<'d, T: Instance> I2c<'d, T> { | |||
| 99 | }); | 107 | }); |
| 100 | } | 108 | } |
| 101 | 109 | ||
| 102 | Self { phantom: PhantomData } | 110 | Self { |
| 111 | phantom: PhantomData, | ||
| 112 | tx_dma, | ||
| 113 | rx_dma, | ||
| 114 | } | ||
| 103 | } | 115 | } |
| 104 | 116 | ||
| 105 | unsafe fn check_and_clear_error_flags(&self) -> Result<i2c::regs::Sr1, Error> { | 117 | unsafe fn check_and_clear_error_flags(&self) -> Result<i2c::regs::Sr1, Error> { |
diff --git a/examples/stm32f4/src/bin/i2c.rs b/examples/stm32f4/src/bin/i2c.rs index 99e3cecfc..12965d2b8 100644 --- a/examples/stm32f4/src/bin/i2c.rs +++ b/examples/stm32f4/src/bin/i2c.rs | |||
| @@ -4,7 +4,9 @@ | |||
| 4 | 4 | ||
| 5 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_stm32::dma::NoDma; | ||
| 7 | use embassy_stm32::i2c::{Error, I2c, TimeoutI2c}; | 8 | use embassy_stm32::i2c::{Error, I2c, TimeoutI2c}; |
| 9 | use embassy_stm32::interrupt; | ||
| 8 | use embassy_stm32::time::Hertz; | 10 | use embassy_stm32::time::Hertz; |
| 9 | use embassy_time::Duration; | 11 | use embassy_time::Duration; |
| 10 | use {defmt_rtt as _, panic_probe as _}; | 12 | use {defmt_rtt as _, panic_probe as _}; |
| @@ -17,7 +19,17 @@ async fn main(_spawner: Spawner) -> ! { | |||
| 17 | info!("Hello world!"); | 19 | info!("Hello world!"); |
| 18 | let p = embassy_stm32::init(Default::default()); | 20 | let p = embassy_stm32::init(Default::default()); |
| 19 | 21 | ||
| 20 | let mut i2c = I2c::new(p.I2C2, p.PB10, p.PB11, Hertz(100_000), Default::default()); | 22 | let irq = interrupt::take!(I2C2_EV); |
| 23 | let mut i2c = I2c::new( | ||
| 24 | p.I2C2, | ||
| 25 | p.PB10, | ||
| 26 | p.PB11, | ||
| 27 | irq, | ||
| 28 | NoDma, | ||
| 29 | NoDma, | ||
| 30 | Hertz(100_000), | ||
| 31 | Default::default(), | ||
| 32 | ); | ||
| 21 | let mut timeout_i2c = TimeoutI2c::new(&mut i2c, Duration::from_millis(1000)); | 33 | let mut timeout_i2c = TimeoutI2c::new(&mut i2c, Duration::from_millis(1000)); |
| 22 | 34 | ||
| 23 | let mut data = [0u8; 1]; | 35 | let mut data = [0u8; 1]; |
