@@ -12,26 +12,38 @@ use util::{self, ToSanitizedSnakeCase, ToSanitizedUpperCase, BITS_PER_BYTE};
12
12
use generate:: register;
13
13
14
14
pub fn render (
15
- p : & Peripheral ,
15
+ p_original : & Peripheral ,
16
16
all_peripherals : & [ Peripheral ] ,
17
17
defaults : & Defaults ,
18
18
nightly : bool ,
19
19
) -> Result < Vec < Tokens > > {
20
20
let mut out = vec ! [ ] ;
21
21
22
+ let p_derivedfrom = p_original. derived_from . as_ref ( ) . and_then ( |s| {
23
+ all_peripherals. iter ( ) . find ( |x| x. name == * s)
24
+ } ) ;
25
+
26
+ let p_merged = p_derivedfrom. map ( |x| p_original. derive_from ( x) ) ;
27
+ let p = p_merged. as_ref ( ) . unwrap_or ( p_original) ;
28
+
29
+ if p_original. derived_from . is_some ( ) && p_derivedfrom. is_none ( ) {
30
+ eprintln ! ( "Couldn't find derivedFrom original: {} for {}, skipping" ,
31
+ p_original. derived_from. as_ref( ) . unwrap( ) , p_original. name) ;
32
+ return Ok ( out) ;
33
+ }
34
+
22
35
let name_pc = Ident :: new ( & * p. name . to_sanitized_upper_case ( ) ) ;
23
36
let address = util:: hex ( p. base_address ) ;
24
37
let description =
25
38
util:: escape_brackets ( util:: respace ( p. description . as_ref ( ) . unwrap_or ( & p. name ) ) . as_ref ( ) ) ;
39
+ let derive_regs = p_derivedfrom. is_some ( ) && p_original. registers . is_none ( ) ;
40
+
26
41
27
42
let name_sc = Ident :: new ( & * p. name . to_sanitized_snake_case ( ) ) ;
28
- let ( base, derived) = if let Some ( base) = p. derived_from . as_ref ( ) {
29
- // TODO Verify that base exists
30
- // TODO We don't handle inheritance style `derivedFrom`, we should raise
31
- // an error in that case
32
- ( Ident :: new ( & * base. to_sanitized_snake_case ( ) ) , true )
43
+ let base = if derive_regs {
44
+ Ident :: new ( & * p_derivedfrom. unwrap ( ) . name . to_sanitized_snake_case ( ) )
33
45
} else {
34
- ( name_sc. clone ( ) , false )
46
+ name_sc. clone ( )
35
47
} ;
36
48
37
49
// Insert the peripheral structure
@@ -57,9 +69,9 @@ pub fn render(
57
69
}
58
70
} ) ;
59
71
60
- // Derived peripherals do not require re-implementation, and will instead
61
- // use a single definition of the non-derived version
62
- if derived {
72
+ // Derived peripherals may not require re-implementation, and will instead
73
+ // use a single definition of the non-derived version.
74
+ if derive_regs {
63
75
return Ok ( out) ;
64
76
}
65
77
0 commit comments