svd2rust icon indicating copy to clipboard operation
svd2rust copied to clipboard

Support `WriteConstraint` on a Register Level

Open cr1901 opened this issue 5 years ago • 0 comments

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.

cr1901 avatar Apr 11 '20 21:04 cr1901