Repository navigation
Odd (and incorrect) code generation from SVD #966
Copy link
Copy link
Open
Description
I was in the process of updating a project that I am working on to new SVD's and when I generated the PAC I got some strange code. This first block is and excerpt from the relevant part of the SVD. The full thing can be found here:
<field>
<name>CTS_ASSERT</name>
<description>CTS assert level</description>
<bitOffset>7</bitOffset>
<bitWidth>1</bitWidth>
<enumeratedValues>
<name>ASSERT_LEVEL</name>
<enumeratedValue>
<name>HIGH</name>
<description>Assert high</description>
<value>0</value>
</enumeratedValue>
<enumeratedValue>
<name>LOW</name>
<description>Assert low</description>
<value>1</value>
</enumeratedValue>
</enumeratedValues>
</field>
<field>
<name>RTS_ASSERT</name>
<description>RTS assert level</description>
<bitOffset>6</bitOffset>
<bitWidth>1</bitWidth>
<enumeratedValues derivedFrom="UART1.CNTL.CTS_ASSERT.ASSERT_LEVEL"></enumeratedValues>
</field>
Generated code is as follows:
#[doc = "RTS assert level"]
pub use AssertLevel;
#[doc = "Field `RTS_ASSERT` reader - RTS assert level"]
pub use CtsAssertR as RtsAssertR;
#[doc = "Field `RTS_ASSERT` writer - RTS assert level"]
pub use CtsAssertW as RtsAssertW;
#[doc = "CTS assert level\n\nValue on reset: 0"]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AssertLevel {
#[doc = "0: Assert high"]
High = 0,
#[doc = "1: Assert low"]
Low = 1,
}
impl From<AssertLevel> for bool {
#[inline(always)]
fn from(variant: AssertLevel) -> Self {
variant as u8 != 0
}
}
#[doc = "Field `CTS_ASSERT` reader - CTS assert level"]
pub type CtsAssertR = crate::BitReader<AssertLevel>;
impl CtsAssertR {
#[doc = "Get enumerated values variant"]
#[inline(always)]
pub const fn variant(&self) -> AssertLevel {
match self.bits {
false => AssertLevel::High,
true => AssertLevel::Low,
}
}
#[doc = "Assert high"]
#[inline(always)]
pub fn is_high(&self) -> bool {
*self == AssertLevel::High
}
#[doc = "Assert low"]
#[inline(always)]
pub fn is_low(&self) -> bool {
*self == AssertLevel::Low
}
}
#[doc = "Field `CTS_ASSERT` writer - CTS assert level"]
pub type CtsAssertW<'a, REG> = crate::BitWriter<'a, REG, AssertLevel>;
impl<'a, REG> CtsAssertW<'a, REG>
where
REG: crate::Writable + crate::RegisterSpec,
{
#[doc = "Assert high"]
#[inline(always)]
pub fn high(self) -> &'a mut crate::W<REG> {
self.variant(AssertLevel::High)
}
#[doc = "Assert low"]
#[inline(always)]
pub fn low(self) -> &'a mut crate::W<REG> {
self.variant(AssertLevel::Low)
}
}
The error here is that for some reason a pub use AssertLevel; was generated just before pub enum AssertLevel {...}. I am using svd2rust version 0.37.1 and rust version 1.92.0. I am running Ubuntu 24.04.
As to what the actual issue here is I'm not sure. However it is unlikely to be the SVD because that specific file has been the same for years.
Let me know if anymore information is needed.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
burrbull commentedon Aug 23, 2026
You are right. It looks like a bug and should be easy to fix.
But I understand why it was not found earlier. Usually we use relative derive path instead of absolute in such cases.
burrbull commentedon Aug 23, 2026
Oh. No. It looks like there is other issue.
Due to this sort:
svd2rust/src/generate/register.rs
Line 631 in b0f2715
RTS_ASSERTis being processed beforeCTS_ASSERTThis leads to this hashset
svd2rust/src/generate/register.rs
Line 762 in b0f2715
is empty when
pub use AssertLevel;is generated.burrbull commentedon Aug 23, 2026
Fast fix from SVD side is to derive
CTS_ASSERTfromRTS_ASSERTinstead ofRTS_ASSERTfromCTS_ASSERT.The fix from
svd2rustside is to move filling ofderiveshashset out of main cycle.burrbull commentedon Aug 23, 2026
I was wrong. Only deleting
sort by offsetcould fix your issue from svd2rust side. But I afraid it can lead to more bugs.MsFormula commentedon Aug 26, 2026
Sorry I've been away without internet for the past few days. Using the branch and using a the reverse flag works perfectly! Thank you so much!
MsFormula commentedon Sep 1, 2026
@burrbull
Ok I'm getting the same issue now but in another part of the code:
SVD:
MsFormula commentedon Sep 6, 2026
I've continued testing and in 0.36.0 I get the same error. Not sure about anything lower.