aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Munns <[email protected]>2025-12-03 20:08:44 +0100
committerGitHub <[email protected]>2025-12-03 20:08:44 +0100
commitb75a6222e892cc58543b30f231b60dedaa0a5f5d (patch)
tree8cccb88b8e61e3eac4bd0715a5370171ecb05ccd
parent8798306fb2c124efc06443c2913c3d7a4919dd83 (diff)
Misc minor cleanups (#85)
-rw-r--r--Cargo.toml7
-rw-r--r--src/clocks/periph_helpers.rs102
-rw-r--r--src/interrupt.rs21
3 files changed, 74 insertions, 56 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 96b7d6b0f..22660bee9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -33,7 +33,7 @@ embassy-time = { version = "0.5.0", optional = true }
33embassy-time-driver = { version = "0.2.1", optional = true } 33embassy-time-driver = { version = "0.2.1", optional = true }
34 34
35[features] 35[features]
36default = [] 36default = ["rt"]
37 37
38# Base defmt feature enables core + panic handler 38# Base defmt feature enables core + panic handler
39# Use with one logger feature: defmt-rtt (preferred) or defmt-uart (fallback) 39# Use with one logger feature: defmt-rtt (preferred) or defmt-uart (fallback)
@@ -41,6 +41,11 @@ defmt = ["dep:defmt", "mcxa-pac/defmt"]
41 41
42unstable-pac = [] 42unstable-pac = []
43 43
44# dummy feature to silence embassy-hal-internal lint
45#
46# This feature makes no change to embassy-mcxa's operation.
47rt = []
48
44# Embassy time 49# Embassy time
45time = [ 50time = [
46 "dep:embassy-time", 51 "dep:embassy-time",
diff --git a/src/clocks/periph_helpers.rs b/src/clocks/periph_helpers.rs
index 24d074e8a..1ea7a99ed 100644
--- a/src/clocks/periph_helpers.rs
+++ b/src/clocks/periph_helpers.rs
@@ -41,6 +41,50 @@ pub trait SPConfHelper {
41 fn post_enable_config(&self, clocks: &Clocks) -> Result<u32, ClockError>; 41 fn post_enable_config(&self, clocks: &Clocks) -> Result<u32, ClockError>;
42} 42}
43 43
44/// Copy and paste macro that:
45///
46/// * Sets the clocksel mux to `$selvar`
47/// * Resets and halts the div, and applies the calculated div4 bits
48/// * Releases reset + halt
49/// * Waits for the div to stabilize
50/// * Returns `Ok($freq / $conf.div.into_divisor())`
51///
52/// Assumes:
53///
54/// * self is a configuration struct that has a field called `div`, which
55/// is a `Div4`
56///
57/// usage:
58///
59/// ```rust
60/// apply_div4!(self, clksel, clkdiv, variant, freq)
61/// ```
62///
63/// In the future if we make all the clksel+clkdiv pairs into commonly derivedFrom
64/// registers, or if we put some kind of simple trait around those regs, we could
65/// do this with something other than a macro, but for now, this is harm-reduction
66/// to avoid incorrect copy + paste
67macro_rules! apply_div4 {
68 ($conf:ident, $selreg:ident, $divreg:ident, $selvar:ident, $freq:ident) => {{
69 // set clksel
70 $selreg.modify(|_r, w| w.mux().variant($selvar));
71
72 // Set up clkdiv
73 $divreg.modify(|_r, w| {
74 unsafe { w.div().bits($conf.div.into_bits()) }
75 .halt()
76 .asserted()
77 .reset()
78 .asserted()
79 });
80 $divreg.modify(|_r, w| w.halt().deasserted().reset().deasserted());
81
82 while $divreg.read().unstab().is_unstable() {}
83
84 Ok($freq / $conf.div.into_divisor())
85 }};
86}
87
44// config types 88// config types
45 89
46/// This type represents a divider in the range 1..=16. 90/// This type represents a divider in the range 1..=16.
@@ -217,22 +261,7 @@ impl SPConfHelper for Lpi2cConfig {
217 }, 261 },
218 }; 262 };
219 263
220 // set clksel 264 apply_div4!(self, clksel, clkdiv, variant, freq)
221 clksel.modify(|_r, w| w.mux().variant(variant));
222
223 // Set up clkdiv
224 clkdiv.modify(|_r, w| {
225 unsafe { w.div().bits(self.div.into_bits()) }
226 .halt()
227 .asserted()
228 .reset()
229 .asserted()
230 });
231 clkdiv.modify(|_r, w| w.halt().deasserted().reset().deasserted());
232
233 while clkdiv.read().unstab().is_unstable() {}
234
235 Ok(freq / self.div.into_divisor())
236 } 265 }
237} 266}
238 267
@@ -347,24 +376,7 @@ impl SPConfHelper for LpuartConfig {
347 }; 376 };
348 377
349 // set clksel 378 // set clksel
350 clksel.modify(|_r, w| w.mux().variant(variant)); 379 apply_div4!(self, clksel, clkdiv, variant, freq)
351
352 // Set up clkdiv
353 clkdiv.modify(|_r, w| {
354 w.halt().asserted();
355 w.reset().asserted();
356 unsafe { w.div().bits(self.div.into_bits()) };
357 w
358 });
359 clkdiv.modify(|_r, w| {
360 w.halt().deasserted();
361 w.reset().deasserted();
362 w
363 });
364
365 while clkdiv.read().unstab().is_unstable() {}
366
367 Ok(freq / self.div.into_divisor())
368 } 380 }
369} 381}
370 382
@@ -482,25 +494,9 @@ impl SPConfHelper for AdcConfig {
482 return Ok(0); 494 return Ok(0);
483 } 495 }
484 }; 496 };
497 let clksel = mrcc0.mrcc_adc_clksel();
498 let clkdiv = mrcc0.mrcc_adc_clkdiv();
485 499
486 // set clksel 500 apply_div4!(self, clksel, clkdiv, variant, freq)
487 mrcc0.mrcc_adc_clksel().modify(|_r, w| w.mux().variant(variant));
488
489 // Set up clkdiv
490 mrcc0.mrcc_adc_clkdiv().modify(|_r, w| {
491 w.halt().asserted();
492 w.reset().asserted();
493 unsafe { w.div().bits(self.div.into_bits()) };
494 w
495 });
496 mrcc0.mrcc_adc_clkdiv().modify(|_r, w| {
497 w.halt().deasserted();
498 w.reset().deasserted();
499 w
500 });
501
502 while mrcc0.mrcc_adc_clkdiv().read().unstab().is_unstable() {}
503
504 Ok(freq / self.div.into_divisor())
505 } 501 }
506} 502}
diff --git a/src/interrupt.rs b/src/interrupt.rs
index 0490e3a66..1d10e3b2d 100644
--- a/src/interrupt.rs
+++ b/src/interrupt.rs
@@ -7,9 +7,26 @@
7#![allow(clippy::missing_safety_doc)] 7#![allow(clippy::missing_safety_doc)]
8 8
9mod generated { 9mod generated {
10 #[rustfmt::skip]
10 embassy_hal_internal::interrupt_mod!( 11 embassy_hal_internal::interrupt_mod!(
11 OS_EVENT, RTC, ADC1, GPIO0, GPIO1, GPIO2, GPIO3, GPIO4, LPI2C0, LPI2C1, LPI2C2, LPI2C3, LPUART0, LPUART1, 12 ADC1,
12 LPUART2, LPUART3, LPUART4, LPUART5, 13 GPIO0,
14 GPIO1,
15 GPIO2,
16 GPIO3,
17 GPIO4,
18 LPI2C0,
19 LPI2C1,
20 LPI2C2,
21 LPI2C3,
22 LPUART0,
23 LPUART1,
24 LPUART2,
25 LPUART3,
26 LPUART4,
27 LPUART5,
28 OS_EVENT,
29 RTC,
13 ); 30 );
14} 31}
15 32