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
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use crate::{cfg::GenericProperty, generate_for_each_macro, number};
/// The capabilities of an RMT channel, used in [device.rmt.channels]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Deserialize, serde::Serialize)]
pub(crate) enum RmtChannelCapability {
Rx,
Tx,
RxTx,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub(crate) struct RmtChannelConfig(
/// The capability of each channel
Vec<RmtChannelCapability>,
);
/// Generates `for_each_rmt_channel!` which can be used to implement channel creators and the main
/// driver struct for the RMT peripheral.
///
/// The macro generates code for each [device.rmt.channels[X]] entry.
impl GenericProperty for RmtChannelConfig {
fn macros(&self) -> Option<TokenStream> {
let channel_cfgs = self
.0
.iter()
.enumerate()
.map(|(num, _)| {
let num = number(num);
quote! {
#num
}
})
.collect::<Vec<_>>();
let make_channel_cfgs = |filter: fn(RmtChannelCapability) -> bool| {
self.0
.iter()
.enumerate()
.filter(|&(_, &cap)| filter(cap))
.enumerate()
.map(|(idx, (num, _))| {
let num = number(num);
let idx = number(idx);
quote! {
#num, #idx
}
})
.collect::<Vec<_>>()
};
let tx_channel_cfgs = make_channel_cfgs(|cap| {
matches!(cap, RmtChannelCapability::Tx | RmtChannelCapability::RxTx)
});
let rx_channel_cfgs = make_channel_cfgs(|cap| {
matches!(cap, RmtChannelCapability::Rx | RmtChannelCapability::RxTx)
});
let for_each = generate_for_each_macro(
"rmt_channel",
&[
("all", &channel_cfgs),
("tx", &tx_channel_cfgs),
("rx", &rx_channel_cfgs),
],
);
Some(quote! {
/// This macro can be used to generate code for each channel of the RMT peripheral.
///
/// For an explanation on the general syntax, as well as usage of individual/repeated
/// matchers, refer to [the crate-level documentation][crate#for_each-macros].
///
/// This macro has three options for its "Individual matcher" case:
///
/// - `all`: `($num:literal)`
/// - `tx`: `($num:literal, $idx:literal)`
/// - `rx`: `($num:literal, $idx:literal)`
///
/// Macro fragments:
///
/// - `$num`: number of the channel, e.g. `0`
/// - `$idx`: index of the channel among channels of the same capability, e.g. `0`
///
/// Example data:
///
/// - `all`: `(0)`
/// - `tx`: `(1, 1)`
/// - `rx`: `(2, 0)`
#for_each
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub(crate) struct RmtClockSourcesConfig {
supported: Vec<String>,
default: String,
}
impl GenericProperty for RmtClockSourcesConfig {
fn cfgs(&self) -> Option<Vec<String>> {
let mut cfgs = Vec::new();
for value in &self.supported {
cfgs.push(format!("rmt_supports_{}_clock", value.to_lowercase()));
}
Some(cfgs)
}
fn macros(&self) -> Option<TokenStream> {
let clock_sources = self
.supported
.iter()
.enumerate()
.filter(|(_, name)| *name != "None")
.map(|(bits, name)| {
let src_name = format_ident!("{}", name);
let bits = number(bits);
quote! {
#src_name, #bits
}
})
.collect::<Vec<_>>();
let default = format_ident!("{}", self.default);
let default_clock_source = [quote!( #default )];
let branches: &[(&str, &[TokenStream])] = if self.supported.len() <= 2 {
&[
("all", &clock_sources),
("default", &default_clock_source),
("is_boolean", &[]),
]
} else {
&[("all", &clock_sources), ("default", &default_clock_source)]
};
Some(generate_for_each_macro("rmt_clock_source", branches))
}
}