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.

Implementations on Foreign Types§

Source§

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

Source§

type P = T

Source§

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

Implementors§

Source§

impl Peripheral for ADC1

Source§

impl Peripheral for ADC2

Source§

impl Peripheral for DAC1

Source§

impl Peripheral for DAC2

Source§

impl Peripheral for AES

Source§

type P = AES

Source§

impl Peripheral for APB_SARADC

Source§

impl Peripheral for ASSIST_DEBUG

Source§

impl Peripheral for BT

Source§

type P = BT

Source§

impl Peripheral for DMA

Source§

type P = DMA

Source§

impl Peripheral for DS

Source§

type P = DS

Source§

impl Peripheral for ECC

Source§

type P = ECC

Source§

impl Peripheral for EFUSE

Source§

impl Peripheral for GPIO

Source§

impl Peripheral for GPIO_SD

Source§

impl Peripheral for HMAC

Source§

impl Peripheral for HP_APM

Source§

impl Peripheral for HP_SYS

Source§

impl Peripheral for I2C0

Source§

impl Peripheral for I2C1

Source§

impl Peripheral for I2S0

Source§

impl Peripheral for IEEE802154

Source§

impl Peripheral for INTERRUPT_CORE0

Source§

impl Peripheral for INTPRI

Source§

impl Peripheral for IO_MUX

Source§

impl Peripheral for LEDC

Source§

impl Peripheral for LPWR

Source§

impl Peripheral for LP_ANA

Source§

impl Peripheral for LP_AON

Source§

impl Peripheral for LP_APM

Source§

impl Peripheral for LP_PERI

Source§

impl Peripheral for LP_TIMER

Source§

impl Peripheral for LP_WDT

Source§

impl Peripheral for MCPWM0

Source§

impl Peripheral for MEM_MONITOR

Source§

impl Peripheral for MODEM_LPCON

Source§

impl Peripheral for MODEM_SYSCON

Source§

impl Peripheral for OTP_DEBUG

Source§

impl Peripheral for PARL_IO

Source§

impl Peripheral for PAU

Source§

type P = PAU

Source§

impl Peripheral for PCNT

Source§

impl Peripheral for PMU

Source§

type P = PMU

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 SHA

Source§

type P = SHA

Source§

impl Peripheral for SOC_ETM

Source§

impl Peripheral for SPI0

Source§

impl Peripheral for SPI1

Source§

impl Peripheral for SPI2

Source§

impl Peripheral for SYSTEM

Source§

impl Peripheral for SYSTIMER

Source§

impl Peripheral for TEE

Source§

type P = TEE

Source§

impl Peripheral for TIMG0

Source§

impl Peripheral for TIMG1

Source§

impl Peripheral for TRACE

Source§

impl Peripheral for TWAI0

Source§

impl Peripheral for UART0

Source§

impl Peripheral for UART1

Source§

impl Peripheral for UHCI0

Source§

impl Peripheral for USB_DEVICE

Source§

impl Peripheral for SystemClockControl

Source§

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

Source§

type P = GpioPin<MODE, GPIONUM>