-
Notifications
You must be signed in to change notification settings - Fork 5
gpdma: add GPDMA driver #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f5cb2a7
gpdma: add GPDMA driver
astapleton c65d457
Fixes, simpler dma example
astapleton 69eab2a
more fixes
astapleton 5d9fc22
missed some defmt derives
astapleton 6f3aa81
use ReadBuffer, WriteBuffer to force immovable buffers; fix example
astapleton 95e3f78
fix example again
astapleton 793a9d5
doc clarification
astapleton 336a8c4
Use mutable reference to channels when creating transfers
astapleton 9bbaca7
fix data width check; remove incomplete documentation
astapleton 5c587f3
Hold onto source/destination; Remove UB from example
astapleton cd32400
mute type complexity clippy
astapleton 5b0d9be
rename fields
astapleton 100bf1b
Add free method, don't consume self in abort/wait_for_transfer_complete
astapleton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
// #![deny(warnings)] | ||
#![no_main] | ||
#![no_std] | ||
|
||
mod utilities; | ||
|
||
use core::mem::MaybeUninit; | ||
|
||
use cortex_m_rt::entry; | ||
use cortex_m_semihosting::debug; | ||
use stm32h5xx_hal::{ | ||
gpdma::{config::transform::*, DmaConfig, DmaTransfer}, | ||
pac, | ||
prelude::*, | ||
}; | ||
|
||
static mut SOURCE_BYTES: MaybeUninit<[u8; 40]> = MaybeUninit::uninit(); | ||
static mut DEST_BYTES: MaybeUninit<[u8; 40]> = MaybeUninit::zeroed(); | ||
static mut DEST_HALF_WORDS: MaybeUninit<[u16; 20]> = MaybeUninit::uninit(); | ||
static mut SOURCE_WORDS: MaybeUninit<[u32; 10]> = MaybeUninit::uninit(); | ||
static mut DEST_WORDS: MaybeUninit<[u32; 10]> = MaybeUninit::uninit(); | ||
|
||
fn u8_to_u8_sequential() -> (&'static [u8; 40], &'static mut [u8; 40]) { | ||
let buf: &mut [MaybeUninit<u8>; 40] = unsafe { | ||
&mut *(core::ptr::addr_of_mut!(SOURCE_BYTES) | ||
as *mut [MaybeUninit<u8>; 40]) | ||
}; | ||
|
||
for (i, value) in buf.iter_mut().enumerate() { | ||
unsafe { | ||
value.as_mut_ptr().write(i as u8); | ||
} | ||
} | ||
#[allow(static_mut_refs)] // TODO: Fix this | ||
let src = unsafe { SOURCE_BYTES.assume_init_ref() }; | ||
|
||
let dest = | ||
unsafe { (*core::ptr::addr_of_mut!(DEST_BYTES)).assume_init_mut() }; | ||
|
||
dest.fill(0); | ||
|
||
(src, dest) | ||
} | ||
|
||
fn u32_to_u32_transform() -> (&'static [u32; 10], &'static mut [u32; 10]) { | ||
let buf: &mut [MaybeUninit<u32>; 10] = unsafe { | ||
&mut *(core::ptr::addr_of_mut!(SOURCE_WORDS) | ||
as *mut [MaybeUninit<u32>; 10]) | ||
}; | ||
|
||
buf.fill(MaybeUninit::new(0x12345678)); | ||
|
||
#[allow(static_mut_refs)] // TODO: Fix this | ||
let src = unsafe { SOURCE_WORDS.assume_init_ref() }; | ||
|
||
let dest = | ||
unsafe { (*core::ptr::addr_of_mut!(DEST_WORDS)).assume_init_mut() }; | ||
|
||
dest.fill(0); | ||
(src, dest) | ||
} | ||
|
||
fn u32_to_u16_truncate() -> (&'static [u32; 10], &'static mut [u16; 20]) { | ||
let buf: &mut [MaybeUninit<u32>; 10] = unsafe { | ||
&mut *(core::ptr::addr_of_mut!(SOURCE_WORDS) | ||
as *mut [MaybeUninit<u32>; 10]) | ||
}; | ||
|
||
buf.fill(MaybeUninit::new(0x12345678)); | ||
|
||
#[allow(static_mut_refs)] // TODO: Fix this | ||
let src = unsafe { SOURCE_WORDS.assume_init_ref() }; | ||
|
||
let dest = unsafe { | ||
(*core::ptr::addr_of_mut!(DEST_HALF_WORDS)).assume_init_mut() | ||
}; | ||
|
||
dest.fill(0); | ||
(src, dest) | ||
} | ||
|
||
fn u32_to_u8_unpack() -> (&'static [u32; 10], &'static mut [u8; 40]) { | ||
let buf: &mut [MaybeUninit<u32>; 10] = unsafe { | ||
&mut *(core::ptr::addr_of_mut!(SOURCE_WORDS) | ||
as *mut [MaybeUninit<u32>; 10]) | ||
}; | ||
|
||
buf.fill(MaybeUninit::new(0x12345678)); | ||
|
||
#[allow(static_mut_refs)] // TODO: Fix this | ||
let src = unsafe { SOURCE_WORDS.assume_init_ref() }; | ||
|
||
let dest = | ||
unsafe { (*core::ptr::addr_of_mut!(DEST_BYTES)).assume_init_mut() }; | ||
|
||
dest.fill(0); | ||
(src, dest) | ||
} | ||
|
||
fn u8_to_u32_pack() -> (&'static [u8; 40], &'static mut [u32; 10]) { | ||
let buf: &mut [MaybeUninit<u8>; 40] = unsafe { | ||
&mut *(core::ptr::addr_of_mut!(SOURCE_BYTES) | ||
as *mut [MaybeUninit<u8>; 40]) | ||
}; | ||
|
||
for chunk in buf.chunks_mut(4) { | ||
unsafe { | ||
chunk[0].as_mut_ptr().write(0x78); | ||
chunk[1].as_mut_ptr().write(0x56); | ||
chunk[2].as_mut_ptr().write(0x34); | ||
chunk[3].as_mut_ptr().write(0x12); | ||
} | ||
} | ||
|
||
#[allow(static_mut_refs)] // TODO: Fix this | ||
let src = unsafe { SOURCE_BYTES.assume_init_ref() }; | ||
|
||
let dest = | ||
unsafe { (*core::ptr::addr_of_mut!(DEST_WORDS)).assume_init_mut() }; | ||
|
||
dest.fill(0); | ||
(src, dest) | ||
} | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
utilities::logger::init(); | ||
|
||
let dp = pac::Peripherals::take().unwrap(); | ||
|
||
let pwr = dp.PWR.constrain(); | ||
let pwrcfg = pwr.vos0().freeze(); | ||
|
||
// Constrain and Freeze clock | ||
let rcc = dp.RCC.constrain(); | ||
let ccdr = rcc.sys_ck(250.MHz()).freeze(pwrcfg, &dp.SBS); | ||
|
||
let channels = dp.GPDMA1.channels(ccdr.peripheral.GPDMA1); | ||
|
||
let (source_buf, dest_buf) = u8_to_u8_sequential(); | ||
|
||
let channel = channels.0; | ||
let config = DmaConfig::new(); | ||
let transfer = | ||
DmaTransfer::memory_to_memory(config, &channel, source_buf, dest_buf); | ||
transfer.start().unwrap(); | ||
transfer.wait_for_transfer_complete().unwrap(); | ||
assert_eq!(source_buf, dest_buf); | ||
|
||
let (source_buf, dest_buf) = u32_to_u32_transform(); | ||
let config = DmaConfig::new().with_data_transform( | ||
DataTransform::builder() | ||
.swap_destination_half_words() | ||
.swap_destination_half_word_byte_order(), | ||
); | ||
|
||
let transfer = | ||
DmaTransfer::memory_to_memory(config, &channel, source_buf, dest_buf); | ||
|
||
transfer.start().unwrap(); | ||
transfer.wait_for_transfer_complete().unwrap(); | ||
let expected = [0x78563412; 10]; | ||
assert_eq!(expected, *dest_buf); | ||
|
||
let (source_buf, dest_buf) = u32_to_u16_truncate(); | ||
let config = DmaConfig::new().with_data_transform( | ||
DataTransform::builder().left_align_right_truncate(), | ||
); | ||
let transfer = | ||
DmaTransfer::memory_to_memory(config, &channel, source_buf, dest_buf); | ||
|
||
transfer.start().unwrap(); | ||
transfer.wait_for_transfer_complete().unwrap(); | ||
let expected = [0x1234; 10]; | ||
assert_eq!(expected, (*dest_buf)[0..10]); | ||
|
||
let (source_buf, dest_buf) = u32_to_u8_unpack(); | ||
let config = | ||
DmaConfig::new().with_data_transform(DataTransform::builder().unpack()); | ||
let transfer = | ||
DmaTransfer::memory_to_memory(config, &channel, source_buf, dest_buf); | ||
|
||
transfer.start().unwrap(); | ||
transfer.wait_for_transfer_complete().unwrap(); | ||
let expected = [0x78, 0x56, 0x34, 0x12]; | ||
assert_eq!(expected, (*dest_buf)[0..4]); | ||
assert_eq!(expected, (*dest_buf)[36..40]); | ||
|
||
let (source_buf, dest_buf) = u8_to_u32_pack(); | ||
let config = | ||
DmaConfig::new().with_data_transform(DataTransform::builder().pack()); | ||
let transfer = | ||
DmaTransfer::memory_to_memory(config, &channel, source_buf, dest_buf); | ||
|
||
transfer.start().unwrap(); | ||
transfer.wait_for_transfer_complete().unwrap(); | ||
let expected = [0x12345678; 10]; | ||
assert_eq!(expected, (*dest_buf)); | ||
assert_eq!(expected, (*dest_buf)); | ||
|
||
loop { | ||
debug::exit(debug::EXIT_SUCCESS) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.