From 3fd35d723aa9ac35487bf16cae5bebfda4279ef1 Mon Sep 17 00:00:00 2001 From: geens Date: Wed, 16 Jul 2025 16:02:59 +0200 Subject: [PATCH] ADC scaling and fixes --- collect.sh | 0 firmware/Cargo.lock | 365 +++++++++++++++++++++++++++++++++++++++++++ firmware/src/adc.rs | 83 ++++++---- xtask/src/collect.rs | 1 + 4 files changed, 415 insertions(+), 34 deletions(-) mode change 100644 => 100755 collect.sh create mode 100644 firmware/Cargo.lock diff --git a/collect.sh b/collect.sh old mode 100644 new mode 100755 diff --git a/firmware/Cargo.lock b/firmware/Cargo.lock new file mode 100644 index 0000000..658602b --- /dev/null +++ b/firmware/Cargo.lock @@ -0,0 +1,365 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bare-metal" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3" +dependencies = [ + "rustc_version", +] + +[[package]] +name = "bare-metal" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fe8f5a8a398345e52358e18ff07cc17a568fbca5c6f73873d3a62056309603" + +[[package]] +name = "bitfield" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bxcan" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3110c36f496cf7110ab17c48ad714225330862101ec30197a9898006cd3e2862" +dependencies = [ + "bitflags", + "embedded-can", + "nb 1.1.0", + "vcell", +] + +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + +[[package]] +name = "cortex-m" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ec610d8f49840a5b376c69663b6369e71f4b34484b9b2eb29fb918d92516cb9" +dependencies = [ + "bare-metal 0.2.5", + "bitfield", + "critical-section", + "embedded-hal 0.2.7", + "volatile-register", +] + +[[package]] +name = "cortex-m-rt" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "801d4dec46b34c299ccf6b036717ae0fce602faa4f4fe816d9013b9a7c9f5ba6" +dependencies = [ + "cortex-m-rt-macros", +] + +[[package]] +name = "cortex-m-rt-macros" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e37549a379a9e0e6e576fd208ee60394ccb8be963889eebba3ffe0980364f472" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "critical-section" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" + +[[package]] +name = "defmt" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "548d977b6da32fa1d1fda2876453da1e7df63ad0304c8b3dae4dbe7b96f39b78" +dependencies = [ + "bitflags", + "defmt-macros", +] + +[[package]] +name = "defmt-macros" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d4fc12a85bcf441cfe44344c4b72d58493178ce635338a3f3b78943aceb258e" +dependencies = [ + "defmt-parser", + "proc-macro-error2", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "defmt-parser" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10d60334b3b2e7c9d91ef8150abfb6fa4c1c39ebbcf4a81c2e346aad939fee3e" +dependencies = [ + "thiserror", +] + +[[package]] +name = "defmt-rtt" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2cac3b8a5644a9e02b75085ebad3b6deafdbdbdec04bb25086523828aa4dfd1" +dependencies = [ + "critical-section", + "defmt", +] + +[[package]] +name = "embedded-can" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9d2e857f87ac832df68fa498d18ddc679175cf3d2e4aa893988e5601baf9438" +dependencies = [ + "nb 1.1.0", +] + +[[package]] +name = "embedded-hal" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35949884794ad573cf46071e41c9b60efb0cb311e3ca01f7af807af1debc66ff" +dependencies = [ + "nb 0.1.3", + "void", +] + +[[package]] +name = "embedded-hal" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "361a90feb7004eca4019fb28352a9465666b24f840f5c3cddf0ff13920590b89" + +[[package]] +name = "embedded-midi" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26119337828c4c6a29d9c1661962eeec2a3f884b859d5713a70c6c4143988497" +dependencies = [ + "embedded-hal 0.2.7", + "midi-types", + "nb 1.1.0", +] + +[[package]] +name = "embedded-storage" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21dea9854beb860f3062d10228ce9b976da520a73474aed3171ec276bc0c032" + +[[package]] +name = "midi-types" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef0bbe5256e5c434947d790788426bb65773502784aed7b23408f7e7fb4d8eb5" + +[[package]] +name = "nb" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "801d31da0513b6ec5214e9bf433a77966320625a37860f910be265be6e18d06f" +dependencies = [ + "nb 1.1.0", +] + +[[package]] +name = "nb" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d" + +[[package]] +name = "panic-halt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812" + +[[package]] +name = "proc-macro-error-attr2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5" +dependencies = [ + "proc-macro2", + "quote", +] + +[[package]] +name = "proc-macro-error2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802" +dependencies = [ + "proc-macro-error-attr2", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver", +] + +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + +[[package]] +name = "stm32f0" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dce86e6516082d73a7b7f7d9d2ffd635b6aa95920203facedcadc339530c871" +dependencies = [ + "bare-metal 1.0.0", + "cortex-m", + "cortex-m-rt", + "vcell", +] + +[[package]] +name = "stm32f042c4-blinky" +version = "0.1.0" +dependencies = [ + "cortex-m", + "cortex-m-rt", + "critical-section", + "defmt", + "defmt-rtt", + "embedded-hal 0.2.7", + "embedded-midi", + "midi-types", + "nb 1.1.0", + "panic-halt", + "stm32f0xx-hal", +] + +[[package]] +name = "stm32f0xx-hal" +version = "0.18.0" +dependencies = [ + "bare-metal 1.0.0", + "bxcan", + "cast", + "cortex-m", + "embedded-hal 0.2.7", + "embedded-hal 1.0.0", + "embedded-storage", + "nb 1.1.0", + "stm32f0", + "void", +] + +[[package]] +name = "syn" +version = "2.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "vcell" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77439c1b53d2303b20d9459b1ade71a83c716e3f9c34f3228c00e6f185d6c002" + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + +[[package]] +name = "volatile-register" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de437e2a6208b014ab52972a27e59b33fa2920d3e00fe05026167a1c509d19cc" +dependencies = [ + "vcell", +] diff --git a/firmware/src/adc.rs b/firmware/src/adc.rs index bc5d6e7..c1c7a02 100644 --- a/firmware/src/adc.rs +++ b/firmware/src/adc.rs @@ -5,10 +5,10 @@ use hal::gpio::{Output, PushPull, Alternate, AF0, AF5}; use embedded_hal::blocking::spi::Transfer; use defmt::*; -pub const PEDAL_A_MIN: u8 = 10; -pub const PEDAL_A_MAX: u8 = 245; -pub const PEDAL_B_MIN: u8 = 10; -pub const PEDAL_B_MAX: u8 = 245; +pub const PEDAL_A_MIN: u8 = 71; +pub const PEDAL_A_MAX: u8 = 248; +pub const PEDAL_B_MIN: u8 = 83; +pub const PEDAL_B_MAX: u8 = 250; const IIR_ALPHA: u16 = 51; @@ -38,7 +38,8 @@ impl Tlc0832 { let sck = sck.into_alternate_af5(critical_section); let miso = miso.into_alternate_af0(critical_section); let mosi = mosi.into_alternate_af0(critical_section); - let cs = cs.into_push_pull_output(critical_section); + let mut cs = cs.into_push_pull_output(critical_section); + cs.set_high().ok(); let spi_mode = Mode { polarity: Polarity::IdleLow, @@ -53,21 +54,14 @@ impl Tlc0832 { rcc, ); - let adc = Self { + Self { spi, cs, pedal_a_filtered: 128 << 8, pedal_b_filtered: 128 << 8, last_midi_a: 64, last_midi_b: 64, - }; - - // Small delay to ensure SPI peripheral is ready - for _ in 0..1000 { - cortex_m::asm::nop(); } - - adc }) } @@ -75,34 +69,35 @@ impl Tlc0832 { if channel > 1 { return Err("Invalid channel"); } - - self.cs.set_high().ok(); - // Small delay before starting transaction - for _ in 0..100 { - cortex_m::asm::nop(); - } self.cs.set_low().ok(); + // Small delay after CS low for _ in 0..100 { cortex_m::asm::nop(); } - let start_bit = 1u8 << 7; - let channel_bit = if channel == 0 { 0x00 } else { 1u8 << 6 }; - let command = start_bit | channel_bit; - - info!("Reading channel {} with command 0x{:02x}", channel, command); + // TLC0832 protocol: + // Send 3 bits: Start(1) + SGL/DIF(1 for single-ended) + ODD/EVEN(channel) + // Then 1 clock cycle of MUX settling (DO goes low) + // Then 8 clock cycles of MSB-first data - // Use HAL SPI transfer - much simpler and more reliable - let mut data = [command, 0x00, 0x00]; // TLC0832 needs 3 bytes - let result = match self.spi.transfer(&mut data) { - Ok(response) => { - info!("SPI transfer successful: [{:02x}, {:02x}, {:02x}]", - response[0], response[1], response[2]); - // TLC0832 returns data in the 3rd byte for 8-bit mode - response[2] - }, + // For single-ended mode: + // Channel 0: 110 (Start=1, SGL/DIF=1, ODD/EVEN=0) + // Channel 1: 111 (Start=1, SGL/DIF=1, ODD/EVEN=1) + + let command_bits = 0x06 | channel; // 110 or 111 in the top 3 bits + let command_byte = command_bits << 5; // Shift to MSB position: 11000000 or 11100000 + + info!("Reading channel {} with command bits {:03b} (0x{:02x})", channel, command_bits, command_byte); + + // Send command byte + 2 dummy bytes to clock out the full response + // The TLC0832 needs 11 total clock cycles: + // 3 for command + 1 for MUX settling + 8 for data = 12 bits total + let mut data = [command_byte, 0x00]; + + let response = match self.spi.transfer(&mut data) { + Ok(resp) => resp, Err(_) => { self.cs.set_high().ok(); error!("SPI transfer error"); @@ -110,12 +105,26 @@ impl Tlc0832 { } }; + info!("SPI response: [{:02x}, {:02x}]", response[0], response[1]); + + // The result spans across the response bytes + // After the 3 command bits + 1 MUX settling bit (4 bits total), + // the next 8 bits are the conversion result + // So we need to extract bits from both response bytes + + // response[0] contains the first 8 bits (3 command + 1 settling + 4 data MSBs) + // response[1] contains the remaining 4 data LSBs + + let result = ((response[0] & 0x0F) << 4) | ((response[1] & 0xF0) >> 4); + // Small delay before CS high for _ in 0..100 { cortex_m::asm::nop(); } + self.cs.set_high().ok(); - info!("Read channel {} = {}", channel, result); + + info!("Channel {} result: {}", channel, result); Ok(result) } @@ -154,13 +163,16 @@ impl Tlc0832 { let mut midi_a_change = None; let mut midi_b_change = None; + // Read channel 0 (pedal A) match self.read_channel(0) { Ok(raw_a) => { + info!("Raw channel 0 value: {}", raw_a); self.apply_iir_filter(0, raw_a); let filtered_a = (self.pedal_a_filtered >> 8) as u8; let midi_a = self.adc_to_midi(filtered_a, PEDAL_A_MIN, PEDAL_A_MAX); if midi_a != self.last_midi_a { + info!("Channel 0 MIDI change: {} -> {}", self.last_midi_a, midi_a); self.last_midi_a = midi_a; midi_a_change = Some(midi_a); } @@ -170,13 +182,16 @@ impl Tlc0832 { } } + // Read channel 1 (pedal B) match self.read_channel(1) { Ok(raw_b) => { + info!("Raw channel 1 value: {}", raw_b); self.apply_iir_filter(1, raw_b); let filtered_b = (self.pedal_b_filtered >> 8) as u8; let midi_b = self.adc_to_midi(filtered_b, PEDAL_B_MIN, PEDAL_B_MAX); if midi_b != self.last_midi_b { + info!("Channel 1 MIDI change: {} -> {}", self.last_midi_b, midi_b); self.last_midi_b = midi_b; midi_b_change = Some(midi_b); } diff --git a/xtask/src/collect.rs b/xtask/src/collect.rs index d00fff1..d884ba3 100644 --- a/xtask/src/collect.rs +++ b/xtask/src/collect.rs @@ -2,6 +2,7 @@ use tokio::process::Command; pub async fn collect() { collect_dir_output("audio_engine", "../collect_audio_engine.txt").await; + //collect_dir_output("firmware", "../collect_firmware.txt").await; collect_dir_output("gui", "../collect_gui.txt").await; collect_dir_output("osc", "../collect_osc.txt").await; collect_dir_output("simulator", "../collect_simulator.txt").await;