GPIO: Use Level enum instead of plain bool in constructors (#1574)

* GPIO: Use Level enum instead of plain bool in constructors

* changelog
This commit is contained in:
Juraj Sadel 2024-05-22 14:37:03 +02:00 committed by GitHub
parent c6ffbb6c9d
commit 60d39e9f33
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 60 additions and 77 deletions

View File

@ -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

View File

@ -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);

View File

@ -28,7 +28,7 @@
//! GpioEtmOutputConfig {
//! open_drain: false,
//! pull: Pull::None,
//! initial_state: false,
//! initial_state: Level::Low,
//! },
//! );
//! let button_event = gpio_ext
@ -37,7 +37,7 @@
//! ```
use crate::{
gpio::Pull,
gpio::{Level, Pull},
peripheral::{Peripheral, PeripheralRef},
private,
};
@ -252,7 +252,7 @@ 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 {
@ -260,7 +260,7 @@ impl Default for GpioEtmOutputConfig {
Self {
open_drain: false,
pull: Pull::None,
initial_state: false,
initial_state: Level::Low,
}
}
}
@ -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);
@ -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);
@ -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);

View File

@ -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 }
@ -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);
@ -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);
@ -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);

View File

@ -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,

View File

@ -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()

View File

@ -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,
@ -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.

View File

@ -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,
@ -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"))]

View File

@ -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,
@ -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 || {

View File

@ -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::*,
@ -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 =

View File

@ -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},
@ -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 {

View File

@ -11,6 +11,7 @@ use esp_hal::{
gpio::{
etm::{GpioEtmChannels, GpioEtmOutputConfig},
Io,
Level,
Pull,
},
peripherals::Peripherals,
@ -37,7 +38,7 @@ fn main() -> ! {
GpioEtmOutputConfig {
open_drain: false,
pull: Pull::None,
initial_state: true,
initial_state: Level::High,
},
);

View File

@ -11,6 +11,7 @@ use esp_hal::{
gpio::{
etm::{GpioEtmChannels, GpioEtmInputConfig, GpioEtmOutputConfig},
Io,
Level,
Pull,
},
peripherals::Peripherals,
@ -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

View File

@ -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::*,
@ -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;

View File

@ -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,
@ -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,

View File

@ -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},
@ -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));
}
}

View File

@ -34,7 +34,7 @@ use esp_hal::{
delay::Delay,
dma::{Dma, DmaPriority},
dma_buffers,
gpio::{Input, Io, Output, Pull},
gpio::{Input, Io, Level, Output, Pull},
peripherals::Peripherals,
prelude::*,
spi::{
@ -53,13 +53,13 @@ fn main() -> ! {
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
let slave_sclk = io.pins.gpio0;
let mut master_sclk = Output::new(io.pins.gpio4, false);
let mut master_sclk = Output::new(io.pins.gpio4, Level::Low);
let slave_miso = io.pins.gpio1;
let master_miso = Input::new(io.pins.gpio5, Pull::None);
let slave_mosi = io.pins.gpio2;
let mut master_mosi = Output::new(io.pins.gpio8, false);
let mut master_mosi = Output::new(io.pins.gpio8, Level::Low);
let slave_cs = io.pins.gpio3;
let mut master_cs = Output::new(io.pins.gpio9, false);
let mut master_cs = Output::new(io.pins.gpio9, Level::Low);
master_cs.set_high();
master_sclk.set_low();
master_mosi.set_low();

View File

@ -18,7 +18,7 @@ use esp_hal::{
clock::ClockControl,
delay::Delay,
embassy,
gpio::{Gpio2, Gpio4, GpioPin, Input, Io, Output, Pull},
gpio::{Gpio2, Gpio4, GpioPin, Input, Io, Level, Output, Pull},
macros::handler,
peripherals::Peripherals,
system::SystemControl,
@ -50,7 +50,7 @@ impl<'d> Context<'d> {
Context {
io2: Input::new(io.pins.gpio2, Pull::Down),
io4: Output::new(io.pins.gpio4, false),
io4: Output::new(io.pins.gpio4, Level::Low),
delay,
}
}
@ -184,8 +184,8 @@ mod tests {
#[test]
fn test_gpio_od(ctx: Context<'static>) {
let mut io2 = OutputOpenDrain::new(unsafe { GpioPin::<2>::steal() }, true, Pull::Up);
let mut io4 = OutputOpenDrain::new(unsafe { GpioPin::<4>::steal() }, true, Pull::Up);
let mut io2 = OutputOpenDrain::new(unsafe { GpioPin::<2>::steal() }, Level::High, Pull::Up);
let mut io4 = OutputOpenDrain::new(unsafe { GpioPin::<4>::steal() }, Level::High, Pull::Up);
ctx.delay.delay_millis(1);