From 663c07fce4e38ad489bf29f03653be53a4eebb77 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Mon, 16 Jun 2025 19:04:46 +0800 Subject: [PATCH 1/5] adds a blinky function to the LEDs on the arty board using bio --- baremetal/src/main.rs | 6 ++-- baremetal/src/repl.rs | 76 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/baremetal/src/main.rs b/baremetal/src/main.rs index 43fb64592..7c6c6892d 100644 --- a/baremetal/src/main.rs +++ b/baremetal/src/main.rs @@ -44,7 +44,7 @@ pub fn uart_irq_handler() { pub unsafe extern "C" fn rust_entry() -> ! { // turn on a green LED to indicate boot let mut rgb = CSR::new(utra::rgb::HW_RGB_BASE as *mut u32); - rgb.wfo(utra::rgb::OUT_OUT, 0x002); + rgb.wfo(utra::rgb::OUT_OUT, 0b010000000000); crate::platform::early_init(); crate::println!("\n~~Baremetal up!~~\n"); @@ -76,7 +76,9 @@ pub unsafe extern "C" fn rust_entry() -> ! { // Animate the LED flashing to indicate repl loop is running delay(1); count += 1; - rgb.wfo(utra::rgb::OUT_OUT, ((count / 500) as u32) << 6); + + // &-ing to set LD3 to 0 and then setting the value + rgb.rmwf(utra::rgb::OUT_OUT, rgb.r(utra::rgb::OUT) & 0b000111111111 | ((count / 500) as u32) << 9); } } diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index 4b670b38d..bfe1bcb83 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -85,9 +85,81 @@ 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]; + + // Determine shift from LED name based on the hardware layout. + let shift = match ld_name.as_str() { + "LD0" => 0, + "LD1" => 3, + "LD2" => 6, + _ => { + crate::println!("Invalid LD name: {}. Use LD0, LD1, or LD2", ld_name); + self.do_cmd = false; + self.cmdline.clear(); + 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(utra::rgb::HW_RGB_BASE as *mut u32); + let new_state = bgr_val << shift; + + rgb.rmwf(utra::rgb::OUT_OUT, rgb.r(utra::rgb::OUT) | new_state); + + crate::println!( + "Set {} to BGR value 0b{:03b} (from hex {}). Other LEDs are off.", + ld_name, + bgr_val, + hex_code + ); + } + Err(_) => { + crate::println!("Invalid hex color value: {}", hex_code); + } + } + } + "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 (turns others off)."); + crate::println!(" LD: LD1, LD2, or LD3"); + crate::println!(" RGB_HEX: e.g., ff0000 (red), 00ff00 (green), 0000ff (blue)"); + } + + "" => {} + _ => { - crate::println!("Command not recognized: {}", cmd); - crate::println!("Commands include: echo, mon"); + crate::println!("Command not recognized: '{}'", cmd); + crate::println!("Type 'help' for a list of commands."); } } From 28d887324aa773eebe685adefaced6f5a54d4741 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Mon, 16 Jun 2025 20:25:36 +0800 Subject: [PATCH 2/5] added a nice abstraction --- baremetal/src/arty_rgb.rs | 10 ++++++++++ baremetal/src/main.rs | 4 ++-- baremetal/src/repl.rs | 30 +++++++++++++++--------------- 3 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 baremetal/src/arty_rgb.rs 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 7c6c6892d..3cd607464 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; @@ -77,8 +78,7 @@ pub unsafe extern "C" fn rust_entry() -> ! { delay(1); count += 1; - // &-ing to set LD3 to 0 and then setting the value - rgb.rmwf(utra::rgb::OUT_OUT, rgb.r(utra::rgb::OUT) & 0b000111111111 | ((count / 500) as u32) << 9); + rgb.rmwf(arty_rgb::LD3, (count / 500) as u32); } } diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index bfe1bcb83..b604587e8 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -4,6 +4,8 @@ use alloc::vec::Vec; use utralib::*; use xous_bio_bdma::*; +use crate::arty_rgb; + pub struct Repl { cmdline: String, do_cmd: bool, @@ -49,7 +51,7 @@ impl Repl { match cmd.as_str() { "mon" => { 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; @@ -65,7 +67,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; @@ -99,15 +101,14 @@ impl Repl { let ld_name = &args[0]; let hex_code = &args[1]; - // Determine shift from LED name based on the hardware layout. - let shift = match ld_name.as_str() { - "LD0" => 0, - "LD1" => 3, - "LD2" => 6, + let target_led_field = match ld_name.as_str() { + "LD0" => arty_rgb::LD0, + "LD1" => arty_rgb::LD1, + "LD2" => arty_rgb::LD2, _ => { - crate::println!("Invalid LD name: {}. Use LD0, LD1, or LD2", ld_name); - self.do_cmd = false; + crate::println!("Invalid LED name: {}. Use LD0, LD1, LD2.", ld_name); self.cmdline.clear(); + self.do_cmd = false; return; } }; @@ -126,13 +127,12 @@ impl Repl { let b_msb = color & 0x000080; let bgr_val = (b_msb >> 6) | (g_msb >> 13) | (r_msb >> 23); - let mut rgb = CSR::new(utra::rgb::HW_RGB_BASE as *mut u32); - let new_state = bgr_val << shift; + let mut rgb = CSR::new(arty_rgb::HW_RGB_BASE as *mut u32); - rgb.rmwf(utra::rgb::OUT_OUT, rgb.r(utra::rgb::OUT) | new_state); + rgb.rmwf(target_led_field, bgr_val); crate::println!( - "Set {} to BGR value 0b{:03b} (from hex {}). Other LEDs are off.", + "Set {} to BGR value 0b{:03b} (from hex {}).", ld_name, bgr_val, hex_code @@ -150,8 +150,8 @@ impl Repl { crate::println!( " mon - Monitors the program counters of the BIO cores." ); - crate::println!(" blinky - Sets an LED to a color (turns others off)."); - crate::println!(" LD: LD1, LD2, or LD3"); + 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)"); } From 0bac7f6c0ffc79761a0fd4c6864d05580582e106 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Mon, 16 Jun 2025 20:29:40 +0800 Subject: [PATCH 3/5] missed one --- baremetal/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/baremetal/src/main.rs b/baremetal/src/main.rs index 3cd607464..b238fda5d 100644 --- a/baremetal/src/main.rs +++ b/baremetal/src/main.rs @@ -45,7 +45,7 @@ pub fn uart_irq_handler() { pub unsafe extern "C" fn rust_entry() -> ! { // turn on a green LED to indicate boot let mut rgb = CSR::new(utra::rgb::HW_RGB_BASE as *mut u32); - rgb.wfo(utra::rgb::OUT_OUT, 0b010000000000); + rgb.wfo(arty_rgb::LD3, 0b010); crate::platform::early_init(); crate::println!("\n~~Baremetal up!~~\n"); From 02fa80af014d720d8364f9a0c1873e385bf64390 Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Tue, 29 Jul 2025 14:51:24 +0800 Subject: [PATCH 4/5] wip --- baremetal/src/main.rs | 2 +- baremetal/src/repl.rs | 88 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/baremetal/src/main.rs b/baremetal/src/main.rs index 31369bb05..763b8e08d 100644 --- a/baremetal/src/main.rs +++ b/baremetal/src/main.rs @@ -57,7 +57,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 f8312542f..8938d116c 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -1,5 +1,6 @@ use alloc::string::{String, ToString}; use alloc::vec::Vec; +use core::sync::atomic::{AtomicBool, Ordering}; use utralib::*; #[cfg(feature = "artybio")] @@ -7,6 +8,30 @@ 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, @@ -146,6 +171,68 @@ impl Repl { } } } + "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; // Restart Core 0 + 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."); @@ -156,6 +243,7 @@ impl Repl { 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."); } "" => {} From 043fc06804c4442e559f9c246fb8d00fe6e2444c Mon Sep 17 00:00:00 2001 From: Sam Chin Date: Thu, 31 Jul 2025 16:48:29 +0800 Subject: [PATCH 5/5] now you can turn on and off the LED --- baremetal/src/repl.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index 8938d116c..87f457f68 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -210,8 +210,7 @@ impl Repl { let prog = pin_control_code(); bio_ss.load_code(prog, 0, BioCore::Core0); - - ctrl |= 0b0001; // Restart Core 0 + 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.");