20
20
//! | RX | PB5 | PB12 |
21
21
22
22
use crate :: afio:: MAPR ;
23
- use crate :: gpio:: { self , Alternate , Cr , Floating , Input , PinMode , PullUp } ;
23
+ use crate :: gpio:: { self , Alternate , Cr , Floating , Input , NoPin , PinMode , PullUp , PushPull } ;
24
24
use crate :: pac:: { self , RCC } ;
25
25
26
26
pub trait InMode { }
27
27
impl InMode for Floating { }
28
28
impl InMode for PullUp { }
29
29
30
+ pub struct Pins < TX , RX > {
31
+ pub tx : TX ,
32
+ pub rx : RX ,
33
+ }
34
+
35
+ impl < TX , RX > From < ( TX , RX ) > for Pins < TX , RX > {
36
+ fn from ( value : ( TX , RX ) ) -> Self {
37
+ Self {
38
+ tx : value. 0 ,
39
+ rx : value. 1 ,
40
+ }
41
+ }
42
+ }
43
+
30
44
pub mod can1 {
31
45
use super :: * ;
32
46
33
47
remap ! {
34
- Pins : [
35
- #[ cfg( not( feature = "connectivity" ) ) ]
36
- All , Tx , Rx , PA12 , PA11 => { |_, w| unsafe { w. can_remap( ) . bits( 0 ) } } ;
37
- #[ cfg( feature = "connectivity" ) ]
38
- All , Tx , Rx , PA12 , PA11 => { |_, w| unsafe { w. can1_remap( ) . bits( 0 ) } } ;
39
- #[ cfg( not( feature = "connectivity" ) ) ]
40
- Remap , RemapTx , RemapRx , PB9 , PB8 => { |_, w| unsafe { w. can_remap( ) . bits( 10 ) } } ;
41
- #[ cfg( feature = "connectivity" ) ]
42
- Remap , RemapTx , RemapRx , PB9 , PB8 => { |_, w| unsafe { w. can1_remap( ) . bits( 10 ) } } ;
43
- ]
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 ) } } ;
44
56
}
45
57
}
46
58
@@ -49,39 +61,39 @@ pub mod can2 {
49
61
use super :: * ;
50
62
51
63
remap ! {
52
- Pins : [
53
- All , Tx , Rx , PB6 , PB5 => { |_, w| w. can2_remap( ) . bit( false ) } ;
54
- Remap , RemapTx , RemapRx , PB13 , PB12 => { |_, w| w. can2_remap( ) . bit( true ) } ;
55
- ]
64
+ PB6 , PB5 => { |_, w| w. can2_remap( ) . bit( false ) } ;
65
+ PB13 , PB12 => { |_, w| w. can2_remap( ) . bit( true ) } ;
56
66
}
57
67
}
58
68
59
69
macro_rules! remap {
60
- ( $name: ident: [
61
- $( $( #[ $attr: meta] ) * $rname: ident, $txonly: ident, $rxonly: ident, $TX: ident, $RX: ident => { $remapex: expr } ; ) +
62
- ] ) => {
63
- pub enum $name<PULL > {
70
+ ( $( $( #[ $attr: meta] ) * $TX: ident, $RX: ident => { $remapex: expr } ; ) +) => {
71
+ pub enum Tx {
64
72
$(
65
73
$( #[ $attr] ) *
66
- $rname { tx: gpio:: $TX<Alternate >, rx: gpio:: $RX<Input <PULL >> } ,
67
- $( #[ $attr] ) *
68
- $txonly { tx: gpio:: $TX<Alternate > } ,
74
+ $TX( gpio:: $TX<Alternate >) ,
75
+ ) +
76
+ None ( NoPin <PushPull >) ,
77
+ }
78
+ pub enum Rx <PULL > {
79
+ $(
69
80
$( #[ $attr] ) *
70
- $rxonly { rx : gpio:: $RX<Input <PULL >> } ,
81
+ $RX ( gpio:: $RX<Input <PULL >>) ,
71
82
) +
83
+ None ( NoPin <PULL >) ,
72
84
}
73
85
74
86
$(
75
87
$( #[ $attr] ) *
76
- impl <PULL : InMode > From <( gpio:: $TX<Alternate >, gpio:: $RX<Input <PULL >>, & mut MAPR ) > for $name< PULL > {
88
+ impl <PULL : InMode > From <( gpio:: $TX<Alternate >, gpio:: $RX<Input <PULL >>, & mut MAPR ) > for Pins < Tx , Rx < PULL > > {
77
89
fn from( p: ( gpio:: $TX<Alternate >, gpio:: $RX<Input <PULL >>, & mut MAPR ) ) -> Self {
78
90
p. 2 . modify_mapr( $remapex) ;
79
- Self :: $rname { tx: p. 0 , rx: p. 1 }
91
+ Self { tx: Tx :: $TX ( p. 0 ) , rx: Rx :: $RX ( p. 1 ) }
80
92
}
81
93
}
82
94
83
95
$( #[ $attr] ) *
84
- impl <PULL > From <( gpio:: $TX, gpio:: $RX, & mut MAPR ) > for $name< PULL >
96
+ impl <PULL > From <( gpio:: $TX, gpio:: $RX, & mut MAPR ) > for Pins < Tx , Rx < PULL > >
85
97
where
86
98
Input <PULL >: PinMode ,
87
99
PULL : InMode ,
@@ -91,29 +103,29 @@ macro_rules! remap {
91
103
let tx = p. 0 . into_mode( & mut cr) ;
92
104
let rx = p. 1 . into_mode( & mut cr) ;
93
105
p. 2 . modify_mapr( $remapex) ;
94
- Self :: $rname { tx, rx }
106
+ Self { tx: Tx :: $TX ( tx ) , rx: Rx :: $RX ( rx ) }
95
107
}
96
108
}
97
109
98
110
$( #[ $attr] ) *
99
- impl From <( gpio:: $TX, & mut MAPR ) > for $name< Floating > {
111
+ impl From <( gpio:: $TX, & mut MAPR ) > for Pins < Tx , Rx < Floating > > {
100
112
fn from( p: ( gpio:: $TX, & mut MAPR ) ) -> Self {
101
113
let tx = p. 0 . into_mode( & mut Cr ) ;
102
114
p. 1 . modify_mapr( $remapex) ;
103
- Self :: $txonly { tx }
115
+ Self { tx : Tx :: $TX ( tx ) , rx : Rx :: None ( NoPin :: new ( ) ) }
104
116
}
105
117
}
106
118
107
119
$( #[ $attr] ) *
108
- impl <PULL > From <( gpio:: $RX, & mut MAPR ) > for $name< PULL >
120
+ impl <PULL > From <( gpio:: $RX, & mut MAPR ) > for Pins < Tx , Rx < PULL > >
109
121
where
110
122
Input <PULL >: PinMode ,
111
123
PULL : InMode ,
112
124
{
113
125
fn from( p: ( gpio:: $RX, & mut MAPR ) ) -> Self {
114
126
let rx = p. 0 . into_mode( & mut Cr ) ;
115
127
p. 1 . modify_mapr( $remapex) ;
116
- Self :: $rxonly { rx }
128
+ Self { tx : Tx :: None ( NoPin :: new ( ) ) , rx : Rx :: $RX ( rx ) }
117
129
}
118
130
}
119
131
) +
@@ -125,7 +137,7 @@ pub trait CanExt: Sized + Instance {
125
137
fn can (
126
138
self ,
127
139
#[ cfg( not( feature = "connectivity" ) ) ] usb : pac:: USB ,
128
- pins : impl Into < Self :: Pins < Floating > > ,
140
+ pins : impl Into < Pins < Self :: Tx , Self :: Rx < Floating > > > ,
129
141
) -> Can < Self , Floating > ;
130
142
fn can_loopback (
131
143
self ,
@@ -137,7 +149,7 @@ impl<CAN: Instance> CanExt for CAN {
137
149
fn can (
138
150
self ,
139
151
#[ cfg( not( feature = "connectivity" ) ) ] usb : pac:: USB ,
140
- pins : impl Into < Self :: Pins < Floating > > ,
152
+ pins : impl Into < Pins < Self :: Tx , Self :: Rx < Floating > > > ,
141
153
) -> Can < Self , Floating > {
142
154
Can :: new (
143
155
self ,
@@ -159,21 +171,24 @@ impl<CAN: Instance> CanExt for CAN {
159
171
}
160
172
161
173
pub trait Instance : crate :: rcc:: Enable {
162
- type Pins < PULL > ;
174
+ type Tx ;
175
+ type Rx < PULL > ;
163
176
}
164
177
impl Instance for pac:: CAN1 {
165
- type Pins < PULL > = can1:: Pins < PULL > ;
178
+ type Tx = can1:: Tx ;
179
+ type Rx < PULL > = can1:: Rx < PULL > ;
166
180
}
167
181
#[ cfg( feature = "connectivity" ) ]
168
182
impl Instance for pac:: CAN2 {
169
- type Pins < PULL > = can2:: Pins < PULL > ;
183
+ type Tx = can2:: Tx ;
184
+ type Rx < PULL > = can2:: Rx < PULL > ;
170
185
}
171
186
172
187
/// Interface to the CAN peripheral.
173
188
#[ allow( unused) ]
174
189
pub struct Can < CAN : Instance , PULL = Floating > {
175
190
can : CAN ,
176
- pins : Option < CAN :: Pins < PULL > > ,
191
+ pins : Option < Pins < CAN :: Tx , CAN :: Rx < PULL > > > ,
177
192
}
178
193
179
194
impl < CAN : Instance , PULL > Can < CAN , PULL > {
@@ -184,7 +199,7 @@ impl<CAN: Instance, PULL> Can<CAN, PULL> {
184
199
pub fn new (
185
200
can : CAN ,
186
201
#[ cfg( not( feature = "connectivity" ) ) ] _usb : pac:: USB ,
187
- pins : impl Into < CAN :: Pins < PULL > > ,
202
+ pins : impl Into < Pins < CAN :: Tx , CAN :: Rx < PULL > > > ,
188
203
) -> Can < CAN , PULL > {
189
204
let rcc = unsafe { & ( * RCC :: ptr ( ) ) } ;
190
205
CAN :: enable ( rcc) ;
0 commit comments