-
Notifications
You must be signed in to change notification settings - Fork 51
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
boondocklabs
wants to merge
11
commits into
stm32-rs:main
Choose a base branch
from
boondocklabs:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ced789f
Fix SysCfg hardfault
boondocklabs 557637a
Update button with workaround for wfi() and debug probes
boondocklabs 033b58a
Remove unused import
boondocklabs 6abbaef
Merge branch 'stm32-rs:main' into main
boondocklabs bac7b62
Remove DBGMCU register modification from button example which is set …
boondocklabs 6c8253a
Merge branch 'stm32-rs:main' into main
boondocklabs b9220ed
Merge branch 'stm32-rs:main' into main
boondocklabs 15f7fab
Use rcc Enable and Reset traits rather than direct register access
boondocklabs a6369ca
Merge branch 'main' of https://github.com/boondocklabs/stm32g4xx-hal
boondocklabs eb3aef7
Add I2C fast mode plus enable function to SysCfg. Added I2C fast mode…
boondocklabs 18754c3
Fix typos
boondocklabs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[default.rtt] | ||
enabled = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
//! I2C Fast Mode Plus example with an AS5600 magnetic angle sensor. | ||
//! | ||
//! This example expects the AS5600 to be connected to the I2C bus on PB7 (SDA) and PA15 (SCL), | ||
//! and a 24MHz HSE oscillator to configure the PLL for 168MHz system clock. | ||
//! | ||
//! The I2C bus is configured with Fast Mode Plus (FMP) enabled in SysCfg, and a 1MHz I2C clock rate. | ||
//! | ||
//! ```DEFMT_LOG=debug cargo run --release --example i2c-fmp-as5600 --features stm32g431,defmt -- --chip STM32G431KBTx``` | ||
|
||
#![deny(warnings)] | ||
#![deny(unsafe_code)] | ||
#![no_main] | ||
#![no_std] | ||
|
||
use fugit::HertzU32 as Hertz; | ||
use hal::prelude::*; | ||
use hal::rcc::SysClockSrc; | ||
use hal::stm32; | ||
use hal::time::RateExtU32; | ||
use stm32g4xx_hal as hal; | ||
use stm32g4xx_hal::syscfg::SysCfgExt; | ||
|
||
use as5600::As5600; | ||
use cortex_m_rt::entry; | ||
|
||
#[macro_use] | ||
mod utils; | ||
use utils::logger::error; | ||
use utils::logger::info; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
utils::logger::init(); | ||
|
||
let dp = stm32::Peripherals::take().expect("cannot take peripherals"); | ||
//let cp = cortex_m::Peripherals::take().expect("cannot take core peripherals"); | ||
|
||
let pwr_cfg = dp | ||
.PWR | ||
.constrain() | ||
.vos(stm32g4xx_hal::pwr::VoltageScale::Range1 { enable_boost: true }) | ||
.freeze(); | ||
|
||
let pll_cfg = hal::rcc::PllConfig { | ||
mux: hal::rcc::PllSrc::HSE(Hertz::MHz(24)), | ||
m: hal::rcc::PllMDiv::DIV_2, | ||
n: hal::rcc::PllNMul::MUL_28, | ||
r: Some(hal::rcc::PllRDiv::DIV_2), | ||
q: None, | ||
p: None, | ||
}; | ||
|
||
info!("Configuring PLL"); | ||
let rcc_cfg = hal::rcc::Config::new(SysClockSrc::PLL) | ||
.boost(true) | ||
.pll_cfg(pll_cfg); | ||
|
||
let mut rcc = dp.RCC.freeze(rcc_cfg, pwr_cfg); | ||
info!("System clock frequency: {}", rcc.clocks.sys_clk.to_Hz()); | ||
|
||
let gpioa = dp.GPIOA.split(&mut rcc); | ||
let gpiob = dp.GPIOB.split(&mut rcc); | ||
|
||
let sda = gpiob.pb7.into_alternate_open_drain(); | ||
let scl = gpioa.pa15.into_alternate_open_drain(); | ||
|
||
let mut syscfg = dp.SYSCFG.constrain(&mut rcc); | ||
|
||
// Enable Fast Mode Plus for I2C1 | ||
syscfg.i2c_fmp_enable::<1>(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does the user figure out if they should pass a |
||
|
||
// Configure I2C for 1MHz | ||
let i2c = dp.I2C1.i2c(sda, scl, 1.MHz(), &mut rcc); | ||
|
||
let mut as5600 = As5600::new(i2c); | ||
|
||
loop { | ||
match as5600.angle() { | ||
Ok(angle) => { | ||
// Convert angle to degrees | ||
let angle_degrees = angle as f32 * 360.0 / 4096.0; | ||
info!("Angle: {}°", angle_degrees); | ||
} | ||
Err(e) => match e { | ||
as5600::error::Error::Communication(_) => error!("I2C communication error"), | ||
as5600::error::Error::Status(_error) => error!("AS5600 status error"), | ||
as5600::error::Error::Configuration(_error) => error!("AS5600 configuration error"), | ||
_ => error!("Other AS5600 error"), | ||
}, | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it would make sense and be possible to use the HSI so as not to require an external clock source?