svd icon indicating copy to clipboard operation
svd copied to clipboard

do validation when parsing serde yaml/json

Open ReinoutHeeck opened this issue 3 years ago • 3 comments

I was handcrafting some svd (yaml) and early on ran into a generator issue.

To reproduce run the following in an empty directory:

cargo init simple_bit_field_failure \
&& cd simple_bit_field_failure \
&& cargo add cortex_m vcell \
&& cd src \
&& echo """
name: MyDevice
version: 0
description: My Device
addressUnitBits: 32
width: 32
peripherals:
  - name: MyPeripheral
    baseAddress: 0xb8000300
    registers:
      - register:
          name: MyRegister
          description: My register.
          addressOffset: 0
          size: 1
          fields:
            - name: MyBitField
              description: My bit field.
              bitRange: \"[0:0]\"
""" |  RUST_LOG=debug svd2rust --strict --source_type yaml \
&&cargo fmt -- --config normalize_doc_attributes=true \
&&cargo build

result:

...
error[E0308]: mismatched types
   --> src/lib.rs:779:37
    |
779 |                 MY_BIT_FIELD_R::new(self.bits)
    |                 ------------------- ^^^^^^^^^ expected `bool`, found `u8`
    |                 |
    |                 arguments to this function are incorrect
    |
...

The first doc line reports version:

//!Peripheral access API for MYDEVICE microcontrollers (generated using svd2rust v0.28.0 ( ))

ReinoutHeeck avatar Jan 15 '23 12:01 ReinoutHeeck

After delving into the generator sources it turns out that register 'size' above is measured in bits instead of bytes as I was using it.

This uncovers a broken corner case in the logic for computing use_cast in register.rs.

Fixing support for single-bit registers is not very urgent I guess ;-)

On a more serious note: should the generator have complained that bit fields beyond the register width were specified in my project?

ReinoutHeeck avatar Jan 15 '23 23:01 ReinoutHeeck

no need to dive in code. there is specification https://www.keil.com/pack/doc/CMSIS/SVD/html/elem_special.html#registerPropertiesGroup_gr

burrbull avatar Jan 16 '23 05:01 burrbull

I knew that site but seeing uncompilable code generated I also 'knew' it was a bug and had to dive in. It's my knee-jerk reaction :-/

ReinoutHeeck avatar Jan 16 '23 11:01 ReinoutHeeck