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
use crate::svd::{array::names, Device, Peripheral};
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};

use log::debug;
use std::fs::File;
use std::io::Write;
use std::path::Path;

use crate::config::{Config, Target};
use crate::util::{self, ident};
use anyhow::{Context, Result};

use crate::generate::{interrupt, peripheral};

/// Whole device generation
pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<TokenStream> {
    let index = svd_parser::expand::Index::create(d);
    let mut out = TokenStream::new();

    let commit_info = {
        let tmp = include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"));

        if tmp.is_empty() {
            " (untracked)"
        } else {
            tmp
        }
    };

    if config.target == Target::Msp430 {
        out.extend(quote! {
            #![feature(abi_msp430_interrupt)]
        });
    }

    if !config.skip_crate_attributes {
        let doc = format!(
            "Peripheral access API for {0} microcontrollers \
             (generated using svd2rust v{1}{commit_info})\n\n\
             You can find an overview of the generated API [here].\n\n\
             API features to be included in the [next] svd2rust \
             release can be generated by cloning the svd2rust [repository], \
             checking out the above commit, and running `cargo doc --open`.\n\n\
             [here]: https://docs.rs/svd2rust/{1}/svd2rust/#peripheral-api\n\
             [next]: https://github.com/rust-embedded/svd2rust/blob/master/CHANGELOG.md#unreleased\n\
             [repository]: https://github.com/rust-embedded/svd2rust",
            d.name.to_uppercase(),
            env!("CARGO_PKG_VERSION"),
        );

        out.extend(quote! { #![doc = #doc] });
    }

    if !config.make_mod && !config.skip_crate_attributes {
        out.extend(quote! {
            // Explicitly allow a few warnings that may be verbose
            #![allow(non_camel_case_types)]
            #![allow(non_snake_case)]
            #![no_std]
        });
    }

    out.extend(quote! {
        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;
    }

    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();
    match config.target {
        Target::CortexM => {
            if config.reexport_core_peripherals {
                let fpu = fpu_present.then(|| quote!(FPU,));
                out.extend(quote! {
                    pub use cortex_m::peripheral::Peripherals as CorePeripherals;
                    pub use cortex_m::peripheral::{
                        CBP, CPUID, DCB, DWT, FPB, #fpu ITM, MPU, NVIC, SCB, SYST, TPIU,
                    };
                });
            }

            if config.reexport_interrupt {
                out.extend(quote! {
                    #[cfg(feature = "rt")]
                    pub use cortex_m_rt::interrupt;
                    #[cfg(feature = "rt")]
                    pub use self::Interrupt as interrupt;
                });
            }
        }

        Target::Msp430 => {
            // XXX: Are there any core peripherals, really? Requires bump of msp430 crate.
            // pub use msp430::peripheral::Peripherals as CorePeripherals;
            if config.reexport_interrupt {
                out.extend(quote! {
                    #[cfg(feature = "rt")]
                    pub use msp430_rt::interrupt;
                    #[cfg(feature = "rt")]
                    pub use self::Interrupt as interrupt;
                });
            }
        }

        Target::Mips => {
            if config.reexport_interrupt {
                out.extend(quote! {
                    #[cfg(feature = "rt")]
                    pub use mips_rt::interrupt;
                });
            }
        }

        _ => {}
    }

    let generic_file = include_str!("generic.rs");
    let generic_atomic_file = include_str!("generic_atomic.rs");
    if config.generic_mod {
        let mut file = File::create(
            config
                .output_dir
                .as_deref()
                .unwrap_or(Path::new("."))
                .join("generic.rs"),
        )?;
        writeln!(file, "{generic_file}")?;
        if config.atomics {
            if let Some(atomics_feature) = config.atomics_feature.as_ref() {
                writeln!(file, "#[cfg(feature = \"{atomics_feature}\")]")?;
            }
            writeln!(file, "\n{generic_atomic_file}")?;
        }

        if !config.make_mod {
            out.extend(quote! {
                #[allow(unused_imports)]
                use generic::*;
                #[doc="Common register and bit access and modify traits"]
                pub mod generic;
            });
        }
    } else {
        let mut tokens = syn::parse_file(generic_file)?.into_token_stream();
        if config.atomics {
            if let Some(atomics_feature) = config.atomics_feature.as_ref() {
                quote!(#[cfg(feature = #atomics_feature)]).to_tokens(&mut tokens);
            }
            syn::parse_file(generic_atomic_file)?.to_tokens(&mut tokens);
        }

        out.extend(quote! {
            #[allow(unused_imports)]
            use generic::*;
            ///Common register and bit access and modify traits
            pub mod generic {
                #tokens
            }
        });
    }

    debug!("Rendering interrupts");
    out.extend(interrupt::render(
        config.target,
        &d.peripherals,
        device_x,
        config,
    )?);

    let feature_format = config.ident_formats.get("peripheral_feature").unwrap();
    for p in &d.peripherals {
        if config.target == Target::CortexM
            && core_peripherals.contains(&p.name.to_uppercase().as_ref())
        {
            // Core peripherals are handled above
            continue;
        }

        debug!("Rendering peripheral {}", p.name);
        let periph = peripheral::render(p, &index, config).with_context(|| {
            let group_name = p.group_name.as_deref().unwrap_or("No group name");
            let mut context_string =
                format!("can't render peripheral '{}', group '{group_name}'", p.name);
            if let Some(dname) = p.derived_from.as_ref() {
                context_string += &format!(", derived from: '{dname}'");
            }
            context_string
        })?;

        out.extend(periph);

        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 mut feature_attribute = TokenStream::new();
        if config.feature_group && p.group_name.is_some() {
            let feature_name = feature_format.apply(p.group_name.as_deref().unwrap());
            feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] })
        };

        let span = Span::call_site();
        match p {
            Peripheral::Single(_p) => {
                let p_name = util::name_of(p, config.ignore_groups);
                let p_feature = feature_format.apply(&p_name);
                let p_ty = ident(&p_name, config, "peripheral", span);
                let p_singleton = ident(&p_name, config, "peripheral_singleton", span);
                if config.feature_peripheral {
                    feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] })
                };
                fields.extend(quote! {
                    #[doc = #p_name]
                    #feature_attribute
                    pub #p_singleton: #p_ty,
                });
                exprs.extend(
                    quote!(#feature_attribute #p_singleton: #p_ty { _marker: PhantomData },),
                );
            }
            Peripheral::Array(p, dim_element) => {
                for p_name in names(p, dim_element) {
                    let p_feature = feature_format.apply(&p_name);
                    let p_ty = ident(&p_name, config, "peripheral", span);
                    let p_singleton = ident(&p_name, config, "peripheral_singleton", span);
                    if config.feature_peripheral {
                        feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] })
                    };
                    fields.extend(quote! {
                        #[doc = #p_name]
                        #feature_attribute
                        pub #p_singleton: #p_ty,
                    });
                    exprs.extend(
                        quote!(#feature_attribute #p_singleton: #p_ty { _marker: PhantomData },),
                    );
                }
            }
        }
    }

    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 {
            /// Returns all the peripherals *once*.
            #[cfg(feature = "critical-section")]
            #[inline]
            pub fn take() -> Option<Self> {
                critical_section::with(|_| {
                    // SAFETY: We are in a critical section, so we have exclusive access
                    // to `DEVICE_PERIPHERALS`.
                    if unsafe { DEVICE_PERIPHERALS } {
                        return None
                    }

                    // SAFETY: `DEVICE_PERIPHERALS` is set to `true` by `Peripherals::steal`,
                    // ensuring the peripherals can only be returned once.
                    Some(unsafe { Peripherals::steal() })
                })
            }

            /// Unchecked version of `Peripherals::take`.
            ///
            /// # Safety
            ///
            /// Each of the returned peripherals must be used at most once.
            #[inline]
            pub unsafe fn steal() -> Self {
                DEVICE_PERIPHERALS = true;

                Peripherals {
                    #exprs
                }
            }
        }
    });

    Ok(out)
}