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

View file

@ -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 <LD> <RGB_HEX> - Sets an LED to a color (turns others off).");
crate::println!(" LD: LD1, LD2, or LD3");
crate::println!(" blinky <LD> <RGB_HEX> - 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)");
}