Skip to content

Commit f6d54a0

Browse files
authored
Generate macros from metadata (#3604)
* Generate macros from metadata * Don't generate metadata env vars * Add trm link, change cores to integer * Port RMT memory constants * Add some I2C flags * Auto-generate PeriConfig, remove trait * Make sure memory_range is hidden
1 parent 7c322ec commit f6d54a0

File tree

30 files changed

+335
-248
lines changed

30 files changed

+335
-248
lines changed

esp-hal/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ strum = { version = "0.27.1", default-features = false, featu
4545

4646
esp-build = { version = "0.3.0", path = "../esp-build" }
4747
esp-config = { version = "0.4.0", path = "../esp-config" }
48-
esp-metadata = { version = "0.7.0", path = "../esp-metadata", default-features = false }
4948
procmacros = { version = "0.18.0", package = "esp-hal-procmacros", path = "../esp-hal-procmacros" }
5049

5150
# Dependencies that are optional because they are used by unstable drivers.

esp-hal/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ fn main() -> Result<(), Box<dyn Error>> {
4141

4242
// Define all necessary configuration symbols for the configured device:
4343
config.define_symbols();
44+
config.generate_metadata();
4445

4546
// Place all linker scripts in `OUT_DIR`, and instruct Cargo how to find these
4647
// files:

esp-hal/src/etm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! channel will trigger the corresponding task automatically.
2020
//!
2121
//! For more information, please refer to the
22-
#![doc = concat!("[ESP-IDF documentation](https://docs.espressif.com/projects/esp-idf/en/latest/", crate::chip!(), "/api-reference/peripherals/etm.html)")]
22+
#![doc = concat!("[ESP-IDF documentation](https://docs.espressif.com/projects/esp-idf/en/latest/", chip!(), "/api-reference/peripherals/etm.html)")]
2323
//! ## Examples
2424
//!
2525
//! ### Control LED by the button via ETM

esp-hal/src/i2c/master/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,12 @@ pub enum SoftwareTimeout {
290290
#[instability::unstable]
291291
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
292292
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
293-
#[cfg(not(any(esp32, esp32s2)))]
293+
#[cfg(i2c_master_has_fsm_timeouts)]
294294
pub struct FsmTimeout {
295295
value: u8,
296296
}
297297

298-
#[cfg(not(any(esp32, esp32s2)))]
298+
#[cfg(i2c_master_has_fsm_timeouts)]
299299
impl FsmTimeout {
300300
const FSM_TIMEOUT_MAX: u8 = 23;
301301

@@ -331,7 +331,7 @@ impl FsmTimeout {
331331
}
332332
}
333333

334-
#[cfg(not(any(esp32, esp32s2)))]
334+
#[cfg(i2c_master_has_fsm_timeouts)]
335335
impl Default for FsmTimeout {
336336
fn default() -> Self {
337337
Self::new_const::<{ Self::FSM_TIMEOUT_DEFAULT }>()
@@ -596,14 +596,14 @@ pub struct Config {
596596
/// Sets the threshold value for the unchanged period of the SCL_FSM.
597597
///
598598
/// Default value: 16.
599-
#[cfg(not(any(esp32, esp32s2)))]
599+
#[cfg(i2c_master_has_fsm_timeouts)]
600600
#[builder_lite(unstable)]
601601
scl_st_timeout: FsmTimeout,
602602

603603
/// Sets the threshold for the unchanged duration of the SCL_MAIN_FSM.
604604
///
605605
/// Default value: 16.
606-
#[cfg(not(any(esp32, esp32s2)))]
606+
#[cfg(i2c_master_has_fsm_timeouts)]
607607
#[builder_lite(unstable)]
608608
scl_main_st_timeout: FsmTimeout,
609609
}
@@ -614,9 +614,9 @@ impl Default for Config {
614614
frequency: Rate::from_khz(100),
615615
timeout: BusTimeout::BusCycles(10),
616616
software_timeout: SoftwareTimeout::PerByte(Duration::from_millis(1)),
617-
#[cfg(not(any(esp32, esp32s2)))]
617+
#[cfg(i2c_master_has_fsm_timeouts)]
618618
scl_st_timeout: Default::default(),
619-
#[cfg(not(any(esp32, esp32s2)))]
619+
#[cfg(i2c_master_has_fsm_timeouts)]
620620
scl_main_st_timeout: Default::default(),
621621
}
622622
}
@@ -1540,7 +1540,7 @@ impl Driver<'_> {
15401540
self.set_frequency(config)?;
15411541

15421542
// Configure additional timeouts
1543-
#[cfg(not(any(esp32, esp32s2)))]
1543+
#[cfg(i2c_master_has_fsm_timeouts)]
15441544
{
15451545
self.regs()
15461546
.scl_st_time_out()
@@ -2236,7 +2236,7 @@ impl Driver<'_> {
22362236
));
22372237
}
22382238

2239-
#[cfg(not(any(esp32, esp32s2)))]
2239+
#[cfg(i2c_master_has_fsm_timeouts)]
22402240
{
22412241
if r.scl_st_to().bit_is_set() {
22422242
return Err(Error::Timeout);
@@ -2817,7 +2817,7 @@ fn calculate_chunk_size(remaining: usize) -> usize {
28172817
}
28182818
}
28192819

2820-
#[cfg(not(any(esp32, esp32s2)))]
2820+
#[cfg(i2c_master_has_hw_bus_clear)]
28212821
mod bus_clear {
28222822
use super::*;
28232823

@@ -2868,7 +2868,7 @@ mod bus_clear {
28682868
}
28692869
}
28702870

2871-
#[cfg(any(esp32, esp32s2))]
2871+
#[cfg(not(i2c_master_has_hw_bus_clear))]
28722872
mod bus_clear {
28732873
use super::*;
28742874
use crate::gpio::AnyPin;

esp-hal/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,22 @@
182182
// MUST be the first module
183183
mod fmt;
184184

185+
#[macro_use]
186+
/// This module contains code generated by `esp-metadata`.
187+
///
188+
/// The main API is the [`property!`] macro, which takes a string key and
189+
/// returns a value. This macro is available anywhere inside esp-hal, without
190+
/// additional imports. Note that this macro can't be used in public macros, as
191+
/// they will fail to compile.
192+
mod _generated {
193+
// This must go before other code, so that they can see macros.
194+
// Generated by esp-metadata.
195+
include!(concat!(env!("OUT_DIR"), "/_generated.rs"));
196+
}
197+
185198
use core::marker::PhantomData;
186199

187-
metadata!("build_info", CHIP_NAME, crate::chip!());
200+
metadata!("build_info", CHIP_NAME, chip!());
188201

189202
#[cfg(riscv)]
190203
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]

esp-hal/src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ macro_rules! before_snippet {
4141
#[macro_export]
4242
macro_rules! trm_markdown_link {
4343
() => {
44-
concat!("[Technical Reference Manual](", $crate::trm_link!(), ")")
44+
concat!("[Technical Reference Manual](", property!("trm"), ")")
4545
};
4646
($anchor:literal) => {
4747
concat!(
4848
"[Technical Reference Manual](",
49-
$crate::trm_link!(),
49+
property!("trm"),
5050
"#",
5151
$anchor,
5252
")"

esp-hal/src/rmt.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
)]
3939
#![doc = ""]
4040
//! For more information, please refer to the
41-
#![doc = concat!("[ESP-IDF documentation](https://docs.espressif.com/projects/esp-idf/en/latest/", crate::chip!(), "/api-reference/peripherals/rmt.html)")]
41+
#![doc = concat!("[ESP-IDF documentation](https://docs.espressif.com/projects/esp-idf/en/latest/", chip!(), "/api-reference/peripherals/rmt.html)")]
4242
//! ## Configuration
4343
//! Each TX/RX channel has the same functionality controlled by a dedicated set
4444
//! of registers and is able to independently transmit or receive data. TX
@@ -241,7 +241,6 @@ use crate::{
241241
},
242242
handler,
243243
peripherals::{Interrupt, RMT},
244-
soc::constants,
245244
system::{self, GenericPeripheralGuard},
246245
time::Rate,
247246
};
@@ -329,7 +328,7 @@ impl PulseCode for u32 {
329328

330329
#[inline]
331330
fn channel_ram_start(ch_num: impl Into<usize>) -> *mut u32 {
332-
(constants::RMT_RAM_START + ch_num.into() * constants::RMT_CHANNEL_RAM_SIZE * 4) as *mut u32
331+
(property!("rmt.ram_start") + ch_num.into() * property!("rmt.channel_ram_size") * 4) as *mut u32
333332
}
334333

335334
/// Channel configuration for TX channels
@@ -632,7 +631,7 @@ where
632631
/// Wait for the transaction to complete
633632
pub fn wait(mut self) -> Result<C, (Error, C)> {
634633
let memsize =
635-
constants::RMT_CHANNEL_RAM_SIZE * <C as TxChannelInternal>::memsize() as usize;
634+
property!("rmt.channel_ram_size") * <C as TxChannelInternal>::memsize() as usize;
636635

637636
while !self.remaining_data.is_empty() {
638637
// wait for TX-THR
@@ -724,7 +723,8 @@ where
724723
<C as TxChannelInternal>::update();
725724

726725
let ptr = channel_ram_start(C::CHANNEL);
727-
for idx in 0..constants::RMT_CHANNEL_RAM_SIZE * <C as TxChannelInternal>::memsize() as usize
726+
for idx in
727+
0..property!("rmt.channel_ram_size") * <C as TxChannelInternal>::memsize() as usize
728728
{
729729
unsafe {
730730
ptr.add(idx).write_volatile(0);
@@ -1234,7 +1234,7 @@ pub trait TxChannel: TxChannelInternal {
12341234
where
12351235
Self: Sized,
12361236
{
1237-
if data.len() > constants::RMT_CHANNEL_RAM_SIZE * Self::memsize() as usize {
1237+
if data.len() > property!("rmt.channel_ram_size") * Self::memsize() as usize {
12381238
return Err(Error::Overflow);
12391239
}
12401240

@@ -1292,7 +1292,7 @@ pub trait RxChannel: RxChannelInternal {
12921292
where
12931293
Self: Sized,
12941294
{
1295-
if data.len() > constants::RMT_CHANNEL_RAM_SIZE * Self::memsize() as usize {
1295+
if data.len() > property!("rmt.channel_ram_size") * Self::memsize() as usize {
12961296
return Err(Error::InvalidDataLength);
12971297
}
12981298

@@ -1373,7 +1373,7 @@ pub trait TxChannelAsync: TxChannelInternal {
13731373
where
13741374
Self: Sized,
13751375
{
1376-
if data.len() > constants::RMT_CHANNEL_RAM_SIZE * Self::memsize() as usize {
1376+
if data.len() > property!("rmt.channel_ram_size") * Self::memsize() as usize {
13771377
return Err(Error::InvalidDataLength);
13781378
}
13791379

@@ -1435,7 +1435,7 @@ pub trait RxChannelAsync: RxChannelInternal {
14351435
where
14361436
Self: Sized,
14371437
{
1438-
if data.len() > constants::RMT_CHANNEL_RAM_SIZE * Self::memsize() as usize {
1438+
if data.len() > property!("rmt.channel_ram_size") * Self::memsize() as usize {
14391439
return Err(Error::InvalidDataLength);
14401440
}
14411441

@@ -1603,7 +1603,7 @@ pub trait TxChannelInternal {
16031603
}
16041604

16051605
let ptr = channel_ram_start(Self::CHANNEL);
1606-
let memsize = constants::RMT_CHANNEL_RAM_SIZE * Self::memsize() as usize;
1606+
let memsize = property!("rmt.channel_ram_size") * Self::memsize() as usize;
16071607
for (idx, entry) in data.iter().take(memsize).enumerate() {
16081608
unsafe {
16091609
ptr.add(idx).write_volatile(*entry);

esp-hal/src/rng.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//! considered pseudo-random only.
2626
//!
2727
//! For more information, please refer to the
28-
#![doc = concat!("[ESP-IDF documentation](https://docs.espressif.com/projects/esp-idf/en/latest/", crate::chip!(), "/api-reference/system/random.html)")]
28+
#![doc = concat!("[ESP-IDF documentation](https://docs.espressif.com/projects/esp-idf/en/latest/", chip!(), "/api-reference/system/random.html)")]
2929
//! ## Configuration
3030
//! To use the [Rng] Driver, you need to initialize it with the RNG peripheral.
3131
//! Once initialized, you can generate random numbers by calling the `random`

esp-hal/src/soc/esp32/mod.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,6 @@ pub mod gpio;
2020
pub mod peripherals;
2121
pub(crate) mod regi2c;
2222

23-
/// The name of the chip ("esp32") as `&str`
24-
#[macro_export]
25-
macro_rules! chip {
26-
() => {
27-
"esp32"
28-
};
29-
}
30-
31-
/// A link to the Technical Reference Manual (TRM) for the chip.
32-
#[doc(hidden)]
33-
#[macro_export]
34-
macro_rules! trm_link {
35-
() => { "https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf" };
36-
}
37-
3823
#[cfg_attr(not(feature = "unstable"), allow(unused))]
3924
pub(crate) mod constants {
4025
use crate::time::Rate;
@@ -43,10 +28,6 @@ pub(crate) mod constants {
4328
pub const I2S_SCLK: u32 = 160_000_000;
4429
/// The default clock source for I2S operations.
4530
pub const I2S_DEFAULT_CLK_SRC: u32 = 2;
46-
/// The starting address of the Remote Control (RMT) module's RAM.
47-
pub const RMT_RAM_START: usize = 0x3ff56800;
48-
/// The size (number of pulse codes) of each RMT channel's dedicated RAM.
49-
pub const RMT_CHANNEL_RAM_SIZE: usize = 64;
5031
/// A reference clock tick of 1 MHz.
5132
pub const REF_TICK: Rate = Rate::from_mhz(1);
5233
}

esp-hal/src/soc/esp32c2/mod.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,6 @@ pub mod gpio;
1313
pub mod peripherals;
1414
pub(crate) mod regi2c;
1515

16-
/// The name of the chip ("esp32c2") as `&str`
17-
#[macro_export]
18-
macro_rules! chip {
19-
() => {
20-
"esp32c2"
21-
};
22-
}
23-
24-
/// A link to the Technical Reference Manual (TRM) for the chip.
25-
#[doc(hidden)]
26-
#[macro_export]
27-
macro_rules! trm_link {
28-
() => { "https://www.espressif.com/sites/default/files/documentation/esp8684_technical_reference_manual_en.pdf" };
29-
}
30-
3116
#[allow(unused)]
3217
pub(crate) mod registers {
3318
pub const INTERRUPT_MAP_BASE: u32 = 0x600c2000;

0 commit comments

Comments
 (0)