Stm32f103 led asynchronously

I'm completely new to Rust, and way out of my depth. I have a lot of C++ experience, though.

I'm trying to get the stm32f1xx_hal working. The examples for a simple LED seem fairly convoluted. Here's how to set up pin pc13 for output, for example:

    let dp = pac::Peripherals::take().unwrap();
    let mut rcc = dp.RCC.constrain();
    let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
    let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);

It would be nice if I could type something like

   let mut pc13 = pin_out(PC13);

Maybe creating an enum Pin { ... }.

However, there appears to be easy way to do this. GPIOA, GPIOB, ... seem to be of fundamentally different and incompatible types, even though, presumably, what I'm trying to obtain is a standard output pin that is the same regardless of the GPIO port I use.

Hmmm. I don't think the stm32f103 hal actually provides a neat answer. It seems that I'd have to hard-code every GPIO pin possibility, or else basically come up with something that peeks and pokes raw register addresses.

Is there a simple solution to this?

Also, what if I want to blink two LEDs asynchronously? What's the "standard solution"?

1 Like

I thought maybe I could use a sum_type?

So I did this:

sum_type! {
    pub enum Port { 
        PortA(stm32f1xx_hal::pac::Peripherals::GPIOA),           
        PortB(stm32f1xx_hal::pac::Peripherals::GPIOB),           
    }   
}  

but now it complains

use fully-qualified syntax: `<stm32f1xx_hal::pac::Peripherals as Trait>::GPIOB`

Hmmm.

My experience with the HAL crates has been largely the same as yours. Super convoluted to get anything done, examples are not consistent across HALs, not even within a single HAL - some examples in the nrf-hal crates are bare-metal, some use RTIC, etc. Unfortunately I don't really have the skill or the knowledge to propose anything better.

The silver lining is that once you're past setting up the peripherals and stuff, everything is pretty much just regular Rust.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.