Skip to content

Commit 5156884

Browse files
authored
Merge pull request #836 from stm32-rs/rccbits
use &mut RCC
2 parents 0584201 + f2d333a commit 5156884

Some content is hidden

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

85 files changed

+1079
-1099
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
- Implement `embedded_hal::i2c::I2c` for `I2cMasterDma` [#838]
1111
- Back to `stm32f4`
1212
- Implement `Ptr`, `Sealed`, `Steal` for generic `Periph` [#834]
13+
- Use `&mut RCC` for `PER::enable/reset`
1314
- Unmacro `Adc` [#832]
1415
- Use `write` instead of `modify` to clear flags [#829]
1516
- Bump `stm32f4-staging` to 0.18, update other dependencies [#831]

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

Lines changed: 18 additions & 19 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::Config};
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,14 +84,12 @@ 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();
87+
let mut rcc = setup_clocks(dp.RCC);
8888

89-
let clocks = setup_clocks(rcc);
89+
let mut syscfg = dp.SYSCFG.constrain(&mut rcc);
9090

91-
let mut syscfg = dp.SYSCFG.constrain();
92-
93-
let gpioa = dp.GPIOA.split();
94-
let gpioe = dp.GPIOE.split();
91+
let gpioa = dp.GPIOA.split(&mut rcc);
92+
let gpioe = dp.GPIOE.split(&mut rcc);
9593

9694
let mut board_btn = gpioa.pa0.into_pull_down_input();
9795
board_btn.make_interrupt_source(&mut syscfg);
@@ -117,17 +115,17 @@ fn main() -> ! {
117115
phase: Phase::CaptureOnFirstTransition,
118116
},
119117
2000.kHz(),
120-
&clocks,
118+
&mut rcc,
121119
);
122120

123121
// Set up the LEDs. On the stm32f429i-disco they are connected to pin PG13 and PG14.
124-
let gpiog = dp.GPIOG.split();
122+
let gpiog = dp.GPIOG.split(&mut rcc);
125123
let mut led3 = gpiog.pg13.into_push_pull_output();
126124
let mut led4 = gpiog.pg14.into_push_pull_output();
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, &mut rcc).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+
Config::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: 4 additions & 5 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::Config};
1212

1313
use crate::hal::{
1414
gpio::{self, Output, PushPull},
@@ -66,19 +66,18 @@ 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 mut rcc = dp.RCC.freeze(Config::hsi().sysclk(16.MHz()).pclk1(8.MHz()));
7170

7271
// Configure PA5 pin to blink LED
73-
let gpioa = dp.GPIOA.split();
72+
let gpioa = dp.GPIOA.split(&mut rcc);
7473
let mut led = gpioa.pa5.into_push_pull_output();
7574
led.set_high(); // Turn off
7675

7776
// Move the pin into our global storage
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(&mut rcc);
8281
timer.start(1.secs()).unwrap();
8382

8483
// Generate an interrupt when the timer expires

examples/blinky.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use cortex_m_rt::entry;
1616
fn main() -> ! {
1717
let p = pac::Peripherals::take().unwrap();
1818

19-
let gpioc = p.GPIOC.split();
19+
let mut rcc = p.RCC.constrain();
20+
21+
let gpioc = p.GPIOC.split(&mut rcc);
2022
let mut led = gpioc.pc13.into_push_pull_output();
2123

2224
loop {

examples/can-send.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,26 @@ 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::Config;
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 mut rcc = dp.RCC.freeze(Config::hse(8.MHz()));
2524

26-
let gpiob = dp.GPIOB.split();
25+
let gpiob = dp.GPIOB.split(&mut rcc);
2726
let mut can1 = {
2827
let rx = gpiob.pb8;
2928
let tx = gpiob.pb9;
3029

3130
// let can = Can::new(dp.CAN1, (tx, rx));
3231
// or
33-
let can = dp.CAN1.can((tx, rx));
32+
let can = dp.CAN1.can((tx, rx), &mut rcc);
3433

3534
bxcan::Can::builder(can)
3635
// APB1 (PCLK1): 8MHz, Bit rate: 500kBit/s, Sample Point 87.5%
@@ -47,7 +46,7 @@ fn main() -> ! {
4746
let tx = gpiob.pb13;
4847
let rx = gpiob.pb12;
4948

50-
let can = dp.CAN2.can((tx, rx));
49+
let can = dp.CAN2.can((tx, rx), &mut rcc);
5150

5251
let can2 = bxcan::Can::builder(can)
5352
// APB1 (PCLK1): 8MHz, Bit rate: 500kBit/s, Sample Point 87.5%

examples/delay-syst-blinky.rs

Lines changed: 6 additions & 7 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::Config};
1313

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

@@ -19,16 +19,15 @@ fn main() -> ! {
1919
pac::Peripherals::take(),
2020
cortex_m::peripheral::Peripherals::take(),
2121
) {
22+
// Set up the system clock. We want to run at 48MHz for this one.
23+
let mut rcc = dp.RCC.freeze(Config::hsi().sysclk(48.MHz()));
24+
2225
// Set up the LED. On the Nucleo-446RE it's connected to pin PA5.
23-
let gpioa = dp.GPIOA.split();
26+
let gpioa = dp.GPIOA.split(&mut rcc);
2427
let mut led = gpioa.pa5.into_push_pull_output();
2528

26-
// 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();
29-
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: 6 additions & 7 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::Config};
1313

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

@@ -19,16 +19,15 @@ fn main() -> ! {
1919
pac::Peripherals::take(),
2020
cortex_m::peripheral::Peripherals::take(),
2121
) {
22+
// Set up the system clock. We want to run at 48MHz for this one.
23+
let mut rcc = dp.RCC.freeze(Config::hse(25.MHz()).sysclk(48.MHz()));
24+
2225
// Set up the LED. On the Mini-F4 it's connected to pin PC13.
23-
let gpioc = dp.GPIOC.split();
26+
let gpioc = dp.GPIOC.split(&mut rcc);
2427
let mut led = gpioc.pc13.into_push_pull_output();
2528

26-
// 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();
29-
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(&mut rcc);
3231

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

examples/display-touch.rs

Lines changed: 11 additions & 13 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::Config,
2626
};
2727

2828
use embedded_graphics_07::{
@@ -53,17 +53,15 @@ 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();
56+
let mut rcc = p.RCC.freeze(Config::hsi().sysclk(100.MHz()));
57+
let mut delay = cp.SYST.delay(&rcc.clocks);
5758

58-
let clocks = rcc.cfgr.sysclk(100.MHz()).freeze();
59-
let mut delay = cp.SYST.delay(&clocks);
60-
61-
let gpiob = p.GPIOB.split();
62-
let gpioc = p.GPIOC.split();
63-
let gpiod = p.GPIOD.split();
64-
let gpioe = p.GPIOE.split();
65-
let gpiof = p.GPIOF.split();
66-
let gpiog = p.GPIOG.split();
59+
let gpiob = p.GPIOB.split(&mut rcc);
60+
let gpioc = p.GPIOC.split(&mut rcc);
61+
let gpiod = p.GPIOD.split(&mut rcc);
62+
let gpioe = p.GPIOE.split(&mut rcc);
63+
let gpiof = p.GPIOF.split(&mut rcc);
64+
let gpiog = p.GPIOG.split(&mut rcc);
6765

6866
// Pins connected to the LCD on the board
6967
use stm32f4xx_hal::gpio::alt::fsmc as alt;
@@ -122,7 +120,7 @@ fn main() -> ! {
122120
let read_timing = Timing::default().data(8).address_setup(8).bus_turnaround(0);
123121

124122
// Initialise FSMC memory provider
125-
let (_fsmc, interface) = FsmcLcd::new(p.FSMC, lcd_pins, &read_timing, &write_timing);
123+
let (_fsmc, interface) = FsmcLcd::new(p.FSMC, lcd_pins, &read_timing, &write_timing, &mut rcc);
126124

127125
// Pass display-interface instance ST7789 driver to setup a new display
128126
let mut disp = ST7789::new(
@@ -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(), &mut rcc) };
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: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,24 @@ 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::Config};
1515

1616
#[entry]
1717
fn main() -> ! {
1818
if let (Some(dp), Some(cp)) = (
1919
pac::Peripherals::take(),
2020
cortex_m::peripheral::Peripherals::take(),
2121
) {
22+
// Set up the system clock. We want to run at 48MHz for this one.
23+
let mut rcc = dp.RCC.freeze(Config::hsi().sysclk(48.MHz()));
24+
2225
// Set up the LEDs. On the STM32F429I-DISC[O1] they are connected to pin PG13/14.
23-
let gpiog = dp.GPIOG.split();
26+
let gpiog = dp.GPIOG.split(&mut rcc);
2427
let mut led1 = gpiog.pg13.into_push_pull_output();
2528
let mut led2 = gpiog.pg14.into_push_pull_output();
2629

27-
// 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();
30-
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: 3 additions & 5 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 mut rcc = dp.RCC.constrain();
2624

2725
// Acquire the GPIOC peripheral
28-
let gpioc = dp.GPIOC.split();
26+
let gpioc = dp.GPIOC.split(&mut rcc);
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

0 commit comments

Comments
 (0)