Skip to content

Provide a way to construct a DynPin (for C interop) #481

@9names

Description

@9names

cr1901 asked on #rust_embedded if there was a way to construct a pin for use with C code.
As in: existing C project. new code written in Rust, assuming it's called through an extern "C" fn

We're almost able to construct a DynPin from scratch now, but the DynRegisters struct is private.

Here's an example of the almost working syntax we currently have:

use embedded_hal::digital::v2::ToggleableOutputPin;
use hal::gpio::*;
use hal::gpio::DynGroup::Bank0;
use hal::gpio::DynOutput::PushPull;

extern "C" fn do_work(pin_id: u8) {
    let pin = DynPinId {
        group: Bank0,
        num: pin_id,
    };
    let mode = DynPinMode::Output(PushPull);
    // This doesn't work because DynPin::new() is private
    let mut dynpin1 = unsafe { DynPin::new(pin, mode) };
    // Panic if the pin wasn't in the right mode
    dynpin1.toggle().unwrap();
}

If we were to provide such a facility (and I think we should) how should we enable this?
Should we add an (unsafe) function that allows you to conjure a DynPin based solely on pin number and mode, along with documentation on how it will explode if you get these wrong?
Would appreciate any thoughts on whether we should do this, and how.

See the below draft PR for a simple impl

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions