-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathlib.rs
252 lines (227 loc) · 7.2 KB
/
lib.rs
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
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(stable_features)]
#![allow(unknown_lints)]
#![allow(clippy::unused_unit)] // enumset
#![warn(clippy::large_futures)]
#![cfg_attr(feature = "nightly", feature(async_fn_in_trait))]
#![cfg_attr(feature = "nightly", allow(async_fn_in_trait))]
#![cfg_attr(feature = "nightly", feature(impl_trait_projections))]
#![cfg_attr(feature = "nightly", feature(doc_cfg))]
#![cfg_attr(target_arch = "xtensa", feature(asm_experimental_arch))]
#[cfg(all(not(feature = "riscv-ulp-hal"), not(feature = "esp-idf-sys")))]
compile_error!("Exactly one of the features `esp-idf-sys` or `riscv-ulp-hal` needs to be enabled");
#[cfg(all(not(feature = "riscv-ulp-hal"), not(esp_idf_comp_driver_enabled)))]
compile_error!("esp-idf-hal requires the `driver` ESP-IDF component to be enabled");
#[cfg(all(
any(
feature = "std",
feature = "alloc",
feature = "critical-section-interrupt",
feature = "critical-section-mutex"
),
feature = "riscv-ulp-hal"
))]
compile_error!("Enabling feature `riscv-ulp-hal` implies no other feature is enabled");
#[cfg(all(feature = "riscv-ulp-hal", not(esp32s2)))]
compile_error!("Feature `riscv-ulp-hal` is currently only supported on esp32s2");
#[macro_use]
pub mod riscv_ulp_hal;
pub mod adc;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod can;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod cpu;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod delay;
pub mod gpio;
#[cfg(all(esp32, esp_idf_version_major = "4"))]
pub mod hall;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod i2c;
#[cfg(all(not(feature = "riscv-ulp-hal"), esp_idf_comp_driver_enabled))]
#[cfg_attr(
feature = "nightly",
doc(cfg(all(esp_idf_soc_i2s_supported, esp_idf_comp_driver_enabled)))
)]
pub mod i2s;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod interrupt;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod io;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod ledc;
#[cfg(all(
any(all(esp32, esp_idf_eth_use_esp32_emac), esp_idf_eth_use_openeth),
not(feature = "riscv-ulp-hal")
))]
pub mod mac;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod modem;
#[cfg(all(not(feature = "riscv-ulp-hal"), any(esp32, esp32s2, esp32s3)))]
pub mod pcnt;
pub mod peripheral;
pub mod peripherals;
pub mod prelude;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod reset;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod rmt;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod rom;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod spi;
pub mod sys;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod task;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod timer;
#[cfg(all(any(esp32, esp32s2, esp32s3), not(feature = "riscv-ulp-hal")))]
pub mod touch;
#[cfg(not(feature = "riscv-ulp-hal"))]
pub mod uart;
#[cfg(all(
any(esp32, esp32s2, esp32s3, esp32c6, esp32p4),
not(feature = "riscv-ulp-hal")
))]
pub mod ulp;
pub mod units;
#[cfg(feature = "riscv-ulp-hal")]
pub use crate::riscv_ulp_hal::delay;
// This is used to create `embedded_hal` compatible error structs
// that preserve original `EspError`.
//
// Example:
// embedded_hal_error!(I2cError, embedded_hal::i2c::Error, embedded_hal::i2c::ErrorKind)
#[cfg(not(feature = "riscv-ulp-hal"))]
#[allow(unused_macros)]
macro_rules! embedded_hal_error {
($error:ident, $errortrait:ty, $kind:ty) => {
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct $error {
kind: $kind,
cause: esp_idf_sys::EspError,
}
impl $error {
pub fn new(kind: $kind, cause: esp_idf_sys::EspError) -> Self {
Self { kind, cause }
}
pub fn other(cause: esp_idf_sys::EspError) -> Self {
Self::new(<$kind>::Other, cause)
}
pub fn cause(&self) -> esp_idf_sys::EspError {
self.cause
}
}
impl From<esp_idf_sys::EspError> for $error {
fn from(e: esp_idf_sys::EspError) -> Self {
Self::other(e)
}
}
impl $errortrait for $error {
fn kind(&self) -> $kind {
self.kind
}
}
impl core::fmt::Display for $error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"{} {{ kind: {}, cause: {} }}",
stringify!($error),
self.kind,
self.cause()
)
}
}
#[cfg(feature = "std")]
impl std::error::Error for $error {}
};
}
#[cfg(feature = "riscv-ulp-hal")]
#[allow(unused_macros)]
macro_rules! embedded_hal_error {
($error:ident, $errortrait:ty, $kind:ty) => {
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct $error {
kind: $kind,
cause: crate::riscv_ulp_hal::sys::EspError,
}
impl $error {
pub fn new(kind: $kind, cause: crate::riscv_ulp_hal::sys::EspError) -> Self {
Self { kind, cause }
}
pub fn other(cause: crate::riscv_ulp_hal::sys::EspError) -> Self {
Self::new(<$kind>::Other, cause)
}
pub fn cause(&self) -> crate::riscv_ulp_hal::sys::EspError {
self.cause
}
}
impl From<crate::riscv_ulp_hal::sys::EspError> for $error {
fn from(e: crate::riscv_ulp_hal::sys::EspError) -> Self {
Self::other(e)
}
}
impl $errortrait for $error {
fn kind(&self) -> $kind {
self.kind
}
}
impl core::fmt::Display for $error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"{} {{ kind: {}, cause: {} }}",
stringify!($error),
self.kind,
self.cause()
)
}
}
};
}
#[macro_export]
#[allow(unused_macros)]
macro_rules! into_ref {
($($name:ident),*) => {
$(
let $name = $name.into_ref();
)*
}
}
#[allow(unused_macros)]
macro_rules! impl_peripheral_trait {
($type:ident) => {
unsafe impl Send for $type {}
impl $crate::peripheral::sealed::Sealed for $type {}
impl $crate::peripheral::Peripheral for $type {
type P = $type;
#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
$type { ..*self }
}
}
};
}
#[allow(unused_macros)]
macro_rules! impl_peripheral {
($type:ident) => {
pub struct $type(::core::marker::PhantomData<*const ()>);
impl $type {
/// # Safety
///
/// Care should be taken not to instantiate this peripheral instance, if it is already instantiated and used elsewhere
#[inline(always)]
pub unsafe fn new() -> Self {
$type(::core::marker::PhantomData)
}
}
$crate::impl_peripheral_trait!($type);
};
}
#[allow(unused_imports)]
pub(crate) use embedded_hal_error;
#[allow(unused_imports)]
pub(crate) use impl_peripheral;
#[allow(unused_imports)]
pub(crate) use impl_peripheral_trait;