ecos-macros 0.1.4

Rust SDK Macros for ECOS StarrySky C1
Documentation
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
extern crate proc_macro;

mod prelude;

use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::{ToTokens, quote};
use std::collections::HashMap;
use syn::{
    ItemFn, Meta, ReturnType, Token, parse::Parser, parse_macro_input, punctuated::Punctuated,
};

use crate::prelude::generate_prelude_imports;

/// 直接执行版本:
///
/// #[rust_main]
/// fn app() -> ! { ... }
#[proc_macro_attribute]
pub fn rust_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let input_fn = parse_macro_input!(item as ItemFn);

    if !input_fn.sig.inputs.is_empty() {
        panic!("Function marked with #[rust_main] must have no parameters");
    }

    let docs: Vec<TokenStream2> = input_fn
        .attrs
        .iter()
        .filter(|attr| attr.path().is_ident("doc"))
        .map(|attr| {
            let attr_tokens = attr.to_token_stream();
            quote! { #attr_tokens }
        })
        .collect();

    match &input_fn.sig.output {
        syn::ReturnType::Default => panic!("Function marked with #[rust_main] must return -> !"),
        syn::ReturnType::Type(_, ty) => {
            if let syn::Type::Never(_) = &**ty {
                // OK
            } else {
                panic!("Function marked with #[rust_main] must return -> !");
            }
        }
    }

    let fn_block = &input_fn.block;

    let prelude = generate_prelude_imports();

    let dev_debug = if cfg!(feature = "dev") {
        quote! {
            loop {
                if '\n' as u8 == ecos_ssc1::Uart::read_byte_blocking() {
                    break;
                }
            }
        }
    } else {
        quote! {}
    };

    let init_alloc = if cfg!(feature = "alloc") {
        quote! {
            unsafe {
                ::ecos_ssc1::features::alloc::init();
            }
        }
    } else {
        quote! {}
    };

    let expanded = quote! {
        #prelude

        #(#docs)*
        #[unsafe(no_mangle)]
        pub extern "C" fn main() -> ! {
            #dev_debug

            #init_alloc
            #fn_block
        }
    };

    TokenStream::from(expanded)
}

/// 默认初始化 UART
///
/// 默认选项:
///     None
///
/// 可用选项:
///     - no_uart
///     - no_gpio
///     - tick
///     - qspi || qspi(clkdiv=0) || qspi(0)
///     - on == 一键开启all
///     - off == 一键关闭all == rust_main
///
/// #[ecos_main]
/// #[ecos_main(no_uart)]
/// #[ecos_main(tick)]
/// #[ecos_main(no_uart, tick, ...)]
#[proc_macro_attribute]
pub fn ecos_main(attr: TokenStream, item: TokenStream) -> TokenStream {
    let parser = Punctuated::<Meta, Token![,]>::parse_terminated;
    let attr_args = parser.parse(attr).unwrap_or_default();

    let input_fn = parse_macro_input!(item as ItemFn);

    if !input_fn.sig.inputs.is_empty() {
        panic!("Function marked with #[ecos_main] must have no parameters");
    }

    let mut qspi_clkdiv: Option<u32> = None;

    for arg in &attr_args {
        match arg {
            Meta::Path(path) => {
                // 简单标识符,如 qspi
                if let Some(ident) = path.get_ident() {
                    if ident == "qspi" {
                        qspi_clkdiv = Some(0);
                    }
                }
            }
            Meta::List(list) => {
                // 带括号的参数,如 qspi(clkdiv=2) 或 qspi(2)
                if let Some(ident) = list.path.get_ident() {
                    if ident == "qspi" {
                        let args = parse_qspi_args(&list.tokens);
                        qspi_clkdiv = Some(args.clkdiv);
                    }
                }
            }
            Meta::NameValue(_) => {
                panic!("ecos_main does not support name=value syntax for qspi");
            }
        }
    }

    let docs: Vec<TokenStream2> = input_fn
        .attrs
        .iter()
        .filter(|attr| attr.path().is_ident("doc"))
        .map(|attr| {
            let attr_tokens = attr.to_token_stream();
            quote! { #attr_tokens }
        })
        .collect();

    match &input_fn.sig.output {
        ReturnType::Default => panic!("Function marked with #[ecos_main] must return -> !"),
        ReturnType::Type(_, ty) => {
            if let syn::Type::Never(_) = &**ty {
                // OK
            } else {
                panic!("Function marked with #[ecos_main] must return -> !");
            }
        }
    }

    let fn_block = &input_fn.block;

    let mut pm = PeripheralManager::new();

    // 注册到off的:默认会初始化(default_enabled = true),禁用就得:no_xxx
    pm.register("uart", true, || {
        if !cfg!(feature = "log") {
            quote! {
                unsafe {
                    ::ecos_ssc1::bindings::sys_uart_init();
                }
            }
        } else {
            quote! {}
        }
    });

    // 注册到on的:默认不会初始化(default_enabled = false),开启就得:xxx
    pm.register("tick", false, || {
        if !cfg!(feature = "log") {
            quote! {
                unsafe {
                    ::ecos_ssc1::bindings::sys_tick_init();
                }
            }
        } else {
            quote! {}
        }
    });

    pm.register("qspi", false, {
        let clkdiv = qspi_clkdiv;
        move || {
            if clkdiv.is_some() {
                let clkdiv_val = clkdiv.unwrap_or(0);
                quote! {
                    unsafe {
                        ::ecos_ssc1::bindings::qspi_init(::ecos_ssc1::bindings::qspi_config_t {
                            clkdiv: #clkdiv_val
                        });
                    }
                }
            } else {
                quote! {}
            }
        }
    });

    // 因为编译优化的原因,不调用一个函数对应的C就直接跳过了,导致其他函数找不到
    pm.register("gpio", true, || {
        quote! { unsafe { ::ecos_ssc1::bindings::gpio_config(
            // 16位全部输出
            &::ecos_ssc1::bindings::gpio_config_t {
                pin_bit_mask: 0xFFFF,
                mode: ::ecos_ssc1::bindings::gpio_mode_t_GPIO_MODE_OUTPUT,
            }
        ); } }
    });

    // on预设:开启所有注册到on的(默认不会初始化的)
    pm.add_preset("on", |pm| {
        // 有 on 标签就开启所有 default_enabled = false 的外设(注册到on的)
        pm.enable("tick");
        pm.enable("qspi");
    });

    // off预设:禁用所有注册到off的(默认会初始化的)
    pm.add_preset("off", |pm| {
        // 有 off 标签禁用所有 default_enabled = true 的外设(注册到off的)
        pm.disable("uart");
        pm.disable("gpio");
    });

    // ================ 处理传入的宏选项 ================
    for arg in attr_args {
        match arg {
            Meta::Path(path) => {
                if let Some(ident) = path.get_ident() {
                    let ident_str = ident.to_string();
                    // 跳过之前已处理的 qspi
                    if ident_str == "qspi" {
                        continue;
                    }

                    pm.process_option(&ident_str);
                }
            }
            Meta::List(list) => {
                // 只处理 qspi(...) 格式,其他列表格式不支持
                if let Some(ident) = list.path.get_ident() {
                    if ident != "qspi" {
                        panic!(
                            "ecos_main only supports qspi with parameters, other options must be simple identifiers"
                        );
                    }
                } else {
                    panic!(
                        "ecos_main only supports simple identifiers as options or qspi(...) syntax"
                    );
                }
            }
            Meta::NameValue(_) => {
                panic!("ecos_main does not support name=value syntax (except qspi(clkdiv=value))");
            }
        }
    }

    let init_pm = pm.generate_init_code();

    let dev_debug = if cfg!(feature = "dev") {
        quote! {
            loop {
                if '\n' as u8 == ecos_ssc1::Uart::read_byte_blocking() {
                    break;
                }
            }
        }
    } else {
        quote! {}
    };

    let init_log = if cfg!(feature = "log") {
        quote! {
            unsafe {
                // 启用log由于要打印时间戳以及初始化uart,附带开启tick ...
                ::ecos_ssc1::bindings::sys_uart_init();
                println!("asdsadas");
                ::ecos_ssc1::bindings::sys_tick_init();
                ::ecos_ssc1::features::log::init_logger();
            }
        }
    } else {
        quote! {}
    };

    let init_alloc = if cfg!(feature = "alloc") {
        quote! {
            unsafe {
                ::ecos_ssc1::features::alloc::init();
            }
        }
    } else {
        quote! {}
    };

    let prelude = generate_prelude_imports();

    let expanded = quote! {
        #prelude

        #(#docs)*
        #[unsafe(no_mangle)]
        pub extern "C" fn main() -> ! {
            #init_pm
            #init_log

            #dev_debug

            #init_alloc
            #fn_block
        }
    };

    TokenStream::from(expanded)
}

struct PeripheralManager {
    peripherals: HashMap<String, PeripheralConfig>,
    presets: HashMap<String, Box<dyn Fn(&mut PeripheralManager)>>,
}

impl PeripheralManager {
    fn new() -> Self {
        Self {
            peripherals: HashMap::new(),
            presets: HashMap::new(),
        }
    }

    /// 注册外设
    /// - name: 外设名称
    /// - default_enabled:
    ///   - true: 注册到off,默认会初始化
    ///   - false: 注册到on,默认不会初始化
    fn register<F>(&mut self, name: &str, default_enabled: bool, init_fn: F)
    where
        F: Fn() -> TokenStream2 + 'static,
    {
        self.peripherals.insert(
            name.to_string(),
            PeripheralConfig {
                enabled: default_enabled,
                init_fn: Box::new(init_fn),
            },
        );
    }

    fn add_preset<F>(&mut self, name: &str, preset_fn: F)
    where
        F: Fn(&mut PeripheralManager) + 'static,
    {
        self.presets.insert(name.to_string(), Box::new(preset_fn));
    }

    fn enable(&mut self, name: &str) {
        if let Some(config) = self.peripherals.get_mut(name) {
            config.enabled = true;
        }
    }

    fn disable(&mut self, name: &str) {
        if let Some(config) = self.peripherals.get_mut(name) {
            config.enabled = false;
        }
    }

    fn process_option(&mut self, option: &str) {
        match option {
            // 预设:on - 开启所有注册到on的(开启所有的默认不会初始化的)
            "on" => {
                let f = |pm: &mut PeripheralManager| {
                    pm.enable("tick");
                };
                f(self);
            }
            // 预设:off - 禁用所有注册到off的(关闭所有的默认会初始化的)
            "off" => {
                let f = |pm: &mut PeripheralManager| {
                    pm.disable("uart");
                };
                f(self);
            }
            _ => {
                // 检查是否是 no_xxx 格式(禁用默认初始化的)
                if let Some(periph_name) = option.strip_prefix("no_") {
                    // 禁用注册到off的外设(默认会初始化的)
                    self.disable(periph_name);
                } else {
                    // 否则是开启注册到on的外设(默认不会初始化的)
                    self.enable(option);
                }
            }
        }
    }

    fn generate_init_code(&self) -> TokenStream2 {
        let mut code = TokenStream2::new();

        for (_, config) in &self.peripherals {
            if config.enabled {
                let init_code = (config.init_fn)();
                code.extend(init_code);
            }
        }

        code
    }
}

struct PeripheralConfig {
    enabled: bool,
    init_fn: Box<dyn Fn() -> TokenStream2>,
}

fn parse_qspi_args(tokens: &TokenStream2) -> QspiArgs {
    let mut args = QspiArgs { clkdiv: 0 };

    if let Ok(value) = syn::parse::<syn::LitInt>(tokens.clone().into()) {
        args.clkdiv = value.base10_parse::<u32>().unwrap_or(0);
        return args;
    }

    let parser = Punctuated::<syn::Meta, Token![,]>::parse_terminated;
    if let Ok(meta_list) = parser.parse(tokens.clone().into()) {
        for meta in meta_list {
            match meta {
                syn::Meta::NameValue(nv) => {
                    if let Some(ident) = nv.path.get_ident() {
                        if ident == "clkdiv" {
                            if let syn::Expr::Lit(expr_lit) = &nv.value {
                                if let syn::Lit::Int(lit_int) = &expr_lit.lit {
                                    args.clkdiv = lit_int.base10_parse::<u32>().unwrap_or(0);
                                } else {
                                    panic!("clkdiv must be an integer literal");
                                }
                            } else {
                                panic!("clkdiv must be a literal");
                            }
                        } else {
                            panic!("qspi only supports clkdiv parameter");
                        }
                    }
                }
                _ => {
                    panic!("qspi only supports clkdiv=value syntax");
                }
            }
        }
        return args;
    }

    args
}

struct QspiArgs {
    clkdiv: u32,
}