From 1e50d3cf79e815e4c5f48f70bb84ca6124d2fb14 Mon Sep 17 00:00:00 2001 From: Brian Schwind Date: Tue, 19 Dec 2023 09:49:48 +0900 Subject: [PATCH] Move the encoder mode register write to counter.rs, document --- src/counter.rs | 10 ++++++++++ src/main.rs | 11 ----------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/counter.rs b/src/counter.rs index 31d4e60..ef174a5 100644 --- a/src/counter.rs +++ b/src/counter.rs @@ -8,6 +8,16 @@ pub struct Counter { impl Counter { pub fn new(qei: Qei) -> 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 } } diff --git a/src/main.rs b/src/main.rs index cc727c7..6e7a07d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, @@ -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();