Skip to content

Commit

Permalink
GPIO: Use Level enum instead of plain bool in constructors (#1574)
Browse files Browse the repository at this point in the history
* GPIO: Use Level enum instead of plain bool in constructors

* changelog
  • Loading branch information
JurajSadel authored May 22, 2024
1 parent c6ffbb6 commit 60d39e9
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 77 deletions.
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Refactoring of GPIO module, have drivers for Input,Output,OutputOpenDrain, all drivers setup their GPIOs correctly (#1542)
- DMA transactions are now found in the `dma` module (#1550)
- Remove unnecessary generics from PARL_IO driver (#1545)
- Use `Level enum` in GPIO constructors instead of plain bools (#1574)

### Removed

Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/etm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
//! ## Example
//! ```no_run
//! let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
//! let mut led = io.pins.gpio1.into_push_pull_output();
//! let button = io.pins.gpio9.into_pull_down_input();
//! let mut led = io.pins.gpio1;
//! let button = io.pins.gpio9;
//!
//! // setup ETM
//! let gpio_ext = GpioEtmChannels::new(peripherals.GPIO_SD);
Expand Down
14 changes: 7 additions & 7 deletions esp-hal/src/gpio/etm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
//! GpioEtmOutputConfig {
//! open_drain: false,
//! pull: Pull::None,
//! initial_state: false,
//! initial_state: Level::Low,
//! },
//! );
//! let button_event = gpio_ext
Expand All @@ -37,7 +37,7 @@
//! ```
use crate::{
gpio::Pull,
gpio::{Level, Pull},
peripheral::{Peripheral, PeripheralRef},
private,
};
Expand Down Expand Up @@ -252,15 +252,15 @@ pub struct GpioEtmOutputConfig {
/// Only used when open-drain
pub pull: Pull,
/// Initial pin state
pub initial_state: bool,
pub initial_state: Level,
}

impl Default for GpioEtmOutputConfig {
fn default() -> Self {
Self {
open_drain: false,
pull: Pull::None,
initial_state: false,
initial_state: Level::Low,
}
}
}
Expand All @@ -285,7 +285,7 @@ impl<const C: u8> GpioEtmTaskChannel<C> {
{
crate::into_ref!(pin);

pin.set_output_high(pin_config.initial_state, private::Internal);
pin.set_output_high(pin_config.initial_state.into(), private::Internal);
if pin_config.open_drain {
pin.internal_pull_down(pin_config.pull == Pull::Down, private::Internal);
pin.internal_pull_up(pin_config.pull == Pull::Up, private::Internal);
Expand All @@ -309,7 +309,7 @@ impl<const C: u8> GpioEtmTaskChannel<C> {
{
crate::into_ref!(pin);

pin.set_output_high(pin_config.initial_state, private::Internal);
pin.set_output_high(pin_config.initial_state.into(), private::Internal);
if pin_config.open_drain {
pin.internal_pull_down(pin_config.pull == Pull::Down, private::Internal);
pin.internal_pull_up(pin_config.pull == Pull::Up, private::Internal);
Expand All @@ -333,7 +333,7 @@ impl<const C: u8> GpioEtmTaskChannel<C> {
{
crate::into_ref!(pin);

pin.set_output_high(pin_config.initial_state, private::Internal);
pin.set_output_high(pin_config.initial_state.into(), private::Internal);
if pin_config.open_drain {
pin.internal_pull_down(pin_config.pull == Pull::Down, private::Internal);
pin.internal_pull_up(pin_config.pull == Pull::Up, private::Internal);
Expand Down
16 changes: 8 additions & 8 deletions esp-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1557,10 +1557,10 @@ where
{
/// Create GPIO output driver for a [GpioPin] with the provided level
#[inline]
pub fn new(pin: impl crate::peripheral::Peripheral<P = P> + 'd, initial_output: bool) -> Self {
pub fn new(pin: impl crate::peripheral::Peripheral<P = P> + 'd, initial_output: Level) -> Self {
crate::into_ref!(pin);

pin.set_output_high(initial_output, private::Internal);
pin.set_output_high(initial_output.into(), private::Internal);
pin.set_to_push_pull_output(private::Internal);

Self { pin }
Expand Down Expand Up @@ -1683,11 +1683,11 @@ where
#[inline]
pub fn new(
pin: impl crate::peripheral::Peripheral<P = P> + 'd,
initial_output: bool,
initial_output: Level,
pull: Pull,
) -> Self {
crate::into_ref!(pin);
pin.set_output_high(initial_output, private::Internal);
pin.set_output_high(initial_output.into(), private::Internal);
pin.set_to_open_drain_output(private::Internal);
pin.internal_pull_down(pull == Pull::Down, private::Internal);
pin.internal_pull_up(pull == Pull::Up, private::Internal);
Expand Down Expand Up @@ -1785,11 +1785,11 @@ impl<'d> AnyOutput<'d> {
#[inline]
pub fn new<P: OutputPin + CreateErasedPin>(
pin: impl crate::peripheral::Peripheral<P = P> + 'd,
initial_output: bool,
initial_output: Level,
) -> Self {
crate::into_ref!(pin);

pin.set_output_high(initial_output, private::Internal);
pin.set_output_high(initial_output.into(), private::Internal);
pin.set_to_push_pull_output(private::Internal);

let pin = pin.erased_pin(private::Internal);
Expand Down Expand Up @@ -1912,11 +1912,11 @@ impl<'d> AnyOutputOpenDrain<'d> {
#[inline]
pub fn new<P: OutputPin + InputPin + CreateErasedPin>(
pin: impl crate::peripheral::Peripheral<P = P> + 'd,
initial_output: bool,
initial_output: Level,
pull: Pull,
) -> Self {
crate::into_ref!(pin);
pin.set_output_high(initial_output, private::Internal);
pin.set_output_high(initial_output.into(), private::Internal);
pin.internal_pull_down(pull == Pull::Down, private::Internal);
pin.internal_pull_up(pull == Pull::Up, private::Internal);
pin.set_to_open_drain_output(private::Internal);
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/rmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
//! let mut channel = rmt
//! .channel0
//! .configure(
//! io.pins.gpio1.into_push_pull_output(),
//! io.pins.gpio1,
//! TxChannelConfig {
//! clk_divider: 1,
//! idle_output_level: false,
Expand Down
31 changes: 5 additions & 26 deletions esp-hal/src/soc/esp32c6/lp_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,22 @@
//!
//! ## Example
//! ```no_run
//! const CODE: &[u8] = &[
//! 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00,
//! 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00,
//! 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13,
//! 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
//! 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00,
//! 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00,
//! 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13,
//! 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
//! 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x17, 0x05, 0x00, 0x00, 0x13, 0x05, 0x05,
//! 0x04, 0x85, 0x45, 0x23, 0x00, 0xb5, 0x00, 0xb7, 0x26, 0x0b, 0x60, 0xa1, 0x06, 0x37, 0x26,
//! 0x0b, 0x60, 0x11, 0x06, 0x09, 0x47, 0x18, 0xc2, 0xb7, 0x47, 0x0f, 0x00, 0x93, 0x87, 0x07,
//! 0x24, 0xfd, 0x17, 0xfd, 0xff, 0x85, 0x05, 0x23, 0x00, 0xb5, 0x00, 0x98, 0xc2, 0xb7, 0x47,
//! 0x0f, 0x00, 0x93, 0x87, 0x07, 0x24, 0xfd, 0x17, 0xfd, 0xff, 0xf9, 0xbf, 0x00, 0x00, 0x00,
//! 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
//! ];
//!
//! // configure GPIO 1 as LP output pin
//! let mut lp_pin = io.pins.gpio1.into_low_power();
//! lp_pin.output_enable(true);
//! let lp_pin = LowPowerOutput::new(io.pins.gpio1);
//!
//! let mut lp_core = esp_hal::lp_core::LpCore::new(peripherals.LP_CORE);
//! lp_core.stop();
//! println!("lp core stopped");
//!
//! // copy code to LP ram
//! let lp_ram = 0x5000_0000 as *mut u8;
//! unsafe {
//! core::ptr::copy_nonoverlapping(CODE as *const _ as *const u8, lp_ram, CODE.len());
//! }
//! println!("copied code (len {})", CODE.len());
//! // load code to LP core
//! let lp_core_code =
//! load_lp_code!("../esp-lp-hal/target/riscv32imac-unknown-none-elf/release/examples/blinky");
//!
//! // start LP core
//! lp_core.run(lp_core::LpCoreWakeupSource::HpCpu);
//! println!("lpcore run");
//!
//! let data = (0x500000c0) as *mut u32;
//! let data = (0x5000_2000) as *mut u32;
//! loop {
//! print!("Current {:x} \u{000d}", unsafe {
//! data.read_volatile()
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use esp_backtrace as _;
use esp_hal::{
clock::ClockControl,
delay::Delay,
gpio::{Io, Output},
gpio::{Io, Level, Output},
peripherals::Peripherals,
prelude::*,
system::SystemControl,
Expand All @@ -25,7 +25,7 @@ fn main() -> ! {

// Set GPIO0 as an output, and set its state high initially.
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
let mut led = Output::new(io.pins.gpio0, true);
let mut led = Output::new(io.pins.gpio0, Level::High);

// Initialize the Delay peripheral, and use it to toggle the LED state in a
// loop.
Expand Down
8 changes: 4 additions & 4 deletions examples/src/bin/blinky_erased_pins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use esp_backtrace as _;
use esp_hal::{
clock::ClockControl,
delay::Delay,
gpio::{AnyInput, AnyOutput, Io, Pull},
gpio::{AnyInput, AnyOutput, Io, Level, Pull},
peripherals::Peripherals,
prelude::*,
system::SystemControl,
Expand All @@ -29,9 +29,9 @@ fn main() -> ! {
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);

// Set LED GPIOs as an output:
let led1 = AnyOutput::new(io.pins.gpio2, false);
let led2 = AnyOutput::new(io.pins.gpio4, false);
let led3 = AnyOutput::new(io.pins.gpio5, false);
let led1 = AnyOutput::new(io.pins.gpio2, Level::Low);
let led2 = AnyOutput::new(io.pins.gpio4, Level::Low);
let led3 = AnyOutput::new(io.pins.gpio5, Level::Low);

// Use boot button as an input:
#[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))]
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/embassy_multicore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use esp_hal::{
cpu_control::{CpuControl, Stack},
embassy::{self, executor::Executor},
get_core,
gpio::{AnyOutput, Io},
gpio::{AnyOutput, Io, Level},
peripherals::Peripherals,
prelude::*,
system::SystemControl,
Expand Down Expand Up @@ -66,7 +66,7 @@ async fn main(_spawner: Spawner) {
static LED_CTRL: StaticCell<Signal<CriticalSectionRawMutex, bool>> = StaticCell::new();
let led_ctrl_signal = &*LED_CTRL.init(Signal::new());

let led = AnyOutput::new(io.pins.gpio0, false);
let led = AnyOutput::new(io.pins.gpio0, Level::Low);

let _guard = cpu_control
.start_app_core(unsafe { &mut *addr_of_mut!(APP_CORE_STACK) }, move || {
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/embassy_multicore_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use esp_hal::{
cpu_control::{CpuControl, Stack},
embassy::{self, executor::InterruptExecutor},
get_core,
gpio::{AnyOutput, Io},
gpio::{AnyOutput, Io, Level},
interrupt::Priority,
peripherals::Peripherals,
prelude::*,
Expand Down Expand Up @@ -85,7 +85,7 @@ fn main() -> ! {
static LED_CTRL: StaticCell<Signal<CriticalSectionRawMutex, bool>> = StaticCell::new();
let led_ctrl_signal = &*LED_CTRL.init(Signal::new());

let led = AnyOutput::new(io.pins.gpio0, false);
let led = AnyOutput::new(io.pins.gpio0, Level::Low);

static EXECUTOR_CORE_1: StaticCell<InterruptExecutor<1>> = StaticCell::new();
let executor_core1 =
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/embassy_rmt_rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use esp_backtrace as _;
use esp_hal::{
clock::ClockControl,
embassy::{self},
gpio::{Gpio5, Io, Output},
gpio::{Gpio5, Io, Level, Output},
peripherals::Peripherals,
prelude::*,
rmt::{asynch::RxChannelAsync, PulseCode, Rmt, RxChannelConfig, RxChannelCreatorAsync},
Expand Down Expand Up @@ -75,7 +75,7 @@ async fn main(spawner: Spawner) {
}

spawner
.spawn(signal_task(Output::new(io.pins.gpio5, false)))
.spawn(signal_task(Output::new(io.pins.gpio5, Level::Low)))
.unwrap();

let mut data = [PulseCode {
Expand Down
3 changes: 2 additions & 1 deletion examples/src/bin/etm_blinky_systimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use esp_hal::{
gpio::{
etm::{GpioEtmChannels, GpioEtmOutputConfig},
Io,
Level,
Pull,
},
peripherals::Peripherals,
Expand All @@ -37,7 +38,7 @@ fn main() -> ! {
GpioEtmOutputConfig {
open_drain: false,
pull: Pull::None,
initial_state: true,
initial_state: Level::High,
},
);

Expand Down
3 changes: 2 additions & 1 deletion examples/src/bin/etm_gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use esp_hal::{
gpio::{
etm::{GpioEtmChannels, GpioEtmInputConfig, GpioEtmOutputConfig},
Io,
Level,
Pull,
},
peripherals::Peripherals,
Expand All @@ -34,7 +35,7 @@ fn main() -> ! {
GpioEtmOutputConfig {
open_drain: false,
pull: Pull::None,
initial_state: false,
initial_state: Level::Low,
},
);
let button_event = gpio_ext
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/gpio_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use esp_backtrace as _;
use esp_hal::{
clock::ClockControl,
delay::Delay,
gpio::{self, Event, Input, Io, Output, Pull},
gpio::{self, Event, Input, Io, Level, Output, Pull},
macros::ram,
peripherals::Peripherals,
prelude::*,
Expand All @@ -36,7 +36,7 @@ fn main() -> ! {
// Set GPIO2 as an output, and set its state high initially.
let mut io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
io.set_interrupt_handler(handler);
let mut led = Output::new(io.pins.gpio2, false);
let mut led = Output::new(io.pins.gpio2, Level::Low);

#[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))]
let button = io.pins.gpio0;
Expand Down
6 changes: 3 additions & 3 deletions examples/src/bin/lcd_i8080.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use esp_hal::{
delay::Delay,
dma::{Dma, DmaPriority},
dma_buffers,
gpio::{Io, Output},
gpio::{Io, Level, Output},
lcd_cam::{
lcd::i8080::{Config, TxEightBits, I8080},
LcdCam,
Expand Down Expand Up @@ -67,8 +67,8 @@ fn main() -> ! {

let delay = Delay::new(&clocks);

let mut backlight = Output::new(lcd_backlight, false);
let mut reset = Output::new(lcd_reset, false);
let mut backlight = Output::new(lcd_backlight, Level::Low);
let mut reset = Output::new(lcd_reset, Level::Low);

let tx_pins = TxEightBits::new(
io.pins.gpio9,
Expand Down
13 changes: 7 additions & 6 deletions examples/src/bin/spi_eh1_device_loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use esp_backtrace as _;
use esp_hal::{
clock::ClockControl,
delay::Delay,
gpio::{self, Io, Output},
gpio::{self, Io, Level, Output},
peripherals::Peripherals,
prelude::*,
spi::{master::Spi, SpiMode},
Expand All @@ -63,19 +63,20 @@ fn main() -> ! {
gpio::NO_PIN,
);
let spi_bus = RefCell::new(spi_bus);
let mut spi_device_1 = RefCellDevice::new_no_delay(&spi_bus, Output::new(io.pins.gpio5, false));
let mut spi_device_1 =
RefCellDevice::new_no_delay(&spi_bus, Output::new(io.pins.gpio5, Level::Low));

cfg_if::cfg_if! {
if #[cfg(feature = "esp32")] {
let mut spi_device_2 =
RefCellDevice::new_no_delay(&spi_bus, Output::new(io.pins.gpio13, false));
RefCellDevice::new_no_delay(&spi_bus, Output::new(io.pins.gpio13, Level::Low));
let mut spi_device_3 =
RefCellDevice::new_no_delay(&spi_bus, Output::new(io.pins.gpio14,false));
RefCellDevice::new_no_delay(&spi_bus, Output::new(io.pins.gpio14, Level::Low));
} else {
let mut spi_device_2 =
RefCellDevice::new_no_delay(&spi_bus, Output::new(io.pins.gpio6,false));
RefCellDevice::new_no_delay(&spi_bus, Output::new(io.pins.gpio6, Level::Low));
let mut spi_device_3 =
RefCellDevice::new_no_delay(&spi_bus, Output::new(io.pins.gpio7, false));
RefCellDevice::new_no_delay(&spi_bus, Output::new(io.pins.gpio7, Level::Low));
}
}

Expand Down
Loading

0 comments on commit 60d39e9

Please sign in to comment.