rust-embedded / svd2rust

Generate Rust register maps (`struct`s) from SVD files
Apache License 2.0
674 stars 147 forks source link

Generating type alias of derived enumeratedValues. #810

Closed seisyuu-hantatsushi closed 4 months ago

seisyuu-hantatsushi commented 4 months ago

svd file

            <field>
              <name>SAI1SEL</name>
              <description>SAI1 and DFSDM1 kernel Aclk clock source
              selection</description>
              <bitOffset>0</bitOffset>
              <bitWidth>3</bitWidth>
              <enumeratedValues>
                <name>SAI1SEL</name>
                <enumeratedValue>
                  <name>PLL1_Q</name>
                  <description>pll1_q selected as peripheral clock</description>
                  <value>0</value>
                </enumeratedValue>
                <enumeratedValue>
                  <name>PLL2_P</name>
                  <description>pll2_p selected as peripheral clock</description>
                  <value>1</value>
                </enumeratedValue>
                <enumeratedValue>
                  <name>PLL3_P</name>
                  <description>pll3_p selected as peripheral clock</description>
                  <value>2</value>
                </enumeratedValue>
                <enumeratedValue>
                  <name>I2S_CKIN</name>
                  <description>I2S_CKIN selected as peripheral clock</description>
                  <value>3</value>
                </enumeratedValue>
                <enumeratedValue>
                  <name>PER</name>
                  <description>PER selected as peripheral clock</description>
                  <value>4</value>
                </enumeratedValue>
              </enumeratedValues>
            </field>
            <field>
              <name>SAI23SEL</name>
              <description>SAI2 and SAI3 kernel clock source
              selection</description>
              <bitOffset>6</bitOffset>
              <bitWidth>3</bitWidth>
              <enumeratedValues derivedFrom="SAI1SEL"/>
            </field>
            <field>
              <name>SPI123SEL</name>
              <description>SPI/I2S1,2 and 3 kernel clock source
              selection</description>
              <bitOffset>12</bitOffset>
              <bitWidth>3</bitWidth>
              <enumeratedValues derivedFrom="SAI1SEL"/>
            </field>

generated rust code from the above svd file.

pub enum SAI1SEL {
    #[doc = "0: pll1_q selected as peripheral clock"]
    Pll1Q = 0,
    #[doc = "1: pll2_p selected as peripheral clock"]
    Pll2P = 1,
    #[doc = "2: pll3_p selected as peripheral clock"]
    Pll3P = 2,
    #[doc = "3: I2S_CKIN selected as peripheral clock"]
    I2sCkin = 3,
    #[doc = "4: PER selected as peripheral clock"]
    Per = 4,
}

impl SAI1SEL_R {
}

pub type SAI1SEL_W<'a, REG> = crate::FieldWriter<'a, REG, 3, SAI1SEL>;
impl<'a, REG> SAI1SEL_W<'a, REG>
where
    REG: crate::Writable + crate::RegisterSpec,
    REG::Ux: From<u8>,
{
}

#[doc = "Field `SAI23SEL` reader - SAI2 and SAI3 kernel clock source selection"]
pub use SAI1SEL_R as SAI23SEL_R;
#[doc = "Field `SPI123SEL` reader - SPI/I2S1,2 and 3 kernel clock source selection"]
pub use SAI1SEL_R as SPI123SEL_R;
#[doc = "Field `SAI23SEL` writer - SAI2 and SAI3 kernel clock source selection"]
pub use SAI1SEL_W as SAI23SEL_W;
#[doc = "Field `SPI123SEL` writer - SPI/I2S1,2 and 3 kernel clock source selection"]
pub use SAI1SEL_W as SPI123SEL_W;

svd2rust does not generate type aliase of SAI23SEL and SPI123SEL. I would like to append type aliase of SAI23SEL and SPI123SEL.

pub type SAI23SEL = SAI1SEL;
pub type SPI123SEL = SAI1SEL;

Because, if the enumerator names do not match the names of Field Reader and Filed Writer, you will be in trouble when you generate the code with macros. And if the enumerator names do not match the names of Field Reader and Filed Writer, the code is difficult to write and may cause bugs.

Emilgardis commented 4 months ago

/ci diff pr

github-actions[bot] commented 4 months ago

Diff for comment

burrbull commented 4 months ago

Related to #652.

Please, don't use type aliases for identical types. This makes compilation slower and grows docs. You can just reexport it with:

pub use oldpath as newname;
seisyuu-hantatsushi commented 4 months ago

Sorry, I could not find out #652. I understand the use of re-export instead of type alias.

Do you plan to implement to add re-export functionality to svd2rust like proposal in #652? Might I implement the ability to add a re-export name from a derived enumeratedValue element?

Emilgardis commented 4 months ago

/ci diff pr

github-actions[bot] commented 4 months ago

Diff for comment

burrbull commented 4 months ago

Please, no any merge master in PR. Use rebase instead

seisyuu-hantatsushi commented 4 months ago

The changes have been committed to the rebased repository. Sorry for any inconvenience caused.

seisyuu-hantatsushi commented 4 months ago

As the master branch is much further along and away from this change, and as this request seems to have been taken into account, I would like to close this pull request for now. Could I close it myself? @burrbull

burrbull commented 4 months ago

Check https://github.com/rust-embedded/svd2rust/pull/816 branch first. Possibly it partially closes your needs.

seisyuu-hantatsushi commented 4 months ago

Check #816 branch first. Possibly it partially closes your needs.

Thank you. I will check this branch.

burrbull commented 4 months ago

/ci diff pr

github-actions[bot] commented 4 months ago

Diff for comment

burrbull commented 4 months ago

If you want to use field names for enums. Just use --field_names_for_enums option.

seisyuu-hantatsushi commented 4 months ago

My issue is resolved by --field_names_for_enums option. Thank you.