Skip to content

Instantly share code, notes, and snippets.

@naotaco
Created December 12, 2017 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naotaco/676f8e2a9b2f664882175ae1ec60f8fd to your computer and use it in GitHub Desktop.
Save naotaco/676f8e2a9b2f664882175ae1ec60f8fd to your computer and use it in GitHub Desktop.
main.rs
// Originally published here: https://github.com/avr-rust/blink
#![feature(asm, lang_items, unwind_attributes)]
#![no_std]
#![no_main]
extern crate arduino;
use arduino::{DDRB, PORTB};
use core::ptr::write_volatile;
#[no_mangle]
pub extern fn main() {
// Set all PORTB pins up as outputs
unsafe { write_volatile(DDRB, 0xFF) }
loop {
// Set all pins on PORTB to high.
unsafe { write_volatile(PORTB, 0xFF) }
small_delay();
// Set all pins on PORTB to low.
unsafe { write_volatile(PORTB, 0x00) }
small_delay();
}
}
/// A small busy loop.
fn small_delay() {
for _ in 0..400000 {
unsafe { asm!("" :::: "volatile")}
}
}
// These do not need to be in a module, but we group them here for clarity.
pub mod std {
#[lang = "eh_personality"]
#[no_mangle]
pub unsafe extern "C" fn rust_eh_personality(_state: (), _exception_object: *mut (), _context: *mut ()) -> () {
}
#[lang = "panic_fmt"]
#[unwind]
pub extern fn rust_begin_panic(_msg: (), _file: &'static str, _line: u32) -> ! {
loop { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment