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

Implementations on Foreign Types§

source§

impl<T> Peripheral for &mut Twhere 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 AES

§

type P = AES

source§

impl Peripheral for APB_CTRL

§

type P = APB_CTRL

source§

impl Peripheral for APB_SARADC

source§

impl Peripheral for ASSIST_DEBUG

source§

impl Peripheral for DMA

§

type P = DMA

source§

impl Peripheral for DS

§

type P = DS

source§

impl Peripheral for EFUSE

§

type P = EFUSE

source§

impl Peripheral for EXTMEM

§

type P = EXTMEM

source§

impl Peripheral for GPIO

§

type P = GPIO

source§

impl Peripheral for GPIO_SD

§

type P = GPIO_SD

source§

impl Peripheral for HMAC

§

type P = HMAC

source§

impl Peripheral for I2C0

§

type P = I2C0

source§

impl Peripheral for I2S0

§

type P = I2S0

source§

impl Peripheral for INTERRUPT_CORE0

source§

impl Peripheral for IO_MUX

§

type P = IO_MUX

source§

impl Peripheral for LEDC

§

type P = LEDC

source§

impl Peripheral for RADIO

§

type P = RADIO

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_CNTL

§

type P = RTC_CNTL

source§

impl Peripheral for SENSITIVE

§

type P = SENSITIVE

source§

impl Peripheral for SHA

§

type P = SHA

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 SYSTEM

§

type P = SYSTEM

source§

impl Peripheral for SYSTIMER

§

type P = SYSTIMER

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 UHCI0

§

type P = UHCI0

source§

impl Peripheral for UHCI1

§

type P = UHCI1

source§

impl Peripheral for USB_DEVICE

source§

impl Peripheral for XTS_AES

§

type P = XTS_AES

source§

impl Peripheral for Bluetooth

§

type P = Bluetooth

source§

impl Peripheral for LowRate

§

type P = LowRate

source§

impl Peripheral for Wifi

§

type P = Wifi

source§

impl Peripheral for SystemClockControl

source§

impl Peripheral for DAC1

§

type P = DAC1

source§

impl Peripheral for DAC2

§

type P = DAC2

source§

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

§

type P = GpioPin<MODE, GPIONUM>