From 1f09c125820eefa1520bc6d526d4206db422d060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rom=C3=A1n=20C=C3=A1rdenas=20Rodr=C3=ADguez?= Date: Sat, 8 Feb 2025 12:20:13 +0100 Subject: [PATCH] Add mtvec_align for RISC-V --- CHANGELOG.md | 1 + src/config.rs | 9 ++++++++- src/config/riscv.rs | 19 +++++++++++++++++++ src/lib.rs | 2 +- src/main.rs | 6 +++++- src/util.rs | 6 +++++- 6 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 312c949a..6604f511 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/). ## [Unreleased] +- Add `mtvec_align` field to `riscv_config` to configure the byte alignment of interrupt vector table. - Fix reexport path when "%s" inside "derivedFrom" - Force using rust edition 2021 in CI - Added lifetime ellision for `FieldWriter` where the explicit lifetimes are not necessary, which diff --git a/src/config.rs b/src/config.rs index ca6ced37..507e799a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,5 @@ use anyhow::{bail, Result}; -use proc_macro2::Span; +use proc_macro2::{Span, TokenStream}; use std::{ collections::HashMap, ops::{Deref, DerefMut}, @@ -345,6 +345,13 @@ impl Settings { self.riscv_config = source.riscv_config; } } + + pub fn extra_build(&self) -> Option { + match self.riscv_config.as_ref() { + Some(riscv_config) => riscv_config.extra_build(), + None => None, + } + } } #[derive(Clone, PartialEq, Eq, Debug)] diff --git a/src/config/riscv.rs b/src/config/riscv.rs index 2091bc2f..90100762 100644 --- a/src/config/riscv.rs +++ b/src/config/riscv.rs @@ -1,3 +1,6 @@ +use proc_macro2::TokenStream; +use quote::quote; + #[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))] #[derive(Clone, PartialEq, Eq, Debug, Default)] #[non_exhaustive] @@ -8,6 +11,22 @@ pub struct RiscvConfig { pub harts: Vec, pub clint: Option, pub plic: Option, + pub mtvec_align: Option, +} + +impl RiscvConfig { + pub fn extra_build(&self) -> Option { + self.mtvec_align.map(|align| { + quote! { + // set environment variable RISCV_MTVEC_ALIGN enfoce correct byte alignment of interrupt vector. + println!( + "cargo:rustc-env=RISCV_MTVEC_ALIGN={}", + #align + ); + println!("cargo:rerun-if-env-changed=RISCV_MTVEC_ALIGN"); + } + }) + } } #[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))] diff --git a/src/lib.rs b/src/lib.rs index b5d5c4ab..3a800b6f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -671,7 +671,7 @@ pub fn generate(input: &str, config: &Config) -> Result { } else { Some(DeviceSpecific { device_x, - build_rs: util::build_rs().to_string(), + build_rs: util::build_rs(&config.settings).to_string(), }) }; diff --git a/src/main.rs b/src/main.rs index 27607bfc..80800d0c 100755 --- a/src/main.rs +++ b/src/main.rs @@ -366,7 +366,11 @@ Ignore this option if you are not building your own FPGA based soft-cores."), .contains(&config.target) { writeln!(File::create(path.join("device.x"))?, "{device_x}")?; - writeln!(File::create(path.join("build.rs"))?, "{}", build_rs())?; + writeln!( + File::create(path.join("build.rs"))?, + "{}", + build_rs(&config.settings) + )?; } if config.feature_group || config.feature_peripheral { diff --git a/src/util.rs b/src/util.rs index fd7bb3c2..6874ce06 100644 --- a/src/util.rs +++ b/src/util.rs @@ -399,7 +399,9 @@ impl U32Ext for u32 { } } -pub fn build_rs() -> TokenStream { +pub fn build_rs(settings: &crate::Settings) -> TokenStream { + let extra_build = settings.extra_build(); + quote! { //! Builder file for Peripheral access crate generated by svd2rust tool @@ -419,6 +421,8 @@ pub fn build_rs() -> TokenStream { println!("cargo:rustc-link-search={}", out.display()); println!("cargo:rerun-if-changed=device.x"); + + #extra_build } println!("cargo:rerun-if-changed=build.rs");