Skip to content

Commit 7a933ea

Browse files
authored
Merge pull request #5774 from xoviat/i2s
i2s: add traits
2 parents b49c424 + 518ca78 commit 7a933ea

3 files changed

Lines changed: 62 additions & 3 deletions

File tree

embassy-stm32/build.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,7 @@ fn main() {
10771077
(("spi", "I2S_SD"), quote!(crate::spi::I2sSdPin)),
10781078
(("spi", "I2S_SDI"), quote!(crate::spi::I2sSdPin)),
10791079
(("spi", "I2S_SDO"), quote!(crate::spi::I2sSdPin)),
1080+
(("spi", "I2S_ext_SD"), quote!(crate::i2s::SdExtPin)),
10801081
(("i2c", "SDA"), quote!(crate::i2c::SdaPin)),
10811082
(("i2c", "SCL"), quote!(crate::i2c::SclPin)),
10821083
(("rcc", "MCO_1"), quote!(crate::rcc::McoPin)),
@@ -1794,6 +1795,7 @@ fn main() {
17941795
(("sai", "B"), quote!(crate::sai::Dma<B>)),
17951796
(("spi", "RX"), quote!(crate::spi::RxDma)),
17961797
(("spi", "TX"), quote!(crate::spi::TxDma)),
1798+
(("spi", "EXT"), quote!(crate::i2s::RxDmaExt)),
17971799
(("spdifrx", "RX"), quote!(crate::spdifrx::Dma)),
17981800
(("i2c", "RX"), quote!(crate::i2c::RxDma)),
17991801
(("i2c", "TX"), quote!(crate::i2c::TxDma)),
@@ -1857,6 +1859,10 @@ fn main() {
18571859

18581860
for p in METADATA.peripherals {
18591861
if let Some(regs) = &p.registers {
1862+
if p.name.starts_with("I2S") {
1863+
continue;
1864+
}
1865+
18601866
for trigger in p.triggers {
18611867
let matches = trigger_expr.captures(trigger.signal).unwrap();
18621868
let signal = &matches[1];
@@ -1876,7 +1882,15 @@ fn main() {
18761882
}
18771883

18781884
let mut dupe = HashSet::new();
1879-
for ch in p.dma_channels {
1885+
let mut dma_channels = vec![p.dma_channels.iter()];
1886+
1887+
if let Some(peri) = p.name.strip_prefix("SPI")
1888+
&& let Some(i2s_peri) = peripheral_map.get(format!("I2S{}", peri).as_str())
1889+
{
1890+
dma_channels.push(i2s_peri.dma_channels.iter());
1891+
}
1892+
1893+
for ch in dma_channels.iter_mut().flatten() {
18801894
if let Some(tr) = signals.get(&(regs.kind, ch.signal)) {
18811895
let peri = format_ident!("{}", p.name);
18821896

embassy-stm32/src/i2s.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ enum Function {
3131
Transmit,
3232
/// Receive audio data
3333
Receive,
34-
#[cfg(any(spi_v4, spi_v5))]
35-
34+
#[cfg(any(spi_v4, spi_v5, spi_v2_i2s))]
3635
/// Transmit and Receive audio data
3736
FullDuplex,
3837
}
@@ -349,6 +348,38 @@ impl<'d, W: Word> I2S<'d, W> {
349348
)
350349
}
351350

351+
#[cfg(spi_v2_i2s)]
352+
/// Create a transmitter driver.
353+
pub fn new_full_duplex<T: I2sSExtInstance, D1: TxDma<T>, D2: RxDmaExt<T>, #[cfg(afio)] A>(
354+
peri: Peri<'d, T>,
355+
txsd: Peri<'d, if_afio!(impl MosiPin<T, A>)>,
356+
rxsd: Peri<'d, if_afio!(impl SdExtPin<T, A>)>,
357+
ws: Peri<'d, if_afio!(impl WsPin<T, A>)>,
358+
ck: Peri<'d, if_afio!(impl CkPin<T, A>)>,
359+
mck: Peri<'d, if_afio!(impl MckPin<T, A>)>,
360+
txdma: Peri<'d, D1>,
361+
txdma_buf: &'d mut [W],
362+
rxdma: Peri<'d, D2>,
363+
rxdma_buf: &'d mut [W],
364+
_irq: impl crate::interrupt::typelevel::Binding<D1::Interrupt, crate::dma::InterruptHandler<D1>>
365+
+ crate::interrupt::typelevel::Binding<D2::Interrupt, crate::dma::InterruptHandler<D2>>
366+
+ 'd,
367+
config: Config,
368+
) -> Self {
369+
Self::new_inner(
370+
peri,
371+
new_pin!(txsd, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
372+
new_pin!(rxsd, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
373+
ws,
374+
ck,
375+
new_pin!(mck, AfType::output(OutputType::PushPull, Speed::VeryHigh)),
376+
new_dma!(txdma, _irq).map(|d| (d, txdma_buf)),
377+
new_dma!(rxdma, _irq).map(|d| (d, rxdma_buf)),
378+
config,
379+
Function::FullDuplex,
380+
)
381+
}
382+
352383
#[cfg(any(spi_v4, spi_v5))]
353384
/// Create a full duplex driver.
354385
pub fn new_full_duplex<T: Instance, D1: TxDma<T>, D2: RxDma<T>, #[cfg(afio)] A>(
@@ -644,6 +675,10 @@ impl<'d, W: Word> I2S<'d, W> {
644675
(Mode::Slave, Function::Receive) => I2scfg::SLAVE_RX,
645676
#[cfg(any(spi_v4, spi_v5))]
646677
(Mode::Slave, Function::FullDuplex) => I2scfg::SLAVE_FULL_DUPLEX,
678+
#[cfg(spi_v2_i2s)]
679+
(Mode::Master, Function::FullDuplex) => todo!(),
680+
#[cfg(spi_v2_i2s)]
681+
(Mode::Slave, Function::FullDuplex) => todo!(),
647682
});
648683
});
649684

@@ -782,6 +817,9 @@ fn reset_incompatible_bitfields<T: Instance>() {
782817
regs.udrdr().write(|w| w.0 = 0);
783818
}
784819

820+
dma_trait!(RxDmaExt, I2sSExtInstance);
821+
pin_trait!(SdExtPin, I2sSExtInstance);
822+
785823
/// Full-Duplex I2s Instance
786824
pub trait I2sSExtInstance: spi::Instance {
787825
/// Ext regs

embassy-stm32/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ pub mod wdg;
163163
#[cfg(xspi)]
164164
pub mod xspi;
165165

166+
#[cfg(all(spi, not(any(spi_v1_i2s, spi_v2_i2s, spi_v3_i2s, spi_v4_i2s, spi_v5_i2s))))]
167+
/// Stub module for I2S
168+
pub mod i2s {
169+
dma_trait!(RxDmaExt, crate::spi::Instance);
170+
pin_trait!(SdExtPin, crate::spi::Instance);
171+
}
172+
166173
#[cfg(feature = "_executor")]
167174
pub mod executor;
168175

0 commit comments

Comments
 (0)