Skip to content

Commit af9e9f0

Browse files
committed
Add syscfg driver to enforce clock enable
1 parent d983c32 commit af9e9f0

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

src/gpio.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
33
use core::marker::PhantomData;
44

5-
use crate::pac::{EXTI, SYSCFG};
5+
use crate::pac::EXTI;
6+
use crate::syscfg::SysCfg;
67

78
/// Extension trait to split a GPIO peripheral in independent pins and registers
89
pub trait GpioExt {
@@ -85,7 +86,7 @@ pub enum Edge {
8586

8687
/// External Interrupt Pin
8788
pub trait ExtiPin {
88-
fn make_interrupt_source(&mut self, syscfg: &mut SYSCFG);
89+
fn make_interrupt_source(&mut self, syscfg: &mut SysCfg);
8990
fn trigger_on_edge(&mut self, exti: &mut EXTI, level: Edge);
9091
fn enable_interrupt(&mut self, exti: &mut EXTI);
9192
fn disable_interrupt(&mut self, exti: &mut EXTI);
@@ -97,7 +98,7 @@ macro_rules! exti_erased {
9798
($PIN:ty, $extigpionr:expr) => {
9899
impl<MODE> ExtiPin for $PIN {
99100
/// Make corresponding EXTI line sensitive to this pin
100-
fn make_interrupt_source(&mut self, syscfg: &mut SYSCFG) {
101+
fn make_interrupt_source(&mut self, syscfg: &mut SysCfg) {
101102
let offset = 4 * (self.i % 4);
102103
match self.i {
103104
0..=3 => {
@@ -177,7 +178,7 @@ macro_rules! exti {
177178
($PIN:ty, $extigpionr:expr, $i:expr, $exticri:ident) => {
178179
impl<MODE> ExtiPin for $PIN {
179180
/// Configure EXTI Line $i to trigger from this pin.
180-
fn make_interrupt_source(&mut self, syscfg: &mut SYSCFG) {
181+
fn make_interrupt_source(&mut self, syscfg: &mut SysCfg) {
181182
let offset = 4 * ($i % 4);
182183
syscfg.$exticri.modify(|r, w| unsafe {
183184
let mut exticr = r.bits();
@@ -247,7 +248,7 @@ macro_rules! gpio {
247248
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, toggleable};
248249
use crate::pac::$GPIOX;
249250

250-
use crate::{pac::{RCC, EXTI, SYSCFG}, bb};
251+
use crate::{pac::{RCC, EXTI}, bb, syscfg::SysCfg};
251252
use super::{
252253
Alternate, AlternateOD, Floating, GpioExt, Input, OpenDrain, Output, Speed,
253254
PullDown, PullUp, PushPull, AF0, AF1, AF2, AF3, AF4, AF5, AF6, AF7, AF8, AF9, AF10,

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ pub mod signature;
178178
#[cfg(feature = "device-selected")]
179179
pub mod spi;
180180
#[cfg(feature = "device-selected")]
181+
pub mod syscfg;
182+
#[cfg(feature = "device-selected")]
181183
pub mod time;
182184
#[cfg(feature = "device-selected")]
183185
pub mod timer;

src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ pub use crate::rcc::RccExt as _stm32f4xx_hal_rcc_RccExt;
2222
))
2323
))]
2424
pub use crate::rng::RngExt as _stm32f4xx_hal_rng_RngExt;
25+
pub use crate::syscfg::SysCfgExt as _stm32f4xx_hal_syscfg_SysCfgExt;
2526
pub use crate::time::U32Ext as _stm32f4xx_hal_time_U32Ext;

src/syscfg.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::stm32::{SYSCFG, RCC};
2+
use core::ops::Deref;
3+
use crate::bb;
4+
5+
/// Extension trait that constrains the `SYSCFG` peripheral
6+
pub trait SysCfgExt {
7+
/// Constrains the `SYSCFG` peripheral so it plays nicely with the other abstractions
8+
fn constrain(self) -> SysCfg;
9+
}
10+
11+
impl SysCfgExt for SYSCFG {
12+
fn constrain(self) -> SysCfg {
13+
unsafe {
14+
// NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
15+
let rcc = &(*RCC::ptr());
16+
17+
// Enable clock.
18+
bb::set(&rcc.apb2enr, 14);
19+
}
20+
21+
SysCfg(self)
22+
}
23+
}
24+
25+
pub struct SysCfg(SYSCFG);
26+
27+
impl Deref for SysCfg {
28+
type Target = SYSCFG;
29+
30+
#[inline(always)]
31+
fn deref(&self) -> &Self::Target {
32+
&self.0
33+
}
34+
}

0 commit comments

Comments
 (0)