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.

Implementations on Foreign Types§

source§

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

§

type P = T

source§

unsafe fn clone_unchecked(&mut self) -> <&mut T as Peripheral>::P

Implementors§

source§

impl Peripheral for ADC1

§

type P = ADC1

source§

impl Peripheral for ADC2

§

type P = ADC2

source§

impl Peripheral for DAC1

§

type P = DAC1

source§

impl Peripheral for DAC2

§

type P = DAC2

source§

impl Peripheral for AES

§

type P = AES

source§

impl Peripheral for APB_CTRL

§

type P = APB_CTRL

source§

impl Peripheral for BB

§

type P = BB

source§

impl Peripheral for BT

§

type P = BT

source§

impl Peripheral for EFUSE

§

type P = EFUSE

source§

impl Peripheral for FLASH_ENCRYPTION

source§

impl Peripheral for FRC_TIMER

§

type P = FRC_TIMER

source§

impl Peripheral for GPIO

§

type P = GPIO

source§

impl Peripheral for GPIO_SD

§

type P = GPIO_SD

source§

impl Peripheral for HINF

§

type P = HINF

source§

impl Peripheral for I2C0

§

type P = I2C0

source§

impl Peripheral for I2C1

§

type P = I2C1

source§

impl Peripheral for I2S0

§

type P = I2S0

source§

impl Peripheral for I2S1

§

type P = I2S1

source§

impl Peripheral for IO_MUX

§

type P = IO_MUX

source§

impl Peripheral for LEDC

§

type P = LEDC

source§

impl Peripheral for LPWR

§

type P = LPWR

source§

impl Peripheral for MCPWM0

§

type P = MCPWM0

source§

impl Peripheral for MCPWM1

§

type P = MCPWM1

source§

impl Peripheral for NRX

§

type P = NRX

source§

impl Peripheral for PCNT

§

type P = PCNT

source§

impl Peripheral for PSRAM

§

type P = PSRAM

source§

impl Peripheral for RMT

§

type P = RMT

source§

impl Peripheral for RNG

§

type P = RNG

source§

impl Peripheral for RSA

§

type P = RSA

source§

impl Peripheral for RTC_I2C

§

type P = RTC_I2C

source§

impl Peripheral for RTC_IO

§

type P = RTC_IO

source§

impl Peripheral for SDHOST

§

type P = SDHOST

source§

impl Peripheral for SENS

§

type P = SENS

source§

impl Peripheral for SHA

§

type P = SHA

source§

impl Peripheral for SLC

§

type P = SLC

source§

impl Peripheral for SLCHOST

§

type P = SLCHOST

source§

impl Peripheral for SPI0

§

type P = SPI0

source§

impl Peripheral for SPI1

§

type P = SPI1

source§

impl Peripheral for SPI2

§

type P = SPI2

source§

impl Peripheral for SPI3

§

type P = SPI3

source§

impl Peripheral for SYSTEM

§

type P = SYSTEM

source§

impl Peripheral for TIMG0

§

type P = TIMG0

source§

impl Peripheral for TIMG1

§

type P = TIMG1

source§

impl Peripheral for TWAI0

§

type P = TWAI0

source§

impl Peripheral for UART0

§

type P = UART0

source§

impl Peripheral for UART1

§

type P = UART1

source§

impl Peripheral for UART2

§

type P = UART2

source§

impl Peripheral for UHCI0

§

type P = UHCI0

source§

impl Peripheral for UHCI1

§

type P = UHCI1

source§

impl Peripheral for WIFI

§

type P = WIFI

source§

impl Peripheral for Dma

§

type P = Dma

source§

impl Peripheral for SystemClockControl

source§

impl<MODE, const GPIONUM: u8> Peripheral for GpioPin<MODE, GPIONUM>
where GpioPin<MODE, GPIONUM>: GpioProperties,

§

type P = GpioPin<MODE, GPIONUM>