Skip to content

Commit

Permalink
Move the encoder mode register write to counter.rs, document
Browse files Browse the repository at this point in the history
  • Loading branch information
bschwind committed Dec 19, 2023
1 parent 33044db commit 1e50d3c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
10 changes: 10 additions & 0 deletions src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ pub struct Counter<PINS> {

impl<PINS> Counter<PINS> {
pub fn new(qei: Qei<TIM1, PINS>) -> Self {
unsafe {
// TODO(bschwind) - Expose this functionality with a safe interface
// in stm32f4xx-hal.
// Change the mode of the QEI decoder to mode 1:
// Counter counts up/down on TI2FP1 edge depending on TI1FP2 level.
// Or in layman's terms, the encoder counts up and down on encoder
// pin A edges, while referencing the state of encoder pin B.
(*TIM1::ptr()).smcr.write(|w| w.sms().encoder_mode_1());
}

let last_count = qei.count();
Counter { qei, last_count }
}
Expand Down
11 changes: 0 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use hal::{
otg_fs::{UsbBus, USB},
pac::TIM1,
prelude::*,
pwm,
qei::Qei,
Expand Down Expand Up @@ -119,16 +118,6 @@ fn main() -> ! {
let rotary_encoder_pins = (gpioa.pa8.into_alternate_af1(), gpioa.pa9.into_alternate_af1());
let rotary_encoder = Qei::new(rotary_encoder_timer, rotary_encoder_pins);

unsafe {
// Change the polarity of the encoder to Falling edge - it could better align with the detents of our volume dial.
// Taken form https://github.com/stm32-rs/stm32f4xx-hal/issues/410
// (*TIM1::ptr()).ccer.write(|w| w.cc1p().set_bit().cc2p().set_bit());

// Change the mode of the QEI decoder to mode 1:
// Counter counts up/down on TI2FP1 edge depending on TI1FP2 level.
(*TIM1::ptr()).smcr.write(|w| w.sms().encoder_mode_1());
}

let mut counter = Counter::new(rotary_encoder);

let button_pin = gpioa.pa10.into_pull_up_input();
Expand Down

0 comments on commit 1e50d3c

Please sign in to comment.