[英]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我想弄清楚如何做 Rust 相当于
pinMode(PIN_D7, INPUT_PULLUP); // Pushbutton
(from https://www.pjrc.com/teensy/td_digital.html ) (来自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 .我已经使用模板https://github.com/mciantyre/teensy4-rs-template创建了一个项目,如 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.不幸的是,Rust arduino 代码是 IntelliJ IDEA 无法完全导航的兔子洞(他们使用宏来生成
struct
s 和impl
s),所以我没有得到任何有用的完成结果来帮助我弄清楚哪些方法和字段是可用的。
I'm not sure what to do with pins.p7
to activate the pull-up resistor, or even sample it.我不确定如何处理
pins.p7
来激活上拉电阻,甚至对其进行采样。 Chasing the docs from p7
to P7
to B1_01
toPad
leaves me still confused.从
p7
到P7
到B1_01
到Pad
的文档让我仍然感到困惑。
(documenting some failure here) (在这里记录一些失败)
I did some experiments and some text searches across the crates and found the Config
structure.我在 crates 中进行了一些实验和一些文本搜索,并找到了
Config
结构。
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.所有 3 个上拉选项都没有用。 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.
我连接了一个万用表,在开关打开和 22k 上拉电阻选项的情况下,引脚和接地之间的读数约为.067v。
When I wire up a physical 10K resistor it behaves like I expect and the multimeter measures 3.23V.当我连接一个 10K 物理电阻时,它的行为与我预期的一样,万用表测量为 3.23V。 If I wire two 10Ks in series for pull-up, it measures 3.20V.
如果我串联两个 10K 用于上拉,它的测量值为 3.20V。
I am going to say that this is not the proper technique for a Teensy 4.0.我要说这不是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根据对 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);
}
Full code at https://github.com/mciantyre/teensy4-rs/blob/997d92cc880185f22272d1cfd54de54732154bb5/examples/pull_down_pin.rs .完整代码在https://github.com/mciantyre/teensy4-rs/blob/997d92cc880185f22272d1cfd54de54732154bb5/examples/pull_down_pin.rs 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.