I2S with knurling-rs

First of all, I'm quite a re-begginer at hardware (studied EE a looooong time ago, going back to it now). I have a nrf52840 DK, an Adafruit MAX98357 I2S Class-D Mono Amp and a speaker and I just wanted to get some WAV out of it, through I2S.I've been stuck with it for 2 weeks (going back at it 1h here and there).

I tested the amp and speaker separately with a rpi, everything works fine, so maybe there's something obvious i'm doing wrong.

Here is the main (a lot of other things are bundled with this, but i don't think it's relevant).The code is inspired by the nrf-hal i2s example but using the new rust embedded from the knurlings-rs.

#![no_main]
#![no_std]

use bell as _; // global logger + panicking-behavior + memory layout

// access to board peripherals:
use nrf52840_hal::{
    self as hal,
    gpio::{p0::Parts as P0Parts, Level},
    i2s::*,
};

#[repr(align(4))]
struct Aligned<T: ?Sized>(T);

#[cortex_m_rt::entry]
fn main() -> ! {
    let board = hal::pac::Peripherals::take().unwrap();

    let pins_0 = P0Parts::new(board.P0);

    // din
    let din = pins_0.p0_01.into_floating_input().degrade();
    // sck
    let bclk = pins_0.p0_02.into_push_pull_output(Level::Low).degrade();
    // lrck
    let lrc = pins_0.p0_03.into_push_pull_output(Level::Low).degrade();

    let mut i2s = I2S::new_controller(board.I2S, None, &bclk, &lrc, Some(&din), None);
    i2s.start();

    defmt::info!("Starting audio");

    static mut SIGNAL_BUF: Aligned<[i16; 32]> = Aligned([0i16; 32]);

    let mut x = 0;
    while x < 100 {
        unsafe {
            let len = SIGNAL_BUF.0.len() / 2;
            for x in 0..len {
                SIGNAL_BUF.0[2 * x] = triangle_wave(x as i32, len, 2048, 0, 1) as i16;
                SIGNAL_BUF.0[2 * x + 1] = triangle_wave(x as i32, len, 2048, 0, 1) as i16;
            }
            // defmt::info!("s={:?}", &SIGNAL_BUF);
            let r = i2s.tx(&SIGNAL_BUF.0).unwrap();
            let (_b, moved_i2s) = r.wait();
            i2s = moved_i2s;
        }
        x += 1;
    }

    defmt::info!("we should have heard smtg");

    bell::exit()
}

const fn triangle_wave(x: i32, length: usize, amplitude: i32, phase: i32, periods: i32) -> i32 {
    let length = length as i32;
    amplitude
        - ((2 * periods * (x + phase + length / (4 * periods)) * amplitude / length)
            % (2 * amplitude)
            - amplitude)
            .abs()
        - amplitude / 2
}

It compiles, it doesn't crash, just no sound.Any ideas welcome!

PS: I posted this on reddit/r/learnrust first and was nicely routed to here, which indeed seems more specific :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.