stm32-rs / stm32l0xx-hal

A hardware abstraction layer (HAL) for the STM32L0 series microcontrollers written in Rust
BSD Zero Clause License
96 stars 60 forks source link

Add QEI support for LPTIM #144

Closed jamwaffles closed 3 years ago

jamwaffles commented 3 years ago

As per the title, adds support for turning on encoder mode for LPTIM. e.g.

let mut pwr = PWR::new(dp.PWR, &mut rcc);

let lptim = LpTimer::init_encoder(
    dp.LPTIM,
    &mut pwr,
    &mut rcc,
    ClockSrc::Hsi16,
    (gpiob.pb5, gpiob.pb7),
    // Roll over at 4096
    4096,
);

defmt::info!("Count {:?}", lptim.count());
jamwaffles commented 3 years ago

This was tested on an L053 Nucleo with a lil hand encoder module from Aliexpress.

jamwaffles commented 3 years ago

I just realised it's not possible to enable interrupts with this API. Converting to draft until that's fixed...

jamwaffles commented 3 years ago

The API looks like this now:

let mut encoder = LpTimer::init_encoder(
    LPTIM,
    &mut pwr,
    &mut rcc,
    // Use low speed internal oscillator
    lptim::ClockSrc::Lsi,
    (gpiob.pb5, gpiob.pb7),
);

encoder.enable_interrupts(lptim::Interrupts {
    enc_dir_up: true,
    ..lptim::Interrupts::default()
});

// Set ARR to 4096
encoder.enable(4096);

I don't think it's very ergonomic, but at least it works... it also reuses as much of the existing API as possible.

Very open to suggestions on how to make this nicer to use!

jamwaffles commented 3 years ago

Review comments addressed - I added a more descriptive comment for arr, although I left it vague as I'm unsure what it wraps around to. The value in CMP maybe?

I also added a disable method as suggested.

jamwaffles commented 3 years ago

Thanks! One less patch to keep in my fork :P