Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type for gpio::AnyPin #1066

Closed
wants to merge 1 commit into from

Conversation

Volkalex28
Copy link
Contributor

This makes it possible to safely implement the InputPin and OutputPin traits for AnyPin. Now you can convert any pin to AnyPin with the appropriate type and use it in other library modules

Added:

  • Peripheral implementation for AnyPin
  • Implementation of Pin for AnyPin
  • Implementation of OutputPin for AnyPin with type IsOutputPin
  • Implementation of InputPin for AnyPin with type IsInputPin
  • Upgrade types for AnyPin (for example InputOutputAnalogPinType -> InputOutputPinType)
  • Implementation of From for AnyPin with the appropriate type

Changed:

  • The Gpio::degrage method returns AnyPin with the appropriate type

A small example where this can be useful:

#![no_std]
#![no_main]

use esp32s3_hal::{clock::ClockControl, gpio::IO, i2c::I2C, peripherals::Peripherals, prelude::*};
use esp_backtrace as _;
use esp_println::println;

enum BoardRevision {
    Dev,
    Release
}

// Take data about the device on which the program is running. 
// This is not known at the compilation stage, but is extracted at runtime,
// for example from the device serial number
fn get_board_revision() -> BoardRevision {
    //...
}

#[entry]
fn main() -> ! {
    let peripherals = Peripherals::take();
    let system = peripherals.SYSTEM.split();
    let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

    let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
    
    // Select pins depending on BoardRevision
    let (sda, scl) = match get_board_revision() {
        BoardRevision::Dev => (io.pins.gpio43.degrade(), io.pins.gpio44.degrade()),
        BoardRevision::Release=>(io.pins.gpio41.degrade(), io.pins.gpio38.degrade()),
    }

    // Create a new peripheral object with the described wiring
    // and standard I2C clock speed
    let mut i2c = I2C::new(
        peripherals.I2C0,
        io.pins.gpio1,
        io.pins.gpio2,
        100u32.kHz(),
        &clocks,
    );

    loop {
        let mut data = [0u8; 22];
        i2c.write_read(0x77, &[0xaa], &mut data).ok();

        println!("{:02x?}", data);
    }
}

This makes it possible to safely implement the InputPin and OutputPin
traits for AnyPin. Now you can convert any pin to AnyPin with the
appropriate type and use it in other library modules

Added:
- Peripheral implementation for AnyPin
- Implementation of Pin for AnyPin
- Implementation of OutputPin for AnyPin with type IsOutputPin
- Implementation of InputPin for AnyPin with type IsInputPin
- Upgrade types for AnyPin (for example InputOutputAnalogPinType ->
InputOutputPinType)
- Implementation of From<Gpio> for AnyPin with the appropriate type

Changed:
- The Gpio::degrage method returns AnyPin with the appropriate type

undefined
@Volkalex28 Volkalex28 closed this Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant