Skip to content

Commit ff526e1

Browse files
committed
refactor: update DMA transfer functions to use separate memory and peripheral sizes
1 parent 90b4164 commit ff526e1

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

embassy-stm32/src/dma/dma_bdma.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ impl AnyChannel {
340340
mem_addr: *mut u32,
341341
mem_len: usize,
342342
incr_mem: bool,
343-
data_size: WordSize,
343+
mem_size: WordSize,
344+
peripheral_size: WordSize,
344345
options: TransferOptions,
345346
) {
346347
let info = self.info();
@@ -380,8 +381,8 @@ impl AnyChannel {
380381
});
381382
ch.cr().write(|w| {
382383
w.set_dir(dir.into());
383-
w.set_msize(data_size.into());
384-
w.set_psize(data_size.into());
384+
w.set_msize(mem_size.into());
385+
w.set_psize(peripheral_size.into());
385386
w.set_pl(options.priority.into());
386387
w.set_minc(incr_mem);
387388
w.set_pinc(false);
@@ -414,8 +415,8 @@ impl AnyChannel {
414415
ch.mar().write_value(mem_addr as u32);
415416
ch.ndtr().write(|w| w.set_ndt(mem_len as u16));
416417
ch.cr().write(|w| {
417-
w.set_psize(data_size.into());
418-
w.set_msize(data_size.into());
418+
w.set_psize(peripheral_size.into());
419+
w.set_msize(mem_size.into());
419420
w.set_minc(incr_mem);
420421
w.set_dir(dir.into());
421422
w.set_teie(true);
@@ -602,27 +603,28 @@ impl<'a> Transfer<'a> {
602603
buf.len(),
603604
true,
604605
W::size(),
606+
W::size(),
605607
options,
606608
)
607609
}
608610

609611
/// Create a new write DMA transfer (memory to peripheral).
610-
pub unsafe fn new_write<W: Word>(
612+
pub unsafe fn new_write<MW: Word, PW: Word>(
611613
channel: impl Peripheral<P = impl Channel> + 'a,
612614
request: Request,
613-
buf: &'a [W],
614-
peri_addr: *mut W,
615+
buf: &'a [MW],
616+
peri_addr: *mut PW,
615617
options: TransferOptions,
616618
) -> Self {
617619
Self::new_write_raw(channel, request, buf, peri_addr, options)
618620
}
619621

620622
/// Create a new write DMA transfer (memory to peripheral), using raw pointers.
621-
pub unsafe fn new_write_raw<W: Word>(
623+
pub unsafe fn new_write_raw<W: Word, PW: Word>(
622624
channel: impl Peripheral<P = impl Channel> + 'a,
623625
request: Request,
624626
buf: *const [W],
625-
peri_addr: *mut W,
627+
peri_addr: *mut PW,
626628
options: TransferOptions,
627629
) -> Self {
628630
into_ref!(channel);
@@ -636,6 +638,7 @@ impl<'a> Transfer<'a> {
636638
buf.len(),
637639
true,
638640
W::size(),
641+
W::size(),
639642
options,
640643
)
641644
}
@@ -660,6 +663,7 @@ impl<'a> Transfer<'a> {
660663
count,
661664
false,
662665
W::size(),
666+
W::size(),
663667
options,
664668
)
665669
}
@@ -673,15 +677,23 @@ impl<'a> Transfer<'a> {
673677
mem_len: usize,
674678
incr_mem: bool,
675679
data_size: WordSize,
680+
peripheral_size: WordSize,
676681
options: TransferOptions,
677682
) -> Self {
678683
assert!(mem_len > 0 && mem_len <= 0xFFFF);
679684

680685
channel.configure(
681-
_request, dir, peri_addr, mem_addr, mem_len, incr_mem, data_size, options,
686+
_request,
687+
dir,
688+
peri_addr,
689+
mem_addr,
690+
mem_len,
691+
incr_mem,
692+
data_size,
693+
peripheral_size,
694+
options,
682695
);
683696
channel.start();
684-
685697
Self { channel }
686698
}
687699

@@ -814,6 +826,7 @@ impl<'a, W: Word> ReadableRingBuffer<'a, W> {
814826
len,
815827
true,
816828
data_size,
829+
data_size,
817830
options,
818831
);
819832

@@ -966,6 +979,7 @@ impl<'a, W: Word> WritableRingBuffer<'a, W> {
966979
len,
967980
true,
968981
data_size,
982+
data_size,
969983
options,
970984
);
971985

embassy-stm32/src/timer/simple_pwm.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ impl<'d, T: GeneralInstance4Channel> SimplePwm<'d, T> {
334334
&mut dma,
335335
req,
336336
duty,
337-
self.inner.regs_1ch().ccr(channel.index()).as_ptr() as *mut _,
337+
self.inner.regs_1ch().ccr(channel.index()).as_ptr() as *mut u16,
338338
dma_transfer_option,
339339
)
340340
.await
@@ -362,13 +362,7 @@ macro_rules! impl_waveform_chx {
362362
($fn_name:ident, $dma_ch:ident, $cc_ch:ident) => {
363363
impl<'d, T: GeneralInstance4Channel> SimplePwm<'d, T> {
364364
/// Generate a sequence of PWM waveform
365-
///
366-
/// Note:
367-
/// 1. you will need to provide corresponding TIMx_CHy DMA channel to use this method.
368-
/// 2. Please make sure the duty data length is aligned to the timer data width(16-bit or 32-bit).
369-
/// 3. Please notice the endianess of the duty data. STM32 use little endian,
370-
/// for example, 0x12345678 as u32 will be stored as [0x78, 0x56, 0x34, 0x12] in memory.
371-
pub async fn $fn_name(&mut self, dma: impl Peripheral<P = impl super::$dma_ch<T>>, duty: &[u8]) {
365+
pub async fn $fn_name(&mut self, dma: impl Peripheral<P = impl super::$dma_ch<T>>, duty: &[u16]) {
372366
use crate::pac::timer::vals::Ccds;
373367

374368
into_ref!(dma);
@@ -411,32 +405,30 @@ macro_rules! impl_waveform_chx {
411405

412406
match self.inner.bits() {
413407
TimerBits::Bits16 => {
414-
// the data must be aligned to double words
415-
assert!(duty.len() % 2 == 0);
416-
let duty = core::slice::from_raw_parts(duty.as_ptr() as *const u16, duty.len() / 2);
417408
Transfer::new_write(
418409
&mut dma,
419410
req,
420411
duty,
421-
self.inner.regs_gp16().ccr(cc_channel.index()).as_ptr() as *mut _,
412+
self.inner.regs_gp16().ccr(cc_channel.index()).as_ptr() as *mut u16,
422413
dma_transfer_option,
423414
)
424415
.await
425416
}
426-
#[cfg(not(stm32l0))]
417+
#[cfg(not(any(stm32l0, bdma, gpdma)))]
427418
TimerBits::Bits32 => {
428-
// the data must be aligned to quad words
429-
assert!(duty.len() % 4 == 0);
430-
let duty = core::slice::from_raw_parts(duty.as_ptr() as *const u32, duty.len() / 4);
431419
Transfer::new_write(
432420
&mut dma,
433421
req,
434422
duty,
435-
self.inner.regs_gp16().ccr(cc_channel.index()).as_ptr() as *mut _,
423+
self.inner.regs_gp16().ccr(cc_channel.index()).as_ptr() as *mut u32,
436424
dma_transfer_option,
437425
)
438426
.await
439427
}
428+
#[cfg(any(stm32l0, bdma, gpdma))]
429+
_ => {
430+
panic!("unsupported timer bits")
431+
}
440432
};
441433
};
442434

0 commit comments

Comments
 (0)