Skip to content

Fix SysCfg hardfault, Add I2C Fast Mode Plus enable method #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Embed.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[default.rtt]
enabled = true
23 changes: 18 additions & 5 deletions examples/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ use stm32g4xx_hal::{
//delay::{DelayExt, SYSTDelayExt},
gpio::{self, ExtiPin, GpioExt, Input, SignalEdge},
rcc::RccExt,
stm32,
stm32::{interrupt, Interrupt},
stm32::{self, interrupt, Interrupt},
syscfg::SysCfgExt,
};

use core::cell::RefCell;
use core::sync::atomic::{AtomicBool, Ordering};
use cortex_m::{asm::wfi, interrupt::Mutex};
use cortex_m::interrupt::Mutex;
use cortex_m_rt::entry;

type ButtonPin = gpio::PC13<Input>;
Expand Down Expand Up @@ -52,8 +51,22 @@ fn main() -> ! {
utils::logger::init();

let mut dp = stm32::Peripherals::take().expect("cannot take peripherals");

// Workaround for RTT when using wfi instruction
// Enable the debug sleep bits in DBGMCU,
// then enable DMA peripheral clock in AHB1ENR
dp.DBGMCU.cr().modify(|_, w| {
w.dbg_sleep().set_bit();
w.dbg_stop().set_bit();
w.dbg_standby().set_bit()
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probe-rs should do it automatically on connect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@boondocklabs , if the DMA enable thing is enough, then how about we do only that? (Same for #214 i presume)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the DBGMCU modification, and left the DMA enable in the button example. Tested on a G431KB without explicit DGBMCU modification and it does not hardfault as long as the DMA enable workaround is there.


let mut rcc = dp.RCC.constrain();
let mut syscfg = dp.SYSCFG.constrain();

// Enable an AHB peripheral clock for debug probe with wfi
rcc.ahb1enr().modify(|_, w| w.dma1en().set_bit());

let mut syscfg = dp.SYSCFG.constrain(&mut rcc);

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

println!("Start Loop");
loop {
wfi();
cortex_m::asm::wfi();
println!("Check");

if G_LED_ON.load(Ordering::Relaxed) {
Expand Down
20 changes: 6 additions & 14 deletions src/syscfg.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
use crate::bb;
use crate::stm32::{RCC, SYSCFG};
use crate::rcc::Rcc;
use crate::stm32::SYSCFG;
use core::ops::Deref;

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

impl SysCfgExt for SYSCFG {
fn constrain(self) -> SysCfg {
unsafe {
// NOTE(unsafe) this reference will only be used for atomic writes with no side effects.
let rcc = &(*RCC::ptr());

// Enable clock.
bb::set(&rcc.apb2enr(), 0);

// Stall the pipeline to work around erratum 2.1.13 (DM00037591)
cortex_m::asm::dsb();
}
fn constrain(self, rcc: &mut Rcc) -> SysCfg {
// Enable SYSCFG peripheral clock in APB2ENR register
rcc.apb2enr().modify(|_, w| w.syscfgen().set_bit());

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not tinkered with the SysCfg, but is there any reason to handle this different from for example how the usarts are enabled. So something like this?

SYSCFG::enable(rcc);
SYSCFG::reset(rcc);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a change to use Enable and Reset traits rather than direct register access

SysCfg(self)
}
Expand Down
Loading