diff options
| author | James Munns <[email protected]> | 2025-12-18 13:55:57 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-18 13:55:57 +0000 |
| commit | b5b49cbcf3a991bf6d434b0870da50f3ee722612 (patch) | |
| tree | dd1c673cbe1e2512c785173762f8d63e472c5e8b | |
| parent | f85337064c724f8fdb855e847345908e4c7384e9 (diff) | |
| parent | d6c65cd0e4b651b1b07e1583562dfccfd5db22b1 (diff) | |
Merge pull request #5071 from jamesmunns/james/bad-sosc
[MCXA]: Add support for SOSC/clk_in
| -rw-r--r-- | embassy-mcxa/src/clkout.rs | 8 | ||||
| -rw-r--r-- | embassy-mcxa/src/clocks/config.rs | 23 | ||||
| -rw-r--r-- | embassy-mcxa/src/clocks/mod.rs | 128 | ||||
| -rw-r--r-- | embassy-mcxa/src/gpio.rs | 20 | ||||
| -rw-r--r-- | embassy-mcxa/src/i2c/mod.rs | 7 | ||||
| -rw-r--r-- | embassy-mcxa/src/lib.rs | 24 | ||||
| -rw-r--r-- | examples/mcxa/src/bin/clkout.rs | 84 |
7 files changed, 239 insertions, 55 deletions
diff --git a/embassy-mcxa/src/clkout.rs b/embassy-mcxa/src/clkout.rs index 5b21f24b0..3495eb886 100644 --- a/embassy-mcxa/src/clkout.rs +++ b/embassy-mcxa/src/clkout.rs | |||
| @@ -20,6 +20,7 @@ pub struct ClockOut<'a> { | |||
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | /// Selected clock source to output | 22 | /// Selected clock source to output |
| 23 | #[derive(Copy, Clone)] | ||
| 23 | pub enum ClockOutSel { | 24 | pub enum ClockOutSel { |
| 24 | /// 12MHz Internal Oscillator | 25 | /// 12MHz Internal Oscillator |
| 25 | Fro12M, | 26 | Fro12M, |
| @@ -36,6 +37,7 @@ pub enum ClockOutSel { | |||
| 36 | } | 37 | } |
| 37 | 38 | ||
| 38 | /// Configuration for the ClockOut | 39 | /// Configuration for the ClockOut |
| 40 | #[derive(Copy, Clone)] | ||
| 39 | pub struct Config { | 41 | pub struct Config { |
| 40 | /// Selected Source Clock | 42 | /// Selected Source Clock |
| 41 | pub sel: ClockOutSel, | 43 | pub sel: ClockOutSel, |
| @@ -157,6 +159,12 @@ mod sealed { | |||
| 157 | fn mux(&self) { | 159 | fn mux(&self) { |
| 158 | self.set_function(crate::pac::port0::pcr0::Mux::$func); | 160 | self.set_function(crate::pac::port0::pcr0::Mux::$func); |
| 159 | self.set_pull(Pull::Disabled); | 161 | self.set_pull(Pull::Disabled); |
| 162 | |||
| 163 | // TODO: we may want to expose these as options to allow the slew rate | ||
| 164 | // and drive strength for clocks if they are particularly high speed. | ||
| 165 | // | ||
| 166 | // self.set_drive_strength(crate::pac::port0::pcr0::Dse::Dse1); | ||
| 167 | // self.set_slew_rate(crate::pac::port0::pcr0::Sre::Sre0); | ||
| 160 | } | 168 | } |
| 161 | } | 169 | } |
| 162 | }; | 170 | }; |
diff --git a/embassy-mcxa/src/clocks/config.rs b/embassy-mcxa/src/clocks/config.rs index 0563b8917..9f97160ff 100644 --- a/embassy-mcxa/src/clocks/config.rs +++ b/embassy-mcxa/src/clocks/config.rs | |||
| @@ -119,6 +119,28 @@ pub struct ClocksConfig { | |||
| 119 | pub sirc: SircConfig, | 119 | pub sirc: SircConfig, |
| 120 | /// FRO16K clock source | 120 | /// FRO16K clock source |
| 121 | pub fro16k: Option<Fro16KConfig>, | 121 | pub fro16k: Option<Fro16KConfig>, |
| 122 | /// SOSC, clk_in clock source | ||
| 123 | pub sosc: Option<SoscConfig>, | ||
| 124 | } | ||
| 125 | |||
| 126 | /// The mode of the external reference clock | ||
| 127 | #[derive(Copy, Clone)] | ||
| 128 | pub enum SoscMode { | ||
| 129 | /// Passive crystal oscillators | ||
| 130 | CrystalOscillator, | ||
| 131 | /// Active external reference clock | ||
| 132 | ActiveClock, | ||
| 133 | } | ||
| 134 | |||
| 135 | // SOSC/clk_in configuration | ||
| 136 | #[derive(Copy, Clone)] | ||
| 137 | pub struct SoscConfig { | ||
| 138 | /// Mode of the external reference clock | ||
| 139 | pub mode: SoscMode, | ||
| 140 | /// Specific frequency of the external reference clock | ||
| 141 | pub frequency: u32, | ||
| 142 | /// Power state of the external reference clock | ||
| 143 | pub power: PoweredClock, | ||
| 122 | } | 144 | } |
| 123 | 145 | ||
| 124 | // FIRC/FRO180M | 146 | // FIRC/FRO180M |
| @@ -199,6 +221,7 @@ impl Default for ClocksConfig { | |||
| 199 | vsys_domain_active: true, | 221 | vsys_domain_active: true, |
| 200 | vdd_core_domain_active: true, | 222 | vdd_core_domain_active: true, |
| 201 | }), | 223 | }), |
| 224 | sosc: None, | ||
| 202 | } | 225 | } |
| 203 | } | 226 | } |
| 204 | } | 227 | } |
diff --git a/embassy-mcxa/src/clocks/mod.rs b/embassy-mcxa/src/clocks/mod.rs index 037f0a656..b41a1ba46 100644 --- a/embassy-mcxa/src/clocks/mod.rs +++ b/embassy-mcxa/src/clocks/mod.rs | |||
| @@ -87,6 +87,7 @@ pub fn init(settings: ClocksConfig) -> Result<(), ClockError> { | |||
| 87 | operator.configure_firc_clocks()?; | 87 | operator.configure_firc_clocks()?; |
| 88 | operator.configure_sirc_clocks()?; | 88 | operator.configure_sirc_clocks()?; |
| 89 | operator.configure_fro16k_clocks()?; | 89 | operator.configure_fro16k_clocks()?; |
| 90 | operator.configure_sosc()?; | ||
| 90 | 91 | ||
| 91 | // For now, just use FIRC as the main/cpu clock, which should already be | 92 | // For now, just use FIRC as the main/cpu clock, which should already be |
| 92 | // the case on reset | 93 | // the case on reset |
| @@ -136,6 +137,7 @@ pub fn with_clocks<R: 'static, F: FnOnce(&Clocks) -> R>(f: F) -> Option<R> { | |||
| 136 | #[non_exhaustive] | 137 | #[non_exhaustive] |
| 137 | pub struct Clocks { | 138 | pub struct Clocks { |
| 138 | /// The `clk_in` is a clock provided by an external oscillator | 139 | /// The `clk_in` is a clock provided by an external oscillator |
| 140 | /// AKA SOSC | ||
| 139 | pub clk_in: Option<Clock>, | 141 | pub clk_in: Option<Clock>, |
| 140 | 142 | ||
| 141 | // FRO180M stuff | 143 | // FRO180M stuff |
| @@ -485,8 +487,20 @@ impl Clocks { | |||
| 485 | } | 487 | } |
| 486 | 488 | ||
| 487 | /// Ensure the `clk_in` clock is active and valid at the given power state. | 489 | /// Ensure the `clk_in` clock is active and valid at the given power state. |
| 488 | pub fn ensure_clk_in_active(&self, _at_level: &PoweredClock) -> Result<u32, ClockError> { | 490 | pub fn ensure_clk_in_active(&self, at_level: &PoweredClock) -> Result<u32, ClockError> { |
| 489 | Err(ClockError::NotImplemented { clock: "clk_in" }) | 491 | let Some(clk) = self.clk_in.as_ref() else { |
| 492 | return Err(ClockError::BadConfig { | ||
| 493 | clock: "clk_in", | ||
| 494 | reason: "required but not active", | ||
| 495 | }); | ||
| 496 | }; | ||
| 497 | if !clk.power.meets_requirement_of(at_level) { | ||
| 498 | return Err(ClockError::BadConfig { | ||
| 499 | clock: "clk_in", | ||
| 500 | reason: "not low power active", | ||
| 501 | }); | ||
| 502 | } | ||
| 503 | Ok(clk.frequency) | ||
| 490 | } | 504 | } |
| 491 | 505 | ||
| 492 | /// Ensure the `clk_16k_vsys` clock is active and valid at the given power state. | 506 | /// Ensure the `clk_16k_vsys` clock is active and valid at the given power state. |
| @@ -851,6 +865,116 @@ impl ClockOperator<'_> { | |||
| 851 | 865 | ||
| 852 | Ok(()) | 866 | Ok(()) |
| 853 | } | 867 | } |
| 868 | |||
| 869 | /// Configure the SOSC/clk_in oscillator | ||
| 870 | fn configure_sosc(&mut self) -> Result<(), ClockError> { | ||
| 871 | let Some(parts) = self.config.sosc.as_ref() else { | ||
| 872 | return Ok(()); | ||
| 873 | }; | ||
| 874 | |||
| 875 | let scg0 = unsafe { pac::Scg0::steal() }; | ||
| 876 | |||
| 877 | // TODO: Config for the LDO? For now, if we have Sosc, just enable | ||
| 878 | // using the default settings: | ||
| 879 | // LDOBYPASS: 0/not bypassed | ||
| 880 | // VOUT_SEL: 0b100: 1.1v | ||
| 881 | // LDOEN: 0/Disabled | ||
| 882 | scg0.ldocsr().modify(|_r, w| w.ldoen().enabled()); | ||
| 883 | while scg0.ldocsr().read().vout_ok().is_disabled() {} | ||
| 884 | |||
| 885 | // TODO: something something pins? This seems to work when the pins are | ||
| 886 | // not enabled, even if GPIO hasn't been initialized at all yet. | ||
| 887 | let eref = match parts.mode { | ||
| 888 | config::SoscMode::CrystalOscillator => pac::scg0::sosccfg::Erefs::Internal, | ||
| 889 | config::SoscMode::ActiveClock => pac::scg0::sosccfg::Erefs::External, | ||
| 890 | }; | ||
| 891 | let freq = parts.frequency; | ||
| 892 | |||
| 893 | // TODO: Fix PAC names here | ||
| 894 | // | ||
| 895 | // #[doc = "0: Frequency range select of 8-16 MHz."] | ||
| 896 | // Freq16to20mhz = 0, | ||
| 897 | // #[doc = "1: Frequency range select of 16-25 MHz."] | ||
| 898 | // LowFreq = 1, | ||
| 899 | // #[doc = "2: Frequency range select of 25-40 MHz."] | ||
| 900 | // MediumFreq = 2, | ||
| 901 | // #[doc = "3: Frequency range select of 40-50 MHz."] | ||
| 902 | // HighFreq = 3, | ||
| 903 | let range = match freq { | ||
| 904 | 0..8_000_000 => { | ||
| 905 | return Err(ClockError::BadConfig { | ||
| 906 | clock: "clk_in", | ||
| 907 | reason: "freq too low", | ||
| 908 | }); | ||
| 909 | } | ||
| 910 | 8_000_000..16_000_000 => pac::scg0::sosccfg::Range::Freq16to20mhz, | ||
| 911 | 16_000_000..25_000_000 => pac::scg0::sosccfg::Range::LowFreq, | ||
| 912 | 25_000_000..40_000_000 => pac::scg0::sosccfg::Range::MediumFreq, | ||
| 913 | 40_000_000..50_000_001 => pac::scg0::sosccfg::Range::HighFreq, | ||
| 914 | 50_000_001.. => { | ||
| 915 | return Err(ClockError::BadConfig { | ||
| 916 | clock: "clk_in", | ||
| 917 | reason: "freq too high", | ||
| 918 | }); | ||
| 919 | } | ||
| 920 | }; | ||
| 921 | |||
| 922 | // Set source/erefs and range | ||
| 923 | scg0.sosccfg().modify(|_r, w| { | ||
| 924 | w.erefs().variant(eref); | ||
| 925 | w.range().variant(range); | ||
| 926 | w | ||
| 927 | }); | ||
| 928 | |||
| 929 | // Disable lock | ||
| 930 | scg0.sosccsr().modify(|_r, w| w.lk().clear_bit()); | ||
| 931 | |||
| 932 | // TODO: We could enable the SOSC clock monitor. There are some things to | ||
| 933 | // figure out first: | ||
| 934 | // | ||
| 935 | // * This requires SIRC to be enabled, not sure which branch. Maybe fro12m_root? | ||
| 936 | // * If SOSC needs to work in deep sleep, AND the monitor is enabled: | ||
| 937 | // * SIRC also need needs to be low power | ||
| 938 | // * We need to decide if we need an interrupt or a reset if the monitor trips | ||
| 939 | |||
| 940 | // Apply remaining config | ||
| 941 | scg0.sosccsr().modify(|_r, w| { | ||
| 942 | // For now, just disable the monitor. See above. | ||
| 943 | w.sosccm().disabled(); | ||
| 944 | |||
| 945 | // Set deep sleep mode | ||
| 946 | match parts.power { | ||
| 947 | PoweredClock::NormalEnabledDeepSleepDisabled => { | ||
| 948 | w.soscsten().clear_bit(); | ||
| 949 | } | ||
| 950 | PoweredClock::AlwaysEnabled => { | ||
| 951 | w.soscsten().set_bit(); | ||
| 952 | } | ||
| 953 | } | ||
| 954 | |||
| 955 | // Enable SOSC | ||
| 956 | w.soscen().enabled() | ||
| 957 | }); | ||
| 958 | |||
| 959 | // Wait for SOSC to be valid, check for errors | ||
| 960 | while !scg0.sosccsr().read().soscvld().bit_is_set() {} | ||
| 961 | if scg0.sosccsr().read().soscerr().is_enabled_and_error() { | ||
| 962 | return Err(ClockError::BadConfig { | ||
| 963 | clock: "clk_in", | ||
| 964 | reason: "soscerr is set", | ||
| 965 | }); | ||
| 966 | } | ||
| 967 | |||
| 968 | // Re-lock the sosc | ||
| 969 | scg0.sosccsr().modify(|_r, w| w.lk().set_bit()); | ||
| 970 | |||
| 971 | self.clocks.clk_in = Some(Clock { | ||
| 972 | frequency: freq, | ||
| 973 | power: parts.power, | ||
| 974 | }); | ||
| 975 | |||
| 976 | Ok(()) | ||
| 977 | } | ||
| 854 | } | 978 | } |
| 855 | 979 | ||
| 856 | // | 980 | // |
diff --git a/embassy-mcxa/src/gpio.rs b/embassy-mcxa/src/gpio.rs index 65f8df985..29d66656d 100644 --- a/embassy-mcxa/src/gpio.rs +++ b/embassy-mcxa/src/gpio.rs | |||
| @@ -81,7 +81,7 @@ fn GPIO4() { | |||
| 81 | irq_handler(4, crate::pac::Gpio4::ptr()); | 81 | irq_handler(4, crate::pac::Gpio4::ptr()); |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | pub(crate) unsafe fn init() { | 84 | pub(crate) unsafe fn interrupt_init() { |
| 85 | use embassy_hal_internal::interrupt::InterruptExt; | 85 | use embassy_hal_internal::interrupt::InterruptExt; |
| 86 | 86 | ||
| 87 | crate::pac::interrupt::GPIO0.enable(); | 87 | crate::pac::interrupt::GPIO0.enable(); |
| @@ -320,8 +320,12 @@ impl GpioPin for AnyPin {} | |||
| 320 | 320 | ||
| 321 | macro_rules! impl_pin { | 321 | macro_rules! impl_pin { |
| 322 | ($peri:ident, $port:expr, $pin:expr, $block:ident) => { | 322 | ($peri:ident, $port:expr, $pin:expr, $block:ident) => { |
| 323 | impl_pin!(crate::peripherals, $peri, $port, $pin, $block); | ||
| 324 | }; | ||
| 325 | |||
| 326 | ($perip:path, $peri:ident, $port:expr, $pin:expr, $block:ident) => { | ||
| 323 | paste! { | 327 | paste! { |
| 324 | impl SealedPin for crate::peripherals::$peri { | 328 | impl SealedPin for $perip::$peri { |
| 325 | fn pin_port(&self) -> usize { | 329 | fn pin_port(&self) -> usize { |
| 326 | $port * 32 + $pin | 330 | $port * 32 + $pin |
| 327 | } | 331 | } |
| @@ -372,15 +376,15 @@ macro_rules! impl_pin { | |||
| 372 | } | 376 | } |
| 373 | } | 377 | } |
| 374 | 378 | ||
| 375 | impl GpioPin for crate::peripherals::$peri {} | 379 | impl GpioPin for $perip::$peri {} |
| 376 | 380 | ||
| 377 | impl From<crate::peripherals::$peri> for AnyPin { | 381 | impl From<$perip::$peri> for AnyPin { |
| 378 | fn from(value: crate::peripherals::$peri) -> Self { | 382 | fn from(value: $perip::$peri) -> Self { |
| 379 | value.degrade() | 383 | value.degrade() |
| 380 | } | 384 | } |
| 381 | } | 385 | } |
| 382 | 386 | ||
| 383 | impl crate::peripherals::$peri { | 387 | impl $perip::$peri { |
| 384 | /// Convenience helper to obtain a type-erased handle to this pin. | 388 | /// Convenience helper to obtain a type-erased handle to this pin. |
| 385 | pub fn degrade(&self) -> AnyPin { | 389 | pub fn degrade(&self) -> AnyPin { |
| 386 | AnyPin::new(self.port(), self.pin(), self.gpio(), self.port_reg(), self.pcr_reg()) | 390 | AnyPin::new(self.port(), self.pin(), self.gpio(), self.port_reg(), self.pcr_reg()) |
| @@ -453,8 +457,8 @@ impl_pin!(P1_26, 1, 26, Gpio1); | |||
| 453 | impl_pin!(P1_27, 1, 27, Gpio1); | 457 | impl_pin!(P1_27, 1, 27, Gpio1); |
| 454 | impl_pin!(P1_28, 1, 28, Gpio1); | 458 | impl_pin!(P1_28, 1, 28, Gpio1); |
| 455 | impl_pin!(P1_29, 1, 29, Gpio1); | 459 | impl_pin!(P1_29, 1, 29, Gpio1); |
| 456 | impl_pin!(P1_30, 1, 30, Gpio1); | 460 | impl_pin!(crate::internal_peripherals, P1_30, 1, 30, Gpio1); |
| 457 | impl_pin!(P1_31, 1, 31, Gpio1); | 461 | impl_pin!(crate::internal_peripherals, P1_31, 1, 31, Gpio1); |
| 458 | 462 | ||
| 459 | impl_pin!(P2_0, 2, 0, Gpio2); | 463 | impl_pin!(P2_0, 2, 0, Gpio2); |
| 460 | impl_pin!(P2_1, 2, 1, Gpio2); | 464 | impl_pin!(P2_1, 2, 1, Gpio2); |
diff --git a/embassy-mcxa/src/i2c/mod.rs b/embassy-mcxa/src/i2c/mod.rs index 9a014224a..55c933f71 100644 --- a/embassy-mcxa/src/i2c/mod.rs +++ b/embassy-mcxa/src/i2c/mod.rs | |||
| @@ -180,8 +180,11 @@ impl_pin!(P1_12, LPI2C1, Mux2, SdaPin); | |||
| 180 | impl_pin!(P1_13, LPI2C1, Mux2, SclPin); | 180 | impl_pin!(P1_13, LPI2C1, Mux2, SclPin); |
| 181 | impl_pin!(P1_14, LPI2C1, Mux2, SclPin); | 181 | impl_pin!(P1_14, LPI2C1, Mux2, SclPin); |
| 182 | impl_pin!(P1_15, LPI2C1, Mux2, SdaPin); | 182 | impl_pin!(P1_15, LPI2C1, Mux2, SdaPin); |
| 183 | impl_pin!(P1_30, LPI2C0, Mux3, SdaPin); | 183 | // NOTE: P1_30 and P1_31 are typically used for the external oscillator |
| 184 | impl_pin!(P1_31, LPI2C0, Mux3, SclPin); | 184 | // For now, we just don't give users these pins. |
| 185 | // | ||
| 186 | // impl_pin!(P1_30, LPI2C0, Mux3, SdaPin); | ||
| 187 | // impl_pin!(P1_31, LPI2C0, Mux3, SclPin); | ||
| 185 | impl_pin!(P3_27, LPI2C3, Mux2, SclPin); | 188 | impl_pin!(P3_27, LPI2C3, Mux2, SclPin); |
| 186 | impl_pin!(P3_28, LPI2C3, Mux2, SdaPin); | 189 | impl_pin!(P3_28, LPI2C3, Mux2, SdaPin); |
| 187 | // impl_pin!(P3_29, LPI2C3, Mux2, HreqPin); What is this HREQ pin? | 190 | // impl_pin!(P3_29, LPI2C3, Mux2, HreqPin); What is this HREQ pin? |
diff --git a/embassy-mcxa/src/lib.rs b/embassy-mcxa/src/lib.rs index 6383353db..76fd58210 100644 --- a/embassy-mcxa/src/lib.rs +++ b/embassy-mcxa/src/lib.rs | |||
| @@ -175,8 +175,18 @@ embassy_hal_internal::peripherals!( | |||
| 175 | P1_27, | 175 | P1_27, |
| 176 | P1_28, | 176 | P1_28, |
| 177 | P1_29, | 177 | P1_29, |
| 178 | P1_30, | 178 | // TODO: These pins are optionally used as the clock sources for SOSC. |
| 179 | P1_31, | 179 | // Ideally, we'd want to have a custom version of the `peripheral!` macro |
| 180 | // that presented these as `Option<Peri<'_, P1_30>>` instead of `Peri<'_, P1_30>` | ||
| 181 | // when the user DOES enable the external SOSC. For now, I'm guessing MOST designs | ||
| 182 | // will have an external clock sitting on these pins anyway, so we just notch them | ||
| 183 | // out from the `Peripherals` struct given to users. | ||
| 184 | // | ||
| 185 | // If you find this and want your extra two pins to be available: please open an | ||
| 186 | // embassy issue to discuss how we could do this. | ||
| 187 | // | ||
| 188 | // P1_30, | ||
| 189 | // P1_31, | ||
| 180 | 190 | ||
| 181 | P2_0, | 191 | P2_0, |
| 182 | P2_1, | 192 | P2_1, |
| @@ -337,6 +347,14 @@ embassy_hal_internal::peripherals!( | |||
| 337 | WWDT0, | 347 | WWDT0, |
| 338 | ); | 348 | ); |
| 339 | 349 | ||
| 350 | // See commented out items above to understand why we create the instances | ||
| 351 | // here but don't give them to the user. | ||
| 352 | pub(crate) mod internal_peripherals { | ||
| 353 | embassy_hal_internal::peripherals_definition!(P1_30, P1_31,); | ||
| 354 | |||
| 355 | pub(crate) use peripherals::*; | ||
| 356 | } | ||
| 357 | |||
| 340 | // Use cortex-m-rt's #[interrupt] attribute directly; PAC does not re-export it. | 358 | // Use cortex-m-rt's #[interrupt] attribute directly; PAC does not re-export it. |
| 341 | 359 | ||
| 342 | // Re-export interrupt traits and types | 360 | // Re-export interrupt traits and types |
| @@ -369,7 +387,7 @@ pub fn init(cfg: crate::config::Config) -> Peripherals { | |||
| 369 | crate::clocks::init(cfg.clock_cfg).unwrap(); | 387 | crate::clocks::init(cfg.clock_cfg).unwrap(); |
| 370 | 388 | ||
| 371 | unsafe { | 389 | unsafe { |
| 372 | crate::gpio::init(); | 390 | crate::gpio::interrupt_init(); |
| 373 | } | 391 | } |
| 374 | 392 | ||
| 375 | // Initialize DMA controller (clock, reset, configuration) | 393 | // Initialize DMA controller (clock, reset, configuration) |
diff --git a/examples/mcxa/src/bin/clkout.rs b/examples/mcxa/src/bin/clkout.rs index 1e52912d3..e6e6a2d3d 100644 --- a/examples/mcxa/src/bin/clkout.rs +++ b/examples/mcxa/src/bin/clkout.rs | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | use embassy_executor::Spawner; | 4 | use embassy_executor::Spawner; |
| 5 | use embassy_mcxa::clkout::{ClockOut, ClockOutSel, Config, Div4}; | 5 | use embassy_mcxa::clkout::{ClockOut, ClockOutSel, Config, Div4}; |
| 6 | use embassy_mcxa::clocks::PoweredClock; | 6 | use embassy_mcxa::clocks::PoweredClock; |
| 7 | use embassy_mcxa::clocks::config::{SoscConfig, SoscMode}; | ||
| 7 | use embassy_mcxa::gpio::{DriveStrength, Level, Output, SlewRate}; | 8 | use embassy_mcxa::gpio::{DriveStrength, Level, Output, SlewRate}; |
| 8 | use embassy_time::Timer; | 9 | use embassy_time::Timer; |
| 9 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; | 10 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; |
| @@ -11,58 +12,61 @@ use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; | |||
| 11 | /// Demonstrate CLKOUT, using Pin P4.2 | 12 | /// Demonstrate CLKOUT, using Pin P4.2 |
| 12 | #[embassy_executor::main] | 13 | #[embassy_executor::main] |
| 13 | async fn main(_spawner: Spawner) { | 14 | async fn main(_spawner: Spawner) { |
| 14 | let p = hal::init(hal::config::Config::default()); | 15 | let mut cfg = hal::config::Config::default(); |
| 16 | cfg.clock_cfg.sosc = Some(SoscConfig { | ||
| 17 | mode: SoscMode::CrystalOscillator, | ||
| 18 | frequency: 8_000_000, | ||
| 19 | power: PoweredClock::NormalEnabledDeepSleepDisabled, | ||
| 20 | }); | ||
| 21 | |||
| 22 | let p = hal::init(cfg); | ||
| 23 | |||
| 15 | let mut pin = p.P4_2; | 24 | let mut pin = p.P4_2; |
| 16 | let mut clkout = p.CLKOUT; | 25 | let mut clkout = p.CLKOUT; |
| 17 | 26 | ||
| 18 | loop { | 27 | const K16_CONFIG: Config = Config { |
| 19 | defmt::info!("Set Low..."); | 28 | sel: ClockOutSel::Clk16K, |
| 20 | let mut output = Output::new(pin.reborrow(), Level::Low, DriveStrength::Normal, SlewRate::Slow); | 29 | div: Div4::no_div(), |
| 21 | Timer::after_millis(500).await; | 30 | level: PoweredClock::NormalEnabledDeepSleepDisabled, |
| 31 | }; | ||
| 32 | const M4_CONFIG: Config = Config { | ||
| 33 | sel: ClockOutSel::Fro12M, | ||
| 34 | div: const { Div4::from_divisor(3).unwrap() }, | ||
| 35 | level: PoweredClock::NormalEnabledDeepSleepDisabled, | ||
| 36 | }; | ||
| 37 | const K512_CONFIG: Config = Config { | ||
| 38 | sel: ClockOutSel::ClkIn, | ||
| 39 | div: const { Div4::from_divisor(16).unwrap() }, | ||
| 40 | level: PoweredClock::NormalEnabledDeepSleepDisabled, | ||
| 41 | }; | ||
| 22 | 42 | ||
| 43 | let configs = [ | ||
| 44 | ("16K -> /1 = 16K", K16_CONFIG), | ||
| 45 | ("12M -> /3 = 4M", M4_CONFIG), | ||
| 46 | ("8M -> /16 = 512K", K512_CONFIG), | ||
| 47 | ]; | ||
| 48 | |||
| 49 | loop { | ||
| 23 | defmt::info!("Set High..."); | 50 | defmt::info!("Set High..."); |
| 24 | output.set_high(); | 51 | let mut output = Output::new(pin.reborrow(), Level::High, DriveStrength::Normal, SlewRate::Slow); |
| 25 | Timer::after_millis(400).await; | 52 | Timer::after_millis(250).await; |
| 26 | 53 | ||
| 27 | defmt::info!("Set Low..."); | 54 | defmt::info!("Set Low..."); |
| 28 | output.set_low(); | 55 | output.set_low(); |
| 29 | Timer::after_millis(500).await; | 56 | Timer::after_millis(750).await; |
| 30 | 57 | ||
| 31 | defmt::info!("16k..."); | 58 | for (name, conf) in configs.iter() { |
| 32 | // Run Clock Out with the 16K clock | 59 | defmt::info!("Running {=str}", name); |
| 33 | let _clock_out = ClockOut::new( | ||
| 34 | clkout.reborrow(), | ||
| 35 | pin.reborrow(), | ||
| 36 | Config { | ||
| 37 | sel: ClockOutSel::Clk16K, | ||
| 38 | div: Div4::no_div(), | ||
| 39 | level: PoweredClock::NormalEnabledDeepSleepDisabled, | ||
| 40 | }, | ||
| 41 | ) | ||
| 42 | .unwrap(); | ||
| 43 | 60 | ||
| 44 | Timer::after_millis(3000).await; | 61 | let _clock_out = ClockOut::new(clkout.reborrow(), pin.reborrow(), *conf).unwrap(); |
| 45 | |||
| 46 | defmt::info!("Set Low..."); | ||
| 47 | drop(_clock_out); | ||
| 48 | 62 | ||
| 49 | let _output = Output::new(pin.reborrow(), Level::Low, DriveStrength::Normal, SlewRate::Slow); | 63 | Timer::after_millis(3000).await; |
| 50 | Timer::after_millis(500).await; | ||
| 51 | 64 | ||
| 52 | // Run Clock Out with the 12M clock, divided by 3 | 65 | defmt::info!("Set Low..."); |
| 53 | defmt::info!("4M..."); | 66 | drop(_clock_out); |
| 54 | let _clock_out = ClockOut::new( | ||
| 55 | clkout.reborrow(), | ||
| 56 | pin.reborrow(), | ||
| 57 | Config { | ||
| 58 | sel: ClockOutSel::Fro12M, | ||
| 59 | div: const { Div4::from_divisor(3).unwrap() }, | ||
| 60 | level: PoweredClock::NormalEnabledDeepSleepDisabled, | ||
| 61 | }, | ||
| 62 | ) | ||
| 63 | .unwrap(); | ||
| 64 | 67 | ||
| 65 | // Let it run for 3 seconds... | 68 | let _output = Output::new(pin.reborrow(), Level::Low, DriveStrength::Normal, SlewRate::Slow); |
| 66 | Timer::after_millis(3000).await; | 69 | Timer::after_millis(500).await; |
| 70 | } | ||
| 67 | } | 71 | } |
| 68 | } | 72 | } |
