Generate API for "write 1 to toggle" registers
Hey all,
Over in rust-embedded/rfcs#40, user @kinnison brought up that some of the STM32's USB control registers have a "write 0 to leave alone; write 1 to toggle" interface, and that svd2rust isn't generating the proper API for these registers. Since he hasn't filed an issue here yet, this is my attempt to document the feature.
In particular, the STM32F0x2 has a register for each USB endpoint with six of these toggleable bits. These registers are described in RM0091 under the name USB_EPnR:
When the application software writes ‘0, the value of DTOG_TX remains unchanged, while writing ‘1 makes the bit value toggle. This bit is read/write but it can only be toggled by writing 1.
Here is a link to the current output generated by svd2rust v0.12.0 from STM32F0x2.svd
STM32f0x2.svd
<field>
<name>DTOG_TX</name>
<description>Data Toggle, for transmission
transfers</description>
<bitOffset>6</bitOffset>
<bitWidth>1</bitWidth>
</field>
svd2rust API
#[doc = "Bit 6 - Data Toggle, for transmission transfers"]
#[inline]
pub fn dtog_tx(&self) -> DTOG_TXR {
let bits = {
const MASK: bool = true;
const OFFSET: u8 = 6;
((self.bits >> OFFSET) & MASK as u32) != 0
};
DTOG_TXR { bits }
}
#[doc = "Bit 6 - Data Toggle, for transmission transfers"]
#[inline]
pub fn dtog_tx(&mut self) -> _DTOG_TXW {
_DTOG_TXW { w: self }
}
Let me know if there's any other information I can dig up! I'm still learning embedded on Rust, and am really excited to see where things are headed.
To implement this we need to enable parsing of <modifiedWriteValues> as defined in the spec and make svd2rust understand this enum.
To make it work you'd have to patch this element into the SVD aswell
<field>
<name>DTOG_TX</name>
<description>Data Toggle, for transmission
transfers</description>
<modifiedWriteValues>oneToToggle</modifiedWriteValues>
<bitOffset>6</bitOffset>
<bitWidth>1</bitWidth>
</field>
Out of interest, did you get anywhere with this? Are you working on a USB driver?
@ah- I haven't gotten anywhere with this yet, since working on svd2rust would be above my current experience with Rust. I have plans to work with the STM32f0 USB peripherals in a month or two, which is why I brought this up.
If this begins to block my work on USB devices and I feel more comfortable with the language, I'd be glad to contribute or assist @Emilgardis following the plan that they have laid out.
The STM32L4 I'm working with also uses
read/clear (rc_w1): Software can read as well as clear this bit by writing 1. Writing ‘0’ has no effect on the bit value. read/clear (rc_w0): Software can read as well as clear this bit by writing 0. Writing ‘1’ has no effect on the bit value.
I guess supporting modifiedWriteValues as suggested would include this, unfortunately the official SVDs don't use this description :-/
@bergus I haven't looked in detail, but I've seen mechanisms to override the svd and fix issues like that in https://github.com/adamgreig/stm32-rs.
reopen if not fixed