简体   繁体   中英

How do you use a teensy 4 pin via the teensy4-bsp Rust crate as an input with the pull-up resistor enabled?

I am trying to figure out how to do the Rust equivalent of

pinMode(PIN_D7, INPUT_PULLUP); // Pushbutton

(from https://www.pjrc.com/teensy/td_digital.html )

I have created a project using the template https://github.com/mciantyre/teensy4-rs-template as outlined in the Getting Started section of https://github.com/mciantyre/teensy4-rs .

Unfortunately, the Rust arduino code is a rabbit hole that IntelliJ IDEA can not fully navigate (they use macros to generate struct s and impl s), so I do not get any helpful completion results that would help me figure out what methods and fields are available.

I'm not sure what to do with pins.p7 to activate the pull-up resistor, or even sample it. Chasing the docs from p7 to P7 to B1_01 toPad leaves me still confused.

(documenting some failure here)

I did some experiments and some text searches across the crates and found the Config structure.

Unfortunately, when I used it like this, the results were not reliable.

// pull-down resistor.  Switch drags to 3.3v
fn mission1(mut switch_pin: B0_10, led: &mut LED, systick: &mut SysTick) -> !
{
    let cfg = teensy4_bsp::hal::iomuxc::Config::zero().set_pullupdown(PullUpDown::Pulldown100k);
    iomuxc::configure(&mut switch_pin, cfg);

    let bacon = GPIO::new(switch_pin);

    loop {
        if bacon.is_set() {
            led.toggle()
        }
        systick.delay(300);
    }
}

It still picked up spurious button clicks. I turned things around and tried to rig it for pull-up

// pull-up resistor.  Switch drags to ground
fn mission2(mut switch_pin: B0_10, led: &mut LED, systick: &mut SysTick) -> !
{

    let pull_up = match 22
    {
        100 => PullUpDown::Pullup100k, // unreliable
        47 => PullUpDown::Pullup47k, // unreliable
        _ => PullUpDown::Pullup22k,
    };
    let cfg = teensy4_bsp::hal::iomuxc::Config::zero().set_pullupdown(pull_up);
    iomuxc::configure(&mut switch_pin, cfg);

    let bacon = GPIO::new(switch_pin);

    loop {
        if ! bacon.is_set() {
            led.toggle()
        }
        systick.delay(300);
    }
}

All 3 of the pull-up options were not useful. I attached a multimeter and it was reading about.067v between the pin and ground with the switch open and the 22k pull-up resistor option.

When I wire up a physical 10K resistor it behaves like I expect and the multimeter measures 3.23V. If I wire two 10Ks in series for pull-up, it measures 3.20V.

I am going to say that this is not the proper technique for a Teensy 4.0.

Based on the response to https://github.com/mciantyre/teensy4-rs/issues/107 and the code at https://github.com/imxrt-rs/imxrt-hal/issues/112 I was able to create the following example that seems to work on my teensy 4.0

let cfg = Config::zero()
    .set_hysteresis(Hysteresis::Enabled)
    .set_pull_keep(PullKeep::Enabled)
    .set_pull_keep_select(PullKeepSelect::Pull)
    .set_pullupdown(PullUpDown::Pulldown100k);
iomuxc::configure(&mut switch_pin, cfg);

let switch_gpio = GPIO::new(switch_pin);

loop {
    if switch_gpio.is_set() {
        led.toggle()
    }
    systick.delay(LED_PERIOD_MS);
}

Full code at https://github.com/mciantyre/teensy4-rs/blob/997d92cc880185f22272d1cfd54de54732154bb5/examples/pull_down_pin.rs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM