Skip to content

Commit 28d8873

Browse files
committed
added a nice abstraction
1 parent 663c07f commit 28d8873

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

baremetal/src/arty_rgb.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use utralib::{Field, Register};
2+
3+
pub const RGB_NUMREGS: usize = 1;
4+
pub const HW_RGB_BASE: usize = 0xf0000000;
5+
pub const OUT: Register = Register::new(0, 0xfff);
6+
pub const OUT_OUT: Field = Field::new(12, 0, OUT);
7+
pub const LD0: Field = Field::new(3, 0, OUT);
8+
pub const LD1: Field = Field::new(3, 3, OUT);
9+
pub const LD2: Field = Field::new(3, 6, OUT);
10+
pub const LD3: Field = Field::new(3, 9, OUT);

baremetal/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate alloc;
55
// contains runtime setup
66
mod asm;
77

8+
pub mod arty_rgb;
89
mod platform;
910
mod repl;
1011
use alloc::collections::VecDeque;
@@ -77,8 +78,7 @@ pub unsafe extern "C" fn rust_entry() -> ! {
7778
delay(1);
7879
count += 1;
7980

80-
// &-ing to set LD3 to 0 and then setting the value
81-
rgb.rmwf(utra::rgb::OUT_OUT, rgb.r(utra::rgb::OUT) & 0b000111111111 | ((count / 500) as u32) << 9);
81+
rgb.rmwf(arty_rgb::LD3, (count / 500) as u32);
8282
}
8383
}
8484

baremetal/src/repl.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use alloc::vec::Vec;
44
use utralib::*;
55
use xous_bio_bdma::*;
66

7+
use crate::arty_rgb;
8+
79
pub struct Repl {
810
cmdline: String,
911
do_cmd: bool,
@@ -49,7 +51,7 @@ impl Repl {
4951
match cmd.as_str() {
5052
"mon" => {
5153
let bio_ss = BioSharedState::new();
52-
let mut rgb = CSR::new(utra::rgb::HW_RGB_BASE as *mut u32);
54+
let mut rgb = CSR::new(arty_rgb::HW_RGB_BASE as *mut u32);
5355
let mut count = 0;
5456
let mut quit = false;
5557
const TICKS_PER_PRINT: usize = 5;
@@ -65,7 +67,7 @@ impl Repl {
6567
bio_ss.bio.r(utra::bio_bdma::SFR_DBG2),
6668
bio_ss.bio.r(utra::bio_bdma::SFR_DBG3)
6769
);
68-
rgb.wfo(utra::rgb::OUT_OUT, (count / TICKS_PER_PRINT) as u32);
70+
rgb.wfo(arty_rgb::OUT_OUT, (count / TICKS_PER_PRINT) as u32);
6971
}
7072
crate::platform::delay(TICK_MS);
7173
count += 1;
@@ -99,15 +101,14 @@ impl Repl {
99101
let ld_name = &args[0];
100102
let hex_code = &args[1];
101103

102-
// Determine shift from LED name based on the hardware layout.
103-
let shift = match ld_name.as_str() {
104-
"LD0" => 0,
105-
"LD1" => 3,
106-
"LD2" => 6,
104+
let target_led_field = match ld_name.as_str() {
105+
"LD0" => arty_rgb::LD0,
106+
"LD1" => arty_rgb::LD1,
107+
"LD2" => arty_rgb::LD2,
107108
_ => {
108-
crate::println!("Invalid LD name: {}. Use LD0, LD1, or LD2", ld_name);
109-
self.do_cmd = false;
109+
crate::println!("Invalid LED name: {}. Use LD0, LD1, LD2.", ld_name);
110110
self.cmdline.clear();
111+
self.do_cmd = false;
111112
return;
112113
}
113114
};
@@ -126,13 +127,12 @@ impl Repl {
126127
let b_msb = color & 0x000080;
127128
let bgr_val = (b_msb >> 6) | (g_msb >> 13) | (r_msb >> 23);
128129

129-
let mut rgb = CSR::new(utra::rgb::HW_RGB_BASE as *mut u32);
130-
let new_state = bgr_val << shift;
130+
let mut rgb = CSR::new(arty_rgb::HW_RGB_BASE as *mut u32);
131131

132-
rgb.rmwf(utra::rgb::OUT_OUT, rgb.r(utra::rgb::OUT) | new_state);
132+
rgb.rmwf(target_led_field, bgr_val);
133133

134134
crate::println!(
135-
"Set {} to BGR value 0b{:03b} (from hex {}). Other LEDs are off.",
135+
"Set {} to BGR value 0b{:03b} (from hex {}).",
136136
ld_name,
137137
bgr_val,
138138
hex_code
@@ -150,8 +150,8 @@ impl Repl {
150150
crate::println!(
151151
" mon - Monitors the program counters of the BIO cores."
152152
);
153-
crate::println!(" blinky <LD> <RGB_HEX> - Sets an LED to a color (turns others off).");
154-
crate::println!(" LD: LD1, LD2, or LD3");
153+
crate::println!(" blinky <LD> <RGB_HEX> - Sets an LED to a color.");
154+
crate::println!(" LD: LD0, LD1, or LD2");
155155
crate::println!(" RGB_HEX: e.g., ff0000 (red), 00ff00 (green), 0000ff (blue)");
156156
}
157157

0 commit comments

Comments
 (0)