Skip to content

Commit df2c873

Browse files
committed
unify Instance traits
1 parent f756ca4 commit df2c873

File tree

8 files changed

+82
-65
lines changed

8 files changed

+82
-65
lines changed

src/dac.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
88
use core::marker::PhantomData;
99
use core::mem::MaybeUninit;
10-
use core::ops::Deref;
1110

1211
use crate::gpio::{Analog, PA4, PA5, PA6};
1312
use crate::pac;
@@ -150,13 +149,7 @@ pub trait Generator {}
150149
impl Generator for WaveGenerator {}
151150
impl Generator for SawtoothConfig {}
152151

153-
pub trait Instance:
154-
rcc::Enable
155-
+ rcc::Reset
156-
+ crate::Ptr<RB = crate::pac::dac1::RegisterBlock>
157-
+ Deref<Target = Self::RB>
158-
{
159-
}
152+
pub trait Instance: rcc::Instance + crate::Ptr<RB = crate::pac::dac1::RegisterBlock> {}
160153

161154
pub struct DacCh<DAC: Instance, const CH: u8, const MODE_BITS: u8, ED> {
162155
_marker: PhantomData<(DAC, ED)>,

src/i2c.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use embedded_hal::i2c::{ErrorKind, Operation, SevenBitAddress, TenBitAddress};
44
use embedded_hal_old::blocking::i2c::{Read, Write, WriteRead};
55

66
use crate::gpio::{self, OpenDrain};
7-
use crate::rcc::{Enable, GetBusFreq, Rcc, Reset};
7+
use crate::rcc::{BusClock, Rcc};
88
#[cfg(any(
99
feature = "stm32g473",
1010
feature = "stm32g474",
@@ -14,7 +14,7 @@ use crate::rcc::{Enable, GetBusFreq, Rcc, Reset};
1414
use crate::stm32::I2C4;
1515
use crate::stm32::{I2C1, I2C2, I2C3};
1616
use crate::time::Hertz;
17-
use core::{cmp, convert::TryInto, ops::Deref};
17+
use core::{cmp, convert::TryInto};
1818

1919
/// I2C bus configuration.
2020
#[derive(Debug, Clone, Copy)]
@@ -197,10 +197,7 @@ macro_rules! busy_wait {
197197
};
198198
}
199199

200-
pub trait Instance:
201-
crate::Sealed + Deref<Target = i2c1::RegisterBlock> + Enable + Reset + GetBusFreq
202-
{
203-
}
200+
pub trait Instance: crate::rcc::Instance + crate::Ptr<RB = i2c1::RegisterBlock> {}
204201

205202
macro_rules! i2c {
206203
($I2CX:ident,
@@ -243,7 +240,7 @@ impl<I2C: Instance> I2cExt<I2C> for I2C {
243240

244241
// Setup protocol timings
245242
self.timingr()
246-
.write(|w| config.timing_bits(I2C::get_frequency(&rcc.clocks), w));
243+
.write(|w| config.timing_bits(I2C::Bus::clock(&rcc.clocks), w));
247244

248245
// Enable the I2C processing
249246
self.cr1().modify(|_, w| {

src/lib.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,41 @@ impl<RB, const A: usize> Sealed for Periph<RB, A> {}
117117
pub trait Ptr: Sealed {
118118
/// RegisterBlock structure
119119
type RB;
120+
/// Pointer to the register block
121+
const PTR: *const Self::RB;
120122
/// Return the pointer to the register block
121-
fn ptr() -> *const Self::RB;
123+
#[inline(always)]
124+
fn ptr() -> *const Self::RB {
125+
Self::PTR
126+
}
122127
}
123128

124129
impl<RB, const A: usize> Ptr for Periph<RB, A> {
125130
type RB = RB;
126-
fn ptr() -> *const Self::RB {
127-
Self::ptr()
131+
const PTR: *const Self::RB = Self::PTR;
132+
}
133+
134+
pub trait Steal: Sealed {
135+
/// Steal an instance of this peripheral
136+
///
137+
/// # Safety
138+
///
139+
/// Ensure that the new instance of the peripheral cannot be used in a way
140+
/// that may race with any existing instances, for example by only
141+
/// accessing read-only or write-only registers, or by consuming the
142+
/// original peripheral and using critical sections to coordinate
143+
/// access between multiple new instances.
144+
///
145+
/// Additionally the HAL may rely on only one
146+
/// peripheral instance existing to ensure memory safety; ensure
147+
/// no stolen instances are passed to such software.
148+
unsafe fn steal() -> Self;
149+
}
150+
151+
impl<RB, const A: usize> Steal for Periph<RB, A> {
152+
#[inline(always)]
153+
unsafe fn steal() -> Self {
154+
Self::steal()
128155
}
129156
}
130157

src/pwm.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ use crate::stm32::TIM20;
191191
use crate::stm32::TIM5;
192192
use crate::stm32::{TIM1, TIM15, TIM16, TIM17, TIM2, TIM3, TIM4, TIM8};
193193

194-
use crate::rcc::{Enable, GetBusFreq, Rcc, Reset};
194+
use crate::rcc::{BusTimerClock, Enable, Rcc, Reset};
195195
use crate::time::{ExtU32, Hertz, NanoSecond, RateExtU32};
196196

197197
#[cfg(any(
@@ -1149,7 +1149,7 @@ macro_rules! tim_hal {
11491149
$TIMX::enable(rcc);
11501150
$TIMX::reset(rcc);
11511151

1152-
let clk = $TIMX::get_timer_frequency(&rcc.clocks);
1152+
let clk = $TIMX::timer_clock(&rcc.clocks);
11531153

11541154
let (period, prescale) = match $bits {
11551155
16 => calculate_frequency_16bit(clk, freq, Alignment::Left),
@@ -1188,7 +1188,7 @@ macro_rules! tim_hal {
11881188
$TIMX::enable(rcc);
11891189
$TIMX::reset(rcc);
11901190

1191-
let clk = $TIMX::get_timer_frequency(&rcc.clocks).raw();
1191+
let clk = $TIMX::timer_clock(&rcc.clocks).raw();
11921192

11931193
PwmBuilder {
11941194
_tim: PhantomData,
@@ -1797,7 +1797,7 @@ macro_rules! lptim_hal {
17971797
$TIMX::enable(rcc);
17981798
$TIMX::reset(rcc);
17991799

1800-
let clk = $TIMX::get_timer_frequency(&rcc.clocks);
1800+
let clk = $TIMX::timer_clock(&rcc.clocks);
18011801
let reload = clk / freq;
18021802
assert!(reload < 128 * (1 << 16));
18031803

src/rcc/mod.rs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ mod enable;
1010
pub use clockout::*;
1111
pub use config::*;
1212

13-
pub trait Instance: crate::Sealed + Enable + Reset + GetBusFreq {}
13+
pub trait Instance:
14+
crate::Ptr + crate::Steal + Enable + Reset + RccBus<Bus: BusClock> + Deref<Target = Self::RB>
15+
{
16+
}
1417

1518
/// HSI speed
1619
pub const HSI_FREQ: u32 = 16_000_000;
@@ -672,67 +675,66 @@ pub trait Reset: RccBus {
672675
}
673676
}
674677

675-
pub trait GetBusFreq {
676-
fn get_frequency(clocks: &Clocks) -> Hertz;
677-
fn get_timer_frequency(clocks: &Clocks) -> Hertz {
678-
Self::get_frequency(clocks)
679-
}
678+
/// Frequency on bus that peripheral is connected in
679+
pub trait BusClock {
680+
/// Calculates frequency depending on `Clock` state
681+
fn clock(clocks: &Clocks) -> Hertz;
680682
}
681683

682-
impl<T> GetBusFreq for T
683-
where
684-
T: RccBus,
685-
T::Bus: GetBusFreq,
686-
{
687-
fn get_frequency(clocks: &Clocks) -> Hertz {
688-
T::Bus::get_frequency(clocks)
689-
}
690-
fn get_timer_frequency(clocks: &Clocks) -> Hertz {
691-
T::Bus::get_timer_frequency(clocks)
692-
}
684+
/// Frequency on bus that timer is connected in
685+
pub trait BusTimerClock {
686+
/// Calculates base frequency of timer depending on `Clock` state
687+
fn timer_clock(clocks: &Clocks) -> Hertz;
693688
}
694689

695-
impl GetBusFreq for AHB1 {
696-
fn get_frequency(clocks: &Clocks) -> Hertz {
690+
impl BusClock for AHB1 {
691+
fn clock(clocks: &Clocks) -> Hertz {
697692
clocks.ahb_clk
698693
}
699694
}
700695

701-
impl GetBusFreq for AHB2 {
702-
fn get_frequency(clocks: &Clocks) -> Hertz {
696+
impl BusClock for AHB2 {
697+
fn clock(clocks: &Clocks) -> Hertz {
703698
clocks.ahb_clk
704699
}
705700
}
706701

707-
impl GetBusFreq for AHB3 {
708-
fn get_frequency(clocks: &Clocks) -> Hertz {
702+
impl BusClock for AHB3 {
703+
fn clock(clocks: &Clocks) -> Hertz {
709704
clocks.ahb_clk
710705
}
711706
}
712707

713-
impl GetBusFreq for APB1_1 {
714-
fn get_frequency(clocks: &Clocks) -> Hertz {
708+
impl BusClock for APB1_1 {
709+
fn clock(clocks: &Clocks) -> Hertz {
715710
clocks.apb1_clk
716711
}
717-
fn get_timer_frequency(clocks: &Clocks) -> Hertz {
712+
}
713+
714+
impl BusTimerClock for APB1_1 {
715+
fn timer_clock(clocks: &Clocks) -> Hertz {
718716
clocks.apb1_tim_clk
719717
}
720718
}
721719

722-
impl GetBusFreq for APB1_2 {
723-
fn get_frequency(clocks: &Clocks) -> Hertz {
720+
impl BusClock for APB1_2 {
721+
fn clock(clocks: &Clocks) -> Hertz {
724722
clocks.apb1_clk
725723
}
726-
fn get_timer_frequency(clocks: &Clocks) -> Hertz {
724+
}
725+
impl BusTimerClock for APB1_2 {
726+
fn timer_clock(clocks: &Clocks) -> Hertz {
727727
clocks.apb1_tim_clk
728728
}
729729
}
730730

731-
impl GetBusFreq for APB2 {
732-
fn get_frequency(clocks: &Clocks) -> Hertz {
731+
impl BusClock for APB2 {
732+
fn clock(clocks: &Clocks) -> Hertz {
733733
clocks.apb2_clk
734734
}
735-
fn get_timer_frequency(clocks: &Clocks) -> Hertz {
735+
}
736+
impl BusTimerClock for APB2 {
737+
fn timer_clock(clocks: &Clocks) -> Hertz {
736738
clocks.apb2_tim_clk
737739
}
738740
}

src/serial/usart.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::dma::{
55
mux::DmaMuxResources, traits::TargetAddress, MemoryToPeripheral, PeripheralToMemory,
66
};
77
use crate::gpio::{self, OpenDrain};
8-
use crate::rcc::{Enable, GetBusFreq, Rcc, RccBus, Reset};
8+
use crate::rcc::{BusClock, Enable, Rcc, RccBus, Reset};
99
use crate::stm32::*;
1010

1111
use cortex_m::interrupt;
@@ -562,7 +562,7 @@ macro_rules! uart_lp {
562562
// try SYSCLK if PCLK is not high enough. We could also select 8x oversampling
563563
// instead of 16x.
564564

565-
let clk = <$USARTX as RccBus>::Bus::get_frequency(&rcc.clocks).raw() as u64;
565+
let clk = <$USARTX as RccBus>::Bus::clock(&rcc.clocks).raw() as u64;
566566
let bdr = config.baudrate.0 as u64;
567567
let div = ($clk_mul * clk) / bdr;
568568
if div < 16 {
@@ -710,7 +710,7 @@ macro_rules! uart_full {
710710
// try SYSCLK if PCLK is not high enough. We could also select 8x oversampling
711711
// instead of 16x.
712712

713-
let clk = <$USARTX as RccBus>::Bus::get_frequency(&rcc.clocks).raw() as u64;
713+
let clk = <$USARTX as RccBus>::Bus::clock(&rcc.clocks).raw() as u64;
714714
let bdr = config.baudrate.0 as u64;
715715
let clk_mul = 1;
716716
let div = (clk_mul * clk) / bdr;

src/spi.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::dma::mux::DmaMuxResources;
22
use crate::dma::traits::TargetAddress;
33
use crate::dma::MemoryToPeripheral;
44
use crate::gpio;
5-
use crate::rcc::{Enable, GetBusFreq, Rcc, Reset};
5+
use crate::rcc::{BusClock, Rcc};
66
#[cfg(any(
77
feature = "stm32g473",
88
feature = "stm32g474",
@@ -12,7 +12,7 @@ use crate::rcc::{Enable, GetBusFreq, Rcc, Reset};
1212
use crate::stm32::SPI4;
1313
use crate::stm32::{spi1, SPI1, SPI2, SPI3};
1414
use crate::time::Hertz;
15-
use core::{ops::Deref, ptr};
15+
use core::ptr;
1616

1717
use embedded_hal::spi::ErrorKind;
1818
pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
@@ -85,9 +85,7 @@ impl FrameSize for u16 {
8585
const DFF: bool = true;
8686
}
8787

88-
pub trait Instance:
89-
crate::Sealed + Deref<Target = spi1::RegisterBlock> + Enable + Reset + GetBusFreq
90-
{
88+
pub trait Instance: crate::rcc::Instance + crate::Ptr<RB = spi1::RegisterBlock> {
9189
const DMA_MUX_RESOURCE: DmaMuxResources;
9290
}
9391

@@ -293,7 +291,7 @@ impl<SPI: Instance> SpiExt<SPI> for SPI {
293291
Self::reset(rcc);
294292

295293
let spi_freq = freq.into().raw();
296-
let bus_freq = SPI::get_frequency(&rcc.clocks).raw();
294+
let bus_freq = SPI::clock(&rcc.clocks).raw();
297295
setup_spi_regs(&self, spi_freq, bus_freq, mode);
298296

299297
Spi { spi: self, pins }

src/timer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl Instant {
202202
}
203203
}
204204

205-
pub trait Instance: crate::Sealed + rcc::Enable + rcc::Reset + rcc::GetBusFreq {}
205+
pub trait Instance: rcc::Instance + rcc::BusTimerClock {}
206206

207207
impl<TIM> Timer<TIM>
208208
where
@@ -219,7 +219,7 @@ where
219219
}
220220

221221
Self {
222-
clk: TIM::get_timer_frequency(clocks),
222+
clk: TIM::timer_clock(clocks),
223223
tim,
224224
}
225225
}

0 commit comments

Comments
 (0)