esp_hal/
peripheral.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! # Exclusive peripheral access

use core::{
    marker::PhantomData,
    ops::{Deref, DerefMut},
};

/// An exclusive reference to a peripheral.
///
/// This is functionally the same as a `&'a mut T`. There's a few advantages in
/// having a dedicated struct instead:
///
/// - Memory efficiency: Peripheral singletons are typically either zero-sized
///   (for concrete peripherals like `GpioPin<5>` or `SPI2`) or very small (for
///   example `AnyPin`, which is 1 byte). However `&mut T` is always 4 bytes for
///   32-bit targets, even if T is zero-sized. PeripheralRef stores a copy of
///   `T` instead, so it's the same size.
/// - Code size efficiency. If the user uses the same driver with both `SPI2`
///   and `&mut SPI2`, the driver code would be monomorphized two times. With
///   PeripheralRef, the driver is generic over a lifetime only. `SPI2` becomes
///   `PeripheralRef<'static, SPI2>`, and `&mut SPI2` becomes `PeripheralRef<'a,
///   SPI2>`. Lifetimes don't cause monomorphization.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PeripheralRef<'a, T> {
    inner: T,
    _lifetime: PhantomData<&'a mut T>,
}

impl<'a, T> PeripheralRef<'a, T> {
    /// Create a new exclusive reference to a peripheral
    #[inline]
    pub fn new(inner: T) -> Self {
        Self {
            inner,
            _lifetime: PhantomData,
        }
    }

    /// 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 `reborrow()` instead. It returns a
    /// `PeripheralRef` that borrows `self`, which allows the borrow checker
    /// to enforce this at compile time.
    pub unsafe fn clone_unchecked(&self) -> PeripheralRef<'a, T>
    where
        T: Peripheral<P = T>,
    {
        PeripheralRef::new(self.inner.clone_unchecked())
    }

    /// Reborrow into a "child" PeripheralRef.
    ///
    /// `self` will stay borrowed until the child PeripheralRef is dropped.
    pub fn reborrow(&mut self) -> PeripheralRef<'_, T>
    where
        T: Peripheral<P = T>,
    {
        // safety: we're returning the clone inside a new PeripheralRef that borrows
        // self, so user code can't use both at the same time.
        PeripheralRef::new(unsafe { self.inner.clone_unchecked() })
    }

    /// Transform the inner peripheral.
    ///
    /// This converts from `PeripheralRef<'a, T>` to `PeripheralRef<'a, U>`,
    /// using a user-provided impl to convert from `T` to `U`.
    #[inline]
    pub fn map<U>(self, transform: impl FnOnce(T) -> U) -> PeripheralRef<'a, U> {
        PeripheralRef {
            inner: transform(self.inner),
            _lifetime: PhantomData,
        }
    }

    /// Map the inner peripheral using `Into`.
    ///
    /// This converts from `PeripheralRef<'a, T>` to `PeripheralRef<'a, U>`,
    /// using an `Into` impl to convert from `T` to `U`.
    ///
    /// For example, this can be useful to degrade GPIO pins: converting from
    /// `PeripheralRef<'a, GpioPin<11>>` to `PeripheralRef<'a, AnyPin>`.
    #[inline]
    pub fn map_into<U>(self) -> PeripheralRef<'a, U>
    where
        T: Into<U>,
    {
        self.map(Into::into)
    }
}

impl<T> Deref for PeripheralRef<'_, T> {
    type Target = T;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

/// 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:
///
/// ```rust, ignore
/// 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`):
///
/// ```rust, ignore
/// 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:
///
/// ```rust, ignore
/// 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>`.
pub trait Peripheral: Sized {
    /// Peripheral singleton type
    type 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.
    unsafe fn clone_unchecked(&self) -> Self::P;

    /// 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>`.
    #[inline]
    fn into_ref<'a>(self) -> PeripheralRef<'a, Self::P>
    where
        Self: 'a,
    {
        PeripheralRef::new(unsafe { self.clone_unchecked() })
    }

    /// 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`.
    #[inline]
    fn map_into<U>(self) -> U
    where
        Self::P: Into<U>,
        U: Peripheral<P = U>,
    {
        self.map(Into::into)
    }

    /// 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`.
    #[inline]
    fn map<U>(self, transform: impl FnOnce(Self::P) -> U) -> U
    where
        U: Peripheral<P = U>,
    {
        transform(unsafe { self.clone_unchecked() })
    }
}

impl<T: DerefMut> Peripheral for T
where
    T::Target: Peripheral,
{
    type P = <T::Target as Peripheral>::P;

    #[inline]
    unsafe fn clone_unchecked(&self) -> Self::P {
        T::Target::clone_unchecked(self)
    }
}

impl<T: Peripheral> Peripheral for PeripheralRef<'_, T> {
    type P = T::P;

    #[inline]
    unsafe fn clone_unchecked(&self) -> Self::P {
        T::clone_unchecked(self)
    }
}

mod peripheral_macros {
    /// Creates a new `Peripherals` struct and its associated methods.
    ///
    /// The macro has a few fields doing different things, in the form of
    /// `second <= third (fourth)`.
    /// - The second field is the name of the peripheral, as it appears in the
    ///   `Peripherals` struct.
    /// - The third field is the name of the peripheral as it appears in the
    ///   PAC. This may be `virtual` if the peripheral is not present in the
    ///   PAC.
    /// - The fourth field is an optional list of interrupts that can be bound
    ///   to the peripheral.
    #[doc(hidden)]
    #[macro_export]
    macro_rules! peripherals {
        (
            peripherals: [
                $(
                    $name:ident <= $from_pac:tt $(($($interrupt:ident),*))?
                ),* $(,)?
            ],
            unstable_peripherals: [
                $(
                    $unstable_name:ident <= $unstable_from_pac:tt $(($($unstable_interrupt:ident),*))?
                ),* $(,)?
            ],
            pins: [
                $( ( $pin:literal, $($pin_tokens:tt)* ) )*
            ],
            dma_channels: [
                $(
                    $channel_name:ident : $channel_ty:path
                ),* $(,)?
            ]
        ) => {
            $(
                $crate::create_peripheral!($name <= $from_pac);
            )*
            $(
                $crate::create_peripheral!(#[instability::unstable] $unstable_name <= $unstable_from_pac);
            )*

            pub(crate) mod gpio {
                $crate::gpio! {
                    $( ($pin, $($pin_tokens)* ) )*
                }
            }

            paste::paste! {
                /// The `Peripherals` struct provides access to all of the hardware peripherals on the chip.
                #[allow(non_snake_case)]
                pub struct Peripherals {
                    $(
                        #[doc = concat!("The ", stringify!($name), " peripheral.")]
                        pub $name: $name,
                    )*
                    $(
                        #[doc = concat!("The ", stringify!($unstable_name), " peripheral.")]
                        #[doc = "**This API is marked as unstable** and is only available when the `unstable`
                                crate feature is enabled. This comes with no stability guarantees, and could be changed
                                or removed at any time."]
                        #[cfg(any(doc, feature = "unstable"))]
                        #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
                        pub $unstable_name: $unstable_name,

                        #[doc = concat!("The ", stringify!($unstable_name), " peripheral.")]
                        #[doc = "**This API is marked as unstable** and is only available when the `unstable`
                                crate feature is enabled. This comes with no stability guarantees, and could be changed
                                or removed at any time."]
                        #[cfg(not(any(doc, feature = "unstable")))]
                        #[allow(unused)]
                        pub(crate) $unstable_name: $unstable_name,
                    )*

                    $(
                        #[doc = concat!("GPIO", stringify!($pin))]
                        pub [<GPIO $pin>]: $crate::gpio::GpioPin<$pin>,
                    )*

                    $(
                        #[doc = concat!(stringify!($channel_name), " DMA channel.")]
                        pub $channel_name: $crate::dma::$channel_ty,
                    )*
                }

                impl Peripherals {
                    /// Returns all the peripherals *once*
                    #[inline]
                    pub(crate) fn take() -> Self {
                        #[no_mangle]
                        static mut _ESP_HAL_DEVICE_PERIPHERALS: bool = false;

                        critical_section::with(|_| unsafe {
                            if _ESP_HAL_DEVICE_PERIPHERALS {
                                panic!("init called more than once!")
                            }
                            _ESP_HAL_DEVICE_PERIPHERALS = true;
                            Self::steal()
                        })
                    }

                    /// Unsafely create an instance of this peripheral out of thin air.
                    ///
                    /// # Safety
                    ///
                    /// You must ensure that you're only using one instance of this type at a time.
                    #[inline]
                    pub unsafe fn steal() -> Self {
                        Self {
                            $(
                                $name: $name::steal(),
                            )*
                            $(
                                $unstable_name: $unstable_name::steal(),
                            )*

                            $(
                                [<GPIO $pin>]: $crate::gpio::GpioPin::<$pin>::steal(),
                            )*

                            $(
                                $channel_name: $crate::dma::$channel_ty::steal(),
                            )*
                        }
                    }
                }
            }

            $(
                $(
                    impl $name {
                        $(
                            paste::paste!{
                                /// Binds an interrupt handler to the corresponding interrupt for this peripheral.
                                #[instability::unstable]
                                pub fn [<bind_ $interrupt:lower _interrupt >](&mut self, handler: unsafe extern "C" fn() -> ()) {
                                    unsafe { $crate::interrupt::bind_interrupt($crate::peripherals::Interrupt::$interrupt, handler); }
                                }
                            }
                        )*
                    }
                )*
            )*

            $(
                $(
                    impl $unstable_name {
                        $(
                            paste::paste!{
                                /// Binds an interrupt handler to the corresponding interrupt for this peripheral.
                                #[instability::unstable]
                                pub fn [<bind_ $unstable_interrupt:lower _interrupt >](&mut self, handler: unsafe extern "C" fn() -> ()) {
                                    unsafe { $crate::interrupt::bind_interrupt($crate::peripherals::Interrupt::$unstable_interrupt, handler); }
                                }
                            }
                        )*
                    }
                )*
            )*
        };
    }

    #[doc(hidden)]
    #[macro_export]
    macro_rules! into_ref {
        ($($name:ident),*) => {
            $(
                #[allow(unused_mut)]
                let mut $name = $name.into_ref();
            )*
        }
    }

    #[doc(hidden)]
    #[macro_export]
    macro_rules! into_mapped_ref {
        ($($name:ident),*) => {
            $(
                #[allow(unused_mut)]
                let mut $name = $name.map_into().into_ref();
            )*
        }
    }

    #[doc(hidden)]
    #[macro_export]
    /// Macro to create a peripheral structure.
    macro_rules! create_peripheral {
        ($(#[$attr:meta])? $name:ident <= virtual) => {
            $(#[$attr])?
            #[derive(Debug)]
            #[cfg_attr(feature = "defmt", derive(defmt::Format))]
            #[non_exhaustive]
            #[allow(non_camel_case_types, clippy::upper_case_acronyms)]
            #[doc = concat!(stringify!($name), " peripheral singleton")]
            pub struct $name;

            impl $name {
                /// Unsafely create an instance of this peripheral out of thin air.
                ///
                /// # Safety
                ///
                /// You must ensure that you're only using one instance of this type at a time.
                #[inline]
                pub unsafe fn steal() -> Self {
                    Self
                }
            }

            impl $crate::peripheral::Peripheral for $name {
                type P = $name;

                #[inline]
                unsafe fn clone_unchecked(&self) -> Self::P {
                    Self::steal()
                }
            }

            impl $crate::private::Sealed for $name {}
        };

        ($(#[$attr:meta])? $name:ident <= $base:ident) => {
            $crate::create_peripheral!($(#[$attr])? $name <= virtual);

            impl $name {
                #[doc = r"Pointer to the register block"]
                #[instability::unstable]
                pub const PTR: *const <pac::$base as core::ops::Deref>::Target = pac::$base::PTR;

                #[doc = r"Return the pointer to the register block"]
                #[inline(always)]
                #[instability::unstable]
                pub const fn ptr() -> *const <pac::$base as core::ops::Deref>::Target {
                    pac::$base::PTR
                }

                #[doc = r"Return a reference to the register block"]
                #[inline(always)]
                #[instability::unstable]
                pub const fn regs<'a>() -> &'a <pac::$base as core::ops::Deref>::Target {
                    unsafe { &*Self::PTR }
                }

                #[doc = r"Return a reference to the register block"]
                #[inline(always)]
                #[instability::unstable]
                pub fn register_block(&self) -> &<pac::$base as core::ops::Deref>::Target {
                    unsafe { &*Self::PTR }
                }
            }
        };
    }
}