Skip to content

Commit 663c07f

Browse files
committed
adds a blinky function to the LEDs on the arty board using bio
1 parent 1bff1ee commit 663c07f

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

baremetal/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn uart_irq_handler() {
4444
pub unsafe extern "C" fn rust_entry() -> ! {
4545
// turn on a green LED to indicate boot
4646
let mut rgb = CSR::new(utra::rgb::HW_RGB_BASE as *mut u32);
47-
rgb.wfo(utra::rgb::OUT_OUT, 0x002);
47+
rgb.wfo(utra::rgb::OUT_OUT, 0b010000000000);
4848

4949
crate::platform::early_init();
5050
crate::println!("\n~~Baremetal up!~~\n");
@@ -76,7 +76,9 @@ pub unsafe extern "C" fn rust_entry() -> ! {
7676
// Animate the LED flashing to indicate repl loop is running
7777
delay(1);
7878
count += 1;
79-
rgb.wfo(utra::rgb::OUT_OUT, ((count / 500) as u32) << 6);
79+
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);
8082
}
8183
}
8284

baremetal/src/repl.rs

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,81 @@ impl Repl {
8585
}
8686
crate::println!("");
8787
}
88+
89+
"blinky" => {
90+
if args.len() != 2 {
91+
crate::println!("Usage: blinky <LED_NAME> <RGB_HEX_VALUE>");
92+
crate::println!("Example: blinky LD2 ff0000");
93+
crate::println!("Available LEDs: LD0, LD1, LD2");
94+
self.do_cmd = false;
95+
self.cmdline.clear();
96+
return;
97+
}
98+
99+
let ld_name = &args[0];
100+
let hex_code = &args[1];
101+
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,
107+
_ => {
108+
crate::println!("Invalid LD name: {}. Use LD0, LD1, or LD2", ld_name);
109+
self.do_cmd = false;
110+
self.cmdline.clear();
111+
return;
112+
}
113+
};
114+
115+
let color_val = if hex_code.starts_with("0x") {
116+
u32::from_str_radix(&hex_code[2..], 16)
117+
} else {
118+
u32::from_str_radix(hex_code, 16)
119+
};
120+
121+
match color_val {
122+
Ok(color) => {
123+
// convert 24-bit RRGGBB into 3-bit BGR
124+
let r_msb = color & 0x800000;
125+
let g_msb = color & 0x008000;
126+
let b_msb = color & 0x000080;
127+
let bgr_val = (b_msb >> 6) | (g_msb >> 13) | (r_msb >> 23);
128+
129+
let mut rgb = CSR::new(utra::rgb::HW_RGB_BASE as *mut u32);
130+
let new_state = bgr_val << shift;
131+
132+
rgb.rmwf(utra::rgb::OUT_OUT, rgb.r(utra::rgb::OUT) | new_state);
133+
134+
crate::println!(
135+
"Set {} to BGR value 0b{:03b} (from hex {}). Other LEDs are off.",
136+
ld_name,
137+
bgr_val,
138+
hex_code
139+
);
140+
}
141+
Err(_) => {
142+
crate::println!("Invalid hex color value: {}", hex_code);
143+
}
144+
}
145+
}
146+
"help" => {
147+
crate::println!("Available commands:");
148+
crate::println!(" help - Shows this help message.");
149+
crate::println!(" echo [ARGS] - Prints the arguments to the console.");
150+
crate::println!(
151+
" mon - Monitors the program counters of the BIO cores."
152+
);
153+
crate::println!(" blinky <LD> <RGB_HEX> - Sets an LED to a color (turns others off).");
154+
crate::println!(" LD: LD1, LD2, or LD3");
155+
crate::println!(" RGB_HEX: e.g., ff0000 (red), 00ff00 (green), 0000ff (blue)");
156+
}
157+
158+
"" => {}
159+
88160
_ => {
89-
crate::println!("Command not recognized: {}", cmd);
90-
crate::println!("Commands include: echo, mon");
161+
crate::println!("Command not recognized: '{}'", cmd);
162+
crate::println!("Type 'help' for a list of commands.");
91163
}
92164
}
93165

0 commit comments

Comments
 (0)