Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for STM32 SPDIFRX #3280

Merged
merged 5 commits into from
Nov 19, 2024
Merged

Conversation

elagil
Copy link
Contributor

@elagil elagil commented Aug 24, 2024

This adds support for the STM32 SPDIFRX peripheral.

Requires embassy-rs/stm32-data#518

@elagil elagil force-pushed the feat_spdifrx_driver branch 2 times, most recently from 590c608 to f4ac473 Compare August 28, 2024 21:14
@elagil elagil force-pushed the feat_spdifrx_driver branch from f4ac473 to 462f0a4 Compare September 5, 2024 19:46
@kalkyl
Copy link
Contributor

kalkyl commented Sep 10, 2024

This is great! Do you have a basic example of how to use it?

@elagil
Copy link
Contributor Author

elagil commented Sep 10, 2024

@kalkyl Yes, I will make one for the STM32H7 + SAI (which can use the SPDIFRX symbol clock), but the SPDIFRX feature is not yet complete.

It is missing some sort of left/right detection, otherwise left and right are randomly assigned, depending on what channel the first frame was meant for.

@kalkyl
Copy link
Contributor

kalkyl commented Sep 10, 2024

@kalkyl Yes, I will make one for the STM32H7 + SAI (which can use the SPDIFRX symbol clock), but the SPDIFRX feature is not yet complete.

It is missing some sort of left/right detection, otherwise left and right are randomly assigned, depending on what channel the first frame was meant for.

IIRC it's the "C" bit (channel status) in "DR", 0=left, 1=right?

@elagil elagil force-pushed the feat_spdifrx_driver branch from 20035bd to 2a8b0d2 Compare October 21, 2024 19:49
@elagil elagil force-pushed the feat_spdifrx_driver branch 2 times, most recently from 1794e43 to f09a7fe Compare November 3, 2024 22:36
@elagil elagil marked this pull request as ready for review November 3, 2024 22:39
@elagil
Copy link
Contributor Author

elagil commented Nov 3, 2024

The example will try to synchronize to an S/PDIF source. When it does, it outputs to SAI.

examples/stm32h723  cargo run --bin spdifrx                                                                                                                                                                                                                                  15s  Sun Nov  3 23:40:49 2024
   Compiling embassy-stm32h7-examples v0.1.0 (/home/elagil/repo/firmware-rs/embassy/examples/stm32h723)
    Finished `dev` profile [optimized + debuginfo] target(s) in 0.47s
     Running `probe-rs run --chip STM32H723ZGTx target/thumbv7em-none-eabihf/debug/spdifrx`
      Erasing ✔ [00:00:01] [#######################################################################################################################################################################################################################################################################################################] 128.00 KiB/128.00 KiB @ 69.26 KiB/s (eta 0s )
  Programming ✔ [00:00:00] [#########################################################################################################################################################################################################################################################################################################] 37.00 KiB/37.00 KiB @ 47.15 KiB/s (eta 0s )    Finished in 2.644s
DEBUG flash: latency=2 wrhighfreq=2
└─ embassy_stm32::rcc::_version::flash_setup @ /home/elagil/repo/firmware-rs/embassy/embassy-stm32/src/rcc/h.rs:1000
TRACE BDCR ok: 00008200
└─ embassy_stm32::rcc::bd::{impl#3}::init @ /home/elagil/repo/firmware-rs/embassy/embassy-stm32/src/rcc/bd.rs:198 
DEBUG rcc: Clocks { csi: MaybeHertz(0), hclk1: MaybeHertz(200000000), hclk2: MaybeHertz(200000000), hclk3: MaybeHertz(200000000), hclk4: MaybeHertz(200000000), hse: MaybeHertz(0), hsi: MaybeHertz(64000000), hsi48: MaybeHertz(48000000), i2s_ckin: MaybeHertz(0), lse: MaybeHertz(0), lsi: MaybeHertz(0), pclk1: MaybeHertz(100000000), pclk1_tim: MaybeHertz(200000000), pclk2: MaybeHertz(100000000), pclk2_tim: MaybeHertz(200000000), pclk3: MaybeHertz(100000000), pclk4: MaybeHertz(100000000), pll1_q: MaybeHertz(400000000), pll2_p: MaybeHertz(0), pll2_q: MaybeHertz(0), pll2_r: MaybeHertz(0), pll3_p: MaybeHertz(0), pll3_q: MaybeHertz(0), pll3_r: MaybeHertz(0), rtc: MaybeHertz(32000), sys: MaybeHertz(400000000) }
└─ embassy_stm32::rcc::set_freqs @ /home/elagil/repo/firmware-rs/embassy/embassy-stm32/src/rcc/mod.rs:71  
INFO  SPDIFRX to SAI4 bridge
└─ spdifrx::____embassy_main_task::{async_fn#0} @ src/bin/spdifrx.rs:58  
TRACE SPDIFRX source sync error, e.g. disconnect.
└─ spdifrx::____embassy_main_task::{async_fn#0} @ src/bin/spdifrx.rs:109 
TRACE SPDIFRX source sync error, e.g. disconnect.
└─ spdifrx::____embassy_main_task::{async_fn#0} @ src/bin/spdifrx.rs:109 
TRACE SPDIFRX source sync error, e.g. disconnect.
└─ spdifrx::____embassy_main_task::{async_fn#0} @ src/bin/spdifrx.rs:109 
TRACE SPDIFRX source sync error, e.g. disconnect.
└─ spdifrx::____embassy_main_task::{async_fn#0} @ src/bin/spdifrx.rs:109 
TRACE SPDIFRX source sync error, e.g. disconnect.
└─ spdifrx::____embassy_main_task::{async_fn#0} @ src/bin/spdifrx.rs:109 
TRACE SPDIFRX source sync error, e.g. disconnect.
└─ spdifrx::____embassy_main_task::{async_fn#0} @ src/bin/spdifrx.rs:109 
TRACE SPDIFRX IRQ
└─ embassy_stm32::spdifrx::{impl#5}::on_interrupt @ /home/elagil/repo/firmware-rs/embassy/embassy-stm32/src/spdifrx/mod.rs:361 
TRACE SPDIFRX sync success, enable
└─ embassy_stm32::spdifrx::{impl#5}::on_interrupt::{closure#0} @ /home/elagil/repo/firmware-rs/embassy/embassy-stm32/src/spdifrx/mod.rs:374 
TRACE Renew SAI.
└─ spdifrx::____embassy_main_task::{async_fn#0} @ src/bin/spdifrx.rs:115 

@elagil elagil marked this pull request as draft November 3, 2024 22:55
@elagil elagil marked this pull request as ready for review November 4, 2024 22:31
@elagil elagil force-pushed the feat_spdifrx_driver branch from 9bd2ef9 to f8d29c6 Compare November 4, 2024 22:34
@elagil
Copy link
Contributor Author

elagil commented Nov 5, 2024

Requires: embassy-rs/stm32-data#535

@elagil elagil force-pushed the feat_spdifrx_driver branch from 4d4f14c to 62dbe40 Compare November 6, 2024 12:42
@elagil
Copy link
Contributor Author

elagil commented Nov 6, 2024

@kalkyl Do you have some chance or hardware for testing this? I verified functionality on my STM32H723, including channel synchronization (left/right).

It makes the most sense to test this on:

  • STM32H723/733
  • H725/735 or
  • H730

They connect the SPDIFRX symbol clock to the input of SAI4, so symbol clocks can be synchronized.

@kalkyl
Copy link
Contributor

kalkyl commented Nov 6, 2024

@kalkyl Do you have some chance or hardware for testing this? I verified functionality on my STM32H723, including channel synchronization (left/right).

It makes the most sense to test this on:

  • STM32H723/733

  • H725/735 or

  • H730

They connect the SPDIFRX symbol clock to the input of SAI4, so symbol clocks can be synchronized.

I think i only have the h750 and the h745 from the h7 family right now in the lab...

@elagil
Copy link
Contributor Author

elagil commented Nov 6, 2024

h750 and the h745

Ok, they don't have it, unfortunately. You could dump the samples into SAI regardless, with some glitches when the buffers drift apart. It would be nice to have some symbol clock output (on a pin) and then use it as the external I2S/SAI clock, but I don't think that is possible either.

@elagil
Copy link
Contributor Author

elagil commented Nov 6, 2024

Needs #3511

@elagil elagil force-pushed the feat_spdifrx_driver branch from 62dbe40 to c7c59bd Compare November 6, 2024 18:57
@elagil
Copy link
Contributor Author

elagil commented Nov 6, 2024

Finally, this seems to be ready for review.

@elagil elagil force-pushed the feat_spdifrx_driver branch from c7c59bd to 84f7bff Compare November 6, 2024 19:21
@Dirbaio
Copy link
Member

Dirbaio commented Nov 6, 2024

bender run

(flaky network issue...)

@elagil elagil force-pushed the feat_spdifrx_driver branch from 84f7bff to 3cece13 Compare November 6, 2024 20:15
@kalkyl
Copy link
Contributor

kalkyl commented Nov 8, 2024

Thanks for working on this! Looks great to me!

@elagil
Copy link
Contributor Author

elagil commented Nov 11, 2024

Here is another example of this code in action: https://github.com/blus-audio/firmware-rs/blob/main/blus_mini_mk2/src/main.rs#L298

The data is then consumed here: https://github.com/blus-audio/firmware-rs/blob/main/blus_mini_mk2/src/audio_routing.rs#L323

There is some strangeness happening (output only 16 of 24 bit) due to me playing on four SAI slots. This is due to my hardware configuration.

@elagil
Copy link
Contributor Author

elagil commented Nov 11, 2024

I want to do some more improvements to this.. Making it a draft again.

@elagil elagil marked this pull request as draft November 11, 2024 22:30
@elagil elagil force-pushed the feat_spdifrx_driver branch from 3cece13 to 577a429 Compare November 16, 2024 15:08
@elagil elagil marked this pull request as ready for review November 16, 2024 15:10
@elagil
Copy link
Contributor Author

elagil commented Nov 16, 2024

Should be fine now. I get reliable left/right synchronization.

@elagil
Copy link
Contributor Author

elagil commented Nov 16, 2024

Making this a draft again, because playing from SPDIFRX causes some crazy side effects in my testing...

@elagil elagil marked this pull request as draft November 16, 2024 22:56
@elagil
Copy link
Contributor Author

elagil commented Nov 17, 2024

The strange behavior was resolved.

I used the SPDIFRX symbol clock to drive SAI, and once SPDIF was disconnected, there was no way to switch SAI clock sources again. Therefore, backup symbol clock had to be enabled to help with that.

image

@elagil elagil marked this pull request as ready for review November 17, 2024 13:42
@elagil elagil force-pushed the feat_spdifrx_driver branch from 0650b13 to ba90e4e Compare November 17, 2024 13:46
@elagil elagil force-pushed the feat_spdifrx_driver branch from ba90e4e to cb8528a Compare November 18, 2024 19:39
@Dirbaio
Copy link
Member

Dirbaio commented Nov 18, 2024

bender run

1 similar comment
@Dirbaio
Copy link
Member

Dirbaio commented Nov 18, 2024

bender run

@elagil elagil force-pushed the feat_spdifrx_driver branch from cb8528a to 99dd5e7 Compare November 18, 2024 19:51
@Dirbaio
Copy link
Member

Dirbaio commented Nov 18, 2024

bender run

@elagil elagil requested a review from kalkyl November 18, 2024 23:13
@Dirbaio Dirbaio added this pull request to the merge queue Nov 19, 2024
Merged via the queue into embassy-rs:main with commit 227e073 Nov 19, 2024
7 checks passed
@elagil elagil deleted the feat_spdifrx_driver branch November 19, 2024 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants