added a nice abstraction

This commit is contained in:
Sam Chin 2025-06-16 20:25:36 +08:00
parent 663c07fce4
commit 28d887324a
3 changed files with 27 additions and 17 deletions

10
baremetal/src/arty_rgb.rs Normal file
View file

@ -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);

View file

@ -5,6 +5,7 @@ extern crate alloc;
// contains runtime setup // contains runtime setup
mod asm; mod asm;
pub mod arty_rgb;
mod platform; mod platform;
mod repl; mod repl;
use alloc::collections::VecDeque; use alloc::collections::VecDeque;
@ -77,8 +78,7 @@ pub unsafe extern "C" fn rust_entry() -> ! {
delay(1); delay(1);
count += 1; count += 1;
// &-ing to set LD3 to 0 and then setting the value rgb.rmwf(arty_rgb::LD3, (count / 500) as u32);
rgb.rmwf(utra::rgb::OUT_OUT, rgb.r(utra::rgb::OUT) & 0b000111111111 | ((count / 500) as u32) << 9);
} }
} }

View file

@ -4,6 +4,8 @@ use alloc::vec::Vec;
use utralib::*; use utralib::*;
use xous_bio_bdma::*; use xous_bio_bdma::*;
use crate::arty_rgb;
pub struct Repl { pub struct Repl {
cmdline: String, cmdline: String,
do_cmd: bool, do_cmd: bool,
@ -49,7 +51,7 @@ impl Repl {
match cmd.as_str() { match cmd.as_str() {
"mon" => { "mon" => {
let bio_ss = BioSharedState::new(); 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 count = 0;
let mut quit = false; let mut quit = false;
const TICKS_PER_PRINT: usize = 5; 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_DBG2),
bio_ss.bio.r(utra::bio_bdma::SFR_DBG3) 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); crate::platform::delay(TICK_MS);
count += 1; count += 1;
@ -99,15 +101,14 @@ impl Repl {
let ld_name = &args[0]; let ld_name = &args[0];
let hex_code = &args[1]; let hex_code = &args[1];
// Determine shift from LED name based on the hardware layout. let target_led_field = match ld_name.as_str() {
let shift = match ld_name.as_str() { "LD0" => arty_rgb::LD0,
"LD0" => 0, "LD1" => arty_rgb::LD1,
"LD1" => 3, "LD2" => arty_rgb::LD2,
"LD2" => 6,
_ => { _ => {
crate::println!("Invalid LD name: {}. Use LD0, LD1, or LD2", ld_name); crate::println!("Invalid LED name: {}. Use LD0, LD1, LD2.", ld_name);
self.do_cmd = false;
self.cmdline.clear(); self.cmdline.clear();
self.do_cmd = false;
return; return;
} }
}; };
@ -126,13 +127,12 @@ impl Repl {
let b_msb = color & 0x000080; let b_msb = color & 0x000080;
let bgr_val = (b_msb >> 6) | (g_msb >> 13) | (r_msb >> 23); 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 mut rgb = CSR::new(arty_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); rgb.rmwf(target_led_field, bgr_val);
crate::println!( crate::println!(
"Set {} to BGR value 0b{:03b} (from hex {}). Other LEDs are off.", "Set {} to BGR value 0b{:03b} (from hex {}).",
ld_name, ld_name,
bgr_val, bgr_val,
hex_code hex_code
@ -150,8 +150,8 @@ impl Repl {
crate::println!( crate::println!(
" mon - Monitors the program counters of the BIO cores." " mon - Monitors the program counters of the BIO cores."
); );
crate::println!(" blinky <LD> <RGB_HEX> - Sets an LED to a color (turns others off)."); crate::println!(" blinky <LD> <RGB_HEX> - Sets an LED to a color.");
crate::println!(" LD: LD1, LD2, or LD3"); crate::println!(" LD: LD0, LD1, or LD2");
crate::println!(" RGB_HEX: e.g., ff0000 (red), 00ff00 (green), 0000ff (blue)"); crate::println!(" RGB_HEX: e.g., ff0000 (red), 00ff00 (green), 0000ff (blue)");
} }