Skip to content

Commit c290d5e

Browse files
authored
Add unstable feature to esp-hal-procmacros (#4309)
* works? * changelog + migrating guide + fmt ugh * address reviews
1 parent 76f4ae4 commit c290d5e

File tree

7 files changed

+118
-26
lines changed

7 files changed

+118
-26
lines changed

esp-hal-procmacros/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414

1515
### Changed
1616

17+
- All `ram` proc macro options except `reclaimed` are considered `unstable` (#4309)
18+
1719
### Fixed
1820

1921
- Replaced `embassy_main` with `rtos_main` (intended to be called as `esp_rtos::main`) (#4172)

esp-hal-procmacros/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ mod rtos_main;
9898
/// # Examples
9999
///
100100
/// ```rust, no_run
101-
/// #[ram(rtc_fast)]
101+
/// #[ram(unstable(rtc_fast))]
102102
/// static mut SOME_INITED_DATA: [u8; 2] = [0xaa, 0xbb];
103103
///
104-
/// #[ram(rtc_fast, persistent)]
104+
/// #[ram(unstable(rtc_fast, persistent))]
105105
/// static mut SOME_PERSISTENT_DATA: [u8; 2] = [0; 2];
106106
///
107-
/// #[ram(rtc_fast, zeroed)]
107+
/// #[ram(unstable(rtc_fast, zeroed))]
108108
/// static mut SOME_ZEROED_DATA: [u8; 8] = [0; 8];
109109
/// ```
110110
///

esp-hal-procmacros/src/ram.rs

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use proc_macro::TokenStream;
22
use proc_macro2::{Ident, Span};
3-
use syn::{Item, Token, parse, parse::Parser, punctuated::Punctuated};
3+
use syn::{Item, Token, parse, parse::Parser, punctuated::Punctuated, spanned::Spanned};
44

55
pub fn ram(args: TokenStream, input: TokenStream) -> TokenStream {
66
let attr_args = match Punctuated::<syn::Meta, Token![,]>::parse_terminated.parse2(args.into()) {
@@ -15,30 +15,99 @@ pub fn ram(args: TokenStream, input: TokenStream) -> TokenStream {
1515
let mut zeroed = false;
1616

1717
for attr_arg in &attr_args {
18-
if let syn::Meta::Path(path) = attr_arg {
19-
let ident = match path.require_ident() {
20-
Ok(i) => i,
21-
Err(e) => return e.into_compile_error().into(),
22-
};
23-
let arg = match ident {
24-
i if i == "rtc_fast" => &mut rtc_fast,
25-
i if i == "rtc_slow" => &mut rtc_slow,
26-
i if i == "reclaimed" => &mut dram2_uninit,
27-
i if i == "persistent" => &mut persistent,
28-
i if i == "zeroed" => &mut zeroed,
29-
i => {
30-
return syn::Error::new(i.span(), format!("Unknown argument `{i}`"))
18+
match attr_arg {
19+
syn::Meta::List(list) if list.path.is_ident("unstable") => {
20+
let nested = &list.tokens;
21+
let nested_args = match Punctuated::<syn::Meta, Token![,]>::parse_terminated
22+
.parse2(nested.clone())
23+
{
24+
Ok(v) => v,
25+
Err(e) => return e.to_compile_error().into(),
26+
};
27+
28+
for meta in nested_args {
29+
match meta {
30+
syn::Meta::Path(path) => {
31+
let Some(ident) = path.get_ident() else {
32+
return syn::Error::new(
33+
path.span(),
34+
"Expected identifier inside `unstable(...)`",
35+
)
36+
.into_compile_error()
37+
.into();
38+
};
39+
let arg = match ident {
40+
i if i == "rtc_fast" => &mut rtc_fast,
41+
i if i == "rtc_slow" => &mut rtc_slow,
42+
i if i == "persistent" => &mut persistent,
43+
i if i == "zeroed" => &mut zeroed,
44+
i => {
45+
return syn::Error::new(
46+
i.span(),
47+
format!("Unknown unstable argument `{i}`"),
48+
)
49+
.into_compile_error()
50+
.into();
51+
}
52+
};
53+
54+
if *arg {
55+
return syn::Error::new(
56+
ident.span(),
57+
format!("Argument `{ident}` is already set"),
58+
)
59+
.into_compile_error()
60+
.into();
61+
}
62+
63+
*arg = true;
64+
}
65+
_ => {
66+
return syn::Error::new(
67+
list.span(),
68+
"Expected identifiers inside `unstable(...)`",
69+
)
70+
.into_compile_error()
71+
.into();
72+
}
73+
}
74+
}
75+
}
76+
77+
syn::Meta::Path(path) => {
78+
let Some(ident) = path.get_ident() else {
79+
return syn::Error::new(path.span(), "Expected identifier")
80+
.into_compile_error()
81+
.into();
82+
};
83+
let arg = match ident {
84+
i if i == "reclaimed" => &mut dram2_uninit,
85+
_ => {
86+
return syn::Error::new(
87+
ident.span(),
88+
format!("`{ident}` must be wrapped in `unstable(...)`"),
89+
)
3190
.into_compile_error()
3291
.into();
92+
}
93+
};
94+
95+
if *arg {
96+
return syn::Error::new(
97+
ident.span(),
98+
format!("Argument `{ident}` is already set"),
99+
)
100+
.into_compile_error()
101+
.into();
33102
}
34-
};
103+
*arg = true;
104+
}
35105

36-
if *arg {
37-
return syn::Error::new(ident.span(), format!("Argument `{ident}` is already set"))
106+
_ => {
107+
return syn::Error::new(attr_arg.span(), "Unsupported attribute syntax for `ram`")
38108
.into_compile_error()
39109
.into();
40110
}
41-
*arg = true;
42111
}
43112
}
44113

esp-hal/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8686
- RMT: `Channel::transmit_continuously` now verifies that loop counts don't exceed the hardware limit (#4276)
8787
- RMT: Receive operations read only received codes instead of the entire buffer and return the number of codes read (#4049)
8888
- `esp_hal::clock::{RtcFastClock, RtcSlowClock, RtcClock}` and `esp_hal::gpio::Event` have been marked unstable (#4293)
89+
- All `ram` proc macro options except `#[ram(reclaimed)]` are considered `unstable` (#4309)
8990

9091
### Fixed
9192

esp-hal/MIGRATING-1.0.0-rc.0.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,23 @@ esp_rtos::start_second_core(
433433
### Interrupt executor changes
434434

435435
Interrupt executors are provided as `esp_rtos::embassy::InterruptExecutor` with no additional changes.
436+
437+
438+
### `ram` procmacro changes
439+
440+
All `ram` proc macro options except `#[ram(reclaimed)]` are considered `unstable` and should be used in a following way:
441+
442+
```diff
443+
- #[ram(rtc_fast)]
444+
+ #[ram(unstable(rtc_fast))]
445+
static mut SOME_INITED_DATA: [u8; 2] = [0xaa, 0xbb];
446+
447+
- #[ram(rtc_fast, persistent)]
448+
+ #[ram(unstable(rtc_fast, persistent))]
449+
static mut SOME_PERSISTENT_DATA: [u8; 2] = [0; 2];
450+
451+
- #[ram(rtc_fast, zeroed)]
452+
+ #[ram(unstable(rtc_fast, zeroed))]
453+
static mut SOME_ZEROED_DATA: [u8; 8] = [0; 8];
454+
455+
```

esp-hal/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ pub(crate) mod private {
535535
#[doc(hidden)]
536536
pub use private::Internal;
537537

538-
/// Marker trait for types that can be safely used in `#[ram(persistent)]`.
538+
/// Marker trait for types that can be safely used in `#[ram(unstable(persistent))]`.
539539
///
540540
/// # Safety
541541
///

qa-test/src/bin/ram.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ use esp_println::println;
2323

2424
esp_bootloader_esp_idf::esp_app_desc!();
2525

26-
#[ram(rtc_fast)]
26+
#[ram(unstable(rtc_fast))]
2727
static mut SOME_INITED_DATA: [u8; 2] = [0xaa, 0xbb];
2828

29-
#[ram(rtc_fast, persistent)]
29+
#[ram(unstable(rtc_fast, persistent))]
3030
static mut SOME_PERSISTENT_DATA: [u8; 2] = [0; 2];
3131

32-
#[ram(rtc_fast, zeroed)]
32+
#[ram(unstable(rtc_fast, zeroed))]
3333
static mut SOME_ZEROED_DATA: [u8; 8] = [0; 8];
3434

3535
#[main]
@@ -87,7 +87,7 @@ fn function_in_ram(count: u32) {
8787
println!("{}", count);
8888
}
8989

90-
#[ram(rtc_fast)]
90+
#[ram(unstable(rtc_fast))]
9191
fn function_in_rtc_ram() -> u32 {
9292
42
9393
}

0 commit comments

Comments
 (0)