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
//! NXP iMXRT1062 hardware abstraction layer
//!
//! An [`embedded-hal`](https://crates.io/crates/embedded-hal) implementation
//! targeting processors in NXP's IMXRT106x family.
//!
//! The HAL is a WIP. More documentation will become available once more capabilities
//! are exposed.
//!
//! In some cases, the HAL simply re-exports peripherals from the peripheral access
//! crates (PAC). If they are not re-exported, all PAC components are available
//! in the `pac` module.
//!
//! To see examples of the HAL, check out the `teensy4-bsp` and the `teensy4-examples` crates.
//! We will skip documentation example and tests, since we cannot yet test them as part
//! of the `cargo test` workflow...

#![no_std]

pub use imxrt1062_pac as pac;

pub mod ccm;
pub mod gpio;
pub mod i2c;
pub mod iomuxc;
pub mod pit;
pub mod pwm;
pub mod spi;
pub mod uart;

pub mod dcdc {
    use imxrt1062_pac as pac;
    pub struct DCDC(pub(crate) pac::DCDC);
    impl DCDC {
        pub fn raw(&mut self) -> &pac::DCDC {
            &self.0
        }
    }
}

pub struct Peripherals {
    pub iomuxc: iomuxc::IOMUXC,
    pub systick: pac::SYST,
    pub ccm: ccm::CCM,
    pub pit: pit::UnclockedPIT,
    pub dcdc: dcdc::DCDC,
    pub pwm1: pwm::UnclockedController<pwm::module::_1>,
    pub pwm2: pwm::UnclockedController<pwm::module::_2>,
    pub pwm3: pwm::UnclockedController<pwm::module::_3>,
    pub pwm4: pwm::UnclockedController<pwm::module::_4>,
    pub i2c: i2c::Unclocked,
    pub spi: spi::Unclocked,
    pub uart: uart::Unclocked,
}

impl Peripherals {
    pub fn take() -> Option<Self> {
        let cp = cortex_m::Peripherals::take()?;
        let p = pac::Peripherals::take()?;
        Some(Peripherals::new(p, cp))
    }

    fn new(p: pac::Peripherals, cp: pac::CorePeripherals) -> Self {
        Peripherals {
            iomuxc: iomuxc::IOMUXC::new(p.IOMUXC),
            systick: cp.SYST,
            ccm: ccm::CCM::new(p.CCM, p.CCM_ANALOG),
            pit: pit::UnclockedPIT::new(p.PIT),
            dcdc: dcdc::DCDC(p.DCDC),
            pwm1: pwm::UnclockedController::new(),
            pwm2: pwm::UnclockedController::new(),
            pwm3: pwm::UnclockedController::new(),
            pwm4: pwm::UnclockedController::new(),
            i2c: i2c::Unclocked::new(),
            spi: spi::Unclocked::new(),
            uart: uart::Unclocked {
                uart1: p.LPUART1,
                uart2: p.LPUART2,
                uart3: p.LPUART3,
                uart4: p.LPUART4,
                uart5: p.LPUART5,
                uart6: p.LPUART6,
                uart7: p.LPUART7,
                uart8: p.LPUART8,
            },
        }
    }
}