Skip to content

Commit e6b7d67

Browse files
committed
refactor(can): gen pin and instance trait impls
1 parent 697bf2c commit e6b7d67

File tree

6 files changed

+29
-31
lines changed

6 files changed

+29
-31
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = [
2626
] }
2727
embedded-hal = { package = "embedded-hal", version = "1.0" }
2828
embedded-hal-async = "1.0.0"
29-
embedded-can = { version = "0.4.1", optional = true }
29+
embedded-can = "0.4.1"
3030

3131
critical-section = { version = "1.2.0" }
3232
defmt = { version = "0.3.8", optional = true }
@@ -89,7 +89,6 @@ time-driver-tim8 = ["_time-driver"]
8989
time-driver-tim9 = ["_time-driver"]
9090
## Use TIM10 as time driver
9191
time-driver-tim10 = ["_time-driver"]
92-
can = ["dep:embedded-can"]
9392
rt-wfi = []
9493

9594
# Chip-selection features

build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ fn main() {
396396
// USBPD, handled by usbpd/mod.rs
397397
//(("usbpd", "CC1"), quote!(crate::usbpd::Cc1Pin)),
398398
//(("usbpd", "CC2"), quote!(crate::usbpd::Cc2Pin)),
399+
(("can", "TX"), quote!(crate::can::TxPin)),
400+
(("can", "RX"), quote!(crate::can::RxPin)),
399401
]
400402
.into();
401403

examples/ch32v203/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ ch32-hal = { path = "../../", features = [
1010
"embassy",
1111
"rt",
1212
"time-driver-tim2",
13-
"can"
1413
], default-features = false }
1514

1615
embassy-executor = { version = "0.6.0", features = [

src/can/can.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use super::enums::*;
22
use super::filter::{BitMode, FilterMode};
33
use super::{CanFilter, CanFrame};
4-
use crate as hal;
54
use crate::can::registers::Registers;
65
use crate::can::util;
7-
use crate::{into_ref, pac, Peripheral, PeripheralRef, RccPeripheral};
6+
use crate::{self as hal, into_ref, pac, peripherals, Peripheral, PeripheralRef, RccPeripheral, RemapPeripheral};
87

98
pub struct Can<'d, T: Instance> {
109
_peri: PeripheralRef<'d, T>,
@@ -21,10 +20,10 @@ impl<'d, T: Instance> Can<'d, T> {
2120
/// Assumes AFIO & PORTB clocks have been enabled by HAL.
2221
///
2322
/// CAN_RX is mapped to PB8, and CAN_TX is mapped to PB9.
24-
pub fn new(
23+
pub fn new<const REMAP: u8>(
2524
peri: impl Peripheral<P = T> + 'd,
26-
rx: impl Peripheral<P = impl RxPin<T>> + 'd,
27-
tx: impl Peripheral<P = impl TxPin<T>> + 'd,
25+
rx: impl Peripheral<P = impl RxPin<T, REMAP>> + 'd,
26+
tx: impl Peripheral<P = impl TxPin<T, REMAP>> + 'd,
2827
fifo: CanFifo,
2928
mode: CanMode,
3029
bitrate: u32,
@@ -47,6 +46,7 @@ impl<'d, T: Instance> Can<'d, T> {
4746
pac::gpio::vals::Mode::OUTPUT_50MHZ,
4847
pac::gpio::vals::Cnf::PULL_IN__AF_PUSH_PULL_OUT,
4948
);
49+
T::set_remap(REMAP);
5050

5151
// //here should remap functionality be added
5252
// T::remap(0b10);
@@ -134,29 +134,27 @@ where
134134
}
135135
}
136136

137-
pub trait SealedInstance: RccPeripheral {
137+
pub trait SealedInstance: RccPeripheral + RemapPeripheral {
138138
fn regs() -> pac::can::Can;
139-
/// Either `0b00`, `0b10` or `b11` on CAN1. `0` or `1` on CAN2.
140-
fn remap(rm: u8) -> ();
139+
// Either `0b00`, `0b10` or `b11` on CAN1. `0` or `1` on CAN2.
140+
// fn remap(rm: u8) -> ();
141141
}
142142

143143
pub trait Instance: SealedInstance + 'static {}
144-
pub trait RxPin<T: Instance>: hal::gpio::Pin {}
145-
pub trait TxPin<T: Instance>: hal::gpio::Pin {}
146144

147-
impl SealedInstance for hal::peripherals::CAN1 {
148-
fn regs() -> pac::can::Can {
149-
pac::CAN1
150-
}
151-
fn remap(rm: u8) {
152-
pac::AFIO.pcfr1().modify(|w| w.set_can1_rm(rm));
153-
}
154-
}
145+
pin_trait!(RxPin, Instance);
146+
pin_trait!(TxPin, Instance);
155147

156-
impl Instance for hal::peripherals::CAN1 {}
157-
158-
impl RxPin<hal::peripherals::CAN1> for hal::peripherals::PB8 {}
159-
impl TxPin<hal::peripherals::CAN1> for hal::peripherals::PB9 {}
148+
foreach_peripheral!(
149+
(can, $inst:ident) => {
150+
impl SealedInstance for peripherals::$inst {
151+
fn regs() -> crate::pac::can::Can {
152+
crate::pac::$inst
153+
}
154+
}
160155

161-
impl RxPin<hal::peripherals::CAN1> for hal::peripherals::PA11 {}
162-
impl TxPin<hal::peripherals::CAN1> for hal::peripherals::PA12 {}
156+
impl Instance for peripherals::$inst {
157+
// type Interrupt = crate::_generated::peripheral_interrupts::$inst::GLOBAL;
158+
}
159+
};
160+
);

src/can/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod frame;
55
mod registers;
66
mod util;
77

8-
pub use can::Can;
8+
pub use can::{Can, Instance, TxPin, RxPin};
99
pub use embedded_can::{ExtendedId, Id, StandardId};
1010
pub use enums::{CanError, CanFifo, CanMode, TxStatus};
1111
pub use filter::{Bit16Mode, Bit32Mode, CanFilter, ListMode, MaskMode};

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ pub(crate) mod internal;
1414

1515
mod macros;
1616

17-
#[cfg(feature = "can")]
18-
pub mod can;
19-
2017
pub mod time;
2118
/// Operating modes for peripherals.
2219
pub mod mode {
@@ -93,6 +90,9 @@ pub mod usbhs;
9390
#[cfg(usbpd)]
9491
pub mod usbpd;
9592

93+
#[cfg(can)]
94+
pub mod can;
95+
9696
#[cfg(feature = "embassy")]
9797
pub mod embassy;
9898

0 commit comments

Comments
 (0)