如何通过 teensy4-bsp Rust crate 使用 teensy 4 引脚作为启用上拉电阻的输入?

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

我想弄清楚如何做 Rust 等同于

pinMode(PIN_D7, INPUT_PULLUP); // Pushbutton

(来自 https://www.pjrc.com/teensy/td_digital.html

我已经使用模板 https://github.com/mciantyre/teensy4-rs-template as outlined in the Getting Started section of https://github.com/mciantyre/teensy4-rs 创建了一个项目。

不幸的是,Rust arduino 代码是一个 IntelliJ IDEA 无法完全导航的兔子洞(他们使用宏来生成 structs 和 impls),所以我没有得到任何帮助完成结果将帮助我找出可用的方法和字段。

我不确定如何使用 pins.p7 来激活上拉电阻,甚至对其进行采样。从 p7P7 再到 B1_01 再到 Pad 的文档让我仍然感到困惑。

(在此处记录一些失败)

我在 crate 中做了一些实验和一些文本搜索,找到了 Config 结构。

不幸的是,当我这样使用它时,结果并不可靠。

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

它仍然检测到虚假的按钮点击。我扭转局面并尝试将其装配成上拉

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

所有 3 个上拉选项都没有用。我连接了一个万用表,在开关打开和 22k 上拉电阻选项的情况下,它在引脚和地之间的读数约为 0.67v。

当我连接一个 10K 的物理电阻器时,它的行为与我预期的一样,万用表的测量值为 3.23V。如果我串联两个 10Ks 用于上拉,它测量到 3.20V。

我要说的是,这不是适合 Teensy 4.0 的技术。

根据对 https://github.com/mciantyre/teensy4-rs/issues/107 and the code at https://github.com/imxrt-rs/imxrt-hal/issues/112 的回复,我能够创建以下似乎适用于我的 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);
}

完整代码位于 https://github.com/mciantyre/teensy4-rs/blob/997d92cc880185f22272d1cfd54de54732154bb5/examples/pull_down_pin.rs .