Skip to content

Commit 5d324ab

Browse files
bors[bot]burrbull
andauthored
Merge #472
472: explicit PINS order, alias defaults r=therealprof a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents 1c3bf79 + e05643c commit 5d324ab

File tree

7 files changed

+178
-173
lines changed

7 files changed

+178
-173
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12+
- Explicit order for PINS, more smart aliases for peripherals
1213
- Add `AFn` type aliases for `Alternate<n>`
1314
- CI updates + cache
1415
- Add missing `embedded-hal 1.0` for `DynamicPin`

src/can.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ mod can3 {
5858
}
5959

6060
pub trait CanExt: Sized + Instance {
61-
fn can<PINS: Pins<Self>>(self, pins: PINS) -> Can<Self, PINS>;
61+
fn can<TX, RX>(self, pins: (TX, RX)) -> Can<Self, (TX, RX)>
62+
where
63+
(TX, RX): Pins<Self>;
6264
fn tx<TX>(self, tx_pin: TX) -> Can<Self, (TX, NoPin)>
6365
where
6466
(TX, NoPin): Pins<Self>;
@@ -68,7 +70,10 @@ pub trait CanExt: Sized + Instance {
6870
}
6971

7072
impl<CAN: Instance> CanExt for CAN {
71-
fn can<PINS: Pins<Self>>(self, pins: PINS) -> Can<Self, PINS> {
73+
fn can<TX, RX>(self, pins: (TX, RX)) -> Can<Self, (TX, RX)>
74+
where
75+
(TX, RX): Pins<Self>,
76+
{
7277
Can::new(self, pins)
7378
}
7479
fn tx<TX>(self, tx_pin: TX) -> Can<Self, (TX, NoPin)>
@@ -91,13 +96,13 @@ pub struct Can<CAN, PINS> {
9196
pins: PINS,
9297
}
9398

94-
impl<CAN, PINS> Can<CAN, PINS>
99+
impl<CAN, TX, RX> Can<CAN, (TX, RX)>
95100
where
96101
CAN: Instance,
97-
PINS: Pins<CAN>,
102+
(TX, RX): Pins<CAN>,
98103
{
99104
/// Creates a CAN interface.
100-
pub fn new(can: CAN, mut pins: PINS) -> Self {
105+
pub fn new(can: CAN, mut pins: (TX, RX)) -> Self {
101106
unsafe {
102107
// NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
103108
let rcc = &(*crate::pac::RCC::ptr());
@@ -110,10 +115,10 @@ where
110115
Can { can, pins }
111116
}
112117

113-
pub fn release(mut self) -> (CAN, PINS) {
118+
pub fn release(mut self) -> (CAN, (TX, RX)) {
114119
self.pins.restore_mode();
115120

116-
(self.can, self.pins)
121+
(self.can, (self.pins.0, self.pins.1))
117122
}
118123
}
119124

src/i2c.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,31 +140,36 @@ impl Instance for pac::I2C3 {}
140140
pub type I2c3<PINS> = I2c<pac::I2C3, PINS>;
141141

142142
pub trait I2cExt: Sized + Instance {
143-
fn i2c<PINS: Pins<Self>>(
143+
fn i2c<SCL, SDA>(
144144
self,
145-
pins: PINS,
145+
pins: (SCL, SDA),
146146
mode: impl Into<Mode>,
147147
clocks: &Clocks,
148-
) -> I2c<Self, PINS>;
148+
) -> I2c<Self, (SCL, SDA)>
149+
where
150+
(SCL, SDA): Pins<Self>;
149151
}
150152

151153
impl<I2C: Instance> I2cExt for I2C {
152-
fn i2c<PINS: Pins<Self>>(
154+
fn i2c<SCL, SDA>(
153155
self,
154-
pins: PINS,
156+
pins: (SCL, SDA),
155157
mode: impl Into<Mode>,
156158
clocks: &Clocks,
157-
) -> I2c<Self, PINS> {
159+
) -> I2c<Self, (SCL, SDA)>
160+
where
161+
(SCL, SDA): Pins<Self>,
162+
{
158163
I2c::new(self, pins, mode, clocks)
159164
}
160165
}
161166

162-
impl<I2C, PINS> I2c<I2C, PINS>
167+
impl<I2C, SCL, SDA> I2c<I2C, (SCL, SDA)>
163168
where
164169
I2C: Instance,
165-
PINS: Pins<I2C>,
170+
(SCL, SDA): Pins<I2C>,
166171
{
167-
pub fn new(i2c: I2C, mut pins: PINS, mode: impl Into<Mode>, clocks: &Clocks) -> Self {
172+
pub fn new(i2c: I2C, mut pins: (SCL, SDA), mode: impl Into<Mode>, clocks: &Clocks) -> Self {
168173
unsafe {
169174
// NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
170175
let rcc = &(*RCC::ptr());
@@ -181,10 +186,10 @@ where
181186
i2c
182187
}
183188

184-
pub fn release(mut self) -> (I2C, PINS) {
189+
pub fn release(mut self) -> (I2C, (SCL, SDA)) {
185190
self.pins.restore_mode();
186191

187-
(self.i2c, self.pins)
192+
(self.i2c, (self.pins.0, self.pins.1))
188193
}
189194
}
190195

src/i2s.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,32 +103,45 @@ macro_rules! i2s {
103103
}
104104

105105
pub trait I2sExt: Sized + Instance {
106-
fn i2s<PINS: Pins<Self>>(self, pins: PINS, clocks: &Clocks) -> I2s<Self, PINS>;
106+
fn i2s<WS, CK, MCLK, SD>(
107+
self,
108+
pins: (WS, CK, MCLK, SD),
109+
clocks: &Clocks,
110+
) -> I2s<Self, (WS, CK, MCLK, SD)>
111+
where
112+
(WS, CK, MCLK, SD): Pins<Self>;
107113
}
108114

109115
impl<SPI: Instance> I2sExt for SPI {
110-
fn i2s<PINS: Pins<Self>>(self, pins: PINS, clocks: &Clocks) -> I2s<Self, PINS> {
116+
fn i2s<WS, CK, MCLK, SD>(
117+
self,
118+
pins: (WS, CK, MCLK, SD),
119+
clocks: &Clocks,
120+
) -> I2s<Self, (WS, CK, MCLK, SD)>
121+
where
122+
(WS, CK, MCLK, SD): Pins<Self>,
123+
{
111124
I2s::new(self, pins, clocks)
112125
}
113126
}
114127

115-
impl<SPI, PINS> I2s<SPI, PINS>
128+
impl<SPI, WS, CK, MCLK, SD> I2s<SPI, (WS, CK, MCLK, SD)>
116129
where
117130
SPI: Instance,
118-
PINS: Pins<SPI>,
131+
(WS, CK, MCLK, SD): Pins<SPI>,
119132
{
120133
/// Creates an I2s object around an SPI peripheral and pins
121134
///
122135
/// This function enables and resets the SPI peripheral, but does not configure it.
123136
///
124-
/// The returned I2s object implements [stm32_i2s_v12x::Instance], so it can be used
137+
/// The returned I2s object implements `stm32_i2s_v12x::Instance`, so it can be used
125138
/// to configure the peripheral and communicate.
126139
///
127140
/// # Panics
128141
///
129142
/// This function panics if the I2S clock input (from the I2S PLL or similar)
130143
/// is not configured.
131-
pub fn new(spi: SPI, mut pins: PINS, clocks: &Clocks) -> Self {
144+
pub fn new(spi: SPI, mut pins: (WS, CK, MCLK, SD), clocks: &Clocks) -> Self {
132145
let input_clock = SPI::i2s_freq(clocks);
133146
unsafe {
134147
// NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
@@ -147,10 +160,13 @@ where
147160
}
148161
}
149162

150-
pub fn release(mut self) -> (SPI, PINS) {
163+
pub fn release(mut self) -> (SPI, (WS, CK, MCLK, SD)) {
151164
self.pins.restore_mode();
152165

153-
(self.spi, self.pins)
166+
(
167+
self.spi,
168+
(self.pins.0, self.pins.1, self.pins.2, self.pins.3),
169+
)
154170
}
155171
}
156172

src/qei.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ use crate::{pac::RCC, rcc, timer::General};
44
pub trait Pins<TIM> {}
55
use crate::timer::CPin;
66

7-
pub trait QeiExt<PINS>: Sized {
8-
fn qei(self, pins: PINS) -> Qei<Self, PINS>;
7+
pub trait QeiExt: Sized {
8+
fn qei<PC1, PC2>(self, pins: (PC1, PC2)) -> Qei<Self, (PC1, PC2)>
9+
where
10+
(PC1, PC2): Pins<Self>;
911
}
1012

11-
impl<TIM, PINS> QeiExt<PINS> for TIM
12-
where
13-
TIM: Instance,
14-
PINS: Pins<TIM>,
15-
{
16-
fn qei(self, pins: PINS) -> Qei<Self, PINS> {
13+
impl<TIM: Instance> QeiExt for TIM {
14+
fn qei<PC1, PC2>(self, pins: (PC1, PC2)) -> Qei<Self, (PC1, PC2)>
15+
where
16+
(PC1, PC2): Pins<Self>,
17+
{
1718
Qei::new(self, pins)
1819
}
1920
}
@@ -31,12 +32,12 @@ pub struct Qei<TIM, PINS> {
3132
pins: PINS,
3233
}
3334

34-
impl<TIM: Instance, PINS> Qei<TIM, PINS>
35+
impl<TIM: Instance, PC1, PC2> Qei<TIM, (PC1, PC2)>
3536
where
36-
PINS: Pins<TIM>,
37+
(PC1, PC2): Pins<TIM>,
3738
{
3839
/// Configures a TIM peripheral as a quadrature encoder interface input
39-
pub fn new(mut tim: TIM, pins: PINS) -> Self {
40+
pub fn new(mut tim: TIM, pins: (PC1, PC2)) -> Self {
4041
// NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
4142
let rcc = unsafe { &(*RCC::ptr()) };
4243
// Enable and reset clock.
@@ -47,12 +48,10 @@ where
4748

4849
Qei { tim, pins }
4950
}
50-
}
5151

52-
impl<TIM: Instance, PINS> Qei<TIM, PINS> {
5352
/// Releases the TIM peripheral and QEI pins
54-
pub fn release(self) -> (TIM, PINS) {
55-
(self.tim, self.pins)
53+
pub fn release(self) -> (TIM, (PC1, PC2)) {
54+
(self.tim, (self.pins.0, self.pins.1))
5655
}
5756
}
5857

0 commit comments

Comments
 (0)