~pcein/rust-shakti

1b1aff0c8bce6badafb2cab03de7f2bc4fcfaff5 — Pramode 6 months ago master
First commit
8 files changed, 334 insertions(+), 0 deletions(-)

A .cargo/config
A .gitignore
A Cargo.lock
A Cargo.toml
A build.rs
A memory.x
A src/bin/blink/main.rs
A src/bin/random/main.rs
A  => .cargo/config +8 -0
@@ 1,8 @@
[target.riscv32imac-unknown-none-elf]
rustflags = [
  "-C", "link-arg=-Tmemory.x",
  "-C", "link-arg=-Tlink.x",
]

[build]
target = "riscv32imac-unknown-none-elf"

A  => .gitignore +1 -0
@@ 1,1 @@
/target

A  => Cargo.lock +152 -0
@@ 1,152 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "bare-metal"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3caf393d93b2d453e80638d0674597020cef3382ada454faacd43d1a55a735a"
dependencies = [
 "rustc_version",
]

[[package]]
name = "bit_field"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed8765909f9009617974ab6b7d332625b320b33c326b1e9321382ef1999b5d56"

[[package]]
name = "panic-halt"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812"

[[package]]
name = "proc-macro2"
version = "0.4.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
dependencies = [
 "unicode-xid",
]

[[package]]
name = "quote"
version = "0.6.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"
dependencies = [
 "proc-macro2",
]

[[package]]
name = "r0"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2a38df5b15c8d5c7e8654189744d8e396bddc18ad48041a500ce52d6948941f"

[[package]]
name = "rand"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c618c47cd3ebd209790115ab837de41425723956ad3ce2e6a7f09890947cacb9"
dependencies = [
 "rand_core 0.3.1",
]

[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
 "rand_core 0.4.2",
]

[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"

[[package]]
name = "riscv"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a12a859c86dbbcad4eb18daa0465932d907687abfa909c9cc0ca32dc237c8c3"
dependencies = [
 "bare-metal",
 "bit_field",
]

[[package]]
name = "riscv-rt"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5b540024086582df97c2e52b0f586977b9a14f95d7f6055438b355e9842b319"
dependencies = [
 "r0",
 "riscv",
 "riscv-rt-macros",
]

[[package]]
name = "riscv-rt-macros"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3525f8341898dec060782087b7a15969e1cfe52818afacc47709265c19a23d53"
dependencies = [
 "proc-macro2",
 "quote",
 "rand",
 "syn",
]

[[package]]
name = "rust-shakti"
version = "0.1.0"
dependencies = [
 "panic-halt",
 "riscv-rt",
]

[[package]]
name = "rustc_version"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
dependencies = [
 "semver",
]

[[package]]
name = "semver"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
dependencies = [
 "semver-parser",
]

[[package]]
name = "semver-parser"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"

[[package]]
name = "syn"
version = "0.15.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-xid",
]

[[package]]
name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"

A  => Cargo.toml +11 -0
@@ 1,11 @@
[package]
name = "rust-shakti"
version = "0.1.0"
authors = ["Pramode <mail@pramode.in>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
riscv-rt = "0.6.1"
panic-halt = "0.2.0"

A  => build.rs +20 -0
@@ 1,20 @@
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;

/// Put the linker script somewhere the linker can find it.
fn main() {
    let out_dir = env::var("OUT_DIR").expect("No out dir");
    let dest_path = Path::new(&out_dir);
    let mut f = File::create(&dest_path.join("memory.x"))
        .expect("Could not create file");

    f.write_all(include_bytes!("memory.x"))
        .expect("Could not write file");

    println!("cargo:rustc-link-search={}", dest_path.display());

    println!("cargo:rerun-if-changed=memory.x");
    println!("cargo:rerun-if-changed=build.rs");
}

A  => memory.x +11 -0
@@ 1,11 @@
MEMORY
{
  RAM : ORIGIN = 0x80000000, LENGTH = 128K
}

REGION_ALIAS("REGION_TEXT", RAM);
REGION_ALIAS("REGION_RODATA", RAM);
REGION_ALIAS("REGION_DATA", RAM);
REGION_ALIAS("REGION_BSS", RAM);
REGION_ALIAS("REGION_HEAP", RAM);
REGION_ALIAS("REGION_STACK", RAM);

A  => src/bin/blink/main.rs +55 -0
@@ 1,55 @@
#![no_std]
#![no_main]
#![feature(asm)]

extern crate panic_halt;

use riscv_rt::entry;

const GPIO_DIR_REG: usize = 0x0004_0100;
const GPIO_DATA_REG: usize = 0x0004_0108;
const LED0_R: u32 = 1 << 18;

fn pin_output(pinctrl: u32) {
    unsafe {
        let x = core::ptr::read_volatile(GPIO_DIR_REG as *mut u32);
        core::ptr::write_volatile(GPIO_DIR_REG as *mut u32, x | pinctrl);
    }
}

fn led_on(pinctrl: u32) {
    unsafe {
        let x = core::ptr::read_volatile(GPIO_DATA_REG as *mut u32);
        core::ptr::write_volatile(GPIO_DATA_REG as *mut u32, x | pinctrl);
    }
}

fn led_off(pinctrl: u32) {
    unsafe {
        let x = core::ptr::read_volatile(GPIO_DATA_REG as *mut u32);
        core::ptr::write_volatile(GPIO_DATA_REG as *mut u32, x & !pinctrl);
    }
}

fn delay() {
    let mut x = 100_000_u32;
    while x > 0 {
        x -= 1;
        // We build in release mode, this is required to prevent
        // LLVM from optimizing away the loop.
        unsafe {
            asm!("addi zero, zero, 0");
        }
    }
}

#[entry]
fn main() -> ! {
    pin_output(LED0_R);
    loop {
        led_off(LED0_R);
        delay();
        led_on(LED0_R);
        delay();
    }
}

A  => src/bin/random/main.rs +76 -0
@@ 1,76 @@
#![no_std]
#![no_main]
#![feature(asm)]

extern crate panic_halt;

use riscv_rt::entry;

const GPIO_DIR_REG: usize = 0x0004_0100;
const GPIO_DATA_REG: usize = 0x0004_0108;

const LED0_R: u32 = 1 << 18;
const LED1_G: u32 = 1 << 20;

fn pin_output(pinctrl: u32) {
    unsafe {
        let x = core::ptr::read_volatile(GPIO_DIR_REG as *mut u32);
        core::ptr::write_volatile(GPIO_DIR_REG as *mut u32, x | pinctrl);
    }
}

fn led_on_off(pinctrl: u32, status: u16) {
    unsafe {
        let x = core::ptr::read_volatile(GPIO_DATA_REG as *mut u32);
        if status == 1 {
            core::ptr::write_volatile(GPIO_DATA_REG as *mut u32, x | pinctrl);
        } else {
            core::ptr::write_volatile(GPIO_DATA_REG as *mut u32, x & !pinctrl);
        }
    }
}

fn delay() {
    let mut x = 50000;
    while x > 0 {
        x -= 1;
        unsafe {
            asm!("addi zero, zero, 0");
        }
    }
}

struct Lfsr {
    start: u16,
}

// Just for fun, make it into an iterator!
impl Iterator for Lfsr {
    type Item = u16;

    fn next(&mut self) -> Option<Self::Item> {
        let bit =
            ((self.start >> 0) ^ (self.start >> 2) ^ (self.start >> 3) ^ (self.start >> 5)) & 1;

        self.start = (self.start >> 1) | (bit << 15);
        Some(bit)
    }
}

fn new_lfsr(n: u16) -> Lfsr {
    Lfsr { start: n }
}

#[entry]
fn main() -> ! {
    pin_output(LED0_R);
    pin_output(LED1_G);
    let a = new_lfsr(0x1234);
    let b = new_lfsr(0xabcd);
    for bits in a.zip(b) {
        led_on_off(LED0_R, bits.0);
        led_on_off(LED1_G, bits.1);
        delay();
    }
    loop {}
}