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

wip: Serial audio interface (SAI) #248

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

mgottschlag
Copy link
Contributor

@mgottschlag mgottschlag commented Jan 3, 2021

This PR contains some preliminary code for the SAI. In the stm32f7xx-hal a driver already exists iirc, but that driver is limited to duplex I2S. This API is slightly more flexible, yet similarly easy to use.

Usage example:

// Initialize clocks.
let rcc = ctx.device.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().unwrap() == 172.mhz().into());
assert!(clocks.saib_clk().unwrap() == 172.mhz().into());

let gpioe = ctx.device.GPIOE.split();
// SAIB is made synchronous to A.
let (saia, saib) = ctx.device.SAI.split_sync_b();
let protocol = Protocol {
    sync: Synchronization::I2S,
    word_size: 16,
    slot_size: 16,
    num_slots: 2,
};
let tx = saia.master_tx(
    (
        gpioe.pe2.into_alternate_af6(),
        gpioe.pe4.into_alternate_af6(),
        gpioe.pe5.into_alternate_af6(),
        gpioe.pe6.into_alternate_af6(),
    ),
    protocol,
    48000.hz(),
    clocks,
);
let rx = saib.slave_rx(gpioe.pe3.into_alternate_af6(), protocol);

let mut duplex = Duplex::new(rx, tx);
duplex.start();
loop {
    duplex.try_send(0xaaaa, 0xf0f0).ok();
    let _input = duplex.try_read();
}

Bits still missing:

  • DMA
  • Implementation of the new embedded-hal I2S traits
  • Support for everything but the STM32F429 (should be very simple)
  • Functions to deinitialize the SAI and release the pins
  • Documentation on how to set the SAI clock

bors bot added a commit that referenced this pull request Apr 15, 2021
265: Add I2S communication using SPI peripherals r=therealprof a=samcrow

# Introduction

Welcome to my first large pull request, which adds support for I2S audio communication using supported SPI peripherals. Like the way we support CAN with the bxcan library, I am proposing to support I2S using my [stm32_i2s_v12x](https://crates.io/crates/stm32_i2s_v12x) library.

Although stm32_i2s_v12x is in a separate repository, we can also talk about it here.

# Notes

* The I2S module is available if the `i2s` feature is enabled, in order to not increase compile time for applications that don't use I2S.
* All four modes are supported: master transmit, master receive, slave transmit, and slave receive.
* Basic support for DMA, interrupts, and error detection is provided.
* All STM32F4 models are supported.
* I added two examples that run on the STM32F411E-DISCO board and send audio to the on-board DAC. One of them uses DMA.

These changes are not perfect, so criticism and suggestions are welcome.

# Limitations

* No support for full-duplex communication
* I have tested master transmit mode fairly thoroughly, but have not tested the other modes.
* No support for embedded-hal I2S traits
  * [The I2S pull request](rust-embedded/embedded-hal#204) is still under review. The proposed traits look compatible with this driver code. Until that pull request gets merged and released, people can still use this driver code directly.

# Related work

* SAI driver pull request in this repository: #248
* Another, less complete, pull request for I2S in this repository: #212
  * also #145
* embedded-hal pull request for I2S traits: rust-embedded/embedded-hal#204 

Co-authored-by: Sam Crow <scrow@eng.ucsd.edu>
Co-authored-by: Sam Crow <samcrow@users.noreply.github.com>
@burrbull
Copy link
Contributor

burrbull commented Mar 1, 2022

Any progress on this?

Mathias Gottschlag and others added 3 commits March 4, 2023 10:19
… now).

Some bits are still missing:
- Support for DMA
- Implementation of the new embedded-hal I2S traits
- Documentation about how to set the intermediate SAI clock
- Support for other MCUs
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.

None yet

2 participants