Skip to content

Commit 5fc8b99

Browse files
authored
Merge pull request #520 from stm32-rs/rmp-new
I2c::new remap
2 parents f62c083 + d9f7eab commit 5fc8b99

File tree

8 files changed

+137
-199
lines changed

8 files changed

+137
-199
lines changed

.zed/settings.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"lsp": {
3+
"rust-analyzer": {
4+
"initialization_options": {
5+
"cargo": {
6+
"features": ["defmt", "rtic", "stm32f103", "medium"]
7+
},
8+
"check": {
9+
"allTargets": false,
10+
"targets": "thumbv7em-none-eabi"
11+
}
12+
}
13+
}
14+
}
15+
}

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212
- Relax pin type generics for `Serial`, `I2c`, `Spi`, `Can`, `Qei`, `PwmInput`. [#462] [#516]
1313
~~Use enums of pin tuples and `Enum::from<(tuple)>` for pin remap before passing to peripheral.~~
1414
Use pin enums and `impl RInto<(enum), R>` for peripheral constructors.
15-
Add `RInto` trait and `Rmp` peripheral wrapper, add `remap` for peripherals. [#514]
15+
Add `RInto` trait and `Rmp` peripheral wrapper, add `remap` for peripherals. [#514] [#520]
1616
Remove `RemapStruct`s. [#462] [#506] [#509]
1717
- Use independent `Spi` and `SpiSlave` structures instead of `OP` generic [#462]
1818
- Take `&Clocks` instead of `Clocks` [#498]
@@ -33,7 +33,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3333
- Unmacro `dma.rs` [#505]
3434
- Updated `usb-device` and `usbd-serial` to latest versions [#510]
3535
- Rework pin remaps, fix CAN1 remap [#511]
36-
- Rework USART remap,
36+
- Rework USART remap,
3737

3838
### Added
3939

@@ -68,6 +68,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
6868
[#511]: https://github.com/stm32-rs/stm32f1xx-hal/pull/511
6969
[#514]: https://github.com/stm32-rs/stm32f1xx-hal/pull/514
7070
[#516]: https://github.com/stm32-rs/stm32f1xx-hal/pull/516
71+
[#520]: https://github.com/stm32-rs/stm32f1xx-hal/pull/520
7172

7273
## [v0.10.0] - 2022-12-12
7374

src/afio.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ remap! {
240240

241241
pub struct Rmp<T, const R: u8>(pub(crate) T);
242242

243+
impl<T> From<T> for Rmp<T, 0> {
244+
fn from(value: T) -> Self {
245+
Self(value)
246+
}
247+
}
248+
243249
pub trait RFrom<T, const R: u8> {
244250
fn rfrom(value: T) -> Self;
245251
}

src/can.rs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,14 @@ pub struct Can<CAN: Instance, PULL = Floating> {
7272
impl<CAN: Instance, const R: u8> Rmp<CAN, R> {
7373
pub fn can<PULL: UpMode>(
7474
self,
75-
#[cfg(not(feature = "connectivity"))] usb: pac::USB,
75+
#[cfg(not(feature = "connectivity"))] _usb: pac::USB,
7676
pins: (impl RInto<CAN::Tx, R>, impl RInto<CAN::Rx<PULL>, R>),
7777
) -> Can<CAN, PULL> {
78-
Can::_new(
79-
self.0,
80-
#[cfg(not(feature = "connectivity"))]
81-
usb,
82-
(Some(pins.0), Some(pins.1)),
83-
)
78+
let rcc = unsafe { &(*RCC::ptr()) };
79+
CAN::enable(rcc);
80+
81+
let pins = (Some(pins.0.rinto()), Some(pins.1.rinto()));
82+
Can { can: self.0, pins }
8483
}
8584
pub fn can_loopback(
8685
self,
@@ -99,34 +98,18 @@ impl<CAN: Instance, PULL: UpMode> Can<CAN, PULL> {
9998
///
10099
/// CAN shares SRAM with the USB peripheral. Take ownership of USB to
101100
/// prevent accidental shared usage.
102-
pub fn new(
103-
can: CAN,
101+
pub fn new<const R: u8>(
102+
can: impl Into<Rmp<CAN, R>>,
104103
#[cfg(not(feature = "connectivity"))] _usb: pac::USB,
105-
pins: (impl RInto<CAN::Tx, 0>, impl RInto<CAN::Rx<PULL>, 0>),
104+
pins: (impl RInto<CAN::Tx, R>, impl RInto<CAN::Rx<PULL>, R>),
106105
) -> Can<CAN, PULL> {
107-
Self::_new(
108-
can,
106+
can.into().can(
109107
#[cfg(not(feature = "connectivity"))]
110108
_usb,
111-
(Some(pins.0), Some(pins.1)),
109+
pins,
112110
)
113111
}
114112

115-
fn _new<const R: u8>(
116-
can: CAN,
117-
#[cfg(not(feature = "connectivity"))] _usb: pac::USB,
118-
pins: (
119-
Option<impl RInto<CAN::Tx, R>>,
120-
Option<impl RInto<CAN::Rx<PULL>, R>>,
121-
),
122-
) -> Can<CAN, PULL> {
123-
let rcc = unsafe { &(*RCC::ptr()) };
124-
CAN::enable(rcc);
125-
126-
let pins = (pins.0.map(RInto::rinto), pins.1.map(RInto::rinto));
127-
Can { can, pins }
128-
}
129-
130113
/// Creates a CAN interface in loopback mode
131114
pub fn new_loopback(
132115
can: CAN,

src/i2c.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -164,29 +164,13 @@ impl Instance for pac::I2C2 {}
164164

165165
impl<I2C: Instance> I2c<I2C> {
166166
/// Creates a generic I2C object
167-
pub fn new(
168-
i2c: I2C,
169-
pins: (impl RInto<I2C::Scl, 0>, impl RInto<I2C::Sda, 0>),
167+
pub fn new<const R: u8>(
168+
i2c: impl Into<Rmp<I2C, R>>,
169+
pins: (impl RInto<I2C::Scl, R>, impl RInto<I2C::Sda, R>),
170170
mode: impl Into<Mode>,
171171
clocks: &Clocks,
172172
) -> Self {
173-
let mode = mode.into();
174-
let rcc = unsafe { &(*RCC::ptr()) };
175-
I2C::enable(rcc);
176-
I2C::reset(rcc);
177-
178-
let pclk1 = I2C::clock(clocks);
179-
180-
assert!(mode.get_frequency() <= kHz(400));
181-
182-
let mut i2c = I2c {
183-
i2c,
184-
pins: (pins.0.rinto(), pins.1.rinto()),
185-
mode,
186-
pclk1,
187-
};
188-
i2c.init();
189-
i2c
173+
i2c.into().i2c(pins, mode, clocks)
190174
}
191175
}
192176

src/i2c/blocking.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ pub struct DwtTimeouts {
2020
impl<I2C: Instance> BlockingI2c<I2C> {
2121
/// Creates a blocking I2C1 object on pins PB6 and PB7 or PB8 and PB9 using the embedded-hal `BlockingI2c` trait.
2222
#[allow(clippy::too_many_arguments)]
23-
pub fn new(
24-
i2c: I2C,
25-
pins: (impl RInto<I2C::Scl, 0>, impl RInto<I2C::Sda, 0>),
23+
pub fn new<const R: u8>(
24+
i2c: impl Into<Rmp<I2C, R>>,
25+
pins: (impl RInto<I2C::Scl, R>, impl RInto<I2C::Sda, R>),
2626
mode: impl Into<Mode>,
2727
clocks: &Clocks,
2828
start_timeout_us: u32,

src/serial.rs

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -147,29 +147,15 @@ impl<USART: Instance> SerialExt for USART {
147147
config: impl Into<Config>,
148148
clocks: &Clocks,
149149
) -> Tx<Self> {
150-
Serial::_new(
151-
self,
152-
(Some(tx_pin), None::<USART::Rx<Floating>>),
153-
config,
154-
clocks,
155-
)
156-
.split()
157-
.0
150+
Serial::tx(self, tx_pin, config, clocks)
158151
}
159152
fn rx<PULL: UpMode>(
160153
self,
161154
rx_pin: impl RInto<Self::Rx<PULL>, 0>,
162155
config: impl Into<Config>,
163156
clocks: &Clocks,
164157
) -> Rx<Self> {
165-
Serial::_new(
166-
self,
167-
(None::<USART::Tx<PushPull>>, Some(rx_pin)),
168-
config,
169-
clocks,
170-
)
171-
.split()
172-
.1
158+
Serial::rx(self, rx_pin, config, clocks)
173159
}
174160
}
175161

@@ -388,38 +374,24 @@ impl<USART: Instance, const R: u8> Rmp<USART, R> {
388374
}
389375

390376
impl<USART: Instance, Otype> Serial<USART, Otype, Floating> {
391-
pub fn tx(
392-
usart: USART,
393-
tx_pin: impl RInto<USART::Tx<Otype>, 0>,
377+
pub fn tx<const R: u8>(
378+
usart: impl Into<Rmp<USART, R>>,
379+
tx_pin: impl RInto<USART::Tx<Otype>, R>,
394380
config: impl Into<Config>,
395381
clocks: &Clocks,
396382
) -> Tx<USART> {
397-
Self::_new(
398-
usart,
399-
(Some(tx_pin), None::<USART::Rx<Floating>>),
400-
config,
401-
clocks,
402-
)
403-
.split()
404-
.0
383+
usart.into().tx(tx_pin, config, clocks)
405384
}
406385
}
407386

408387
impl<USART: Instance, PULL: UpMode> Serial<USART, PushPull, PULL> {
409-
pub fn rx(
410-
usart: USART,
411-
rx_pin: impl RInto<USART::Rx<PULL>, 0>,
388+
pub fn rx<const R: u8>(
389+
usart: impl Into<Rmp<USART, R>>,
390+
rx_pin: impl RInto<USART::Rx<PULL>, R>,
412391
config: impl Into<Config>,
413392
clocks: &Clocks,
414393
) -> Rx<USART> {
415-
Self::_new(
416-
usart,
417-
(None::<USART::Tx<PushPull>>, Some(rx_pin)),
418-
config,
419-
clocks,
420-
)
421-
.split()
422-
.1
394+
usart.into().rx(rx_pin, config, clocks)
423395
}
424396
}
425397

@@ -439,16 +411,16 @@ impl<USART: Instance, Otype, PULL: UpMode> Serial<USART, Otype, PULL> {
439411
/// `MAPR` and `APBX` are register handles which are passed for
440412
/// configuration. (`MAPR` is used to map the USART to the
441413
/// corresponding pins. `APBX` is used to reset the USART.)
442-
pub fn new(
443-
usart: USART,
414+
pub fn new<const R: u8>(
415+
usart: impl Into<Rmp<USART, R>>,
444416
pins: (
445-
impl RInto<USART::Tx<Otype>, 0>,
446-
impl RInto<USART::Rx<PULL>, 0>,
417+
impl RInto<USART::Tx<Otype>, R>,
418+
impl RInto<USART::Rx<PULL>, R>,
447419
),
448420
config: impl Into<Config>,
449421
clocks: &Clocks,
450422
) -> Self {
451-
Self::_new(usart, (Some(pins.0), Some(pins.1)), config, clocks)
423+
usart.into().serial(pins, config, clocks)
452424
}
453425

454426
fn _new<const R: u8>(

0 commit comments

Comments
 (0)