-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #248 from mgottschlag/sai
wip: Serial audio interface (SAI)
- Loading branch information
Showing
10 changed files
with
970 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#![deny(unsafe_code)] | ||
#![deny(warnings)] | ||
#![no_main] | ||
#![no_std] | ||
|
||
use panic_halt as _; | ||
|
||
use stm32f4xx_hal as hal; | ||
|
||
use crate::hal::{ | ||
pac, | ||
prelude::*, | ||
sai::{Duplex, Protocol, SlotSize, Synchronization, WordSize}, | ||
}; | ||
use cortex_m_rt::entry; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
let p = pac::Peripherals::take().unwrap(); | ||
|
||
// The following code configures the both sub-blocks of SAI for full-duplex communication using | ||
// I2S-encoded audio. | ||
|
||
// Initialize clocks. | ||
let rcc = p.RCC.constrain(); | ||
let clocks = rcc | ||
.cfgr | ||
.use_hse(8.MHz()) | ||
.saia_clk(172.MHz()) | ||
.saib_clk(172.MHz()) | ||
.freeze(); | ||
// Test that the SAI clock is suitable for 48000KHz audio. | ||
assert!(clocks.saia_clk() == Some(172.MHz())); | ||
assert!(clocks.saib_clk() == Some(172.MHz())); | ||
|
||
let gpioe = p.GPIOE.split(); | ||
// SAIB is made synchronous to A. | ||
let (saia, saib) = p.SAI.split_sync_b(); | ||
let protocol = Protocol { | ||
sync: Synchronization::I2S, | ||
word_size: WordSize::Bit16, | ||
slot_size: SlotSize::DataSize, | ||
num_slots: 2, | ||
}; | ||
let tx = saia.master_tx( | ||
(gpioe.pe2, gpioe.pe4, gpioe.pe5, gpioe.pe6), | ||
protocol, | ||
48.kHz(), | ||
&clocks, | ||
); | ||
let rx = saib.slave_rx(gpioe.pe3, protocol); | ||
|
||
let mut duplex = Duplex::new(rx, tx); | ||
duplex.start(); | ||
loop { | ||
duplex.try_send(0xaaaa, 0xf0f0).ok(); | ||
let _input = duplex.try_read(); | ||
} | ||
|
||
/* | ||
// The following code configures the A sub-block of SAI as a master transmitter for PCM-encoded audio. | ||
// Initialize clocks. | ||
let rcc = p.RCC.constrain(); | ||
let clocks = rcc.cfgr.use_hse(8.MHz()).saia_clk(172.MHz()).freeze(); | ||
// Test that the SAI clock is suitable for 48000KHz audio. | ||
assert!(clocks.saia_clk() == Some(172.MHz())); | ||
let gpioe = p.GPIOE.split(); | ||
let (saia, _) = p.SAI.split(); | ||
let protocol = Protocol { | ||
sync: Synchronization::PCMShortFrame, | ||
word_size: WordSize::Bit16, | ||
slot_size: SlotSize::DataSize, | ||
// Stereo audio, two slots per frame. | ||
num_slots: 2, | ||
}; | ||
let mut tx = saia.master_tx( | ||
(gpioe.pe2, gpioe.pe4, gpioe.pe5, gpioe.pe6), | ||
protocol, | ||
48.kHz(), | ||
&clocks, | ||
); | ||
tx.start(); | ||
loop { | ||
tx.try_send(0xaaaa, 0xf0f0).ok(); | ||
} | ||
*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.