Skip to content

Commit d76ab4c

Browse files
burrbullusbalbin
authored andcommitted
rcc::Enable and cleans
1 parent 5308cce commit d76ab4c

File tree

18 files changed

+221
-226
lines changed

18 files changed

+221
-226
lines changed

examples/can-echo.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::hal::{
66
gpio::{GpioExt as _, Speed},
77
nb::block,
88
pwr::PwrExt,
9-
rcc::{Config, RccExt, SysClockSrc},
9+
rcc::{Config, RccExt},
1010
stm32::Peripherals,
1111
time::RateExtU32,
1212
};
@@ -49,7 +49,7 @@ fn main() -> ! {
4949

5050
let pwr = dp.PWR.constrain().freeze();
5151
let rcc = dp.RCC.constrain();
52-
let mut rcc = rcc.freeze(Config::new(SysClockSrc::HSE(24.MHz())), pwr);
52+
let mut rcc = rcc.freeze(Config::hse(24.MHz()), pwr);
5353

5454
info!("Split GPIO");
5555

@@ -61,7 +61,7 @@ fn main() -> ! {
6161
let tx = gpiob.pb9.into_alternate().speed(Speed::VeryHigh);
6262

6363
info!("-- Create CAN 1 instance");
64-
let mut can = dp.FDCAN1.fdcan(tx, rx, &rcc);
64+
let mut can = dp.FDCAN1.fdcan(tx, rx, &mut rcc);
6565
can.set_protocol_exception_handling(false);
6666

6767
info!("-- Configure nominal timing");

examples/i2c-mpu6050.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#![no_main]
44
#![no_std]
55

6-
use hal::i2c::Config;
76
use hal::prelude::*;
87
use hal::stm32;
98
use hal::time::{ExtU32, RateExtU32};
@@ -29,7 +28,7 @@ fn main() -> ! {
2928
let sda = gpiob.pb9.into_alternate_open_drain();
3029
let scl = gpiob.pb8.into_alternate_open_drain();
3130

32-
let i2c = dp.I2C1.i2c(sda, scl, Config::new(100.kHz()), &mut rcc);
31+
let i2c = dp.I2C1.i2c(sda, scl, 100.kHz(), &mut rcc);
3332

3433
let mut mpu = Mpu6050::new(i2c);
3534
let mut delay = cp.SYST.delay(&rcc.clocks);

examples/i2c.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#![no_main]
44
#![no_std]
55

6-
use hal::i2c::Config;
76
use hal::prelude::*;
87
use hal::stm32;
98
use hal::time::RateExtU32;
@@ -26,7 +25,7 @@ fn main() -> ! {
2625
let sda = gpiob.pb9.into_alternate_open_drain();
2726
let scl = gpiob.pb8.into_alternate_open_drain();
2827

29-
let mut i2c = dp.I2C1.i2c(sda, scl, Config::new(40.kHz()), &mut rcc);
28+
let mut i2c = dp.I2C1.i2c(sda, scl, 40.kHz(), &mut rcc);
3029
// Alternatively, it is possible to specify the exact timing as follows (see the documentation
3130
// of with_timing() for an explanation of the constant):
3231
//let mut i2c = dp

src/adc/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,8 @@ fn init_adc_common<ADCC: AdcCommonExt>(
4949
) -> AdcCommon<ADCC> {
5050
let cfg = cfg.to_bits(rcc);
5151

52-
unsafe {
53-
let rcc_ptr = &(*stm32::RCC::ptr());
54-
ADCC::enable(rcc_ptr);
55-
ADCC::reset(rcc_ptr);
56-
}
52+
ADCC::enable(rcc);
53+
ADCC::reset(rcc);
5754

5855
// Select system clock as ADC clock source
5956
rcc.rb

src/can.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ where
3333
self,
3434
_tx: TX,
3535
_rx: RX,
36-
rcc: &Rcc,
36+
rcc: &mut Rcc,
3737
) -> fdcan::FdCan<Can<Self>, fdcan::ConfigMode>
3838
where
3939
TX: sealed::Tx<Self>,
4040
RX: sealed::Rx<Self>,
4141
{
42-
Self::enable(&rcc.rb);
42+
Self::enable(rcc);
4343

4444
self.fdcan_unchecked()
4545
}

src/dac.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::gpio::{Analog, PA4, PA5, PA6};
1313
use crate::pac;
1414
use crate::rcc::{self, *};
1515
use crate::stm32::dac1::mcr::HFSEL;
16-
use crate::stm32::RCC;
1716
use embedded_hal::delay::DelayNs;
1817

1918
pub trait DacOut<V> {
@@ -458,15 +457,12 @@ pub trait DacExt: Sized {
458457
}
459458

460459
impl<DAC: Instance> DacExt for DAC {
461-
fn constrain<PINS>(self, _pins: PINS, _rcc: &mut Rcc) -> PINS::Output
460+
fn constrain<PINS>(self, _pins: PINS, rcc: &mut Rcc) -> PINS::Output
462461
where
463462
PINS: Pins<Self>,
464463
{
465-
unsafe {
466-
let rcc_ptr = &(*RCC::ptr());
467-
Self::enable(rcc_ptr);
468-
Self::reset(rcc_ptr);
469-
}
464+
Self::enable(rcc);
465+
Self::reset(rcc);
470466

471467
#[allow(clippy::uninit_assumed_init)]
472468
unsafe {

src/delay.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,12 @@ impl DelayMs<u8> for SystDelay {
119119
}
120120

121121
pub trait DelayExt {
122-
fn delay<T>(&mut self, delay: T)
123-
where
124-
T: Into<MicroSecond>;
122+
fn delay(&mut self, delay: MicroSecond);
125123
}
126124

127125
impl DelayExt for SystDelay {
128-
fn delay<T>(&mut self, delay: T)
129-
where
130-
T: Into<MicroSecond>,
131-
{
132-
self.0.delay_us(delay.into().ticks())
126+
fn delay(&mut self, delay: MicroSecond) {
127+
self.0.delay_us(delay.ticks())
133128
}
134129
}
135130

src/gpio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,8 @@ macro_rules! gpio {
582582
type Parts = Parts;
583583

584584
fn split(self, rcc: &mut Rcc) -> Parts {
585-
Self::enable(&rcc.rb);
586-
Self::reset(&rcc.rb);
585+
Self::enable(rcc);
586+
Self::reset(rcc);
587587

588588
Parts {
589589
$(

src/i2c.rs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::rcc::{Enable, GetBusFreq, Rcc, RccBus, Reset};
1212
feature = "stm32g484"
1313
))]
1414
use crate::stm32::I2C4;
15-
use crate::stm32::{I2C1, I2C2, I2C3, RCC};
15+
use crate::stm32::{I2C1, I2C2, I2C3};
1616
use crate::time::Hertz;
1717
use core::cmp;
1818
use core::convert::TryInto;
@@ -29,12 +29,9 @@ pub struct Config {
2929

3030
impl Config {
3131
/// Creates a default configuration for the given bus frequency.
32-
pub fn new<T>(speed: T) -> Self
33-
where
34-
T: Into<Hertz>,
35-
{
32+
pub fn new(speed: Hertz) -> Self {
3633
Config {
37-
speed: Some(speed.into()),
34+
speed: Some(speed),
3835
timing: None,
3936
analog_filter: true,
4037
digital_filter: 0,
@@ -98,16 +95,17 @@ impl Config {
9895
(psc, scll, sclh, sdadel, scldel)
9996
};
10097

101-
reg.presc()
102-
.set(psc.try_into().unwrap())
103-
.scldel()
104-
.set(scldel)
105-
.sdadel()
106-
.set(sdadel)
107-
.sclh()
108-
.set(sclh.try_into().unwrap())
109-
.scll()
110-
.set(scll.try_into().unwrap())
98+
reg.presc().set(psc.try_into().unwrap());
99+
reg.scldel().set(scldel);
100+
reg.sdadel().set(sdadel);
101+
reg.sclh().set(sclh.try_into().unwrap());
102+
reg.scll().set(scll.try_into().unwrap())
103+
}
104+
}
105+
106+
impl From<Hertz> for Config {
107+
fn from(value: Hertz) -> Self {
108+
Self::new(value)
111109
}
112110
}
113111

@@ -149,7 +147,13 @@ impl embedded_hal::i2c::Error for Error {
149147
}
150148

151149
pub trait I2cExt<I2C> {
152-
fn i2c<SDA, SCL>(self, sda: SDA, scl: SCL, config: Config, rcc: &mut Rcc) -> I2c<I2C, SDA, SCL>
150+
fn i2c<SDA, SCL>(
151+
self,
152+
sda: SDA,
153+
scl: SCL,
154+
config: impl Into<Config>,
155+
rcc: &mut Rcc,
156+
) -> I2c<I2C, SDA, SCL>
153157
where
154158
SDA: SDAPin<I2C>,
155159
SCL: SCLPin<I2C>;
@@ -216,7 +220,7 @@ macro_rules! i2c {
216220
self,
217221
sda: SDA,
218222
scl: SCL,
219-
config: Config,
223+
config: impl Into<Config>,
220224
rcc: &mut Rcc,
221225
) -> I2c<$I2CX, SDA, SCL>
222226
where
@@ -232,17 +236,15 @@ macro_rules! i2c {
232236
SCL: SCLPin<$I2CX>
233237
{
234238
/// Initializes the I2C peripheral.
235-
pub fn $i2cx(i2c: $I2CX, sda: SDA, scl: SCL, config: Config, rcc: &mut Rcc) -> Self
239+
pub fn $i2cx(i2c: $I2CX, sda: SDA, scl: SCL, config: impl Into<Config>, rcc: &mut Rcc) -> Self
236240
where
237241
SDA: SDAPin<$I2CX>,
238242
SCL: SCLPin<$I2CX>,
239243
{
244+
let config = config.into();
240245
// Enable and reset I2C
241-
unsafe {
242-
let rcc_ptr = &(*RCC::ptr());
243-
$I2CX::enable(rcc_ptr);
244-
$I2CX::reset(rcc_ptr);
245-
}
246+
$I2CX::enable(rcc);
247+
$I2CX::reset(rcc);
246248

247249
// Make sure the I2C unit is disabled so we can configure it
248250
i2c.cr1().modify(|_, w| w.pe().clear_bit());
@@ -252,12 +254,9 @@ macro_rules! i2c {
252254

253255
// Enable the I2C processing
254256
i2c.cr1().modify(|_, w| {
255-
w.pe()
256-
.set_bit()
257-
.dnf()
258-
.set(config.digital_filter)
259-
.anfoff()
260-
.bit(!config.analog_filter)
257+
w.pe().set_bit();
258+
w.dnf().set(config.digital_filter);
259+
w.anfoff().bit(!config.analog_filter)
261260
});
262261

263262
I2c { i2c, sda, scl }
@@ -267,9 +266,8 @@ macro_rules! i2c {
267266
pub fn release(self) -> ($I2CX, SDA, SCL) {
268267
// Disable I2C.
269268
unsafe {
270-
let rcc_ptr = &(*RCC::ptr());
271-
$I2CX::reset(rcc_ptr);
272-
$I2CX::disable(rcc_ptr);
269+
$I2CX::reset_unchecked();
270+
$I2CX::disable_unchecked();
273271
}
274272

275273
(self.i2c, self.sda, self.scl)

src/pwm.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ use core::marker::PhantomData;
173173
use core::mem::MaybeUninit;
174174

175175
use crate::stm32::LPTIMER1;
176-
use crate::stm32::RCC;
177176
#[cfg(any(
178177
feature = "stm32g473",
179178
feature = "stm32g474",
@@ -1147,11 +1146,8 @@ macro_rules! tim_hal {
11471146
where
11481147
PINS: Pins<$TIMX, T, U>,
11491148
{
1150-
unsafe {
1151-
let rcc_ptr = &(*RCC::ptr());
1152-
$TIMX::enable(rcc_ptr);
1153-
$TIMX::reset(rcc_ptr);
1154-
}
1149+
$TIMX::enable(rcc);
1150+
$TIMX::reset(rcc);
11551151

11561152
let clk = $TIMX::get_timer_frequency(&rcc.clocks);
11571153

@@ -1189,11 +1185,8 @@ macro_rules! tim_hal {
11891185
where
11901186
PINS: Pins<Self, CHANNEL, COMP>
11911187
{
1192-
unsafe {
1193-
let rcc_ptr = &(*RCC::ptr());
1194-
$TIMX::enable(rcc_ptr);
1195-
$TIMX::reset(rcc_ptr);
1196-
}
1188+
$TIMX::enable(rcc);
1189+
$TIMX::reset(rcc);
11971190

11981191
let clk = $TIMX::get_timer_frequency(&rcc.clocks).raw();
11991192

@@ -1801,11 +1794,8 @@ macro_rules! lptim_hal {
18011794
where
18021795
PINS: Pins<$TIMX, T, U>,
18031796
{
1804-
unsafe {
1805-
let rcc_ptr = &(*RCC::ptr());
1806-
$TIMX::enable(rcc_ptr);
1807-
$TIMX::reset(rcc_ptr);
1808-
}
1797+
$TIMX::enable(rcc);
1798+
$TIMX::reset(rcc);
18091799

18101800
let clk = $TIMX::get_timer_frequency(&rcc.clocks);
18111801
let reload = clk / freq;

0 commit comments

Comments
 (0)