Created
January 31, 2018 04:15
-
-
Save alexjh/f21b4603ae5e97c1f1afdd75e4e42156 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(asm, lang_items, unwind_attributes, abi_avr_interrupt)] | |
#![no_std] | |
#![no_main] | |
extern crate arduino; | |
use arduino::*; | |
use core::ptr::write_volatile; | |
use core::ptr::read_volatile; | |
#[no_mangle] | |
pub extern fn main() { | |
init_timer(); | |
unsafe { | |
asm!("CLI"); | |
//asm!("SEI"); | |
} | |
unsafe { write_volatile(PORTB, read_volatile(PORTB) ^ PORTB5) } | |
small_delay(10000); | |
unsafe { write_volatile(PORTB, read_volatile(PORTB) ^ PORTB5) } | |
small_delay(10000); | |
unsafe { write_volatile(PORTB, read_volatile(PORTB) ^ PORTB5) } | |
loop { | |
} | |
} | |
fn init_timer() { | |
const CPU_FREQUENCY_HZ: u64 = 16_000_000; | |
const DESIRED_HZ_TIM1: f64 = 2.0; | |
const TIM1_PRESCALER: u64 = 1024; | |
const INTERRUPT_EVERY_1_HZ_1024_PRESCALER: u16 = | |
((CPU_FREQUENCY_HZ as f64 / (DESIRED_HZ_TIM1 * TIM1_PRESCALER as f64)) as u64 - 1) as u16; | |
timer1::Timer::new() | |
.waveform_generation_mode(timer1::WaveformGenerationMode::ClearOnTimerMatchOutputCompare) | |
.clock_source(timer1::ClockSource::Prescale1024) | |
.output_compare_1(Some(INTERRUPT_EVERY_1_HZ_1024_PRESCALER)) | |
.configure(); | |
} | |
/// A small busy loop. | |
fn small_delay(val: u16) { | |
for _ in 0..val { | |
unsafe { asm!("" :::: "volatile")} | |
} | |
} | |
#[no_mangle] | |
pub unsafe extern "avr-interrupt" fn _ivr_timer1_compare_a() { | |
let prev_value = read_volatile(PORTB); | |
write_volatile(PORTB, prev_value ^ PINB5); | |
} | |
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