Skip to content

Commit 0bd0c7e

Browse files
committed
Rcc inspired by g0/g4xx-hal
1 parent 78df546 commit 0bd0c7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+351
-346
lines changed

examples/analog-stopwatch-with-spi-ssd1306.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
#![no_main]
99

1010
use panic_semihosting as _;
11-
use stm32f4xx_hal as hal;
11+
use stm32f4xx_hal::{self as hal, rcc::CFGR};
1212

1313
use crate::hal::{
1414
gpio::{Edge, Input, PA0},
1515
interrupt, pac,
1616
prelude::*,
17-
rcc::{Clocks, Rcc},
17+
rcc::Rcc,
1818
spi::{Mode, Phase, Polarity, Spi},
1919
timer::{CounterUs, Event, FTimer, Flag, Timer},
2020
};
@@ -84,9 +84,7 @@ fn main() -> ! {
8484
let cp = cortex_m::peripheral::Peripherals::take().unwrap();
8585
dp.RCC.apb2enr().write(|w| w.syscfgen().enabled());
8686

87-
let rcc = dp.RCC.constrain();
88-
89-
let clocks = setup_clocks(rcc);
87+
let rcc = setup_clocks(dp.RCC);
9088

9189
let mut syscfg = dp.SYSCFG.constrain();
9290

@@ -117,7 +115,7 @@ fn main() -> ! {
117115
phase: Phase::CaptureOnFirstTransition,
118116
},
119117
2000.kHz(),
120-
&clocks,
118+
&rcc.clocks,
121119
);
122120

123121
// Set up the LEDs. On the stm32f429i-disco they are connected to pin PG13 and PG14.
@@ -127,7 +125,7 @@ fn main() -> ! {
127125

128126
let dc = gpioe.pe3.into_push_pull_output();
129127
let mut ss = gpioe.pe4.into_push_pull_output();
130-
let mut delay = Timer::syst(cp.SYST, &clocks).delay();
128+
let mut delay = Timer::syst(cp.SYST, &rcc.clocks).delay();
131129

132130
ss.set_high();
133131
delay.delay_ms(100);
@@ -142,7 +140,7 @@ fn main() -> ! {
142140
disp.flush().unwrap();
143141

144142
// Create a 1ms periodic interrupt from TIM2
145-
let mut timer = FTimer::new(dp.TIM2, &clocks).counter();
143+
let mut timer = FTimer::new(dp.TIM2, &rcc.clocks).counter();
146144
timer.start(1.secs()).unwrap();
147145
timer.listen(Event::Update);
148146

@@ -223,13 +221,14 @@ fn main() -> ! {
223221
}
224222
}
225223

226-
fn setup_clocks(rcc: Rcc) -> Clocks {
227-
rcc.cfgr
228-
.hclk(180.MHz())
229-
.sysclk(180.MHz())
230-
.pclk1(45.MHz())
231-
.pclk2(90.MHz())
232-
.freeze()
224+
fn setup_clocks(rcc: pac::RCC) -> Rcc {
225+
rcc.freeze(
226+
CFGR::hsi()
227+
.hclk(180.MHz())
228+
.sysclk(180.MHz())
229+
.pclk1(45.MHz())
230+
.pclk2(90.MHz()),
231+
)
233232
}
234233

235234
#[interrupt]

examples/blinky-timer-irq.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
use panic_halt as _;
1010

11-
use stm32f4xx_hal as hal;
11+
use stm32f4xx_hal::{self as hal, rcc::CFGR};
1212

1313
use crate::hal::{
1414
gpio::{self, Output, PushPull},
@@ -66,8 +66,7 @@ fn TIM2() {
6666
fn main() -> ! {
6767
let dp = Peripherals::take().unwrap();
6868

69-
let rcc = dp.RCC.constrain();
70-
let clocks = rcc.cfgr.sysclk(16.MHz()).pclk1(8.MHz()).freeze();
69+
let rcc = dp.RCC.freeze(CFGR::hsi().sysclk(16.MHz()).pclk1(8.MHz()));
7170

7271
// Configure PA5 pin to blink LED
7372
let gpioa = dp.GPIOA.split();
@@ -78,7 +77,7 @@ fn main() -> ! {
7877
cortex_m::interrupt::free(|cs| *G_LED.borrow(cs).borrow_mut() = Some(led));
7978

8079
// Set up a timer expiring after 1s
81-
let mut timer = dp.TIM2.counter(&clocks);
80+
let mut timer = dp.TIM2.counter(&rcc.clocks);
8281
timer.start(1.secs()).unwrap();
8382

8483
// Generate an interrupt when the timer expires

examples/can-send.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ use bxcan::filter::Mask32;
1010
use bxcan::{Fifo, Frame, StandardId};
1111
use cortex_m_rt::entry;
1212
use nb::block;
13+
use stm32f4xx_hal::rcc::CFGR;
1314
use stm32f4xx_hal::{pac, prelude::*};
1415

1516
#[entry]
1617
fn main() -> ! {
1718
let dp = pac::Peripherals::take().unwrap();
1819

19-
let rcc = dp.RCC.constrain();
20-
2120
// To meet CAN clock accuracy requirements an external crystal or ceramic
2221
// resonator must be used. The blue pill has a 8MHz external crystal.
2322
// Other boards might have a crystal with another frequency or none at all.
24-
rcc.cfgr.use_hse(8.MHz()).freeze();
23+
let _rcc = dp.RCC.freeze(CFGR::hse(8.MHz()));
2524

2625
let gpiob = dp.GPIOB.split();
2726
let mut can1 = {

examples/delay-syst-blinky.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use panic_halt as _; // panic handler
1010

1111
use cortex_m_rt::entry;
12-
use stm32f4xx_hal as hal;
12+
use stm32f4xx_hal::{self as hal, rcc::CFGR};
1313

1414
use crate::hal::{pac, prelude::*};
1515

@@ -24,11 +24,10 @@ fn main() -> ! {
2424
let mut led = gpioa.pa5.into_push_pull_output();
2525

2626
// Set up the system clock. We want to run at 48MHz for this one.
27-
let rcc = dp.RCC.constrain();
28-
let clocks = rcc.cfgr.sysclk(48.MHz()).freeze();
27+
let rcc = dp.RCC.freeze(CFGR::hsi().sysclk(48.MHz()));
2928

3029
// Create a delay abstraction based on SysTick
31-
let mut delay = cp.SYST.delay(&clocks);
30+
let mut delay = cp.SYST.delay(&rcc.clocks);
3231

3332
loop {
3433
// On for 1s, off for 1s.

examples/delay-timer-blinky.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use panic_halt as _; // panic handler
1010

1111
use cortex_m_rt::entry;
12-
use stm32f4xx_hal as hal;
12+
use stm32f4xx_hal::{self as hal, rcc::CFGR};
1313

1414
use crate::hal::{pac, prelude::*};
1515

@@ -24,11 +24,10 @@ fn main() -> ! {
2424
let mut led = gpioc.pc13.into_push_pull_output();
2525

2626
// Set up the system clock. We want to run at 48MHz for this one.
27-
let rcc = dp.RCC.constrain();
28-
let clocks = rcc.cfgr.use_hse(25.MHz()).sysclk(48.MHz()).freeze();
27+
let rcc = dp.RCC.freeze(CFGR::hse(25.MHz()).sysclk(48.MHz()));
2928

3029
// Create a delay abstraction based on general-pupose 32-bit timer TIM5
31-
let mut delay = dp.TIM5.delay_us(&clocks);
30+
let mut delay = dp.TIM5.delay_us(&rcc.clocks);
3231

3332
loop {
3433
// On for 1s, off for 3s.

examples/display-touch.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use stm32f4xx_hal::{
2222
gpio::Speed,
2323
pac,
2424
prelude::*,
25-
rcc::Rcc,
25+
rcc::CFGR,
2626
};
2727

2828
use embedded_graphics_07::{
@@ -53,10 +53,8 @@ fn main() -> ! {
5353
let p = pac::Peripherals::take().unwrap();
5454
let cp = cortex_m::Peripherals::take().unwrap();
5555

56-
let rcc: Rcc = p.RCC.constrain();
57-
58-
let clocks = rcc.cfgr.sysclk(100.MHz()).freeze();
59-
let mut delay = cp.SYST.delay(&clocks);
56+
let rcc = p.RCC.freeze(CFGR::hsi().sysclk(100.MHz()));
57+
let mut delay = cp.SYST.delay(&rcc.clocks);
6058

6159
let gpiob = p.GPIOB.split();
6260
let gpioc = p.GPIOC.split();
@@ -148,7 +146,7 @@ fn main() -> ! {
148146
// STM32F412 uses I2c1 type for i2c bus.
149147
// The pins are mentioned in documentation -um2135-discovery-kit-with-stm32f412zg-mcu-stmicroelectronics
150148
#[cfg(feature = "stm32f412")]
151-
let mut i2c = { I2c::new(p.I2C1, (gpiob.pb6, gpiob.pb7), 400.kHz(), &clocks) };
149+
let mut i2c = { I2c::new(p.I2C1, (gpiob.pb6, gpiob.pb7), 400.kHz(), &rcc.clocks) };
152150

153151
// STM32F413 uses FMPI2C1 type.
154152
// The pins are mentioned in documentation -um2135-discovery-kit-with-stm32f413zh-mcu-stmicroelectronics

examples/dwt-blinky.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::hal::{
1111
};
1212
use cortex_m_rt::entry;
1313
use panic_halt as _;
14-
use stm32f4xx_hal as hal;
14+
use stm32f4xx_hal::{self as hal, rcc::CFGR};
1515

1616
#[entry]
1717
fn main() -> ! {
@@ -25,11 +25,10 @@ fn main() -> ! {
2525
let mut led2 = gpiog.pg14.into_push_pull_output();
2626

2727
// Set up the system clock. We want to run at 48MHz for this one.
28-
let rcc = dp.RCC.constrain();
29-
let clocks = rcc.cfgr.sysclk(48.MHz()).freeze();
28+
let rcc = dp.RCC.freeze(CFGR::hsi().sysclk(48.MHz()));
3029

3130
// Create a delay abstraction based on DWT cycle counter
32-
let dwt = cp.DWT.constrain(cp.DCB, &clocks);
31+
let dwt = cp.DWT.constrain(cp.DCB, &rcc.clocks);
3332
let mut delay = dwt.delay();
3433

3534
// Create a stopwatch for maximum 9 laps

examples/dynamic-gpio.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@ fn main() -> ! {
1818
let dp = pac::Peripherals::take().unwrap();
1919

2020
// Take ownership over raw device and convert it into the corresponding HAL struct
21-
let rcc = dp.RCC.constrain();
22-
2321
// Freeze the configuration of all the clocks in the system and store the frozen frequencies in
2422
// `clocks`
25-
let clocks = rcc.cfgr.freeze();
23+
let rcc = dp.RCC.constrain();
2624

2725
// Acquire the GPIOC peripheral
2826
let gpioc = dp.GPIOC.split();
2927

3028
let mut pin = gpioc.pc13.into_dynamic();
3129
// Configure the syst timer to trigger an update every second
32-
let mut timer = Timer::syst(cp.SYST, &clocks).counter_us();
30+
let mut timer = Timer::syst(cp.SYST, &rcc.clocks).counter_us();
3331
timer.start(1.secs()).unwrap();
3432

3533
// Wait for the timer to trigger an update and change the state of the LED

examples/f413disco-lcd-ferris.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use panic_halt as _;
1414
use rtt_target::{self, rtt_init_print, ChannelMode};
1515

16-
use stm32f4xx_hal as hal;
16+
use stm32f4xx_hal::{self as hal, rcc::CFGR};
1717

1818
use crate::hal::{
1919
fsmc_lcd::{DataPins16, FsmcLcd, LcdPins, Timing},
@@ -705,8 +705,7 @@ fn main() -> ! {
705705
let gpiog = p.GPIOG.split();
706706

707707
// Configure and lock the clocks at maximum warp
708-
let rcc = p.RCC.constrain();
709-
let clocks = rcc.cfgr.sysclk(100.MHz()).freeze();
708+
let rcc = p.RCC.freeze(CFGR::hsi().sysclk(100.MHz()));
710709

711710
// Define the pins we need for our 16bit parallel bus
712711
use stm32f4xx_hal::gpio::alt::fsmc as alt;
@@ -729,7 +728,7 @@ fn main() -> ! {
729728
let mut _te = gpiob.pb14.into_floating_input();
730729

731730
// Get delay provider
732-
let mut delay = cp.SYST.delay(&clocks);
731+
let mut delay = cp.SYST.delay(&rcc.clocks);
733732

734733
// Set up timing
735734
let write_timing = Timing::default().data(3).address_setup(3).bus_turnaround(0);

examples/f469disco-lcd-test.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use cortex_m_rt::entry;
1414
use defmt_rtt as _;
1515
use panic_probe as _;
1616

17-
use stm32f4xx_hal as hal;
17+
use stm32f4xx_hal::{self as hal, rcc::CFGR};
1818

1919
use crate::hal::{
2020
dsi::{
@@ -52,16 +52,11 @@ fn main() -> ! {
5252
let dp = Peripherals::take().unwrap();
5353
let cp = CorePeripherals::take().unwrap();
5454

55-
let rcc = dp.RCC.constrain();
56-
5755
let hse_freq = 8.MHz();
58-
let clocks = rcc
59-
.cfgr
60-
.use_hse(hse_freq)
61-
.pclk2(32.MHz())
62-
.sysclk(180.MHz())
63-
.freeze();
64-
let mut delay = cp.SYST.delay(&clocks);
56+
let rcc = dp
57+
.RCC
58+
.freeze(CFGR::hse(hse_freq).pclk2(32.MHz()).sysclk(180.MHz()));
59+
let mut delay = cp.SYST.delay(&rcc.clocks);
6560

6661
let gpioh = dp.GPIOH.split();
6762

@@ -112,7 +107,7 @@ fn main() -> ! {
112107
DISPLAY_CONFIGURATION,
113108
dsi_config,
114109
dp.DSI,
115-
&clocks,
110+
&rcc.clocks,
116111
)
117112
.unwrap();
118113

0 commit comments

Comments
 (0)