如何将stm32f3discovery API传递到一个函数中?

问题描述 投票:0回答:1

我试图创建一个单独的filemodule,其中的函数可以处理stm32f3discovery的LED或陀螺。我试图将持有所有寄存器的stm32f3 API传递到一个函数中,然后在里面使用。

当我运行这段代码时,我得到一个错误,说 "在'##'类型上没有'##'字段"。我如何才能做到这一点?

main.rs

#![no_std]
#![no_main]
use stm32f3::stm32f303;

mod my_api;

#[entry]
fn main() -> ! {
    let periph = stm32f303::Peripherals::take().unwrap();

    let gpioe = periph.GPIOE;
    let rcc = periph.RCC;

    my_api::led::setup_led(&gpioe, &rcc);

    loop {
        my_api::led::all_led_on(&gpioe);
    }
}

my_api.rs

pub mod led {
    pub fn setup_led<G, R>(gpio: &G, rcc: &R) {
        *rcc.ahbenr.modify(|_, w| w.iopeen().set_bit()); //enables clock
        *gpio.moder.modify(|_, w| {
            w.moder8().bits(0b01);
            w.moder9().bits(0b01);
            w.moder10().bits(0b01);
            w.moder11().bits(0b01);
            w.moder12().bits(0b01);
            w.moder13().bits(0b01);
            w.moder14().bits(0b01);
            w.moder15().bits(0b01)
        });
    }

    pub fn all_led_on<G>(gpio: &G) {
        *gpio.odr.modify(|_, w| {
            w.odr8().set_bit();
            w.odr9().set_bit();
            w.odr10().set_bit();
            w.odr11().set_bit();
            w.odr12().set_bit();
            w.odr13().set_bit();
            w.odr14().set_bit();
            w.odr15().set_bit()
        });
    }

    pub fn all_led_off<G>(gpio: &G) {
        *gpio.odr.modify(|_, w| {
            w.odr8().clear_bit();
            w.odr9().clear_bit();
            w.odr10().clear_bit();
            w.odr11().clear_bit();
            w.odr12().clear_bit();
            w.odr13().clear_bit();
            w.odr14().clear_bit();
            w.odr15().clear_bit()
        });
    }
}

錯誤

error[E0609]: no field `odr` on type `&G`
  --> src/my_api.rs:30:15
   |
29 |     pub fn all_led_off <G> (gpio: &G) {
   |                         - type parameter 'G' declared here
30 |         *gpio.odr.modify(|_,w| {
   |               ^^^

对任何一个寄存器的调用都会出现这个错误。

api rust peripherals stm32f3
1个回答
0
投票

与其使用一个泛型来尝试并强迫我们传递一个你不知道的类型,不如使用类似的东西。

let my_name: () = 39. 2;

它将给出一个错误,告诉你右边的值是什么,你可以用它来计算你可以传入函数的数据类型。如图所示 锈迹斑斑的印刷字体

© www.soinside.com 2019 - 2024. All rights reserved.