uart: fix #36, explicitly handle scientific notation in YAML config

This commit is contained in:
Fischer Moseley 2025-04-03 23:12:07 -06:00
parent e11d9a8315
commit 22a6966610
8 changed files with 28 additions and 17 deletions

View File

@ -13,8 +13,8 @@ The UART interface is used by adding a `uart` entry at the bottom of the configu
```yaml
uart:
port: "auto"
baudrate: 3000000
clock_freq: 100000000
baudrate: 115200
clock_freq: 100e6
stall_interval: 16
chunk_size: 256
```

View File

@ -13,4 +13,4 @@ cores:
uart:
port: "/dev/ttyUSB3"
baudrate: 115200
clock_freq: 12000000
clock_freq: 12e6

View File

@ -17,4 +17,4 @@ cores:
uart:
port: "/dev/ttyUSB3"
baudrate: 115200
clock_freq: 12000000
clock_freq: 12e6

View File

@ -9,4 +9,4 @@ cores:
uart:
port: "/dev/ttyUSB1"
baudrate: 115200
clock_freq: 100000000
clock_freq: 100e6

View File

@ -23,4 +23,4 @@ cores:
uart:
port: "/dev/ttyUSB1"
baudrate: 115200
clock_freq: 100000000
clock_freq: 100e6

View File

@ -17,4 +17,4 @@ cores:
uart:
port: "/dev/ttyUSB1"
baudrate: 115200
clock_freq: 100000000
clock_freq: 100e6

View File

@ -75,25 +75,36 @@ class UARTInterface(Elaboratable):
@classmethod
def from_config(cls, config):
port = config.get("port")
clock_freq = config.get("clock_freq")
baudrate = config.get("baudrate")
# Warn if unrecognized options have been given
recognized_options = [
"port",
integer_options = [
"clock_freq",
"baudrate",
"chunk_size",
"stall_interval",
]
string_options = [
"port",
]
sanitized_config = {}
for option in config:
if option not in recognized_options:
# Since PyYAML is written to the YAML 1.1 spec, it will parse numeric values written
# with scientific notation (ie, `12e6` or `+5.0E+2`) as strings, not floats. At the
# time of writing, YAML 1.2 support is pending in PyYAML, so the casting is done
# manually here. Switching to ruyaml would also solve this.
if option in integer_options:
sanitized_config[option] = int(float(config[option]))
elif option in string_options:
sanitized_config[option] = config[option]
else:
warn(
f"Ignoring unrecognized option '{option}' in UART interface config."
)
return cls(**config)
return cls(**sanitized_config)
def to_config(self):
return {

View File

@ -24,4 +24,4 @@ cores:
uart:
port: "/dev/ttyUSB1"
baudrate: 115200
clock_freq: 12000000
clock_freq: 12e6