diff options
Diffstat (limited to 'embassy-nxp/src/lib.rs')
| -rw-r--r-- | embassy-nxp/src/lib.rs | 87 |
1 files changed, 83 insertions, 4 deletions
diff --git a/embassy-nxp/src/lib.rs b/embassy-nxp/src/lib.rs index 433aca9e0..5e77fc0db 100644 --- a/embassy-nxp/src/lib.rs +++ b/embassy-nxp/src/lib.rs | |||
| @@ -1,11 +1,20 @@ | |||
| 1 | #![no_std] | 1 | #![no_std] |
| 2 | 2 | ||
| 3 | // This mod MUST go first, so that the others see its macros. | ||
| 4 | pub(crate) mod fmt; | ||
| 5 | |||
| 3 | pub mod gpio; | 6 | pub mod gpio; |
| 4 | #[cfg(feature = "lpc55")] | 7 | #[cfg(feature = "lpc55")] |
| 5 | pub mod pint; | 8 | pub mod pint; |
| 6 | 9 | ||
| 10 | #[cfg(feature = "_time_driver")] | ||
| 11 | #[cfg_attr(feature = "time-driver-pit", path = "time_driver/pit.rs")] | ||
| 12 | mod time_driver; | ||
| 13 | |||
| 7 | // This mod MUST go last, so that it sees all the `impl_foo!` macros | 14 | // This mod MUST go last, so that it sees all the `impl_foo!` macros |
| 8 | #[cfg_attr(feature = "lpc55", path = "chips/lpc55.rs")] | 15 | #[cfg_attr(feature = "lpc55", path = "chips/lpc55.rs")] |
| 16 | #[cfg_attr(feature = "mimxrt1011", path = "chips/mimxrt1011.rs")] | ||
| 17 | #[cfg_attr(feature = "mimxrt1062", path = "chips/mimxrt1062.rs")] | ||
| 9 | mod chip; | 18 | mod chip; |
| 10 | 19 | ||
| 11 | #[cfg(feature = "unstable-pac")] | 20 | #[cfg(feature = "unstable-pac")] |
| @@ -21,13 +30,66 @@ pub use embassy_hal_internal::{Peri, PeripheralType}; | |||
| 21 | /// | 30 | /// |
| 22 | /// This should only be called once and at startup, otherwise it panics. | 31 | /// This should only be called once and at startup, otherwise it panics. |
| 23 | pub fn init(_config: config::Config) -> Peripherals { | 32 | pub fn init(_config: config::Config) -> Peripherals { |
| 24 | #[cfg(feature = "lpc55")] | 33 | // Do this first, so that it panics if user is calling `init` a second time |
| 34 | // before doing anything important. | ||
| 35 | let peripherals = Peripherals::take(); | ||
| 36 | |||
| 37 | #[cfg(feature = "mimxrt1011")] | ||
| 25 | { | 38 | { |
| 26 | gpio::init(); | 39 | // The RT1010 Reference manual states that core clock root must be switched before |
| 27 | pint::init(); | 40 | // reprogramming PLL2. |
| 41 | pac::CCM.cbcdr().modify(|w| { | ||
| 42 | w.set_periph_clk_sel(pac::ccm::vals::PeriphClkSel::PERIPH_CLK_SEL_1); | ||
| 43 | }); | ||
| 44 | |||
| 45 | while matches!( | ||
| 46 | pac::CCM.cdhipr().read().periph_clk_sel_busy(), | ||
| 47 | pac::ccm::vals::PeriphClkSelBusy::PERIPH_CLK_SEL_BUSY_1 | ||
| 48 | ) {} | ||
| 49 | |||
| 50 | info!("Core clock root switched"); | ||
| 51 | |||
| 52 | // 480 * 18 / 24 = 360 | ||
| 53 | pac::CCM_ANALOG.pfd_480().modify(|x| x.set_pfd2_frac(12)); | ||
| 54 | |||
| 55 | //480*18/24(pfd0)/4 | ||
| 56 | pac::CCM_ANALOG.pfd_480().modify(|x| x.set_pfd0_frac(24)); | ||
| 57 | pac::CCM.cscmr1().modify(|x| x.set_flexspi_podf(3.into())); | ||
| 58 | |||
| 59 | // CPU Core | ||
| 60 | pac::CCM_ANALOG.pfd_528().modify(|x| x.set_pfd3_frac(18)); | ||
| 61 | cortex_m::asm::delay(500_000); | ||
| 62 | |||
| 63 | // Clock core clock with PLL 2. | ||
| 64 | pac::CCM | ||
| 65 | .cbcdr() | ||
| 66 | .modify(|x| x.set_periph_clk_sel(pac::ccm::vals::PeriphClkSel::PERIPH_CLK_SEL_0)); // false | ||
| 67 | |||
| 68 | while matches!( | ||
| 69 | pac::CCM.cdhipr().read().periph_clk_sel_busy(), | ||
| 70 | pac::ccm::vals::PeriphClkSelBusy::PERIPH_CLK_SEL_BUSY_1 | ||
| 71 | ) {} | ||
| 72 | |||
| 73 | pac::CCM | ||
| 74 | .cbcmr() | ||
| 75 | .write(|v| v.set_pre_periph_clk_sel(pac::ccm::vals::PrePeriphClkSel::PRE_PERIPH_CLK_SEL_0)); | ||
| 76 | |||
| 77 | // TODO: Some for USB PLLs | ||
| 78 | |||
| 79 | // DCDC clock? | ||
| 80 | pac::CCM.ccgr6().modify(|v| v.set_cg0(1)); | ||
| 28 | } | 81 | } |
| 29 | 82 | ||
| 30 | crate::Peripherals::take() | 83 | #[cfg(any(feature = "lpc55", rt1xxx))] |
| 84 | gpio::init(); | ||
| 85 | |||
| 86 | #[cfg(feature = "lpc55")] | ||
| 87 | pint::init(); | ||
| 88 | |||
| 89 | #[cfg(feature = "_time_driver")] | ||
| 90 | time_driver::init(); | ||
| 91 | |||
| 92 | peripherals | ||
| 31 | } | 93 | } |
| 32 | 94 | ||
| 33 | /// HAL configuration for the NXP board. | 95 | /// HAL configuration for the NXP board. |
| @@ -35,3 +97,20 @@ pub mod config { | |||
| 35 | #[derive(Default)] | 97 | #[derive(Default)] |
| 36 | pub struct Config {} | 98 | pub struct Config {} |
| 37 | } | 99 | } |
| 100 | |||
| 101 | #[allow(unused)] | ||
| 102 | struct BitIter(u32); | ||
| 103 | |||
| 104 | impl Iterator for BitIter { | ||
| 105 | type Item = u32; | ||
| 106 | |||
| 107 | fn next(&mut self) -> Option<Self::Item> { | ||
| 108 | match self.0.trailing_zeros() { | ||
| 109 | 32 => None, | ||
| 110 | b => { | ||
| 111 | self.0 &= !(1 << b); | ||
| 112 | Some(b) | ||
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 116 | } | ||
