diff --git a/Cargo.toml b/Cargo.toml index d8fd588..963bad7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] edition = "2021" name = "subtone" -version = "0.3.0" +version = "0.4.0" license = "MIT" resolver = "2" @@ -15,19 +15,20 @@ panic-probe = { version = "0.3", features = ["print-defmt"] } embassy-embedded-hal = { version ="0.1.0", features = ["defmt"] } -embassy-executor = { version = "0.4.0", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] } +embassy-executor = { version = "0.5.0", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] } embassy-futures = { version = "0.1.1", features = ["defmt"] } embassy-sync = { version = "0.5.0", features = ["defmt"] } -embassy-time = { version = "0.2.0", features = ["defmt", "defmt-timestamp-uptime"] } +embassy-time = { version = "0.3.0", features = ["defmt", "defmt-timestamp-uptime"] } embassy-rp = { version = "0.1.0", features = ["defmt", "unstable-pac", "time-driver", "critical-section-impl"] } ssd1306 = "0.8.0" fixed = "1.23.1" pio-proc = "0.2" pio = "0.2.1" -static_cell = "1.3.0" +static_cell = "2.0.0" +portable-atomic = { version = "1.6.0", features = ["critical-section"] } embedded-graphics = "0.8.1" -display-interface = "0.4.1" +display-interface = "0.5.0" rp2040-boot2 = "0.3.0" @@ -37,12 +38,12 @@ tinybmp = "0.5.0" pdm = {version = "0.1.0", git = "https://github.com/zarath/embedded-libs" } [patch.crates-io] -embassy-embedded-hal = { git = "https://github.com/embassy-rs/embassy", rev = "ca2e3759ad31f511c239d30132a28714b0e402db" } -embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "ca2e3759ad31f511c239d30132a28714b0e402db" } -embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "ca2e3759ad31f511c239d30132a28714b0e402db" } -embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "ca2e3759ad31f511c239d30132a28714b0e402db" } -embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "ca2e3759ad31f511c239d30132a28714b0e402db" } -embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "ca2e3759ad31f511c239d30132a28714b0e402db" } +embassy-embedded-hal = { git = "https://github.com/embassy-rs/embassy", rev = "f3a0bcb15eb7f7d1b2f9d948f8d487b76a4308fe" } +embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "f3a0bcb15eb7f7d1b2f9d948f8d487b76a4308fe" } +embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "f3a0bcb15eb7f7d1b2f9d948f8d487b76a4308fe" } +embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "f3a0bcb15eb7f7d1b2f9d948f8d487b76a4308fe" } +embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "f3a0bcb15eb7f7d1b2f9d948f8d487b76a4308fe" } +embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "f3a0bcb15eb7f7d1b2f9d948f8d487b76a4308fe" } # cargo build/run [profile.dev] diff --git a/OpenSCAD/case.scad b/OpenSCAD/case.scad index 6dbd645..5b68790 100644 --- a/OpenSCAD/case.scad +++ b/OpenSCAD/case.scad @@ -213,8 +213,19 @@ module knob() cylinder(h = 12.0, d1 = 2.4, d2 = 2.0); } } + for (w = [45.0:90.0:315.0]) + { + rotate([0.0, 0.0, w]) + translate([4.0, 0.0, 3.5]) + cube([2.0, 0.6, 7], center=true); + } + for (w = [45.0:90.0:135.0]) + { + rotate([0.0, 0.0, w]) + translate([3.8, 0.0, 3.5]) + cube([2.6, 0.6, 7], center=true); + } } // baseplate(); // case (); knob(); -// pipe(h=20, do=20, di=16); diff --git a/OpenSCAD/case.stl b/OpenSCAD/case.stl deleted file mode 100644 index 9666632..0000000 Binary files a/OpenSCAD/case.stl and /dev/null differ diff --git a/rust-toolchain.toml b/rust-toolchain.toml index a886a97..df5628f 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,7 +1,7 @@ # Before upgrading check that everything is available on all tier1 targets here: # https://rust-lang.github.io/rustup-components-history [toolchain] -channel = "nightly-2023-12-19" +channel = "nightly-2024-03-26" components = [ "rust-src", "rustfmt" ] targets = [ "thumbv6m-none-eabi", diff --git a/src/encoder.rs b/src/encoder.rs index 83d233c..4c2fe19 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -1,8 +1,7 @@ use defmt::info; use embassy_futures::join::join; use embassy_futures::select::{select, Either}; -use embassy_rp::gpio::{Flex, Pin, Pull}; -use embassy_rp::Peripheral; +use embassy_rp::gpio::Input; use embassy_time::{Duration, Timer}; pub enum EncoderDirection { @@ -12,20 +11,14 @@ pub enum EncoderDirection { pub static DELAY_DEFAULT: Duration = Duration::from_millis(5); -pub struct Encoder<'d, T: Pin, V: Pin> { - pin_a: Flex<'d, T>, - pin_b: Flex<'d, V>, +pub struct Encoder<'d> { + pin_a: Input<'d>, + pin_b: Input<'d>, } -impl<'d, T: Pin, V: Pin> Encoder<'d, T, V> { +impl<'d> Encoder<'d> { #[inline] - pub fn new(pin_a: impl Peripheral

+ 'd, pin_b: impl Peripheral

+ 'd) -> Self { - let mut pin_a = Flex::new(pin_a); - let mut pin_b = Flex::new(pin_b); - pin_a.set_as_input(); - pin_a.set_pull(Pull::Up); - pin_b.set_as_input(); - pin_b.set_pull(Pull::Up); + pub fn new(pin_a: Input<'static>, pin_b: Input<'static>) -> Self { Self { pin_a, pin_b } } diff --git a/src/main.rs b/src/main.rs index aecc58f..154cee1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,9 @@ #![no_std] #![no_main] #![feature(type_alias_impl_trait)] - +#[allow(static_mut_refs)] use core::mem::size_of; +use core::ptr::addr_of_mut; use defmt::info; use embassy_executor::Executor; @@ -13,8 +14,8 @@ use embassy_rp::{ gpio::{Input, Pull}, i2c::{Config as I2cConfig, I2c}, multicore::{spawn_core1, Stack}, - peripherals::{DMA_CH0, DMA_CH1, FLASH, I2C1, PIN_2, PIN_26, PIN_3, PIN_4, PIO0}, - pio::{Config as PioConfig, FifoJoin, InterruptHandler, Pio, ShiftDirection}, + peripherals::{DMA_CH0, DMA_CH1, FLASH, I2C1, PIN_26, PIO0}, + pio::{Config as PioConfig, FifoJoin, InterruptHandler, Pio, ShiftDirection, StateMachine}, rom_data::{float_funcs::fdiv, reset_to_usb_boot}, Peripheral, }; @@ -88,8 +89,8 @@ static CHANNEL: Channel = Channel::new(); mod encoder; -type Encoder = encoder::Encoder<'static, PIN_2, PIN_4>; -type Button = Input<'static, PIN_3>; +type Encoder = encoder::Encoder<'static>; +type Button = Input<'static>; type I2cChan1 = embassy_rp::i2c::I2c<'static, I2C1, embassy_rp::i2c::Blocking>; type Display = Ssd1306< @@ -118,11 +119,11 @@ fn write_config(flash: &mut Flash<'_, FLASH, Async, FLASH_SIZE>, cfg: Config) { if read_config(flash) == cfg { return; } - // info!("Flashing!"); - defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET, ADDR_OFFSET + ERASE_SIZE as u32)); let buf: &[u8] = unsafe { core::slice::from_raw_parts(&cfg as *const Config as *const u8, size_of::()) }; + info!("Flashing!"); + defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET, ADDR_OFFSET + ERASE_SIZE as u32)); flash.blocking_write(ADDR_OFFSET, buf).unwrap(); } @@ -173,7 +174,7 @@ fn main() -> ! { let p = embassy_rp::init(Default::default()); // info!("Init Input"); - let enc = encoder::Encoder::new(p.PIN_2, p.PIN_4); + let enc = encoder::Encoder::new(Input::new(p.PIN_2, Pull::Up), Input::new(p.PIN_4, Pull::Up)); let button = Input::new(p.PIN_3, Pull::Up); // info!("Set up I2c"); @@ -183,14 +184,18 @@ fn main() -> ! { i2c_config.frequency = 400_000; let i2c = I2c::new_blocking(p.I2C1, scl, sda, i2c_config); - spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || { - let executor1 = EXECUTOR1.init(Executor::new()); - executor1.run(|spawner| { - spawner - .spawn(core1_task(p.PIN_26, p.PIO0, p.DMA_CH0)) - .unwrap() - }); - }); + spawn_core1( + p.CORE1, + unsafe { &mut *addr_of_mut!(CORE1_STACK) }, + move || { + let executor1 = EXECUTOR1.init(Executor::new()); + executor1.run(|spawner| { + spawner + .spawn(core1_task(p.PIN_26, p.PIO0, p.DMA_CH0)) + .unwrap() + }); + }, + ); let executor0 = EXECUTOR0.init(Executor::new()); executor0.run(|spawner| { @@ -309,20 +314,35 @@ async fn core1_task(pdm_pin: PIN_26, pio_0: PIO0, dma_0: DMA_CH0) { let mut dma_out_ref = dma_0.into_ref(); loop { - match select( - CHANNEL.receive(), - sm.tx().dma_push(dma_out_ref.reborrow(), &PDM_TABLE.0), - ) - .await - { - Either::First(Message { freq, enabled }) => { - info!("Got Message: {} {}", freq, enabled); - sm.set_enable(false); - cfg.clock_divider = freq_2_divider(freq); - sm.set_config(&cfg); - sm.set_enable(enabled); + if enabled { + match select( + CHANNEL.receive(), + sm.tx().dma_push(dma_out_ref.reborrow(), &PDM_TABLE.0), + ) + .await + { + Either::First(Message { freq, enabled }) => { + set_divider(&mut sm, &mut cfg, freq, enabled); + } + Either::Second(_) => (), } - Either::Second(_) => (), + } else { + let Message { freq, enabled } = CHANNEL.receive().await; + set_divider(&mut sm, &mut cfg, freq, enabled); } } } + +#[inline] +fn set_divider<'d>( + sm: &mut StateMachine<'d, PIO0, 0>, + cfg: &mut PioConfig<'d, PIO0>, + freq: f32, + enabled: bool, +) { + info!("Set freq: {}", freq); + sm.set_enable(false); + cfg.clock_divider = freq_2_divider(freq); + sm.set_config(&cfg); + sm.set_enable(enabled); +}