Skip to content

Commit 68f21d0

Browse files
authored
Merge pull request #796 from stm32-rs/i2s_clk
make I2sFreq trait similar to BusClock
2 parents fb847b9 + 97024cc commit 68f21d0

File tree

4 files changed

+196
-186
lines changed

4 files changed

+196
-186
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: 57 additions & 45 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,25 @@ 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.
245-
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);
250-
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);
256-
257-
// All STM32F4 models except STM32F410 support SPI3/I2S3
258271
#[cfg(any(
259-
feature = "gpio-f401",
272+
feature = "gpio-f410",
260273
feature = "gpio-f411",
261-
feature = "gpio-f417",
262-
feature = "gpio-f427",
263-
feature = "gpio-f469",
274+
feature = "gpio-f412",
275+
feature = "gpio-f413",
276+
feature = "gpio-f446"
264277
))]
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);
278+
i2s!(pac::SPI1, I2s1, i2s1);
279+
280+
i2s!(pac::SPI2, I2s2, i2s2);
281+
282+
#[cfg(feature = "spi3")]
283+
i2s!(pac::SPI3, I2s3, i2s3);
284+
285+
#[cfg(feature = "spi4")]
286+
i2s!(pac::SPI4, I2s4, i2s4);
287+
288+
#[cfg(feature = "spi5")]
289+
i2s!(pac::SPI5, I2s5, i2s5);
278290

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

0 commit comments

Comments
 (0)