@@ -9,7 +9,7 @@ use std::{fs::File, io::Read, path::Path};
9
9
use super :: iterators:: { MatchIter , Matched } ;
10
10
use super :: peripheral:: { PeripheralExt , RegisterBlockExt } ;
11
11
use super :: yaml_ext:: { AsType , GetVal } ;
12
- use super :: { abspath, matchname, Config , PatchResult , Spec , VAL_LVL } ;
12
+ use super :: { abspath, matchname, update_env , Config , Env , PatchResult , Spec , VAL_LVL } ;
13
13
use super :: { make_address_block, make_address_blocks, make_cpu, make_interrupt, make_peripheral} ;
14
14
use super :: { make_dim_element, modify_dim_element, modify_register_properties} ;
15
15
@@ -33,14 +33,14 @@ pub trait DeviceExt {
33
33
fn modify_cpu ( & mut self , cmod : & Hash ) -> PatchResult ;
34
34
35
35
/// Modify pspec inside device according to pmod
36
- fn modify_peripheral ( & mut self , pspec : & str , pmod : & Hash ) -> PatchResult ;
36
+ fn modify_peripheral ( & mut self , pspec : & str , pmod : & Hash , env : & Env ) -> PatchResult ;
37
37
38
38
/// Add pname given by padd to device
39
- fn add_peripheral ( & mut self , pname : & str , padd : & Hash ) -> PatchResult ;
39
+ fn add_peripheral ( & mut self , pname : & str , padd : & Hash , env : & Env ) -> PatchResult ;
40
40
41
41
/// Remove registers from pname and mark it as derivedFrom pderive.
42
42
/// Update all derivedFrom referencing pname
43
- fn derive_peripheral ( & mut self , pname : & str , pderive : & Yaml ) -> PatchResult ;
43
+ fn derive_peripheral ( & mut self , pname : & str , pderive : & Yaml , env : & Env ) -> PatchResult ;
44
44
45
45
/// Move registers from pold to pnew.
46
46
/// Update all derivedFrom referencing pold
@@ -55,6 +55,7 @@ pub trait DeviceExt {
55
55
pspec : & str ,
56
56
peripheral : & Hash ,
57
57
config : & Config ,
58
+ env : & Env ,
58
59
) -> PatchResult ;
59
60
}
60
61
@@ -64,6 +65,9 @@ impl DeviceExt for Device {
64
65
}
65
66
66
67
fn process ( & mut self , device : & Hash , config : & Config ) -> PatchResult {
68
+ let mut env = Env :: new ( ) ;
69
+ update_env ( & mut env, device) ?;
70
+
67
71
// Handle any deletions
68
72
for pspec in device. str_vec_iter ( "_delete" ) ? {
69
73
self . delete_peripheral ( pspec)
@@ -91,7 +95,7 @@ impl DeviceExt for Device {
91
95
"_peripherals" => {
92
96
for ( pspec, pmod) in val. hash ( ) ? {
93
97
let pspec = pspec. str ( ) ?;
94
- self . modify_peripheral ( pspec, pmod. hash ( ) ?)
98
+ self . modify_peripheral ( pspec, pmod. hash ( ) ?, & env )
95
99
. with_context ( || {
96
100
format ! ( "Modifying peripherals matched to `{pspec}`" )
97
101
} ) ?;
@@ -115,7 +119,7 @@ impl DeviceExt for Device {
115
119
}
116
120
117
121
_ => self
118
- . modify_peripheral ( key, val. hash ( ) ?)
122
+ . modify_peripheral ( key, val. hash ( ) ?, & env )
119
123
. with_context ( || format ! ( "Modifying peripherals matched to `{key}`" ) ) ?,
120
124
}
121
125
}
@@ -130,14 +134,14 @@ impl DeviceExt for Device {
130
134
// Handle any new peripherals (!)
131
135
for ( pname, padd) in device. hash_iter ( "_add" ) {
132
136
let pname = pname. str ( ) ?;
133
- self . add_peripheral ( pname, padd. hash ( ) ?)
137
+ self . add_peripheral ( pname, padd. hash ( ) ?, & env )
134
138
. with_context ( || format ! ( "Adding peripheral `{pname}`" ) ) ?;
135
139
}
136
140
137
141
// Handle any derived peripherals
138
142
for ( pname, pderive) in device. hash_iter ( "_derive" ) {
139
143
let pname = pname. str ( ) ?;
140
- self . derive_peripheral ( pname, pderive)
144
+ self . derive_peripheral ( pname, pderive, & env )
141
145
. with_context ( || format ! ( "Deriving peripheral `{pname}` from `{pderive:?}`" ) ) ?;
142
146
}
143
147
@@ -154,7 +158,7 @@ impl DeviceExt for Device {
154
158
let periphspec = periphspec. str ( ) ?;
155
159
if !periphspec. starts_with ( '_' ) {
156
160
//val["_path"] = device["_path"]; // TODO: check
157
- self . process_peripheral ( periphspec, val. hash ( ) ?, config)
161
+ self . process_peripheral ( periphspec, val. hash ( ) ?, config, & env )
158
162
. with_context ( || format ! ( "According to `{periphspec}`" ) ) ?;
159
163
}
160
164
}
@@ -218,11 +222,11 @@ impl DeviceExt for Device {
218
222
Ok ( ( ) )
219
223
}
220
224
221
- fn modify_peripheral ( & mut self , pspec : & str , pmod : & Hash ) -> PatchResult {
225
+ fn modify_peripheral ( & mut self , pspec : & str , pmod : & Hash , env : & Env ) -> PatchResult {
222
226
let mut modified = HashSet :: new ( ) ;
223
227
let ptags = self . iter_peripherals ( pspec) . collect :: < Vec < _ > > ( ) ;
224
228
if !ptags. is_empty ( ) {
225
- let peripheral_builder = make_peripheral ( pmod, true ) ?;
229
+ let peripheral_builder = make_peripheral ( pmod, true , env ) ?;
226
230
let dim = make_dim_element ( pmod) ?;
227
231
for ptag in ptags {
228
232
modified. insert ( ptag. name . clone ( ) ) ;
@@ -267,12 +271,12 @@ impl DeviceExt for Device {
267
271
Ok ( ( ) )
268
272
}
269
273
270
- fn add_peripheral ( & mut self , pname : & str , padd : & Hash ) -> PatchResult {
274
+ fn add_peripheral ( & mut self , pname : & str , padd : & Hash , env : & Env ) -> PatchResult {
271
275
if self . get_peripheral ( pname) . is_some ( ) {
272
276
return Err ( anyhow ! ( "device already has a peripheral {pname}" ) ) ;
273
277
}
274
278
275
- let pnew = make_peripheral ( padd, false ) ?
279
+ let pnew = make_peripheral ( padd, false , env ) ?
276
280
. name ( pname. to_string ( ) )
277
281
. build ( VAL_LVL ) ?;
278
282
let pnew = if let Some ( dim) = make_dim_element ( padd) ? {
@@ -285,7 +289,7 @@ impl DeviceExt for Device {
285
289
Ok ( ( ) )
286
290
}
287
291
288
- fn derive_peripheral ( & mut self , pname : & str , pderive : & Yaml ) -> PatchResult {
292
+ fn derive_peripheral ( & mut self , pname : & str , pderive : & Yaml , env : & Env ) -> PatchResult {
289
293
let ( pderive, info) = if let Some ( pderive) = pderive. as_str ( ) {
290
294
(
291
295
pderive,
@@ -300,7 +304,7 @@ impl DeviceExt for Device {
300
304
} ) ?;
301
305
(
302
306
pderive,
303
- make_peripheral ( hash, true ) ?. derived_from ( Some ( pderive. into ( ) ) ) ,
307
+ make_peripheral ( hash, true , env ) ?. derived_from ( Some ( pderive. into ( ) ) ) ,
304
308
)
305
309
} else {
306
310
return Err ( anyhow ! ( "derive: incorrect syntax for {pname}" ) ) ;
@@ -415,13 +419,14 @@ impl DeviceExt for Device {
415
419
pspec : & str ,
416
420
peripheral : & Hash ,
417
421
config : & Config ,
422
+ env : & Env ,
418
423
) -> PatchResult {
419
424
// Find all peripherals that match the spec
420
425
let mut pcount = 0 ;
421
426
let ( pspec, ignore) = pspec. spec ( ) ;
422
427
for ptag in self . iter_peripherals ( pspec) {
423
428
pcount += 1 ;
424
- ptag. process ( peripheral, config)
429
+ ptag. process ( peripheral, config, env . clone ( ) )
425
430
. with_context ( || format ! ( "Processing peripheral `{}`" , ptag. name) ) ?;
426
431
}
427
432
if !ignore && pcount == 0 {
0 commit comments