-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathdevice.rs
453 lines (403 loc) · 16 KB
/
device.rs
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use anyhow::{anyhow, Context};
use svd_parser::svd::{Device, Peripheral, PeripheralInfo};
use yaml_rust::yaml::Hash;
use std::{fs::File, io::Read, path::Path};
use super::modify_register_properties;
use super::peripheral::PeripheralExt;
use super::yaml_ext::{AsType, GetVal};
use super::{abspath, matchname, PatchResult, VAL_LVL};
use super::{make_address_block, make_address_blocks, make_cpu, make_interrupt, make_peripheral};
pub struct PerIter<'a, 'b> {
it: std::slice::IterMut<'a, Peripheral>,
spec: &'b str,
check_derived: bool,
}
impl<'a, 'b> Iterator for PerIter<'a, 'b> {
type Item = &'a mut Peripheral;
fn next(&mut self) -> Option<Self::Item> {
self.it.by_ref().find(|next| {
matchname(&next.name, self.spec) && !(self.check_derived && next.derived_from.is_some())
})
}
}
/// Collecting methods for processing device contents
pub trait DeviceExt {
/// Iterates over all peripherals that match pspec
fn iter_peripherals<'a, 'b>(
&'a mut self,
spec: &'b str,
check_derived: bool,
) -> PerIter<'a, 'b>;
/// Work through a device, handling all peripherals
fn process(&mut self, device: &Hash, update_fields: bool) -> PatchResult;
/// Delete registers matched by rspec inside ptag
fn delete_peripheral(&mut self, pspec: &str) -> PatchResult;
/// Create copy of peripheral
fn copy_peripheral(&mut self, pname: &str, pmod: &Hash, path: &Path) -> PatchResult;
/// Modify the `cpu` node inside `device` according to `mod`
fn modify_cpu(&mut self, cmod: &Hash) -> PatchResult;
/// Modify pspec inside device according to pmod
fn modify_peripheral(&mut self, pspec: &str, pmod: &Hash) -> PatchResult;
/// Add pname given by padd to device
fn add_peripheral(&mut self, pname: &str, padd: &Hash) -> PatchResult;
/// Remove registers from pname and mark it as derivedFrom pderive.
/// Update all derivedFrom referencing pname
fn derive_peripheral(&mut self, pname: &str, pderive: &str) -> PatchResult;
/// Move registers from pold to pnew.
/// Update all derivedFrom referencing pold
fn rebase_peripheral(&mut self, pnew: &str, pold: &str) -> PatchResult;
/// Clear contents of all fields inside peripherals matched by pspec
fn clear_fields(&mut self, fspec: &str) -> PatchResult;
/// Work through a peripheral, handling all registers
fn process_peripheral(
&mut self,
pspec: &str,
peripheral: &Hash,
update_fields: bool,
) -> PatchResult;
}
impl DeviceExt for Device {
fn iter_peripherals<'a, 'b>(
&'a mut self,
spec: &'b str,
check_derived: bool,
) -> PerIter<'a, 'b> {
// check_derived=True
PerIter {
spec,
check_derived,
it: self.peripherals.iter_mut(),
}
}
fn process(&mut self, device: &Hash, update_fields: bool) -> PatchResult {
// Handle any deletions
for pspec in device.str_vec_iter("_delete") {
self.delete_peripheral(pspec)
.with_context(|| format!("Deleting peripheral matched to `{}`", pspec))?;
}
// Handle any copied peripherals
for (pname, val) in device.hash_iter("_copy") {
let pname = pname.str()?;
self.copy_peripheral(
pname,
val.hash()?,
Path::new(device.get_str("_path")?.unwrap_or(".")),
)
.with_context(|| format!("Copying peripheral `{}`", pname))?;
}
// Handle any modifications
for (key, val) in device.hash_iter("_modify") {
let key = key.str()?;
match key {
"cpu" => self
.modify_cpu(val.hash()?)
.with_context(|| "Modifying Cpu tag")?,
"_peripherals" => {
for (pspec, pmod) in val.hash()? {
let pspec = pspec.str()?;
self.modify_peripheral(pspec, pmod.hash()?)
.with_context(|| {
format!("Modifying peripherals matched to `{}`", pspec)
})?;
}
}
"vendor" => self.vendor = Some(val.str()?.into()),
"vendorID" => self.vendor_id = Some(val.str()?.into()),
"name" => self.name = val.str()?.into(),
"series" => self.series = Some(val.str()?.into()),
"version" => self.version = val.str()?.into(),
"description" => self.description = val.str()?.into(),
"licenseText" => self.license_text = Some(val.str()?.into()),
"headerSystemFilename" => self.header_system_filename = Some(val.str()?.into()),
"headerDefinitionsPrefix" => {
self.header_definitions_prefix = Some(val.str()?.into())
}
"addressUnitBits" => self.address_unit_bits = val.i64()? as u32,
"width" => self.width = val.i64()? as u32,
"size" | "access" | "protection" | "resetValue" | "resetMask" => {
modify_register_properties(&mut self.default_register_properties, key, val)?;
}
_ => self
.modify_peripheral(key, val.hash()?)
.with_context(|| format!("Modifying peripherals matched to `{}`", key))?,
}
}
// Handle field clearing
for pspec in device.str_vec_iter("_clear_fields") {
self.clear_fields(pspec).with_context(|| {
format!(
"Clearing contents of fields in peripherals matched to `{}` ",
pspec
)
})?;
}
// Handle any new peripherals (!)
for (pname, padd) in device.hash_iter("_add") {
let pname = pname.str()?;
self.add_peripheral(pname, padd.hash()?)
.with_context(|| format!("Adding peripheral `{}`", pname))?;
}
// Handle any derived peripherals
for (pname, pderive) in device.hash_iter("_derive") {
let pname = pname.str()?;
let pderive = pderive.str()?;
self.derive_peripheral(pname, pderive)
.with_context(|| format!("Deriving peripheral `{}` from `{}`", pname, pderive))?;
}
// Handle any rebased peripherals
for (pname, pold) in device.hash_iter("_rebase") {
let pname = pname.str()?;
let pold = pold.str()?;
self.rebase_peripheral(pname, pold)
.with_context(|| format!("Rebasing peripheral from `{}` to `{}`", pold, pname))?;
}
// Now process all peripherals
for (periphspec, val) in device {
let periphspec = periphspec.str()?;
if !periphspec.starts_with('_') {
//val["_path"] = device["_path"]; // TODO: check
self.process_peripheral(periphspec, val.hash()?, update_fields)
.with_context(|| format!("According to `{}`", periphspec))?;
}
}
Ok(())
}
fn delete_peripheral(&mut self, pspec: &str) -> PatchResult {
self.peripherals.retain(|p| !(matchname(&p.name, pspec)));
Ok(())
}
fn copy_peripheral(&mut self, pname: &str, pmod: &Hash, path: &Path) -> PatchResult {
let pcopysrc = pmod
.get_str("from")?
.unwrap()
.split(':')
.collect::<Vec<_>>();
let mut new = match pcopysrc.as_slice() {
[ppath, pcopyname] => {
let f = File::open(abspath(path, Path::new(ppath))?)?;
let mut contents = String::new();
(&f).read_to_string(&mut contents).unwrap();
let filedev = svd_parser::parse(&contents)
.with_context(|| format!("Parsing file {}", contents))?;
filedev
.get_peripheral(pcopyname)
.ok_or_else(|| anyhow!("peripheral {} not found", pcopyname))?
.clone()
}
[pcopyname] => {
let mut new = self
.get_peripheral(pcopyname)
.ok_or_else(|| anyhow!("peripheral {} not found", pcopyname))?
.clone();
// When copying from a peripheral in the same file, remove any interrupts.
new.interrupt = Vec::new();
new
}
_ => return Err(anyhow!("Incorrect `from` tag")),
};
new.name = pname.into();
new.derived_from = None;
if let Some(ptag) = self.get_mut_peripheral(pname) {
new.base_address = ptag.base_address;
new.interrupt = std::mem::take(&mut ptag.interrupt);
*ptag = new;
} else {
self.peripherals.push(new)
}
Ok(())
}
fn modify_cpu(&mut self, cmod: &Hash) -> PatchResult {
let cpu = make_cpu(cmod)?;
if let Some(c) = self.cpu.as_mut() {
c.modify_from(cpu, VAL_LVL)?;
} else {
self.cpu = Some(cpu.build(VAL_LVL)?);
}
Ok(())
}
fn modify_peripheral(&mut self, pspec: &str, pmod: &Hash) -> PatchResult {
for ptag in self.iter_peripherals(pspec, true) {
ptag.modify_from(make_peripheral(pmod, true)?, VAL_LVL)?;
if let Some(ints) = pmod.get_hash("interrupts")? {
for (iname, val) in ints {
let iname = iname.str()?;
let int = make_interrupt(val.hash()?)?;
for i in &mut ptag.interrupt {
if i.name == iname {
i.modify_from(int, VAL_LVL)?;
break;
}
}
}
}
if let Some(abmod) = pmod.get_hash("addressBlock").ok().flatten() {
let v = &mut ptag.address_block;
let ab = make_address_block(abmod)?;
match v.as_deref_mut() {
Some([adb]) => adb.modify_from(ab, VAL_LVL)?,
_ => *v = Some(vec![ab.build(VAL_LVL)?]),
}
} else if let Some(abmod) = pmod.get_vec("addressBlocks").ok().flatten() {
ptag.address_block = Some(make_address_blocks(abmod)?);
}
}
Ok(())
}
fn add_peripheral(&mut self, pname: &str, padd: &Hash) -> PatchResult {
if self.get_peripheral(pname).is_some() {
return Err(anyhow!("device already has a peripheral {}", pname));
}
self.peripherals.push(
make_peripheral(padd, false)?
.name(pname.to_string())
.build(VAL_LVL)?
.single(),
);
Ok(())
}
fn derive_peripheral(&mut self, pname: &str, pderive: &str) -> PatchResult {
self.get_peripheral(pderive)
.ok_or_else(|| anyhow!("peripheral {} not found", pderive))?;
self.get_mut_peripheral(pname)
.ok_or_else(|| anyhow!("peripheral {} not found", pname))?
.modify_from(
PeripheralInfo::builder().derived_from(Some(pderive.into())),
VAL_LVL,
)?;
for p in self
.peripherals
.iter_mut()
.filter(|p| p.derived_from.as_deref() == Some(pname))
{
p.derived_from = Some(pderive.into());
}
Ok(())
}
fn rebase_peripheral(&mut self, pnew: &str, pold: &str) -> PatchResult {
let old = self
.get_mut_peripheral(pold)
.ok_or_else(|| anyhow!("peripheral {} not found", pold))?;
let mut d = std::mem::replace(
old,
PeripheralInfo::builder()
.name(pold.into())
.base_address(old.base_address)
.interrupt(if old.interrupt.is_empty() {
None
} else {
Some(old.interrupt.clone())
})
.derived_from(Some(pnew.into()))
.build(VAL_LVL)?
.single(),
);
let new = self
.get_mut_peripheral(pnew)
.ok_or_else(|| anyhow!("peripheral {} not found", pnew))?;
d.name = new.name.clone();
d.base_address = new.base_address;
d.interrupt = new.interrupt.clone();
*new = d;
for p in self
.peripherals
.iter_mut()
.filter(|p| p.derived_from.as_deref() == Some(pold))
{
p.derived_from = Some(pnew.into());
}
Ok(())
}
fn clear_fields(&mut self, pspec: &str) -> PatchResult {
for ptag in self.iter_peripherals(pspec, false) {
ptag.clear_fields("*")?;
}
Ok(())
}
fn process_peripheral(
&mut self,
pspec: &str,
peripheral: &Hash,
update_fields: bool,
) -> PatchResult {
// Find all peripherals that match the spec
let mut pcount = 0;
for ptag in self.iter_peripherals(pspec, false) {
pcount += 1;
ptag.process(peripheral, update_fields)
.with_context(|| format!("Processing peripheral `{}`", ptag.name))?;
}
if pcount == 0 {
Err(anyhow!("Could not find `{}`", pspec))
} else {
Ok(())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils;
use std::path::Path;
#[test]
fn add_peripherals() {
let (mut device, yaml) = test_utils::get_patcher(Path::new("add")).unwrap();
assert_eq!(device.peripherals.len(), 1);
device.process(&yaml, true).unwrap();
assert_eq!(device.peripherals.len(), 2);
let periph1 = &device.peripherals[0];
assert_eq!(periph1.name, "DAC1");
let periph2 = &device.peripherals[1];
assert_eq!(periph2.name, "CPUID");
}
#[test]
fn delete_peripherals() {
let (mut device, yaml) = test_utils::get_patcher(Path::new("delete")).unwrap();
assert_eq!(device.peripherals.len(), 3);
device.process(&yaml, true).unwrap();
assert_eq!(device.peripherals.len(), 1);
let remaining_periph = &device.peripherals[0];
assert_eq!(remaining_periph.name, "DAC2");
}
#[test]
fn copy_peripherals() {
let (mut device, yaml) = test_utils::get_patcher(Path::new("copy")).unwrap();
assert_eq!(device.peripherals.len(), 3);
let dac1 = device.get_peripheral("DAC1").unwrap();
let dac2 = device.get_peripheral("DAC2").unwrap();
assert_ne!(dac1.registers, dac2.registers);
device.process(&yaml, true).unwrap();
assert_eq!(device.peripherals.len(), 3);
let dac1 = device.get_peripheral("DAC1").unwrap();
let dac2 = device.get_peripheral("DAC2").unwrap();
assert_eq!(dac1.registers, dac2.registers);
}
#[test]
fn modify_device() {
let (mut device, yaml) = test_utils::get_patcher(Path::new("modify")).unwrap();
// check device initial config
assert_eq!(&device.version, "1.6");
assert_eq!(&device.description, "");
// check cpu initial config
let cpu = &device.cpu.clone().unwrap();
assert_eq!(cpu.nvic_priority_bits, 3);
// check peripheral initial config
assert_eq!(device.peripherals.len(), 2);
let dac1 = device.get_peripheral("DAC1").unwrap();
assert_eq!(dac1.name, "DAC1");
assert_eq!(dac1.description, None);
device.process(&yaml, true).unwrap();
// check device final config
assert_eq!(&device.version, "1.7");
assert_eq!(&device.description, "STM32L4x2");
// check cpu final config
let cpu = &device.cpu.clone().unwrap();
assert_eq!(cpu.nvic_priority_bits, 4);
// check peripheral final config
let dac1 = device.get_peripheral("DAC11").unwrap();
assert_eq!(dac1.name, "DAC11");
assert_eq!(
dac1.description,
Some("Digital-to-analog converter".to_string())
);
}
}