Commit 040f9d4
Merge #592
592: add code generation support of peripheral arrays r=burrbull a=duskmoon314
## Brief Intro
- generate ArrayProxy of peripherals when const_generic is true
- generate separate struct of peripherals when const_generic is false
close issue: #492
## Design
**Notice:** This is my first try of implement this feature. Please provide suggestions for this PR to better implement this feature.
This PR aims to achieve what issue #492 is asking for. This PR implements a basic try of generating codes depending on the `const_generic` flag.
Let's say we have an svd file with descriptions of peripherals like this:
```XML
<peripheral>
<dim>2</dim>
<dimIncrement>0x100</dimIncrement>
<name>PTEST[%s]</name>
<groupName>PGroup</groupName>
<baseAddress>0x20000000</baseAddress>
<addressBlock>
<offset>0</offset>
<size>0x100</size>
<usage>registers</usage>
</addressBlock>
<registers>
...
</registers>
</peripheral>
```
If the `const_generic` is `false`, there is no `ArrayProxy`. So I generate separate structs and only one mod:
```rust
pub struct PTEST0 {
_marker: PhantomData<*const ()>,
}
...
impl PTEST0 {
#[doc = r"Pointer to the register block"]
pub const PTR: *const ptest::RegisterBlock = 0x2000_0000 as *const _;
...
}
impl Deref for PTEST0 {
type Target = ptest::RegisterBlock;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { &*Self::PTR }
}
}
pub struct PTEST1 {
_marker: PhantomData<*const ()>,
}
...
impl PTEST1 {
#[doc = r"Pointer to the register block"]
pub const PTR: *const ptest::RegisterBlock = 0x2000_0100 as *const _;
...
}
impl Deref for PTEST1 {
type Target = ptest::RegisterBlock;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { &*Self::PTR }
}
}
#[doc = "PTEST"]
pub mod ptest;
```
**NOTICE:** The following design is actually incorrect and has been removed. See the following comment.
<details>
If the `const_generic` is `true`, we can use `ArrayProxy`. So I generate code like this:
```rust
pub struct PTEST {
_marker: PhantomData<*const ()>,
}
...
pub struct Peripherals {
#[doc = "PTEST"]
pub PTEST: crate::ArrayProxy<PTEST, 2usize, 0x0100>,
...
impl Peripherals {
...
pub unsafe fn steal() -> Self {
DEVICE_PERIPHERALS = true;
Peripherals {
PTEST: crate::ArrayProxy::new(),
```
Since `_array` is a private field of `ArrayProxy`, I add a `new` method to implement the `steal` method of `Peripherals`.
</details>
I also add the support of using `name_of` on `MaybeArray<PeripheralInfo>` to achieve this implementation.
Well, I haven't fully tested this implementation yet. So there may be problems that need to be solved.
Co-authored-by: Campbell He <[email protected]>
Co-authored-by: Campbell He <[email protected]>File tree
5 files changed
+124
-48
lines changed- src
- generate
5 files changed
+124
-48
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | 1 | | |
3 | 2 | | |
4 | 3 | | |
| |||
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
15 | | - | |
| 14 | + | |
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
| |||
27 | 26 | | |
28 | 27 | | |
29 | 28 | | |
30 | | - | |
31 | | - | |
| 29 | + | |
32 | 30 | | |
33 | 31 | | |
34 | 32 | | |
35 | 33 | | |
36 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
236 | 237 | | |
237 | 238 | | |
238 | 239 | | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
| 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 | + | |
246 | 265 | | |
247 | 266 | | |
248 | 267 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | | - | |
| 6 | + | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
46 | 47 | | |
47 | | - | |
| 48 | + | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
52 | | - | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
| 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 | + | |
63 | 86 | | |
64 | | - | |
| 87 | + | |
| 88 | + | |
65 | 89 | | |
66 | | - | |
67 | | - | |
68 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
69 | 95 | | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
75 | 103 | | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
76 | 109 | | |
77 | | - | |
78 | | - | |
| 110 | + | |
79 | 111 | | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
85 | 115 | | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
90 | 138 | | |
91 | | - | |
| 139 | + | |
92 | 140 | | |
93 | 141 | | |
94 | 142 | | |
| |||
195 | 243 | | |
196 | 244 | | |
197 | 245 | | |
198 | | - | |
199 | | - | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
200 | 249 | | |
201 | 250 | | |
202 | 251 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
221 | 222 | | |
222 | 223 | | |
223 | 224 | | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
228 | 229 | | |
229 | 230 | | |
230 | 231 | | |
| |||
445 | 446 | | |
446 | 447 | | |
447 | 448 | | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
0 commit comments