Skip to content

Commit f31e9c1

Browse files
committed
spi: update to use DmaChannel generic
1 parent eac2d82 commit f31e9c1

File tree

1 file changed

+54
-60
lines changed

1 file changed

+54
-60
lines changed

src/spi/dma.rs

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use futures_util::task::AtomicWaker;
1313
use crate::gpdma::{
1414
config::DmaConfig,
1515
periph::{DmaDuplex, DmaRx, DmaTx, Rx, RxAddr, Tx, TxAddr},
16-
ChannelRegs, DmaChannel, DmaTransfer, Error as DmaError, Word as DmaWord,
16+
DmaChannel, DmaTransfer, Error as DmaError, Word as DmaWord,
1717
};
1818

1919
use super::{Error, Instance, Spi, Word};
@@ -37,43 +37,43 @@ where
3737
{
3838
pub fn use_dma_tx<CH>(
3939
self,
40-
channel: DmaChannel<CH>,
41-
) -> SpiDma<SPI, W, DmaTx<SPI, W, CH>>
40+
channel: CH,
41+
) -> SpiDma<SPI, DmaTx<SPI, W, CH>, W>
4242
where
43-
CH: ChannelRegs,
43+
CH: DmaChannel,
4444
{
4545
SpiDma::new_simplex_transmitter(self, channel)
4646
}
4747

4848
pub fn use_dma_rx<CH>(
4949
self,
50-
channel: DmaChannel<CH>,
51-
) -> SpiDma<SPI, W, DmaRx<SPI, W, CH>>
50+
channel: CH,
51+
) -> SpiDma<SPI, DmaRx<SPI, W, CH>, W>
5252
where
53-
CH: ChannelRegs,
53+
CH: DmaChannel,
5454
{
5555
SpiDma::new_simplex_receiver(self, channel)
5656
}
5757

5858
pub fn use_dma_duplex<TX, RX>(
5959
self,
60-
tx_channel: DmaChannel<TX>,
61-
rx_channel: DmaChannel<RX>,
62-
) -> SpiDma<SPI, W, DmaDuplex<SPI, W, TX, RX>>
60+
tx_channel: TX,
61+
rx_channel: RX,
62+
) -> SpiDma<SPI, DmaDuplex<SPI, W, TX, RX>, W>
6363
where
64-
TX: ChannelRegs,
65-
RX: ChannelRegs,
64+
TX: DmaChannel,
65+
RX: DmaChannel,
6666
{
6767
SpiDma::new_duplex(self, tx_channel, rx_channel)
6868
}
6969
}
7070

71-
pub struct SpiDma<SPI, W: Word, MODE> {
71+
pub struct SpiDma<SPI, MODE, W: Word = u8> {
7272
spi: Spi<SPI, W>,
7373
mode: MODE,
7474
}
7575

76-
impl<SPI, W, MODE> SpiDma<SPI, W, MODE>
76+
impl<SPI, MODE, W> SpiDma<SPI, MODE, W>
7777
where
7878
SPI: Instance,
7979
W: Word,
@@ -115,75 +115,69 @@ where
115115
}
116116
}
117117

118-
impl<SPI, W, CH> SpiDma<SPI, W, DmaTx<SPI, W, CH>>
118+
impl<SPI, CH, W> SpiDma<SPI, DmaTx<SPI, W, CH>, W>
119119
where
120120
SPI: Instance,
121121
W: Word,
122-
CH: ChannelRegs,
122+
CH: DmaChannel,
123123
{
124-
pub fn new_simplex_transmitter(
125-
spi: Spi<SPI, W>,
126-
channel: DmaChannel<CH>,
127-
) -> Self {
124+
pub fn new_simplex_transmitter(spi: Spi<SPI, W>, channel: CH) -> Self {
128125
Self {
129126
spi,
130127
mode: DmaTx::from(channel),
131128
}
132129
}
133130

134-
pub fn free(self) -> (Spi<SPI, W>, DmaChannel<CH>) {
131+
pub fn free(self) -> (Spi<SPI, W>, CH) {
135132
let spi = self.spi;
136-
let channel = self.mode.into();
133+
let channel = self.mode.free();
137134
(spi, channel)
138135
}
139136
}
140137

141-
impl<SPI, W, CH> SpiDma<SPI, W, DmaRx<SPI, W, CH>>
138+
impl<SPI, CH, W> SpiDma<SPI, DmaRx<SPI, W, CH>, W>
142139
where
143140
SPI: Instance,
144141
W: Word,
145-
CH: ChannelRegs,
142+
CH: DmaChannel,
146143
{
147-
pub fn new_simplex_receiver(
148-
spi: Spi<SPI, W>,
149-
channel: DmaChannel<CH>,
150-
) -> Self {
144+
pub fn new_simplex_receiver(spi: Spi<SPI, W>, channel: CH) -> Self {
151145
Self {
152146
spi,
153147
mode: DmaRx::from(channel),
154148
}
155149
}
156150

157-
pub fn free(self) -> (Spi<SPI, W>, DmaChannel<CH>) {
158-
(self.spi, self.mode.into())
151+
pub fn free(self) -> (Spi<SPI, W>, CH) {
152+
(self.spi, self.mode.free())
159153
}
160154
}
161155

162-
impl<SPI, W, TX, RX> SpiDma<SPI, W, DmaDuplex<SPI, W, TX, RX>>
156+
impl<SPI, TX, RX, W> SpiDma<SPI, DmaDuplex<SPI, W, TX, RX>, W>
163157
where
164158
SPI: Instance + TxAddr<W> + RxAddr<W>,
165159
W: Word + DmaWord,
166-
TX: ChannelRegs,
167-
RX: ChannelRegs,
160+
TX: DmaChannel,
161+
RX: DmaChannel,
168162
{
169163
pub fn new_duplex(
170164
spi: Spi<SPI, W>,
171-
tx_channel: DmaChannel<TX>,
172-
rx_channel: DmaChannel<RX>,
165+
tx_channel: TX,
166+
rx_channel: RX,
173167
) -> Self {
174168
Self {
175169
spi,
176170
mode: DmaDuplex::new(tx_channel, rx_channel),
177171
}
178172
}
179173

180-
pub fn free(self) -> (Spi<SPI, W>, DmaChannel<TX>, DmaChannel<RX>) {
174+
pub fn free(self) -> (Spi<SPI, W>, TX, RX) {
181175
let (tx, rx) = self.mode.free();
182176
(self.spi, tx, rx)
183177
}
184178
}
185179

186-
impl<SPI, W, MODE> Deref for SpiDma<SPI, W, MODE>
180+
impl<SPI, MODE, W> Deref for SpiDma<SPI, MODE, W>
187181
where
188182
SPI: Instance,
189183
W: Word,
@@ -195,7 +189,7 @@ where
195189
}
196190
}
197191

198-
impl<SPI, W, MODE> DerefMut for SpiDma<SPI, W, MODE>
192+
impl<SPI, MODE, W> DerefMut for SpiDma<SPI, MODE, W>
199193
where
200194
SPI: Instance,
201195
W: Word,
@@ -205,7 +199,7 @@ where
205199
}
206200
}
207201

208-
impl<SPI, W, MODE> SpiDma<SPI, W, MODE>
202+
impl<SPI, MODE, W> SpiDma<SPI, MODE, W>
209203
where
210204
SPI: Instance,
211205
W: Word + DmaWord,
@@ -214,7 +208,7 @@ where
214208
pub fn start_dma_read<'a>(
215209
&'a mut self,
216210
words: &'a mut [W],
217-
) -> Result<DmaTransfer<'a, impl ChannelRegs + use<'a, SPI, W, MODE>>, Error>
211+
) -> Result<DmaTransfer<'a, impl DmaChannel + use<'a, SPI, W, MODE>>, Error>
218212
{
219213
let config = DmaConfig::new().with_request(SPI::rx_dma_request());
220214

@@ -240,7 +234,7 @@ where
240234
}
241235
}
242236

243-
impl<SPI, W, MODE> SpiDma<SPI, W, MODE>
237+
impl<SPI, MODE, W> SpiDma<SPI, MODE, W>
244238
where
245239
SPI: Instance,
246240
W: Word + DmaWord,
@@ -249,7 +243,7 @@ where
249243
pub fn start_dma_write<'a>(
250244
&'a mut self,
251245
words: &'a [W],
252-
) -> Result<DmaTransfer<'a, impl ChannelRegs + use<'a, SPI, W, MODE>>, Error>
246+
) -> Result<DmaTransfer<'a, impl DmaChannel + use<'a, SPI, W, MODE>>, Error>
253247
{
254248
let config = DmaConfig::new().with_request(SPI::tx_dma_request());
255249

@@ -274,7 +268,7 @@ where
274268
}
275269
}
276270

277-
impl<SPI, W, MODE> SpiDma<SPI, W, MODE>
271+
impl<SPI, MODE, W> SpiDma<SPI, MODE, W>
278272
where
279273
SPI: Instance,
280274
W: Word + DmaWord,
@@ -286,8 +280,8 @@ where
286280
write: &'a [W],
287281
) -> Result<
288282
(
289-
DmaTransfer<'a, impl ChannelRegs + use<'a, SPI, W, MODE>>,
290-
DmaTransfer<'a, impl ChannelRegs + use<'a, SPI, W, MODE>>,
283+
DmaTransfer<'a, impl DmaChannel + use<'a, SPI, W, MODE>>,
284+
DmaTransfer<'a, impl DmaChannel + use<'a, SPI, W, MODE>>,
291285
),
292286
Error,
293287
> {
@@ -335,19 +329,19 @@ where
335329
}
336330
}
337331

338-
impl<SPI, W, MODE> ErrorType for SpiDma<SPI, W, MODE>
332+
impl<SPI, MODE, W> ErrorType for SpiDma<SPI, MODE, W>
339333
where
340334
SPI: Instance,
341335
W: Word,
342336
{
343337
type Error = Error;
344338
}
345339

346-
impl<SPI, W, CH> SpiBus<W> for SpiDma<SPI, W, DmaTx<SPI, W, CH>>
340+
impl<SPI, CH, W> SpiBus<W> for SpiDma<SPI, DmaTx<SPI, W, CH>, W>
347341
where
348342
SPI: Instance,
349343
W: Word + DmaWord,
350-
CH: ChannelRegs,
344+
CH: DmaChannel,
351345
{
352346
async fn read(&mut self, _words: &mut [W]) -> Result<(), Self::Error> {
353347
unimplemented!("Not supported for simplex transmitter")
@@ -378,11 +372,11 @@ where
378372
}
379373
}
380374

381-
impl<SPI, W, CH> SpiBus<W> for SpiDma<SPI, W, DmaRx<SPI, W, CH>>
375+
impl<SPI, CH, W> SpiBus<W> for SpiDma<SPI, DmaRx<SPI, W, CH>, W>
382376
where
383377
SPI: Instance,
384378
W: Word + DmaWord,
385-
CH: ChannelRegs,
379+
CH: DmaChannel,
386380
{
387381
async fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
388382
self.read_dma(words).await
@@ -413,12 +407,12 @@ where
413407
}
414408
}
415409

416-
impl<SPI, W, TX, RX> SpiBus<W> for SpiDma<SPI, W, DmaDuplex<SPI, W, TX, RX>>
410+
impl<SPI, TX, RX, W> SpiBus<W> for SpiDma<SPI, DmaDuplex<SPI, W, TX, RX>, W>
417411
where
418412
SPI: Instance,
419413
W: Word + DmaWord,
420-
TX: ChannelRegs,
421-
RX: ChannelRegs,
414+
TX: DmaChannel,
415+
RX: DmaChannel,
422416
{
423417
async fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
424418
self.read_dma(words).await
@@ -449,23 +443,23 @@ where
449443
}
450444
}
451445

452-
struct SpiDmaFuture<'a, SPI: Instance, W: Word, MODE> {
453-
spi: &'a mut SpiDma<SPI, W, MODE>,
446+
struct SpiDmaFuture<'a, SPI: Instance, MODE, W: Word> {
447+
spi: &'a mut SpiDma<SPI, MODE, W>,
454448
waker: AtomicWaker,
455449
}
456450

457-
impl<'a, SPI: Instance, W: Word, MODE> SpiDmaFuture<'a, SPI, W, MODE> {
458-
fn new(spi: &'a mut SpiDma<SPI, W, MODE>) -> Self {
451+
impl<'a, SPI: Instance, MODE, W: Word> SpiDmaFuture<'a, SPI, MODE, W> {
452+
fn new(spi: &'a mut SpiDma<SPI, MODE, W>) -> Self {
459453
Self {
460454
spi,
461455
waker: AtomicWaker::new(),
462456
}
463457
}
464458
}
465459

466-
impl<SPI: Instance, W: Word, MODE> Unpin for SpiDmaFuture<'_, SPI, W, MODE> {}
460+
impl<SPI: Instance, MODE, W: Word> Unpin for SpiDmaFuture<'_, SPI, MODE, W> {}
467461

468-
impl<SPI: Instance, W: Word, MODE> Drop for SpiDmaFuture<'_, SPI, W, MODE> {
462+
impl<SPI: Instance, MODE, W: Word> Drop for SpiDmaFuture<'_, SPI, MODE, W> {
469463
fn drop(&mut self) {
470464
if !self.spi.is_transaction_complete() {
471465
self.spi.abort_transaction();
@@ -477,7 +471,7 @@ impl<SPI: Instance, W: Word, MODE> Drop for SpiDmaFuture<'_, SPI, W, MODE> {
477471
}
478472
}
479473

480-
impl<SPI: Instance, W: Word, MODE> Future for SpiDmaFuture<'_, SPI, W, MODE> {
474+
impl<SPI: Instance, MODE, W: Word> Future for SpiDmaFuture<'_, SPI, MODE, W> {
481475
type Output = Result<(), Error>;
482476

483477
fn poll(

0 commit comments

Comments
 (0)