Skip to content

Commit

Permalink
Ptr & Steal traits
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Jul 27, 2024
1 parent 2061acc commit 8388c52
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 65 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- RTIC2 monotonics fix: CC1 instead of CC3
- Allow different lengths of buffers in hal_1 SpiBus impl [#566]
- Clean SPI write impls
- move `ptr()` to `Ptr` trait [#773]
- `steal` UART peripheral on `Rx::new`

[#248]: https://github.com/stm32-rs/stm32f4xx-hal/pull/248
[#566]: https://github.com/stm32-rs/stm32f4xx-hal/pull/566
[#706]: https://github.com/stm32-rs/stm32f4xx-hal/pull/706
[#725]: https://github.com/stm32-rs/stm32f4xx-hal/pull/725
[#731]: https://github.com/stm32-rs/stm32f4xx-hal/pull/731
[#758]: https://github.com/stm32-rs/stm32f4xx-hal/pull/758
[#725]: https://github.com/stm32-rs/stm32f4xx-hal/pull/725
[#773]: https://github.com/stm32-rs/stm32f4xx-hal/pull/773

## [v0.21.0] - 2024-05-30

Expand Down
23 changes: 14 additions & 9 deletions src/dma/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,22 +302,27 @@ use address;
pub type DMARegisterBlock = pac::dma1::RegisterBlock;

/// Trait that represents an instance of a DMA peripheral.
pub trait Instance: Deref<Target = DMARegisterBlock> + crate::Sealed {
/// Gives a pointer to the RegisterBlock.
fn ptr() -> *const DMARegisterBlock;
pub trait Instance:
crate::Sealed + crate::Ptr<RB = DMARegisterBlock> + Deref<Target = Self::RB>
{
}

impl Instance for DMA1 {
impl Instance for DMA1 {}
impl Instance for DMA2 {}

impl crate::Ptr for DMA1 {
type RB = DMARegisterBlock;
#[inline(always)]
fn ptr() -> *const DMARegisterBlock {
DMA1::ptr()
fn ptr() -> *const Self::RB {
Self::ptr()
}
}

impl Instance for DMA2 {
impl crate::Ptr for DMA2 {
type RB = DMARegisterBlock;
#[inline(always)]
fn ptr() -> *const DMARegisterBlock {
DMA2::ptr()
fn ptr() -> *const Self::RB {
Self::ptr()
}
}

Expand Down
20 changes: 14 additions & 6 deletions src/fmpi2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,30 @@ mod hal_02;
mod hal_1;

pub trait Instance:
crate::Sealed + Deref<Target = fmpi2c1::RegisterBlock> + Enable + Reset + gpio::alt::I2cCommon
crate::Sealed
+ crate::Ptr<RB = fmpi2c1::RegisterBlock>
+ Deref<Target = Self::RB>
+ Enable
+ Reset
+ gpio::alt::I2cCommon
{
#[doc(hidden)]
fn ptr() -> *const fmpi2c1::RegisterBlock;
fn clock_hsi(rcc: &crate::pac::rcc::RegisterBlock);
}

impl Instance for FMPI2C1 {
fn ptr() -> *const fmpi2c1::RegisterBlock {
FMPI2C1::ptr() as *const _
}
fn clock_hsi(rcc: &crate::pac::rcc::RegisterBlock) {
rcc.dckcfgr2().modify(|_, w| w.fmpi2c1sel().hsi());
}
}

impl crate::Ptr for FMPI2C1 {
type RB = fmpi2c1::RegisterBlock;
#[inline(always)]
fn ptr() -> *const Self::RB {
Self::ptr()
}
}

/// I2C FastMode+ abstraction
pub struct FMPI2c<I2C: Instance> {
i2c: I2C,
Expand Down
18 changes: 12 additions & 6 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,26 @@ impl Error {
}

pub trait Instance:
crate::Sealed + Deref<Target = i2c1::RegisterBlock> + Enable + Reset + gpio::alt::I2cCommon
crate::Sealed
+ crate::Ptr<RB = i2c1::RegisterBlock>
+ Deref<Target = Self::RB>
+ Enable
+ Reset
+ gpio::alt::I2cCommon
{
#[doc(hidden)]
fn ptr() -> *const i2c1::RegisterBlock;
}

// Implemented by all I2C instances
macro_rules! i2c {
($I2C:ty: $I2c:ident) => {
pub type $I2c = I2c<$I2C>;

impl Instance for $I2C {
fn ptr() -> *const i2c1::RegisterBlock {
<$I2C>::ptr() as *const _
impl Instance for $I2C {}

impl crate::Ptr for $I2C {
type RB = i2c1::RegisterBlock;
fn ptr() -> *const Self::RB {
Self::ptr()
}
}
};
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,12 @@ pub trait Listen {
self.unlisten(BitFlags::ALL)
}
}

pub trait Ptr {
type RB;
fn ptr() -> *const Self::RB;
}

pub trait Steal {
unsafe fn steal() -> Self;
}
27 changes: 18 additions & 9 deletions src/sai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,14 @@ pub struct Receive;
pub struct Transmit;

pub trait Instance:
crate::Sealed + Deref<Target = sai::RegisterBlock> + rcc::Enable + rcc::Reset + rcc::BusClock
crate::Sealed
+ crate::Ptr<RB = sai::RegisterBlock>
+ crate::Steal
+ Deref<Target = Self::RB>
+ rcc::Enable
+ rcc::Reset
+ rcc::BusClock
{
#[doc(hidden)]
fn ptr() -> *const sai::RegisterBlock;
#[doc(hidden)]
unsafe fn steal() -> Self;
}

impl<SAI: Instance, const C: bool> SAICH<SAI, C> {
Expand All @@ -234,12 +236,19 @@ macro_rules! sai_impl {
($SAI:ty, $sai:ident, $SAIA:ident, $SAIB:ident) => {
pub type $SAIA = SAIA<$SAI>;
pub type $SAIB = SAIB<$SAI>;
impl Instance for $SAI {
fn ptr() -> *const sai::RegisterBlock {
<$SAI>::ptr()

impl Instance for $SAI {}

impl crate::Ptr for $SAI {
type RB = sai::RegisterBlock;
fn ptr() -> *const Self::RB {
Self::ptr()
}
}

impl crate::Steal for $SAI {
unsafe fn steal() -> Self {
<$SAI>::steal()
Self::steal()
}
}
};
Expand Down
20 changes: 12 additions & 8 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ impl<USART: Instance, WORD> Serial<USART, WORD> {
clocks: &Clocks,
) -> Result<Self, config::InvalidConfig>
where
<USART as Instance>::RegisterBlock: uart_impls::RegisterBlockImpl,
<USART as crate::Ptr>::RB: uart_impls::RegisterBlockImpl,
{
<USART as Instance>::RegisterBlock::new(usart, pins, config, clocks)
<USART as crate::Ptr>::RB::new(usart, pins, config, clocks)
}
}

Expand All @@ -257,12 +257,6 @@ macro_rules! halUsart {
pub type $Rx<WORD = u8> = Rx<$USART, WORD>;

impl Instance for $USART {
type RegisterBlock = crate::serial::uart_impls::RegisterBlockUsart;

fn ptr() -> *const crate::serial::uart_impls::RegisterBlockUsart {
<$USART>::ptr() as *const _
}

fn set_stopbits(&self, bits: config::StopBits) {
use crate::pac::usart1::cr2::STOP;
use config::StopBits;
Expand All @@ -276,7 +270,17 @@ macro_rules! halUsart {
})
});
}
}

impl crate::Ptr for $USART {
type RB = crate::serial::uart_impls::RegisterBlockUsart;

fn ptr() -> *const Self::RB {
Self::ptr()
}
}

impl crate::Steal for $USART {
unsafe fn steal() -> Self {
Self::steal()
}
Expand Down
2 changes: 1 addition & 1 deletion src/serial/hal_02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ mod blocking {

impl<USART: Instance> Write<u16> for Tx<USART, u16>
where
USART: Deref<Target = <USART as Instance>::RegisterBlock>,
USART: Deref<Target = <USART as crate::Ptr>::RB>,
{
type Error = Error;

Expand Down
4 changes: 2 additions & 2 deletions src/serial/hal_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ mod io {

impl<USART: Instance> Write for Tx<USART, u8>
where
<USART as Instance>::RegisterBlock: RegisterBlockImpl,
USART: Deref<Target = <USART as Instance>::RegisterBlock>,
<USART as crate::Ptr>::RB: RegisterBlockImpl,
USART: Deref<Target = <USART as crate::Ptr>::RB>,
{
fn write(&mut self, bytes: &[u8]) -> Result<usize, Self::Error> {
let mut i = 0;
Expand Down
16 changes: 6 additions & 10 deletions src/serial/uart_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,26 @@ impl crate::Sealed for RegisterBlockUsart {}
// Implemented by all USART/UART instances
pub trait Instance:
crate::Sealed
+ crate::Ptr<RB: RegisterBlockImpl>
+ crate::Steal
+ core::ops::Deref<Target = Self::RB>
+ rcc::Enable
+ rcc::Reset
+ rcc::BusClock
+ CommonPins
+ core::ops::Deref<Target = Self::RegisterBlock>
{
type RegisterBlock: RegisterBlockImpl;

#[doc(hidden)]
fn ptr() -> *const Self::RegisterBlock;
#[doc(hidden)]
fn set_stopbits(&self, bits: config::StopBits);
#[doc(hidden)]
#[inline(always)]
fn peri_address() -> u32 {
unsafe { &*Self::ptr() }.peri_address()
}
#[doc(hidden)]
unsafe fn steal() -> Self;
}

pub trait RegisterBlockImpl: crate::Sealed {
#[allow(clippy::new_ret_no_self)]
fn new<UART: Instance<RegisterBlock = Self>, WORD>(
fn new<UART: Instance + crate::Ptr<RB = Self>, WORD>(
uart: UART,
pins: (impl Into<UART::Tx<PushPull>>, impl Into<UART::Rx<PushPull>>),
config: impl Into<config::Config>,
Expand Down Expand Up @@ -267,7 +263,7 @@ macro_rules! uartCommon {
}

impl RegisterBlockImpl for RegisterBlockUsart {
fn new<UART: Instance<RegisterBlock = Self>, WORD>(
fn new<UART: Instance + crate::Ptr<RB = Self>, WORD>(
uart: UART,
pins: (impl Into<UART::Tx<PushPull>>, impl Into<UART::Rx<PushPull>>),
config: impl Into<config::Config>,
Expand Down Expand Up @@ -408,7 +404,7 @@ where {

#[cfg(feature = "uart4")]
impl RegisterBlockImpl for RegisterBlockUart {
fn new<UART: Instance<RegisterBlock = Self>, WORD>(
fn new<UART: Instance + crate::Ptr<RB = Self>, WORD>(
uart: UART,
pins: (impl Into<UART::Tx<PushPull>>, impl Into<UART::Rx<PushPull>>),
config: impl Into<config::Config>,
Expand Down
14 changes: 8 additions & 6 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,13 @@ impl<SPI: Instance, const BIDI: bool, W> DerefMut for SpiSlave<SPI, BIDI, W> {
// Implemented by all SPI instances
pub trait Instance:
crate::Sealed
+ Deref<Target = spi1::RegisterBlock>
+ crate::Ptr<RB = spi1::RegisterBlock>
+ Deref<Target = Self::RB>
+ rcc::Enable
+ rcc::Reset
+ rcc::BusClock
+ gpio::alt::SpiCommon
{
#[doc(hidden)]
fn ptr() -> *const spi1::RegisterBlock;
}

// Implemented by all SPI instances
Expand All @@ -212,9 +211,12 @@ macro_rules! spi {
pub type $Spi<const BIDI: bool = false, W = u8> = Spi<$SPI, BIDI, W>;
pub type $SpiSlave<const BIDI: bool = false, W = u8> = SpiSlave<$SPI, BIDI, W>;

impl Instance for $SPI {
fn ptr() -> *const spi1::RegisterBlock {
<$SPI>::ptr() as *const _
impl Instance for $SPI {}

impl crate::Ptr for $SPI {
type RB = spi1::RegisterBlock;
fn ptr() -> *const Self::RB {
Self::ptr()
}
}
};
Expand Down
18 changes: 11 additions & 7 deletions src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use crate::pac;

use crate::serial::uart_impls::{RegisterBlockImpl, RegisterBlockUart};
use crate::serial::uart_impls::RegisterBlockUart;

pub use crate::serial::{config, Error, Event, Instance, NoRx, NoTx, Rx, RxISR, Serial, Tx, TxISR};
pub use config::Config;
Expand All @@ -28,12 +28,6 @@ macro_rules! halUart {
pub type $Rx<WORD = u8> = Rx<$UART, WORD>;

impl Instance for $UART {
type RegisterBlock = RegisterBlockUart;

fn ptr() -> *const RegisterBlockUart {
<$UART>::ptr() as *const _
}

fn set_stopbits(&self, bits: config::StopBits) {
use crate::pac::uart4::cr2::STOP;
use config::StopBits;
Expand All @@ -51,7 +45,17 @@ macro_rules! halUart {
})
});
}
}

impl crate::Ptr for $UART {
type RB = RegisterBlockUart;

fn ptr() -> *const Self::RB {
Self::ptr()
}
}

impl crate::Steal for $UART {
unsafe fn steal() -> Self {
Self::steal()
}
Expand Down

0 comments on commit 8388c52

Please sign in to comment.