Skip to content

feat: configurable link section attribute for irqs #718

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

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
- Optimize case change/sanitize
- Fix dangling implicit derives
- Fix escaping <> and & characters in doc attributes
- Add `interrupt_link_section` config parameter for controlling the `#[link_section = "..."]` attribute of `__INTERRUPTS`

## [v0.28.0] - 2022-12-25

Expand Down
34 changes: 32 additions & 2 deletions src/generate/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ pub fn render(
writeln!(device_x, "PROVIDE({name} = DefaultHandler);")?;
}

let link_section_name = config
.interrupt_link_section
.as_deref()
.unwrap_or(".vector_table.interrupts");
let link_section_attr = quote! {
#[link_section = #link_section_name]
};

root.extend(quote! {
#[cfg(feature = "rt")]
extern "C" {
Expand All @@ -132,7 +140,7 @@ pub fn render(

#[cfg(feature = "rt")]
#[doc(hidden)]
#[link_section = ".vector_table.interrupts"]
#link_section_attr
#[no_mangle]
pub static __INTERRUPTS: [Vector; #n] = [
#elements
Expand All @@ -144,6 +152,14 @@ pub fn render(
writeln!(device_x, "PROVIDE({name} = DefaultHandler);").unwrap();
}

let link_section_name = config
.interrupt_link_section
.as_deref()
.unwrap_or(".vector_table.interrupts");
let link_section_attr = quote! {
#[link_section = #link_section_name]
};

root.extend(quote! {
#[cfg(feature = "rt")]
extern "msp430-interrupt" {
Expand All @@ -158,7 +174,7 @@ pub fn render(

#[cfg(feature = "rt")]
#[doc(hidden)]
#[link_section = ".vector_table.interrupts"]
#link_section_attr
#[no_mangle]
#[used]
pub static __INTERRUPTS:
Expand All @@ -172,6 +188,12 @@ pub fn render(
writeln!(device_x, "PROVIDE({name} = DefaultHandler);")?;
}

let link_section_attr = config.interrupt_link_section.as_ref().map(|section| {
quote! {
#[link_section = #section]
}
});

root.extend(quote! {
#[cfg(feature = "rt")]
extern "C" {
Expand All @@ -186,6 +208,7 @@ pub fn render(

#[cfg(feature = "rt")]
#[doc(hidden)]
#link_section_attr
#[no_mangle]
pub static __EXTERNAL_INTERRUPTS: [Vector; #n] = [
#elements
Expand All @@ -197,6 +220,12 @@ pub fn render(
writeln!(device_x, "PROVIDE({name} = DefaultHandler);")?;
}

let link_section_attr = config.interrupt_link_section.as_ref().map(|section| {
quote! {
#[link_section = #section]
}
});

root.extend(quote! {
#[cfg(feature = "rt")]
extern "C" {
Expand All @@ -210,6 +239,7 @@ pub fn render(
}

#[cfg(feature = "rt")]
#link_section_attr
#[doc(hidden)]
pub static __INTERRUPTS: [Vector; #n] = [
#elements
Expand Down
9 changes: 6 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ pub struct Config {
pub source_type: SourceType,
#[cfg_attr(feature = "serde", serde(default))]
pub log_level: Option<String>,
#[cfg_attr(feature = "serde", serde(default))]
pub interrupt_link_section: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -120,6 +122,7 @@ impl Default for Config {
input: None,
source_type: SourceType::default(),
log_level: None,
interrupt_link_section: None,
}
}
}
Expand Down Expand Up @@ -229,15 +232,15 @@ pub trait ToSanitizedCase {
impl ToSanitizedCase for str {
fn to_sanitized_pascal_case(&self) -> Cow<str> {
let s = Case::Pascal.sanitize(self);
if s.as_bytes().get(0).unwrap_or(&0).is_ascii_digit() {
if s.as_bytes().first().unwrap_or(&0).is_ascii_digit() {
Cow::from(format!("_{}", s))
} else {
s
}
}
fn to_sanitized_constant_case(&self) -> Cow<str> {
let s = Case::Constant.sanitize(self);
if s.as_bytes().get(0).unwrap_or(&0).is_ascii_digit() {
if s.as_bytes().first().unwrap_or(&0).is_ascii_digit() {
Cow::from(format!("_{}", s))
} else {
s
Expand All @@ -247,7 +250,7 @@ impl ToSanitizedCase for str {
const INTERNALS: [&str; 4] = ["set_bit", "clear_bit", "bit", "bits"];

let s = Case::Snake.sanitize(self);
if s.as_bytes().get(0).unwrap_or(&0).is_ascii_digit() {
if s.as_bytes().first().unwrap_or(&0).is_ascii_digit() {
format!("_{}", s).into()
} else if INTERNALS.contains(&s.as_ref()) {
s + "_"
Expand Down