Skip to content

Commit 0f97c39

Browse files
grodinomvertescher
authored andcommitted
Added Enable, Disable traits for data buses (AHBx, APBx)
1 parent a84ac66 commit 0f97c39

File tree

2 files changed

+209
-20
lines changed

2 files changed

+209
-20
lines changed

src/rcc.rs

Lines changed: 205 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ pub trait RccExt {
1414
impl RccExt for RCC {
1515
fn constrain(self) -> Rcc {
1616
Rcc {
17-
ahb1: AHB1(()),
17+
ahb1: AHB1 { _0: () },
18+
ahb2: AHB2 { _0: () },
19+
ahb3: AHB3 { _0: () },
1820
apb1: APB1 { _0: () },
1921
apb2: APB2 { _0: () },
2022
cfgr: CFGR {
@@ -34,6 +36,10 @@ impl RccExt for RCC {
3436
pub struct Rcc {
3537
/// Advanced High-Performance Bus 1 (AHB1) registers
3638
pub ahb1: AHB1,
39+
/// Advanced High-Performance Bus 1 (AHB1) registers
40+
pub ahb2: AHB2,
41+
/// Advanced High-Performance Bus 1 (AHB1) registers
42+
pub ahb3: AHB3,
3743

3844
/// Advanced Peripheral Bus 1 (APB1) registers
3945
pub apb1: APB1,
@@ -42,21 +48,6 @@ pub struct Rcc {
4248
pub cfgr: CFGR,
4349
}
4450

45-
/// Advanced High-Performance Bus 1 (AHB1) registers
46-
pub struct AHB1(());
47-
48-
impl AHB1 {
49-
pub fn enr(&mut self) -> &rcc::AHB1ENR {
50-
// NOTE(unsafe) this proxy grants exclusive access to this register
51-
unsafe { &(*RCC::ptr()).ahb1enr }
52-
}
53-
54-
pub fn rstr(&mut self) -> &rcc::AHB1RSTR {
55-
// NOTE(unsafe) this proxy grants exclusive access to this register
56-
unsafe { &(*RCC::ptr()).ahb1rstr }
57-
}
58-
}
59-
6051
/// Advanced Peripheral Bus 1 (APB1) registers
6152
pub struct APB1 {
6253
_0: (),
@@ -91,6 +82,58 @@ impl APB2 {
9182
}
9283
}
9384

85+
/// Advanced High-performance Bus 1 (AHB1) registers
86+
pub struct AHB1 {
87+
_0: (),
88+
}
89+
90+
impl AHB1 {
91+
pub(crate) fn enr(&mut self) -> &rcc::AHB1ENR {
92+
// NOTE(unsafe) this proxy grants exclusive access to this register
93+
unsafe { &(*RCC::ptr()).ahb1enr }
94+
}
95+
96+
pub(crate) fn rstr(&mut self) -> &rcc::AHB1RSTR {
97+
// NOTE(unsafe) this proxy grants exclusive access to this register
98+
unsafe { &(*RCC::ptr()).ahb1rstr }
99+
}
100+
}
101+
102+
/// Advanced High-performance Bus 1 (AHB1) registers
103+
pub struct AHB2 {
104+
_0: (),
105+
}
106+
107+
impl AHB2 {
108+
pub(crate) fn enr(&mut self) -> &rcc::AHB2ENR {
109+
// NOTE(unsafe) this proxy grants exclusive access to this register
110+
unsafe { &(*RCC::ptr()).ahb2enr }
111+
}
112+
113+
pub(crate) fn rstr(&mut self) -> &rcc::AHB2RSTR {
114+
// NOTE(unsafe) this proxy grants exclusive access to this register
115+
unsafe { &(*RCC::ptr()).ahb2rstr }
116+
}
117+
}
118+
119+
/// Advanced High-performance Bus 1 (AHB1) registers
120+
pub struct AHB3 {
121+
_0: (),
122+
}
123+
124+
impl AHB3 {
125+
pub(crate) fn enr(&mut self) -> &rcc::AHB3ENR {
126+
// NOTE(unsafe) this proxy grants exclusive access to this register
127+
unsafe { &(*RCC::ptr()).ahb3enr }
128+
}
129+
130+
pub(crate) fn rstr(&mut self) -> &rcc::AHB3RSTR {
131+
// NOTE(unsafe) this proxy grants exclusive access to this register
132+
unsafe { &(*RCC::ptr()).ahb3rstr }
133+
}
134+
}
135+
136+
94137

95138
/// HSE Clock modes
96139
/// * `Oscillator`: Use of an external crystal/ceramic resonator
@@ -490,3 +533,149 @@ impl Clocks {
490533
self.timclk2
491534
}
492535
}
536+
537+
pub trait GetBusFreq {
538+
fn get_frequency(clocks: &Clocks) -> Hertz;
539+
fn get_timer_frequency(clocks: &Clocks) -> Hertz {
540+
Self::get_frequency(clocks)
541+
}
542+
}
543+
544+
impl GetBusFreq for AHB1 {
545+
fn get_frequency(clocks: &Clocks) -> Hertz {
546+
clocks.hclk
547+
}
548+
}
549+
550+
impl GetBusFreq for AHB2 {
551+
fn get_frequency(clocks: &Clocks) -> Hertz {
552+
clocks.hclk
553+
}
554+
}
555+
556+
impl GetBusFreq for AHB3 {
557+
fn get_frequency(clocks: &Clocks) -> Hertz {
558+
clocks.hclk
559+
}
560+
}
561+
562+
impl GetBusFreq for APB1 {
563+
fn get_frequency(clocks: &Clocks) -> Hertz {
564+
clocks.pclk1
565+
}
566+
fn get_timer_frequency(clocks: &Clocks) -> Hertz {
567+
clocks.timclk1()
568+
}
569+
}
570+
571+
impl GetBusFreq for APB2 {
572+
fn get_frequency(clocks: &Clocks) -> Hertz {
573+
clocks.pclk2
574+
}
575+
fn get_timer_frequency(clocks: &Clocks) -> Hertz {
576+
clocks.timclk2()
577+
}
578+
}
579+
580+
pub(crate) mod sealed {
581+
/// Bus associated to peripheral
582+
pub trait RccBus {
583+
/// Bus type;
584+
type Bus;
585+
}
586+
}
587+
use sealed::RccBus;
588+
589+
/// Enable/disable peripheral
590+
pub trait Enable: RccBus {
591+
fn enable(apb: &mut Self::Bus);
592+
fn disable(apb: &mut Self::Bus);
593+
}
594+
595+
/// Reset peripheral
596+
pub trait Reset: RccBus {
597+
fn reset(apb: &mut Self::Bus);
598+
}
599+
600+
macro_rules! bus {
601+
($($PER:ident => ($apbX:ty, $peren:ident, $perrst:ident),)+) => {
602+
$(
603+
impl RccBus for crate::device::$PER {
604+
type Bus = $apbX;
605+
}
606+
impl Enable for crate::device::$PER {
607+
#[inline(always)]
608+
fn enable(apb: &mut Self::Bus) {
609+
apb.enr().modify(|_, w| w.$peren().set_bit());
610+
}
611+
#[inline(always)]
612+
fn disable(apb: &mut Self::Bus) {
613+
apb.enr().modify(|_, w| w.$peren().clear_bit());
614+
}
615+
}
616+
impl Reset for crate::device::$PER {
617+
#[inline(always)]
618+
fn reset(apb: &mut Self::Bus) {
619+
apb.rstr().modify(|_, w| w.$perrst().set_bit());
620+
apb.rstr().modify(|_, w| w.$perrst().clear_bit());
621+
}
622+
}
623+
)+
624+
}
625+
}
626+
627+
// Peripherals respective buses
628+
// TODO: check which processor has which peripheral and add them
629+
bus! {
630+
I2C1 => (APB1, i2c1en, i2c1rst),
631+
I2C2 => (APB1, i2c2en, i2c2rst),
632+
I2C3 => (APB1, i2c3en, i2c3rst),
633+
I2C4 => (APB1, i2c4en, i2c4rst),
634+
635+
SPI1 => (APB2, spi1en, spi1rst),
636+
SPI2 => (APB1, spi2en, spi2rst),
637+
SPI3 => (APB1, spi3en, spi3rst),
638+
639+
USART1 => (APB2, usart1en, usart1rst),
640+
USART2 => (APB1, usart2en, uart2rst),
641+
USART3 => (APB1, usart3en, uart3rst),
642+
UART4 => (APB1, uart4en, uart4rst),
643+
UART5 => (APB1, uart5en, uart5rst),
644+
USART6 => (APB2, usart6en, usart6rst),
645+
UART7 => (APB1, uart7en, uart7rst),
646+
UART8 => (APB1, uart8en, uart8rst),
647+
648+
WWDG => (APB1, wwdgen, wwdgrst),
649+
650+
DMA1 => (AHB1, dma1en, dma1rst),
651+
DMA2 => (AHB1, dma2en, dma2rst),
652+
653+
DMA2D => (AHB1, dma2den, dma2drst),
654+
655+
GPIOA => (AHB1, gpioaen, gpioarst),
656+
GPIOB => (AHB1, gpioben, gpiobrst),
657+
GPIOC => (AHB1, gpiocen, gpiocrst),
658+
GPIOD => (AHB1, gpioden, gpiodrst),
659+
GPIOE => (AHB1, gpioeen, gpioerst),
660+
GPIOF => (AHB1, gpiofen, gpiofrst),
661+
GPIOG => (AHB1, gpiogen, gpiogrst),
662+
GPIOH => (AHB1, gpiohen, gpiohrst),
663+
GPIOI => (AHB1, gpioien, gpioirst),
664+
GPIOJ => (AHB1, gpiojen, gpiojrst),
665+
GPIOK => (AHB1, gpioken, gpiokrst),
666+
667+
TIM1 => (APB2, tim1en, tim1rst),
668+
TIM2 => (APB1, tim2en, tim2rst),
669+
TIM3 => (APB1, tim3en, tim3rst),
670+
TIM4 => (APB1, tim4en, tim4rst),
671+
TIM5 => (APB1, tim5en, tim5rst),
672+
TIM6 => (APB1, tim6en, tim6rst),
673+
TIM7 => (APB1, tim7en, tim7rst),
674+
TIM8 => (APB2, tim8en, tim8rst),
675+
TIM9 => (APB2, tim9en, tim9rst),
676+
TIM10 => (APB2, tim10en, tim10rst),
677+
TIM11 => (APB2, tim11en, tim11rst),
678+
TIM12 => (APB1, tim12en, tim12rst),
679+
TIM13 => (APB1, tim13en, tim13rst),
680+
TIM14 => (APB1, tim14en, tim14rst),
681+
}

src/time.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/// Bits per second
2-
#[derive(PartialEq, PartialOrd, Clone, Copy)]
2+
#[derive(PartialEq, PartialOrd, Clone, Copy, Debug)]
33
pub struct Bps(pub u32);
44

5-
#[derive(PartialEq, PartialOrd, Clone, Copy)]
5+
#[derive(PartialEq, PartialOrd, Clone, Copy, Debug)]
66
pub struct Hertz(pub u32);
77

8-
#[derive(PartialEq, PartialOrd, Clone, Copy)]
8+
#[derive(PartialEq, PartialOrd, Clone, Copy, Debug)]
99
pub struct KiloHertz(pub u32);
1010

11-
#[derive(PartialEq, PartialOrd, Clone, Copy)]
11+
#[derive(PartialEq, PartialOrd, Clone, Copy, Debug)]
1212
pub struct MegaHertz(pub u32);
1313

1414
/// Extension trait that adds convenience methods to the `u32` type

0 commit comments

Comments
 (0)