41
41
//! - [Basic Example](https://github.com/stm32-rs/stm32h7xx-hal/blob/master/examples/can-echo.rs)
42
42
//! - [CAN-FD with Bit Rate Switching](https://github.com/stm32-rs/stm32h7xx-hal/blob/master/examples/can-fd.rs)
43
43
44
- use crate :: gpio:: gpioa:: { PA11 , PA12 } ;
45
- use crate :: gpio:: gpiob:: { PB12 , PB13 , PB5 , PB6 , PB8 , PB9 } ;
46
- use crate :: gpio:: gpiod:: { PD0 , PD1 } ;
47
- use crate :: gpio:: gpioh:: { PH13 , PH14 } ;
48
- use crate :: gpio:: Alternate ;
49
44
use crate :: rcc:: { rec, rec:: ResetEnable } ;
50
45
51
46
/// Storage type for the CAN controller
@@ -60,21 +55,23 @@ impl<FDCAN> Can<FDCAN> {
60
55
}
61
56
}
62
57
58
+ pub trait Instance : crate :: Sealed {
59
+ type Tx ;
60
+ type Rx ;
61
+ }
62
+
63
63
/// Extension trait for CAN controller
64
- pub trait CanExt : Sized
64
+ pub trait CanExt : Sized + Instance
65
65
where
66
66
Can < Self > : fdcan:: Instance ,
67
67
{
68
- fn fdcan < TX , RX > (
68
+ fn fdcan (
69
69
self ,
70
- _tx : TX ,
71
- _rx : RX ,
70
+ tx : impl Into < Self :: Tx > ,
71
+ rx : impl Into < Self :: Rx > ,
72
72
prec : rec:: Fdcan ,
73
- ) -> fdcan:: FdCan < Can < Self > , fdcan:: ConfigMode >
74
- where
75
- TX : sealed:: Tx < Self > ,
76
- RX : sealed:: Rx < Self > ,
77
- {
73
+ ) -> fdcan:: FdCan < Can < Self > , fdcan:: ConfigMode > {
74
+ let _pins = ( tx. into ( ) , rx. into ( ) ) ;
78
75
self . fdcan_unchecked ( prec)
79
76
}
80
77
@@ -152,62 +149,17 @@ macro_rules! message_ram_layout {
152
149
} ;
153
150
}
154
151
155
- mod sealed {
156
- /// A TX pin configured for CAN communication
157
- pub trait Tx < FDCAN > { }
158
- /// An RX pin configured for CAN communication
159
- pub trait Rx < FDCAN > { }
160
- }
161
-
162
- /// Implements sealed::{Tx,Rx} for pins associated with a CAN peripheral
163
- macro_rules! pins {
164
- ( $PER: ident =>
165
- ( TX : [ $( $( #[ $pmetatx: meta ] ) * $tx: ty) ,+ $( , ) ? ] ,
166
- RX : [ $( $( #[ $pmetarx: meta ] ) * $rx: ty) ,+ $( , ) ? ] ) ) => {
167
- $(
168
- $( #[ $pmetatx ] ) *
169
- impl sealed:: Tx <crate :: stm32:: $PER> for $tx { }
170
- ) +
171
- $(
172
- $( #[ $pmetarx ] ) *
173
- impl sealed:: Rx <crate :: stm32:: $PER> for $rx { }
174
- ) +
175
- } ;
176
- }
177
-
178
- pins ! {
179
- FDCAN1 => (
180
- TX : [
181
- PA12 <Alternate <9 >>,
182
- PB9 <Alternate <9 >>,
183
- PD1 <Alternate <9 >>,
184
- PH13 <Alternate <9 >>
185
- ] ,
186
- RX : [
187
- PA11 <Alternate <9 >>,
188
- PB8 <Alternate <9 >>,
189
- PD0 <Alternate <9 >>,
190
- PH14 <Alternate <9 >>
191
- ]
192
- )
193
- }
194
- pins ! {
195
- FDCAN2 => (
196
- TX : [
197
- PB6 <Alternate <9 >>,
198
- PB13 <Alternate <9 >>
199
- ] ,
200
- RX : [
201
- PB5 <Alternate <9 >>,
202
- PB12 <Alternate <9 >>
203
- ]
204
- )
205
- }
206
-
207
152
mod fdcan1 {
208
153
use super :: { rec, Can , CanExt , ResetEnable } ;
154
+ use crate :: gpio;
209
155
use crate :: stm32:: FDCAN1 ;
210
156
157
+ impl crate :: Sealed for FDCAN1 { }
158
+ impl super :: Instance for FDCAN1 {
159
+ type Tx = gpio:: alt:: fdcan1:: Tx ;
160
+ type Rx = gpio:: alt:: fdcan1:: Rx ;
161
+ }
162
+
211
163
impl Can < FDCAN1 > {
212
164
pub fn fdcan1 (
213
165
rb : FDCAN1 ,
@@ -242,8 +194,15 @@ mod fdcan1 {
242
194
243
195
mod fdcan2 {
244
196
use super :: { rec, Can , CanExt , ResetEnable } ;
197
+ use crate :: gpio;
245
198
use crate :: stm32:: FDCAN2 ;
246
199
200
+ impl crate :: Sealed for FDCAN2 { }
201
+ impl super :: Instance for FDCAN2 {
202
+ type Tx = gpio:: alt:: fdcan2:: Tx ;
203
+ type Rx = gpio:: alt:: fdcan2:: Rx ;
204
+ }
205
+
247
206
impl Can < FDCAN2 > {
248
207
pub fn fdcan2 (
249
208
rb : FDCAN2 ,
0 commit comments