-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathdevice.rs
More file actions
261 lines (231 loc) · 7.51 KB
/
Copy pathdevice.rs
File metadata and controls
261 lines (231 loc) · 7.51 KB
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
use crate::svd::Device;
use proc_macro2::{Ident, Span, TokenStream};
use quote::ToTokens;
use std::fs::File;
use std::io::Write;
use crate::errors::*;
use crate::modules::Module;
use crate::util::{self, ToSanitizedUpperCase};
use crate::Target;
use crate::generate::{interrupt, peripheral};
/// Whole device generation
pub fn render(
d: &Device,
target: Target,
nightly: bool,
generic_mod: bool,
device_x: &mut String,
) -> Result<Module> {
let mut out = Module::new("lib", "");
let doc = format!(
"Peripheral access API for {0} microcontrollers \
(generated using svd2rust v{1})\n\n\
You can find an overview of the API [here].\n\n\
[here]: https://docs.rs/svd2rust/{1}/svd2rust/#peripheral-api",
d.name.to_uppercase(),
env!("CARGO_PKG_VERSION")
);
if target == Target::Msp430 {
out.extend(quote! {
#![feature(abi_msp430_interrupt)]
});
}
out.extend(quote! {
#![doc = #doc]
// Deny a subset of warnings
#![deny(const_err)]
#![deny(dead_code)]
#![deny(improper_ctypes)]
#![deny(missing_docs)]
#![deny(no_mangle_generic_items)]
#![deny(non_shorthand_field_patterns)]
#![deny(overflowing_literals)]
#![deny(path_statements)]
#![deny(patterns_in_fns_without_body)]
#![deny(private_in_public)]
#![deny(unconditional_recursion)]
#![deny(unused_allocation)]
#![deny(unused_comparisons)]
#![deny(unused_parens)]
#![deny(while_true)]
// Explicitly allow a few warnings that may be verbose
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![no_std]
});
match target {
Target::CortexM => {
out.extend(quote! {
extern crate cortex_m;
#[cfg(feature = "rt")]
extern crate cortex_m_rt;
});
}
Target::Msp430 => {
out.extend(quote! {
extern crate msp430;
#[cfg(feature = "rt")]
extern crate msp430_rt;
});
}
Target::RISCV => {
out.extend(quote! {
extern crate riscv;
#[cfg(feature = "rt")]
extern crate riscv_rt;
});
}
Target::None => {}
}
out.extend(quote! {
extern crate bare_metal;
extern crate vcell;
use core::ops::Deref;
use core::marker::PhantomData;
});
// Retaining the previous assumption
let mut fpu_present = true;
if let Some(cpu) = d.cpu.as_ref() {
let bits = util::unsuffixed(u64::from(cpu.nvic_priority_bits));
out.extend(quote! {
///Number available in the NVIC for configuring priority
pub const NVIC_PRIO_BITS: u8 = #bits;
});
fpu_present = cpu.fpu_present;
}
out.extend(interrupt::render(target, &d.peripherals, device_x)?);
let core_peripherals: &[_] = if fpu_present {
&[
"CBP", "CPUID", "DCB", "DWT", "FPB", "FPU", "ITM", "MPU", "NVIC", "SCB", "SYST", "TPIU",
]
} else {
&[
"CBP", "CPUID", "DCB", "DWT", "FPB", "ITM", "MPU", "NVIC", "SCB", "SYST", "TPIU",
]
};
let mut fields = TokenStream::new();
let mut exprs = TokenStream::new();
if target == Target::CortexM {
out.extend(quote! {
pub use cortex_m::peripheral::Peripherals as CorePeripherals;
#[cfg(feature = "rt")]
pub use cortex_m_rt::interrupt;
#[cfg(feature = "rt")]
pub use self::Interrupt as interrupt;
});
if fpu_present {
out.extend(quote! {
pub use cortex_m::peripheral::{
CBP, CPUID, DCB, DWT, FPB, FPU, ITM, MPU, NVIC, SCB, SYST, TPIU,
};
});
} else {
out.extend(quote! {
pub use cortex_m::peripheral::{
CBP, CPUID, DCB, DWT, FPB, ITM, MPU, NVIC, SCB, SYST, TPIU,
};
});
}
}
if target == Target::Msp430 {
out.extend(quote! {
// XXX: Are there any core peripherals, really? Requires bump of msp430 crate.
// pub use msp430::peripheral::Peripherals as CorePeripherals;
#[cfg(feature = "rt")]
pub use msp430_rt::interrupt;
#[cfg(feature = "rt")]
pub use self::Interrupt as interrupt;
});
}
let generic_file = std::str::from_utf8(include_bytes!("generic.rs")).unwrap();
if generic_mod {
writeln!(File::create("generic.rs").unwrap(), "{}", generic_file).unwrap();
} else {
let mut generic_module = Module::new(
"generic",
"Common register and bit access and modify traits",
);
generic_module.out.extend(quote! {
#[allow(unused_imports)]
use generic::*;
});
generic_module.extend(syn::parse_file(generic_file).unwrap().into_token_stream());
out.push_module(generic_module);
}
for p in &d.peripherals {
if target == Target::CortexM && core_peripherals.contains(&&*p.name.to_uppercase()) {
// Core peripherals are handled above
continue;
}
out.push_module(peripheral::render(
p,
&d.peripherals,
&d.default_register_properties,
nightly,
)?);
if p.registers
.as_ref()
.map(|v| &v[..])
.unwrap_or(&[])
.is_empty()
&& p.derived_from.is_none()
{
// No register block will be generated so don't put this peripheral
// in the `Peripherals` struct
continue;
}
let p = p.name.to_sanitized_upper_case();
let id = Ident::new(&p, Span::call_site());
fields.extend(quote! {
#[doc = #p]
pub #id: #id,
});
exprs.extend(quote!(#id: #id { _marker: PhantomData },));
}
let span = Span::call_site();
let take = match target {
Target::CortexM => Some(Ident::new("cortex_m", span)),
Target::Msp430 => Some(Ident::new("msp430", span)),
Target::RISCV => Some(Ident::new("riscv", span)),
Target::None => None,
}
.map(|krate| {
quote! {
///Returns all the peripherals *once*
#[inline]
pub fn take() -> Option<Self> {
#krate::interrupt::free(|_| {
if unsafe { DEVICE_PERIPHERALS } {
None
} else {
Some(unsafe { Peripherals::steal() })
}
})
}
}
});
out.extend(quote! {
// NOTE `no_mangle` is used here to prevent linking different minor versions of the device
// crate as that would let you `take` the device peripherals more than once (one per minor
// version)
#[no_mangle]
static mut DEVICE_PERIPHERALS: bool = false;
///All the peripherals
#[allow(non_snake_case)]
pub struct Peripherals {
#fields
}
impl Peripherals {
#take
///Unchecked version of `Peripherals::take`
#[inline]
pub unsafe fn steal() -> Self {
DEVICE_PERIPHERALS = true;
Peripherals {
#exprs
}
}
}
});
Ok(out)
}