Apply config

This commit is contained in:
Christian Perez Llamas 2022-11-09 22:47:55 +01:00
parent 3760b60db3
commit 356beabc3b
2 changed files with 65 additions and 22 deletions

View File

@ -9,11 +9,10 @@
//use embassy_hal_common::drop::OnDrop; //use embassy_hal_common::drop::OnDrop;
use embassy_hal_common::{into_ref, PeripheralRef}; use embassy_hal_common::{into_ref, PeripheralRef};
//use crate::pac::i2s::config::mcken;
//use crate::gpio::sealed::Pin as _; //use crate::gpio::sealed::Pin as _;
use crate::gpio::{AnyPin, Pin as GpioPin}; use crate::gpio::{AnyPin, Pin as GpioPin};
use crate::interrupt::Interrupt; use crate::interrupt::Interrupt;
use crate::pac::i2s::CONFIG;
use crate::Peripheral; use crate::Peripheral;
// TODO: Define those in lib.rs somewhere else // TODO: Define those in lib.rs somewhere else
@ -40,7 +39,7 @@ pub enum Error {
#[non_exhaustive] #[non_exhaustive]
pub struct Config { pub struct Config {
pub ratio: Ratio, pub ratio: Ratio,
pub sample_width: SampleWidth, pub swidth: SampleWidth,
pub align: Align, pub align: Align,
pub format: Format, pub format: Format,
pub channels: Channels, pub channels: Channels,
@ -50,7 +49,7 @@ impl Default for Config {
fn default() -> Self { fn default() -> Self {
Self { Self {
ratio: Ratio::_32x, ratio: Ratio::_32x,
sample_width: SampleWidth::_16bit, swidth: SampleWidth::_16bit,
align: Align::Left, align: Align::Left,
format: Format::I2S, format: Format::I2S,
channels: Channels::Stereo, channels: Channels::Stereo,
@ -165,7 +164,7 @@ pub enum Mode {
/// Interface to the UARTE peripheral using EasyDMA to offload the transmission and reception workload. /// Interface to the UARTE peripheral using EasyDMA to offload the transmission and reception workload.
/// ///
/// For more details about EasyDMA, consult the module documentation. /// For more details about EasyDMA, consult the module documentation.
pub struct I2s<'d, T: Instance> { pub struct I2S<'d, T: Instance> {
output: I2sOutput<'d, T>, output: I2sOutput<'d, T>,
_input: I2sInput<'d, T>, _input: I2sInput<'d, T>,
} }
@ -182,7 +181,7 @@ pub struct I2sInput<'d, T: Instance> {
_p: PeripheralRef<'d, T>, _p: PeripheralRef<'d, T>,
} }
impl<'d, T: Instance> I2s<'d, T> { impl<'d, T: Instance> I2S<'d, T> {
/// Create a new I2S /// Create a new I2S
pub fn new( pub fn new(
i2s: impl Peripheral<P = T> + 'd, i2s: impl Peripheral<P = T> + 'd,
@ -215,25 +214,13 @@ impl<'d, T: Instance> I2s<'d, T> {
lrck: PeripheralRef<'d, AnyPin>, lrck: PeripheralRef<'d, AnyPin>,
sdin: PeripheralRef<'d, AnyPin>, sdin: PeripheralRef<'d, AnyPin>,
sdout: PeripheralRef<'d, AnyPin>, sdout: PeripheralRef<'d, AnyPin>,
_config: Config, config: Config,
) -> Self { ) -> Self {
into_ref!( into_ref!(i2s, /* irq, */ mck, sck, lrck, sdin, sdout);
i2s, // irq,
mck, sck, lrck, sdin, sdout
);
let r = T::regs(); let r = T::regs();
// TODO get configuration rather than hardcoding ratio, swidth, align, format, channels Self::apply_config(&r.config, &config);
r.config.mcken.write(|w| w.mcken().enabled());
r.config.mckfreq.write(|w| w.mckfreq()._32mdiv16());
r.config.ratio.write(|w| w.ratio()._192x());
r.config.mode.write(|w| w.mode().master());
r.config.swidth.write(|w| w.swidth()._16bit());
r.config.align.write(|w| w.align().left());
r.config.format.write(|w| w.format().i2s());
r.config.channels.write(|w| w.channels().stereo());
r.psel.mck.write(|w| { r.psel.mck.write(|w| {
unsafe { w.bits(mck.psel_bits()) }; unsafe { w.bits(mck.psel_bits()) };
@ -325,6 +312,62 @@ impl<'d, T: Instance> I2s<'d, T> {
pub async fn tx(&mut self, ptr: *const u8, len: usize) -> Result<(), Error> { pub async fn tx(&mut self, ptr: *const u8, len: usize) -> Result<(), Error> {
self.output.tx(ptr, len).await self.output.tx(ptr, len).await
} }
fn apply_config(c: &CONFIG, config: &Config) {
// TODO support slave too
c.mcken.write(|w| w.mcken().enabled());
c.mckfreq.write(|w| w.mckfreq()._32mdiv16());
c.mode.write(|w| w.mode().master());
c.ratio.write(|w| {
let ratio = w.ratio();
match config.ratio {
Ratio::_32x => ratio._32x(),
Ratio::_48x => ratio._48x(),
Ratio::_64x => ratio._64x(),
Ratio::_96x => ratio._96x(),
Ratio::_128x => ratio._128x(),
Ratio::_192x => ratio._192x(),
Ratio::_256x => ratio._256x(),
Ratio::_384x => ratio._384x(),
Ratio::_512x => ratio._512x(),
}
});
c.swidth.write(|w| {
let swidth = w.swidth();
match config.swidth {
SampleWidth::_8bit => swidth._8bit(),
SampleWidth::_16bit => swidth._16bit(),
SampleWidth::_24bit => swidth._24bit(),
}
});
c.align.write(|w| {
let align = w.align();
match config.align {
Align::Left => align.left(),
Align::Right => align.right(),
}
});
c.format.write(|w| {
let format = w.format();
match config.format {
Format::I2S => format.i2s(),
Format::Aligned => format.aligned(),
}
});
c.channels.write(|w| {
let channels = w.channels();
match config.channels {
Channels::Stereo => channels.stereo(),
Channels::Left => channels.left(),
Channels::Right => channels.right(),
}
});
}
} }
impl<'d, T: Instance> I2sOutput<'d, T> { impl<'d, T: Instance> I2sOutput<'d, T> {

View File

@ -17,7 +17,7 @@ async fn main(_spawner: Spawner) {
let p = embassy_nrf::init(Default::default()); let p = embassy_nrf::init(Default::default());
let config = i2s::Config::default(); let config = i2s::Config::default();
let mut i2s = i2s::I2s::new(p.I2S, p.P0_28, p.P0_29, p.P0_31, p.P0_11, p.P0_30, config); let mut i2s = i2s::I2S::new(p.I2S, p.P0_28, p.P0_29, p.P0_31, p.P0_11, p.P0_30, config);
let mut signal_buf: Aligned<[i16; 32]> = Aligned([0i16; 32]); let mut signal_buf: Aligned<[i16; 32]> = Aligned([0i16; 32]);
let len = signal_buf.0.len() / 2; let len = signal_buf.0.len() / 2;