Skip to content

Commit f4cfefd

Browse files
bors[bot]burrbull
andauthored
Merge #403
403: qei cleanups r=therealprof a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents 9ed5acb + 0b2a560 commit f4cfefd

File tree

5 files changed

+33
-51
lines changed

5 files changed

+33
-51
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2626

2727
### Changed
2828

29+
- Qei macro cleanups [#403]
2930
- Update RTIC to 1.0 [#401]
3031
- Finish SDIO data transmission before querying card status in `write_block` [#395]
3132
- SDIO: Rewrite loop conditions to silence clippy
@@ -50,6 +51,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
5051
[#393]: https://github.com/stm32-rs/stm32f4xx-hal/pull/393
5152
[#395]: https://github.com/stm32-rs/stm32f4xx-hal/pull/395
5253
[#401]: https://github.com/stm32-rs/stm32f4xx-hal/pull/401
54+
[#401]: https://github.com/stm32-rs/stm32f4xx-hal/pull/403
5355

5456
## [v0.10.1] - 2021-10-26
5557

src/delay/timer.rs

+1-18
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,7 @@ hal! {
133133
pac::TIM5: (tim5, wait_tim5),
134134
}
135135

136-
#[cfg(any(
137-
feature = "stm32f401",
138-
feature = "stm32f405",
139-
feature = "stm32f407",
140-
feature = "stm32f411",
141-
feature = "stm32f412",
142-
feature = "stm32f413",
143-
feature = "stm32f415",
144-
feature = "stm32f417",
145-
feature = "stm32f423",
146-
feature = "stm32f427",
147-
feature = "stm32f429",
148-
feature = "stm32f437",
149-
feature = "stm32f439",
150-
feature = "stm32f446",
151-
feature = "stm32f469",
152-
feature = "stm32f479"
153-
))]
136+
#[cfg(feature = "tim2")]
154137
hal! {
155138
pac::TIM2: (tim2, wait_tim2),
156139
}

src/qei.rs

+19-24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{
33
hal::{self, Direction},
44
pac::RCC,
55
rcc,
6+
timer::General,
67
};
78

89
pub trait Pins<TIM> {}
@@ -48,7 +49,7 @@ impl<TIM: Instance, PINS> Qei<TIM, PINS> {
4849
}
4950

5051
impl<TIM: Instance, PINS> hal::Qei for Qei<TIM, PINS> {
51-
type Count = TIM::Count;
52+
type Count = TIM::Width;
5253

5354
fn count(&self) -> Self::Count {
5455
self.tim.read_count() as Self::Count
@@ -63,23 +64,22 @@ impl<TIM: Instance, PINS> hal::Qei for Qei<TIM, PINS> {
6364
}
6465
}
6566

66-
pub trait Instance: crate::Sealed + rcc::Enable + rcc::Reset {
67-
type Count;
68-
67+
pub trait Instance: crate::Sealed + rcc::Enable + rcc::Reset + General {
6968
fn setup_qei(&mut self);
70-
fn read_count(&self) -> Self::Count;
69+
7170
fn read_direction(&self) -> bool;
7271
}
7372

7473
macro_rules! hal {
75-
($($TIM:ty: ($bits:ident),)+) => {
74+
($($TIM:ty,)+) => {
7675
$(
7776
impl Instance for $TIM {
78-
type Count = $bits;
79-
8077
fn setup_qei(&mut self) {
8178
// Configure TxC1 and TxC2 as captures
82-
self.ccmr1_output()
79+
#[cfg(not(feature = "stm32f410"))]
80+
self.ccmr1_input().write(|w| w.cc1s().ti1().cc2s().ti2());
81+
#[cfg(feature = "stm32f410")]
82+
self.ccmr1_input()
8383
.write(|w| unsafe { w.cc1s().bits(0b01).cc2s().bits(0b01) });
8484
// enable and configure to capture on rising edge
8585
self.ccer.write(|w| {
@@ -92,19 +92,14 @@ macro_rules! hal {
9292
.cc2p()
9393
.clear_bit()
9494
});
95-
// configure as quadrature encoder
96-
// some chip variants declare `.bits()` as unsafe, some don't
97-
#[allow(unused_unsafe)]
95+
#[cfg(not(feature = "stm32f410"))]
96+
self.smcr.write(|w| w.sms().encoder_mode_3());
97+
#[cfg(feature = "stm32f410")]
9898
self.smcr.write(|w| unsafe { w.sms().bits(3) });
99-
#[allow(unused_unsafe)]
100-
self.arr.write(|w| unsafe { w.bits($bits::MAX as u32) });
99+
self.set_auto_reload(<$TIM as General>::Width::MAX as u32).unwrap();
101100
self.cr1.write(|w| w.cen().set_bit());
102101
}
103102

104-
fn read_count(&self) -> Self::Count {
105-
self.cnt.read().bits() as Self::Count
106-
}
107-
108103
fn read_direction(&self) -> bool {
109104
self.cr1.read().dir().bit_is_clear()
110105
}
@@ -114,18 +109,18 @@ macro_rules! hal {
114109
}
115110

116111
hal! {
117-
crate::pac::TIM1: (u16),
118-
crate::pac::TIM5: (u32),
112+
crate::pac::TIM1,
113+
crate::pac::TIM5,
119114
}
120115

121116
#[cfg(feature = "tim2")]
122117
hal! {
123-
crate::pac::TIM2: (u32),
124-
crate::pac::TIM3: (u16),
125-
crate::pac::TIM4: (u16),
118+
crate::pac::TIM2,
119+
crate::pac::TIM3,
120+
crate::pac::TIM4,
126121
}
127122

128123
#[cfg(feature = "tim8")]
129124
hal! {
130-
crate::pac::TIM8: (u16),
125+
crate::pac::TIM8,
131126
}

src/rcc/enable.rs

-8
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,8 @@ impl RccBus for crate::pac::FSMC {
132132
type Bus = AHB3;
133133
}
134134
#[cfg(feature = "fsmc")]
135-
#[cfg(any(feature = "stm32f427", feature = "stm32f437"))]
136135
bus_enable!(FSMC => 0);
137136
#[cfg(feature = "fsmc")]
138-
#[cfg(not(any(feature = "stm32f427", feature = "stm32f437")))]
139-
bus_enable!(FSMC => 0);
140-
#[cfg(feature = "fsmc")]
141-
#[cfg(any(feature = "stm32f427", feature = "stm32f437"))]
142-
bus_reset!(FSMC => 0);
143-
#[cfg(feature = "fsmc")]
144-
#[cfg(not(any(feature = "stm32f427", feature = "stm32f437")))]
145137
bus_reset!(FSMC => 0);
146138

147139
bus! {

src/timer.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl Instant {
190190

191191
mod sealed {
192192
pub trait General {
193-
type Width;
193+
type Width: Into<u32>;
194194
fn enable_counter(&mut self);
195195
fn disable_counter(&mut self);
196196
fn is_counter_enabled(&self) -> bool;
@@ -201,6 +201,8 @@ mod sealed {
201201
fn clear_update_interrupt_flag(&mut self);
202202
fn listen_update_interrupt(&mut self, b: bool);
203203
fn get_update_interrupt_flag(&self) -> bool;
204+
fn read_count(&self) -> Self::Width;
205+
fn read_auto_reload(&self) -> Self::Width;
204206
}
205207
}
206208
pub(crate) use sealed::General;
@@ -285,6 +287,14 @@ macro_rules! hal {
285287
fn get_update_interrupt_flag(&self) -> bool {
286288
self.sr.read().uif().bit_is_clear()
287289
}
290+
#[inline(always)]
291+
fn read_count(&self) -> Self::Width {
292+
self.cnt.read().bits() as Self::Width
293+
}
294+
#[inline(always)]
295+
fn read_auto_reload(&self) -> Self::Width {
296+
self.arr.read().bits() as Self::Width
297+
}
288298
}
289299
)+
290300
}

0 commit comments

Comments
 (0)