diff --git a/baremetal/src/arty_rgb.rs b/baremetal/src/arty_rgb.rs new file mode 100644 index 000000000..50888e820 --- /dev/null +++ b/baremetal/src/arty_rgb.rs @@ -0,0 +1,10 @@ +use utralib::{Field, Register}; + +pub const RGB_NUMREGS: usize = 1; +pub const HW_RGB_BASE: usize = 0xf0000000; +pub const OUT: Register = Register::new(0, 0xfff); +pub const OUT_OUT: Field = Field::new(12, 0, OUT); +pub const LD0: Field = Field::new(3, 0, OUT); +pub const LD1: Field = Field::new(3, 3, OUT); +pub const LD2: Field = Field::new(3, 6, OUT); +pub const LD3: Field = Field::new(3, 9, OUT); diff --git a/baremetal/src/main.rs b/baremetal/src/main.rs index 81956802d..c57ce7bf1 100644 --- a/baremetal/src/main.rs +++ b/baremetal/src/main.rs @@ -5,6 +5,7 @@ extern crate alloc; // contains runtime setup mod asm; +pub mod arty_rgb; mod platform; mod repl; use alloc::collections::VecDeque; @@ -60,7 +61,7 @@ pub unsafe extern "C" fn rust_entry() -> ! { // select a BIO test to run #[cfg(feature = "artybio")] fifo_basic(); - // hello_world(); + hello_world(); // The green LEDs flash whenever the FPGA is configured with the Arty BIO design. // The RGB LEDs flash when the CPU is running this code. diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index ed9c6655a..c5a0e63b2 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -1,11 +1,38 @@ use alloc::string::{String, ToString}; use alloc::vec::Vec; +use core::sync::atomic::{AtomicBool, Ordering}; #[allow(unused_imports)] use utralib::*; #[cfg(feature = "artybio")] use xous_bio_bdma::*; +use crate::arty_rgb; + +// A server program that runs on a BIO core to handle GPIO control. +// It waits for a command from the main CPU via FIFO, executes it, and waits for the next. +#[cfg(feature = "artybio")] +#[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" +); + +// A flag to ensure we only initialize the BIO core once. +static GPIO_CORE_INITIALIZED: AtomicBool = AtomicBool::new(false); + pub struct Repl { cmdline: String, do_cmd: bool, @@ -54,7 +81,7 @@ impl Repl { "mon" => { #[cfg(feature = "artybio")] let bio_ss = BioSharedState::new(); - let mut rgb = CSR::new(utra::rgb::HW_RGB_BASE as *mut u32); + let mut rgb = CSR::new(arty_rgb::HW_RGB_BASE as *mut u32); let mut count = 0; let mut quit = false; const TICKS_PER_PRINT: usize = 5; @@ -71,7 +98,7 @@ impl Repl { bio_ss.bio.r(utra::bio_bdma::SFR_DBG2), bio_ss.bio.r(utra::bio_bdma::SFR_DBG3) ); - rgb.wfo(utra::rgb::OUT_OUT, (count / TICKS_PER_PRINT) as u32); + rgb.wfo(arty_rgb::OUT_OUT, (count / TICKS_PER_PRINT) as u32); } crate::platform::delay(TICK_MS); count += 1; @@ -228,6 +255,138 @@ impl Repl { } crate::println!(""); } + + "blinky" => { + if args.len() != 2 { + crate::println!("Usage: blinky "); + crate::println!("Example: blinky LD2 ff0000"); + crate::println!("Available LEDs: LD0, LD1, LD2"); + self.do_cmd = false; + self.cmdline.clear(); + return; + } + + let ld_name = &args[0]; + let hex_code = &args[1]; + + let target_led_field = match ld_name.as_str() { + "LD0" => arty_rgb::LD0, + "LD1" => arty_rgb::LD1, + "LD2" => arty_rgb::LD2, + _ => { + crate::println!("Invalid LED name: {}. Use LD0, LD1, LD2.", ld_name); + self.cmdline.clear(); + self.do_cmd = false; + return; + } + }; + + let color_val = if hex_code.starts_with("0x") { + u32::from_str_radix(&hex_code[2..], 16) + } else { + u32::from_str_radix(hex_code, 16) + }; + + match color_val { + Ok(color) => { + // convert 24-bit RRGGBB into 3-bit BGR + let r_msb = color & 0x800000; + let g_msb = color & 0x008000; + let b_msb = color & 0x000080; + let bgr_val = (b_msb >> 6) | (g_msb >> 13) | (r_msb >> 23); + + let mut rgb = CSR::new(arty_rgb::HW_RGB_BASE as *mut u32); + + rgb.rmwf(target_led_field, bgr_val); + + crate::println!( + "Set {} to BGR value 0b{:03b} (from hex {}).", + ld_name, + bgr_val, + hex_code + ); + } + Err(_) => { + crate::println!("Invalid hex color value: {}", hex_code); + } + } + } + "capsense" => { + #[cfg(feature = "artybio")] + { + 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(); + + // On first run, load and start the GPIO control program on BIO Core 0. + if !GPIO_CORE_INITIALIZED.load(Ordering::Relaxed) { + crate::println!("Initializing GPIO control core (Core 0)..."); + let mut ctrl = bio_ss.bio.r(utra::bio_bdma::SFR_CTRL); + ctrl &= !0b0001; // Stop Core 0 + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + + let prog = pin_control_code(); + bio_ss.load_code(prog, 0, BioCore::Core0); + ctrl |= 0b0001_0001_0001; + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, ctrl); + GPIO_CORE_INITIALIZED.store(true, Ordering::Relaxed); + crate::println!("GPIO control core is running."); + } + + // Prepare the command for the BIO core. + let pin_mask = 1 << pin; + let state_val = if state == "on" { 1 } else { 0 }; + + // Send the command words to the BIO core via its FIFO. + // The core is waiting to read these two values. + 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()); + } + #[cfg(not(feature = "artybio"))] + { + crate::println!("'capsense' command requires 'artybio' feature."); + } + } + "help" => { + crate::println!("Available commands:"); + crate::println!(" help - Shows this help message."); + crate::println!(" echo [ARGS] - Prints the arguments to the console."); + crate::println!( + " mon - Monitors the program counters of the BIO cores." + ); + crate::println!(" blinky - Sets an LED to a color."); + crate::println!(" LD: LD0, LD1, or LD2"); + crate::println!(" RGB_HEX: e.g., ff0000 (red), 00ff00 (green), 0000ff (blue)"); + crate::println!(" capsense - Sets a GPIO pin high or low."); + } + + "" => {} + _ => { crate::println!("Command not recognized: {}", cmd); crate::print!("Commands include: echo, poke, peek, bogomips");