Skip to content

Commit df75a34

Browse files
committed
CG channel
1 parent 9cd6f32 commit df75a34

File tree

6 files changed

+57
-60
lines changed

6 files changed

+57
-60
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4242

4343
### Changed
4444

45+
- Split `Stream` trait on `Stream` and `StreamISR`.
46+
Use const generics for `Stream` and `Channel`.
4547
- [breaking-change] `Timer::new` now just initializes peripheral.
4648
Use `.start_count_down` to start count, `pwm` or `delay` on `Timer` struct.
4749
- Add `Spi::new`, `I2s::new, `spi::Instance` and deprecate `Spi:spix`,

examples/adc_dma_rtic.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use stm32f4xx_hal::{
1111
config::{AdcConfig, Dma, SampleTime, Scan, Sequence},
1212
Adc, Temperature,
1313
},
14-
dma::{config::DmaConfig, Channel0, PeripheralToMemory, Stream0, StreamsTuple, Transfer},
14+
dma::{config::DmaConfig, PeripheralToMemory, Stream0, StreamsTuple, Transfer},
1515
prelude::*,
1616
signature::{VtempCal110, VtempCal30},
1717
stm32,
@@ -20,8 +20,7 @@ use stm32f4xx_hal::{
2020

2121
const POLLING_PERIOD: u32 = 168_000_000 / 2;
2222

23-
type DMATransfer =
24-
Transfer<Stream0<DMA2>, Channel0, Adc<ADC1>, PeripheralToMemory, &'static mut [u16; 2]>;
23+
type DMATransfer = Transfer<Stream0<DMA2>, Adc<ADC1>, PeripheralToMemory, &'static mut [u16; 2], 0>;
2524

2625
#[rtic::app(device = stm32f4xx_hal::stm32, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)]
2726
const APP: () = {

examples/i2s-audio-out-dma.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use stm32_i2s_v12x::{MasterClock, MasterConfig, Polarity, TransmitMode};
5151
use stm32f4xx_hal::delay::Delay;
5252
use stm32f4xx_hal::dma::config::DmaConfig;
5353
use stm32f4xx_hal::dma::MemoryToPeripheral;
54-
use stm32f4xx_hal::dma::{Channel0, Stream5, StreamsTuple, Transfer};
54+
use stm32f4xx_hal::dma::{Stream5, StreamsTuple, Transfer};
5555
use stm32f4xx_hal::gpio::gpioa::PA4;
5656
use stm32f4xx_hal::gpio::gpioc::{PC10, PC12, PC7};
5757
use stm32f4xx_hal::gpio::Alternate;
@@ -212,7 +212,6 @@ fn main() -> ! {
212212

213213
type I2sDmaTransfer = Transfer<
214214
Stream5<DMA1>,
215-
Channel0,
216215
stm32_i2s_v12x::I2s<
217216
I2s<
218217
SPI3,
@@ -227,6 +226,7 @@ type I2sDmaTransfer = Transfer<
227226
>,
228227
MemoryToPeripheral,
229228
&'static mut [u16; SINE_SAMPLES * 2],
229+
0,
230230
>;
231231

232232
/// DMA transfer handoff from main() to interrupt handler

src/dma/mod.rs

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,11 @@ where
367367
}
368368

369369
#[inline(always)]
370-
fn set_channel<C: Channel>(&mut self, channel: C) {
371-
unsafe { Self::st() }
372-
.cr
373-
.modify(|_, w| w.chsel().bits(channel.bits()));
370+
fn set_channel<const C: u8>(&mut self)
371+
where
372+
ChannelX<C>: Channel,
373+
{
374+
unsafe { Self::st() }.cr.modify(|_, w| w.chsel().bits(C));
374375
}
375376

376377
#[inline(always)]
@@ -645,30 +646,28 @@ dma_stream!(
645646
#[derive(Debug, Clone, Copy)]
646647
pub struct ChannelX<const C: u8>;
647648

648-
impl<const C: u8> Bits<u8> for ChannelX<C> {
649-
fn bits(self) -> u8 {
650-
C
651-
}
649+
macro_rules! dma_channel {
650+
($(($name:ident, $value:literal)),+ $(,)*) => {
651+
$(
652+
impl Channel for ChannelX<$value> {}
653+
pub type $name = ChannelX<$value>;
654+
)+
655+
};
652656
}
653657

654-
impl<const C: u8> Channel for ChannelX<C> {
655-
fn new() -> Self {
656-
Self
657-
}
658-
}
659-
pub type Channel0 = ChannelX<0>;
660-
pub type Channel1 = ChannelX<1>;
661-
pub type Channel2 = ChannelX<2>;
662-
pub type Channel3 = ChannelX<3>;
663-
pub type Channel4 = ChannelX<4>;
664-
pub type Channel5 = ChannelX<5>;
665-
pub type Channel6 = ChannelX<6>;
666-
pub type Channel7 = ChannelX<7>;
658+
dma_channel!(
659+
(Channel0, 0),
660+
(Channel1, 1),
661+
(Channel2, 2),
662+
(Channel3, 3),
663+
(Channel4, 4),
664+
(Channel5, 5),
665+
(Channel6, 6),
666+
(Channel7, 7),
667+
);
667668

668669
#[cfg(any(feature = "stm32f413", feature = "stm32f423"))]
669-
pub type Channel8 = ChannelX<8>;
670-
#[cfg(any(feature = "stm32f413", feature = "stm32f423"))]
671-
pub type Channel9 = ChannelX<9>;
670+
dma_channel!((Channel8, 8), (Channel9, 9),);
672671

673672
/// Contains types related to DMA configuration.
674673
pub mod config {
@@ -870,13 +869,12 @@ pub mod config {
870869
}
871870

872871
/// DMA Transfer.
873-
pub struct Transfer<STREAM, CHANNEL, PERIPHERAL, DIRECTION, BUF>
872+
pub struct Transfer<STREAM, PERIPHERAL, DIRECTION, BUF, const CHANNEL: u8>
874873
where
875874
STREAM: Stream,
876875
PERIPHERAL: PeriAddress,
877876
{
878877
stream: STREAM,
879-
_channel: PhantomData<CHANNEL>,
880878
peripheral: PERIPHERAL,
881879
_direction: PhantomData<DIRECTION>,
882880
buf: Option<BUF>,
@@ -885,12 +883,12 @@ where
885883
transfer_length: u16,
886884
}
887885

888-
impl<STREAM, CHANNEL, PERIPHERAL, BUF>
889-
Transfer<STREAM, CHANNEL, PERIPHERAL, MemoryToPeripheral, BUF>
886+
impl<STREAM, PERIPHERAL, BUF, const CHANNEL: u8>
887+
Transfer<STREAM, PERIPHERAL, MemoryToPeripheral, BUF, CHANNEL>
890888
where
891889
STREAM: Stream,
892-
CHANNEL: Channel,
893-
PERIPHERAL: PeriAddress + DMASet<STREAM, CHANNEL, MemoryToPeripheral>,
890+
ChannelX<CHANNEL>: Channel,
891+
PERIPHERAL: PeriAddress + DMASet<STREAM, MemoryToPeripheral, CHANNEL>,
894892
BUF: StaticReadBuffer<Word = <PERIPHERAL as PeriAddress>::MemSize>,
895893
{
896894
/// Configures the DMA stream to the correct channel for the peripheral, configures source and
@@ -922,7 +920,6 @@ where
922920

923921
Self {
924922
stream,
925-
_channel: PhantomData,
926923
peripheral,
927924
_direction: PhantomData,
928925
buf: Some(buf),
@@ -1017,12 +1014,12 @@ where
10171014
}
10181015
}
10191016

1020-
impl<STREAM, CHANNEL, PERIPHERAL, BUF>
1021-
Transfer<STREAM, CHANNEL, PERIPHERAL, PeripheralToMemory, BUF>
1017+
impl<STREAM, PERIPHERAL, BUF, const CHANNEL: u8>
1018+
Transfer<STREAM, PERIPHERAL, PeripheralToMemory, BUF, CHANNEL>
10221019
where
10231020
STREAM: Stream,
1024-
CHANNEL: Channel,
1025-
PERIPHERAL: PeriAddress + DMASet<STREAM, CHANNEL, PeripheralToMemory>,
1021+
ChannelX<CHANNEL>: Channel,
1022+
PERIPHERAL: PeriAddress + DMASet<STREAM, PeripheralToMemory, CHANNEL>,
10261023
BUF: StaticWriteBuffer<Word = <PERIPHERAL as PeriAddress>::MemSize>,
10271024
{
10281025
/// Configures the DMA stream to the correct channel for the peripheral, configures source and
@@ -1054,7 +1051,6 @@ where
10541051

10551052
Self {
10561053
stream,
1057-
_channel: PhantomData,
10581054
peripheral,
10591055
_direction: PhantomData,
10601056
buf: Some(buf),
@@ -1152,12 +1148,12 @@ where
11521148
}
11531149
}
11541150

1155-
impl<STREAM, CHANNEL, PERIPHERAL, BUF, S>
1156-
Transfer<STREAM, CHANNEL, PERIPHERAL, MemoryToMemory<S>, BUF>
1151+
impl<STREAM, PERIPHERAL, BUF, S, const CHANNEL: u8>
1152+
Transfer<STREAM, PERIPHERAL, MemoryToMemory<S>, BUF, CHANNEL>
11571153
where
11581154
STREAM: Stream,
1159-
CHANNEL: Channel,
1160-
PERIPHERAL: PeriAddress + DMASet<STREAM, CHANNEL, MemoryToMemory<S>>,
1155+
ChannelX<CHANNEL>: Channel,
1156+
PERIPHERAL: PeriAddress + DMASet<STREAM, MemoryToMemory<S>, CHANNEL>,
11611157
MemoryToMemory<S>: PeriAddress,
11621158
BUF: StaticWriteBuffer<Word = <PERIPHERAL as PeriAddress>::MemSize>,
11631159
{
@@ -1192,7 +1188,6 @@ where
11921188

11931189
Self {
11941190
stream,
1195-
_channel: PhantomData,
11961191
peripheral,
11971192
_direction: PhantomData,
11981193
buf: Some(buf),
@@ -1252,12 +1247,13 @@ where
12521247
}
12531248
}
12541249

1255-
impl<STREAM, CHANNEL, PERIPHERAL, DIR, BUF> Transfer<STREAM, CHANNEL, PERIPHERAL, DIR, BUF>
1250+
impl<STREAM, PERIPHERAL, DIR, BUF, const CHANNEL: u8>
1251+
Transfer<STREAM, PERIPHERAL, DIR, BUF, CHANNEL>
12561252
where
12571253
STREAM: Stream,
1258-
CHANNEL: Channel,
1254+
ChannelX<CHANNEL>: Channel,
12591255
DIR: Direction,
1260-
PERIPHERAL: PeriAddress + DMASet<STREAM, CHANNEL, DIR>,
1256+
PERIPHERAL: PeriAddress + DMASet<STREAM, DIR, CHANNEL>,
12611257
{
12621258
/// Starts the transfer, the closure will be executed right after enabling the stream.
12631259
pub fn start<F>(&mut self, f: F)
@@ -1385,7 +1381,7 @@ where
13851381
stream.disable();
13861382

13871383
// Set the channel
1388-
stream.set_channel(CHANNEL::new());
1384+
stream.set_channel::<CHANNEL>();
13891385

13901386
// Set peripheral to memory mode
13911387
stream.set_direction(DIR::new());
@@ -1583,7 +1579,8 @@ where
15831579
}
15841580
}
15851581

1586-
impl<STREAM, CHANNEL, PERIPHERAL, DIR, BUF> Drop for Transfer<STREAM, CHANNEL, PERIPHERAL, DIR, BUF>
1582+
impl<STREAM, PERIPHERAL, DIR, BUF, const CHANNEL: u8> Drop
1583+
for Transfer<STREAM, PERIPHERAL, DIR, BUF, CHANNEL>
15871584
where
15881585
STREAM: Stream,
15891586
PERIPHERAL: PeriAddress,

src/dma/traits.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ pub trait Stream: StreamISR + Sealed {
9595
fn disable(&mut self);
9696

9797
/// Set the channel for the (chsel) the DMA stream.
98-
fn set_channel<C: Channel>(&mut self, channel: C);
98+
fn set_channel<const C: u8>(&mut self)
99+
where
100+
ChannelX<C>: Channel;
99101

100102
/// Set the priority (pl) the DMA stream.
101103
fn set_priority(&mut self, priority: config::Priority);
@@ -279,24 +281,21 @@ macro_rules! tim_channels {
279281
}
280282

281283
/// A channel that can be configured on a DMA stream.
282-
pub trait Channel: Bits<u8> {
283-
/// Returns a new instance of the type.
284-
fn new() -> Self;
285-
}
284+
pub trait Channel {}
286285

287286
/// Trait to mark a set of Stream, Channel and Direction for a Peripheral as correct together.
288287
///
289288
/// # Safety
290289
///
291290
/// Memory corruption might occur if this trait is implemented for an invalid combination.
292-
pub unsafe trait DMASet<STREAM, CHANNEL, DIRECTION> {}
291+
pub unsafe trait DMASet<STREAM, DIRECTION, const CHANNEL: u8> {}
293292

294293
tim_channels!(CCR1, CCR2, CCR3, CCR4, DMAR, ARR);
295294

296295
macro_rules! dma_map {
297296
($(($Stream:ty, $C:literal, $Peripheral:ty, $Dir:ty)),+ $(,)*) => {
298297
$(
299-
unsafe impl DMASet<$Stream, ChannelX<$C>, $Dir> for $Peripheral {}
298+
unsafe impl DMASet<$Stream, $Dir, $C> for $Peripheral {}
300299
)+
301300
};
302301
}

src/i2s.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,10 @@ mod dma {
446446
}
447447

448448
/// DMA is available for I2S based on the underlying implementations for SPI
449-
unsafe impl<SPI, PINS, MODE, STREAM, CHANNEL, DIR> DMASet<STREAM, CHANNEL, DIR>
449+
unsafe impl<SPI, PINS, MODE, STREAM, DIR, const CHANNEL: u8> DMASet<STREAM, DIR, CHANNEL>
450450
for stm32_i2s_v12x::I2s<I2s<SPI, PINS>, MODE>
451451
where
452-
SPI: DMASet<STREAM, CHANNEL, DIR>,
452+
SPI: DMASet<STREAM, DIR, CHANNEL>,
453453
{
454454
}
455455
}

0 commit comments

Comments
 (0)