nop hacking

This commit is contained in:
Edward Shen 2020-02-06 19:21:26 -05:00
parent 1291a267d8
commit 959cd2c4cc
No known key found for this signature in database
GPG Key ID: 19182661E818369F
1 changed files with 57 additions and 209 deletions

View File

@ -10,7 +10,6 @@ extern crate panic_halt; // you can put a breakpoint on `rust_begin_unwind` to c
use metro_m0 as hal; use metro_m0 as hal;
use cortex_m::asm; use cortex_m::asm;
use embedded_hal;
use hal::clock::GenericClockController; use hal::clock::GenericClockController;
use hal::delay::Delay; use hal::delay::Delay;
use hal::entry; use hal::entry;
@ -18,186 +17,47 @@ use hal::pac::{CorePeripherals, Peripherals};
use hal::prelude::*; use hal::prelude::*;
macro_rules! write_rgb_zero { macro_rules! write_rgb_zero {
($pin:expr, $delay:expr) => { ($pin:expr) => {
$pin.set_high().unwrap(); $pin.set_high().unwrap();
for _ in 0..4 {
// for _ in 0..12 {
asm::nop(); asm::nop();
asm::nop(); }
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
$pin.set_low().unwrap(); $pin.set_low().unwrap();
for _ in 0..48 {
asm::nop(); asm::nop();
asm::nop(); }
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
// 12
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
// 24
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
// 36
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
}; };
} }
macro_rules! write_rgb_one { macro_rules! write_rgb_one {
($pin:expr, $delay:expr) => { ($pin:expr) => {
$pin.set_high().unwrap(); $pin.set_high().unwrap();
for _ in 0..20 {
// for _ iQn 0..28 {
asm::nop(); asm::nop();
asm::nop(); }
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
$pin.set_low().unwrap(); $pin.set_low().unwrap();
for _ in 0..28 {
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop();
asm::nop(); asm::nop();
}
}; };
} }
// #[proc_macro] macro_rules! write_rgb {
// pub fn write_rgb(item: TokenStream) -> TokenStream { ($pin:expr, $uint8:expr) => {
// item for i in (0..8).rev() {
// } if ($uint8 >> i) & 1 == 1 {
write_rgb_one!($pin);
// macro_rules! write_rgb { } else {
// ($pin:expr, $delay:expr, $uint8:expr) => { write_rgb_zero!($pin);
// for i in (0..8).rev() { }
// if ($uint8 >> i) & 1 == 1 { }
// write_rgb_one!($pin, $delay); };
// } else { }
// write_rgb_zero!($pin, $delay);
// }
// }
// };
// }
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
@ -210,9 +70,14 @@ fn main() -> ! {
&mut peripherals.NVMCTRL, &mut peripherals.NVMCTRL,
); );
let mut pins = hal::Pins::new(peripherals.PORT); let mut pins = hal::Pins::new(peripherals.PORT);
let mut red_led = pins.d13.into_push_pull_output(&mut pins.port); let mut col1 = pins.d6.into_open_drain_output(&mut pins.port);
let mut assert_pin = pins.d12.into_push_pull_output(&mut pins.port); let mut col2 = pins.d7.into_open_drain_output(&mut pins.port);
let switch = pins.d3.into_pull_down_input(&mut pins.port); let row1 = pins.d11.into_pull_down_input(&mut pins.port);
let row2 = pins.d12.into_pull_down_input(&mut pins.port);
// let mut red_led = pins.d13.into_push_pull_output(&mut pins.port);
// let mut assert_pin = pins.d12.into_push_pull_output(&mut pins.port);
// let switch = pins.d3.into_pull_down_input(&mut pins.port);
let mut rgb_meme = pins.neopixel.into_push_pull_output(&mut pins.port); let mut rgb_meme = pins.neopixel.into_push_pull_output(&mut pins.port);
@ -221,15 +86,23 @@ fn main() -> ! {
loop { loop {
delay.delay_us(255u8); delay.delay_us(255u8);
assert_pin.set_high().unwrap(); // lol(&mut rgb_meme, &mut delay, 20, 0, 0);
if switch.is_high().unwrap() {
red_led.set_high().unwrap();
} else {
red_led.set_low().unwrap();
}
assert_pin.set_low().unwrap(); col1.set_high().unwrap();
lol(&mut rgb_meme, &mut delay); if row1.is_high().unwrap() {
lol(&mut rgb_meme, &mut delay, 0, 0, 1);
} else if row2.is_high().unwrap() {
lol(&mut rgb_meme, &mut delay, 1, 1, 0);
}
col1.set_low().unwrap();
col2.set_high().unwrap();
if row1.is_high().unwrap() {
lol(&mut rgb_meme, &mut delay, 0, 1, 0);
} else if row2.is_high().unwrap() {
lol(&mut rgb_meme, &mut delay, 1, 0, 0);
}
col2.set_low().unwrap();
} }
} }
@ -237,39 +110,14 @@ fn main() -> ! {
fn lol( fn lol(
pin: &mut metro_m0::gpio::Pa30<metro_m0::gpio::Output<metro_m0::gpio::PushPull>>, pin: &mut metro_m0::gpio::Pa30<metro_m0::gpio::Output<metro_m0::gpio::PushPull>>,
delay: &mut hal::delay::Delay, delay: &mut hal::delay::Delay,
r: u8,
g: u8,
b: u8,
) { ) {
pin.set_low().unwrap(); pin.set_low().unwrap();
delay.delay_us(60u8); delay.delay_us(60u8);
// write_rgb!(pin, delay, 10); write_rgb!(pin, g);
// write_rgb!(pin, delay, 0); write_rgb!(pin, r);
// write_rgb!(pin, delay, 0); write_rgb!(pin, b);
// write_rgb!(pin, delay, 128);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_zero!(pin, delay);
write_rgb_one!(pin, delay);
} }