Skip to main content

teensy4_bsp/
board.rs

1//! Pre-configured board resources.
2//!
3//! The `board` module conveniently exposes drivers and hardware resources.
4//! It configures peripheral clocks and power so that you can simply use the
5//! board's peripherals.
6//!
7//! # Getting started
8//!
9//! Create board resources depending on your board:
10//!
11//! - Teensy 4.0 boards should use [`t40`].
12//! - Teensy 4.1 boards should use [`t41`].
13//! - Teensy MicroMod boards should use [`tmm`].
14//!
15//! The produced resources are nearly the same, except for the pins.
16//!
17//! ```no_run
18//! use teensy4_bsp as bsp;
19//! use bsp::board;
20//!
21//! let resources = board::t40(board::instances());
22//! ```
23//!
24//! Resources take ownership of all peripheral instances `Instances`. Use [`instances()`] to
25//! acquire these once.
26//!
27//! > Note that if you're using RTIC, you can let the RTIC framework allocate
28//! > the instances. The examples included in this package demonstrate this pattern.
29//!
30//! # Resource categories
31//!
32//! The objects in [`Resources`] have two main categories:
33//!
34//! 1. ready-to-go drivers, like `gpt1` and `dma`, which are unlikely to change no matter your
35//!    hardware setup.
36//! 2. lower-level resources, like `pins` and `lpuart3`, which you can compose to create drivers
37//!    specific for your hardware setup.
38//!
39//! Given the Teensy 4's I/O flexibility, it would be difficult to expose all features as ready-to-go
40//! drivers. Instead, `board` tries to help you initialize drivers for your hardware
41//! setup. For example,
42//!
43//! - use [`led`] to prepare the on-board LED.
44//! - use [`lpspi`] to prepare SPI peripherals.
45//! - use [`lpuart`] to prepare serial peripherals.
46//! - use [`lpi2c`] to prepare I2C peripherals.
47//!
48//! For the complete list of helper functions, study the rest of this API documentation and their
49//! examples. And for the complete driver documentation, see [the `hal` module](crate::hal) and
50//! its submodules.
51//!
52//! These helper functions assume that you're following the `board`'s clock (and power) policy.
53//!
54//! # Clock policy
55//!
56//! Once [`t40`] and [`t41`] return, the clocks of all returned resources are running, and clock gates are
57//! enabled. Essentially, `board` handles CCM configurations so that you can use peripherals with reasonable
58//! settings.
59//!
60//! `board` exposes constants to help you understand those clock frequencies. You should
61//! use these frequencies, along with your own settings, to describe certain types of objects, like
62//! blocking delays.
63//!
64//! The example below shows how a user configures a GPT to meet a given GPT frequency.
65//!
66//! ```no_run
67//! # use teensy4_bsp as bsp;
68//! # use bsp::board;
69//! use bsp::hal::{gpt, timer::Blocking};
70//!
71//! // Given this GPT clock source...
72//! const GPT_CLOCK_SOURCE: gpt::ClockSource = gpt::ClockSource::PeripheralClock;
73//! // ...and this GPT-specific divider...
74//! const GPT_DIVIDER: u32 = 8;
75//! /// ...the GPT frequency is
76//! const GPT_FREQUENCY: u32 = board::PERCLK_FREQUENCY / GPT_DIVIDER;
77//!
78//! let board::Resources{ mut gpt1, .. }  = board::t40(board::instances());
79//!
80//! gpt1.set_clock_source(GPT_CLOCK_SOURCE);
81//! gpt1.set_divider(GPT_DIVIDER);
82//!
83//! let mut delay = Blocking::<_, { GPT_FREQUENCY }>::from_gpt(gpt1);
84//! delay.block_ms(500);
85//! ```
86//!
87//! Clock frequency constants, and all APIs depending on those constants, assume that you do not change the clock policy.
88//! But for those who want extra control, `Resources` exposes the clock management peripherals. If you take this
89//! route, you're responsible for ensuring your peripheral's clock configurations, and you should ignore the constants
90//! exposed by this module.
91//!
92//! ## Clock details
93//!
94//! `board` makes a few guarantees about the clock policy. This section describes those guarantees.
95//!
96//! It's considered an API-breaking change to vary the frequencies and derivations of these clocks:
97//!
98//! - The ARM core frequency is documented in [`ARM_FREQUENCY`].
99//! - PERCLK, the root clock for GPT and PIT timers, is documented in [`PERCLK_FREQUENCY`].
100//! - The crystal oscillator is documented in [`XTAL_OSCILLATOR_FREQUENCY`].
101//!
102//! `board` does not touch the following root clock components. You should configure these components yourself.
103//!
104//! - FlexIO1 and FlexIO2 dividers and multiplexers.
105//!
106//! Other clock frequencies are exempt from this policy; they may change value or derivation without
107//! notice. Nevertheless, if these clock frequencies / derivations change, `board` still guarantees that
108//! the functions to set peripheral baud and clock rates will work as expected.
109//!
110//! By the time your code has access to [`Resources`], the clock gates for _all_ peripherals provided by
111//! `Resources` is set to "on." If you're not using specific resources, you may configure their clock
112//! gates to "off." `board` code that borrows or converts peripherals may assume that clock gates are
113//! set to "on."
114//!
115//! If you're not using the BSP to prepare board `Resources`, consider using [`prepare_clocks_and_power`]
116//! to realize the BSP's clock policy.
117//!
118//! # Naming conventions
119//!
120//! This module identifies peripherals by their hardware names. It does not use identifiers
121//! from the Teensy 4 SDK and documentation. For instance, to name the serial peripheral
122//! that uses pins 14 and 15,
123//!
124//! - this module uses the name "LPUART**2**".
125//! - the Teensy 4 SDK / documentation uses the name "UART**3**".
126//!
127//! There's a few reasons for these differences:
128//!
129//! - It would be unintuitive to register a LPUART**2** *interrupt* for the peripheral named
130//!   UART**3**. Since the BSP typically does not manage interrupt registration, this naming
131//!   decision makes it easier for you to register and implement interrupts.
132//! - It makes clear the peripheral implementation. You know that the serial peripheral
133//!   supports low-power (LP) operation, and that it's not a FlexIO peripheral emulating a UART
134//!   device.
135
136use crate::{hal, pins, ral};
137use core::sync::atomic::{AtomicBool, Ordering};
138pub use hal::lpspi::Pins as LpspiPins;
139
140/// Use [`instances()`] to safely acquire.
141pub use ral::Instances;
142
143/// Acquire peripheral instances.
144///
145/// These are the resources supplied to [a resource constructor](crate::board#getting-started).
146/// They can also be used to initialize your own drivers.
147///
148/// ```
149/// use teensy4_bsp as bsp;
150/// use bsp::board;
151///
152/// let instances = board::instances();
153/// ```
154///
155/// # Panics
156///
157/// Panics if the instances have already been taken from this function.
158/// If you're using RTIC, know that RTIC may take these instances before
159/// calling your `init` function.
160///
161/// ```should_panic
162/// # use teensy4_bsp as bsp;
163/// # use bsp::board;
164/// let instances = board::instances();
165/// // Sometime later...
166/// board::instances(); // Panics! Instances already taken.
167/// ```
168///
169/// # Safety
170///
171/// This function provides a safe, convenient way to acquire one [`Instances`] object.
172/// It's considered safe for the use-cases that `teensy4-bsp` intends to support;
173/// specifically, you're building one binary that represents the entire firmware
174/// image, and that there's no other software which is acquiring these resources.
175pub fn instances() -> Instances {
176    static TAKEN: AtomicBool = AtomicBool::new(false);
177    assert!(!TAKEN.swap(true, Ordering::SeqCst));
178    unsafe { Instances::instances() }
179}
180
181pub use crate::clock_power::*;
182
183/// Resources for a Teensy 4.0.
184///
185/// Use [`t40`] to construct this. The pins are specific to the Teensy 4.0.
186pub type T40Resources = Resources<pins::t40::Pins>;
187/// Resources for a Teensy 4.1.
188///
189/// Use [`t41`] to construct this. The pins are specific to the Teensy 4.1.
190pub type T41Resources = Resources<pins::t41::Pins>;
191/// Resources for a Teensy MicroMod.
192///
193/// Use [`tmm`] to construct this. The pins are specific to the Teensy MicroMod.
194pub type TMMResources = Resources<pins::tmm::Pins>;
195
196/// Resources constructed by the board.
197///
198/// The concrete `Pins` type depends on how this is constructed.
199/// See the various `*Resources` aliases for more information.
200#[non_exhaustive]
201pub struct Resources<Pins> {
202    /// Periodic interrupt timer channels.
203    pub pit: hal::pit::Channels,
204    /// General purpose timer 1.
205    pub gpt1: hal::gpt::Gpt1,
206    /// General purpose timer 2.
207    pub gpt2: hal::gpt::Gpt2,
208    /// GPIO1 port.
209    pub gpio1: hal::gpio::Port<1>,
210    /// GPIO2 port.
211    pub gpio2: hal::gpio::Port<2>,
212    /// GPIO3 port.
213    pub gpio3: hal::gpio::Port<3>,
214    /// GPIO4 port.
215    pub gpio4: hal::gpio::Port<4>,
216    /// USB1 instances.
217    ///
218    /// Use this to construct higher-level USB drivers, or to initialize the USB logger.
219    pub usb: hal::usbd::Instances<1>,
220    /// DMA channels.
221    pub dma: [Option<hal::dma::channel::Channel>; hal::dma::CHANNEL_COUNT],
222    /// The secure real-time counter.
223    ///
224    /// It's initially disabled, and you may enable it in your firmware.
225    pub srtc: hal::snvs::srtc::Disabled,
226    /// Core registers for the SNVS low-power domain.
227    ///
228    /// Use this with the SRTC and other SNVS LP components.
229    pub snvs_lp_core: hal::snvs::LpCore,
230    /// Clock control module.
231    pub ccm: ral::ccm::CCM,
232    /// Analog clock control module.
233    pub ccm_analog: ral::ccm_analog::CCM_ANALOG,
234    /// DCDC converter
235    pub dcdc: ral::dcdc::DCDC,
236    /// All available pins.
237    pub pins: Pins,
238    /// The register block for [`Lpi2c1`].
239    pub lpi2c1: ral::lpi2c::LPI2C1,
240    /// The register block for [`Lpi2c3`].
241    pub lpi2c3: ral::lpi2c::LPI2C3,
242    /// The register blocks for [`Lpspi1`].
243    pub lpspi1: ral::lpspi::LPSPI1,
244    /// The register blocks for [`Lpspi2`].
245    pub lpspi2: ral::lpspi::LPSPI2,
246    /// The register blocks for [`Lpspi3`].
247    pub lpspi3: ral::lpspi::LPSPI3,
248    /// The register block for [`Lpspi4`].
249    pub lpspi4: ral::lpspi::LPSPI4,
250    /// The register block for [`Lpuart6`].
251    pub lpuart6: ral::lpuart::LPUART6,
252    /// The register block for [`Lpuart4`].
253    pub lpuart4: ral::lpuart::LPUART4,
254    /// The register block for [`Lpuart2`].
255    pub lpuart2: ral::lpuart::LPUART2,
256    /// The register block for [`Lpuart3`].
257    pub lpuart3: ral::lpuart::LPUART3,
258    /// The register block for [`Lpuart8`].
259    pub lpuart8: ral::lpuart::LPUART8,
260    /// The register block for [`Lpuart1`].
261    pub lpuart1: ral::lpuart::LPUART1,
262    /// The register block for [`Lpuart5`].
263    pub lpuart5: ral::lpuart::LPUART5,
264    /// The register block for [`Lpuart7`].
265    pub lpuart7: ral::lpuart::LPUART7,
266    /// FlexPWM1 components.
267    pub flexpwm1: (hal::flexpwm::Pwm<1>, hal::flexpwm::Submodules<1>),
268    /// FlexPWM2 components.
269    pub flexpwm2: (hal::flexpwm::Pwm<2>, hal::flexpwm::Submodules<2>),
270    /// FlexPWM3 components.
271    pub flexpwm3: (hal::flexpwm::Pwm<3>, hal::flexpwm::Submodules<3>),
272    /// FlexPWM4 components.
273    pub flexpwm4: (hal::flexpwm::Pwm<4>, hal::flexpwm::Submodules<4>),
274    /// The FlexIO1 register block.
275    pub flexio1: ral::flexio::FLEXIO1,
276    /// The FlexIO2 register block.
277    pub flexio2: ral::flexio::FLEXIO2,
278    /// The FlexIO3 register block.
279    pub flexio3: ral::flexio::FLEXIO3,
280    /// The register block for ADC1.
281    ///
282    /// ADC drivers constructed by `board` use a pre-configured clock and divisor. To change
283    /// this configuration, call `release()` to acquire the register block, then re-construct
284    /// the driver.
285    pub adc1: hal::adc::Adc<1>,
286    /// The register block for ADC2.
287    pub adc2: hal::adc::Adc<2>,
288    /// True random number generator.
289    pub trng: hal::trng::Trng,
290    /// Temperature monitor of the core.
291    pub tempmon: hal::tempmon::TempMon,
292    /// The register block for SAI1 (I2S audio).
293    ///
294    /// SAI1 is the primary audio interface used by the Teensy Audio Shield.
295    pub sai1: ral::sai::SAI1,
296    /// The register block for SAI2.
297    pub sai2: ral::sai::SAI2,
298    /// The register block for SAI3.
299    pub sai3: ral::sai::SAI3,
300    /// The IOMUXC general purpose register block.
301    pub iomuxc_gpr: ral::iomuxc_gpr::IOMUXC_GPR,
302}
303
304/// The board's dedicated LED.
305pub type Led = hal::gpio::Output<pins::common::P13>;
306
307/// Create the board's LED.
308///
309/// ```no_run
310/// use teensy4_bsp as bsp;
311/// use bsp::board;
312///
313/// let board::Resources { mut gpio2, pins, .. }
314///     = board::t40(board::instances());
315///
316/// let led = board::led(&mut gpio2, pins.p13);
317/// ```
318pub fn led(gpio2: &mut hal::gpio::Port<2>, p13: pins::common::P13) -> Led {
319    gpio2.output(p13)
320}
321
322/// Create a LPI2C peripheral.
323///
324/// Consider using explicit type annotations, as demonstrated below,
325/// to simplify any errors from the compiler.
326///
327/// This helper assumes that the actual LPI2C clock frequency equals [`LPI2C_FREQUENCY`].
328///
329/// ```no_run
330/// use teensy4_bsp as bsp;
331/// use bsp::board;
332///
333/// let board::Resources { lpi2c3, pins, ..}
334///     = board::t40(board::instances());
335///
336/// let mut lpi2c: board::Lpi2c3 = board::lpi2c(
337///     lpi2c3,
338///     pins.p16,
339///     pins.p17,
340///     board::Lpi2cClockSpeed::KHz400,
341/// );
342/// ```
343pub fn lpi2c<Scl, Sda, const N: u8>(
344    instance: ral::lpi2c::Instance<N>,
345    scl: Scl,
346    sda: Sda,
347    clock_speed: Lpi2cClockSpeed,
348) -> hal::lpi2c::Lpi2c<hal::lpi2c::Pins<Scl, Sda>, N>
349where
350    Scl: hal::iomuxc::lpi2c::Pin<
351        Signal = hal::iomuxc::lpi2c::Scl,
352        Module = hal::iomuxc::consts::Const<N>,
353    >,
354    Sda: hal::iomuxc::lpi2c::Pin<
355        Signal = hal::iomuxc::lpi2c::Sda,
356        Module = hal::iomuxc::consts::Const<N>,
357    >,
358{
359    hal::lpi2c::Lpi2c::new(
360        instance,
361        hal::lpi2c::Pins { scl, sda },
362        &lpi2c_baud(clock_speed),
363    )
364}
365
366/// LPI2C1 peripheral.
367///
368/// - Pin 19 is the clock line.
369/// - Pin 18 is the data line.
370///
371/// Use [`lpi2c`] to create this driver.
372pub type Lpi2c1 = hal::lpi2c::Lpi2c<hal::lpi2c::Pins<pins::common::P19, pins::common::P18>, 1>;
373
374/// LPI2C3 peripheral.
375///
376/// - Pin 16 is the clock line.
377/// - Pin 17 is the data line.
378///
379/// Use [`lpi2c`] to create this driver.
380pub type Lpi2c3 = hal::lpi2c::Lpi2c<hal::lpi2c::Pins<pins::common::P16, pins::common::P17>, 3>;
381
382/// LPSPI1 peripheral.
383///
384/// - SDO:  GPIO_SD_B0_02 (p43) or GPIO_EMC_28 (p50)
385/// - SDI:  GPIO_SD_B0_03 (p42) or GPIO_EMC_29 (p54)
386/// - SCK:  GPIO_SD_B0_00 (p45) or GPIO_EMC_27 (p49)
387/// - PCS0: GPIO_SD_B0_01 (p44) or GPIO_EMC_30
388///
389/// Use [`lpspi`] to create this driver.
390pub type Lpspi1<SDO, SDI, SCK, PCS0> = hal::lpspi::Lpspi<LpspiPins<SDO, SDI, SCK, PCS0>, 1>;
391
392/// LPSPI2 peripheral.
393///
394/// - SDO:  GPIO_SD_B1_08 or GPIO_EMC_02
395/// - SDI:  GPIO_SD_B1_09 or GPIO_EMC_03
396/// - SCK:  GPIO_SD_B1_07 or GPIO_EMC_00
397/// - PCS0: GPIO_SD_B1_06 or GPIO_EMC_01
398///
399/// Use [`lpspi`] to create this driver.
400pub type Lpspi2<SDO, SDI, SCK, PCS0> = hal::lpspi::Lpspi<LpspiPins<SDO, SDI, SCK, PCS0>, 2>;
401
402/// LPSPI3 peripheral.
403///
404/// CS and SDI have two options each for which pin to use.
405///
406/// - Pin 26 is data out (SDO).
407/// - Pin 39 or 1 is data in (SDI).
408/// - Pin 27 is clock (SCK).
409/// - Pin 0 or 38 is chip select (CS).
410///
411/// Use [`lpspi`] to create this driver.
412pub type Lpspi3<SDI, CS> =
413    hal::lpspi::Lpspi<LpspiPins<pins::common::P26, SDI, pins::common::P27, CS>, 3>;
414
415/// LPSPI4 peripheral.
416///
417/// - Pin 10 is chip select (CS).
418/// - Pin 11 is data out (SDO).
419/// - Pin 12 is data in (SDI).
420/// - Pin 13 is clock (SCK).
421///
422/// Use [`lpspi`] to create this driver.
423pub type Lpspi4 = hal::lpspi::Lpspi<
424    LpspiPins<pins::common::P11, pins::common::P12, pins::common::P13, pins::common::P10>,
425    4,
426>;
427
428/// Create a LPSPI peripheral.
429///
430/// `baud` is the SCK frequency, in Hz. Consider using explicit type annotations, as demonstrated
431/// below, to simplify any errors from the compiler.
432///
433/// This helper assumes that the LPSPI clock frequency equals [`LPSPI_FREQUENCY`].
434///
435/// ```no_run
436/// use teensy4_bsp as bsp;
437/// use bsp::board;
438///
439/// let board::T40Resources { lpspi4, pins, .. }
440///     = board::t40(board::instances());
441///
442/// let mut lpspi4: board::Lpspi4 = board::lpspi(
443///     lpspi4,
444///     board::LpspiPins {
445///         sdo: pins.p11,
446///         sdi: pins.p12,
447///         sck: pins.p13,
448///         pcs0: pins.p10,
449///     },
450///     1_000_000,
451/// );
452/// ```
453pub fn lpspi<Sdo, Sdi, Sck, Pcs0, const N: u8>(
454    instance: ral::lpspi::Instance<N>,
455    pins: LpspiPins<Sdo, Sdi, Sck, Pcs0>,
456    baud: u32,
457) -> hal::lpspi::Lpspi<LpspiPins<Sdo, Sdi, Sck, Pcs0>, N>
458where
459    Sdo: hal::iomuxc::lpspi::Pin<
460        Signal = hal::iomuxc::lpspi::Sdo,
461        Module = hal::iomuxc::consts::Const<N>,
462    >,
463    Sdi: hal::iomuxc::lpspi::Pin<
464        Signal = hal::iomuxc::lpspi::Sdi,
465        Module = hal::iomuxc::consts::Const<N>,
466    >,
467    Sck: hal::iomuxc::lpspi::Pin<
468        Signal = hal::iomuxc::lpspi::Sck,
469        Module = hal::iomuxc::consts::Const<N>,
470    >,
471    Pcs0: hal::iomuxc::lpspi::Pin<
472        Signal = hal::iomuxc::lpspi::Pcs0,
473        Module = hal::iomuxc::consts::Const<N>,
474    >,
475{
476    let mut spi = hal::lpspi::Lpspi::new(instance, pins);
477    spi.disabled(|spi| spi.set_clock_hz(LPSPI_FREQUENCY, baud));
478    spi
479}
480
481/// Create a LPUART peripheral.
482///
483/// The specific peripheral you're creating depends on
484///
485/// - the type of `instance`.
486/// - the TX and RX pins.
487/// - the return type, which can be explicitly annotated.
488///
489/// Consider using an explicit type annotation to help catch any
490/// programming errors.
491///
492/// This helper assumes that the UART clock frequency equals [`UART_FREQUENCY`].
493///
494/// ```no_run
495/// use teensy4_bsp as bsp;
496/// use bsp::board;
497///
498/// let board::T40Resources { lpuart6, lpuart2, pins, .. }
499///     = board::t40(board::instances());
500///
501/// // Explicit type:
502/// let mut lpuart6: board::Lpuart6 = board::lpuart(
503///     lpuart6,
504///     pins.p1,
505///     pins.p0,
506///     115200,
507/// );
508///
509/// // Implicit type:
510/// let mut lpuart2 = board::lpuart(lpuart2, pins.p14, pins.p15, 9600);
511/// ```
512pub fn lpuart<Tx, Rx, const N: u8>(
513    instance: ral::lpuart::Instance<N>,
514    tx: Tx,
515    rx: Rx,
516    baud: u32,
517) -> hal::lpuart::Lpuart<hal::lpuart::Pins<Tx, Rx>, N>
518where
519    Tx: hal::iomuxc::lpuart::Pin<
520        Direction = hal::iomuxc::lpuart::Tx,
521        Module = hal::iomuxc::consts::Const<N>,
522    >,
523    Rx: hal::iomuxc::lpuart::Pin<
524        Direction = hal::iomuxc::lpuart::Rx,
525        Module = hal::iomuxc::consts::Const<N>,
526    >,
527{
528    let mut uart = hal::lpuart::Lpuart::new(instance, hal::lpuart::Pins { tx, rx });
529    uart.disable(|uart| uart.set_baud(&lpuart_baud(baud)));
530    uart
531}
532
533/// LPUART6 peripheral.
534///
535/// - Pin 1 is TX.
536/// - Pin 0 is RX.
537///
538/// Use [`lpuart`] to create this driver.
539pub type Lpuart6 = hal::lpuart::Lpuart<hal::lpuart::Pins<pins::common::P1, pins::common::P0>, 6>;
540
541/// LPUART4 peripheral.
542///
543/// - Pin 8 is TX.
544/// - Pin 7 is RX.
545///
546/// Use [`lpuart`] to create this driver.
547pub type Lpuart4 = hal::lpuart::Lpuart<hal::lpuart::Pins<pins::common::P8, pins::common::P7>, 4>;
548
549/// LPUART2 peripheral.
550///
551/// - Pin 14 is TX.
552/// - Pin 15 is RX.
553///
554/// Use [`lpuart`] to create this driver.
555pub type Lpuart2 = hal::lpuart::Lpuart<hal::lpuart::Pins<pins::common::P14, pins::common::P15>, 2>;
556
557/// LPUART3 peripheral.
558///
559/// - Pin 17 is TX.
560/// - Pin 16 is RX.
561///
562/// Use [`lpuart`] to create this driver.
563pub type Lpuart3 = hal::lpuart::Lpuart<hal::lpuart::Pins<pins::common::P17, pins::common::P16>, 3>;
564
565/// LPUART8 peripheral.
566///
567/// - Pin 20 is TX.
568/// - Pin 21 is RX.
569///
570/// Use [`lpuart`] to create this driver.
571pub type Lpuart8 = hal::lpuart::Lpuart<hal::lpuart::Pins<pins::common::P20, pins::common::P21>, 8>;
572
573/// LPUART1 peripheral.
574///
575/// - Pin 24 is TX.
576/// - Pin 25 is RX.
577///
578/// Use [`lpuart`] to create this driver.
579pub type Lpuart1 = hal::lpuart::Lpuart<hal::lpuart::Pins<pins::common::P24, pins::common::P25>, 1>;
580
581/// LPUART7 peripheral.
582///
583/// - Pin 29 is TX.
584/// - Pin 28 is RX.
585///
586/// Use [`lpuart`] to create this driver.
587pub type Lpuart7 = hal::lpuart::Lpuart<hal::lpuart::Pins<pins::common::P29, pins::common::P28>, 7>;
588
589/// LPUART5 peripheral, available on the Teensy 4.1.
590///
591/// - Pin 35 is TX.
592/// - Pin 34 is RX.
593///
594/// Use [`lpuart`] to create this driver.
595pub type Lpuart5 = hal::lpuart::Lpuart<hal::lpuart::Pins<pins::t41::P35, pins::t41::P34>, 5>;
596
597fn prepare_resources<Pins>(
598    mut instances: Instances,
599    from_pads: impl FnOnce(hal::iomuxc::pads::Pads) -> Pins,
600) -> Resources<Pins> {
601    prepare_clocks_and_power(
602        &mut instances.CCM,
603        &mut instances.CCM_ANALOG,
604        &mut instances.DCDC,
605    );
606    let iomuxc = hal::iomuxc::into_pads(instances.IOMUXC);
607    let pins = from_pads(iomuxc);
608
609    // Stop timers in debug mode.
610    ral::modify_reg!(ral::pit, instances.PIT, MCR, FRZ: FRZ_1);
611    let pit = hal::pit::new(instances.PIT);
612
613    let mut gpt1 = hal::gpt::Gpt::new(instances.GPT1);
614    gpt1.disable();
615
616    let mut gpt2 = hal::gpt::Gpt::new(instances.GPT2);
617    gpt2.disable();
618
619    let hal::snvs::Snvs {
620        low_power:
621            hal::snvs::LowPower {
622                core: snvs_lp_core,
623                srtc,
624                ..
625            },
626        ..
627    } = hal::snvs::new(instances.SNVS);
628
629    let adc1 = hal::adc::Adc::new(
630        instances.ADC1,
631        hal::adc::ClockSelect::ADACK,
632        hal::adc::ClockDivision::Div8,
633    );
634    let adc2 = hal::adc::Adc::new(
635        instances.ADC2,
636        hal::adc::ClockSelect::ADACK,
637        hal::adc::ClockDivision::Div8,
638    );
639
640    let trng = hal::trng::Trng::new(instances.TRNG, Default::default(), Default::default());
641    let tempmon = hal::tempmon::TempMon::with_measure_freq(instances.TEMPMON, 0x1000);
642
643    Resources {
644        pit,
645        gpt1,
646        gpt2,
647        gpio1: hal::gpio::Port::new(instances.GPIO1),
648        gpio2: hal::gpio::Port::new(instances.GPIO2),
649        gpio3: hal::gpio::Port::new(instances.GPIO3),
650        gpio4: hal::gpio::Port::new(instances.GPIO4),
651        usb: hal::usbd::Instances {
652            usb: instances.USB1,
653            usbphy: instances.USBPHY1,
654            usbnc: instances.USBNC1,
655        },
656        dma: hal::dma::channels(instances.DMA, instances.DMAMUX),
657        srtc,
658        snvs_lp_core,
659        ccm: instances.CCM,
660        ccm_analog: instances.CCM_ANALOG,
661        dcdc: instances.DCDC,
662        pins,
663        lpi2c1: instances.LPI2C1,
664        lpi2c3: instances.LPI2C3,
665        lpspi1: instances.LPSPI1,
666        lpspi2: instances.LPSPI2,
667        lpspi3: instances.LPSPI3,
668        lpspi4: instances.LPSPI4,
669        lpuart6: instances.LPUART6,
670        lpuart4: instances.LPUART4,
671        lpuart2: instances.LPUART2,
672        lpuart3: instances.LPUART3,
673        lpuart8: instances.LPUART8,
674        lpuart1: instances.LPUART1,
675        lpuart7: instances.LPUART7,
676        lpuart5: instances.LPUART5,
677        flexio1: instances.FLEXIO1,
678        flexio2: instances.FLEXIO2,
679        flexio3: instances.FLEXIO3,
680        flexpwm1: hal::flexpwm::new(instances.PWM1),
681        flexpwm2: hal::flexpwm::new(instances.PWM2),
682        flexpwm3: hal::flexpwm::new(instances.PWM3),
683        flexpwm4: hal::flexpwm::new(instances.PWM4),
684        adc1,
685        adc2,
686        trng,
687        tempmon,
688        sai1: instances.SAI1,
689        sai2: instances.SAI2,
690        sai3: instances.SAI3,
691        iomuxc_gpr: instances.IOMUXC_GPR,
692    }
693}
694
695/// Create resources for the Teensy 4.0 board.
696///
697/// Note that the peripheral instances acquired by RTIC -- named `device` in the
698/// `init::Context` object -- can be used as the argument to this function.
699pub fn t40(instances: impl Into<Instances>) -> T40Resources {
700    prepare_resources(instances.into(), pins::t40::from_pads)
701}
702
703/// Create resources for the Teensy 4.1 board.
704///
705/// Note that the peripheral instances acquired by RTIC -- named `device` in the
706/// `init::Context` object -- can be used as the argument to this function.
707pub fn t41(instances: impl Into<Instances>) -> T41Resources {
708    prepare_resources(instances.into(), pins::t41::from_pads)
709}
710
711/// Create resources for the Teensy MicroMod board.
712///
713/// Note that the peripheral instances acquired by RTIC -- named `device` in the
714/// `init::Context` object -- can be used as the argument to this function.
715pub fn tmm(instances: impl Into<Instances>) -> TMMResources {
716    prepare_resources(instances.into(), pins::tmm::from_pads)
717}