rmk-macro 0.8.0

Proc-macro crate of RMK
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
//! Add `bind_interrupts!` boilerplate of RMK, including USB or BLE
//!

use std::collections::HashSet;

use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use rmk_config::resolved::Hardware;
use rmk_config::resolved::hardware::{BoardConfig, InputDeviceConfig, UniBodyConfig};
use syn::{ItemFn, ItemMod};

use crate::codegen::display::expand_display_interrupt;
use crate::codegen::feature::{get_rmk_features, is_feature_enabled};
use crate::codegen::input_device::iqs5xx::expand_iqs5xx_interrupts;
use crate::codegen::override_helper::{Overwritten, find_overwritten};

/// Does this function override the generated `bind_interrupts!` boilerplate?
///
/// Two markers are accepted:
/// - `#[Override(bind_interrupt)]` / `#[Overwritten(bind_interrupt)]` (the form the stm32h7
///   example documents), selected through the shared override matcher so inert attributes and
///   `cfg` gating behave like every other override;
/// - the legacy bare `#[bind_interrupt]` attribute (the original syntax), matched exactly as
///   before for backward compatibility: it must be the function's only attribute.
fn is_bind_interrupt_override(item_fn: &ItemFn) -> bool {
    let current = matches!(
        find_overwritten(item_fn),
        Some(Ok(Overwritten::BindInterrupt))
    );
    let legacy = item_fn.attrs.len() == 1
        && item_fn.attrs[0]
            .meta
            .path()
            .get_ident()
            .is_some_and(|i| i == "bind_interrupt");
    current || legacy
}

/// Expand `bind_interrupt!` stuffs, and other code before `main` function
pub(crate) fn expand_bind_interrupt(hardware: &Hardware, item_mod: &ItemMod) -> TokenStream2 {
    // If there is a function marked as the bind_interrupt override, use its body
    if let Some((_, items)) = &item_mod.content {
        items
            .iter()
            .find_map(|item| {
                if let syn::Item::Fn(item_fn) = &item
                    && is_bind_interrupt_override(item_fn)
                {
                    let content = &item_fn.block.stmts;
                    return Some(quote! {
                        #(#content)*
                    });
                }
                None
            })
            .unwrap_or(bind_interrupt_default(hardware, item_mod))
    } else {
        bind_interrupt_default(hardware, item_mod)
    }
}

pub(crate) fn find_extern_irqs(item_mod: &ItemMod) -> Vec<TokenStream2> {
    let mut extern_irqs: Vec<TokenStream2> = Vec::new();
    if let Some((_, items)) = &item_mod.content {
        items.iter().for_each(|item| {
            if let syn::Item::Macro(item_macro) = &item
                && item_macro.mac.path.is_ident("add_interrupt")
            {
                extern_irqs.push(item_macro.mac.tokens.clone());
            }
        });
    }
    extern_irqs
}

/// Expand default `bind_interrupt!` for different chips and nrf-sdc config for nRF52
pub(crate) fn bind_interrupt_default(hardware: &Hardware, item_mod: &ItemMod) -> TokenStream2 {
    let extern_irqs_vec = find_extern_irqs(item_mod);
    let extern_irqs = if extern_irqs_vec.is_empty() {
        quote! {}
    } else {
        quote! {
            #(#extern_irqs_vec)*
        }
    };

    let chip = &hardware.chip;
    let board = &hardware.board;
    let communication = &hardware.communication;

    let display_config = match board {
        BoardConfig::UniBody(_) => hardware.display.as_ref(),
        BoardConfig::Split(split_config) => split_config.central.display.as_ref(),
    };
    let display_interrupt = if let Some(display_config) = display_config {
        expand_display_interrupt(&chip.series, display_config)
    } else {
        quote! {}
    };

    // IQS5xx devices on the unibody / central side need an I²C interrupt
    // binding so the async I²C driver can service the bus.
    let iqs5xx_config = match board {
        BoardConfig::UniBody(UniBodyConfig { input_device, .. }) => {
            input_device.clone().iqs5xx.unwrap_or(Vec::new())
        }
        BoardConfig::Split(split_config) => split_config
            .central
            .input_device
            .clone()
            .unwrap_or(InputDeviceConfig::default())
            .iqs5xx
            .unwrap_or(Vec::new()),
    };
    let iqs5xx_interrupt = expand_iqs5xx_interrupts(&chip.series, &iqs5xx_config);

    match chip.series {
        rmk_config::resolved::hardware::ChipSeries::Stm32 => {
            // For stm32, bind USB interrupt and EXTI interrupts (if async_matrix is enabled)
            let rmk_features = get_rmk_features();
            let async_matrix = is_feature_enabled(&rmk_features, "async_matrix");

            // Generate EXTI interrupt bindings for async_matrix
            let exti_interrupts = if async_matrix {
                generate_stm32_exti_interrupts(board)
            } else {
                quote! {}
            };

            if let Some(usb_info) = communication.get_usb_info() {
                let interrupt_name = format_ident!("{}", usb_info.interrupt_name);
                let peripheral_name = format_ident!("{}", usb_info.peripheral_name);
                quote! {
                    use ::embassy_stm32::bind_interrupts;
                    bind_interrupts!(struct Irqs {
                        #interrupt_name => ::embassy_stm32::usb::InterruptHandler<::embassy_stm32::peripherals::#peripheral_name>;
                        #exti_interrupts
                        #display_interrupt
                        #extern_irqs
                    });
                }
            } else if async_matrix {
                quote! {
                    use ::embassy_stm32::bind_interrupts;
                    bind_interrupts!(struct Irqs {
                        #exti_interrupts
                        #display_interrupt
                        #extern_irqs
                    });
                }
            } else if !display_interrupt.is_empty() {
                quote! {
                    use ::embassy_stm32::bind_interrupts;
                    bind_interrupts!(struct Irqs {
                        #display_interrupt
                        #extern_irqs
                    });
                }
            } else {
                quote! {
                    #extern_irqs
                }
            }
        }
        rmk_config::resolved::hardware::ChipSeries::Nrf52 => {
            // Usb and clock interrupt
            let usb_and_clock_interrupt = if let Some(usb_info) = communication.get_usb_info() {
                let interrupt_name = format_ident!("{}", usb_info.interrupt_name);
                let peripheral_name = format_ident!("{}", usb_info.peripheral_name);
                quote! {
                    #interrupt_name => ::embassy_nrf::usb::InterruptHandler<::embassy_nrf::peripherals::#peripheral_name>;
                    CLOCK_POWER => ::nrf_sdc::mpsl::ClockInterruptHandler, ::embassy_nrf::usb::vbus_detect::InterruptHandler;
                }
            } else {
                quote! { CLOCK_POWER => ::nrf_sdc::mpsl::ClockInterruptHandler; }
            };

            let ble_config = communication.get_ble_config().unwrap();
            let tx_power = if let Some(pwr) = ble_config.default_tx_power {
                quote! { .default_tx_power(#pwr)?  }
            } else {
                quote! {}
            };
            let use_2m_phy = if ble_config.use_2m_phy.unwrap_or(true) {
                quote! { .support_le_2m_phy() }
            } else {
                quote! {}
            };

            // nrf-sdc interrupt config
            let nrf_sdc_config = match &board {
                BoardConfig::Split(_) => {
                    let num_peri = board.get_num_peripheral() as u8;
                    let support_subrating = if is_feature_enabled(&get_rmk_features(), "subrating")
                    {
                        quote! { .support_connection_subrating_central() }
                    } else {
                        quote! {}
                    };
                    quote! {
                        ::nrf_sdc::Builder::new()?
                        .support_scan()
                        .support_central()
                        .support_adv()
                        .support_peripheral()
                        .support_dle_peripheral()
                        .support_dle_central()
                        .support_phy_update_central()
                        .support_phy_update_peripheral()
                        #support_subrating
                        #use_2m_phy
                        #tx_power
                        .central_count(#num_peri)?
                        .peripheral_count(1)?
                        .buffer_cfg(L2CAP_MTU as u16, L2CAP_MTU as u16, L2CAP_TXQ, L2CAP_RXQ)?
                        .build(p, rng, mpsl, mem)
                    }
                }
                BoardConfig::UniBody(_) => quote! {
                    ::nrf_sdc::Builder::new()?
                    .support_adv()
                    .support_peripheral()
                    .support_dle_peripheral()
                    .support_phy_update_peripheral()
                    #use_2m_phy
                    #tx_power
                    .peripheral_count(1)?
                    .buffer_cfg(L2CAP_MTU as u16, L2CAP_MTU as u16, L2CAP_TXQ, L2CAP_RXQ)?
                    .build(p, rng, mpsl, mem)
                },
            };

            // Extract PMW33xx configuration
            let pmw33xx_config = match &board {
                BoardConfig::UniBody(UniBodyConfig { input_device, .. }) => {
                    input_device.clone().pmw33xx.unwrap_or(Vec::new())
                }
                BoardConfig::Split(split_config) => split_config
                    .central
                    .input_device
                    .clone()
                    .unwrap_or(InputDeviceConfig::default())
                    .pmw33xx
                    .unwrap_or(Vec::new()),
            };

            // Generate SPI interrupts for each sensor
            let mut pmw33xx_spi_interrupts = Vec::new();

            for sensor in &pmw33xx_config {
                let instance_ident = format_ident!("{}", &sensor.spi.instance);

                pmw33xx_spi_interrupts.push(quote! {
                    #instance_ident => ::embassy_nrf::spim::InterruptHandler<::embassy_nrf::peripherals::#instance_ident>;
                });
            }

            let pmw33xx_spi_interrupts = if pmw33xx_spi_interrupts.is_empty() {
                quote! {}
            } else {
                quote! {
                    #(#pmw33xx_spi_interrupts)*
                }
            };

            quote! {
                use ::embassy_nrf::bind_interrupts;
                bind_interrupts!(struct Irqs {
                    #usb_and_clock_interrupt
                    RNG => ::embassy_nrf::rng::InterruptHandler<::embassy_nrf::peripherals::RNG>;
                    EGU0_SWI0 => ::nrf_sdc::mpsl::LowPrioInterruptHandler;
                    RADIO => ::nrf_sdc::mpsl::HighPrioInterruptHandler;
                    TIMER0 => ::nrf_sdc::mpsl::HighPrioInterruptHandler;
                    RTC0 => ::nrf_sdc::mpsl::HighPrioInterruptHandler;
                    #pmw33xx_spi_interrupts
                    #iqs5xx_interrupt
                    #display_interrupt
                    #extern_irqs
                });

                #[::embassy_executor::task]
                async fn mpsl_task(mpsl: &'static ::nrf_sdc::mpsl::MultiprotocolServiceLayer<'static>) -> ! {
                    mpsl.run().await
                }
                /// How many outgoing L2CAP buffers per link
                const L2CAP_TXQ: u8 = 3;

                /// How many incoming L2CAP buffers per link
                const L2CAP_RXQ: u8 = 3;

                /// Size of L2CAP packets
                const L2CAP_MTU: usize = 251;
                fn build_sdc<'d, const N: usize>(
                    p: ::nrf_sdc::Peripherals<'d>,
                    rng: &'d mut ::embassy_nrf::rng::Rng<::embassy_nrf::mode::Async>,
                    mpsl: &'d ::nrf_sdc::mpsl::MultiprotocolServiceLayer,
                    mem: &'d mut ::nrf_sdc::Mem<N>,
                ) -> Result<::nrf_sdc::SoftdeviceController<'d>, ::nrf_sdc::Error> {
                    #nrf_sdc_config
                }
            }
        }
        rmk_config::resolved::hardware::ChipSeries::Rp2040 => {
            let usb_info = communication
                .get_usb_info()
                .expect("no usb info for the chip");
            let interrupt_name = format_ident!("{}", usb_info.interrupt_name);
            let peripheral_name = format_ident!("{}", usb_info.peripheral_name);
            // Pico W's cyw43 SPI needs a third DMA channel (separate rx); bind it only for BLE boards.
            let dma_ch2 = if communication.ble_enabled() {
                quote! { , ::embassy_rp::dma::InterruptHandler<::embassy_rp::peripherals::DMA_CH2> }
            } else {
                quote! {}
            };
            let dma_irq_0 = quote! {
                DMA_IRQ_0 => ::embassy_rp::dma::InterruptHandler<::embassy_rp::peripherals::DMA_CH0>, ::embassy_rp::dma::InterruptHandler<::embassy_rp::peripherals::DMA_CH1> #dma_ch2;
            };
            // For Pico W, enabled PIO0_IRQ_0 interrupt
            let (pio0_irq_0, ble_task) = if communication.ble_enabled() {
                (
                    quote! {
                        PIO0_IRQ_0 => ::embassy_rp::pio::InterruptHandler<::embassy_rp::peripherals::PIO0>;
                    },
                    quote! {
                        #[::embassy_executor::task]
                        async fn cyw43_task(runner: ::cyw43::Runner<'static, ::cyw43::SpiBus<::embassy_rp::gpio::Output<'static>, ::cyw43_pio::PioSpi<'static, ::embassy_rp::peripherals::PIO0, 0>>, ::cyw43::Cyw43439>) -> ! {
                            runner.run().await
                        }
                    },
                )
            } else {
                (quote! {}, quote! {})
            };
            quote! {
                use ::embassy_rp::bind_interrupts;
                bind_interrupts!(struct Irqs {
                    #interrupt_name => ::embassy_rp::usb::InterruptHandler<::embassy_rp::peripherals::#peripheral_name>;
                    #dma_irq_0
                    #pio0_irq_0
                    #iqs5xx_interrupt
                    #display_interrupt
                });
                #ble_task
            }
        }
        rmk_config::resolved::hardware::ChipSeries::Esp32 => quote! {},
    }
}

/// Generate STM32 EXTI interrupt bindings based on row pins
/// STM32 EXTI lines:
/// - EXTI0 - EXTI4: each has its own interrupt
/// - EXTI5 - EXTI9: share EXTI9_5 interrupt
/// - EXTI10 - EXTI15: share EXTI15_10 interrupt
fn generate_stm32_exti_interrupts(board: &BoardConfig) -> TokenStream2 {
    // Collect all row pins from the matrix configuration
    let row_pins: Vec<String> = match board {
        BoardConfig::UniBody(unibody) => unibody.matrix.row_pins.clone().unwrap_or_default(),
        BoardConfig::Split(split) => split.central.matrix.row_pins.clone().unwrap_or_default(),
    };

    // Extract pin numbers and determine required EXTI interrupts
    let mut required_interrupts: HashSet<String> = HashSet::new();

    for pin in &row_pins {
        if let Some(pin_num_str) = get_pin_num_stm32(pin)
            && let Ok(pin_num) = pin_num_str.parse::<u8>()
        {
            let interrupt_name = match pin_num {
                0 => "EXTI0",
                1 => "EXTI1",
                2 => "EXTI2",
                3 => "EXTI3",
                4 => "EXTI4",
                5..=9 => "EXTI9_5",
                10..=15 => "EXTI15_10",
                _ => continue,
            };
            required_interrupts.insert(interrupt_name.to_string());
        }
    }

    // Generate interrupt bindings
    let interrupt_bindings: Vec<TokenStream2> = required_interrupts
        .iter()
        .map(|irq_name| {
            let irq_ident = format_ident!("{}", irq_name);
            quote! {
                #irq_ident => ::embassy_stm32::exti::InterruptHandler<::embassy_stm32::interrupt::typelevel::#irq_ident>;
            }
        })
        .collect();

    quote! {
        #(#interrupt_bindings)*
    }
}

/// Get pin number from pin str.
/// For example, if the pin str is "PD13", this function will return "13".
fn get_pin_num_stm32(gpio_name: &str) -> Option<String> {
    if gpio_name.len() < 3 {
        None
    } else {
        Some(gpio_name[2..].to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse_fn(src: &str) -> ItemFn {
        syn::parse_str(src).expect("test fn should parse")
    }

    // Selection regression tests (#967 review): validation accepting a marker
    // is meaningless if selection doesn't recognize the same marker.

    #[test]
    fn documented_override_form_is_selected() {
        // The form the stm32h7 example documents. It used to pass validation
        // but never get selected, silently dropping the custom binding.
        assert!(is_bind_interrupt_override(&parse_fn(
            "#[Override(bind_interrupt)]\nfn bind_interrupt() {}"
        )));
    }

    #[test]
    fn overwritten_spelling_is_selected() {
        assert!(is_bind_interrupt_override(&parse_fn(
            "#[Overwritten(bind_interrupt)]\nfn bind_interrupt() {}"
        )));
    }

    #[test]
    fn legacy_bare_marker_is_still_selected() {
        // The original syntax; kept working for existing user code.
        assert!(is_bind_interrupt_override(&parse_fn(
            "#[bind_interrupt]\nfn bind_interrupt() {}"
        )));
    }

    #[test]
    fn doc_comment_does_not_disable_documented_form() {
        assert!(is_bind_interrupt_override(&parse_fn(
            "/// custom irq binding\n#[Override(bind_interrupt)]\nfn bind_interrupt() {}"
        )));
    }

    #[test]
    fn cfg_gated_override_is_not_selected() {
        // Same `cfg` semantics as every other override: the macro cannot
        // evaluate `cfg`, so the function is left unselected.
        assert!(!is_bind_interrupt_override(&parse_fn(
            "#[cfg(feature = \"x\")]\n#[Override(bind_interrupt)]\nfn bind_interrupt() {}"
        )));
    }

    #[test]
    fn other_override_marker_is_not_selected() {
        assert!(!is_bind_interrupt_override(&parse_fn(
            "#[Override(entry)]\nfn run() {}"
        )));
    }
}