1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use super::{
register::{RegIter, RegIterMut},
AddressBlock, BuildError, EmptyToNone, Interrupt, RegisterCluster, RegisterProperties,
SvdError, ValidateLevel,
};
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum Error {
#[error("Peripheral have `registers` tag, but it is empty")]
EmptyRegisters,
}
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct Peripheral {
pub name: String,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub display_name: Option<String>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub version: Option<String>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub description: Option<String>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub group_name: Option<String>,
pub base_address: u64,
#[cfg_attr(feature = "serde", serde(flatten))]
pub default_register_properties: RegisterProperties,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub address_block: Option<Vec<AddressBlock>>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Vec::is_empty")
)]
pub interrupt: Vec<Interrupt>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub registers: Option<Vec<RegisterCluster>>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub derived_from: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct PeripheralBuilder {
name: Option<String>,
display_name: Option<String>,
version: Option<String>,
description: Option<String>,
group_name: Option<String>,
base_address: Option<u64>,
default_register_properties: RegisterProperties,
address_block: Option<Vec<AddressBlock>>,
interrupt: Option<Vec<Interrupt>>,
registers: Option<Vec<RegisterCluster>>,
derived_from: Option<String>,
}
impl From<Peripheral> for PeripheralBuilder {
fn from(p: Peripheral) -> Self {
Self {
name: Some(p.name),
display_name: p.display_name,
version: p.version,
description: p.description,
group_name: p.group_name,
base_address: Some(p.base_address),
default_register_properties: p.default_register_properties,
address_block: p.address_block,
interrupt: Some(p.interrupt),
registers: p.registers,
derived_from: p.derived_from,
}
}
}
impl PeripheralBuilder {
pub fn name(mut self, value: String) -> Self {
self.name = Some(value);
self
}
pub fn display_name(mut self, value: Option<String>) -> Self {
self.display_name = value;
self
}
pub fn version(mut self, value: Option<String>) -> Self {
self.version = value;
self
}
pub fn description(mut self, value: Option<String>) -> Self {
self.description = value;
self
}
pub fn group_name(mut self, value: Option<String>) -> Self {
self.group_name = value;
self
}
pub fn base_address(mut self, value: u64) -> Self {
self.base_address = Some(value);
self
}
pub fn default_register_properties(mut self, value: RegisterProperties) -> Self {
self.default_register_properties = value;
self
}
pub fn address_block(mut self, value: Option<Vec<AddressBlock>>) -> Self {
self.address_block = value;
self
}
pub fn interrupt(mut self, value: Option<Vec<Interrupt>>) -> Self {
self.interrupt = value;
self
}
pub fn registers(mut self, value: Option<Vec<RegisterCluster>>) -> Self {
self.registers = value;
self
}
pub fn derived_from(mut self, value: Option<String>) -> Self {
self.derived_from = value;
self
}
pub fn build(self, lvl: ValidateLevel) -> Result<Peripheral, SvdError> {
let mut per = Peripheral {
name: self
.name
.ok_or_else(|| BuildError::Uninitialized("name".to_string()))?,
display_name: self.display_name.empty_to_none(),
version: self.version.empty_to_none(),
description: self.description.empty_to_none(),
group_name: self.group_name.empty_to_none(),
base_address: self
.base_address
.ok_or_else(|| BuildError::Uninitialized("base_address".to_string()))?,
default_register_properties: self.default_register_properties.build(lvl)?,
address_block: self.address_block,
interrupt: self.interrupt.unwrap_or_default(),
registers: self.registers,
derived_from: self.derived_from,
};
if !lvl.is_disabled() {
per.validate(lvl)?;
}
Ok(per)
}
}
impl Peripheral {
pub fn builder() -> PeripheralBuilder {
PeripheralBuilder::default()
}
pub fn modify_from(
&mut self,
builder: PeripheralBuilder,
lvl: ValidateLevel,
) -> Result<(), SvdError> {
if let Some(name) = builder.name {
self.name = name;
}
if builder.display_name.is_some() {
self.display_name = builder.display_name.empty_to_none();
}
if builder.version.is_some() {
self.version = builder.version.empty_to_none();
}
if builder.description.is_some() {
self.description = builder.description.empty_to_none();
}
if builder.group_name.is_some() {
self.group_name = builder.group_name.empty_to_none();
}
if let Some(base_address) = builder.base_address {
self.base_address = base_address;
}
if let Some(interrupt) = builder.interrupt {
self.interrupt = interrupt;
}
if builder.derived_from.is_some() {
self.derived_from = builder.derived_from;
self.registers = None;
self.address_block = None;
self.default_register_properties = RegisterProperties::default();
} else {
if builder.address_block.is_some() {
self.address_block = builder.address_block;
}
self.default_register_properties
.modify_from(builder.default_register_properties, lvl)?;
if builder.registers.is_some() {
self.registers = builder.registers.empty_to_none();
}
}
if !lvl.is_disabled() {
self.validate(lvl)
} else {
Ok(())
}
}
pub fn validate(&mut self, lvl: ValidateLevel) -> Result<(), SvdError> {
if lvl.is_strict() {
super::check_dimable_name(&self.name, "name")?;
}
if let Some(name) = self.derived_from.as_ref() {
if lvl.is_strict() {
super::check_dimable_name(name, "derivedFrom")?;
}
} else if let Some(registers) = self.registers.as_ref() {
if registers.is_empty() && lvl.is_strict() {
return Err(Error::EmptyRegisters.into());
}
}
Ok(())
}
pub fn reg_iter(&self) -> RegIter {
if let Some(regs) = &self.registers {
let mut rem: Vec<&RegisterCluster> = Vec::with_capacity(regs.len());
for r in regs.iter().rev() {
rem.push(r);
}
RegIter { rem }
} else {
RegIter { rem: Vec::new() }
}
}
pub fn reg_iter_mut(&mut self) -> RegIterMut {
if let Some(regs) = &mut self.registers {
let mut rem: Vec<&mut RegisterCluster> = Vec::with_capacity(regs.len());
for r in regs.iter_mut().rev() {
rem.push(r);
}
RegIterMut { rem }
} else {
RegIterMut { rem: Vec::new() }
}
}
}