From b32fa12da846ce7ac8ce8fd6e01aed8ce08a81a7 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Mon, 11 Aug 2025 17:52:25 +0800 Subject: [PATCH 01/11] working square wave and pinup with nto bgas --- baremetal/src/repl.rs | 235 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index bfe7e3ce0..618316cf7 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -460,6 +460,154 @@ impl Repl { } } } + #[cfg(feature = "nto-bio")] + "capsense" => { + { + if args.len() != 2 { + crate::println!("Usage: capsense "); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + + let pin = match u32::from_str_radix(&args[0], 10) { + Ok(p) if p < 32 => p, + _ => { + crate::println!("Invalid pin number. Must be 0-31."); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + }; + + let state = args[1].as_str(); + if state != "on" && state != "off" { + crate::println!("Invalid state. Use 'on' or 'off'."); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + + let mut bio_ss = BioSharedState::new(); + let mut ctrl = bio_ss.bio.r(utra::bio_bdma::SFR_CTRL); + let iox = cramium_hal::iox::Iox::new(utra::iox::HW_IOX_BASE as *mut u32); + // Set the ports as bio ports + iox.set_ports_from_pio_bitmask(0xFFFF_FFFF); + + if state == "on" { + // Stop Core 0 to ensure a clean state before loading new code. + ctrl &= !0b0001; + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + + // Clear the FIFO for Core 0 to remove any stale data. + bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0b0001); + + // Load the slow wave generator program. + let prog = slow_wave_generator_code(); + bio_ss.load_code(prog, 0, BioCore::Core0); + + // Start Core 0. + ctrl |= 0b0001_0001_0001; + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + + // Set clock divider for a slow 1kHz BIO clock. + // Divisor = 40,000,000 / 1,000 = 40,000 (0x9C40) + // bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x9C40_0000); + + // Set clock divider for a 25kHz BIO clock. + // bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x6400000); + + // Set clock divider for a 32250kHz BIO clock. + bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x5000000); + + // Send parameters to the BIO core via FIFO0. + let pin_mask = 1 << pin; + // For a 2-second half-period with a 1ms (1kHz) clock, we need 2000 counts. + let delay_count = 2000; + bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, pin_mask); + bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, delay_count); + + crate::println!("Generating ~0.25 Hz (4 second period) wave on pin {}.", pin); + } else { + // Stop Core 0. + ctrl &= !0b0001; + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + crate::println!("Stopped wave generator on Core 0."); + } + } + } + #[cfg(feature = "nto-bio")] + "pinup" => { + { + if args.len() != 2 { + crate::println!("Usage: pinup "); + + self.do_cmd = false; + + self.cmdline.clear(); + + return; + } + + let pin = match u32::from_str_radix(&args[0], 10) { + Ok(p) if p < 32 => p, + + _ => { + crate::println!("Invalid pin number. Must be 0-31."); + self.do_cmd = false; + self.cmdline.clear(); + + return; + } + }; + + let state = args[1].as_str(); + if state != "on" && state != "off" { + crate::println!("Invalid state. Use 'on' or 'off'."); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + + let mut bio_ss = BioSharedState::new(); + let iox = cramium_hal::iox::Iox::new(utra::iox::HW_IOX_BASE as *mut u32); + + iox.set_ports_from_pio_bitmask(0xFFFF_FFFF); + crate::println!("Configuring BIO Core 0 for GPIO control..."); + + // 1. Stop Core 0 to ensure a clean state + let mut ctrl = bio_ss.bio.r(utra::bio_bdma::SFR_CTRL); + ctrl &= !0b0001; + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + + // 2. Clear any stale data from the FIFO + bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0b0001); + + // 3. Load the correct program + let prog = pin_control_code(); + bio_ss.load_code(prog, 0, BioCore::Core0); + + // 5. Start the core + ctrl |= 0b0001_0001_0001; //enable all the cores + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + crate::println!("Core 0 is ready."); + + // 4. Reset the clock divider to a default (e.g., full speed) A value of 0 might mean full + // speed, or a small value like 1. This prevents capsense's slow clock from affecting + // pinup. + bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0); + + // Prepare and send the command to the now-ready core + let pin_mask = 1 << pin; + let state_val = if state == "on" { 0xFFFFFFFF } else { 0 }; // Use all 1s for "on" + + bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, pin_mask); + bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, state_val); + + crate::println!("Command sent: Pin {} -> {}.", pin, state.to_uppercase()); + } + } + "echo" => { for word in args { crate::print!("{} ", word); @@ -475,6 +623,8 @@ impl Repl { crate::print!(", mon"); #[cfg(feature = "nto-bio")] crate::print!(", bio"); + #[cfg(feature = "nto-bio")] + crate::print!(", pinup, capsense"); crate::println!(""); } } @@ -491,6 +641,91 @@ impl Repl { } } +#[rustfmt::skip] +bio_code!(slow_wave_generator_code, SLOW_WAVE_START, SLOW_WAVE_END, + // Configure all GPIOs as outputs. + "li t0, 0xFFFFFFFF", + "mv x24, t0", + // Read the pin mask from FIFO0 into t1. + "mv t1, x16", + // Read the delay count from FIFO0 into t2. + "mv t2, x16", + // Set the GPIO mask register to the pin mask. + "mv x26, t1", + "10:", // Main loop + // --- HIGH PULSE --- + "mv x21, t1", // Set pin high + "mv t3, t2", // Load counter into t3 for the delay loop + "11:", // Delay loop 1 + "mv x20, zero", // << Wait for one (slow) BIO clock cycle + "addi t3, t3, -1", // Decrement counter + "bne t3, zero, 11b", // Loop if not zero + // --- LOW PULSE --- + "mv x21, zero", // Set pin low + "mv t3, t2", // Re-load counter for the delay loop + "12:", // Delay loop 2 + "mv x20, zero", // << Wait for one (slow) BIO clock cycle + "addi t3, t3, -1", // Decrement counter + "bne t3, zero, 12b", // Loop if not zero + "j 10b" // Repeat the whole cycle +); +#[rustfmt::skip] +bio_code!(pin_control_code, PIN_CONTROL_START, PIN_CONTROL_END, + // Configure all GPIOs as outputs once at the start. + "li t0, 0xFFFFFFFF", + "mv x24, t0", + "wait_for_cmd:", + // Read the pin mask from FIFO0 (x16). The core will stall here until the CPU sends data. + "mv t1, x16", + // Read the desired state (1 for high, 0 for low) from FIFO0. Stalls again. + "mv t2, x16", + // Set the GPIO mask register (x26) to the pin mask we just received. + "mv x26, t1", + // Set the GPIO output register (x21) to the state (high/low) we received. + "mv x21, t2", + // Loop back to wait for the next command. + "j wait_for_cmd" +); + +#[rustfmt::skip] +bio_code!(pin_read_write_code, PIN_RW_START, PIN_RW_END, + // This program waits for three words from the CPU: + // 1. Write Pin Mask (the pin to set as output) + // 2. State Value (0 for LOW, 0xFFFFFFFF for HIGH) + // 3. Read Pin Mask (the pin to read from) + // It then performs the write, reads the input pin, + // and sends the result (0 or non-zero) back to the CPU. + "wait_for_cmd:", + // --- Receive parameters from the CPU --- + "mv t1, x16", // t1 = Write Pin Mask + "mv t2, x16", // t2 = State Value + "mv t3, x16", // t3 = Read Pin Mask + + // --- Configure Pin Directions --- + // Set the write pin as an OUTPUT (bit = 1) and all others as INPUT (bit = 0). + "mv x24, t1", + + // --- Perform the Write Operation --- + // Set the GPIO mask register to the write pin. + "mv x26, t1", + // Set the output register to the desired state. + "mv x21, t2", + + // --- Perform the Read Operation --- + // Read the state of all input pins into t4. + "mv t4, x25", + // Isolate the bit for the specific pin we want to read. + "and t5, t4, t3", // t5 = (value of all pins) & (read pin mask) + + // --- Send Result Back to CPU --- + // Write the result (t5) to the transmit FIFO (x17). + // The CPU will read this value. + "mv x17, t5", + + // Loop back to wait for the next command. + "j wait_for_cmd" +); + #[rustfmt::skip] bio_code!(simple_test_code, SIMPLE_TEST_START, SIMPLE_TEST_END, "li sp, 0x800", From e954ca60e242cbeeabfe24e576e06d26c1734b92 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Mon, 11 Aug 2025 17:53:47 +0800 Subject: [PATCH 02/11] without other changes --- baremetal/src/repl.rs | 40 ---------------------------------------- 1 file changed, 40 deletions(-) diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index 618316cf7..fb9a4ecd9 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -686,46 +686,6 @@ bio_code!(pin_control_code, PIN_CONTROL_START, PIN_CONTROL_END, // Loop back to wait for the next command. "j wait_for_cmd" ); - -#[rustfmt::skip] -bio_code!(pin_read_write_code, PIN_RW_START, PIN_RW_END, - // This program waits for three words from the CPU: - // 1. Write Pin Mask (the pin to set as output) - // 2. State Value (0 for LOW, 0xFFFFFFFF for HIGH) - // 3. Read Pin Mask (the pin to read from) - // It then performs the write, reads the input pin, - // and sends the result (0 or non-zero) back to the CPU. - "wait_for_cmd:", - // --- Receive parameters from the CPU --- - "mv t1, x16", // t1 = Write Pin Mask - "mv t2, x16", // t2 = State Value - "mv t3, x16", // t3 = Read Pin Mask - - // --- Configure Pin Directions --- - // Set the write pin as an OUTPUT (bit = 1) and all others as INPUT (bit = 0). - "mv x24, t1", - - // --- Perform the Write Operation --- - // Set the GPIO mask register to the write pin. - "mv x26, t1", - // Set the output register to the desired state. - "mv x21, t2", - - // --- Perform the Read Operation --- - // Read the state of all input pins into t4. - "mv t4, x25", - // Isolate the bit for the specific pin we want to read. - "and t5, t4, t3", // t5 = (value of all pins) & (read pin mask) - - // --- Send Result Back to CPU --- - // Write the result (t5) to the transmit FIFO (x17). - // The CPU will read this value. - "mv x17, t5", - - // Loop back to wait for the next command. - "j wait_for_cmd" -); - #[rustfmt::skip] bio_code!(simple_test_code, SIMPLE_TEST_START, SIMPLE_TEST_END, "li sp, 0x800", From db7690210abab2de1850260921feec276a51aa1c Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Tue, 12 Aug 2025 17:06:42 +0800 Subject: [PATCH 03/11] pin up in library --- baremetal/src/repl.rs | 205 ++++++++++++++++++++++------------ libs/xous-bio-bdma/src/lib.rs | 50 +++++++++ 2 files changed, 183 insertions(+), 72 deletions(-) diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index fb9a4ecd9..4b3d5504f 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -536,75 +536,120 @@ impl Repl { } } } + #[cfg(feature = "nto-bio")] - "pinup" => { - { - if args.len() != 2 { - crate::println!("Usage: pinup "); + "pin" => { + if args.is_empty() { + crate::println!("Usage: pin ..."); + self.do_cmd = false; + self.cmdline.clear(); + return; + } - self.do_cmd = false; - - self.cmdline.clear(); - - return; - } - - let pin = match u32::from_str_radix(&args[0], 10) { - Ok(p) if p < 32 => p, - - _ => { - crate::println!("Invalid pin number. Must be 0-31."); + match args[0].as_str() { + "read" => { + if args.len() != 2 { + crate::println!("Usage: pin read "); self.do_cmd = false; self.cmdline.clear(); - return; } - }; - let state = args[1].as_str(); - if state != "on" && state != "off" { - crate::println!("Invalid state. Use 'on' or 'off'."); - self.do_cmd = false; - self.cmdline.clear(); - return; + let pin = match u32::from_str_radix(&args[1], 10) { + Ok(p) if p < 32 => p, + _ => { + crate::println!("Invalid pin number. Must be 0-31."); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + }; + + let mut bio_ss = BioSharedState::new(); + let iox = cramium_hal::iox::Iox::new(utra::iox::HW_IOX_BASE as *mut u32); + iox.set_ports_from_pio_bitmask(0xFFFF_FFFF); + + // Stop Core 0 to ensure a clean state + let mut ctrl = bio_ss.bio.r(utra::bio_bdma::SFR_CTRL); + ctrl &= !0b0001; + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + + // Clear any stale data from the FIFO + bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0b0001); + + // Load the pin reading program + let prog = pin_read_code(); + bio_ss.load_code(prog, 0, BioCore::Core0); + + // Start the core + ctrl |= 0b0001_0001_0001; + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + + // Send the pin mask to the core + let pin_mask = 1 << pin; + bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, pin_mask); + + // Wait for the result to appear in the output FIFO. + // A timeout could be added here to prevent hanging. + while bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL0) == 0 {} + + // Read the result + let result = bio_ss.bio.r(utra::bio_bdma::SFR_RXF0); + let state_str = if result == 0 { "low" } else { "high" }; + crate::println!("Pin {} is {}.", pin, state_str); + + // Stop the core after use to prevent interference + ctrl &= !0b0001; + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); } + "up" => { + // --- THIS IS THE REFACTORED BLOCK --- + if args.len() != 3 { + crate::println!("Usage: pin up "); + self.do_cmd = false; + self.cmdline.clear(); + return; + } - let mut bio_ss = BioSharedState::new(); - let iox = cramium_hal::iox::Iox::new(utra::iox::HW_IOX_BASE as *mut u32); + let pin = match u32::from_str_radix(&args[1], 10) { + Ok(p) if p < 32 => p, + _ => { + crate::println!("Invalid pin number. Must be 0-31."); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + }; - iox.set_ports_from_pio_bitmask(0xFFFF_FFFF); - crate::println!("Configuring BIO Core 0 for GPIO control..."); + let state_str = args[2].as_str(); + if state_str != "on" && state_str != "off" { + crate::println!("Invalid state. Use 'on' or 'off'."); + self.do_cmd = false; + self.cmdline.clear(); + return; + } - // 1. Stop Core 0 to ensure a clean state - let mut ctrl = bio_ss.bio.r(utra::bio_bdma::SFR_CTRL); - ctrl &= !0b0001; - bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + // 1. Initialize BIO and IOX + let mut bio_ss = BioSharedState::new(); + let iox = cramium_hal::iox::Iox::new(utra::iox::HW_IOX_BASE as *mut u32); + iox.set_ports_from_pio_bitmask(0xFFFF_FFFF); - // 2. Clear any stale data from the FIFO - bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0b0001); + // 2. Convert string state to boolean + let state_bool = state_str == "on"; - // 3. Load the correct program - let prog = pin_control_code(); - bio_ss.load_code(prog, 0, BioCore::Core0); - - // 5. Start the core - ctrl |= 0b0001_0001_0001; //enable all the cores - bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); - crate::println!("Core 0 is ready."); - - // 4. Reset the clock divider to a default (e.g., full speed) A value of 0 might mean full - // speed, or a small value like 1. This prevents capsense's slow clock from affecting - // pinup. - bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0); - - // Prepare and send the command to the now-ready core - let pin_mask = 1 << pin; - let state_val = if state == "on" { 0xFFFFFFFF } else { 0 }; // Use all 1s for "on" - - bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, pin_mask); - bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, state_val); - - crate::println!("Command sent: Pin {} -> {}.", pin, state.to_uppercase()); + // 3. Call the library function + crate::println!( + "Setting pin {} to {} which is {} using the library function...", + pin, + state_str.to_uppercase(), + state_bool + ); + bio_ss.set_pin(pin, state_bool); // Use `None` to default to Core 0 + crate::println!("Command sent."); + } + _ => { + crate::println!("Unknown pin command: '{}'. Use 'up' or 'read'.", args[0]); + } } } @@ -622,9 +667,7 @@ impl Repl { #[cfg(not(feature = "cramium-soc"))] crate::print!(", mon"); #[cfg(feature = "nto-bio")] - crate::print!(", bio"); - #[cfg(feature = "nto-bio")] - crate::print!(", pinup, capsense"); + crate::print!(", bio, pin, capsense"); crate::println!(""); } } @@ -669,23 +712,41 @@ bio_code!(slow_wave_generator_code, SLOW_WAVE_START, SLOW_WAVE_END, "bne t3, zero, 12b", // Loop if not zero "j 10b" // Repeat the whole cycle ); +// #[rustfmt::skip] +// bio_code!(pin_control_code, PIN_CONTROL_START, PIN_CONTROL_END, +// // Configure all GPIOs as outputs once at the start. +// "li t0, 0xFFFFFFFF", +// "mv x24, t0", +// "wait_for_cmd:", +// // Read the pin mask from FIFO0 (x16). The core will stall here until the CPU sends data. +// "mv t1, x16", +// // Read the desired state (1 for high, 0 for low) from FIFO0. Stalls again. +// "mv t2, x16", +// // Set the GPIO mask register (x26) to the pin mask we just received. +// "mv x26, t1", +// // Set the GPIO output register (x21) to the state (high/low) we received. +// "mv x21, t2", +// // Loop back to wait for the next command. +// "j wait_for_cmd" +// ); #[rustfmt::skip] -bio_code!(pin_control_code, PIN_CONTROL_START, PIN_CONTROL_END, - // Configure all GPIOs as outputs once at the start. - "li t0, 0xFFFFFFFF", - "mv x24, t0", +bio_code!(pin_read_code, PIN_READ_START, PIN_READ_END, + // Configure all pins as inputs by clearing the direction register. + "li t0, 1", + "mv x25, t0", "wait_for_cmd:", - // Read the pin mask from FIFO0 (x16). The core will stall here until the CPU sends data. - "mv t1, x16", - // Read the desired state (1 for high, 0 for low) from FIFO0. Stalls again. - "mv t2, x16", - // Set the GPIO mask register (x26) to the pin mask we just received. - "mv x26, t1", - // Set the GPIO output register (x21) to the state (high/low) we received. - "mv x21, t2", - // Loop back to wait for the next command. + // Wait for the CPU to send a pin mask via FIFO0. + "mv t1, x16", // t1 = pin_mask + // Read the current state of all GPIO input pins. + "mv t2, x21", // t2 = all pin states + // Isolate the state of the requested pin. + "and t3, t2, t1", // t3 = 0 if low, pin_mask if high + // Send the result back to the CPU via FIFO0. + "mv x17, t3", + // Loop to wait for the next command. "j wait_for_cmd" ); + #[rustfmt::skip] bio_code!(simple_test_code, SIMPLE_TEST_START, SIMPLE_TEST_END, "li sp, 0x800", diff --git a/libs/xous-bio-bdma/src/lib.rs b/libs/xous-bio-bdma/src/lib.rs index 4bbc9479c..a32d134ec 100644 --- a/libs/xous-bio-bdma/src/lib.rs +++ b/libs/xous-bio-bdma/src/lib.rs @@ -325,6 +325,35 @@ impl BioSharedState { Ok(()) } + // pub fn set_pin(&mut self, pin: u32, state: bool, core: Option) { + pub fn set_pin(&mut self, pin: u32, state: bool) { + // 1. Stop Core 0 to ensure a clean state. + let mut ctrl = self.bio.r(utra::bio_bdma::SFR_CTRL); + ctrl &= !0b0001; + self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + + // 2. Clear any stale data from the FIFO for Core 0. + self.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0b0001); + + // 3. Load the correct program onto Core 0. + let prog = pin_control_code(); + self.load_code(prog, 0, BioCore::Core0); + + // 4. Start Core 0. + ctrl |= 0b0001_0001_0001; + self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + + // 5. Reset the clock divider for Core 0. + self.bio.wo(utra::bio_bdma::SFR_QDIV0, 0); + + // 6. Prepare and send the command to Core 0's FIFO. Use the 'state' boolean directly in the if + // condition. + let state_val = if state { 0xFFFFFFFF } else { 0 }; + + self.bio.wo(utra::bio_bdma::SFR_TXF0, 1 << pin); + self.bio.wo(utra::bio_bdma::SFR_TXF0, state_val); + } + pub fn debug_pc(&self) { crate::println!( "c0:{:04x} c1:{:04x} c2:{:04x} c3:{:04x}", @@ -398,3 +427,24 @@ bio_code!(mem_init_code, MEM_INIT_START, MEM_INIT_END, "10:", "j 10b" ); + +// #[rustfmt::skip] +bio_code!( + pin_control_code, + PIN_CONTROL_START, + PIN_CONTROL_END, + // Configure all GPIOs as outputs once at the start. + "li t0, 0xFFFFFFFF", + "mv x24, t0", + "wait_for_cmds:", + // Read the pin mask from FIFO0 (x16). The core will stall here until the CPU sends data. + "mv t1, x16", + // Read the desired state (1 for high, 0 for low) from FIFO0. Stalls again. + "mv t2, x16", + // Set the GPIO mask register (x26) to the pin mask we just received. + "mv x26, t1", + // Set the GPIO output register (x21) to the state (high/low) we received. + "mv x21, t2", + // Loop back to wait for the next command. + "j wait_for_cmds" +); From a5e95c9c6d6e5291c1467dce74f306e00d432ca2 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Tue, 12 Aug 2025 19:39:56 +0800 Subject: [PATCH 04/11] pin up works for core 0 via lib --- baremetal/src/repl.rs | 36 +++++++++++++++++----- libs/xous-bio-bdma/src/lib.rs | 57 ++++++++++++++++++++++++++--------- 2 files changed, 70 insertions(+), 23 deletions(-) diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index 4b3d5504f..985797f80 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -1,3 +1,4 @@ +use alloc::format; use alloc::string::{String, ToString}; use alloc::vec::Vec; @@ -604,8 +605,8 @@ impl Repl { } "up" => { // --- THIS IS THE REFACTORED BLOCK --- - if args.len() != 3 { - crate::println!("Usage: pin up "); + if args.len() < 3 || args.len() > 4 { + crate::println!("Usage: pin up [core_id: 0-3]"); self.do_cmd = false; self.cmdline.clear(); return; @@ -629,22 +630,41 @@ impl Repl { return; } - // 1. Initialize BIO and IOX + // 1. Parse the optional core ID argument. + let target_core: Option = if args.len() == 4 { + match u32::from_str_radix(&args[3], 10) { + Ok(0) => Some(BioCore::Core0), + Ok(1) => Some(BioCore::Core1), + Ok(2) => Some(BioCore::Core2), + Ok(3) => Some(BioCore::Core3), + _ => { + crate::println!("Invalid core ID. Must be 0, 1, 2, or 3."); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + } + } else { + None // Default to Core 0 if no core is specified. + }; + + // 2. Initialize BIO and IOX let mut bio_ss = BioSharedState::new(); let iox = cramium_hal::iox::Iox::new(utra::iox::HW_IOX_BASE as *mut u32); iox.set_ports_from_pio_bitmask(0xFFFF_FFFF); - // 2. Convert string state to boolean + // 3. Convert string state to boolean let state_bool = state_str == "on"; - // 3. Call the library function + // 4. Call the library function with the parsed core. + let core_name = format!("{:?}", target_core.unwrap_or(BioCore::Core0)); crate::println!( - "Setting pin {} to {} which is {} using the library function...", + "Setting pin {} to {} using {}...", pin, state_str.to_uppercase(), - state_bool + core_name ); - bio_ss.set_pin(pin, state_bool); // Use `None` to default to Core 0 + bio_ss.set_pin(pin, state_bool, target_core); crate::println!("Command sent."); } _ => { diff --git a/libs/xous-bio-bdma/src/lib.rs b/libs/xous-bio-bdma/src/lib.rs index a32d134ec..1bcbd30d1 100644 --- a/libs/xous-bio-bdma/src/lib.rs +++ b/libs/xous-bio-bdma/src/lib.rs @@ -326,32 +326,59 @@ impl BioSharedState { } // pub fn set_pin(&mut self, pin: u32, state: bool, core: Option) { - pub fn set_pin(&mut self, pin: u32, state: bool) { - // 1. Stop Core 0 to ensure a clean state. + pub fn set_pin(&mut self, pin: u32, state: bool, core: Option) { + let target_core = core.unwrap_or(BioCore::Core0); // Default to Core 0 if None + let core_mask = 1 << (target_core as usize); + + // 1. Stop the target core. let mut ctrl = self.bio.r(utra::bio_bdma::SFR_CTRL); - ctrl &= !0b0001; + ctrl &= !core_mask; // Clear only the EN bit for the target core self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); - // 2. Clear any stale data from the FIFO for Core 0. - self.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0b0001); + // 2. Clear the target core's FIFO using the correct field and a bitmask. This is the corrected block. + self.bio.wfo(utra::bio_bdma::SFR_FIFO_CLR_SFR_FIFO_CLR, core_mask as u32); - // 3. Load the correct program onto Core 0. + // 3. Load the program onto the target core. let prog = pin_control_code(); - self.load_code(prog, 0, BioCore::Core0); + self.load_code(prog, 0, target_core); - // 4. Start Core 0. - ctrl |= 0b0001_0001_0001; + // 4. Start the target core using read-modify-write for safety. + let start_mask = self.bio.ms(utra::bio_bdma::SFR_CTRL_EN, core_mask) + | self.bio.ms(utra::bio_bdma::SFR_CTRL_RESTART, core_mask) + | self.bio.ms(utra::bio_bdma::SFR_CTRL_CLKDIV_RESTART, core_mask); + ctrl |= start_mask; self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); - // 5. Reset the clock divider for Core 0. - self.bio.wo(utra::bio_bdma::SFR_QDIV0, 0); + // 5. Reset the clock divider for the selected core. + match target_core { + BioCore::Core0 => self.bio.wo(utra::bio_bdma::SFR_QDIV0, 0), + BioCore::Core1 => self.bio.wo(utra::bio_bdma::SFR_QDIV1, 0), + BioCore::Core2 => self.bio.wo(utra::bio_bdma::SFR_QDIV2, 0), + BioCore::Core3 => self.bio.wo(utra::bio_bdma::SFR_QDIV3, 0), + } - // 6. Prepare and send the command to Core 0's FIFO. Use the 'state' boolean directly in the if - // condition. + // 6. Prepare and send the command to the target core's FIFO. let state_val = if state { 0xFFFFFFFF } else { 0 }; + let pin_bitmask = 1 << pin; - self.bio.wo(utra::bio_bdma::SFR_TXF0, 1 << pin); - self.bio.wo(utra::bio_bdma::SFR_TXF0, state_val); + match target_core { + BioCore::Core0 => { + self.bio.wo(utra::bio_bdma::SFR_TXF0, pin_bitmask); + self.bio.wo(utra::bio_bdma::SFR_TXF0, state_val); + } + BioCore::Core1 => { + self.bio.wo(utra::bio_bdma::SFR_TXF1, pin_bitmask); + self.bio.wo(utra::bio_bdma::SFR_TXF1, state_val); + } + BioCore::Core2 => { + self.bio.wo(utra::bio_bdma::SFR_TXF2, pin_bitmask); + self.bio.wo(utra::bio_bdma::SFR_TXF2, state_val); + } + BioCore::Core3 => { + self.bio.wo(utra::bio_bdma::SFR_TXF3, pin_bitmask); + self.bio.wo(utra::bio_bdma::SFR_TXF3, state_val); + } + } } pub fn debug_pc(&self) { From ba997be92f126a196a173476f9a015b7105afb10 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Tue, 12 Aug 2025 20:35:02 +0800 Subject: [PATCH 05/11] working modular gpio with all bio cores --- libs/xous-bio-bdma/src/lib.rs | 135 ++++++++++++++++++++++++++-------- 1 file changed, 106 insertions(+), 29 deletions(-) diff --git a/libs/xous-bio-bdma/src/lib.rs b/libs/xous-bio-bdma/src/lib.rs index 1bcbd30d1..47ccc7fcc 100644 --- a/libs/xous-bio-bdma/src/lib.rs +++ b/libs/xous-bio-bdma/src/lib.rs @@ -325,29 +325,57 @@ impl BioSharedState { Ok(()) } - // pub fn set_pin(&mut self, pin: u32, state: bool, core: Option) { pub fn set_pin(&mut self, pin: u32, state: bool, core: Option) { - let target_core = core.unwrap_or(BioCore::Core0); // Default to Core 0 if None + let target_core = core.unwrap_or(BioCore::Core0); let core_mask = 1 << (target_core as usize); - // 1. Stop the target core. - let mut ctrl = self.bio.r(utra::bio_bdma::SFR_CTRL); - ctrl &= !core_mask; // Clear only the EN bit for the target core - self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + // --- Debug --- + crate::println!("\n--- Running set_pin for {:?} ---", target_core); + let initial_ctrl_state = self.bio.r(utra::bio_bdma::SFR_CTRL); + crate::println!("[0] Initial SFR_CTRL value: {:#010x}", initial_ctrl_state); + // --- End Debug --- - // 2. Clear the target core's FIFO using the correct field and a bitmask. This is the corrected block. + // 1. Stop the target core, leaving other cores running. + let ctrl_with_target_stopped = initial_ctrl_state & !core_mask; + self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl_with_target_stopped); + + // --- Debug --- + crate::println!("[1] Stopping core with EN mask: {:#010x}", core_mask); + crate::println!(" SFR_CTRL after stop: {:#010x}", ctrl_with_target_stopped); + // --- End Debug --- + + // 2. Clear the target core's FIFO. self.bio.wfo(utra::bio_bdma::SFR_FIFO_CLR_SFR_FIFO_CLR, core_mask as u32); - // 3. Load the program onto the target core. - let prog = pin_control_code(); + // --- Debug --- + crate::println!("[2] Cleared FIFO for core with mask: {:#010x}", core_mask); + // --- End Debug --- + + // 3. Load the correct, dedicated program for the target core. + let prog = match target_core { + BioCore::Core0 => pin_control_core0(), + BioCore::Core1 => pin_control_core1(), + BioCore::Core2 => pin_control_core2(), + BioCore::Core3 => pin_control_core3(), + }; self.load_code(prog, 0, target_core); - // 4. Start the target core using read-modify-write for safety. - let start_mask = self.bio.ms(utra::bio_bdma::SFR_CTRL_EN, core_mask) + // --- Debug --- + crate::println!("[3] Loaded dedicated program onto {:?}", target_core); + // --- End Debug --- + + // 4. Calculate the start mask for the target core AND reset Core 0's clocks. + let target_start_mask = self.bio.ms(utra::bio_bdma::SFR_CTRL_EN, core_mask) | self.bio.ms(utra::bio_bdma::SFR_CTRL_RESTART, core_mask) | self.bio.ms(utra::bio_bdma::SFR_CTRL_CLKDIV_RESTART, core_mask); - ctrl |= start_mask; - self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + let final_start_mask = target_start_mask | 0x111; + let final_ctrl_state = ctrl_with_target_stopped | final_start_mask; + self.bio.wo(utra::bio_bdma::SFR_CTRL, final_ctrl_state); + + // --- Debug --- + crate::println!("[4] Starting core with final_start_mask: {:#010x}", final_start_mask); + crate::println!(" SFR_CTRL after start: {:#010x}", final_ctrl_state); + // --- End Debug --- // 5. Reset the clock divider for the selected core. match target_core { @@ -361,6 +389,12 @@ impl BioSharedState { let state_val = if state { 0xFFFFFFFF } else { 0 }; let pin_bitmask = 1 << pin; + // --- Debug --- + // MODIFIED: Removed `format!` macro. + crate::println!("[5] Sending command to FIFO for core {}:", target_core as usize); + crate::println!(" pin_bitmask: {:#010x}, state_val: {:#010x}", pin_bitmask, state_val); + // --- End Debug --- + match target_core { BioCore::Core0 => { self.bio.wo(utra::bio_bdma::SFR_TXF0, pin_bitmask); @@ -455,23 +489,66 @@ bio_code!(mem_init_code, MEM_INIT_START, MEM_INIT_END, "j 10b" ); -// #[rustfmt::skip] +#[rustfmt::skip] bio_code!( - pin_control_code, - PIN_CONTROL_START, - PIN_CONTROL_END, - // Configure all GPIOs as outputs once at the start. + pin_control_core0, + PIN_CONTROL_START_0, + PIN_CONTROL_END_0, + // For Core 0: Reads from fifo[0] (x16) "li t0, 0xFFFFFFFF", "mv x24, t0", - "wait_for_cmds:", - // Read the pin mask from FIFO0 (x16). The core will stall here until the CPU sends data. - "mv t1, x16", - // Read the desired state (1 for high, 0 for low) from FIFO0. Stalls again. - "mv t2, x16", - // Set the GPIO mask register (x26) to the pin mask we just received. - "mv x26, t1", - // Set the GPIO output register (x21) to the state (high/low) we received. - "mv x21, t2", - // Loop back to wait for the next command. - "j wait_for_cmds" +"80:", + "mv t1, x16", // Read pin_mask + "mv t2, x16", // Read state_val + "mv x26, t1", // Set write-mask to the target pin + "mv x21, t2", // Write state, masked by x26 + "j 80b" +); + +#[rustfmt::skip] +bio_code!( + pin_control_core1, + PIN_CONTROL_START_1, + PIN_CONTROL_END_1, + // For Core 1: Reads from fifo[1] (x17) + "li t0, 0xFFFFFFFF", + "mv x24, t0", +"82:", + "mv t1, x17", // Read pin_mask + "mv t2, x17", // Read state_val + "mv x26, t1", // Set write-mask to the target pin + "mv x21, t2", // Write state, masked by x26 + "j 82b" +); + +#[rustfmt::skip] +bio_code!( + pin_control_core2, + PIN_CONTROL_START_2, + PIN_CONTROL_END_2, + // For Core 2: Reads from fifo[2] (x18) + "li t0, 0xFFFFFFFF", + "mv x24, t0", +"84:", + "mv t1, x18", // Read pin_mask + "mv t2, x18", // Read state_val + "mv x26, t1", // Set write-mask to the target pin + "mv x21, t2", // Write state, masked by x26 + "j 84b" +); + +#[rustfmt::skip] +bio_code!( + pin_control_core3, + PIN_CONTROL_START_3, + PIN_CONTROL_END_3, + // For Core 3: Reads from fifo[3] (x19) + "li t0, 0xFFFFFFFF", + "mv x24, t0", +"86:", + "mv t1, x19", // Read pin_mask + "mv t2, x19", // Read state_val + "mv x26, t1", // Set write-mask to the target pin + "mv x21, t2", // Write state, masked by x26 + "j 86b" ); From a6643a33149e386e3ffd77b3261867e9e453109a Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Tue, 12 Aug 2025 21:19:29 +0800 Subject: [PATCH 06/11] moving square wave generator over - it does not play nice with other things right now --- baremetal/src/repl.rs | 88 ++-------------------------- libs/xous-bio-bdma/src/lib.rs | 107 ++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 82 deletions(-) diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index 985797f80..0e5077c63 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -490,49 +490,19 @@ impl Repl { } let mut bio_ss = BioSharedState::new(); - let mut ctrl = bio_ss.bio.r(utra::bio_bdma::SFR_CTRL); let iox = cramium_hal::iox::Iox::new(utra::iox::HW_IOX_BASE as *mut u32); - // Set the ports as bio ports iox.set_ports_from_pio_bitmask(0xFFFF_FFFF); if state == "on" { - // Stop Core 0 to ensure a clean state before loading new code. - ctrl &= !0b0001; - bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); - - // Clear the FIFO for Core 0 to remove any stale data. - bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0b0001); - - // Load the slow wave generator program. - let prog = slow_wave_generator_code(); - bio_ss.load_code(prog, 0, BioCore::Core0); - - // Start Core 0. - ctrl |= 0b0001_0001_0001; - bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); - - // Set clock divider for a slow 1kHz BIO clock. - // Divisor = 40,000,000 / 1,000 = 40,000 (0x9C40) - // bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x9C40_0000); - - // Set clock divider for a 25kHz BIO clock. - // bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x6400000); - - // Set clock divider for a 32250kHz BIO clock. - bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x5000000); - - // Send parameters to the BIO core via FIFO0. - let pin_mask = 1 << pin; - // For a 2-second half-period with a 1ms (1kHz) clock, we need 2000 counts. + // Example parameters for a ~0.25 Hz wave + let clock_divisor = 0x5000000; // For ~32kHz BIO clock let delay_count = 2000; - bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, pin_mask); - bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, delay_count); - crate::println!("Generating ~0.25 Hz (4 second period) wave on pin {}.", pin); + bio_ss.start_wave_generator(pin, BioCore::Core0, clock_divisor, delay_count); + + crate::println!("Generating wave on pin {}.", pin); } else { - // Stop Core 0. - ctrl &= !0b0001; - bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + bio_ss.stop_wave_generator(BioCore::Core0); crate::println!("Stopped wave generator on Core 0."); } } @@ -604,7 +574,6 @@ impl Repl { bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); } "up" => { - // --- THIS IS THE REFACTORED BLOCK --- if args.len() < 3 || args.len() > 4 { crate::println!("Usage: pin up [core_id: 0-3]"); self.do_cmd = false; @@ -704,51 +673,6 @@ impl Repl { } } -#[rustfmt::skip] -bio_code!(slow_wave_generator_code, SLOW_WAVE_START, SLOW_WAVE_END, - // Configure all GPIOs as outputs. - "li t0, 0xFFFFFFFF", - "mv x24, t0", - // Read the pin mask from FIFO0 into t1. - "mv t1, x16", - // Read the delay count from FIFO0 into t2. - "mv t2, x16", - // Set the GPIO mask register to the pin mask. - "mv x26, t1", - "10:", // Main loop - // --- HIGH PULSE --- - "mv x21, t1", // Set pin high - "mv t3, t2", // Load counter into t3 for the delay loop - "11:", // Delay loop 1 - "mv x20, zero", // << Wait for one (slow) BIO clock cycle - "addi t3, t3, -1", // Decrement counter - "bne t3, zero, 11b", // Loop if not zero - // --- LOW PULSE --- - "mv x21, zero", // Set pin low - "mv t3, t2", // Re-load counter for the delay loop - "12:", // Delay loop 2 - "mv x20, zero", // << Wait for one (slow) BIO clock cycle - "addi t3, t3, -1", // Decrement counter - "bne t3, zero, 12b", // Loop if not zero - "j 10b" // Repeat the whole cycle -); -// #[rustfmt::skip] -// bio_code!(pin_control_code, PIN_CONTROL_START, PIN_CONTROL_END, -// // Configure all GPIOs as outputs once at the start. -// "li t0, 0xFFFFFFFF", -// "mv x24, t0", -// "wait_for_cmd:", -// // Read the pin mask from FIFO0 (x16). The core will stall here until the CPU sends data. -// "mv t1, x16", -// // Read the desired state (1 for high, 0 for low) from FIFO0. Stalls again. -// "mv t2, x16", -// // Set the GPIO mask register (x26) to the pin mask we just received. -// "mv x26, t1", -// // Set the GPIO output register (x21) to the state (high/low) we received. -// "mv x21, t2", -// // Loop back to wait for the next command. -// "j wait_for_cmd" -// ); #[rustfmt::skip] bio_code!(pin_read_code, PIN_READ_START, PIN_READ_END, // Configure all pins as inputs by clearing the direction register. diff --git a/libs/xous-bio-bdma/src/lib.rs b/libs/xous-bio-bdma/src/lib.rs index 47ccc7fcc..d5d5f3a93 100644 --- a/libs/xous-bio-bdma/src/lib.rs +++ b/libs/xous-bio-bdma/src/lib.rs @@ -415,6 +415,84 @@ impl BioSharedState { } } + // Add these two new functions to your `impl BioSharedState` block. + + /// Starts a continuous square wave on a given pin using a dedicated BIO core. + /// + /// This function is NOT cooperative. The core it uses will take exclusive + /// control of the GPIO data bus via the `x21` register. + /// + /// # Arguments + /// + /// * `pin` - The GPIO pin number to toggle (0-31). + /// * `core` - The `BioCore` to run the generator program on. + /// * `clock_divisor` - The value for the `QDIV` register to set the frequency. + /// * `delay_count` - The number of BIO clock cycles for each half-period (high/low). + pub fn start_wave_generator(&mut self, pin: u32, core: BioCore, clock_divisor: u32, delay_count: u32) { + let core_mask = 1 << (core as usize); + + // 1. Stop the target core to ensure a clean state. + let mut ctrl = self.bio.r(utra::bio_bdma::SFR_CTRL); + ctrl &= !core_mask; + self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + + // 2. Clear any stale data from the core's FIFO. + self.bio.wfo(utra::bio_bdma::SFR_FIFO_CLR_SFR_FIFO_CLR, core_mask as u32); + + // 3. Load the slow wave generator program. + let prog = slow_wave_generator_code(); + self.load_code(prog, 0, core); + + // 4. Start the core. + // Note: This uses a simple start and does not include the master clock reset + // from the cooperative pin_set function, as this function is exclusive. + let start_mask = core_mask | (core_mask << 4) | (core_mask << 8); + ctrl |= start_mask; + self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + + // 5. Set the clock divider for the selected core. + match core { + BioCore::Core0 => self.bio.wo(utra::bio_bdma::SFR_QDIV0, clock_divisor), + BioCore::Core1 => self.bio.wo(utra::bio_bdma::SFR_QDIV1, clock_divisor), + BioCore::Core2 => self.bio.wo(utra::bio_bdma::SFR_QDIV2, clock_divisor), + BioCore::Core3 => self.bio.wo(utra::bio_bdma::SFR_QDIV3, clock_divisor), + } + + // 6. Send parameters to the BIO core via its FIFO. + let pin_mask = 1 << pin; + match core { + BioCore::Core0 => { + self.bio.wo(utra::bio_bdma::SFR_TXF0, pin_mask); + self.bio.wo(utra::bio_bdma::SFR_TXF0, delay_count); + } + BioCore::Core1 => { + self.bio.wo(utra::bio_bdma::SFR_TXF1, pin_mask); + self.bio.wo(utra::bio_bdma::SFR_TXF1, delay_count); + } + BioCore::Core2 => { + self.bio.wo(utra::bio_bdma::SFR_TXF2, pin_mask); + self.bio.wo(utra::bio_bdma::SFR_TXF2, delay_count); + } + BioCore::Core3 => { + self.bio.wo(utra::bio_bdma::SFR_TXF3, pin_mask); + self.bio.wo(utra::bio_bdma::SFR_TXF3, delay_count); + } + } + } + + /// Stops a BIO core that is running a program (like the wave generator). + /// + /// # Arguments + /// + /// * `core` - The `BioCore` to halt. + pub fn stop_wave_generator(&mut self, core: BioCore) { + let core_mask = 1 << (core as usize); + let mut ctrl = self.bio.r(utra::bio_bdma::SFR_CTRL); + // Clear only the enable bit for the target core. + ctrl &= !core_mask; + self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + } + pub fn debug_pc(&self) { crate::println!( "c0:{:04x} c1:{:04x} c2:{:04x} c3:{:04x}", @@ -552,3 +630,32 @@ bio_code!( "mv x21, t2", // Write state, masked by x26 "j 86b" ); + +#[rustfmt::skip] +bio_code!(slow_wave_generator_code, SLOW_WAVE_START, SLOW_WAVE_END, + // Configure all GPIOs as outputs. + "li t0, 0xFFFFFFFF", + "mv x24, t0", + // Read the pin mask from FIFO0 into t1. + "mv t1, x16", + // Read the delay count from FIFO0 into t2. + "mv t2, x16", + // Set the GPIO mask register to the pin mask. + "mv x26, t1", + "10:", // Main loop + // --- HIGH PULSE --- + "mv x21, t1", // Set pin high + "mv t3, t2", // Load counter into t3 for the delay loop + "11:", // Delay loop 1 + "mv x20, zero", // << Wait for one (slow) BIO clock cycle + "addi t3, t3, -1", // Decrement counter + "bne t3, zero, 11b", // Loop if not zero + // --- LOW PULSE --- + "mv x21, zero", // Set pin low + "mv t3, t2", // Re-load counter for the delay loop + "12:", // Delay loop 2 + "mv x20, zero", // << Wait for one (slow) BIO clock cycle + "addi t3, t3, -1", // Decrement counter + "bne t3, zero, 12b", // Loop if not zero + "j 10b" // Repeat the whole cycle +); From f9cbf14d1989aaf21d4f846a8bc6efbeca5f13f3 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Wed, 13 Aug 2025 14:52:50 +0800 Subject: [PATCH 07/11] now the pins can toggle without impacting capsense since I removed reset --- libs/xous-bio-bdma/src/lib.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/libs/xous-bio-bdma/src/lib.rs b/libs/xous-bio-bdma/src/lib.rs index d5d5f3a93..4d6d9754c 100644 --- a/libs/xous-bio-bdma/src/lib.rs +++ b/libs/xous-bio-bdma/src/lib.rs @@ -365,16 +365,26 @@ impl BioSharedState { // --- End Debug --- // 4. Calculate the start mask for the target core AND reset Core 0's clocks. - let target_start_mask = self.bio.ms(utra::bio_bdma::SFR_CTRL_EN, core_mask) - | self.bio.ms(utra::bio_bdma::SFR_CTRL_RESTART, core_mask) - | self.bio.ms(utra::bio_bdma::SFR_CTRL_CLKDIV_RESTART, core_mask); - let final_start_mask = target_start_mask | 0x111; + let target_start_mask = self.bio.ms(utra::bio_bdma::SFR_CTRL_EN, core_mask); + // | self.bio.ms(utra::bio_bdma::SFR_CTRL_RESTART, core_mask) + self.bio.ms(utra::bio_bdma::SFR_CTRL_CLKDIV_RESTART, core_mask); + let final_start_mask = target_start_mask; let final_ctrl_state = ctrl_with_target_stopped | final_start_mask; + + // --- Debug Lines Start --- + // crate::println!("--- Mask Calculation ---"); + // crate::println!(" Target Core Mask (raw): {:012b}", core_mask); + // crate::println!(" Target Start Mask: {:012b}", target_start_mask); + // crate::println!(" Final Start Mask (+C0): {:012b}", final_start_mask); + // crate::println!(" Previous CTRL State: {:012b}", ctrl_with_target_stopped); + // crate::println!(" Final CTRL Value to Write:{:012b}", final_ctrl_state); + // // --- Debug Lines End --- + self.bio.wo(utra::bio_bdma::SFR_CTRL, final_ctrl_state); // --- Debug --- - crate::println!("[4] Starting core with final_start_mask: {:#010x}", final_start_mask); - crate::println!(" SFR_CTRL after start: {:#010x}", final_ctrl_state); + crate::println!("[4] Starting core with final_start_mask: {:012b}", final_start_mask); + crate::println!(" SFR_CTRL after start: {:012b}", final_ctrl_state); // --- End Debug --- // 5. Reset the clock divider for the selected core. From 7af77a1d10eea13de39a061ef873ac7766d2962c Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Wed, 13 Aug 2025 15:34:39 +0800 Subject: [PATCH 08/11] can now set multiple clocks on multiple fifos --- baremetal/src/repl.rs | 74 ++++++++++------- libs/xous-bio-bdma/src/lib.rs | 147 ++++++++++++++++++++-------------- 2 files changed, 129 insertions(+), 92 deletions(-) diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index 0e5077c63..1918fba7c 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -462,49 +462,61 @@ impl Repl { } } #[cfg(feature = "nto-bio")] - "capsense" => { - { - if args.len() != 2 { - crate::println!("Usage: capsense "); + "pwm" => { + // Check for the correct number of arguments: pin, state, and core. + if args.len() != 3 { + crate::println!("Usage: pwm "); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + + // Parse the pin number (args[0]). + let pin = match u32::from_str_radix(&args[0], 10) { + Ok(p) if p < 32 => p, + _ => { + crate::println!("Invalid pin number. Must be 0-31."); self.do_cmd = false; self.cmdline.clear(); return; } + }; - let pin = match u32::from_str_radix(&args[0], 10) { - Ok(p) if p < 32 => p, - _ => { - crate::println!("Invalid pin number. Must be 0-31."); - self.do_cmd = false; - self.cmdline.clear(); - return; - } - }; + // Parse the state (args[1]). + let state = args[1].as_str(); + if state != "on" && state != "off" { + crate::println!("Invalid state. Use 'on' or 'off'."); + self.do_cmd = false; + self.cmdline.clear(); + return; + } - let state = args[1].as_str(); - if state != "on" && state != "off" { - crate::println!("Invalid state. Use 'on' or 'off'."); + // Parse the core number (args[2]). + let core_num = match u32::from_str_radix(&args[2], 10) { + Ok(c) if c < 4 => c, // Valid cores are 0, 1, 2, 3 + _ => { + crate::println!("Invalid core number. Must be 0-3."); self.do_cmd = false; self.cmdline.clear(); return; } + }; + let target_core = BioCore::from(core_num as usize); - let mut bio_ss = BioSharedState::new(); - let iox = cramium_hal::iox::Iox::new(utra::iox::HW_IOX_BASE as *mut u32); - iox.set_ports_from_pio_bitmask(0xFFFF_FFFF); + let mut bio_ss = BioSharedState::new(); + let iox = cramium_hal::iox::Iox::new(utra::iox::HW_IOX_BASE as *mut u32); + iox.set_ports_from_pio_bitmask(0xFFFF_FFFF); - if state == "on" { - // Example parameters for a ~0.25 Hz wave - let clock_divisor = 0x5000000; // For ~32kHz BIO clock - let delay_count = 2000; + if state == "on" { + // Example parameters for a ~0.25 Hz wave + let clock_divisor = 0x5000000; // For ~32kHz BIO clock + let delay_count = 2000; - bio_ss.start_wave_generator(pin, BioCore::Core0, clock_divisor, delay_count); - - crate::println!("Generating wave on pin {}.", pin); - } else { - bio_ss.stop_wave_generator(BioCore::Core0); - crate::println!("Stopped wave generator on Core 0."); - } + bio_ss.start_wave_generator(pin, target_core, clock_divisor, delay_count); + crate::println!("Generating wave on pin {} using {:?}.", pin, target_core); + } else { + bio_ss.stop_wave_generator(target_core); + crate::println!("Stopped wave generator on {:?}.", target_core); } } @@ -656,7 +668,7 @@ impl Repl { #[cfg(not(feature = "cramium-soc"))] crate::print!(", mon"); #[cfg(feature = "nto-bio")] - crate::print!(", bio, pin, capsense"); + crate::print!(", bio, pin, pwm"); crate::println!(""); } } diff --git a/libs/xous-bio-bdma/src/lib.rs b/libs/xous-bio-bdma/src/lib.rs index 4d6d9754c..640a81eef 100644 --- a/libs/xous-bio-bdma/src/lib.rs +++ b/libs/xous-bio-bdma/src/lib.rs @@ -329,28 +329,14 @@ impl BioSharedState { let target_core = core.unwrap_or(BioCore::Core0); let core_mask = 1 << (target_core as usize); - // --- Debug --- - crate::println!("\n--- Running set_pin for {:?} ---", target_core); - let initial_ctrl_state = self.bio.r(utra::bio_bdma::SFR_CTRL); - crate::println!("[0] Initial SFR_CTRL value: {:#010x}", initial_ctrl_state); - // --- End Debug --- - // 1. Stop the target core, leaving other cores running. + let initial_ctrl_state = self.bio.r(utra::bio_bdma::SFR_CTRL); let ctrl_with_target_stopped = initial_ctrl_state & !core_mask; self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl_with_target_stopped); - // --- Debug --- - crate::println!("[1] Stopping core with EN mask: {:#010x}", core_mask); - crate::println!(" SFR_CTRL after stop: {:#010x}", ctrl_with_target_stopped); - // --- End Debug --- - // 2. Clear the target core's FIFO. self.bio.wfo(utra::bio_bdma::SFR_FIFO_CLR_SFR_FIFO_CLR, core_mask as u32); - // --- Debug --- - crate::println!("[2] Cleared FIFO for core with mask: {:#010x}", core_mask); - // --- End Debug --- - // 3. Load the correct, dedicated program for the target core. let prog = match target_core { BioCore::Core0 => pin_control_core0(), @@ -360,33 +346,16 @@ impl BioSharedState { }; self.load_code(prog, 0, target_core); - // --- Debug --- - crate::println!("[3] Loaded dedicated program onto {:?}", target_core); - // --- End Debug --- + // 4. Calculate the final start mask for the target core. + // This now correctly includes both the EN (Enable) and CLKDIV_RESTART bits. + let target_start_mask = self.bio.ms(utra::bio_bdma::SFR_CTRL_EN, core_mask) + | self.bio.ms(utra::bio_bdma::SFR_CTRL_CLKDIV_RESTART, core_mask); + let final_ctrl_state = ctrl_with_target_stopped | target_start_mask; - // 4. Calculate the start mask for the target core AND reset Core 0's clocks. - let target_start_mask = self.bio.ms(utra::bio_bdma::SFR_CTRL_EN, core_mask); - // | self.bio.ms(utra::bio_bdma::SFR_CTRL_RESTART, core_mask) - self.bio.ms(utra::bio_bdma::SFR_CTRL_CLKDIV_RESTART, core_mask); - let final_start_mask = target_start_mask; - let final_ctrl_state = ctrl_with_target_stopped | final_start_mask; - - // --- Debug Lines Start --- - // crate::println!("--- Mask Calculation ---"); - // crate::println!(" Target Core Mask (raw): {:012b}", core_mask); - // crate::println!(" Target Start Mask: {:012b}", target_start_mask); - // crate::println!(" Final Start Mask (+C0): {:012b}", final_start_mask); - // crate::println!(" Previous CTRL State: {:012b}", ctrl_with_target_stopped); - // crate::println!(" Final CTRL Value to Write:{:012b}", final_ctrl_state); - // // --- Debug Lines End --- + crate::println!("[CTRL] Applying state {:#x} for {:?}", final_ctrl_state, target_core); self.bio.wo(utra::bio_bdma::SFR_CTRL, final_ctrl_state); - // --- Debug --- - crate::println!("[4] Starting core with final_start_mask: {:012b}", final_start_mask); - crate::println!(" SFR_CTRL after start: {:012b}", final_ctrl_state); - // --- End Debug --- - // 5. Reset the clock divider for the selected core. match target_core { BioCore::Core0 => self.bio.wo(utra::bio_bdma::SFR_QDIV0, 0), @@ -399,12 +368,6 @@ impl BioSharedState { let state_val = if state { 0xFFFFFFFF } else { 0 }; let pin_bitmask = 1 << pin; - // --- Debug --- - // MODIFIED: Removed `format!` macro. - crate::println!("[5] Sending command to FIFO for core {}:", target_core as usize); - crate::println!(" pin_bitmask: {:#010x}, state_val: {:#010x}", pin_bitmask, state_val); - // --- End Debug --- - match target_core { BioCore::Core0 => { self.bio.wo(utra::bio_bdma::SFR_TXF0, pin_bitmask); @@ -441,24 +404,35 @@ impl BioSharedState { pub fn start_wave_generator(&mut self, pin: u32, core: BioCore, clock_divisor: u32, delay_count: u32) { let core_mask = 1 << (core as usize); - // 1. Stop the target core to ensure a clean state. - let mut ctrl = self.bio.r(utra::bio_bdma::SFR_CTRL); - ctrl &= !core_mask; - self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + // 1. Stop the target core to preserve the state of other cores. + let initial_ctrl_state = self.bio.r(utra::bio_bdma::SFR_CTRL); + let ctrl_with_target_stopped = initial_ctrl_state & !core_mask; + self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl_with_target_stopped); // 2. Clear any stale data from the core's FIFO. self.bio.wfo(utra::bio_bdma::SFR_FIFO_CLR_SFR_FIFO_CLR, core_mask as u32); // 3. Load the slow wave generator program. - let prog = slow_wave_generator_code(); + let prog = match core { + BioCore::Core0 => slow_wave_generator_core0(), + BioCore::Core1 => slow_wave_generator_core1(), + BioCore::Core2 => slow_wave_generator_core2(), + BioCore::Core3 => slow_wave_generator_core3(), + }; + self.load_code(prog, 0, core); - // 4. Start the core. - // Note: This uses a simple start and does not include the master clock reset - // from the cooperative pin_set function, as this function is exclusive. - let start_mask = core_mask | (core_mask << 4) | (core_mask << 8); - ctrl |= start_mask; - self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + // 4. Start the core using a cooperative mask. + // We include RESTART here to ensure the program always starts from the beginning. + let target_start_mask = self.bio.ms(utra::bio_bdma::SFR_CTRL_EN, core_mask) + | self.bio.ms(utra::bio_bdma::SFR_CTRL_RESTART, core_mask) + | self.bio.ms(utra::bio_bdma::SFR_CTRL_CLKDIV_RESTART, core_mask); + + // Combine the state of other running cores with the start command for our target core. + let final_ctrl_state = ctrl_with_target_stopped | target_start_mask; + + crate::println!("[CTRL] Applying wavegen state {:#x} for {:?}", final_ctrl_state, core); + self.bio.wo(utra::bio_bdma::SFR_CTRL, final_ctrl_state); // 5. Set the clock divider for the selected core. match core { @@ -640,15 +614,14 @@ bio_code!( "mv x21, t2", // Write state, masked by x26 "j 86b" ); - #[rustfmt::skip] -bio_code!(slow_wave_generator_code, SLOW_WAVE_START, SLOW_WAVE_END, +bio_code!(slow_wave_generator_core0, SLOW_WAVE_START_0, SLOW_WAVE_END_0, // Configure all GPIOs as outputs. "li t0, 0xFFFFFFFF", "mv x24, t0", - // Read the pin mask from FIFO0 into t1. + // Read the pin mask from FIFO0 (x16) into t1. "mv t1, x16", - // Read the delay count from FIFO0 into t2. + // Read the delay count from FIFO0 (x16) into t2. "mv t2, x16", // Set the GPIO mask register to the pin mask. "mv x26, t1", @@ -657,15 +630,67 @@ bio_code!(slow_wave_generator_code, SLOW_WAVE_START, SLOW_WAVE_END, "mv x21, t1", // Set pin high "mv t3, t2", // Load counter into t3 for the delay loop "11:", // Delay loop 1 - "mv x20, zero", // << Wait for one (slow) BIO clock cycle + "mv x20, zero", // Wait for one BIO clock cycle "addi t3, t3, -1", // Decrement counter "bne t3, zero, 11b", // Loop if not zero // --- LOW PULSE --- "mv x21, zero", // Set pin low "mv t3, t2", // Re-load counter for the delay loop "12:", // Delay loop 2 - "mv x20, zero", // << Wait for one (slow) BIO clock cycle + "mv x20, zero", // Wait for one BIO clock cycle "addi t3, t3, -1", // Decrement counter "bne t3, zero, 12b", // Loop if not zero "j 10b" // Repeat the whole cycle ); + +#[rustfmt::skip] +bio_code!(slow_wave_generator_core1, SLOW_WAVE_START_1, SLOW_WAVE_END_1, + "li t0, 0xFFFFFFFF", "mv x24, t0", + "mv t1, x17", // Read pin mask from FIFO1 (x17) + "mv t2, x17", // Read delay count from FIFO1 (x17) + "mv x26, t1", + "20:", + "mv x21, t1", + "mv t3, t2", + "21:", + "mv x20, zero", "addi t3, t3, -1", "bne t3, zero, 21b", + "mv x21, zero", + "mv t3, t2", + "22:", + "mv x20, zero", "addi t3, t3, -1", "bne t3, zero, 22b", + "j 20b" +); +#[rustfmt::skip] +bio_code!(slow_wave_generator_core2, SLOW_WAVE_START_2, SLOW_WAVE_END_2, + "li t0, 0xFFFFFFFF", "mv x24, t0", + "mv t1, x18", // Read pin mask from FIFO2 (x18) + "mv t2, x18", // Read delay count from FIFO2 (x18) + "mv x26, t1", + "30:", + "mv x21, t1", + "mv t3, t2", + "31:", + "mv x20, zero", "addi t3, t3, -1", "bne t3, zero, 31b", + "mv x21, zero", + "mv t3, t2", + "32:", + "mv x20, zero", "addi t3, t3, -1", "bne t3, zero, 32b", + "j 30b" +); +#[rustfmt::skip] +bio_code!(slow_wave_generator_core3, SLOW_WAVE_START_3, SLOW_WAVE_END_3, + "li t0, 0xFFFFFFFF", "mv x24, t0", + "mv t1, x19", // Read pin mask from FIFO3 (x19) + "mv t2, x19", // Read delay count from FIFO3 (x19) + "mv x26, t1", + "40:", + "mv x21, t1", + "mv t3, t2", + "41:", + "mv x20, zero", "addi t3, t3, -1", "bne t3, zero, 41b", + "mv x21, zero", + "mv t3, t2", + "42:", + "mv x20, zero", "addi t3, t3, -1", "bne t3, zero, 42b", + "j 40b" +); From e020a4591331daa982531eaf642fa69b6c59afa4 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Wed, 13 Aug 2025 15:40:04 +0800 Subject: [PATCH 09/11] refactoring so that pin is a nice coherent function --- baremetal/src/repl.rs | 296 +++++++++++++++++++++++------------------- 1 file changed, 163 insertions(+), 133 deletions(-) diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index 1918fba7c..875f92316 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -462,137 +462,33 @@ impl Repl { } } #[cfg(feature = "nto-bio")] - "pwm" => { - // Check for the correct number of arguments: pin, state, and core. - if args.len() != 3 { - crate::println!("Usage: pwm "); + #[cfg(feature = "nto-bio")] + "pin" => { + // We need at least a subcommand. + if args.is_empty() { + crate::println!("Usage: pin ..."); self.do_cmd = false; self.cmdline.clear(); return; } - // Parse the pin number (args[0]). - let pin = match u32::from_str_radix(&args[0], 10) { - Ok(p) if p < 32 => p, - _ => { - crate::println!("Invalid pin number. Must be 0-31."); - self.do_cmd = false; - self.cmdline.clear(); - return; - } - }; - - // Parse the state (args[1]). - let state = args[1].as_str(); - if state != "on" && state != "off" { - crate::println!("Invalid state. Use 'on' or 'off'."); - self.do_cmd = false; - self.cmdline.clear(); - return; - } - - // Parse the core number (args[2]). - let core_num = match u32::from_str_radix(&args[2], 10) { - Ok(c) if c < 4 => c, // Valid cores are 0, 1, 2, 3 - _ => { - crate::println!("Invalid core number. Must be 0-3."); - self.do_cmd = false; - self.cmdline.clear(); - return; - } - }; - let target_core = BioCore::from(core_num as usize); + let subcommand = args[0].as_str(); + // Initialize BIO and IOX once, as they are common to all subcommands. let mut bio_ss = BioSharedState::new(); let iox = cramium_hal::iox::Iox::new(utra::iox::HW_IOX_BASE as *mut u32); iox.set_ports_from_pio_bitmask(0xFFFF_FFFF); - if state == "on" { - // Example parameters for a ~0.25 Hz wave - let clock_divisor = 0x5000000; // For ~32kHz BIO clock - let delay_count = 2000; - - bio_ss.start_wave_generator(pin, target_core, clock_divisor, delay_count); - crate::println!("Generating wave on pin {} using {:?}.", pin, target_core); - } else { - bio_ss.stop_wave_generator(target_core); - crate::println!("Stopped wave generator on {:?}.", target_core); - } - } - - #[cfg(feature = "nto-bio")] - "pin" => { - if args.is_empty() { - crate::println!("Usage: pin ..."); - self.do_cmd = false; - self.cmdline.clear(); - return; - } - - match args[0].as_str() { - "read" => { - if args.len() != 2 { - crate::println!("Usage: pin read "); - self.do_cmd = false; - self.cmdline.clear(); - return; - } - - let pin = match u32::from_str_radix(&args[1], 10) { - Ok(p) if p < 32 => p, - _ => { - crate::println!("Invalid pin number. Must be 0-31."); - self.do_cmd = false; - self.cmdline.clear(); - return; - } - }; - - let mut bio_ss = BioSharedState::new(); - let iox = cramium_hal::iox::Iox::new(utra::iox::HW_IOX_BASE as *mut u32); - iox.set_ports_from_pio_bitmask(0xFFFF_FFFF); - - // Stop Core 0 to ensure a clean state - let mut ctrl = bio_ss.bio.r(utra::bio_bdma::SFR_CTRL); - ctrl &= !0b0001; - bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); - - // Clear any stale data from the FIFO - bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0b0001); - - // Load the pin reading program - let prog = pin_read_code(); - bio_ss.load_code(prog, 0, BioCore::Core0); - - // Start the core - ctrl |= 0b0001_0001_0001; - bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); - - // Send the pin mask to the core - let pin_mask = 1 << pin; - bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, pin_mask); - - // Wait for the result to appear in the output FIFO. - // A timeout could be added here to prevent hanging. - while bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL0) == 0 {} - - // Read the result - let result = bio_ss.bio.r(utra::bio_bdma::SFR_RXF0); - let state_str = if result == 0 { "low" } else { "high" }; - crate::println!("Pin {} is {}.", pin, state_str); - - // Stop the core after use to prevent interference - ctrl &= !0b0001; - bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); - } - "up" => { + match subcommand { + "set" => { if args.len() < 3 || args.len() > 4 { - crate::println!("Usage: pin up [core_id: 0-3]"); + crate::println!("Usage: pin set [core]"); self.do_cmd = false; self.cmdline.clear(); return; } + // Arg 1: Pin Number let pin = match u32::from_str_radix(&args[1], 10) { Ok(p) if p < 32 => p, _ => { @@ -603,6 +499,7 @@ impl Repl { } }; + // Arg 2: State let state_str = args[2].as_str(); if state_str != "on" && state_str != "off" { crate::println!("Invalid state. Use 'on' or 'off'."); @@ -610,34 +507,23 @@ impl Repl { self.cmdline.clear(); return; } + let state_bool = state_str == "on"; - // 1. Parse the optional core ID argument. + // Arg 3 (Optional): Core ID let target_core: Option = if args.len() == 4 { match u32::from_str_radix(&args[3], 10) { - Ok(0) => Some(BioCore::Core0), - Ok(1) => Some(BioCore::Core1), - Ok(2) => Some(BioCore::Core2), - Ok(3) => Some(BioCore::Core3), + Ok(c) if c < 4 => Some(BioCore::from(c as usize)), _ => { - crate::println!("Invalid core ID. Must be 0, 1, 2, or 3."); + crate::println!("Invalid core ID. Must be 0-3."); self.do_cmd = false; self.cmdline.clear(); return; } } } else { - None // Default to Core 0 if no core is specified. + None // `set_pin` function handles the default core. }; - // 2. Initialize BIO and IOX - let mut bio_ss = BioSharedState::new(); - let iox = cramium_hal::iox::Iox::new(utra::iox::HW_IOX_BASE as *mut u32); - iox.set_ports_from_pio_bitmask(0xFFFF_FFFF); - - // 3. Convert string state to boolean - let state_bool = state_str == "on"; - - // 4. Call the library function with the parsed core. let core_name = format!("{:?}", target_core.unwrap_or(BioCore::Core0)); crate::println!( "Setting pin {} to {} using {}...", @@ -648,8 +534,152 @@ impl Repl { bio_ss.set_pin(pin, state_bool, target_core); crate::println!("Command sent."); } + "pwm" => { + if args.len() < 3 || args.len() > 4 { + crate::println!("Usage: pin pwm [core]"); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + + // Arg 1: Pin Number + let pin = match u32::from_str_radix(&args[1], 10) { + Ok(p) if p < 32 => p, + _ => { + crate::println!("Invalid pin number. Must be 0-31."); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + }; + + // Arg 2: State + let state = args[2].as_str(); + if state != "on" && state != "off" { + crate::println!("Invalid state. Use 'on' or 'off'."); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + + // Arg 3 (Optional): Core ID + let target_core = if args.len() == 4 { + match u32::from_str_radix(&args[3], 10) { + Ok(c) if c < 4 => BioCore::from(c as usize), + _ => { + crate::println!("Invalid core number. Must be 0-3."); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + } + } else { + BioCore::Core0 // Default to Core0 if not specified + }; + + if state == "on" { + let clock_divisor = 0x5000000; + let delay_count = 2000; + bio_ss.start_wave_generator(pin, target_core, clock_divisor, delay_count); + crate::println!("Generating PWM on pin {} using {:?}.", pin, target_core); + } else { + bio_ss.stop_wave_generator(target_core); + crate::println!("Stopped PWM on {:?}.", target_core); + } + } + "read" => { + if args.len() < 2 || args.len() > 3 { + crate::println!("Usage: pin read [core]"); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + + // Arg 1: Pin Number + let pin = match u32::from_str_radix(&args[1], 10) { + Ok(p) if p < 32 => p, + _ => { + crate::println!("Invalid pin number. Must be 0-31."); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + }; + + // Arg 2 (Optional): Core ID + let target_core = if args.len() == 3 { + match u32::from_str_radix(&args[2], 10) { + Ok(c) if c < 4 => BioCore::from(c as usize), + _ => { + crate::println!("Invalid core number. Must be 0-3."); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + } + } else { + BioCore::Core0 // Default to Core0 if not specified + }; + + // Stop the core before use to ensure a clean state + bio_ss.stop_wave_generator(target_core); + bio_ss.bio.wfo(utra::bio_bdma::SFR_FIFO_CLR_SFR_FIFO_CLR, 1 << (target_core as u32)); + + // Load the pin reading program. + // NOTE: This assumes a generic `pin_read_code` exists. For this to be + // truly multi-core, you would need core-specific read programs + // similar to the `slow_wave_generator_coreX` functions. + let prog = pin_read_code(); + bio_ss.load_code(prog, 0, target_core); + + // Start the core cooperatively + let start_mask = + bio_ss.bio.ms(utra::bio_bdma::SFR_CTRL_EN, 1 << (target_core as u32)) + | bio_ss.bio.ms(utra::bio_bdma::SFR_CTRL_RESTART, 1 << (target_core as u32)); + let initial_ctrl = bio_ss.bio.r(utra::bio_bdma::SFR_CTRL); + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, initial_ctrl | start_mask); + + // Send the pin mask to the correct core's FIFO + let (txf_reg, rxf_reg, flevel_reg) = match target_core { + BioCore::Core0 => ( + utra::bio_bdma::SFR_TXF0, + utra::bio_bdma::SFR_RXF0, + utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL0, + ), + BioCore::Core1 => ( + utra::bio_bdma::SFR_TXF1, + utra::bio_bdma::SFR_RXF1, + utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1, + ), + BioCore::Core2 => ( + utra::bio_bdma::SFR_TXF2, + utra::bio_bdma::SFR_RXF2, + utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL2, + ), + BioCore::Core3 => ( + utra::bio_bdma::SFR_TXF3, + utra::bio_bdma::SFR_RXF3, + utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL3, + ), + }; + bio_ss.bio.wo(txf_reg, 1 << pin); + + // Wait for the result in the correct FIFO + while bio_ss.bio.rf(flevel_reg) == 0 {} + + // Read and print the result + let result = bio_ss.bio.r(rxf_reg); + let state_str = if result == 0 { "low" } else { "high" }; + crate::println!("Pin {} on {:?} is {}.", pin, target_core, state_str); + + // Stop the core after use + bio_ss.stop_wave_generator(target_core); + } _ => { - crate::println!("Unknown pin command: '{}'. Use 'up' or 'read'.", args[0]); + crate::println!( + "Unknown pin command: '{}'. Use 'set', 'pwm', or 'read'.", + subcommand + ); } } } @@ -668,7 +698,7 @@ impl Repl { #[cfg(not(feature = "cramium-soc"))] crate::print!(", mon"); #[cfg(feature = "nto-bio")] - crate::print!(", bio, pin, pwm"); + crate::print!(", bio, pin"); crate::println!(""); } } From d02970c2648e2c179e90a48d5852ad6514e38189 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Wed, 13 Aug 2025 18:53:33 +0800 Subject: [PATCH 10/11] now with read --- baremetal/src/repl.rs | 86 ++++------------------- libs/xous-bio-bdma/src/lib.rs | 127 ++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 72 deletions(-) diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index 875f92316..1392dacd6 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -493,6 +493,7 @@ impl Repl { Ok(p) if p < 32 => p, _ => { crate::println!("Invalid pin number. Must be 0-31."); + crate::println!("Usage: pin set [core]"); self.do_cmd = false; self.cmdline.clear(); return; @@ -503,6 +504,7 @@ impl Repl { let state_str = args[2].as_str(); if state_str != "on" && state_str != "off" { crate::println!("Invalid state. Use 'on' or 'off'."); + crate::println!("Usage: pin set [core]"); self.do_cmd = false; self.cmdline.clear(); return; @@ -515,6 +517,7 @@ impl Repl { Ok(c) if c < 4 => Some(BioCore::from(c as usize)), _ => { crate::println!("Invalid core ID. Must be 0-3."); + crate::println!("Usage: pin set [core]"); self.do_cmd = false; self.cmdline.clear(); return; @@ -547,7 +550,7 @@ impl Repl { Ok(p) if p < 32 => p, _ => { crate::println!("Invalid pin number. Must be 0-31."); - self.do_cmd = false; + crate::println!("Usage: pin pwm [core]"); self.cmdline.clear(); return; } @@ -557,6 +560,7 @@ impl Repl { let state = args[2].as_str(); if state != "on" && state != "off" { crate::println!("Invalid state. Use 'on' or 'off'."); + crate::println!("Usage: pin pwm [core]"); self.do_cmd = false; self.cmdline.clear(); return; @@ -568,6 +572,7 @@ impl Repl { Ok(c) if c < 4 => BioCore::from(c as usize), _ => { crate::println!("Invalid core number. Must be 0-3."); + crate::println!("Usage: pin pwm [core]"); self.do_cmd = false; self.cmdline.clear(); return; @@ -588,6 +593,7 @@ impl Repl { } } "read" => { + // Validate arguments: must have a pin number, core is optional. if args.len() < 2 || args.len() > 3 { crate::println!("Usage: pin read [core]"); self.do_cmd = false; @@ -595,23 +601,25 @@ impl Repl { return; } - // Arg 1: Pin Number + // Arg 1: Parse the pin number. let pin = match u32::from_str_radix(&args[1], 10) { Ok(p) if p < 32 => p, _ => { crate::println!("Invalid pin number. Must be 0-31."); + crate::println!("Usage: pin read [core]"); self.do_cmd = false; self.cmdline.clear(); return; } }; - // Arg 2 (Optional): Core ID + // Arg 2 (Optional): Parse the core ID. let target_core = if args.len() == 3 { match u32::from_str_radix(&args[2], 10) { Ok(c) if c < 4 => BioCore::from(c as usize), _ => { crate::println!("Invalid core number. Must be 0-3."); + crate::println!("Usage: pin read [core]"); self.do_cmd = false; self.cmdline.clear(); return; @@ -621,59 +629,11 @@ impl Repl { BioCore::Core0 // Default to Core0 if not specified }; - // Stop the core before use to ensure a clean state - bio_ss.stop_wave_generator(target_core); - bio_ss.bio.wfo(utra::bio_bdma::SFR_FIFO_CLR_SFR_FIFO_CLR, 1 << (target_core as u32)); + // Call the library function to read the pin state. + let is_high = bio_ss.read_pin(pin, target_core); + let state_str = if is_high { "high" } else { "low" }; - // Load the pin reading program. - // NOTE: This assumes a generic `pin_read_code` exists. For this to be - // truly multi-core, you would need core-specific read programs - // similar to the `slow_wave_generator_coreX` functions. - let prog = pin_read_code(); - bio_ss.load_code(prog, 0, target_core); - - // Start the core cooperatively - let start_mask = - bio_ss.bio.ms(utra::bio_bdma::SFR_CTRL_EN, 1 << (target_core as u32)) - | bio_ss.bio.ms(utra::bio_bdma::SFR_CTRL_RESTART, 1 << (target_core as u32)); - let initial_ctrl = bio_ss.bio.r(utra::bio_bdma::SFR_CTRL); - bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, initial_ctrl | start_mask); - - // Send the pin mask to the correct core's FIFO - let (txf_reg, rxf_reg, flevel_reg) = match target_core { - BioCore::Core0 => ( - utra::bio_bdma::SFR_TXF0, - utra::bio_bdma::SFR_RXF0, - utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL0, - ), - BioCore::Core1 => ( - utra::bio_bdma::SFR_TXF1, - utra::bio_bdma::SFR_RXF1, - utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1, - ), - BioCore::Core2 => ( - utra::bio_bdma::SFR_TXF2, - utra::bio_bdma::SFR_RXF2, - utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL2, - ), - BioCore::Core3 => ( - utra::bio_bdma::SFR_TXF3, - utra::bio_bdma::SFR_RXF3, - utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL3, - ), - }; - bio_ss.bio.wo(txf_reg, 1 << pin); - - // Wait for the result in the correct FIFO - while bio_ss.bio.rf(flevel_reg) == 0 {} - - // Read and print the result - let result = bio_ss.bio.r(rxf_reg); - let state_str = if result == 0 { "low" } else { "high" }; crate::println!("Pin {} on {:?} is {}.", pin, target_core, state_str); - - // Stop the core after use - bio_ss.stop_wave_generator(target_core); } _ => { crate::println!( @@ -715,24 +675,6 @@ impl Repl { } } -#[rustfmt::skip] -bio_code!(pin_read_code, PIN_READ_START, PIN_READ_END, - // Configure all pins as inputs by clearing the direction register. - "li t0, 1", - "mv x25, t0", - "wait_for_cmd:", - // Wait for the CPU to send a pin mask via FIFO0. - "mv t1, x16", // t1 = pin_mask - // Read the current state of all GPIO input pins. - "mv t2, x21", // t2 = all pin states - // Isolate the state of the requested pin. - "and t3, t2, t1", // t3 = 0 if low, pin_mask if high - // Send the result back to the CPU via FIFO0. - "mv x17, t3", - // Loop to wait for the next command. - "j wait_for_cmd" -); - #[rustfmt::skip] bio_code!(simple_test_code, SIMPLE_TEST_START, SIMPLE_TEST_END, "li sp, 0x800", diff --git a/libs/xous-bio-bdma/src/lib.rs b/libs/xous-bio-bdma/src/lib.rs index 640a81eef..2fc17df71 100644 --- a/libs/xous-bio-bdma/src/lib.rs +++ b/libs/xous-bio-bdma/src/lib.rs @@ -477,6 +477,89 @@ impl BioSharedState { self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); } + /// Reads the state of a single GPIO pin. + /// + /// This function loads a dedicated program onto the specified core, + /// sets the pin to input, reads its value, and returns true (high) or false (low). + /// The operation is cooperative and will not interfere with other running cores. + /// + /// # Arguments + /// * `pin` - The GPIO pin number to read (0-31). + /// * `core` - The `BioCore` to use for the operation. (Currently hard-coded to Core0) + /// + /// # Returns + /// * `bool` - The state of the pin (`true` for high, `false` for low). + pub fn read_pin(&mut self, pin: u32, core: BioCore) -> bool { + let target_core = core; + let core_mask = 1 << (target_core as usize); + + // 1. Cooperatively stop the target core + let initial_ctrl_state = self.bio.r(utra::bio_bdma::SFR_CTRL); + let ctrl_with_target_stopped = initial_ctrl_state & !core_mask; + self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl_with_target_stopped); + + // 2. Clear the correct core's FIFO + self.bio.wfo(utra::bio_bdma::SFR_FIFO_CLR_SFR_FIFO_CLR, core_mask as u32); + + // 3. Load the correct program for the target core + let prog = match target_core { + BioCore::Core0 => pin_read_code_core0(), + BioCore::Core1 => pin_read_code_core1(), + BioCore::Core2 => pin_read_code_core2(), + BioCore::Core3 => pin_read_code_core3(), + }; + self.load_code(prog, 0, target_core); + + // 4. Cooperatively start the core + let start_mask = self.bio.ms(utra::bio_bdma::SFR_CTRL_EN, core_mask) + | self.bio.ms(utra::bio_bdma::SFR_CTRL_RESTART, core_mask); + let final_start_state = ctrl_with_target_stopped | start_mask; + + // --- Final Debug Statement --- + crate::println!("[read_pin] Starting core with CTRL state: {:012b}", final_start_state); + + self.bio.wo(utra::bio_bdma::SFR_CTRL, final_start_state); + + // 5. Select the correct FIFO registers for the target core + let (txf_reg, rxf_reg, flevel_reg) = match target_core { + BioCore::Core0 => ( + utra::bio_bdma::SFR_TXF0, + utra::bio_bdma::SFR_RXF0, + utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL0, + ), + BioCore::Core1 => ( + utra::bio_bdma::SFR_TXF1, + utra::bio_bdma::SFR_RXF1, + utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1, + ), + BioCore::Core2 => ( + utra::bio_bdma::SFR_TXF2, + utra::bio_bdma::SFR_RXF2, + utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL2, + ), + BioCore::Core3 => ( + utra::bio_bdma::SFR_TXF3, + utra::bio_bdma::SFR_RXF3, + utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL3, + ), + }; + + // 6. Send the pin mask to the selected core's FIFO + let pin_mask = 1 << pin; + self.bio.wo(txf_reg, pin_mask); + + // 7. Wait for the result in the correct FIFO + while self.bio.rf(flevel_reg) == 0 {} + + // 8. Read the result from the correct FIFO + let result = self.bio.r(rxf_reg); + + // 9. Stop the core + self.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl_with_target_stopped); + + result != 0 + } + pub fn debug_pc(&self) { crate::println!( "c0:{:04x} c1:{:04x} c2:{:04x} c3:{:04x}", @@ -694,3 +777,47 @@ bio_code!(slow_wave_generator_core3, SLOW_WAVE_START_3, SLOW_WAVE_END_3, "mv x20, zero", "addi t3, t3, -1", "bne t3, zero, 42b", "j 40b" ); + +#[rustfmt::skip] +bio_code!(pin_read_code_core0, PIN_READ_START_0, PIN_READ_END_0, + // Label for the start of the main loop. + "50:", + // Read the pin mask from FIFO0 (x16). The core will halt here until data is available. + "mv t0, x16", + + // Set the GPIO action mask (x26) to our target pin mask. + // This ensures subsequent GPIO operations only affect our target pin. + "mv x26, t0", + + // Using the mask in x26, set the target pin's direction to input. + // Writing the pin mask to x25 makes the pin an input. + "mv x25, t0", + + // Read the state of all 32 GPIO pins into a temporary register. + "mv t1, x21", + + // Isolate the state of our target pin by ANDing the full state with our mask. + // The result is 0 if the pin is low, or the pin mask value if it's high. + "and t2, t1, t0", + + // Send the result back to the host via FIFO0 (x16). + "mv x16, t2", + + // Loop back to wait for the next read request. + "j 50b" +); + +#[rustfmt::skip] +bio_code!(pin_read_code_core1, PIN_READ_START_1, PIN_READ_END_1, + "60:", "mv t0, x17", "mv x26, t0", "mv x25, t0", "mv t1, x21", "and t2, t1, t0", "mv x17, t2", "j 60b" +); + +#[rustfmt::skip] +bio_code!(pin_read_code_core2, PIN_READ_START_2, PIN_READ_END_2, + "70:", "mv t0, x18", "mv x26, t0", "mv x25, t0", "mv t1, x21", "and t2, t1, t0", "mv x18, t2", "j 70b" +); + +#[rustfmt::skip] +bio_code!(pin_read_code_core3, PIN_READ_START_3, PIN_READ_END_3, + "80:", "mv t0, x19", "mv x26, t0", "mv x25, t0", "mv t1, x21", "and t2, t1, t0", "mv x19, t2", "j 80b" +); From bd8a3c7f88cc0cbe63ae71549382283c4e249aab Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Wed, 13 Aug 2025 19:15:46 +0800 Subject: [PATCH 11/11] cleaning up code --- baremetal/src/repl.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index 1392dacd6..e5eff60ea 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -462,7 +462,6 @@ impl Repl { } } #[cfg(feature = "nto-bio")] - #[cfg(feature = "nto-bio")] "pin" => { // We need at least a subcommand. if args.is_empty() {