Trait esp_idf_svc::hal::peripheral::Peripheral

source ·
pub trait Peripheral: Sized + Sealed {
    type P;

    // Required method
    unsafe fn clone_unchecked(&mut 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(&mut 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>.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl Peripheral for ADC1

§

type P = ADC1

source§

impl Peripheral for ADC2

§

type P = ADC2

source§

impl Peripheral for CAN

§

type P = CAN

source§

impl Peripheral for AnyIOPin

§

type P = AnyIOPin

source§

impl Peripheral for AnyInputPin

source§

impl Peripheral for AnyOutputPin

source§

impl Peripheral for Gpio0

§

type P = Gpio0

source§

impl Peripheral for Gpio1

§

type P = Gpio1

source§

impl Peripheral for Gpio2

§

type P = Gpio2

source§

impl Peripheral for Gpio3

§

type P = Gpio3

source§

impl Peripheral for Gpio4

§

type P = Gpio4

source§

impl Peripheral for Gpio5

§

type P = Gpio5

source§

impl Peripheral for Gpio6

§

type P = Gpio6

source§

impl Peripheral for Gpio7

§

type P = Gpio7

source§

impl Peripheral for Gpio8

§

type P = Gpio8

source§

impl Peripheral for Gpio9

§

type P = Gpio9

source§

impl Peripheral for Gpio10

§

type P = Gpio10

source§

impl Peripheral for Gpio11

§

type P = Gpio11

source§

impl Peripheral for Gpio12

§

type P = Gpio12

source§

impl Peripheral for Gpio13

§

type P = Gpio13

source§

impl Peripheral for Gpio14

§

type P = Gpio14

source§

impl Peripheral for Gpio15

§

type P = Gpio15

source§

impl Peripheral for Gpio16

§

type P = Gpio16

source§

impl Peripheral for Gpio17

§

type P = Gpio17

source§

impl Peripheral for Gpio18

§

type P = Gpio18

source§

impl Peripheral for Gpio19

§

type P = Gpio19

source§

impl Peripheral for Gpio20

§

type P = Gpio20

source§

impl Peripheral for Gpio21

§

type P = Gpio21

source§

impl Peripheral for I2C0

§

type P = I2C0

source§

impl Peripheral for I2S0

§

type P = I2S0

source§

impl Peripheral for esp_idf_svc::hal::ledc::CHANNEL0

§

type P = CHANNEL0

source§

impl Peripheral for esp_idf_svc::hal::ledc::CHANNEL1

§

type P = CHANNEL1

source§

impl Peripheral for esp_idf_svc::hal::ledc::CHANNEL2

§

type P = CHANNEL2

source§

impl Peripheral for esp_idf_svc::hal::ledc::CHANNEL3

§

type P = CHANNEL3

source§

impl Peripheral for CHANNEL4

§

type P = CHANNEL4

source§

impl Peripheral for CHANNEL5

§

type P = CHANNEL5

source§

impl Peripheral for TIMER0

§

type P = TIMER0

source§

impl Peripheral for TIMER1

§

type P = TIMER1

source§

impl Peripheral for TIMER2

§

type P = TIMER2

source§

impl Peripheral for TIMER3

§

type P = TIMER3

source§

impl Peripheral for BluetoothModem

source§

impl Peripheral for Modem

§

type P = Modem

source§

impl Peripheral for WifiModem

§

type P = WifiModem

source§

impl Peripheral for esp_idf_svc::hal::rmt::CHANNEL0

§

type P = CHANNEL0

source§

impl Peripheral for esp_idf_svc::hal::rmt::CHANNEL1

§

type P = CHANNEL1

source§

impl Peripheral for esp_idf_svc::hal::rmt::CHANNEL2

§

type P = CHANNEL2

source§

impl Peripheral for esp_idf_svc::hal::rmt::CHANNEL3

§

type P = CHANNEL3

source§

impl Peripheral for SPI1

§

type P = SPI1

source§

impl Peripheral for SPI2

§

type P = SPI2

source§

impl Peripheral for TWDT

§

type P = TWDT

source§

impl Peripheral for TIMER00

§

type P = TIMER00

source§

impl Peripheral for TIMER10

§

type P = TIMER10

source§

impl Peripheral for UART0

§

type P = UART0

source§

impl Peripheral for UART1

§

type P = UART1

source§

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

§

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