-
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
ced789f
557637a
033b58a
6abbaef
bac7b62
6c8253a
b9220ed
15f7fab
a6369ca
eb3aef7
18754c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 rand --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 { | ||
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. Do you think it would make sense and be possible to use the HSI so as not to require an external clock source? |
||
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"), | ||
}, | ||
} | ||
} | ||
} |
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.
This is not the
rand
example :)