Skip to content

Commit 564a91b

Browse files
committed
gpdma: refactoring channels
1 parent 8f07859 commit 564a91b

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ stm32h562 = ["stm32h5/stm32h562", "device-selected", "rm0481", "h56x_h573"]
5050
stm32h563 = ["stm32h5/stm32h563", "device-selected", "rm0481", "h56x_h573", "sdmmc2", "ethernet"]
5151
stm32h573 = ["stm32h5/stm32h573", "device-selected", "rm0481", "h56x_h573", "otfdec", "sdmmc2", "ethernet"]
5252

53+
# Flags for async APIs
54+
futures = ["dep:futures-util"]
55+
gpdma-futures = ["futures"]
56+
async = ["gpdma-futures"]
57+
5358
# Flags for examples
5459
log = ["dep:log"]
5560
log-itm = ["log"]
@@ -71,7 +76,7 @@ embedded-hal = "1.0.0"
7176
defmt = { version = "1.0.0", optional = true }
7277
paste = "1.0.15"
7378
log = { version = "0.4.20", optional = true}
74-
futures-util = { version = "0.3", default-features = false, features = ["async-await-macro"]}
79+
futures-util = { version = "0.3", default-features = false, features = ["async-await-macro"], optional = true}
7580
stm32-usbd = "0.8.0"
7681

7782
[dev-dependencies]

src/gpdma.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ use embedded_dma::{ReadBuffer, Word as DmaWord, WriteBuffer};
8989

9090
mod ch;
9191
pub mod config;
92+
#[cfg(feature = "gpdma-futures")]
9293
mod future;
9394
pub mod periph;
9495

@@ -102,7 +103,6 @@ use config::{
102103
PeripheralSource, PeripheralToMemory, PeripheralToPeripheral,
103104
PeripheralToPeripheralDirection, TransferDirection, TransferType,
104105
};
105-
use future::InstanceWaker;
106106

107107
/// Supported word types for the STM32H5 GPDMA implementation.
108108
///
@@ -148,9 +148,7 @@ impl<DMA: Instance> GpdmaExt<DMA> for DMA {
148148
}
149149

150150
#[allow(private_bounds)]
151-
pub trait Instance:
152-
Sealed + InstanceWaker + Deref<Target = gpdma1::RegisterBlock>
153-
{
151+
pub trait Instance: Sealed + Deref<Target = gpdma1::RegisterBlock> {
154152
type Rec: ResetEnable;
155153

156154
fn ptr() -> *const gpdma1::RegisterBlock;

src/gpdma/ch.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use core::{marker::PhantomData, ops::Deref};
22

3-
use futures_util::task::AtomicWaker;
4-
53
use crate::stm32::gpdma1::{
64
self,
75
ch::{CR, DAR, FCR, LBAR, SAR, SR, TR1, TR2},
@@ -14,7 +12,6 @@ use super::{
1412
AddressingMode, AhbPort, HardwareRequest, PeripheralRequest,
1513
PeripheralSource, Priority, TransferDirection, TransferType,
1614
},
17-
future::ChannelWaker,
1815
DmaConfig, Error, Instance, Word,
1916
};
2017

@@ -539,8 +536,6 @@ pub(super) trait Channel {
539536
/// Disable transfer interrupts for the channel. It is expected that this will be called from
540537
/// an interrupt handler after a transfer is completed.
541538
fn disable_transfer_interrupts(&self);
542-
543-
fn waker(&self) -> &'static AtomicWaker;
544539
}
545540

546541
impl<DMA, CH, const N: usize> Channel for DmaChannelRef<DMA, CH, N>
@@ -744,17 +739,19 @@ where
744739
w.tcie().disabled().dteie().disabled().useie().disabled()
745740
});
746741
}
747-
748-
fn waker(&self) -> &'static AtomicWaker {
749-
self.ch.waker()
750-
}
751742
}
752743

744+
#[cfg(feature = "gpdma-futures")]
745+
pub use super::future::DmaChannel;
746+
753747
/// DmaChannel trait provides the API contract that all GPDMA channels exposed to the user
754748
/// implement.
749+
#[cfg(not(feature = "gpdma-futures"))]
755750
#[allow(private_bounds)]
756751
pub trait DmaChannel: Channel {}
757752

753+
#[cfg(not(feature = "gpdma-futures"))]
754+
#[allow(private_bounds)]
758755
impl<DMA, CH, const N: usize> DmaChannel for DmaChannelRef<DMA, CH, N>
759756
where
760757
DMA: Instance,

src/gpdma/future.rs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,29 @@ use crate::stm32::{GPDMA1, GPDMA2};
1313

1414
use super::{
1515
ch::{
16-
ChannelRegs, DmaChannel, DmaChannel0, DmaChannel1, DmaChannel2,
17-
DmaChannel3, DmaChannel4, DmaChannel5, DmaChannel6, DmaChannel7,
18-
DmaChannelImpl, DmaChannelRef,
16+
Channel, ChannelRegs, DmaChannel0, DmaChannel1,
17+
DmaChannel2, DmaChannel3, DmaChannel4, DmaChannel5, DmaChannel6,
18+
DmaChannel7, DmaChannelRef,
1919
},
2020
DmaTransfer, Error, Instance,
2121
};
2222

2323
#[allow(private_bounds)]
24-
impl<'a, CH: DmaChannel> IntoFuture for DmaTransfer<'a, CH> {
24+
pub trait DmaChannel: Channel + ChannelWaker {}
25+
26+
impl<DMA, CH, const N: usize> DmaChannel for DmaChannelRef<DMA, CH, N>
27+
where
28+
DMA: Instance + InstanceWaker,
29+
CH: ChannelRegs,
30+
Self: Deref<Target = CH>,
31+
{
32+
}
33+
34+
#[allow(private_bounds)]
35+
impl<'a, CH> IntoFuture for DmaTransfer<'a, CH>
36+
where
37+
CH: DmaChannel,
38+
{
2539
type Output = Result<(), Error>;
2640
type IntoFuture = DmaTransferFuture<'a, CH>;
2741

@@ -76,7 +90,7 @@ impl<'a, CH: DmaChannel> Unpin for DmaTransferFuture<'a, CH> {}
7690

7791
impl<'a, CH> Future for DmaTransferFuture<'a, CH>
7892
where
79-
CH: DmaChannel,
93+
CH: DmaChannel + ChannelWaker,
8094
{
8195
type Output = Result<(), Error>;
8296

@@ -91,11 +105,11 @@ where
91105
}
92106

93107
#[allow(private_bounds)]
94-
impl<DMA, CH, const N: usize> DmaChannelImpl<DmaChannelRef<DMA, CH, N>>
108+
impl<DMA, CH, const N: usize> DmaChannelRef<DMA, CH, N>
95109
where
96-
DMA: Instance,
110+
DMA: Instance + InstanceWaker,
97111
CH: ChannelRegs,
98-
DmaChannelRef<DMA, CH, N>: ChannelRegs,
112+
Self: Deref<Target = CH>,
99113
{
100114
#[inline(always)]
101115
fn handle_interrupt() {
@@ -105,6 +119,18 @@ where
105119
}
106120
}
107121

122+
impl<DMA, CH, const N: usize> ChannelWaker for DmaChannelRef<DMA, CH, N>
123+
where
124+
DMA: Instance + InstanceWaker,
125+
CH: ChannelRegs,
126+
Self: Deref<Target = CH>,
127+
{
128+
#[inline(always)]
129+
fn waker(&self) -> &'static AtomicWaker {
130+
DMA::waker(N)
131+
}
132+
}
133+
108134
macro_rules! gpdma_irq {
109135
($GPDMA:ident, $CH:literal) => {
110136
paste::item! {
@@ -125,17 +151,6 @@ pub(super) trait ChannelWaker {
125151
fn waker(&self) -> &'static AtomicWaker;
126152
}
127153

128-
impl<DMA, CH, const N: usize> ChannelWaker for DmaChannelRef<DMA, CH, N>
129-
where
130-
DMA: Instance,
131-
CH: ChannelRegs,
132-
{
133-
#[inline(always)]
134-
fn waker(&self) -> &'static AtomicWaker {
135-
DMA::waker(N)
136-
}
137-
}
138-
139154
mod gpdma1 {
140155
use super::*;
141156

0 commit comments

Comments
 (0)