stm32/spi: add support for all word sizes.

Co-Authored-By: anton smeenk <asmeenk@planet.nl>
This commit is contained in:
Dario Nieuwenhuis
2023-04-18 20:56:23 +02:00
parent a673b9aa29
commit 2080d8bb6d
6 changed files with 197 additions and 140 deletions

View File

@ -9,7 +9,8 @@ use embassy_cortex_m::interrupt::Priority;
use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
use super::{Dir, Word, WordSize};
use super::word::{Word, WordSize};
use super::Dir;
use crate::_generated::BDMA_CHANNEL_COUNT;
use crate::interrupt::{Interrupt, InterruptExt};
use crate::pac;
@ -167,7 +168,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
ptr as *mut u32,
len,
true,
W::bits(),
W::size(),
options,
)
}
@ -202,7 +203,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
ptr as *mut u32,
len,
true,
W::bits(),
W::size(),
options,
)
}
@ -225,7 +226,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
repeated as *const W as *mut u32,
count,
false,
W::bits(),
W::size(),
options,
)
}

View File

@ -9,7 +9,8 @@ use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
use pac::dma::regs;
use super::{Dir, Word, WordSize};
use super::word::{Word, WordSize};
use super::Dir;
use crate::_generated::DMA_CHANNEL_COUNT;
use crate::interrupt::{Interrupt, InterruptExt};
use crate::pac::dma::vals;
@ -246,7 +247,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
ptr as *mut u32,
len,
true,
W::bits(),
W::size(),
options,
)
}
@ -281,7 +282,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
ptr as *mut u32,
len,
true,
W::bits(),
W::size(),
options,
)
}
@ -304,7 +305,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
repeated as *const W as *mut u32,
count,
false,
W::bits(),
W::size(),
options,
)
}
@ -464,7 +465,7 @@ impl<'a, C: Channel, W: Word> DoubleBuffered<'a, C, W> {
assert!(len > 0 && len <= 0xFFFF);
let dir = Dir::PeripheralToMemory;
let data_size = W::bits();
let data_size = W::size();
let channel_number = channel.num();
let dma = channel.regs();

View File

@ -9,7 +9,8 @@ use embassy_cortex_m::interrupt::Priority;
use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
use super::{Dir, Word, WordSize};
use super::word::{Word, WordSize};
use super::Dir;
use crate::_generated::GPDMA_CHANNEL_COUNT;
use crate::interrupt::{Interrupt, InterruptExt};
use crate::pac;
@ -165,7 +166,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
ptr as *mut u32,
len,
true,
W::bits(),
W::size(),
options,
)
}
@ -200,7 +201,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
ptr as *mut u32,
len,
true,
W::bits(),
W::size(),
options,
)
}
@ -223,7 +224,7 @@ impl<'a, C: Channel> Transfer<'a, C> {
repeated as *const W as *mut u32,
count,
false,
W::bits(),
W::size(),
options,
)
}

View File

@ -21,6 +21,8 @@ pub use gpdma::*;
#[cfg(dmamux)]
mod dmamux;
pub mod word;
use core::mem;
use embassy_cortex_m::interrupt::Priority;
@ -36,53 +38,6 @@ enum Dir {
PeripheralToMemory,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WordSize {
OneByte,
TwoBytes,
FourBytes,
}
impl WordSize {
pub fn bytes(&self) -> usize {
match self {
Self::OneByte => 1,
Self::TwoBytes => 2,
Self::FourBytes => 4,
}
}
}
mod word_sealed {
pub trait Word {}
}
pub trait Word: word_sealed::Word {
fn bits() -> WordSize;
}
impl word_sealed::Word for u8 {}
impl Word for u8 {
fn bits() -> WordSize {
WordSize::OneByte
}
}
impl word_sealed::Word for u16 {}
impl Word for u16 {
fn bits() -> WordSize {
WordSize::TwoBytes
}
}
impl word_sealed::Word for u32 {}
impl Word for u32 {
fn bits() -> WordSize {
WordSize::FourBytes
}
}
pub struct NoDma;
impl_peripheral!(NoDma);

View File

@ -0,0 +1,79 @@
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WordSize {
OneByte,
TwoBytes,
FourBytes,
}
impl WordSize {
pub fn bytes(&self) -> usize {
match self {
Self::OneByte => 1,
Self::TwoBytes => 2,
Self::FourBytes => 4,
}
}
}
mod sealed {
pub trait Word {}
}
pub trait Word: sealed::Word + Default + Copy + 'static {
fn size() -> WordSize;
fn bits() -> usize;
}
macro_rules! impl_word {
(_, $T:ident, $bits:literal, $size:ident) => {
impl sealed::Word for $T {}
impl Word for $T {
fn bits() -> usize {
$bits
}
fn size() -> WordSize {
WordSize::$size
}
}
};
($T:ident, $uX:ident, $bits:literal, $size:ident) => {
#[repr(transparent)]
#[derive(Copy, Clone, Default)]
pub struct $T(pub $uX);
impl_word!(_, $T, $bits, $size);
};
}
impl_word!(U1, u8, 1, OneByte);
impl_word!(U2, u8, 2, OneByte);
impl_word!(U3, u8, 3, OneByte);
impl_word!(U4, u8, 4, OneByte);
impl_word!(U5, u8, 5, OneByte);
impl_word!(U6, u8, 6, OneByte);
impl_word!(U7, u8, 7, OneByte);
impl_word!(_, u8, 8, OneByte);
impl_word!(U9, u16, 9, TwoBytes);
impl_word!(U10, u16, 10, TwoBytes);
impl_word!(U11, u16, 11, TwoBytes);
impl_word!(U12, u16, 12, TwoBytes);
impl_word!(U13, u16, 13, TwoBytes);
impl_word!(U14, u16, 14, TwoBytes);
impl_word!(U15, u16, 15, TwoBytes);
impl_word!(_, u16, 16, TwoBytes);
impl_word!(U17, u32, 17, FourBytes);
impl_word!(U18, u32, 18, FourBytes);
impl_word!(U19, u32, 19, FourBytes);
impl_word!(U20, u32, 20, FourBytes);
impl_word!(U21, u32, 21, FourBytes);
impl_word!(U22, u32, 22, FourBytes);
impl_word!(U23, u32, 23, FourBytes);
impl_word!(U24, u32, 24, FourBytes);
impl_word!(U25, u32, 25, FourBytes);
impl_word!(U26, u32, 26, FourBytes);
impl_word!(U27, u32, 27, FourBytes);
impl_word!(U28, u32, 28, FourBytes);
impl_word!(U29, u32, 29, FourBytes);
impl_word!(U30, u32, 30, FourBytes);
impl_word!(U31, u32, 31, FourBytes);
impl_word!(_, u32, 32, FourBytes);