Compare commits

..

2 Commits

Author SHA1 Message Date
kirbylife 24daab9c17 add comments 2022-12-10 23:48:27 -06:00
kirbylife 76102d28a4 now use the read_volatile and xor op to flip the bit value 2022-12-10 00:45:48 -06:00
2 changed files with 39 additions and 12 deletions

View File

@ -4,8 +4,6 @@ version = "0.1.0"
edition = "2021"
[dependencies]
arduino-hal = { git = "https://github.com/rahix/avr-hal", features = ["arduino-uno"] }
panic-halt = "0.2.0"
[profile.release]
lto = true

View File

@ -1,19 +1,48 @@
#![feature(asm_experimental_arch)]
#![no_std]
#![no_main]
use arduino_hal::delay_ms;
use panic_halt as _;
use core::arch::asm;
use core::ptr::{read_volatile, write_volatile};
#[arduino_hal::entry]
fn main() -> ! {
let peripherals = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(peripherals);
/*
PORTC{0..5} Analog
PORTC6 Reset
let mut led = pins.d13.into_output();
led.set_high();
PORTB{0..5} Digital {8..13}
PORTD{0..7} Digital {0..7}
PORTB5 LED_BUILTIN
*/
const DDRB: *mut u8 = 0x24 as *mut u8;
const PORTB: *mut u8 = 0x25 as *mut u8;
// ATmega328p cycle speed
const CPU_SPEED: u32 = 16_000_000;
#[no_mangle]
pub extern "C" fn main() -> ! {
// Set all the B pins on OUTPUT mode
unsafe { write_volatile(DDRB, 0b11111111) };
loop {
led.toggle();
delay_ms(1000);
// if pin[5] == 1: 1^1 = 0
// if pin[5] == 0: 0^1 = 1
let mut portb_value = unsafe { read_volatile(PORTB) };
portb_value = portb_value ^ (1 << 5);
unsafe { write_volatile(PORTB, portb_value) };
sleep(1);
}
}
fn sleep(time: u32) {
for _ in 0..(CPU_SPEED / 10 * time) {
unsafe { asm!("nop") }
}
}
#[panic_handler]
fn panic(_info: &::core::panic::PanicInfo) -> ! {
loop {}
}