Skip to content

Instantly share code, notes, and snippets.

@undefinedbehavior
Last active January 17, 2017 06:47
Show Gist options
  • Save undefinedbehavior/74ee3155f3eba97cf95975707a292e44 to your computer and use it in GitHub Desktop.
Save undefinedbehavior/74ee3155f3eba97cf95975707a292e44 to your computer and use it in GitHub Desktop.
Rust demo for Pi 3 bare metal
#![feature(asm)]
#![feature(lang_items)]
#![crate_type = "staticlib"]
#![no_std]
const GPIO_BASE: u32 = 0x3F200000; // base address for Pi 2 and Pi 3
fn sleep(value: u32){
for _ in 1..value {
unsafe { asm!("");}
}
}
#[no_mangle]
pub extern fn main() {
let gpio = GPIO_BASE as *const u32;
// Set SEL0 reg, 15-17 bits to 1 to use Pin 15 (1 Pin = 3 bits)
let sel = unsafe { gpio.offset(0) as *mut u32 };
// Set SET0 reg, 5th bit to 1
let on = unsafe { gpio.offset(7) as *mut u32 };
// Set CLR0 reg, 5th bit to 1
let off = unsafe { gpio.offset(10) as *mut u32 };
unsafe {
// use Pin 15
*sel |= 1 << 15;
}
loop {
unsafe {
*off = 1 << 5;
}
sleep(500000);
unsafe {
*on = 1 << 5;
}
sleep(500000);
}
}
#[lang = "eh_personality"]
extern fn eh_personality() {}
#[lang = "panic_fmt"]
extern fn panic_fmt() {}
#[no_mangle]
pub extern fn __aeabi_unwind_cpp_pr0() {}
// rustc --target arm-unknown-linux-gnueabihf -O --emit=obj src/kernel.rs
// arm-none-eabi-gcc -O3 -mcpu=cortex-a53 -mfpu=neon-fp-armv8 -mfloat-abi=hard -funsafe-math-optimizations -nostartfiles kernel.o -o kernel.elf
// arm-none-eabi-objcopy kernel.elf -O binary kernel.img
// copy kernel.img, start.elf, bootcode.bin to SD card
// connect pin 5 to led
// power on the pi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment