Skip to content

Commit 3f853e0

Browse files
committed
use &mut RCC
1 parent 34ec683 commit 3f853e0

File tree

4 files changed

+32
-31
lines changed

4 files changed

+32
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
## [Unreleased]
99

1010
- Implement `Ptr`, `Sealed`, `Steal` for generic `Periph` [#834]
11+
- Use `&mut RCC` for `PER::enable/reset`
1112
- Unmacro `Adc` [#832]
1213
- Use `write` instead of `modify` to clear flags [#829]
1314
- Bump `stm32f4-staging` to 0.18, update other dependencies [#831]

src/rcc/f4/enable.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ macro_rules! bus_enable {
55
($PER:ident => $bit:literal) => {
66
impl Enable for crate::pac::$PER {
77
#[inline(always)]
8-
fn enable(rcc: &RccRB) {
8+
fn enable(rcc: &mut RCC) {
99
unsafe {
1010
bb::set(Self::Bus::enr(rcc), $bit);
1111
}
1212
// Stall the pipeline to work around erratum 2.1.13 (DM00037591)
1313
cortex_m::asm::dsb();
1414
}
1515
#[inline(always)]
16-
fn disable(rcc: &RccRB) {
16+
fn disable(rcc: &mut RCC) {
1717
unsafe {
1818
bb::clear(Self::Bus::enr(rcc), $bit);
1919
}
@@ -30,15 +30,15 @@ macro_rules! bus_lpenable {
3030
($PER:ident => $bit:literal) => {
3131
impl LPEnable for crate::pac::$PER {
3232
#[inline(always)]
33-
fn enable_in_low_power(rcc: &RccRB) {
33+
fn enable_in_low_power(rcc: &mut RCC) {
3434
unsafe {
3535
bb::set(Self::Bus::lpenr(rcc), $bit);
3636
}
3737
// Stall the pipeline to work around erratum 2.1.13 (DM00037591)
3838
cortex_m::asm::dsb();
3939
}
4040
#[inline(always)]
41-
fn disable_in_low_power(rcc: &RccRB) {
41+
fn disable_in_low_power(rcc: &mut RCC) {
4242
unsafe {
4343
bb::clear(Self::Bus::lpenr(rcc), $bit);
4444
}
@@ -55,7 +55,7 @@ macro_rules! bus_reset {
5555
($PER:ident => $bit:literal) => {
5656
impl Reset for crate::pac::$PER {
5757
#[inline(always)]
58-
fn reset(rcc: &RccRB) {
58+
fn reset(rcc: &mut RCC) {
5959
unsafe {
6060
bb::set(Self::Bus::rstr(rcc), $bit);
6161
bb::clear(Self::Bus::rstr(rcc), $bit);

src/rcc/f4/mod.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ use crate::pac::rcc::RegisterBlock as RccRB;
1515
#[allow(clippy::missing_safety_doc)]
1616
pub trait Enable: RccBus {
1717
/// Enables peripheral
18-
fn enable(rcc: &RccRB);
18+
fn enable(rcc: &mut RCC);
1919

2020
/// Disables peripheral
21-
fn disable(rcc: &RccRB);
21+
fn disable(rcc: &mut RCC);
2222

2323
/// Check if peripheral enabled
2424
fn is_enabled() -> bool;
@@ -33,27 +33,27 @@ pub trait Enable: RccBus {
3333
///
3434
/// Enables peripheral. Takes access to RCC internally
3535
unsafe fn enable_unchecked() {
36-
let rcc = &*pac::RCC::ptr();
37-
Self::enable(rcc);
36+
let mut rcc = pac::RCC::steal();
37+
Self::enable(&mut rcc);
3838
}
3939

4040
/// # Safety
4141
///
4242
/// Disables peripheral. Takes access to RCC internally
4343
unsafe fn disable_unchecked() {
44-
let rcc = pac::RCC::ptr();
45-
Self::disable(&*rcc);
44+
let mut rcc = pac::RCC::steal();
45+
Self::disable(&mut rcc);
4646
}
4747
}
4848

4949
/// Low power enable/disable peripheral
5050
#[allow(clippy::missing_safety_doc)]
5151
pub trait LPEnable: RccBus {
5252
/// Enables peripheral in low power mode
53-
fn enable_in_low_power(rcc: &RccRB);
53+
fn enable_in_low_power(rcc: &mut RCC);
5454

5555
/// Disables peripheral in low power mode
56-
fn disable_in_low_power(rcc: &RccRB);
56+
fn disable_in_low_power(rcc: &mut RCC);
5757

5858
/// Check if peripheral enabled in low power mode
5959
fn is_enabled_in_low_power() -> bool;
@@ -68,31 +68,31 @@ pub trait LPEnable: RccBus {
6868
///
6969
/// Enables peripheral in low power mode. Takes access to RCC internally
7070
unsafe fn enable_in_low_power_unchecked() {
71-
let rcc = pac::RCC::ptr();
72-
Self::enable_in_low_power(&*rcc);
71+
let mut rcc = pac::RCC::steal();
72+
Self::enable_in_low_power(&mut rcc);
7373
}
7474

7575
/// # Safety
7676
///
7777
/// Disables peripheral in low power mode. Takes access to RCC internally
7878
unsafe fn disable_in_low_power_unchecked() {
79-
let rcc = pac::RCC::ptr();
80-
Self::disable_in_low_power(&*rcc);
79+
let mut rcc = pac::RCC::steal();
80+
Self::disable_in_low_power(&mut rcc);
8181
}
8282
}
8383

8484
/// Reset peripheral
8585
#[allow(clippy::missing_safety_doc)]
8686
pub trait Reset: RccBus {
8787
/// Resets peripheral
88-
fn reset(rcc: &RccRB);
88+
fn reset(rcc: &mut RCC);
8989

9090
/// # Safety
9191
///
9292
/// Resets peripheral. Takes access to RCC internally
9393
unsafe fn reset_unchecked() {
94-
let rcc = pac::RCC::ptr();
95-
Self::reset(&*rcc);
94+
let mut rcc = pac::RCC::steal();
95+
Self::reset(&mut rcc);
9696
}
9797
}
9898

src/rtc.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use crate::bb;
66
use crate::pac::rtc::{dr, tr};
7-
use crate::pac::{self, rcc::RegisterBlock, PWR, RCC, RTC};
7+
use crate::pac::{self, PWR, RCC, RTC};
88
use crate::rcc::Enable;
99
use core::fmt;
1010
use fugit::RateExtU32;
@@ -152,28 +152,28 @@ impl Rtc {
152152
// Enable write protect
153153

154154
unsafe {
155-
let rcc = &(*RCC::ptr());
155+
let mut rcc = RCC::steal();
156156
// As per the sample code, unlock comes first. (Enable PWR and DBP)
157-
result.unlock(rcc, pwr);
157+
result.unlock(&mut rcc, pwr);
158158
match result.clock_source {
159159
ClockSource::Lse(mode) => {
160160
// If necessary, enable the LSE.
161161
if rcc.bdcr().read().lserdy().bit_is_clear() {
162-
result.enable_lse(rcc, mode);
162+
result.enable_lse(&mut rcc, mode);
163163
}
164164
// Set clock source to LSE.
165165
rcc.bdcr().modify(|_, w| w.rtcsel().lse());
166166
}
167167
ClockSource::Lsi => {
168168
// If necessary, enable the LSE.
169169
if rcc.csr().read().lsirdy().bit_is_clear() {
170-
result.enable_lsi(rcc);
170+
result.enable_lsi(&mut rcc);
171171
}
172172
// Set clock source to LSI.
173173
rcc.bdcr().modify(|_, w| w.rtcsel().lsi());
174174
}
175175
}
176-
result.enable(rcc);
176+
result.enable(&mut rcc);
177177
}
178178

179179
result.modify(true, |regs| {
@@ -191,7 +191,7 @@ impl Rtc {
191191

192192
/// Enable the low frequency external oscillator. This is the only mode currently
193193
/// supported, to avoid exposing the `CR` and `CRS` registers.
194-
fn enable_lse(&mut self, rcc: &RegisterBlock, mode: LSEClockMode) {
194+
fn enable_lse(&mut self, rcc: &mut RCC, mode: LSEClockMode) {
195195
unsafe {
196196
// Force a reset of the backup domain.
197197
self.backup_reset(rcc);
@@ -221,15 +221,15 @@ impl Rtc {
221221
Self::with_config(regs, pwr, ClockSource::Lsi, prediv_s, prediv_a)
222222
}
223223

224-
fn enable_lsi(&mut self, rcc: &RegisterBlock) {
224+
fn enable_lsi(&mut self, rcc: &mut RCC) {
225225
// Force a reset of the backup domain.
226226
self.backup_reset(rcc);
227227
// Enable the LSI.
228228
rcc.csr().modify(|_, w| w.lsion().on());
229229
while rcc.csr().read().lsirdy().is_not_ready() {}
230230
}
231231

232-
fn unlock(&mut self, rcc: &RegisterBlock, pwr: &mut PWR) {
232+
fn unlock(&mut self, rcc: &mut RCC, pwr: &mut PWR) {
233233
// Enable the backup interface
234234
// Set APB1 - Bit 28 (PWREN)
235235
PWR::enable(rcc);
@@ -238,7 +238,7 @@ impl Rtc {
238238
pwr.cr().modify(|_, w| w.dbp().set_bit());
239239
}
240240

241-
fn backup_reset(&mut self, rcc: &RegisterBlock) {
241+
fn backup_reset(&mut self, rcc: &mut RCC) {
242242
unsafe {
243243
// Set BDCR - Bit 16 (BDRST)
244244
bb::set(rcc.bdcr(), 16);
@@ -247,7 +247,7 @@ impl Rtc {
247247
}
248248
}
249249

250-
fn enable(&mut self, rcc: &RegisterBlock) {
250+
fn enable(&mut self, rcc: &mut RCC) {
251251
// Start the actual RTC.
252252
// Set BDCR - Bit 15 (RTCEN)
253253
unsafe {

0 commit comments

Comments
 (0)