esp-metadata 0.10.1

Metadata for Espressif devices
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
pub(crate) mod aes;
pub(crate) mod ecc;
pub(crate) mod gpio;
pub(crate) mod i2c_master;
pub(crate) mod interrupt;
pub(crate) mod rmt;
pub(crate) mod rsa;
pub(crate) mod sha;
pub(crate) mod soc;
pub(crate) mod spi_master;
pub(crate) mod spi_slave;
pub(crate) mod timergroup;
pub(crate) mod uart;

pub(crate) use aes::*;
pub(crate) use ecc::*;
pub(crate) use gpio::*;
pub(crate) use i2c_master::*;
pub(crate) use interrupt::*;
pub(crate) use rmt::*;
pub(crate) use sha::*;
pub(crate) use soc::*;
pub(crate) use spi_master::*;
pub(crate) use spi_slave::*;
pub(crate) use timergroup::*;
pub(crate) use uart::*;

use crate::support_status::{SupportStatus, SupportStatusLevel};

pub(crate) trait GenericProperty {
    fn cfgs(&self) -> Option<Vec<String>> {
        None
    }

    fn macros(&self) -> Option<proc_macro2::TokenStream> {
        None
    }

    fn property_macro_branches(&self) -> proc_macro2::TokenStream {
        quote::quote! {}
    }
}

impl<T: GenericProperty> GenericProperty for Option<T> {
    fn cfgs(&self) -> Option<Vec<String>> {
        self.as_ref().and_then(|v| v.cfgs())
    }

    fn macros(&self) -> Option<proc_macro2::TokenStream> {
        self.as_ref().and_then(|v| v.macros())
    }

    fn property_macro_branches(&self) -> proc_macro2::TokenStream {
        self.as_ref()
            .map(|v| v.property_macro_branches())
            .unwrap_or_default()
    }
}

/// Represents a value in the driver configuration.
pub(crate) enum Value {
    Unset,
    /// A numeric value. The generated macro will not include a type suffix
    /// (i.e. will not be generated as `0u32`).
    Number(u32),
    /// A boolean value. If true, the value is included in the cfg symbols.
    Boolean(bool),
    /// A string.
    String(String),
    /// A list of numeric values. A for-each macro is generated for the list.
    NumberList(Vec<u32>),
    /// A list of strings. A separate symbol is generated for each string.
    StringList(Vec<String>),
    /// A generic object.
    Generic(Box<dyn GenericProperty>),
}

impl From<Option<u32>> for Value {
    fn from(value: Option<u32>) -> Self {
        match value {
            Some(v) => Value::Number(v),
            None => Value::Unset,
        }
    }
}

/// An empty configuration, used when a driver just wants to declare that
/// it supports a peripheral, but does not have any configuration options.
#[derive(Debug, Default, Clone, serde::Deserialize, serde::Serialize)]
pub(crate) struct EmptyInstanceConfig {}

/// A peripheral instance for which a driver is implemented.
#[derive(Debug, Default, Clone, serde::Deserialize, serde::Serialize)]
pub(crate) struct PeriInstance<I = EmptyInstanceConfig> {
    /// The name of the instance
    pub name: String,
    #[serde(flatten)]
    pub instance_config: I,
}

/// A single cell in the peripheral support table.
#[derive(Default)]
pub(crate) struct SupportItem {
    /// The human-readable name of the driver in the table (leftmost cell.)
    pub name: &'static str,
    /// The ID of the driver ([device.<config_group>]) in the TOML, that this
    /// item corresponds to.
    pub config_group: &'static str,
    /// If true, this driver is not shown in the peripheral support table.
    pub hide_from_peri_table: bool,
}

/// Define driver configuration structs, and a PeriConfig struct
/// that contains all of them.
macro_rules! driver_configs {
    (@ignore $t:tt) => {};
    (@property (u32)           $self:ident, $config:ident) => { Value::Number($self.$config) };
    (@property (bool)          $self:ident, $config:ident) => { Value::Boolean($self.$config) };
    (@property (String)        $self:ident, $config:ident) => { Value::String($self.$config.clone()) };
    (@property (Vec<u32>)      $self:ident, $config:ident) => { Value::NumberList($self.$config.clone()) };
    (@property (Vec<String>)   $self:ident, $config:ident) => { Value::StringList($self.$config.clone()) };
    (@property (Option<u32>)   $self:ident, $config:ident) => { Value::from($self.$config) };
    (@property ($($other:ty)*) $self:ident, $config:ident) => { Value::Generic(Box::new($self.$config.clone())) };
    (@is_optional Option<$t:ty>) => { true };
    (@is_optional $t:ty) => { false };

    (@default $default:literal) => { $default };
    (@default $default:literal $opt:literal) => { $opt };

    // Creates a single struct
    (@one
        $struct:ident $(<$instance_config:ident>)? ($group:ident) {
            $(
                $(#[$meta:meta])* $config:ident: $ty:tt $(<$generic:tt>)?,
            )*
        }
    ) => {
        #[derive(Debug, Clone, serde::Deserialize)]
        pub(crate) struct $struct {
            #[serde(default)]
            #[serde(deserialize_with = "crate::support_status::string_or_struct")]
            pub support_status: SupportStatus,

            // The list of peripherals for which this driver is implemented.
            // If empty, the driver supports a single instance only.
            #[serde(default)]
            pub instances: Vec<PeriInstance $(<$instance_config>)?>,
            $(
                $(#[$meta])*
                pub $config: $ty $(<$generic>)?
            ),*
        }

        impl $struct {
            fn properties(&self) -> impl Iterator<Item = (&str, bool, Value)> {
                [$( // for each property, generate a tuple
                    (
                        /* name: */  concat!(stringify!($group), ".", stringify!($config)),
                        /* is_optional: */ driver_configs!(@is_optional $ty $(<$generic>)?),
                        /* value: */ driver_configs!(@property ($ty $(<$generic>)?) self, $config),
                    ),
                )*].into_iter()
            }
        }
    };

    // Repeat pattern for multiple structs
    ($(
        $struct:ident $(<$instance_config:ident>)? {
            // This name will be emitted as a cfg symbol, to activate a driver.
            driver: $driver:ident,

            // Driver name, used in the generated documentation.
            name: $name:literal,

            $(hide_from_peri_table: $hide:literal,)?

            // When set, the type must provide `fn computed_properties(&self) -> impl Iterator<Item = (&str, bool, Value)>`.
            // The iterator yields `(name_with_prefix, optional, value)`.
            $(has_computed_properties: $computed:literal,)?

            properties: $tokens:tt
        },
    )+) => {
        // Implement the config driver and DriverConfig trait for each driver
        $(
            driver_configs!(@one $struct $(<$instance_config>)? ($driver) $tokens);
        )+

        // Generate a single PeriConfig struct that contains all the drivers. Each of the
        // drivers is optional to support devices that may not have all peripherals.
        #[derive(Default, Debug, Clone, serde::Deserialize)]
        pub(crate) struct PeriConfig {
            $(
                // Each driver is an optional struct.
                #[serde(default)]
                pub(crate) $driver: Option<$struct>,
            )+
        }

        impl PeriConfig {
            pub fn drivers() -> &'static [SupportItem] {
                &[
                    $(
                        SupportItem {
                            name: $name,
                            config_group: stringify!($driver),
                            hide_from_peri_table: driver_configs!(@default false $($hide)?),
                        },
                    )+
                ]
            }

            /// Returns an iterator over all driver names, that are
            /// available on the selected device.
            pub fn driver_names(&self) -> impl Iterator<Item = &str> {
                [$(
                    self.$driver.as_ref().and_then(|d| {
                        match d.support_status.status {
                            SupportStatusLevel::NotAvailable | SupportStatusLevel::NotSupported => None,
                            _ => Some(stringify!($driver)),
                        }
                    }),
                )*].into_iter().flatten()
            }

            pub fn driver_instances(&self) -> impl Iterator<Item = String> {
                // Collect into a vector. This compiles faster than chaining iterators.
                let mut instances = vec![];
                $(
                    if let Some(driver) = &self.$driver {
                        instances.extend(driver.instances.iter().map(|i| {
                            format!("{}.{}", stringify!($driver), i.name)
                        }));
                    }
                )*
                instances.into_iter()
            }

            /// Returns an iterator over all properties of all peripherals.
            ///
            /// (property name, optional?, value)
            pub fn properties(&self) -> impl Iterator<Item = (&str, bool, Value)> {
                // Collect into a vector. This compiles faster than chaining iterators.
                let mut properties = vec![];
                $(
                    if let Some(driver) = &self.$driver {
                        properties.extend(driver.properties());
                        $(
                            driver_configs!(@ignore $computed);
                            properties.extend(driver.computed_properties());
                        )?
                    }
                )*
                properties.into_iter()
            }

            /// Returns the support status of a peripheral by its name.
            pub fn support_status(&self, driver: &str) -> SupportStatus {
                let maybe_status = match driver {
                    $(stringify!($driver) => self.$driver.as_ref().map(|p| p.support_status),)*
                    _ => None,
                };
                maybe_status.unwrap_or(SupportStatus { status: SupportStatusLevel::NotAvailable, issue: None })
            }
        }
    };
}

// TODO: sort this similar to how the product portfolio is organized
driver_configs![
    AdcProperties {
        driver: adc,
        name: "ADC",
        properties: {}
    },
    AesProperties {
        driver: aes,
        name: "AES",
        properties: {
            key_length: AesKeyLength,
            #[serde(default)]
            dma: bool,
            #[serde(default)]
            dma_mode: Vec<String>,
            has_split_text_registers: bool,
            endianness_configurable: bool,
        }
    },
    AssistDebugProperties {
        driver: assist_debug,
        name: "ASSIST_DEBUG",
        properties: {
            #[serde(default)]
            has_sp_monitor: bool,
            #[serde(default)]
            has_region_monitor: bool,
        }
    },
    AvcProperties {
        driver: avc,
        name: "Analog Voltage Comparator",
        properties: {}
    },
    BitScramblerProperties {
        driver: bit_scrambler,
        name: "Bit Scrambler",
        properties: {}
    },
    BluetoothProperties {
        driver: bt,
        name: "Bluetooth",
        properties: {
            controller: String,
        }
    },
    CameraProperties {
        driver: camera,
        name: "Camera interface", // LCD_CAM, ESP32 I2S, S2 SPI
        properties: {}
    },
    DacProperties {
        driver: dac,
        name: "DAC",
        properties: {}
    },
    DedicatedGpioProperties {
        driver: dedicated_gpio,
        name: "Dedicated GPIO",
        properties: {
            #[serde(default)]
            needs_initialization: bool,
            #[serde(flatten)]
            channel_properties: DedicatedGpioChannels,
        }
    },
    DmaProperties {
        driver: dma,
        name: "DMA",
        properties: {
            kind: String,
            #[serde(default)]
            supports_mem2mem: bool,
            #[serde(default)]
            can_access_psram: bool,
            #[serde(default)]
            ext_mem_configurable_block_size: bool,
            #[serde(default)]
            separate_in_out_interrupts: bool,
            #[serde(default)]
            max_priority: Option<u32>,
            #[serde(default)]
            gdma_version: Option<u32>,
        }
    },
    DsProperties {
        driver: ds,
        name: "DS",
        properties: {}
    },
    EccProperties {
        driver: ecc,
        name: "ECC",
        properties: {
            #[serde(default)]
            zero_extend_writes: bool,
            #[serde(default)]
            separate_jacobian_point_memory: bool, // Qx, Qy, Qz memory blocks
            #[serde(default)]
            has_memory_clock_gate: bool, // ECC_MULT_MEM_CLOCK_GATE_FORCE_ON
            #[serde(default)]
            supports_enhanced_security: bool, // ECC_MULT_SECURITY_MODE
            #[serde(flatten)]
            extras: EccDriverProperties,
        }
    },
    EthernetProperties {
        driver: ethernet,
        name: "Ethernet",
        properties: {}
    },
    EtmProperties {
        driver: etm,
        name: "ETM",
        properties: {}
    },
    GpioProperties {
        driver: gpio,
        name: "GPIO",
        has_computed_properties: true,
        properties: {
            #[serde(default)]
            has_bank_1: bool,
            gpio_function: u32,
            constant_0_input: u32,
            constant_1_input: u32,
            #[serde(default)]
            remap_iomux_pin_registers: bool,
            #[serde(default)] // currently 0 in all devices
            func_in_sel_offset: u32,

            #[serde(flatten)]
            pins_and_signals: GpioPinsAndSignals,
        }
    },
    HmacProperties {
        driver: hmac,
        name: "HMAC",
        properties: {}
    },
    I2cMasterProperties<I2cMasterInstanceConfig> {
        driver: i2c_master,
        name: "I2C master",
        properties: {
            #[serde(default)]
            has_fsm_timeouts: bool,
            #[serde(default)]
            has_hw_bus_clear: bool,
            #[serde(default)]
            has_bus_timeout_enable: bool,
            #[serde(default)]
            separate_filter_config_registers: bool,
            #[serde(default)]
            can_estimate_nack_reason: bool,
            #[serde(default)]
            has_conf_update: bool,
            #[serde(default)]
            has_reliable_fsm_reset: bool,
            #[serde(default)]
            has_arbitration_en: bool,
            #[serde(default)]
            has_tx_fifo_watermark: bool,
            #[serde(default)]
            bus_timeout_is_exponential: bool,
            #[serde(default)]
            i2c0_data_register_ahb_address: Option<u32>,
            max_bus_timeout: u32,
            ll_intr_mask: u32,
            fifo_size: u32,
        }
    },
    I2cSlaveProperties {
        driver: i2c_slave,
        name: "I2C slave",
        properties: {}
    },
    I2sProperties {
        driver: i2s,
        name: "I2S",
        properties: {}
    },
    IeeeProperties {
        driver: ieee802154,
        name: "IEEE 802.15.4",
        properties: {}
    },
    InterruptProperties {
        driver: interrupts,
        name: "Interrupts",
        properties: {
            status_registers: u32,
            controller: InterruptControllerProperties,
            #[serde(flatten)]
            software_interrupt_properties: SoftwareInterruptProperties,
        }
    },
    IoMuxProperties {
        driver: io_mux,
        name: "IOMUX",
        properties: {}
    },
    KeyManagerProperties {
        driver: key_manager,
        name: "Key Manager",
        properties: {}
    },
    LedcProperties {
        driver: ledc,
        name: "LEDC",
        properties: {}
    },
    LpI2cMasterProperties {
        driver: lp_i2c_master,
        name: "LP I2C master",
        properties: {
            fifo_size: u32,
        }
    },
    LpUartProperties {
        driver: lp_uart,
        name: "LP UART",
        properties: {
            ram_size: u32,
        }
    },
    McpwmProperties {
        driver: mcpwm,
        name: "MCPWM",
        properties: {}
    },
    ParlIoProperties {
        driver: parl_io,
        name: "PARL_IO",
        properties: {
            version: u32,
            // TODO: model signal counts, RC CLK out capability, etc.
        }
    },
    PcntProperties {
        driver: pcnt,
        name: "PCNT",
        properties: {}
    },
    PhyProperties {
        driver: phy,
        name: "PHY",
        properties: {
            #[serde(default)]
            combo_module: bool,
            #[serde(default)]
            backed_up_digital_register_count: Option<u32>,
        }
    },
    PsramProperties {
        driver: psram,
        name: "PSRAM",
        properties: {
            #[serde(default)]
            octal_spi: bool,
            extmem_origin: u32,
        }
    },
    RgbProperties {
        driver: rgb_display,
        name: "RGB display", // LCD_CAM, ESP32 I2S, S2 SPI
        properties: {}
    },
    RmtProperties {
        driver: rmt,
        name: "RMT",
        properties: {
            ram_start: u32,
            channel_ram_size: u32,
            channels: RmtChannelConfig,
            #[serde(default)]
            has_tx_immediate_stop: bool,
            #[serde(default)]
            has_tx_loop_count: bool,
            #[serde(default)]
            has_tx_loop_auto_stop: bool,
            #[serde(default)]
            has_tx_carrier_data_only: bool,
            #[serde(default)]
            has_tx_sync: bool,
            #[serde(default)]
            has_rx_wrap: bool,
            #[serde(default)]
            has_rx_demodulation: bool,
            #[serde(default)]
            has_dma: bool,
            #[serde(default)]
            has_per_channel_clock: bool,
            clock_sources: RmtClockSourcesConfig,
        }
    },
    RngProperties {
        driver: rng,
        name: "RNG",
        properties: {
            apb_cycle_wait_num: u32,
            #[serde(default)]
            trng_supported: bool,
        }
    },
    RsaProperties {
        driver: rsa,
        name: "RSA",
        has_computed_properties: true,
        properties: {
            size_increment: u32,
            memory_size_bytes: u32,
        }
    },
    LpTimer {
        driver: lp_timer,
        name: "RTC Timekeeping",
        properties: {}
    },
    SdHostProperties {
        driver: sd_host,
        name: "SDIO host",
        properties: {}
    },
    SdSlaveProperties {
        driver: sd_slave,
        name: "SDIO slave",
        properties: {}
    },
    ShaProperties {
        driver: sha,
        name: "SHA",
        properties: {
            #[serde(default)]
            dma: bool,
            #[serde(default)]
            algo: ShaAlgoMap,
        }
    },
    SleepProperties {
        driver: sleep,
        name: "Light/deep sleep",
        properties: {
            #[serde(default)]
            light_sleep: bool,
            #[serde(default)]
            deep_sleep: bool,
        }
    },
    SocProperties {
        driver: soc,
        name: "SOC",
        hide_from_peri_table: true,
        properties: {
            #[serde(default)]
            cpu_has_branch_predictor: bool,
            #[serde(default)]
            cpu_has_csr_pc: bool,
            #[serde(default)]
            multi_core_enabled: bool,
            #[serde(default)]
            cpu_csr_prv_mode: Option<u32>,
            #[serde(default)]
            rc_fast_clk_default: Option<u32>,
            #[serde(flatten)]
            config: SocConfig,
        }
    },
    SpiMasterProperties<SpiMasterInstanceConfig> {
        driver: spi_master,
        name: "SPI master",
        properties: {
            #[serde(default)]
            supports_dma: bool,
            #[serde(default)]
            has_octal: bool,
            #[serde(default)]
            has_app_interrupts: bool,
            #[serde(default)]
            has_dma_segmented_transfer: bool,
            /// The PCR has a clock pre-divider before the SPI peripheral.
            #[serde(default)]
            has_clk_pre_div: bool,
        }
    },
    SpiSlaveProperties<SpiSlaveInstanceConfig> {
        driver: spi_slave,
        name: "SPI slave",
        properties: {
            #[serde(default)]
            supports_dma: bool,
        }
    },
    SysTimerProperties {
        driver: systimer,
        name: "SYSTIMER",
        properties: {}
    },
    TempProperties {
        driver: temp_sensor,
        name: "Temperature sensor",
        properties: {}
    },
    TimersProperties {
        driver: timergroup,
        name: "Timers",
        properties: {
            #[serde(default)]
            timg_has_timer1: bool,
            #[serde(default)]
            timg_has_divcnt_rst: bool,

            #[serde(default)]
            rc_fast_calibration: Option<RcFastCalibrationProperties>,
        }
    },
    TouchProperties {
        driver: touch,
        name: "Touch",
        properties: {}
    },
    TwaiProperties {
        driver: twai,
        name: "TWAI / CAN / CANFD",
        properties: {}
    },
    UartProperties<UartInstanceConfig> {
        driver: uart,
        name: "UART",
        properties: {
            ram_size: u32,
            #[serde(default)]
            peripheral_controls_mem_clk: bool,
            // Whether the MCU has a CLK_CONF register _in_ the UART peripheral.
            #[serde(default)]
            has_sclk_divider: bool,
        }
    },
    UhciProperties {
        driver: uhci,
        name: "UHCI",
        properties: {
            #[serde(default)]
            combined_uart_selector_field: bool,
        }
    },
    UlpFsmProperties {
        driver: ulp_fsm,
        name: "ULP (FSM)",
        properties: {}
    },
    UlpRiscvProperties {
        driver: ulp_riscv,
        name: "ULP (RISC-V)",
        properties: {}
    },
    UsbOtgProperties {
        driver: usb_otg,
        name: "USB OTG FS",
        properties: {}
    },
    UsbSerialJtagProperties {
        driver: usb_serial_jtag,
        name: "USB Serial/JTAG",
        properties: {}
    },
    WifiProperties {
        driver: wifi,
        name: "WIFI",
        properties: {
            #[serde(default)]
            has_wifi6: bool,
            mac_version: u32,
            #[serde(default)]
            has_5g: bool,
            #[serde(default)]
            csi_supported: bool,
        }
    },
];