Skip to content

Commit fe94f6c

Browse files
authored
Merge pull request #541 from nrf-rs/qdec-pins
Make quadrature decoder input pins generic
2 parents e39fe1e + 82a9c8a commit fe94f6c

25 files changed

Lines changed: 87 additions & 48 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
(no changes)
66

7+
## [0.20.0]
8+
9+
### Breaking changes
10+
11+
- Made pin modes for `qdec` generic
12+
713
## [0.19.0]
814

915
### Breaking changes
@@ -445,3 +451,4 @@ None
445451
[0.17.1]: https://github.com/nrf-rs/nrf-hal/releases/tag/v0.17.1
446452
[0.18.0]: https://github.com/nrf-rs/nrf-hal/releases/tag/v0.18.0
447453
[0.19.0]: https://github.com/nrf-rs/nrf-hal/releases/tag/v0.19.0
454+
[0.20.0]: https://github.com/nrf-rs/nrf-hal/releases/tag/v0.20.0

examples/qdec-demo/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use {core::panic::PanicInfo, nrf52840_hal as hal, rtt_target::rprintln};
66
#[rtic::app(device = crate::hal::pac, peripherals = true)]
77
mod app {
88
use {
9-
hal::qdec::*,
9+
hal::{gpio::PullUp, qdec::*},
1010
nrf52840_hal as hal,
1111
rtt_target::{rprintln, rtt_init_print},
1212
};
@@ -16,7 +16,7 @@ mod app {
1616

1717
#[local]
1818
struct Local {
19-
qdec: Qdec,
19+
qdec: Qdec<PullUp>,
2020
value: i16,
2121
}
2222

nrf-hal-common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "nrf-hal-common"
3-
version = "0.19.0"
3+
version = "0.20.0"
44
description = "Implementation details of the nRF HAL crates. Don't use this directly, use one of the specific HAL crates instead (`nrfXYZ-hal`)."
55
readme = "../README.md"
66

nrf-hal-common/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Implementation details of the nRF HAL crates. Don't use this directly, use one of the specific
22
//! HAL crates instead (`nrfXYZ-hal`).
33
4-
#![doc(html_root_url = "https://docs.rs/nrf-hal-common/0.19.0")]
4+
#![doc(html_root_url = "https://docs.rs/nrf-hal-common/0.20.0")]
55
#![no_std]
66

77
#[cfg(feature = "rtic-monotonic")]

nrf-hal-common/src/qdec.rs

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44
//! It is suitable for mechanical and optical sensors.
55
66
use {
7-
crate::gpio::{Input, Pin, PullUp},
7+
crate::gpio::{Input, Pin},
88
crate::pac::QDEC,
9+
core::marker::PhantomData,
910
};
1011

11-
/// A safe wrapper around the `QDEC` peripheral with associated pins.
12-
pub struct Qdec {
12+
/// A safe wrapper around the `QDEC` peripheral with
13+
/// associated pins. The `MODE` here reflects the pin mode
14+
/// of all of the pins when *not* in use by the `QDEC`: that
15+
/// is, at `new()`-time and `free()`-time.
16+
pub struct Qdec<MODE> {
1317
qdec: QDEC,
18+
_i: PhantomData<MODE>,
1419
}
1520

16-
impl Qdec {
17-
/// Takes ownership of the `QDEC` peripheral and associated pins, returning a safe wrapper.
18-
pub fn new(qdec: QDEC, pins: Pins, sample_period: SamplePeriod) -> Self {
21+
impl<MODE> Qdec<MODE> {
22+
/// Takes ownership of the `QDEC` peripheral and
23+
/// associated pins, returning a safe wrapper.
24+
/// All pins must be in the given input `MODE`.
25+
pub fn new(qdec: QDEC, pins: Pins<MODE>, sample_period: SamplePeriod) -> Self {
1926
qdec.psel.a.write(|w| {
2027
unsafe { w.bits(pins.a.psel_bits()) };
2128
w.connect().connected()
@@ -46,7 +53,10 @@ impl Qdec {
4653
SamplePeriod::_131ms => qdec.sampleper.write(|w| w.sampleper()._131ms()),
4754
}
4855

49-
Self { qdec }
56+
Self {
57+
qdec,
58+
_i: PhantomData,
59+
}
5060
}
5161

5262
/// Enables/disables input debounce filters.
@@ -131,9 +141,12 @@ impl Qdec {
131141
self.qdec.accread.read().bits() as i16
132142
}
133143

134-
/// Consumes `self` and returns back the raw `QDEC` peripheral.
144+
/// Consumes `self` and returns back the raw `QDEC`
145+
/// peripheral and its pins. The pins are returned in
146+
/// the input `MODE` they were in at the time of
147+
/// `QDec::new()`.
135148
#[inline]
136-
pub fn free(self) -> (QDEC, Pins) {
149+
pub fn free(self) -> (QDEC, Pins<MODE>) {
137150
let a = unsafe { Pin::from_psel_bits(self.qdec.psel.a.read().bits()) };
138151
let b = unsafe { Pin::from_psel_bits(self.qdec.psel.b.read().bits()) };
139152
let led = {
@@ -152,13 +165,29 @@ impl Qdec {
152165
}
153166
}
154167

155-
/// Pins for the QDEC
156-
pub struct Pins {
157-
pub a: Pin<Input<PullUp>>,
158-
pub b: Pin<Input<PullUp>>,
159-
pub led: Option<Pin<Input<PullUp>>>,
168+
/// Pins for the QDEC. The `led` pin must be given to the
169+
/// nRF configured as an input, but starting the QDEC will
170+
/// transform it into an output. The generic input `MODE`
171+
/// should be either `PullUp`, `PullDown` or `Floating`
172+
/// depending on circuit requirements.
173+
///
174+
/// **Note:** The requirement that all pins be in the same
175+
/// input `MODE` at startup is not strictly enforced by the
176+
/// hardware, which cares only whether these pins are
177+
/// inputs. It is instead a convenience to avoid carrying
178+
/// around three type parameters, two of which would almost
179+
/// certainly be redundant (A and B modes) and one of which
180+
/// is often unnecessary (LED mode).
181+
pub struct Pins<MODE> {
182+
pub a: Pin<Input<MODE>>,
183+
pub b: Pin<Input<MODE>>,
184+
pub led: Option<Pin<Input<MODE>>>,
160185
}
161186

187+
/// Period with which to sample the encoder. Note that
188+
/// periods named in "`ms`" are approximations to exact
189+
/// periods in µs. When debounce is enabled, the inputs will
190+
/// be ignored unless they stay stable for this period.
162191
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
163192
pub enum SamplePeriod {
164193
_128us,
@@ -174,6 +203,8 @@ pub enum SamplePeriod {
174203
_131ms,
175204
}
176205

206+
/// Number of samples before a decoder interrupt is
207+
/// triggered.
177208
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
178209
pub enum NumSamples {
179210
_10smpl,
@@ -187,6 +218,7 @@ pub enum NumSamples {
187218
_1smpl,
188219
}
189220

221+
/// Accomodation for high-side LED setups.
190222
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
191223
pub enum LedPolarity {
192224
ActiveHigh,

nrf51-hal/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "nrf51-hal"
3-
version = "0.19.0"
3+
version = "0.20.0"
44
edition = "2018"
55
description = "HAL for nRF51 microcontrollers"
66
readme = "../README.md"
@@ -25,7 +25,7 @@ nrf51-pac = "0.12.2"
2525
path = "../nrf-hal-common"
2626
default-features = false
2727
features = ["51"]
28-
version = "=0.19.0"
28+
version = "=0.20.0"
2929

3030
[features]
3131
doc = []

nrf51-hal/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![no_std]
2-
#![doc(html_root_url = "https://docs.rs/nrf51-hal/0.19.0")]
2+
#![doc(html_root_url = "https://docs.rs/nrf51-hal/0.20.0")]
33

44
pub use nrf_hal_common::*;
55

nrf52805-hal/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "nrf52805-hal"
3-
version = "0.19.0"
3+
version = "0.20.0"
44
edition = "2018"
55
description = "HAL for nRF52805 microcontrollers"
66
readme = "../README.md"
@@ -24,7 +24,7 @@ nrf52805-pac = "0.12.2"
2424
path = "../nrf-hal-common"
2525
default-features = false
2626
features = ["52805"]
27-
version = "=0.19.0"
27+
version = "=0.20.0"
2828

2929
[features]
3030
doc = []

nrf52805-hal/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![no_std]
2-
#![doc(html_root_url = "https://docs.rs/nrf52805-hal/0.19.0")]
2+
#![doc(html_root_url = "https://docs.rs/nrf52805-hal/0.20.0")]
33

44
pub use nrf_hal_common::*;
55

nrf52810-hal/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "nrf52810-hal"
3-
version = "0.19.0"
3+
version = "0.20.0"
44
edition = "2018"
55
description = "HAL for nRF52810 microcontrollers"
66
readme = "../README.md"
@@ -24,7 +24,7 @@ nrf52810-pac = "0.12.2"
2424
path = "../nrf-hal-common"
2525
default-features = false
2626
features = ["52810"]
27-
version = "=0.19.0"
27+
version = "=0.20.0"
2828

2929
[features]
3030
doc = []

0 commit comments

Comments
 (0)