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 Feature/sound #69

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/raw/teleport.raw
Binary file not shown.
5 changes: 3 additions & 2 deletions esp32-s3-box/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ edition = "2021"
license = "MIT"

[target.xtensa-esp32s3-none-elf.dependencies]
xtensa-atomic-emulation-trap = "0.4.0"
hal = { package = "esp32s3-hal", version = "0.13.0" , features = ["opsram-8m"] }
hal = { package = "esp32s3-hal", version = "0.13.0" , features = ["opsram-8m", "eh1"] }
esp-backtrace = { version = "0.9.0", features = [
"esp32s3",
"panic-handler",
Expand All @@ -22,9 +21,11 @@ embedded-hal = "0.2"
display-interface = "0.4"
display-interface-spi = "0.4"
icm42670 = { git = "https://github.com/jessebraham/icm42670/" }
log = "0.4.14"
mipidsi = "0.7.1"
panic-halt = "0.2"
shared-bus = { version = "0.3.0" }
spooky-core = { path = "../spooky-core" }
spooky-embedded = { path = "../spooky-embedded", default-features = false, features = [ "esp32s3", "dynamic_maze", "resolution_320x240" ] }
spi-dma-displayinterface = { path = "../spi-dma-displayinterface", features = [ "esp32s3" ] }
es8311 = { git = "https://github.com/bjoernQ/es8311-rs", default-features = false }
122 changes: 106 additions & 16 deletions esp32-s3-box/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use hal::{
dma::DmaPriority,
gdma::Gdma,
i2c,
i2s::{DataFormat, I2s, I2s0New, I2sWriteDma, MclkPin, PinsBclkWsDout, Standard},
peripherals::Peripherals,
prelude::*,
psram,
Expand All @@ -45,6 +46,12 @@ use esp_backtrace as _;
use icm42670::{Address, Icm42670};
use shared_bus::BusManagerSimple;

use es8311::{Config, Resolution, SampleFreq};

use log::info;

const TELEPORT_SAMPLE: &[u8] = include_bytes!("../../assets/raw/teleport.raw");

fn init_psram_heap() {
unsafe {
ALLOCATOR.init(psram::psram_vaddr_start() as *mut u8, psram::PSRAM_BYTES);
Expand All @@ -69,7 +76,7 @@ fn main() -> ! {
let lcd_sclk = io.pins.gpio7;
let lcd_mosi = io.pins.gpio6;
let lcd_cs = io.pins.gpio5;
let lcd_miso = io.pins.gpio2; // random unused pin
let lcd_miso = io.pins.gpio3; // random unused pin
let lcd_dc = io.pins.gpio4.into_push_pull_output();
let mut lcd_backlight = io.pins.gpio45.into_push_pull_output();
let lcd_reset = io.pins.gpio48.into_push_pull_output();
Expand All @@ -78,10 +85,10 @@ fn main() -> ! {
let i2c_scl = io.pins.gpio18;

let dma = Gdma::new(peripherals.DMA);
let dma_channel = dma.channel0;
let dma_graphics_channel = dma.channel0;

let mut descriptors = [0u32; 8 * 3];
let mut rx_descriptors = [0u32; 8 * 3];
let mut graphics_descriptors = [0u32; 8 * 3];
let mut graphics_rx_descriptors = [0u32; 8 * 3];

let spi = Spi::new(
peripherals.SPI2,
Expand All @@ -93,10 +100,10 @@ fn main() -> ! {
SpiMode::Mode0,
&clocks,
)
.with_dma(dma_channel.configure(
.with_dma(dma_graphics_channel.configure(
false,
&mut descriptors,
&mut rx_descriptors,
&mut graphics_descriptors,
&mut graphics_rx_descriptors,
DmaPriority::Priority0,
));

Expand Down Expand Up @@ -133,23 +140,106 @@ fn main() -> ! {
.draw(&mut display)
.unwrap();

// #[cfg(any(feature = "imu_controls"))]
let i2c = i2c::I2C::new(peripherals.I2C0, i2c_sda, i2c_scl, 100u32.kHz(), &clocks);

// #[cfg(any(feature = "imu_controls"))]
let bus = BusManagerSimple::new(i2c);
// #[cfg(any(feature = "imu_controls"))]
let icm = Icm42670::new(bus.acquire_i2c(), Address::Primary).unwrap();
// let bus = BusManagerSimple::new(i2c);
// let icm = Icm42670::new(bus.acquire_i2c(), Address::Primary).unwrap();

let mut rng = Rng::new(peripherals.RNG);
let mut seed_buffer = [0u8; 32];
rng.read(&mut seed_buffer).unwrap();

let accel_movement_controller = AccelMovementController::new(icm, 0.2);
let demo_movement_controller = spooky_core::demo_movement_controller::DemoMovementController::new(seed_buffer);
let movement_controller = AccelCompositeController::new(demo_movement_controller, accel_movement_controller);
// let accel_movement_controller = AccelMovementController::new(icm, 0.2);
// let demo_movement_controller = spooky_core::demo_movement_controller::DemoMovementController::new(seed_buffer);
// let movement_controller = AccelCompositeController::new(demo_movement_controller, accel_movement_controller);
let movement_controller = spooky_core::demo_movement_controller::DemoMovementController::new(seed_buffer);


info!("Initializing audio");
let mut pa_ctrl = io.pins.gpio46.into_push_pull_output();
pa_ctrl.set_high().unwrap();

let mut es8311 = es8311::Es8311::new(i2c, es8311::Address::Primary);

let cfg = Config {
sample_frequency: SampleFreq::Freq44KHz,
mclk: Some(es8311::MclkFreq::Freq2822KHz),
res_in: Resolution::Resolution16,
res_out: Resolution::Resolution16,
mclk_inverted: false,
sclk_inverted: true,
};

es8311.init(delay, &cfg).unwrap();
info!("init done");
es8311.voice_mute(false).unwrap();
es8311.set_voice_volume(180).unwrap();

let mut audio_tx_descriptors = [0u32; 20 * 3];
let mut audio_rx_descriptors = [0u32; 8 * 3];

let dma_audio_channel = dma.channel1;

let i2s = I2s::new(
peripherals.I2S0,
MclkPin::new(io.pins.gpio2),
Standard::Philips,
DataFormat::Data16Channel16,
44100u32.Hz(),
dma_audio_channel.configure(
false,
&mut audio_tx_descriptors,
&mut audio_rx_descriptors,
DmaPriority::Priority0,
),
&clocks,
);

let i2s_tx = i2s.i2s_tx.with_pins(PinsBclkWsDout::new(
io.pins.gpio17,
io.pins.gpio47,
io.pins.gpio15,
));

let data =
unsafe { core::slice::from_raw_parts(TELEPORT_SAMPLE as *const _ as *const u8, TELEPORT_SAMPLE.len()) };

println!("Entering main loop");
let buffer = dma_buffer();
let mut idx = 0;
for i in 0..usize::min(data.len(), buffer.len()) {
buffer[i] = data[idx];

idx += 1;

if idx >= data.len() {
idx = 0;
}
}

let mut filler = [0u8; 10000];

let mut transfer = i2s_tx.write_dma_circular(buffer).unwrap();
let avail = transfer.available();
if avail > 0 {
let avail = usize::min(10000, avail);
for bidx in 0..avail {
filler[bidx] = data[idx];
idx += 1;

if idx >= data.len() {
idx = 0;
}
}
transfer.push(&filler[0..avail]).unwrap();
}

info!("Entering main loop");
app_loop(&mut display, seed_buffer, movement_controller);
loop {}
}


fn dma_buffer() -> &'static mut [u8; 32000] {
static mut BUFFER: [u8; 32000] = [0u8; 32000];
unsafe { &mut BUFFER }
}