How to pass a stm32f3discovery API into a function?

59 Views Asked by At

I am trying to create a separate file/module that has functions that can deal with the LEDs or gyro for the stm32f3discovery. I am trying to pass the stm32f3 API that holds all of the registers into a function to then use inside.

When I run this code, I get an error saying "there is no field '###' on type '##'". How can I do this?

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

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| {
   |               ^^^

It has this error for all of the calls onto any of the registers

1

There are 1 best solutions below

0
On

Instead of using a generic to try and force our way into passing in a type that you don't know use something like:

let my_name: () = 39.2;

It will give an error that will tell you what the value on the right is and you can use that to work out what data type you can pass into the function. As shown printing variable type in rust