Skip to content

Commit ced789f

Browse files
committed
Fix SysCfg hardfault
The `SysCfg` peripheral used bit banding to set the APB2 peripheral clock enable bit which fails on an assertion on a G431KBT (have not tested on other chips, but I suspect they would do the same). This takes a mutable reference to `Rcc` in `SysCfg::constrain` and uses safe accessors to enable the clock. Updated and tested the button example which is all that uses SysCfg. ```[INFO ] Configuring PLL (stm32_foc stm32-foc/src/main.rs:132) [INFO ] System clock frequency: 168000000 (stm32_foc stm32-foc/src/main.rs:138) [DEBUG] Write 20007FB0 (stm32g4xx_hal stm32g4xx-hal/src/bb.rs:42) [ERROR] panicked at /Users/fuzz/wave/stm32g4xx-hal/src/bb.rs:44:5: assertion failed: (PERI_ADDRESS_START..=PERI_ADDRESS_END).contains(&addr) (panic_probe panic-probe-1.0.0/src/lib.rs:104) Firmware exited unexpectedly: Multiple Core 0 Frame 0: HardFault_ @ 0x08006394 /Users/fuzz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/cortex-m-rt-0.7.5/src/lib.rs:1103:1 Frame 1: HardFault <Cause: Escalated UsageFault <Cause: Undefined instruction>> @ 0x08005ce2```
1 parent ee7c010 commit ced789f

File tree

3 files changed

+10
-16
lines changed

3 files changed

+10
-16
lines changed

Embed.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[default.rtt]
2+
enabled = true

examples/button.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn main() -> ! {
5353

5454
let mut dp = stm32::Peripherals::take().expect("cannot take peripherals");
5555
let mut rcc = dp.RCC.constrain();
56-
let mut syscfg = dp.SYSCFG.constrain();
56+
let mut syscfg = dp.SYSCFG.constrain(&mut rcc);
5757

5858
println!("Led Init");
5959
// Configure PA5 pin to blink LED
@@ -80,7 +80,7 @@ fn main() -> ! {
8080

8181
println!("Start Loop");
8282
loop {
83-
wfi();
83+
//wfi();
8484
println!("Check");
8585

8686
if G_LED_ON.load(Ordering::Relaxed) {

src/syscfg.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
1-
use crate::bb;
2-
use crate::stm32::{RCC, SYSCFG};
1+
use crate::rcc::Rcc;
2+
use crate::stm32::SYSCFG;
33
use core::ops::Deref;
44

55
/// Extension trait that constrains the `SYSCFG` peripheral
66
pub trait SysCfgExt {
77
/// Constrains the `SYSCFG` peripheral so it plays nicely with the other abstractions
8-
fn constrain(self) -> SysCfg;
8+
fn constrain(self, rcc: &mut Rcc) -> SysCfg;
99
}
1010

1111
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(), 0);
19-
20-
// Stall the pipeline to work around erratum 2.1.13 (DM00037591)
21-
cortex_m::asm::dsb();
22-
}
12+
fn constrain(self, rcc: &mut Rcc) -> SysCfg {
13+
// Enable SYSCFG peripheral clock in APB2ENR register
14+
rcc.apb2enr().modify(|_, w| w.syscfgen().set_bit());
2315

2416
SysCfg(self)
2517
}

0 commit comments

Comments
 (0)