Skip to content

Commit dd31504

Browse files
committed
use bit banding
1 parent c07fe63 commit dd31504

File tree

2 files changed

+37
-56
lines changed

2 files changed

+37
-56
lines changed

src/can.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,23 @@
1818
//! | TX | PB13 |
1919
//! | RX | PB12 |
2020
21+
use crate::bb;
2122
use crate::gpio::{
2223
gpioa::{PA11, PA12},
2324
gpiob::{PB12, PB13, PB5, PB6, PB8, PB9},
2425
Alternate, AF9,
2526
};
26-
use crate::pac::CAN1;
27-
use crate::pac::CAN2;
27+
use crate::pac::{CAN1, CAN2};
2828
use crate::rcc::APB1;
2929

3030
mod sealed {
3131
pub trait Sealed {}
32+
33+
/// Bus associated to peripheral
34+
pub trait RccBus {
35+
/// Bus type;
36+
type Bus;
37+
}
3238
}
3339

3440
pub trait Pins: sealed::Sealed {
@@ -57,14 +63,42 @@ pins! {
5763
CAN2 => (PB6, PB5),
5864
}
5965

66+
use sealed::RccBus;
67+
68+
/// Enable/disable peripheral
69+
pub trait Enable: RccBus {
70+
fn enable(apb: &mut Self::Bus);
71+
}
72+
73+
macro_rules! bus {
74+
($($PER:ident => ($apbX:ty, $peren:literal),)+) => {
75+
$(
76+
impl RccBus for crate::pac::$PER {
77+
type Bus = $apbX;
78+
}
79+
impl Enable for crate::pac::$PER {
80+
#[inline(always)]
81+
fn enable(apb: &mut Self::Bus) {
82+
unsafe { bb::set(apb.enr(), $peren) };
83+
}
84+
}
85+
)+
86+
}
87+
}
88+
89+
bus! {
90+
CAN1 => (APB1, 25),
91+
CAN2 => (APB1, 26),
92+
}
93+
6094
/// Interface to the CAN peripheral.
6195
pub struct Can<Instance> {
6296
_peripheral: Instance,
6397
}
6498

6599
impl<Instance> Can<Instance>
66100
where
67-
Instance: crate::rcc::Enable<Bus = APB1>,
101+
Instance: Enable<Bus = APB1>,
68102
{
69103
/// Creates a CAN interaface.
70104
pub fn new<P>(can: Instance, _pins: P, apb: &mut APB1) -> Can<Instance>

src/rcc/mod.rs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,56 +1506,3 @@ impl Clocks {
15061506
self.sai2_clk
15071507
}
15081508
}
1509-
1510-
pub(crate) mod sealed {
1511-
/// Bus associated to peripheral
1512-
pub trait RccBus {
1513-
/// Bus type;
1514-
type Bus;
1515-
}
1516-
}
1517-
use sealed::RccBus;
1518-
1519-
/// Enable/disable peripheral
1520-
pub trait Enable: RccBus {
1521-
fn enable(apb: &mut Self::Bus);
1522-
fn disable(apb: &mut Self::Bus);
1523-
}
1524-
1525-
/// Reset peripheral
1526-
pub trait Reset: RccBus {
1527-
fn reset(apb: &mut Self::Bus);
1528-
}
1529-
1530-
macro_rules! bus {
1531-
($($PER:ident => ($apbX:ty, $peren:ident, $perrst:ident),)+) => {
1532-
$(
1533-
impl RccBus for crate::pac::$PER {
1534-
type Bus = $apbX;
1535-
}
1536-
impl Enable for crate::pac::$PER {
1537-
#[inline(always)]
1538-
fn enable(apb: &mut Self::Bus) {
1539-
apb.enr().modify(|_, w| w.$peren().set_bit());
1540-
}
1541-
#[inline(always)]
1542-
fn disable(apb: &mut Self::Bus) {
1543-
apb.enr().modify(|_, w| w.$peren().clear_bit());
1544-
}
1545-
}
1546-
impl Reset for crate::pac::$PER {
1547-
#[inline(always)]
1548-
fn reset(apb: &mut Self::Bus) {
1549-
apb.rstr().modify(|_, w| w.$perrst().set_bit());
1550-
apb.rstr().modify(|_, w| w.$perrst().clear_bit());
1551-
}
1552-
}
1553-
)+
1554-
}
1555-
}
1556-
1557-
#[cfg(feature = "has-can")]
1558-
bus! {
1559-
CAN1 => (APB1, can1en, can1rst),
1560-
CAN2 => (APB1, can2en, can2rst),
1561-
}

0 commit comments

Comments
 (0)