Skip to content

Commit 51321ac

Browse files
authored
Merge pull request #511 from stm32-rs/remap
Remap trait
2 parents c207345 + d327eec commit 51321ac

File tree

6 files changed

+209
-127
lines changed

6 files changed

+209
-127
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2626
- Check "device selected" in `build.rs` [#502]
2727
- Use gpio field enums internally [#506]
2828
- Unmacro `dma.rs` [#505]
29-
- Rework USART remap,
29+
- Rework pin remaps, fix CAN1 remap [#511]
3030

3131
### Added
3232

@@ -57,6 +57,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
5757
[#505]: https://github.com/stm32-rs/stm32f1xx-hal/pull/505
5858
[#506]: https://github.com/stm32-rs/stm32f1xx-hal/pull/506
5959
[#509]: https://github.com/stm32-rs/stm32f1xx-hal/pull/509
60+
[#511]: https://github.com/stm32-rs/stm32f1xx-hal/pull/511
6061

6162
## [v0.10.0] - 2022-12-12
6263

src/afio.rs

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//! # Alternate Function I/Os
2-
use crate::pac::{afio, AFIO, RCC};
2+
use crate::pac::{self, afio, AFIO, RCC};
33

44
use crate::rcc::{Enable, Reset};
55

66
use crate::gpio::{
77
Debugger, Floating, Input, PA15, {PB3, PB4},
88
};
9+
use crate::sealed::Sealed;
910

1011
pub trait AfioExt {
1112
fn constrain(self) -> Parts;
@@ -150,4 +151,88 @@ impl MAPR2 {
150151
pub fn mapr2(&mut self) -> &afio::MAPR2 {
151152
unsafe { (*AFIO::ptr()).mapr2() }
152153
}
154+
155+
pub fn modify_mapr<F>(&mut self, mod_fn: F)
156+
where
157+
F: for<'w> FnOnce(&afio::mapr2::R, &'w mut afio::mapr2::W) -> &'w mut afio::mapr2::W,
158+
{
159+
self.mapr2().modify(|r, w| mod_fn(r, w));
160+
}
161+
}
162+
163+
pub trait Remap: Sealed {
164+
type Mapr;
165+
fn remap(mapr: &mut Self::Mapr, to: u8);
166+
}
167+
168+
macro_rules! remap {
169+
($(
170+
$PER:ty: $MAPR:ident, $w:ident: $field:ident;
171+
)+) => {
172+
$(
173+
remap!($PER: $MAPR, $w: $field);
174+
)+
175+
};
176+
($PER:ty: $MAPR:ident, bool: $field:ident) => {
177+
impl Remap for $PER {
178+
type Mapr = $MAPR;
179+
fn remap(mapr: &mut Self::Mapr, to: u8) {
180+
mapr.modify_mapr(|_, w| w.$field().bit(to != 0));
181+
}
182+
}
183+
};
184+
($PER:ty: $MAPR:ident, u8: $field:ident) => {
185+
impl Remap for $PER {
186+
type Mapr = $MAPR;
187+
fn remap(mapr: &mut Self::Mapr, to: u8) {
188+
mapr.modify_mapr(|_, w| unsafe { w.$field().bits(to) });
189+
}
190+
}
191+
};
192+
}
193+
use remap;
194+
195+
remap! {
196+
pac::SPI1: MAPR, bool: spi1_remap;
197+
pac::I2C1: MAPR, bool: i2c1_remap;
198+
pac::USART1: MAPR, bool: usart1_remap;
199+
pac::USART2: MAPR, bool: usart2_remap;
200+
pac::USART3: MAPR, u8: usart3_remap;
201+
pac::TIM2: MAPR, u8: tim2_remap;
202+
pac::TIM3: MAPR, u8: tim3_remap;
203+
}
204+
205+
#[cfg(feature = "medium")]
206+
remap! {
207+
pac::TIM4: MAPR, bool: tim4_remap;
208+
}
209+
210+
#[cfg(any(feature = "stm32f100", feature = "stm32f103", feature = "connectivity"))]
211+
remap! {
212+
pac::TIM1: MAPR, u8: tim1_remap;
213+
}
214+
215+
#[cfg(feature = "stm32f103")]
216+
remap! {
217+
pac::CAN1: MAPR, u8: can_remap;
218+
}
219+
220+
#[cfg(feature = "connectivity")]
221+
remap! {
222+
pac::CAN1: MAPR, u8: can1_remap;
223+
//pac::ETHERNET_MAC: MAPR, bool: eth_remap;
224+
pac::CAN2: MAPR, bool: can2_remap;
225+
pac::SPI3: MAPR, bool: spi3_remap;
226+
}
227+
228+
#[cfg(feature = "xl")]
229+
remap! {
230+
pac::TIM9: MAPR2, bool: tim9_remap;
231+
pac::TIM10: MAPR2, bool: tim10_remap;
232+
pac::TIM11: MAPR2, bool: tim11_remap;
233+
}
234+
#[cfg(any(feature = "xl", all(feature = "stm32f100", feature = "high")))]
235+
remap! {
236+
pac::TIM13: MAPR2, bool: tim13_remap;
237+
pac::TIM14: MAPR2, bool: tim14_remap;
153238
}

src/can.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! | TX | PB6 | PB13 |
2020
//! | RX | PB5 | PB12 |
2121
22-
use crate::afio::MAPR;
22+
use crate::afio::Remap;
2323
use crate::gpio::{self, Alternate, Cr, Floating, Input, NoPin, PinMode, PullUp, PushPull};
2424
use crate::pac::{self, RCC};
2525

@@ -45,14 +45,10 @@ pub mod can1 {
4545
use super::*;
4646

4747
remap! {
48-
#[cfg(not(feature = "connectivity"))]
49-
PA12, PA11 => { |_, w| unsafe { w.can_remap().bits(0) } };
50-
#[cfg(feature = "connectivity")]
51-
PA12, PA11 => { |_, w| unsafe { w.can1_remap().bits(0) } };
52-
#[cfg(not(feature = "connectivity"))]
53-
PB9, PB8 => { |_, w| unsafe { w.can_remap().bits(10) } };
54-
#[cfg(feature = "connectivity")]
55-
PB9, PB8 => { |_, w| unsafe { w.can1_remap().bits(10) } };
48+
pac::CAN1: [
49+
PA12, PA11 => 0;
50+
PB9, PB8 => 2;
51+
]
5652
}
5753
}
5854

@@ -61,70 +57,66 @@ pub mod can2 {
6157
use super::*;
6258

6359
remap! {
64-
PB6, PB5 => { |_, w| w.can2_remap().bit(false) };
65-
PB13, PB12 => { |_, w| w.can2_remap().bit(true) };
60+
pac::CAN2: [
61+
PB6, PB5 => 0;
62+
PB13, PB12 => 1;
63+
]
6664
}
6765
}
6866

6967
macro_rules! remap {
70-
($($(#[$attr:meta])* $TX:ident, $RX:ident => { $remapex:expr };)+) => {
68+
($PER:ty: [$($TX:ident, $RX:ident => $remap:literal;)+]) => {
7169
pub enum Tx {
7270
$(
73-
$(#[$attr])*
7471
$TX(gpio::$TX<Alternate>),
7572
)+
7673
None(NoPin<PushPull>),
7774
}
7875
pub enum Rx<PULL> {
7976
$(
80-
$(#[$attr])*
8177
$RX(gpio::$RX<Input<PULL>>),
8278
)+
8379
None(NoPin<PULL>),
8480
}
8581

8682
$(
87-
$(#[$attr])*
88-
impl<PULL: InMode> From<(gpio::$TX<Alternate>, gpio::$RX<Input<PULL>>, &mut MAPR)> for Pins<Tx, Rx<PULL>> {
89-
fn from(p: (gpio::$TX<Alternate>, gpio::$RX<Input<PULL>>, &mut MAPR)) -> Self {
90-
p.2.modify_mapr($remapex);
83+
impl<PULL: InMode> From<(gpio::$TX<Alternate>, gpio::$RX<Input<PULL>>, &mut <$PER as Remap>::Mapr)> for Pins<Tx, Rx<PULL>> {
84+
fn from(p: (gpio::$TX<Alternate>, gpio::$RX<Input<PULL>>, &mut <$PER as Remap>::Mapr)) -> Self {
85+
<$PER>::remap(p.2, $remap);
9186
Self { tx: Tx::$TX(p.0), rx: Rx::$RX(p.1) }
9287
}
9388
}
9489

95-
$(#[$attr])*
96-
impl<PULL> From<(gpio::$TX, gpio::$RX, &mut MAPR)> for Pins<Tx, Rx<PULL>>
90+
impl<PULL> From<(gpio::$TX, gpio::$RX, &mut <$PER as Remap>::Mapr)> for Pins<Tx, Rx<PULL>>
9791
where
9892
Input<PULL>: PinMode,
9993
PULL: InMode,
10094
{
101-
fn from(p: (gpio::$TX, gpio::$RX, &mut MAPR)) -> Self {
95+
fn from(p: (gpio::$TX, gpio::$RX, &mut <$PER as Remap>::Mapr)) -> Self {
10296
let mut cr = Cr;
10397
let tx = p.0.into_mode(&mut cr);
10498
let rx = p.1.into_mode(&mut cr);
105-
p.2.modify_mapr($remapex);
99+
<$PER>::remap(p.2, $remap);
106100
Self { tx: Tx::$TX(tx), rx: Rx::$RX(rx) }
107101
}
108102
}
109103

110-
$(#[$attr])*
111-
impl From<(gpio::$TX, &mut MAPR)> for Pins<Tx, Rx<Floating>> {
112-
fn from(p: (gpio::$TX, &mut MAPR)) -> Self {
104+
impl From<(gpio::$TX, &mut <$PER as Remap>::Mapr)> for Pins<Tx, Rx<Floating>> {
105+
fn from(p: (gpio::$TX, &mut <$PER as Remap>::Mapr)) -> Self {
113106
let tx = p.0.into_mode(&mut Cr);
114-
p.1.modify_mapr($remapex);
107+
<$PER>::remap(p.1, $remap);
115108
Self { tx: Tx::$TX(tx), rx: Rx::None(NoPin::new()) }
116109
}
117110
}
118111

119-
$(#[$attr])*
120-
impl<PULL> From<(gpio::$RX, &mut MAPR)> for Pins<Tx, Rx<PULL>>
112+
impl<PULL> From<(gpio::$RX, &mut <$PER as Remap>::Mapr)> for Pins<Tx, Rx<PULL>>
121113
where
122114
Input<PULL>: PinMode,
123115
PULL: InMode,
124116
{
125-
fn from(p: (gpio::$RX, &mut MAPR)) -> Self {
117+
fn from(p: (gpio::$RX, &mut <$PER as Remap>::Mapr)) -> Self {
126118
let rx = p.0.into_mode(&mut Cr);
127-
p.1.modify_mapr($remapex);
119+
<$PER>::remap(p.1, $remap);
128120
Self { tx: Tx::None(NoPin::new()), rx: Rx::$RX(rx) }
129121
}
130122
}

src/i2c.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
// parts of this code is based on
55
// https://www.st.com/content/ccc/resource/technical/document/application_note/5d/ae/a3/6f/08/69/4e/9b/CD00209826.pdf/files/CD00209826.pdf/jcr:content/translations/en.CD00209826.pdf
66

7+
use crate::afio::{Remap, MAPR};
78
use crate::gpio::{self, Alternate, Cr, OpenDrain};
8-
use crate::pac::{DWT, I2C1, I2C2, RCC};
9+
use crate::pac::{self, DWT, RCC};
910
use crate::rcc::{BusClock, Clocks, Enable, Reset};
1011
use crate::time::{kHz, Hertz};
1112
use core::ops::Deref;
@@ -100,30 +101,28 @@ impl<SCL, SDA> From<(SCL, SDA)> for Pins<SCL, SDA> {
100101
}
101102

102103
pub mod i2c1 {
103-
use crate::afio::MAPR;
104-
105104
use super::*;
106105

107106
remap! {
108-
[
109-
PB6, PB7 => MAPR { |_, w| w.i2c1_remap().bit(false) };
110-
PB8, PB9 => MAPR { |_, w| w.i2c1_remap().bit(true) };
107+
pac::I2C1: [
108+
PB6, PB7 => MAPR: 0;
109+
PB8, PB9 => MAPR: 1;
111110
]
112111
}
113112
}
114113
pub mod i2c2 {
115114
use super::*;
116115

117116
remap! {
118-
[
117+
pac::I2C2: [
119118
PB10, PB11;
120119
]
121120
}
122121
}
123122

124123
macro_rules! remap {
125-
([
126-
$($SCL:ident, $SDA:ident $( => $MAPR:ident { $remapex:expr })?;)+
124+
($PER:ty: [
125+
$($SCL:ident, $SDA:ident $( => $MAPR:ident: $remap:literal)?;)+
127126
]) => {
128127
pub enum Scl {
129128
$(
@@ -139,7 +138,7 @@ macro_rules! remap {
139138
$(
140139
impl From<(gpio::$SCL<Alternate<OpenDrain>>, gpio::$SDA<Alternate<OpenDrain>> $(, &mut $MAPR)?)> for Pins<Scl, Sda> {
141140
fn from(p: (gpio::$SCL<Alternate<OpenDrain>>, gpio::$SDA<Alternate<OpenDrain>> $(, &mut $MAPR)?)) -> Self {
142-
$(p.2.modify_mapr($remapex);)?
141+
$(<$PER>::remap(p.2, $remap);)?
143142
Self { scl: Scl::$SCL(p.0), sda: Sda::$SDA(p.1) }
144143
}
145144
}
@@ -149,7 +148,7 @@ macro_rules! remap {
149148
let mut cr = Cr;
150149
let scl = p.0.into_mode(&mut cr);
151150
let sda = p.1.into_mode(&mut cr);
152-
$(p.2.modify_mapr($remapex);)?
151+
$(<$PER>::remap(p.2, $remap);)?
153152
Self { scl: Scl::$SCL(scl), sda: Sda::$SDA(sda) }
154153
}
155154
}
@@ -213,11 +212,11 @@ pub trait Instance:
213212
type Sda;
214213
}
215214

216-
impl Instance for I2C1 {
215+
impl Instance for pac::I2C1 {
217216
type Scl = i2c1::Scl;
218217
type Sda = i2c1::Sda;
219218
}
220-
impl Instance for I2C2 {
219+
impl Instance for pac::I2C2 {
221220
type Scl = i2c2::Scl;
222221
type Sda = i2c2::Sda;
223222
}

0 commit comments

Comments
 (0)