embassy-rp

Crates

git

Versions

rp2040

Flavors

embassy_rp

Trait Peripheral

Source
pub trait Peripheral: Sized {
    type P;

    // Required method
    unsafe fn clone_unchecked(&self) -> Self::P;

    // Provided method
    fn into_ref<'a>(self) -> PeripheralRef<'a, Self::P>
       where Self: 'a { ... }
}
Expand description

Trait for any type that can be used as a peripheral of type P.

This is used in driver constructors, to allow passing either owned peripherals (e.g. TWISPI0), or borrowed peripherals (e.g. &mut TWISPI0).

For example, if you have a driver with a constructor like this:

impl<'d, T: Instance> Twim<'d, T> {
    pub fn new(
        twim: impl Peripheral<P = T> + 'd,
        irq: impl Peripheral<P = T::Interrupt> + 'd,
        sda: impl Peripheral<P = impl GpioPin> + 'd,
        scl: impl Peripheral<P = impl GpioPin> + 'd,
        config: Config,
    ) -> Self { .. }
}

You may call it with owned peripherals, which yields an instance that can live forever ('static):

let mut twi: Twim<'static, ...> = Twim::new(p.TWISPI0, irq, p.P0_03, p.P0_04, config);

Or you may call it with borrowed peripherals, which yields an instance that can only live for as long as the borrows last:

let mut twi: Twim<'_, ...> = Twim::new(&mut p.TWISPI0, &mut irq, &mut p.P0_03, &mut p.P0_04, config);

§Implementation details, for HAL authors

When writing a HAL, the intended way to use this trait is to take impl Peripheral<P = ..> in the HAL’s public API (such as driver constructors), calling .into_ref() to obtain a PeripheralRef, and storing that in the driver struct.

.into_ref() on an owned T yields a PeripheralRef<'static, T>. .into_ref() on an &'a mut T yields a PeripheralRef<'a, T>.

Required Associated Types§

Source

type P

Peripheral singleton type

Required Methods§

Source

unsafe fn clone_unchecked(&self) -> Self::P

Unsafely clone (duplicate) a peripheral singleton.

§Safety

This returns an owned clone of the peripheral. You must manually ensure only one copy of the peripheral is in use at a time. For example, don’t create two SPI drivers on SPI1, because they will “fight” each other.

You should strongly prefer using into_ref() instead. It returns a PeripheralRef, which allows the borrow checker to enforce this at compile time.

Provided Methods§

Source

fn into_ref<'a>(self) -> PeripheralRef<'a, Self::P>
where Self: 'a,

Convert a value into a PeripheralRef.

When called on an owned T, yields a PeripheralRef<'static, T>. When called on an &'a mut T, yields a PeripheralRef<'a, T>.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl Peripheral for AnyChannel

Source§

impl Peripheral for AnyPin

Source§

impl Peripheral for ADC

Source§

type P = ADC

Source§

impl Peripheral for ADC_TEMP_SENSOR

Source§

impl Peripheral for BOOTSEL

Source§

impl Peripheral for CORE1

Source§

impl Peripheral for DMA_CH0

Source§

impl Peripheral for DMA_CH1

Source§

impl Peripheral for DMA_CH2

Source§

impl Peripheral for DMA_CH3

Source§

impl Peripheral for DMA_CH4

Source§

impl Peripheral for DMA_CH5

Source§

impl Peripheral for DMA_CH6

Source§

impl Peripheral for DMA_CH7

Source§

impl Peripheral for DMA_CH8

Source§

impl Peripheral for DMA_CH9

Source§

impl Peripheral for DMA_CH10

Source§

impl Peripheral for DMA_CH11

Source§

impl Peripheral for FLASH

Source§

impl Peripheral for I2C0

Source§

impl Peripheral for I2C1

Source§

impl Peripheral for PIN_0

Source§

impl Peripheral for PIN_1

Source§

impl Peripheral for PIN_2

Source§

impl Peripheral for PIN_3

Source§

impl Peripheral for PIN_4

Source§

impl Peripheral for PIN_5

Source§

impl Peripheral for PIN_6

Source§

impl Peripheral for PIN_7

Source§

impl Peripheral for PIN_8

Source§

impl Peripheral for PIN_9

Source§

impl Peripheral for PIN_10

Source§

impl Peripheral for PIN_11

Source§

impl Peripheral for PIN_12

Source§

impl Peripheral for PIN_13

Source§

impl Peripheral for PIN_14

Source§

impl Peripheral for PIN_15

Source§

impl Peripheral for PIN_16

Source§

impl Peripheral for PIN_17

Source§

impl Peripheral for PIN_18

Source§

impl Peripheral for PIN_19

Source§

impl Peripheral for PIN_20

Source§

impl Peripheral for PIN_21

Source§

impl Peripheral for PIN_22

Source§

impl Peripheral for PIN_23

Source§

impl Peripheral for PIN_24

Source§

impl Peripheral for PIN_25

Source§

impl Peripheral for PIN_26

Source§

impl Peripheral for PIN_27

Source§

impl Peripheral for PIN_28

Source§

impl Peripheral for PIN_29

Source§

impl Peripheral for PIN_QSPI_SCLK

Source§

impl Peripheral for PIN_QSPI_SD0

Source§

impl Peripheral for PIN_QSPI_SD1

Source§

impl Peripheral for PIN_QSPI_SD2

Source§

impl Peripheral for PIN_QSPI_SD3

Source§

impl Peripheral for PIN_QSPI_SS

Source§

impl Peripheral for PIO0

Source§

impl Peripheral for PIO1

Source§

impl Peripheral for PWM_SLICE0

Source§

impl Peripheral for PWM_SLICE1

Source§

impl Peripheral for PWM_SLICE2

Source§

impl Peripheral for PWM_SLICE3

Source§

impl Peripheral for PWM_SLICE4

Source§

impl Peripheral for PWM_SLICE5

Source§

impl Peripheral for PWM_SLICE6

Source§

impl Peripheral for PWM_SLICE7

Source§

impl Peripheral for RTC

Source§

type P = RTC

Source§

impl Peripheral for SPI0

Source§

impl Peripheral for SPI1

Source§

impl Peripheral for UART0

Source§

impl Peripheral for UART1

Source§

impl Peripheral for USB

Source§

type P = USB

Source§

impl Peripheral for WATCHDOG

Source§

impl<'b, T> Peripheral for PeripheralRef<'_, T>
where T: Peripheral,

Source§

type P = <T as Peripheral>::P

Source§

impl<'b, T> Peripheral for T
where T: DerefMut, <T as Deref>::Target: Peripheral,

Source§

type P = <<T as Deref>::Target as Peripheral>::P