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 cortex_m::asm;
use embedded_hal;
use hal::clock::GenericClockController;
use hal::delay::Delay;
use hal::entry;
@ -18,186 +17,47 @@ use hal::pac::{CorePeripherals, Peripherals};
use hal::prelude::*;
macro_rules! write_rgb_zero {
($pin:expr, $delay:expr) => {
($pin:expr) => {
$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();
$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();
// 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 {
($pin:expr, $delay:expr) => {
($pin:expr) => {
$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();
}
$pin.set_low().unwrap();
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();
for _ in 0..28 {
asm::nop();
}
};
}
// #[proc_macro]
// pub fn write_rgb(item: TokenStream) -> TokenStream {
// item
// }
// macro_rules! write_rgb {
// ($pin:expr, $delay:expr, $uint8:expr) => {
// for i in (0..8).rev() {
// if ($uint8 >> i) & 1 == 1 {
// write_rgb_one!($pin, $delay);
// } else {
// write_rgb_zero!($pin, $delay);
// }
// }
// };
// }
macro_rules! write_rgb {
($pin:expr, $uint8:expr) => {
for i in (0..8).rev() {
if ($uint8 >> i) & 1 == 1 {
write_rgb_one!($pin);
} else {
write_rgb_zero!($pin);
}
}
};
}
#[entry]
fn main() -> ! {
@ -210,9 +70,14 @@ fn main() -> ! {
&mut peripherals.NVMCTRL,
);
let mut pins = hal::Pins::new(peripherals.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 col1 = pins.d6.into_open_drain_output(&mut pins.port);
let mut col2 = pins.d7.into_open_drain_output(&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);
@ -221,15 +86,23 @@ fn main() -> ! {
loop {
delay.delay_us(255u8);
assert_pin.set_high().unwrap();
if switch.is_high().unwrap() {
red_led.set_high().unwrap();
} else {
red_led.set_low().unwrap();
}
// lol(&mut rgb_meme, &mut delay, 20, 0, 0);
assert_pin.set_low().unwrap();
lol(&mut rgb_meme, &mut delay);
col1.set_high().unwrap();
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(
pin: &mut metro_m0::gpio::Pa30<metro_m0::gpio::Output<metro_m0::gpio::PushPull>>,
delay: &mut hal::delay::Delay,
r: u8,
g: u8,
b: u8,
) {
pin.set_low().unwrap();
delay.delay_us(60u8);
// write_rgb!(pin, delay, 10);
// write_rgb!(pin, delay, 0);
// write_rgb!(pin, delay, 0);
// 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);
write_rgb!(pin, g);
write_rgb!(pin, r);
write_rgb!(pin, b);
}