diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index bfe7e3ce0..e5eff60ea 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; @@ -460,6 +461,188 @@ impl Repl { } } } + #[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; + } + + 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); + + match subcommand { + "set" => { + if args.len() < 3 || args.len() > 4 { + 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, + _ => { + crate::println!("Invalid pin number. Must be 0-31."); + crate::println!("Usage: pin set [core]"); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + }; + + // 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'."); + crate::println!("Usage: pin set [core]"); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + let state_bool = state_str == "on"; + + // Arg 3 (Optional): Core ID + let target_core: Option = if args.len() == 4 { + match u32::from_str_radix(&args[3], 10) { + 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; + } + } + } else { + None // `set_pin` function handles the default core. + }; + + let core_name = format!("{:?}", target_core.unwrap_or(BioCore::Core0)); + crate::println!( + "Setting pin {} to {} using {}...", + pin, + state_str.to_uppercase(), + core_name + ); + 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."); + crate::println!("Usage: pin pwm [core]"); + 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'."); + crate::println!("Usage: pin pwm [core]"); + 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."); + crate::println!("Usage: pin pwm [core]"); + 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" => { + // 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; + self.cmdline.clear(); + return; + } + + // 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): 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; + } + } + } else { + BioCore::Core0 // Default to Core0 if not specified + }; + + // 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" }; + + crate::println!("Pin {} on {:?} is {}.", pin, target_core, state_str); + } + _ => { + crate::println!( + "Unknown pin command: '{}'. Use 'set', 'pwm', or 'read'.", + subcommand + ); + } + } + } + "echo" => { for word in args { crate::print!("{} ", word); @@ -474,7 +657,7 @@ impl Repl { #[cfg(not(feature = "cramium-soc"))] crate::print!(", mon"); #[cfg(feature = "nto-bio")] - crate::print!(", bio"); + crate::print!(", bio, pin"); crate::println!(""); } } diff --git a/libs/xous-bio-bdma/src/lib.rs b/libs/xous-bio-bdma/src/lib.rs index 4bbc9479c..2fc17df71 100644 --- a/libs/xous-bio-bdma/src/lib.rs +++ b/libs/xous-bio-bdma/src/lib.rs @@ -325,6 +325,241 @@ impl BioSharedState { Ok(()) } + pub fn set_pin(&mut self, pin: u32, state: bool, core: Option) { + let target_core = core.unwrap_or(BioCore::Core0); + let core_mask = 1 << (target_core as usize); + + // 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); + + // 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 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. 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; + + crate::println!("[CTRL] Applying state {:#x} for {:?}", final_ctrl_state, target_core); + + self.bio.wo(utra::bio_bdma::SFR_CTRL, final_ctrl_state); + + // 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 the target core's FIFO. + let state_val = if state { 0xFFFFFFFF } else { 0 }; + let pin_bitmask = 1 << pin; + + 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); + } + } + } + + // 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 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 = 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 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 { + 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); + } + + /// 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}", @@ -398,3 +633,191 @@ bio_code!(mem_init_code, MEM_INIT_START, MEM_INIT_END, "10:", "j 10b" ); + +#[rustfmt::skip] +bio_code!( + 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", +"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" +); +#[rustfmt::skip] +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 (x16) into t1. + "mv t1, x16", + // Read the delay count from FIFO0 (x16) 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 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 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" +); + +#[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" +);