Skip to content

Commit bc4e180

Browse files
authored
Merge pull request #280 from bjoernQ/gpio-refactoring
GPIO module refactoring
2 parents 5f4a7b5 + 878bbc4 commit bc4e180

File tree

33 files changed

+1866
-1400
lines changed

33 files changed

+1866
-1400
lines changed

esp-hal-common/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ embedded-hal-nb = { version = "=1.0.0-alpha.1", optional = true }
2020
fugit = "0.3.6"
2121
lock_api = { version = "0.4.9", optional = true }
2222
nb = "1.0.0"
23-
paste = "=1.0.8"
23+
paste = "1.0.9"
2424
procmacros = { version = "0.1.0", package = "esp-hal-procmacros", path = "../esp-hal-procmacros" }
2525
void = { version = "1.0.2", default-features = false }
2626
embedded-dma = "0.2.0"
@@ -34,7 +34,7 @@ embassy-time = { version = "0.1.0", features = ["nightly"], optional = tru
3434

3535
# RISC-V
3636
riscv = { version = "0.10.0", optional = true }
37-
riscv-atomic-emulation-trap = { version = "0.2.0", optional = true }
37+
riscv-atomic-emulation-trap = { version = "0.3.0", optional = true }
3838

3939
# Xtensa
4040
xtensa-lx = { version = "0.7.0", optional = true }

esp-hal-common/src/analog/adc/esp32.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,59 @@ macro_rules! impl_adc_interface {
414414
}
415415

416416
pub use impl_adc_interface;
417+
418+
pub mod implementation {
419+
//! Analog to digital (ADC) conversion support.
420+
//!
421+
//! This module provides functions for reading analog values from two
422+
//! analog to digital converters available on the ESP32: `ADC1` and `ADC2`.
423+
//!
424+
//! The following pins can be configured for analog readout:
425+
//!
426+
//! | Channel | ADC1 | ADC2 |
427+
//! |---------|----------------------|---------------|
428+
//! | 0 | GPIO36 (SENSOR_VP) | GPIO4 |
429+
//! | 1 | GPIO37 (SENSOR_CAPP) | GPIO0 |
430+
//! | 2 | GPIO38 (SENSOR_CAPN) | GPIO2 |
431+
//! | 3 | GPIO39 (SENSOR_VN) | GPIO15 (MTDO) |
432+
//! | 4 | GPIO33 (32K_XP) | GPIO13 (MTCK) |
433+
//! | 5 | GPIO32 (32K_XN) | GPIO12 (MTDI) |
434+
//! | 6 | GPIO34 (VDET_1) | GPIO14 (MTMS) |
435+
//! | 7 | GPIO35 (VDET_2) | GPIO27 |
436+
//! | 8 | | GPIO25 |
437+
//! | 9 | | GPIO26 |
438+
439+
use embedded_hal::adc::Channel;
440+
441+
use super::impl_adc_interface;
442+
pub use crate::analog::{adc::*, ADC1, ADC2};
443+
use crate::gpio::*;
444+
445+
impl_adc_interface! {
446+
ADC1 [
447+
(Gpio36, 0), // Alt. name: SENSOR_VP
448+
(Gpio37, 1), // Alt. name: SENSOR_CAPP
449+
(Gpio38, 2), // Alt. name: SENSOR_CAPN
450+
(Gpio39, 3), // Alt. name: SENSOR_VN
451+
(Gpio33, 4), // Alt. name: 32K_XP
452+
(Gpio32, 5), // Alt. name: 32K_XN
453+
(Gpio34, 6), // Alt. name: VDET_1
454+
(Gpio35, 7), // Alt. name: VDET_2
455+
]
456+
}
457+
458+
impl_adc_interface! {
459+
ADC2 [
460+
(Gpio4, 0),
461+
(Gpio0, 1),
462+
(Gpio2, 2),
463+
(Gpio15, 3), // Alt. name: MTDO
464+
(Gpio13, 4), // Alt. name: MTCK
465+
(Gpio12, 5), // Alt. name: MTDI
466+
(Gpio14, 6), // Alt. name: MTMS
467+
(Gpio27, 7),
468+
(Gpio25, 8),
469+
(Gpio26, 9),
470+
]
471+
}
472+
}

esp-hal-common/src/analog/adc/riscv.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,58 @@ macro_rules! impl_adc_interface {
285285
}
286286

287287
pub use impl_adc_interface;
288+
289+
#[cfg(esp32c3)]
290+
pub mod implementation {
291+
//! Analog to digital (ADC) conversion support.
292+
//!
293+
//! This module provides functions for reading analog values from two
294+
//! analog to digital converters available on the ESP32-C3: `ADC1` and
295+
//! `ADC2`.
296+
297+
use embedded_hal::adc::Channel;
298+
299+
use super::impl_adc_interface;
300+
pub use crate::analog::{adc::*, ADC1, ADC2};
301+
use crate::gpio::*;
302+
303+
impl_adc_interface! {
304+
ADC1 [
305+
(Gpio0, 0),
306+
(Gpio1, 1),
307+
(Gpio2, 2),
308+
(Gpio3, 3),
309+
(Gpio4, 4),
310+
]
311+
}
312+
313+
impl_adc_interface! {
314+
ADC2 [
315+
(Gpio5, 4),
316+
]
317+
}
318+
}
319+
320+
#[cfg(esp32c2)]
321+
pub mod implementation {
322+
//! Analog to digital (ADC) conversion support.
323+
//!
324+
//! This module provides functions for reading analog values from the
325+
//! analog to digital converter available on the ESP32-C2: `ADC1`.
326+
327+
use embedded_hal::adc::Channel;
328+
329+
use super::impl_adc_interface;
330+
pub use crate::analog::{adc::*, ADC1};
331+
use crate::gpio::*;
332+
333+
impl_adc_interface! {
334+
ADC1 [
335+
(Gpio0, 0),
336+
(Gpio1, 1),
337+
(Gpio2, 2),
338+
(Gpio3, 3),
339+
(Gpio4, 4),
340+
]
341+
}
342+
}

esp-hal-common/src/analog/adc/xtensa.rs

Lines changed: 92 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,7 @@ impl RegisterAccess for ADC1 {
161161

162162
fn read_done_sar() -> bool {
163163
let sensors = unsafe { &*SENS::ptr() };
164-
sensors.
165-
sar_meas1_ctrl2.
166-
read().
167-
meas1_done_sar().
168-
bit_is_set()
164+
sensors.sar_meas1_ctrl2.read().meas1_done_sar().bit_is_set()
169165
}
170166

171167
fn read_data_sar() -> u16 {
@@ -242,11 +238,7 @@ impl RegisterAccess for ADC2 {
242238

243239
fn read_done_sar() -> bool {
244240
let sensors = unsafe { &*SENS::ptr() };
245-
sensors.
246-
sar_meas2_ctrl2.
247-
read().
248-
meas2_done_sar().
249-
bit_is_set()
241+
sensors.sar_meas2_ctrl2.read().meas2_done_sar().bit_is_set()
250242
}
251243

252244
fn read_data_sar() -> u16 {
@@ -413,3 +405,93 @@ macro_rules! impl_adc_interface {
413405
}
414406

415407
pub use impl_adc_interface;
408+
409+
#[cfg(esp32s3)]
410+
pub mod implementation {
411+
//! Analog to digital (ADC) conversion support.
412+
//!
413+
//! This module provides functions for reading analog values from two
414+
//! analog to digital converters available on the ESP32-S3: `ADC1` and
415+
//! `ADC2`.
416+
417+
use embedded_hal::adc::Channel;
418+
419+
use super::impl_adc_interface;
420+
pub use crate::analog::{adc::*, ADC1, ADC2};
421+
use crate::gpio::*;
422+
423+
impl_adc_interface! {
424+
ADC1 [
425+
(Gpio1, 0),
426+
(Gpio2, 1),
427+
(Gpio3, 2),
428+
(Gpio4, 3),
429+
(Gpio5, 4),
430+
(Gpio6, 5),
431+
(Gpio7, 6),
432+
(Gpio8, 7),
433+
(Gpio9, 8),
434+
(Gpio10,9),
435+
]
436+
}
437+
438+
impl_adc_interface! {
439+
ADC2 [
440+
(Gpio11, 0),
441+
(Gpio12, 1),
442+
(Gpio13, 2),
443+
(Gpio14, 3),
444+
(Gpio15, 4),
445+
(Gpio16, 5),
446+
(Gpio17, 6),
447+
(Gpio18, 7),
448+
(Gpio19, 8),
449+
(Gpio20, 9),
450+
]
451+
}
452+
}
453+
454+
#[cfg(esp32s2)]
455+
pub mod implementation {
456+
//! Analog to digital (ADC) conversion support.
457+
//!
458+
//! This module provides functions for reading analog values from two
459+
//! analog to digital converters available on the ESP32-S2: `ADC1` and
460+
//! `ADC2`.
461+
462+
use embedded_hal::adc::Channel;
463+
464+
use super::impl_adc_interface;
465+
pub use crate::analog::{adc::*, ADC1, ADC2};
466+
use crate::gpio::*;
467+
468+
impl_adc_interface! {
469+
ADC1 [
470+
(Gpio1, 0),
471+
(Gpio2, 1),
472+
(Gpio3, 2),
473+
(Gpio4, 3),
474+
(Gpio5, 4),
475+
(Gpio6, 5),
476+
(Gpio7, 6),
477+
(Gpio8, 7),
478+
(Gpio9, 8),
479+
(Gpio10,9),
480+
]
481+
}
482+
483+
impl_adc_interface! {
484+
ADC2 [
485+
(Gpio11, 0),
486+
(Gpio12, 1),
487+
(Gpio13, 2),
488+
(Gpio14, 3),
489+
(Gpio15, 4),
490+
(Gpio16, 5),
491+
(Gpio17, 6),
492+
(Gpio18, 7),
493+
(Gpio19, 8),
494+
(Gpio20, 9),
495+
]
496+
}
497+
}

esp-hal-common/src/analog/dac.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ macro_rules! impl_dac {
8888
use crate::gpio;
8989

9090
$(
91-
paste! {
92-
pub use esp_hal_common::analog::dac::[<DAC $number Impl>];
91+
paste::paste! {
92+
pub use $crate::analog::dac::[<DAC $number Impl>];
9393

9494
/// DAC channel
9595
pub struct [<DAC $number>] {
@@ -101,8 +101,8 @@ macro_rules! impl_dac {
101101
impl [<DAC $number>] {
102102
/// Constructs a new DAC instance
103103
pub fn dac(
104-
_dac: esp_hal_common::analog::[<DAC $number>],
105-
_pin: gpio::$gpio<esp_hal_common::Analog>,
104+
_dac: $crate::analog::[<DAC $number>],
105+
_pin: gpio::$gpio<$crate::Analog>,
106106
) -> Result<Self, ()> {
107107
let dac = Self {
108108
_private: PhantomData,
@@ -125,3 +125,33 @@ macro_rules! impl_dac {
125125
}
126126

127127
pub use impl_dac;
128+
129+
#[cfg(esp32)]
130+
pub mod implementation {
131+
//! Digital to analog (DAC) conversion.
132+
//!
133+
//! This module provides functions for controling two digital to
134+
//! analog converters, available on ESP32: `DAC1` and `DAC2`.
135+
//!
136+
//! The DAC1 is available on the GPIO pin 25, and DAC2 on pin 26.
137+
138+
pub use super::*;
139+
use crate::impl_dac;
140+
141+
impl_dac!(1 => Gpio25, 2 => Gpio26,);
142+
}
143+
144+
#[cfg(esp32s2)]
145+
pub mod implementation {
146+
//! Digital to analog (DAC) conversion.
147+
//!
148+
//! This module provides functions for controling two digital to
149+
//! analog converters, available on ESP32: `DAC1` and `DAC2`.
150+
//!
151+
//! The DAC1 is available on the GPIO pin 17, and DAC2 on pin 18.
152+
153+
pub use super::*;
154+
use crate::impl_dac;
155+
156+
impl_dac!(1 => Gpio17, 2 => Gpio18,);
157+
}

0 commit comments

Comments
 (0)