Skip to content

Commit fc9d5b8

Browse files
committed
make I2sFreq trait similar to BusClock
1 parent fb847b9 commit fc9d5b8

File tree

3 files changed

+67
-66
lines changed

3 files changed

+67
-66
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2929
- Allow different lengths of buffers in hal_1 SpiBus impl [#566]
3030
- Clean SPI write impls
3131
- move `ptr()` to `Ptr` trait [#773]
32+
- make `I2sFreq` trait similar to `BusClock`
3233
- `steal` UART peripheral on `Rx::new`
3334

3435
[#248]: https://github.com/stm32-rs/stm32f4xx-hal/pull/248

src/i2s.rs

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,46 @@ pub trait DualInstance: Instance + gpio::alt::I2sExtPin {
3838

3939
/// Trait to get I2s frequency at SPI peripheral input.
4040
pub trait I2sFreq {
41-
fn i2s_freq(clocks: &Clocks) -> Hertz;
41+
fn try_i2s_freq(clocks: &Clocks) -> Option<Hertz>;
42+
fn i2s_freq(clocks: &Clocks) -> Hertz {
43+
Self::try_i2s_freq(clocks).expect("I2S clock input for SPI not enabled")
44+
}
45+
}
46+
47+
impl<T> I2sFreq for T
48+
where
49+
T: rcc::RccBus,
50+
T::Bus: I2sFreq,
51+
{
52+
fn try_i2s_freq(clocks: &Clocks) -> Option<Hertz> {
53+
T::Bus::try_i2s_freq(clocks)
54+
}
55+
}
56+
57+
impl I2sFreq for rcc::APB1 {
58+
fn try_i2s_freq(clocks: &Clocks) -> Option<Hertz> {
59+
#[cfg(not(any(feature = "gpio-f412", feature = "gpio-f413", feature = "gpio-f446")))]
60+
{
61+
clocks.i2s_clk()
62+
}
63+
#[cfg(any(feature = "gpio-f412", feature = "gpio-f413", feature = "gpio-f446"))]
64+
{
65+
clocks.i2s_apb1_clk()
66+
}
67+
}
68+
}
69+
70+
impl I2sFreq for rcc::APB2 {
71+
fn try_i2s_freq(clocks: &Clocks) -> Option<Hertz> {
72+
#[cfg(not(any(feature = "gpio-f412", feature = "gpio-f413", feature = "gpio-f446")))]
73+
{
74+
clocks.i2s_clk()
75+
}
76+
#[cfg(any(feature = "gpio-f412", feature = "gpio-f413", feature = "gpio-f446"))]
77+
{
78+
clocks.i2s_apb2_clk()
79+
}
80+
}
4281
}
4382

4483
/// Trait to build an [`I2s`] object from SPI peripheral, pins and clocks
@@ -186,22 +225,12 @@ impl<I: Instance> I2s<I> {
186225
/// $SPI: The fully-capitalized name of the SPI peripheral from pac module (example: SPI1)
187226
/// $I2s: The CamelCase I2S alias name for hal I2s wrapper (example: I2s1).
188227
/// $i2s: module containing the Ws pin definition. (example: i2s1).
189-
/// $clock: The name of the Clocks function that returns the frequency of the I2S clock input
190-
/// to this SPI peripheral (i2s_cl, i2s_apb1_clk, or i2s2_apb_clk)
191228
macro_rules! i2s {
192-
($SPI:ty, $I2s:ident, $i2s:ident, $clock:ident) => {
229+
($SPI:ty, $I2s:ident, $i2s:ident) => {
193230
pub type $I2s = I2s<$SPI>;
194231

195232
impl Instance for $SPI {}
196233

197-
impl I2sFreq for $SPI {
198-
fn i2s_freq(clocks: &Clocks) -> Hertz {
199-
clocks
200-
.$clock()
201-
.expect("I2S clock input for SPI not enabled")
202-
}
203-
}
204-
205234
#[cfg(feature = "i2s")]
206235
impl stm32_i2s_v12x::WsPin for gpio::alt::$i2s::Ws {
207236
fn is_high(&self) -> bool {
@@ -239,42 +268,30 @@ macro_rules! i2s {
239268
};
240269
}
241270

242-
// Actually define the SPI instances that can be used for I2S
243-
// Each one has to be split into two declarations because the F412, F413, F423, and F446
244-
// have two different I2S clocks while other models have only one.
271+
#[cfg(any(
272+
feature = "gpio-f410",
273+
feature = "gpio-f411",
274+
feature = "gpio-f412",
275+
feature = "gpio-f413",
276+
feature = "gpio-f446"
277+
))]
278+
i2s!(pac::SPI1, I2s1, i2s1);
245279

246-
#[cfg(any(feature = "gpio-f410", feature = "gpio-f411"))]
247-
i2s!(pac::SPI1, I2s1, i2s1, i2s_clk);
248-
#[cfg(any(feature = "gpio-f412", feature = "gpio-f413", feature = "gpio-f446"))]
249-
i2s!(pac::SPI1, I2s1, i2s1, i2s_apb2_clk);
280+
i2s!(pac::SPI2, I2s2, i2s2);
250281

251-
// All STM32F4 models support SPI2/I2S2
252-
#[cfg(not(any(feature = "gpio-f412", feature = "gpio-f413", feature = "gpio-f446")))]
253-
i2s!(pac::SPI2, I2s2, i2s2, i2s_clk);
254-
#[cfg(any(feature = "gpio-f412", feature = "gpio-f413", feature = "gpio-f446"))]
255-
i2s!(pac::SPI2, I2s2, i2s2, i2s_apb1_clk);
282+
#[cfg(feature = "spi3")]
283+
i2s!(pac::SPI3, I2s3, i2s3);
284+
285+
#[cfg(any(feature = "gpio-f411", feature = "gpio-f412", feature = "gpio-f413"))]
286+
i2s!(pac::SPI4, I2s4, i2s4);
256287

257-
// All STM32F4 models except STM32F410 support SPI3/I2S3
258288
#[cfg(any(
259-
feature = "gpio-f401",
289+
feature = "gpio-f410",
260290
feature = "gpio-f411",
261-
feature = "gpio-f417",
262-
feature = "gpio-f427",
263-
feature = "gpio-f469",
291+
feature = "gpio-f412",
292+
feature = "gpio-f413"
264293
))]
265-
i2s!(pac::SPI3, I2s3, i2s3, i2s_clk);
266-
#[cfg(any(feature = "gpio-f412", feature = "gpio-f413", feature = "gpio-f446"))]
267-
i2s!(pac::SPI3, I2s3, i2s3, i2s_apb1_clk);
268-
269-
#[cfg(feature = "gpio-f411")]
270-
i2s!(pac::SPI4, I2s4, i2s4, i2s_clk);
271-
#[cfg(any(feature = "gpio-f412", feature = "gpio-f413"))]
272-
i2s!(pac::SPI4, I2s4, i2s4, i2s_apb2_clk);
273-
274-
#[cfg(any(feature = "gpio-f410", feature = "gpio-f411"))]
275-
i2s!(pac::SPI5, I2s5, i2s5, i2s_clk);
276-
#[cfg(any(feature = "gpio-f412", feature = "gpio-f413"))]
277-
i2s!(pac::SPI5, I2s5, i2s5, i2s_apb2_clk);
294+
i2s!(pac::SPI5, I2s5, i2s5);
278295

279296
/// A wrapper around a SPI and a I2SEXT object and pins for full duplex I2S operation
280297
#[allow(clippy::type_complexity)]

src/rcc/f4/mod.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,11 @@ impl RccExt for RCC {
222222
sysclk: None,
223223
pll48clk: false,
224224
i2s_ckin: None,
225-
#[cfg(any(
226-
feature = "gpio-f401",
227-
feature = "gpio-f410",
228-
feature = "gpio-f411",
229-
feature = "gpio-f417",
230-
feature = "gpio-f427",
231-
feature = "gpio-f469",
232-
))]
225+
#[cfg(not(any(
226+
feature = "gpio-f412",
227+
feature = "gpio-f413",
228+
feature = "gpio-f446"
229+
)))]
233230
i2s_clk: None,
234231
#[cfg(any(feature = "gpio-f412", feature = "gpio-f413", feature = "gpio-f446"))]
235232
i2s_apb1_clk: None,
@@ -323,14 +320,7 @@ pub struct CFGR {
323320
pll48clk: bool,
324321

325322
i2s_ckin: Option<u32>,
326-
#[cfg(any(
327-
feature = "gpio-f401",
328-
feature = "gpio-f410",
329-
feature = "gpio-f411",
330-
feature = "gpio-f417",
331-
feature = "gpio-f427",
332-
feature = "gpio-f469",
333-
))]
323+
#[cfg(not(any(feature = "gpio-f412", feature = "gpio-f413", feature = "gpio-f446")))]
334324
i2s_clk: Option<u32>,
335325
#[cfg(any(feature = "gpio-f412", feature = "gpio-f413", feature = "gpio-f446"))]
336326
i2s_apb1_clk: Option<u32>,
@@ -399,14 +389,7 @@ impl CFGR {
399389
}
400390

401391
/// Selects an I2S clock frequency and enables the I2S clock.
402-
#[cfg(any(
403-
feature = "gpio-f401",
404-
feature = "gpio-f410",
405-
feature = "gpio-f411",
406-
feature = "gpio-f417",
407-
feature = "gpio-f427",
408-
feature = "gpio-f469",
409-
))]
392+
#[cfg(not(any(feature = "gpio-f412", feature = "gpio-f413", feature = "gpio-f446")))]
410393
pub fn i2s_clk(mut self, freq: Hertz) -> Self {
411394
self.i2s_clk = Some(freq.raw());
412395
self

0 commit comments

Comments
 (0)