diff options
| author | Jeremy Fitzhardinge <[email protected]> | 2022-10-17 21:50:40 -0700 |
|---|---|---|
| committer | Jeremy Fitzhardinge <[email protected]> | 2022-10-17 21:50:40 -0700 |
| commit | 02a3cdb507d535908bc0668a31526c05b5d01005 (patch) | |
| tree | d4e8492ba812ab18d953a630557fc0e25f0007bd | |
| parent | e4c2b2aa9a61e5135d2e129705c9bdc3b4cb73ae (diff) | |
Associate state with the instance rather than having a separate array
| -rw-r--r-- | embassy-rp/src/i2c.rs | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/embassy-rp/src/i2c.rs b/embassy-rp/src/i2c.rs index b9e4382c7..d6742f6a6 100644 --- a/embassy-rp/src/i2c.rs +++ b/embassy-rp/src/i2c.rs | |||
| @@ -70,8 +70,6 @@ impl<'d, T: Instance> I2c<'d, T, Blocking> { | |||
| 70 | } | 70 | } |
| 71 | } | 71 | } |
| 72 | 72 | ||
| 73 | static I2C_WAKER: [AtomicWaker; 2] = [AtomicWaker::new(), AtomicWaker::new()]; | ||
| 74 | |||
| 75 | impl<'d, T: Instance> I2c<'d, T, Async> { | 73 | impl<'d, T: Instance> I2c<'d, T, Async> { |
| 76 | pub fn new_async( | 74 | pub fn new_async( |
| 77 | peri: impl Peripheral<P = T> + 'd, | 75 | peri: impl Peripheral<P = T> + 'd, |
| @@ -109,7 +107,7 @@ impl<'d, T: Instance> I2c<'d, T, Async> { | |||
| 109 | let r = f(self); | 107 | let r = f(self); |
| 110 | 108 | ||
| 111 | if r.is_pending() { | 109 | if r.is_pending() { |
| 112 | I2C_WAKER[T::IDX].register(cx.waker()); | 110 | T::waker().register(cx.waker()); |
| 113 | g(self); | 111 | g(self); |
| 114 | } | 112 | } |
| 115 | r | 113 | r |
| @@ -122,7 +120,7 @@ impl<'d, T: Instance> I2c<'d, T, Async> { | |||
| 122 | let i2c = T::regs(); | 120 | let i2c = T::regs(); |
| 123 | i2c.ic_intr_mask().write_value(pac::i2c::regs::IcIntrMask::default()); | 121 | i2c.ic_intr_mask().write_value(pac::i2c::regs::IcIntrMask::default()); |
| 124 | 122 | ||
| 125 | I2C_WAKER[T::IDX].wake(); | 123 | T::waker().wake(); |
| 126 | } | 124 | } |
| 127 | 125 | ||
| 128 | async fn read_async_internal(&mut self, buffer: &mut [u8], restart: bool, send_stop: bool) -> Result<(), Error> { | 126 | async fn read_async_internal(&mut self, buffer: &mut [u8], restart: bool, send_stop: bool) -> Result<(), Error> { |
| @@ -809,16 +807,17 @@ fn i2c_reserved_addr(addr: u16) -> bool { | |||
| 809 | 807 | ||
| 810 | mod sealed { | 808 | mod sealed { |
| 811 | use embassy_cortex_m::interrupt::Interrupt; | 809 | use embassy_cortex_m::interrupt::Interrupt; |
| 810 | use embassy_sync::waitqueue::AtomicWaker; | ||
| 812 | 811 | ||
| 813 | pub trait Instance { | 812 | pub trait Instance { |
| 814 | const TX_DREQ: u8; | 813 | const TX_DREQ: u8; |
| 815 | const RX_DREQ: u8; | 814 | const RX_DREQ: u8; |
| 816 | const IDX: usize; | ||
| 817 | 815 | ||
| 818 | type Interrupt: Interrupt; | 816 | type Interrupt: Interrupt; |
| 819 | 817 | ||
| 820 | fn regs() -> crate::pac::i2c::I2c; | 818 | fn regs() -> crate::pac::i2c::I2c; |
| 821 | fn reset() -> crate::pac::resets::regs::Peripherals; | 819 | fn reset() -> crate::pac::resets::regs::Peripherals; |
| 820 | fn waker() -> &'static AtomicWaker; | ||
| 822 | } | 821 | } |
| 823 | 822 | ||
| 824 | pub trait Mode {} | 823 | pub trait Mode {} |
| @@ -845,11 +844,10 @@ impl_mode!(Async); | |||
| 845 | pub trait Instance: sealed::Instance {} | 844 | pub trait Instance: sealed::Instance {} |
| 846 | 845 | ||
| 847 | macro_rules! impl_instance { | 846 | macro_rules! impl_instance { |
| 848 | ($type:ident, $idx:expr, $irq:ident, $reset:ident, $tx_dreq:expr, $rx_dreq:expr) => { | 847 | ($type:ident, $irq:ident, $reset:ident, $tx_dreq:expr, $rx_dreq:expr) => { |
| 849 | impl sealed::Instance for peripherals::$type { | 848 | impl sealed::Instance for peripherals::$type { |
| 850 | const TX_DREQ: u8 = $tx_dreq; | 849 | const TX_DREQ: u8 = $tx_dreq; |
| 851 | const RX_DREQ: u8 = $rx_dreq; | 850 | const RX_DREQ: u8 = $rx_dreq; |
| 852 | const IDX: usize = $idx; | ||
| 853 | 851 | ||
| 854 | type Interrupt = crate::interrupt::$irq; | 852 | type Interrupt = crate::interrupt::$irq; |
| 855 | 853 | ||
| @@ -864,13 +862,20 @@ macro_rules! impl_instance { | |||
| 864 | ret.$reset(true); | 862 | ret.$reset(true); |
| 865 | ret | 863 | ret |
| 866 | } | 864 | } |
| 865 | |||
| 866 | #[inline] | ||
| 867 | fn waker() -> &'static AtomicWaker { | ||
| 868 | static WAKER: AtomicWaker = AtomicWaker::new(); | ||
| 869 | |||
| 870 | &WAKER | ||
| 871 | } | ||
| 867 | } | 872 | } |
| 868 | impl Instance for peripherals::$type {} | 873 | impl Instance for peripherals::$type {} |
| 869 | }; | 874 | }; |
| 870 | } | 875 | } |
| 871 | 876 | ||
| 872 | impl_instance!(I2C0, 0, I2C0_IRQ, set_i2c0, 32, 33); | 877 | impl_instance!(I2C0, I2C0_IRQ, set_i2c0, 32, 33); |
| 873 | impl_instance!(I2C1, 1, I2C1_IRQ, set_i2c1, 34, 35); | 878 | impl_instance!(I2C1, I2C1_IRQ, set_i2c1, 34, 35); |
| 874 | 879 | ||
| 875 | pub trait SdaPin<T: Instance>: sealed::SdaPin<T> + crate::gpio::Pin {} | 880 | pub trait SdaPin<T: Instance>: sealed::SdaPin<T> + crate::gpio::Pin {} |
| 876 | pub trait SclPin<T: Instance>: sealed::SclPin<T> + crate::gpio::Pin {} | 881 | pub trait SclPin<T: Instance>: sealed::SclPin<T> + crate::gpio::Pin {} |
