Skip to content

Commit b75a622

Browse files
authored
Misc minor cleanups (#85)
1 parent 8798306 commit b75a622

File tree

3 files changed

+74
-56
lines changed

3 files changed

+74
-56
lines changed

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@ embassy-time = { version = "0.5.0", optional = true }
3333
embassy-time-driver = { version = "0.2.1", optional = true }
3434

3535
[features]
36-
default = []
36+
default = ["rt"]
3737

3838
# Base defmt feature enables core + panic handler
3939
# Use with one logger feature: defmt-rtt (preferred) or defmt-uart (fallback)
4040
defmt = ["dep:defmt", "mcxa-pac/defmt"]
4141

4242
unstable-pac = []
4343

44+
# dummy feature to silence embassy-hal-internal lint
45+
#
46+
# This feature makes no change to embassy-mcxa's operation.
47+
rt = []
48+
4449
# Embassy time
4550
time = [
4651
"dep:embassy-time",

src/clocks/periph_helpers.rs

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,50 @@ pub trait SPConfHelper {
4141
fn post_enable_config(&self, clocks: &Clocks) -> Result<u32, ClockError>;
4242
}
4343

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
67+
macro_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+
4488
// config types
4589

4690
/// This type represents a divider in the range 1..=16.
@@ -217,22 +261,7 @@ impl SPConfHelper for Lpi2cConfig {
217261
},
218262
};
219263

220-
// set clksel
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())
264+
apply_div4!(self, clksel, clkdiv, variant, freq)
236265
}
237266
}
238267

@@ -347,24 +376,7 @@ impl SPConfHelper for LpuartConfig {
347376
};
348377

349378
// set clksel
350-
clksel.modify(|_r, w| w.mux().variant(variant));
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())
379+
apply_div4!(self, clksel, clkdiv, variant, freq)
368380
}
369381
}
370382

@@ -482,25 +494,9 @@ impl SPConfHelper for AdcConfig {
482494
return Ok(0);
483495
}
484496
};
497+
let clksel = mrcc0.mrcc_adc_clksel();
498+
let clkdiv = mrcc0.mrcc_adc_clkdiv();
485499

486-
// set clksel
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())
500+
apply_div4!(self, clksel, clkdiv, variant, freq)
505501
}
506502
}

src/interrupt.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,26 @@
77
#![allow(clippy::missing_safety_doc)]
88

99
mod generated {
10+
#[rustfmt::skip]
1011
embassy_hal_internal::interrupt_mod!(
11-
OS_EVENT, RTC, ADC1, GPIO0, GPIO1, GPIO2, GPIO3, GPIO4, LPI2C0, LPI2C1, LPI2C2, LPI2C3, LPUART0, LPUART1,
12-
LPUART2, LPUART3, LPUART4, LPUART5,
12+
ADC1,
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,
1330
);
1431
}
1532

0 commit comments

Comments
 (0)