#![feature(lang_items, asm)]
#![crate_type = "staticlib"]
#![no_std]

#[no_mangle]
pub extern "C" fn __eabi_unwind_cpp_pr0() { }

#[cfg(not(test))]
#[lang = "eh_personality"]
#[no_mangle]
pub extern "C" fn eh_personality() {}

#[cfg(not(test))]
#[lang = "panic_fmt"]
#[no_mangle]
pub extern "C" fn panic_fmt() -> ! { loop {}}

#[cfg(not(test))]
#[lang = "eh_unwind_resume"]
#[no_mangle]
pub extern "C" fn eh_unwind_resume() { }

#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn _Unwind_Resume() -> ! {
    loop {}
}

const GPIO_BASE: u32 = 0x2020000;

#[no_mangle]
pub extern "C" fn kernel_main() {

    let gpio = GPIO_BASE as *const u32;
    let led_on = unsafe { gpio.offset(8) as *mut u32 };
    let led_off = unsafe { gpio.offset(11) as *mut u32 };

    loop {
        unsafe { *(led_on) = 1 << 15 };
        sleep(500_000);
        unsafe { *(led_off) = 1 << 15 };
        sleep(500_000);

        unsafe { *(led_on) = 1 << 15 };
        sleep(200_000);
        unsafe { *(led_off) = 1 << 15 };
        sleep(200_000);
    }
}

fn sleep(value: u32) {
    for _ in 1..value {
        unsafe { asm!("") }
    }
}

