Skip to content

Commit

Permalink
Merge pull request #280 from bjoernQ/gpio-refactoring
Browse files Browse the repository at this point in the history
GPIO module refactoring
  • Loading branch information
bjoernQ authored Nov 30, 2022
2 parents 5f4a7b5 + 878bbc4 commit bc4e180
Show file tree
Hide file tree
Showing 33 changed files with 1,866 additions and 1,400 deletions.
4 changes: 2 additions & 2 deletions esp-hal-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ embedded-hal-nb = { version = "=1.0.0-alpha.1", optional = true }
fugit = "0.3.6"
lock_api = { version = "0.4.9", optional = true }
nb = "1.0.0"
paste = "=1.0.8"
paste = "1.0.9"
procmacros = { version = "0.1.0", package = "esp-hal-procmacros", path = "../esp-hal-procmacros" }
void = { version = "1.0.2", default-features = false }
embedded-dma = "0.2.0"
Expand All @@ -34,7 +34,7 @@ embassy-time = { version = "0.1.0", features = ["nightly"], optional = tru

# RISC-V
riscv = { version = "0.10.0", optional = true }
riscv-atomic-emulation-trap = { version = "0.2.0", optional = true }
riscv-atomic-emulation-trap = { version = "0.3.0", optional = true }

# Xtensa
xtensa-lx = { version = "0.7.0", optional = true }
Expand Down
56 changes: 56 additions & 0 deletions esp-hal-common/src/analog/adc/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,59 @@ macro_rules! impl_adc_interface {
}

pub use impl_adc_interface;

pub mod implementation {
//! Analog to digital (ADC) conversion support.
//!
//! This module provides functions for reading analog values from two
//! analog to digital converters available on the ESP32: `ADC1` and `ADC2`.
//!
//! The following pins can be configured for analog readout:
//!
//! | Channel | ADC1 | ADC2 |
//! |---------|----------------------|---------------|
//! | 0 | GPIO36 (SENSOR_VP) | GPIO4 |
//! | 1 | GPIO37 (SENSOR_CAPP) | GPIO0 |
//! | 2 | GPIO38 (SENSOR_CAPN) | GPIO2 |
//! | 3 | GPIO39 (SENSOR_VN) | GPIO15 (MTDO) |
//! | 4 | GPIO33 (32K_XP) | GPIO13 (MTCK) |
//! | 5 | GPIO32 (32K_XN) | GPIO12 (MTDI) |
//! | 6 | GPIO34 (VDET_1) | GPIO14 (MTMS) |
//! | 7 | GPIO35 (VDET_2) | GPIO27 |
//! | 8 | | GPIO25 |
//! | 9 | | GPIO26 |

use embedded_hal::adc::Channel;

use super::impl_adc_interface;
pub use crate::analog::{adc::*, ADC1, ADC2};
use crate::gpio::*;

impl_adc_interface! {
ADC1 [
(Gpio36, 0), // Alt. name: SENSOR_VP
(Gpio37, 1), // Alt. name: SENSOR_CAPP
(Gpio38, 2), // Alt. name: SENSOR_CAPN
(Gpio39, 3), // Alt. name: SENSOR_VN
(Gpio33, 4), // Alt. name: 32K_XP
(Gpio32, 5), // Alt. name: 32K_XN
(Gpio34, 6), // Alt. name: VDET_1
(Gpio35, 7), // Alt. name: VDET_2
]
}

impl_adc_interface! {
ADC2 [
(Gpio4, 0),
(Gpio0, 1),
(Gpio2, 2),
(Gpio15, 3), // Alt. name: MTDO
(Gpio13, 4), // Alt. name: MTCK
(Gpio12, 5), // Alt. name: MTDI
(Gpio14, 6), // Alt. name: MTMS
(Gpio27, 7),
(Gpio25, 8),
(Gpio26, 9),
]
}
}
55 changes: 55 additions & 0 deletions esp-hal-common/src/analog/adc/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,58 @@ macro_rules! impl_adc_interface {
}

pub use impl_adc_interface;

#[cfg(esp32c3)]
pub mod implementation {
//! Analog to digital (ADC) conversion support.
//!
//! This module provides functions for reading analog values from two
//! analog to digital converters available on the ESP32-C3: `ADC1` and
//! `ADC2`.

use embedded_hal::adc::Channel;

use super::impl_adc_interface;
pub use crate::analog::{adc::*, ADC1, ADC2};
use crate::gpio::*;

impl_adc_interface! {
ADC1 [
(Gpio0, 0),
(Gpio1, 1),
(Gpio2, 2),
(Gpio3, 3),
(Gpio4, 4),
]
}

impl_adc_interface! {
ADC2 [
(Gpio5, 4),
]
}
}

#[cfg(esp32c2)]
pub mod implementation {
//! Analog to digital (ADC) conversion support.
//!
//! This module provides functions for reading analog values from the
//! analog to digital converter available on the ESP32-C2: `ADC1`.

use embedded_hal::adc::Channel;

use super::impl_adc_interface;
pub use crate::analog::{adc::*, ADC1};
use crate::gpio::*;

impl_adc_interface! {
ADC1 [
(Gpio0, 0),
(Gpio1, 1),
(Gpio2, 2),
(Gpio3, 3),
(Gpio4, 4),
]
}
}
102 changes: 92 additions & 10 deletions esp-hal-common/src/analog/adc/xtensa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,7 @@ impl RegisterAccess for ADC1 {

fn read_done_sar() -> bool {
let sensors = unsafe { &*SENS::ptr() };
sensors.
sar_meas1_ctrl2.
read().
meas1_done_sar().
bit_is_set()
sensors.sar_meas1_ctrl2.read().meas1_done_sar().bit_is_set()
}

fn read_data_sar() -> u16 {
Expand Down Expand Up @@ -242,11 +238,7 @@ impl RegisterAccess for ADC2 {

fn read_done_sar() -> bool {
let sensors = unsafe { &*SENS::ptr() };
sensors.
sar_meas2_ctrl2.
read().
meas2_done_sar().
bit_is_set()
sensors.sar_meas2_ctrl2.read().meas2_done_sar().bit_is_set()
}

fn read_data_sar() -> u16 {
Expand Down Expand Up @@ -413,3 +405,93 @@ macro_rules! impl_adc_interface {
}

pub use impl_adc_interface;

#[cfg(esp32s3)]
pub mod implementation {
//! Analog to digital (ADC) conversion support.
//!
//! This module provides functions for reading analog values from two
//! analog to digital converters available on the ESP32-S3: `ADC1` and
//! `ADC2`.

use embedded_hal::adc::Channel;

use super::impl_adc_interface;
pub use crate::analog::{adc::*, ADC1, ADC2};
use crate::gpio::*;

impl_adc_interface! {
ADC1 [
(Gpio1, 0),
(Gpio2, 1),
(Gpio3, 2),
(Gpio4, 3),
(Gpio5, 4),
(Gpio6, 5),
(Gpio7, 6),
(Gpio8, 7),
(Gpio9, 8),
(Gpio10,9),
]
}

impl_adc_interface! {
ADC2 [
(Gpio11, 0),
(Gpio12, 1),
(Gpio13, 2),
(Gpio14, 3),
(Gpio15, 4),
(Gpio16, 5),
(Gpio17, 6),
(Gpio18, 7),
(Gpio19, 8),
(Gpio20, 9),
]
}
}

#[cfg(esp32s2)]
pub mod implementation {
//! Analog to digital (ADC) conversion support.
//!
//! This module provides functions for reading analog values from two
//! analog to digital converters available on the ESP32-S2: `ADC1` and
//! `ADC2`.

use embedded_hal::adc::Channel;

use super::impl_adc_interface;
pub use crate::analog::{adc::*, ADC1, ADC2};
use crate::gpio::*;

impl_adc_interface! {
ADC1 [
(Gpio1, 0),
(Gpio2, 1),
(Gpio3, 2),
(Gpio4, 3),
(Gpio5, 4),
(Gpio6, 5),
(Gpio7, 6),
(Gpio8, 7),
(Gpio9, 8),
(Gpio10,9),
]
}

impl_adc_interface! {
ADC2 [
(Gpio11, 0),
(Gpio12, 1),
(Gpio13, 2),
(Gpio14, 3),
(Gpio15, 4),
(Gpio16, 5),
(Gpio17, 6),
(Gpio18, 7),
(Gpio19, 8),
(Gpio20, 9),
]
}
}
38 changes: 34 additions & 4 deletions esp-hal-common/src/analog/dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ macro_rules! impl_dac {
use crate::gpio;

$(
paste! {
pub use esp_hal_common::analog::dac::[<DAC $number Impl>];
paste::paste! {
pub use $crate::analog::dac::[<DAC $number Impl>];

/// DAC channel
pub struct [<DAC $number>] {
Expand All @@ -101,8 +101,8 @@ macro_rules! impl_dac {
impl [<DAC $number>] {
/// Constructs a new DAC instance
pub fn dac(
_dac: esp_hal_common::analog::[<DAC $number>],
_pin: gpio::$gpio<esp_hal_common::Analog>,
_dac: $crate::analog::[<DAC $number>],
_pin: gpio::$gpio<$crate::Analog>,
) -> Result<Self, ()> {
let dac = Self {
_private: PhantomData,
Expand All @@ -125,3 +125,33 @@ macro_rules! impl_dac {
}

pub use impl_dac;

#[cfg(esp32)]
pub mod implementation {
//! Digital to analog (DAC) conversion.
//!
//! This module provides functions for controling two digital to
//! analog converters, available on ESP32: `DAC1` and `DAC2`.
//!
//! The DAC1 is available on the GPIO pin 25, and DAC2 on pin 26.

pub use super::*;
use crate::impl_dac;

impl_dac!(1 => Gpio25, 2 => Gpio26,);
}

#[cfg(esp32s2)]
pub mod implementation {
//! Digital to analog (DAC) conversion.
//!
//! This module provides functions for controling two digital to
//! analog converters, available on ESP32: `DAC1` and `DAC2`.
//!
//! The DAC1 is available on the GPIO pin 17, and DAC2 on pin 18.

pub use super::*;
use crate::impl_dac;

impl_dac!(1 => Gpio17, 2 => Gpio18,);
}
Loading

0 comments on commit bc4e180

Please sign in to comment.