Module stm32_i2s_v12x::driver

source ·
Expand description

Types definitions for I2S drivers

This module provides thin abstractions that try to give access to relevant hardware details while preventing irrelevant or meaningless operations. This allow precise and concise control of a SPI/I2S peripheral. It’s meant for advanced usage, for example with interrupts or DMA. The job is mainly done by I2sDriver and DualI2sDriver, types that wrap respectively an I2sPeripheral and a DualI2sPeripheral to control them.

§Configure and instantiate driver

I2sDriverConfig is used to create configuration of a I2sDriver:

let driver_config = I2sDriverConfig::new_master()
    .direction(Receive)
    .standard(Philips)
    .data_format(DataFormat::Data16Channel32)
    .master_clock(true)
    .request_frequency(48_000);

Then you can instantiate the driver around an I2sPeripheral:

// instantiate from configuration
let driver = driver_config.i2s_driver(i2s_peripheral);

// alternate way
let driver = I2sDriver::new(i2s_peripheral, driver_config);

Similarly, DualI2sDriverConfig is used to create configuration of a DualI2sDriver:

let driver_config = DualI2sDriverConfig::new_master()
    .direction(Receive, Transmit) // set "main" part to receive and "ext" part to transmit
    .standard(Philips)
    .data_format(DataFormat::Data16Channel32)
    .master_clock(true)
    .request_frequency(48_000);

Then you can instantiate the driver around an DualI2sPeripheral:

// instantiate from configuration
let driver = driver_config.dual_i2s_driver(dual_i2s_peripheral);

// alternate way
let driver = DualI2sDriver::new(dual_i2s_peripheral, driver_config);

§Usage

I2sDriver and DualI2sDriver actually give direct access to hardware. They don’t have the concept of audio data. It’s up to the user to reconstruct this information by controlling the hardware and using available information.

Pseudocode example with an I2sDriver configured to receive 16 bit audio data:

let status = driver.status();
if status.rxne() {
    let data = driver.read_data_register();
    match status.chside() {
        Channel::Left => /* `data` contains left channel audio data */,
        Channel::Right => /* `data` contains right channel audio data */,
    }
}

With DualI2sDriver you control 2 peripherals, a “main” SPI peripheral and an “ext” I2SEXT peripheral. Many operations are done on the “main” or “ext” parts. The following pseudocode example explains usage of DualI2sDriver configured for 16 bit audio data with main part receiving and ext part transmitting.

let main_status = driver.main().status();
if main_status.rxne() {
    let received_data = driver.main().read_data_register();
    match main_status.chside() {
        Channel::Left => /* `data` contains left channel audio data */,
        Channel::Right => /* `data` contains right channel audio data */,
    }
}
let ext_status = driver.ext().status();
if ext_status.txe() {
    match ext_status.chside() {
        Channel::Left =>driver.ext().write_data_register(left_data),
        Channel::Right =>driver.ext().write_data_register(right_data),
    }
}

Re-exports§

Structs§

Enums§