Skip to content

Commit 1026fbc

Browse files
committed
Allow configuration of auto reload value
1 parent d494da0 commit 1026fbc

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/qei.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::pwm_input::Pins;
2121
use crate::timer::{sealed::Remap, Timer};
2222

2323
/// SMS (Slave Mode Selection) register
24+
#[derive(Copy, Clone, Debug)]
2425
pub enum SlaveMode {
2526
/// Counter counts up/down on TI2FP1 edge depending on TI1FP2 level.
2627
EncoderMode1 = 0b001,
@@ -39,14 +40,27 @@ pub enum SlaveMode {
3940
ExternalClockMode1 = 0b111,
4041
}
4142

43+
/// Quadrature Encoder Interface (QEI) options
44+
///
45+
/// The `Default` implementation provides a configuration for a 4-count pulse which counts from
46+
/// 0-65535. The counter wraps back to 0 on overflow.
47+
#[derive(Copy, Clone, Debug)]
4248
pub struct QeiOptions {
43-
slave_mode: SlaveMode,
49+
/// Encoder slave mode
50+
pub slave_mode: SlaveMode,
51+
52+
/// Autoreload value
53+
///
54+
/// This value allows the maximum count to be configured, up to 65535. Setting a lower value
55+
/// will overflow the counter to 0 sooner.
56+
pub auto_reload_value: u16,
4457
}
4558

4659
impl Default for QeiOptions {
4760
fn default() -> Self {
4861
Self {
4962
slave_mode: SlaveMode::EncoderMode3,
63+
auto_reload_value: u16::MAX,
5064
}
5165
}
5266
}
@@ -154,7 +168,7 @@ macro_rules! hal {
154168
// configure as quadrature encoder
155169
tim.smcr.write(|w| w.sms().bits(options.slave_mode as u8));
156170

157-
tim.arr.write(|w| w.arr().bits(u16::MAX));
171+
tim.arr.write(|w| w.arr().bits(options.auto_reload_value));
158172
tim.cr1.write(|w| w.cen().set_bit());
159173

160174
Qei { tim, pins, _remap: PhantomData }

0 commit comments

Comments
 (0)