esp_idf_hal::peripheral

Trait 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>.

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 ADC1

Source§

impl Peripheral for ADC2

Source§

impl Peripheral for CAN

Source§

type P = CAN

Source§

impl Peripheral for AnyIOPin

Source§

impl Peripheral for AnyInputPin

Source§

impl Peripheral for AnyOutputPin

Source§

impl Peripheral for Gpio0

Source§

impl Peripheral for Gpio1

Source§

impl Peripheral for Gpio2

Source§

impl Peripheral for Gpio3

Source§

impl Peripheral for Gpio4

Source§

impl Peripheral for Gpio5

Source§

impl Peripheral for Gpio6

Source§

impl Peripheral for Gpio7

Source§

impl Peripheral for Gpio8

Source§

impl Peripheral for Gpio9

Source§

impl Peripheral for Gpio10

Source§

impl Peripheral for Gpio11

Source§

impl Peripheral for Gpio12

Source§

impl Peripheral for Gpio13

Source§

impl Peripheral for Gpio14

Source§

impl Peripheral for Gpio15

Source§

impl Peripheral for Gpio16

Source§

impl Peripheral for Gpio17

Source§

impl Peripheral for Gpio18

Source§

impl Peripheral for Gpio19

Source§

impl Peripheral for Gpio20

Source§

impl Peripheral for Gpio21

Source§

impl Peripheral for I2C0

Source§

impl Peripheral for I2S0

Source§

impl Peripheral for esp_idf_hal::ledc::CHANNEL0

Source§

impl Peripheral for esp_idf_hal::ledc::CHANNEL1

Source§

impl Peripheral for esp_idf_hal::ledc::CHANNEL2

Source§

impl Peripheral for esp_idf_hal::ledc::CHANNEL3

Source§

impl Peripheral for CHANNEL4

Source§

impl Peripheral for CHANNEL5

Source§

impl Peripheral for TIMER0

Source§

impl Peripheral for TIMER1

Source§

impl Peripheral for TIMER2

Source§

impl Peripheral for TIMER3

Source§

impl Peripheral for BluetoothModem

Source§

impl Peripheral for Modem

Source§

impl Peripheral for WifiModem

Source§

impl Peripheral for esp_idf_hal::rmt::CHANNEL0

Source§

impl Peripheral for esp_idf_hal::rmt::CHANNEL1

Source§

impl Peripheral for esp_idf_hal::rmt::CHANNEL2

Source§

impl Peripheral for esp_idf_hal::rmt::CHANNEL3

Source§

impl Peripheral for SPI1

Source§

impl Peripheral for SPI2

Source§

impl Peripheral for TWDT

Source§

impl Peripheral for TempSensor

Source§

impl Peripheral for TIMER00

Source§

impl Peripheral for TIMER10

Source§

impl Peripheral for UART0

Source§

impl Peripheral for UART1

Source§

impl Peripheral for USB_SERIAL

Source§

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

Source§

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