Skip to content
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

Generating type alias of derived enumeratedValues. #810

Conversation

seisyuu-hantatsushi
Copy link

@seisyuu-hantatsushi seisyuu-hantatsushi commented Feb 10, 2024

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.

@seisyuu-hantatsushi seisyuu-hantatsushi requested a review from a team as a code owner February 10, 2024 11:02
@Emilgardis
Copy link
Member

/ci diff pr

Copy link

Diff for comment

@burrbull
Copy link
Member

burrbull commented Feb 11, 2024

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
Copy link
Author

seisyuu-hantatsushi commented Feb 12, 2024

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
Copy link
Member

/ci diff pr

Copy link

Diff for comment

@burrbull
Copy link
Member

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

@seisyuu-hantatsushi seisyuu-hantatsushi force-pushed the alias_enum_val branch 2 times, most recently from 2dc7d19 to 36dda44 Compare February 18, 2024 02:10
@seisyuu-hantatsushi
Copy link
Author

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

Comment on lines 1439 to 1443
match usage {
Usage::Read => Some(name.to_owned() + "R"),
Usage::Write => Some(name.to_owned() + "W"),
Usage::ReadWrite => Some(name.to_owned()),
}
Copy link
Member

Choose a reason for hiding this comment

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

This should also use ident.

@Emilgardis this is yet one question we need to discuss next meeting.

Now we use:
common RW: enum_name
only R: enum_name
only W: enum_name
different R+W: enum_name + enum_writer_name

Should we simplify it to:
RW: enum_name
R: enum_reader_name
W: enum_writer_name

Or support both notation somehow?

Choose a reason for hiding this comment

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

I support to simplify it.
For the following reasons.

  • Simpler rules are better.
  • Supporting both notations would increase maintenance costs.
  • Even if the functionality changes between reads and writes, the bit names would be different and can be distinguished.

Further My Opinion .
I think it is not good that these notations are added by 'svd2rust'.
Since these notations are added by 'svdtool patch', it would be better to add the names for re-export by 'svdtool patch'.
I have created pull request rust-embedded/svdtools#205 for svdtools for this opinion.

@Emilgardis Emilgardis added T-tools S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Feb 18, 2024
@seisyuu-hantatsushi
Copy link
Author

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
Copy link
Member

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

@seisyuu-hantatsushi
Copy link
Author

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

Thank you. I will check this branch.

…d_value does not have a name.

Because a derived enumerate values in patched svd are written over by a base enumerated balue.
@burrbull
Copy link
Member

burrbull commented Mar 4, 2024

/ci diff pr

Copy link

github-actions bot commented Mar 4, 2024

Diff for comment

@burrbull
Copy link
Member

burrbull commented Mar 4, 2024

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

@seisyuu-hantatsushi
Copy link
Author

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

@seisyuu-hantatsushi seisyuu-hantatsushi deleted the alias_enum_val branch March 4, 2024 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-tools
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants