Macro that matches on numeric range

Hi,

for some embedded code I am trying to create a macro that returns the correct register depending on a pin number. The register has different type. Is there a way to create a macro that returns a different "symbol" or instance depending on a range? In my non-working macro I am trying to do:

                    match $id {
                        0..=31 => (*pac::GPIO::ptr()).int0en.write(|p| p.[< gpio $id >]().bit(f)),
                        32..49= => (*pac::GPIO::ptr()).int1en.write(|p| p.[< gpio $id >]().bit(f)),
                    }

int0en and int1en are different types. Can I make a macro, e.g:

macro_rules! intreg {
    ($id:literal) => {
                    match $id {
                        0..=31 => 0
                        32..49 => 1
                    }
    };
}

so that the match returns 0 or 1, and I can build the register string in the original macro?

The only comparison you can do in a macro_rules is matching the exact value of a token:

macro_rules! intreg_small_example {
    (0) => {0};
    (1) => {0};
    (2) => {0};
    (3) => {0};
    (4) => {1};
    (5) => {1};
    (6) => {1};
    (7) => {1};
}

You need either a macro_rules with 50 arms or a proc-macro.

1 Like

Thanks, then I'll just specify it in the macro taking that output.