Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring the VP to parity with testing in Keelhaul #1

Merged
merged 12 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ jobs:
- name: Install requirements
run: rustup target add riscv64imac-unknown-none-elf
- name: Build examples
working-directory: ./examples/hello-dla
working-directory: ./examples/hpc/hello-dla
run: cargo build --example dla
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: dla-debug
path: ./examples/hello-dla/target/riscv64imac-unknown-none-elf/debug/examples/dla
path: ./examples/target/riscv64imac-unknown-none-elf/debug/examples/dla
if-no-files-found: error
retention-days: 14

Expand Down
11 changes: 11 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[workspace]
members = [
"headsail-bsp",
"hpc/hello-hpc",
"hpc/hello-dla",
"sysctrl/hello-sysctrl",
]
resolver = "2"

[profile.release]
debug = true
23 changes: 23 additions & 0 deletions examples/headsail-bsp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "headsail-bsp"
version = "0.1.0"
edition = "2021"

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

[features]
hpc-rt = ["rt"]
sysctrl-rt = [
"rt",
"riscv-rt/single-hart",
"riscv/critical-section-single-hart",
]
panic-uart = []

# This is generated by the above options, don't use directly
rt = ["dep:riscv-rt", "dep:riscv"]

[dependencies]
ufmt = "0.2.0"
riscv-rt = { version = "0.12.2", optional = true }
riscv = { version = "0.11.1", optional = true }
File renamed without changes.
75 changes: 75 additions & 0 deletions examples/headsail-bsp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//! A light-weight memory map based board support package for Headsail.
#![no_std]

pub mod sprintln;
pub mod uart;

#[cfg(any(feature = "rt"))]
pub use riscv_rt as rt;
pub use ufmt;

mod mmap;
mod ufmt_panic;

/// # Safety
///
/// Unaligned reads may fail to produce expected results on RISC-V.
#[inline(always)]
pub unsafe fn read_u8(addr: usize) -> u8 {
core::ptr::read_volatile(addr as *const _)
}

/// # Safety
///
/// Unaligned writes may fail to produce expected results on RISC-V.
#[inline(always)]
pub unsafe fn write_u8(addr: usize, val: u8) {
core::ptr::write_volatile(addr as *mut _, val)
}

#[inline(always)]
pub fn read_u32(addr: usize) -> u32 {
unsafe { core::ptr::read_volatile(addr as *const _) }
}

#[inline(always)]
pub fn write_u32(addr: usize, val: u32) {
unsafe { core::ptr::write_volatile(addr as *mut _, val) }
}

#[cfg(feature = "sysctrl-rt")]
#[export_name = "_setup_interrupts"]
fn setup_interrupt_vector() {
use riscv::register::mtvec;

// Set the trap vector
unsafe {
extern "C" {
fn _trap_vector();
}

// Set all the trap vectors for good measure
let bits = _trap_vector as usize;
mtvec::write(bits, mtvec::TrapMode::Vectored);
}
}

// The vector table
//
// Do the ESP trick and route all interrupts to the direct dispatcher.
//
// N.b. vectors length must be exactly 0x80
#[cfg(feature = "sysctrl-rt")]
core::arch::global_asm!(
"
.section .vectors, \"ax\"
.global _trap_vector
// Trap vector base address must always be aligned on a 4-byte boundary
.align 4
_trap_vector:
j _start_trap
.rept 31
.word _start_trap // 1..31
.endr
"
);
File renamed without changes.
38 changes: 38 additions & 0 deletions examples/headsail-bsp/src/sprintln.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::uart::uart_write;

pub struct Uart;

#[macro_export]
macro_rules! sprint {
($s:expr) => {{
use $crate::{sprintln, ufmt};
ufmt::uwrite!(sprintln::Uart {}, $s).unwrap()
}};
($($tt:tt)*) => {{
use $crate::{sprintln, ufmt};
ufmt::uwrite!(sprintln::Uart, $($tt)*).unwrap()
}};
}

#[macro_export]
macro_rules! sprintln {
() => {{
use $crate::sprint;
sprint!("\r\n");
}};
// IMPORTANT use `tt` fragments instead of `expr` fragments (i.e. `$($exprs:expr),*`)
($($tt:tt)*) => {{
use $crate::{sprint, sprintln};
sprint!($($tt)*);
sprint!("\r\n");
}};
}

impl ufmt::uWrite for Uart {
type Error = core::convert::Infallible;

fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
uart_write(s);
Ok(())
}
}
14 changes: 14 additions & 0 deletions examples/headsail-bsp/src/uart.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::{mmap::UART0_ADDR, write_u8};

#[inline]
pub fn uart_write(s: &str) {
for b in s.as_bytes() {
putc(*b);
}
}

#[inline]
fn putc(c: u8) {
// Safety: we don't know if u8 writes work for all target architectures
unsafe { write_u8(UART0_ADDR, c as u8) };
}
26 changes: 26 additions & 0 deletions examples/headsail-bsp/src/ufmt_panic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Set panicking behavior to print into UART

use crate::ufmt::uDisplay;
use core::panic::PanicInfo;

pub(crate) struct PanicInfoWrapper<'a>(pub(crate) &'a PanicInfo<'a>);

impl uDisplay for PanicInfoWrapper<'_> {
fn fmt<W>(&self, f: &mut crate::ufmt::Formatter<'_, W>) -> Result<(), W::Error>
where
W: crate::ufmt::uWrite + ?Sized,
{
if let Some(msg) = self.0.payload().downcast_ref::<&str>() {
f.write_str(&msg)
} else {
f.write_str("panic occurred")
}
}
}

#[cfg(feature = "panic-uart")]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
sprintln!("{}", PanicInfoWrapper(info));
loop {}
}
7 changes: 0 additions & 7 deletions examples/hello-hpc/renode.sh

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "hello-hpc"
name = "hello-dla"
version = "0.1.0"
edition = "2021"

[workspace]

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

[dependencies]
panic-halt = "0.2.0"
riscv-rt = "0.12.2"
headsail-bsp = { version = "0.1.0", path = "../../headsail-bsp", features = [
"hpc-rt",
] }
13 changes: 13 additions & 0 deletions examples/hpc/hello-dla/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! This Cargo build script finds the linker on setups where using a .cargo/config.toml file would
//! be inconvenient, e.g. in a Cargo workspace.

use std::{env, fs, path};

const LINK_SCRIPT: &'static str = "memory.x";

fn main() {
// Put link script in our output directory and ensure it's on the linker search path
let out = &path::PathBuf::from(env::var_os("OUT_DIR").unwrap());
fs::copy(LINK_SCRIPT, out.join(LINK_SCRIPT)).unwrap();
println!("cargo:rustc-link-search={}", out.display());
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![no_std]
#![no_main]

use hello_hpc::{dla_read, dla_write, uart_write};
use headsail_bsp::{rt::entry, sprintln};
use hello_dla::{dla_read, dla_write};
use panic_halt as _;
use riscv_rt::entry;

#[entry]
fn main() -> ! {
uart_write("Hello world!\r\n");
sprintln!("Hello world!");
dla_write("Hello DLA");
let mut buf: [u8; 9] = [0; 9];
dla_read(&mut buf, 9, 0);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ BASEDIR=$(dirname "$0")
BIN=${BIN=$1}
RENODE_PATH=$(dirname $(which renode))
RENODE_PYTHON_PERIPHERALS="$RENODE_PATH/scripts/pydev"
VP_PYTHON_PERIPHERALS="$BASEDIR/../../vp/devel/python_peripherals"
VP_PYTHON_PERIPHERALS="$BASEDIR/../../../vp/devel/python_peripherals"

# Check if symlinks exist
if [ ! -h "$RENODE_PYTHON_PERIPHERALS/DLA.py" ]; then
Expand All @@ -15,4 +15,4 @@ if [ ! -h "$RENODE_PYTHON_PERIPHERALS/DLA.py" ]; then
ln -s $(readlink -f "$VP_PYTHON_PERIPHERALS/DLA.py") "$RENODE_PYTHON_PERIPHERALS/DLA.py"
fi

renode --console -e "set bin @$BIN; include @$BASEDIR/../../scripts/2_run_hpc.resc"
renode --console -e "set bin @$BIN; include @$BASEDIR/../../../scripts/2_run_hpc.resc"
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ name = "hello-hpc"
version = "0.1.0"
edition = "2021"

[workspace]

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

[dependencies]
panic-halt = "0.2.0"
riscv-rt = "0.12.2"
headsail-bsp = { version = "0.1.0", path = "../../headsail-bsp", features = [
"hpc-rt",
] }
13 changes: 13 additions & 0 deletions examples/hpc/hello-hpc/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! This Cargo build script finds the linker on setups where using a .cargo/config.toml file would
//! be inconvenient, e.g. in a Cargo workspace.

use std::{env, fs, path};

const LINK_SCRIPT: &'static str = "memory.x";

fn main() {
// Put link script in our output directory and ensure it's on the linker search path
let out = &path::PathBuf::from(env::var_os("OUT_DIR").unwrap());
fs::copy(LINK_SCRIPT, out.join(LINK_SCRIPT)).unwrap();
println!("cargo:rustc-link-search={}", out.display());
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#![no_std]
#![no_main]

use hello_hpc::uart_write;
use headsail_bsp::{rt::entry, uart::uart_write};
use panic_halt as _;
use riscv_rt::entry;

#[entry]
fn main() -> ! {
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions examples/hpc/hello-hpc/renode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
set -x # echo on

BASEDIR=$(dirname "$0")
BIN=${BIN=$1}

renode --console -e "set bin @$BIN; include @$BASEDIR/../../../scripts/2_run_hpc.resc"
Empty file.
File renamed without changes.
1 change: 1 addition & 0 deletions examples/hpc/hello-hpc/src/mmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) const UART0_ADDR: usize = 0xFFF00000;
6 changes: 6 additions & 0 deletions examples/sysctrl/hello-sysctrl/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[build]
target = "riscv32imc-unknown-none-elf"

[target.riscv32imc-unknown-none-elf]
runner = "./renode.sh"
rustflags = ["-C", "link-arg=-Tmemory.x", "-C", "link-arg=-Tlink.x"]
12 changes: 12 additions & 0 deletions examples/sysctrl/hello-sysctrl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "hello-sysctrl"
version = "0.1.0"
edition = "2021"

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

[dependencies]
headsail-bsp = { version = "0.1.0", path = "../../headsail-bsp", features = [
"sysctrl-rt",
] }
panic-halt = "0.2.0"
13 changes: 13 additions & 0 deletions examples/sysctrl/hello-sysctrl/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! This Cargo build script finds the linker on setups where using a .cargo/config.toml file would
//! be inconvenient, e.g. in a Cargo workspace.

use std::{env, fs, path};

const LINK_SCRIPT: &'static str = "memory.x";

fn main() {
// Put link script in our output directory and ensure it's on the linker search path
let out = &path::PathBuf::from(env::var_os("OUT_DIR").unwrap());
fs::copy(LINK_SCRIPT, out.join(LINK_SCRIPT)).unwrap();
println!("cargo:rustc-link-search={}", out.display());
}
3 changes: 3 additions & 0 deletions examples/sysctrl/hello-sysctrl/connect-and-load.gdb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Sysctrl is exposed at to 3333
target remote :3333
backtrace
11 changes: 11 additions & 0 deletions examples/sysctrl/hello-sysctrl/examples/uart0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![no_std]
#![no_main]

use headsail_bsp::{rt::entry, uart::uart_write};
use panic_halt as _;

#[entry]
fn main() -> ! {
uart_write("Hello world!");
loop {}
}
12 changes: 12 additions & 0 deletions examples/sysctrl/hello-sysctrl/memory.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
MEMORY
{
BANK0 : ORIGIN = 0x1c000000, LENGTH = 0x8000
BANK1 : ORIGIN = 0x1c008000, LENGTH = 0x8000
}

REGION_ALIAS("REGION_TEXT", BANK0);
REGION_ALIAS("REGION_DATA", BANK0);
REGION_ALIAS("REGION_RODATA", BANK1);
REGION_ALIAS("REGION_BSS", BANK1);
REGION_ALIAS("REGION_HEAP", BANK1);
REGION_ALIAS("REGION_STACK", BANK1);
Loading