繁体   English   中英

如何通过 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创建了一个项目,如 https://github.com/mciantyre/teensy4-rs 的入门部分中所述

不幸的是,Rust arduino 代码是 IntelliJ IDEA 无法完全导航的兔子洞(他们使用宏来生成struct s 和impl s),所以我没有得到任何有用的完成结果来帮助我弄清楚哪些方法和字段是可用的。

我不确定如何处理pins.p7来激活上拉电阻,甚至对其进行采样。 p7P7B1_01Pad的文档让我仍然感到困惑。

(在这里记录一些失败)

我在 crates 中进行了一些实验和一些文本搜索,并找到了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 上拉电阻选项的情况下,引脚和接地之间的读数约为.067v。

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

我要说这不是Teensy 4.0 的正确技术。

根据对 https://github.com/mciantyre/teensy4-rs/issues/107 的响应和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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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