Skip to content

Generate macros from metadata #3604

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

bugadani
Copy link
Contributor

@bugadani bugadani commented Jun 5, 2025

The goal of this PR is to rely on esp-metadata more, by generating code from metadata. Eventually this should allow us to better parametrize drivers.

@bugadani bugadani added the skip-changelog No changelog modification needed label Jun 5, 2025
g.extend(quote::quote! {
/// Macro to get the address range of the given memory region.
#[macro_export]
macro_rules! memory_range {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a macro? I want to be able to put some of this data into doc comments, and the idea I have is separate macro branches that would generate strings. The main use case is the I2C FIFO size, which we put into a (private) doc comment, and that could look something like property!(i2c_fifo_len, str).

Additionally, these macros are visible globally in esp-hal, so no import statements are needed. I don't intend to create a new macro for each property, but a single property! macro that would have keys, so we don't clutter up the global namespace, but that's not terribly important at this point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea a lot, we often have this information in two places because of the need the value in both code and in docs.

@bugadani bugadani marked this pull request as ready for review June 6, 2025 08:58
@bugadani
Copy link
Contributor Author

bugadani commented Jun 6, 2025

I think I'll stop here so that we can evaluate the idea. I hope we'll eventually be able to remove the entirety of the soc module, and end up with a single way of customizing device drivers for each implementation (as the soc module is one such way, e.g. it implements device-specific bits of TRNG). We should also be able to reuse the efuse codegen from espflash, or rather make esp-metadata the source of truth for it.

@bjoernQ
Copy link
Contributor

bjoernQ commented Jun 6, 2025

I guess I like the general idea. At least we should be able to get rid of things like the soc::constants module easily

Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this approach seems solid to me!

I'd love to be able to import this directly from some other format, but regardless it's probably better to be editing toml files than code when we need new constants. I particularly like the format specifier with the macro branches, I think we'll be able to do some creative things with that.

g.extend(quote::quote! {
/// Macro to get the address range of the given memory region.
#[macro_export]
macro_rules! memory_range {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea a lot, we often have this information in two places because of the need the value in both code and in docs.

@@ -182,9 +182,22 @@
// MUST be the first module
mod fmt;

#[macro_use]
/// This module contains code generated by `esp-metadata`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even want to publicly document this? Maybe one (and probably should have some mention here anyways) for the DEVELOPER-GUIDELINES?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This macro shouldn't be publicly visible at all, the documentation is meant to be for us. D-G could use a section about how a driver looks, in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip-changelog No changelog modification needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants