Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Datatype #261

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions svd-encoder/src/datatype.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use super::{Config, Element, Encode, EncodeError, XMLNode};

impl Encode for crate::svd::DataType {
type Error = EncodeError;

fn encode_with_config(&self, _config: &Config) -> Result<Element, EncodeError> {
let mut elem = Element::new("dataType");
elem.children.push(XMLNode::Text(self.as_str().to_string()));
Ok(elem)
}
}
1 change: 1 addition & 0 deletions svd-encoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ mod bitrange;
mod cluster;
mod config;
mod cpu;
mod datatype;
mod device;
mod dimelement;
mod endian;
Expand Down
4 changes: 4 additions & 0 deletions svd-encoder/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ impl Encode for RegisterInfo {
elem.children
.extend(self.properties.encode_with_config(config)?);

if let Some(v) = &self.datatype {
elem.children.push(v.encode_node_with_config(config)?);
}

if let Some(v) = &self.modified_write_values {
elem.children.push(v.encode_node_with_config(config)?);
}
Expand Down
14 changes: 14 additions & 0 deletions svd-parser/src/datatype.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use super::*;
use crate::svd::DataType;

impl Parse for DataType {
type Object = Self;
type Error = SVDErrorAt;
type Config = Config;

fn parse(tree: &Node, _config: &Self::Config) -> Result<Self, Self::Error> {
let text = tree.get_text()?;

Self::parse_str(text).ok_or_else(|| SVDError::InvalidDatatype(text.into()).at(tree.id()))
}
}
4 changes: 1 addition & 3 deletions svd-parser/src/device.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use super::*;
use crate::svd::{
cpu::Cpu, peripheral::Peripheral, registerproperties::RegisterProperties, Device,
};
use crate::svd::{cpu::Cpu, peripheral::Peripheral, registerproperties::RegisterProperties};

/// Parses a SVD file
impl Parse for Device {
Expand Down
3 changes: 3 additions & 0 deletions svd-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ mod addressblock;
mod bitrange;
mod cluster;
mod cpu;
mod datatype;
mod device;
mod dimelement;
mod endian;
Expand Down Expand Up @@ -247,6 +248,8 @@ pub enum SVDError {
NotExpectedTag(String),
#[error("Invalid RegisterCluster (expected register or cluster), found {0}")]
InvalidRegisterCluster(String),
#[error("Invalid datatype variant, found {0}")]
InvalidDatatype(String),
#[error("Invalid modifiedWriteValues variant, found {0}")]
InvalidModifiedWriteValues(String),
#[error("Invalid readAction variant, found {0}")]
Expand Down
3 changes: 2 additions & 1 deletion svd-parser/src/register.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use crate::svd::{
Field, ModifiedWriteValues, ReadAction, Register, RegisterInfo, RegisterProperties,
DataType, Field, ModifiedWriteValues, ReadAction, Register, RegisterInfo, RegisterProperties,
WriteConstraint,
};

Expand Down Expand Up @@ -28,6 +28,7 @@ impl Parse for RegisterInfo {
.alternate_register(tree.get_child_text_opt("alternateRegister")?)
.address_offset(tree.get_child_u32("addressOffset")?)
.properties(RegisterProperties::parse(tree, config)?)
.datatype(optional::<DataType>("dataType", tree, config)?)
.modified_write_values(optional::<ModifiedWriteValues>(
"modifiedWriteValues",
tree,
Expand Down
2 changes: 2 additions & 0 deletions svd-rs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

- Add `DataType`

## [v0.14.8] - 2024-02-13

- add `maybe_array` constructors
Expand Down
88 changes: 88 additions & 0 deletions svd-rs/src/datatype.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/// Register data type
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(rename_all = "snake_case")
)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DataType {
/// unsigned byte
U8,
/// unsigned half word
U16,
/// unsigned word
U32,
/// unsigned double word
U64,
/// signed byte
I8,
/// signed half word
I16,
/// signed world
I32,
/// signed double word
I64,
/// pointer to unsigned byte
U8Ptr,
/// pointer to unsigned half word
U16Ptr,
/// pointer to unsigned word
U32Ptr,
/// pointer to unsigned double word
U64Ptr,
/// pointer to signed byte
I8Ptr,
/// pointer to signed half word
I16Ptr,
/// pointer to signed world
I32Ptr,
/// pointer to signed double word
I64Ptr,
}

impl DataType {
/// Parse a string into an [`DataType`] value, returning [`Option::None`] if the string is not valid.
pub fn parse_str(s: &str) -> Option<Self> {
match s {
"uint8_t" => Some(Self::U8),
"uint16_t" => Some(Self::U16),
"uint32_t" => Some(Self::U32),
"uint64_t" => Some(Self::U64),
"int8_t" => Some(Self::I8),
"int16_t" => Some(Self::I16),
"int32_t" => Some(Self::I32),
"int64_t" => Some(Self::I64),
"uint8_t *" => Some(Self::U8Ptr),
"uint16_t *" => Some(Self::U16Ptr),
"uint32_t *" => Some(Self::U32Ptr),
"uint64_t *" => Some(Self::U64Ptr),
"int8_t *" => Some(Self::I8Ptr),
"int16_t *" => Some(Self::I16Ptr),
"int32_t *" => Some(Self::I32Ptr),
"int64_t *" => Some(Self::I64Ptr),
_ => None,
}
}

/// Convert this [`DataType`] into a static string.
pub const fn as_str(self) -> &'static str {
match self {
Self::U8 => "uint8_t",
Self::U16 => "uint16_t",
Self::U32 => "uint32_t",
Self::U64 => "uint64_t",
Self::I8 => "int8_t",
Self::I16 => "int16_t",
Self::I32 => "int32_t",
Self::I64 => "int64_t",
Self::U8Ptr => "uint8_t *",
Self::U16Ptr => "uint16_t *",
Self::U32Ptr => "uint32_t *",
Self::U64Ptr => "uint64_t *",
Self::I8Ptr => "int8_t *",
Self::I16Ptr => "int16_t *",
Self::I32Ptr => "int32_t *",
Self::I64Ptr => "int64_t *",
}
}
}
4 changes: 4 additions & 0 deletions svd-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ pub use self::readaction::ReadAction;
pub mod protection;
pub use self::protection::Protection;

/// DataType objects
pub mod datatype;
pub use self::datatype::DataType;

/// Level of validation
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ValidateLevel {
Expand Down
21 changes: 20 additions & 1 deletion svd-rs/src/register.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
array::{descriptions, names},
Access, BuildError, Description, DimElement, EmptyToNone, Field, MaybeArray,
Access, BuildError, DataType, Description, DimElement, EmptyToNone, Field, MaybeArray,
ModifiedWriteValues, Name, ReadAction, RegisterProperties, SvdError, ValidateLevel,
WriteConstraint,
};
Expand Down Expand Up @@ -66,6 +66,13 @@ pub struct RegisterInfo {
#[cfg_attr(feature = "serde", serde(flatten))]
pub properties: RegisterProperties,

/// Register data type
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub datatype: Option<DataType>,

/// Specifies the write side effects
#[cfg_attr(
feature = "serde",
Expand Down Expand Up @@ -143,6 +150,7 @@ pub struct RegisterInfoBuilder {
alternate_register: Option<String>,
address_offset: Option<u32>,
properties: RegisterProperties,
datatype: Option<DataType>,
modified_write_values: Option<ModifiedWriteValues>,
write_constraint: Option<WriteConstraint>,
read_action: Option<ReadAction>,
Expand All @@ -160,6 +168,7 @@ impl From<RegisterInfo> for RegisterInfoBuilder {
alternate_register: r.alternate_register,
address_offset: Some(r.address_offset),
properties: r.properties,
datatype: r.datatype,
modified_write_values: r.modified_write_values,
write_constraint: r.write_constraint,
read_action: r.read_action,
Expand Down Expand Up @@ -205,6 +214,11 @@ impl RegisterInfoBuilder {
self.properties = value;
self
}
/// Set the datatype of the register.
pub fn datatype(mut self, value: Option<DataType>) -> Self {
self.datatype = value;
self
}
/// Set the size of the register.
pub fn size(mut self, value: Option<u32>) -> Self {
self.properties.size = value;
Expand Down Expand Up @@ -264,6 +278,7 @@ impl RegisterInfoBuilder {
.address_offset
.ok_or_else(|| BuildError::Uninitialized("address_offset".to_string()))?,
properties: self.properties.build(lvl)?,
datatype: self.datatype,
modified_write_values: self.modified_write_values,
write_constraint: self.write_constraint,
read_action: self.read_action,
Expand Down Expand Up @@ -324,10 +339,14 @@ impl RegisterInfo {
self.derived_from = builder.derived_from;
self.fields = None;
self.properties = RegisterProperties::default();
self.datatype = None;
self.modified_write_values = None;
self.write_constraint = None;
} else {
self.properties.modify_from(builder.properties, lvl)?;
if builder.datatype.is_some() {
self.datatype = builder.datatype;
}
if builder.modified_write_values.is_some() {
self.modified_write_values = builder.modified_write_values;
}
Expand Down