Skip to content

Commit 3c3b9b3

Browse files
committed
can alt enums
1 parent 4c26d82 commit 3c3b9b3

File tree

1 file changed

+25
-66
lines changed

1 file changed

+25
-66
lines changed

src/can.rs

Lines changed: 25 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@
4141
//! - [Basic Example](https://github.com/stm32-rs/stm32h7xx-hal/blob/master/examples/can-echo.rs)
4242
//! - [CAN-FD with Bit Rate Switching](https://github.com/stm32-rs/stm32h7xx-hal/blob/master/examples/can-fd.rs)
4343
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;
4944
use crate::rcc::{rec, rec::ResetEnable};
5045

5146
/// Storage type for the CAN controller
@@ -60,21 +55,23 @@ impl<FDCAN> Can<FDCAN> {
6055
}
6156
}
6257

58+
pub trait Instance: crate::Sealed {
59+
type Tx;
60+
type Rx;
61+
}
62+
6363
/// Extension trait for CAN controller
64-
pub trait CanExt: Sized
64+
pub trait CanExt: Sized + Instance
6565
where
6666
Can<Self>: fdcan::Instance,
6767
{
68-
fn fdcan<TX, RX>(
68+
fn fdcan(
6969
self,
70-
_tx: TX,
71-
_rx: RX,
70+
tx: impl Into<Self::Tx>,
71+
rx: impl Into<Self::Rx>,
7272
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());
7875
self.fdcan_unchecked(prec)
7976
}
8077

@@ -152,62 +149,17 @@ macro_rules! message_ram_layout {
152149
};
153150
}
154151

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-
207152
mod fdcan1 {
208153
use super::{rec, Can, CanExt, ResetEnable};
154+
use crate::gpio;
209155
use crate::stm32::FDCAN1;
210156

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+
211163
impl Can<FDCAN1> {
212164
pub fn fdcan1(
213165
rb: FDCAN1,
@@ -242,8 +194,15 @@ mod fdcan1 {
242194

243195
mod fdcan2 {
244196
use super::{rec, Can, CanExt, ResetEnable};
197+
use crate::gpio;
245198
use crate::stm32::FDCAN2;
246199

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+
247206
impl Can<FDCAN2> {
248207
pub fn fdcan2(
249208
rb: FDCAN2,

0 commit comments

Comments
 (0)