rust-embedded / svd2rust

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

Support `WriteConstraint` on a Register Level #434

Closed cr1901 closed 1 year ago

cr1901 commented 4 years ago

The CMSIS SVD Specification allows WriteConstraint fields to be applied to a Register type, and not just its fields:

  <xs:complexType name="registerType">
    <xs:sequence>
...
     <!-- writeConstraint specifies the subset of allowed write values -->
      <xs:element name="writeConstraint" type="writeConstraintType" minOccurs="0"/>
...
    </xs:sequence>
    <xs:attribute name="derivedFrom" type="dimableIdentifierType" use="optional"/>
  </xs:complexType>

Right now, svd2rust has no provisions for providing a safe bits() function which accesses the entire register (and not just a field), because the generics bits() function for writing a register is- correctly marked as unsafe!

To avoid having application writers have to place unsafe code in their application, I propose some standardized function to safely access all the bits of a register, as a unit, when svd2rust can prove that no invalid values can be written into a register.

svd2rust could either prove this info by reading the WriteConstraint field of a Register type, or keeping a running tally of which bits are stated to be valid in children (fieldType) of the fieldsType sequence.

This is useful for RMW GPIO registers (direction, value, etc) and setting timer values for countdown all at once, without having to chain proxies together for each individual bit.

burrbull commented 2 years ago

Related PR: #666

burrbull commented 1 year ago

reopen if not fixed yet