esp_hal::peripheral

Trait Peripheral

Source
pub trait Peripheral: Sized + Sealed {
    type P;

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

    // Provided methods
    fn into_ref<'a>(self) -> PeripheralRef<'a, Self::P>
       where Self: 'a { ... }
    fn map_into<U>(self) -> U
       where Self::P: Into<U>,
             U: Peripheral<P = U> { ... }
    fn map<U>(self, transform: impl FnOnce(Self::P) -> U) -> U
       where U: Peripheral<P = U> { ... }
}
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. UART0), or borrowed peripherals (e.g. &mut UART0).

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

impl<'d, T> Uart<'d, T, Blocking> {
    pub fn new<TX: PeripheralOutput, RX: PeripheralInput>(
        uart: impl Peripheral<P = T> + 'd,
        rx: impl Peripheral<P = RX> + 'd,
        tx: impl Peripheral<P = TX> + 'd,
    ) -> Result<Self, Error> {
        Ok(Self { .. })
    }
}

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

let mut uart: Uart<'static, ...> = Uart::new(p.UART0, p.GPIO0, p.GPIO1);

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 uart: Uart<'_, ...> = Uart::new(&mut p.UART0, &mut p.GPIO0, &mut p.GPIO1);

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

Source

fn map_into<U>(self) -> U
where Self::P: Into<U>, U: Peripheral<P = U>,

Map the peripheral using Into.

This converts from Peripheral<P = T> to Peripheral<P = U>, using an Into impl to convert from T to U.

Source

fn map<U>(self, transform: impl FnOnce(Self::P) -> U) -> U
where U: Peripheral<P = U>,

Map the peripheral using Into.

This converts from Peripheral<P = T> to Peripheral<P = U>, using an Into impl to convert from T to U.

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.

Implementations on Foreign Types§

Source§

impl<T, P> Peripheral for &mut T
where T: Peripheral<P = P>,

Source§

type P = P

Source§

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

Implementors§

Source§

impl Peripheral for Level

Source§

impl Peripheral for AnyI2sDmaChannel

Available on crate feature unstable only.
Source§

impl Peripheral for AnyI2sDmaRxChannel

Available on crate feature unstable only.
Source§

impl Peripheral for AnyI2sDmaTxChannel

Available on crate feature unstable only.
Source§

impl Peripheral for AnySpiDmaChannel

Available on crate feature unstable only.
Source§

impl Peripheral for AnySpiDmaRxChannel

Available on crate feature unstable only.
Source§

impl Peripheral for AnySpiDmaTxChannel

Available on crate feature unstable only.
Source§

impl Peripheral for I2s0DmaChannel

Available on crate feature unstable only.
Source§

impl Peripheral for I2s1DmaChannel

Available on crate feature unstable only.
Source§

impl Peripheral for Spi2DmaChannel

Available on crate feature unstable only.
Source§

impl Peripheral for Spi3DmaChannel

Available on crate feature unstable only.
Source§

impl Peripheral for InputConnection

Available on crate feature unstable only.
Source§

impl Peripheral for InputSignal

Available on crate feature unstable only.
Source§

impl Peripheral for OutputConnection

Available on crate feature unstable only.
Source§

impl Peripheral for OutputSignal

Available on crate feature unstable only.
Source§

impl Peripheral for AnyPin

Source§

impl Peripheral for Flex<'_>

Source§

type P = Flex<'_>

Source§

impl Peripheral for NoPin

Source§

impl Peripheral for AnyI2c

Source§

impl Peripheral for esp_hal::i2s::master::AnyI2s

Available on crate feature unstable only.
Source§

impl Peripheral for esp_hal::i2s::parallel::AnyI2s

Available on crate feature unstable only.
Source§

impl Peripheral for ADC1

Source§

impl Peripheral for ADC2

Source§

impl Peripheral for AES

Source§

type P = AES

Source§

impl Peripheral for APB_CTRL

Source§

impl Peripheral for BB

Source§

type P = BB

Source§

impl Peripheral for BT

Source§

type P = BT

Source§

impl Peripheral for CPU_CTRL

Source§

impl Peripheral for DAC1

Source§

impl Peripheral for DAC2

Source§

impl Peripheral for EFUSE

Source§

impl Peripheral for FLASH_ENCRYPTION

Source§

impl Peripheral for FRC_TIMER

Source§

impl Peripheral for GPIO_SD

Source§

impl Peripheral for HINF

Source§

impl Peripheral for I2C0

Source§

impl Peripheral for I2C1

Source§

impl Peripheral for I2S0

Source§

impl Peripheral for I2S1

Source§

impl Peripheral for IO_MUX

Source§

impl Peripheral for LEDC

Source§

impl Peripheral for LPWR

Source§

impl Peripheral for MCPWM0

Source§

impl Peripheral for MCPWM1

Source§

impl Peripheral for NRX

Source§

type P = NRX

Source§

impl Peripheral for PCNT

Source§

impl Peripheral for PSRAM

Source§

impl Peripheral for RADIO_CLK

Source§

impl Peripheral for RMT

Source§

type P = RMT

Source§

impl Peripheral for RNG

Source§

type P = RNG

Source§

impl Peripheral for RSA

Source§

type P = RSA

Source§

impl Peripheral for RTC_I2C

Source§

impl Peripheral for RTC_IO

Source§

impl Peripheral for SDHOST

Source§

impl Peripheral for SHA

Source§

type P = SHA

Source§

impl Peripheral for SLC

Source§

type P = SLC

Source§

impl Peripheral for SLCHOST

Source§

impl Peripheral for SPI0

Source§

impl Peripheral for SPI1

Source§

impl Peripheral for SPI2

Source§

impl Peripheral for SPI3

Source§

impl Peripheral for SW_INTERRUPT

Source§

impl Peripheral for SYSTEM

Source§

impl Peripheral for TIMG0

Source§

impl Peripheral for TIMG1

Source§

impl Peripheral for TOUCH

Source§

impl Peripheral for TWAI0

Source§

impl Peripheral for UART0

Source§

impl Peripheral for UART1

Source§

impl Peripheral for UART2

Source§

impl Peripheral for UHCI0

Source§

impl Peripheral for UHCI1

Source§

impl Peripheral for WIFI

Source§

impl Peripheral for AnySpi

Source§

impl Peripheral for AnyTimer

Available on crate feature unstable only.
Source§

impl Peripheral for Timer

Available on crate feature unstable only.
Source§

impl Peripheral for AnyTwai

Available on crate feature unstable only.
Source§

impl Peripheral for AnyUart

Source§

impl<'d> Peripheral for Input<'d>

Source§

type P = Flex<'d>

Source§

impl<'d> Peripheral for Output<'d>

Source§

type P = Flex<'d>

Source§

impl<'d> Peripheral for OutputOpenDrain<'d>

Source§

type P = Flex<'d>

Source§

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

Source§

type P = <T as Peripheral>::P

Source§

impl<const GPIONUM: u8> Peripheral for GpioPin<GPIONUM>
where Self: Pin,

Source§

type P = GpioPin<GPIONUM>

Source§

impl<const NUM: u8> Peripheral for SoftwareInterrupt<NUM>

Available on crate feature unstable only.