4
4
5
5
mod utilities;
6
6
7
+ use core:: mem:: MaybeUninit ;
8
+
7
9
use cortex_m_rt:: entry;
8
10
use cortex_m_semihosting:: debug;
9
11
use stm32h5xx_hal:: {
@@ -12,6 +14,114 @@ use stm32h5xx_hal::{
12
14
prelude:: * ,
13
15
} ;
14
16
17
+ static mut SOURCE_BYTES : MaybeUninit < [ u8 ; 40 ] > = MaybeUninit :: uninit ( ) ;
18
+ static mut DEST_BYTES : MaybeUninit < [ u8 ; 40 ] > = MaybeUninit :: zeroed ( ) ;
19
+ static mut DEST_HALF_WORDS : MaybeUninit < [ u16 ; 20 ] > = MaybeUninit :: uninit ( ) ;
20
+ static mut SOURCE_WORDS : MaybeUninit < [ u32 ; 10 ] > = MaybeUninit :: uninit ( ) ;
21
+ static mut DEST_WORDS : MaybeUninit < [ u32 ; 10 ] > = MaybeUninit :: uninit ( ) ;
22
+
23
+ fn u8_to_u8_sequential ( ) -> ( & ' static [ u8 ; 40 ] , & ' static mut [ u8 ; 40 ] ) {
24
+ let buf: & mut [ MaybeUninit < u8 > ; 40 ] = unsafe {
25
+ & mut * ( core:: ptr:: addr_of_mut!( SOURCE_BYTES )
26
+ as * mut [ MaybeUninit < u8 > ; 40 ] )
27
+ } ;
28
+
29
+ for ( i, value) in buf. iter_mut ( ) . enumerate ( ) {
30
+ unsafe {
31
+ value. as_mut_ptr ( ) . write ( i as u8 ) ;
32
+ }
33
+ }
34
+ #[ allow( static_mut_refs) ] // TODO: Fix this
35
+ let src = unsafe { SOURCE_BYTES . assume_init_ref ( ) } ;
36
+
37
+ let dest =
38
+ unsafe { ( * core:: ptr:: addr_of_mut!( DEST_BYTES ) ) . assume_init_mut ( ) } ;
39
+
40
+ dest. fill ( 0 ) ;
41
+
42
+ ( src, dest)
43
+ }
44
+
45
+ fn u32_to_u32_transform ( ) -> ( & ' static [ u32 ; 10 ] , & ' static mut [ u32 ; 10 ] ) {
46
+ let buf: & mut [ MaybeUninit < u32 > ; 10 ] = unsafe {
47
+ & mut * ( core:: ptr:: addr_of_mut!( SOURCE_WORDS )
48
+ as * mut [ MaybeUninit < u32 > ; 10 ] )
49
+ } ;
50
+
51
+ buf. fill ( MaybeUninit :: new ( 0x12345678 ) ) ;
52
+
53
+ #[ allow( static_mut_refs) ] // TODO: Fix this
54
+ let src = unsafe { SOURCE_WORDS . assume_init_ref ( ) } ;
55
+
56
+ let dest =
57
+ unsafe { ( * core:: ptr:: addr_of_mut!( DEST_WORDS ) ) . assume_init_mut ( ) } ;
58
+
59
+ dest. fill ( 0 ) ;
60
+ ( src, dest)
61
+ }
62
+
63
+ fn u32_to_u16_truncate ( ) -> ( & ' static [ u32 ; 10 ] , & ' static mut [ u16 ; 20 ] ) {
64
+ let buf: & mut [ MaybeUninit < u32 > ; 10 ] = unsafe {
65
+ & mut * ( core:: ptr:: addr_of_mut!( SOURCE_WORDS )
66
+ as * mut [ MaybeUninit < u32 > ; 10 ] )
67
+ } ;
68
+
69
+ buf. fill ( MaybeUninit :: new ( 0x12345678 ) ) ;
70
+
71
+ #[ allow( static_mut_refs) ] // TODO: Fix this
72
+ let src = unsafe { SOURCE_WORDS . assume_init_ref ( ) } ;
73
+
74
+ let dest = unsafe {
75
+ ( * core:: ptr:: addr_of_mut!( DEST_HALF_WORDS ) ) . assume_init_mut ( )
76
+ } ;
77
+
78
+ dest. fill ( 0 ) ;
79
+ ( src, dest)
80
+ }
81
+
82
+ fn u32_to_u8_unpack ( ) -> ( & ' static [ u32 ; 10 ] , & ' static mut [ u8 ; 40 ] ) {
83
+ let buf: & mut [ MaybeUninit < u32 > ; 10 ] = unsafe {
84
+ & mut * ( core:: ptr:: addr_of_mut!( SOURCE_WORDS )
85
+ as * mut [ MaybeUninit < u32 > ; 10 ] )
86
+ } ;
87
+
88
+ buf. fill ( MaybeUninit :: new ( 0x12345678 ) ) ;
89
+
90
+ #[ allow( static_mut_refs) ] // TODO: Fix this
91
+ let src = unsafe { SOURCE_WORDS . assume_init_ref ( ) } ;
92
+
93
+ let dest =
94
+ unsafe { ( * core:: ptr:: addr_of_mut!( DEST_BYTES ) ) . assume_init_mut ( ) } ;
95
+
96
+ dest. fill ( 0 ) ;
97
+ ( src, dest)
98
+ }
99
+
100
+ fn u8_to_u32_pack ( ) -> ( & ' static [ u8 ; 40 ] , & ' static mut [ u32 ; 10 ] ) {
101
+ let buf: & mut [ MaybeUninit < u8 > ; 40 ] = unsafe {
102
+ & mut * ( core:: ptr:: addr_of_mut!( SOURCE_BYTES )
103
+ as * mut [ MaybeUninit < u8 > ; 40 ] )
104
+ } ;
105
+
106
+ for chunk in buf. chunks_mut ( 4 ) {
107
+ unsafe {
108
+ chunk[ 0 ] . as_mut_ptr ( ) . write ( 0x78 ) ;
109
+ chunk[ 1 ] . as_mut_ptr ( ) . write ( 0x56 ) ;
110
+ chunk[ 2 ] . as_mut_ptr ( ) . write ( 0x34 ) ;
111
+ chunk[ 3 ] . as_mut_ptr ( ) . write ( 0x12 ) ;
112
+ }
113
+ }
114
+
115
+ #[ allow( static_mut_refs) ] // TODO: Fix this
116
+ let src = unsafe { SOURCE_BYTES . assume_init_ref ( ) } ;
117
+
118
+ let dest =
119
+ unsafe { ( * core:: ptr:: addr_of_mut!( DEST_WORDS ) ) . assume_init_mut ( ) } ;
120
+
121
+ dest. fill ( 0 ) ;
122
+ ( src, dest)
123
+ }
124
+
15
125
#[ entry]
16
126
fn main ( ) -> ! {
17
127
utilities:: logger:: init ( ) ;
@@ -27,85 +137,72 @@ fn main() -> ! {
27
137
28
138
let channels = dp. GPDMA1 . channels ( ccdr. peripheral . GPDMA1 ) ;
29
139
30
- // u8 to u8
31
- log:: info!( "u8 to u8 transfer" ) ;
32
- let src: [ u8 ; 40 ] = core:: array:: from_fn ( |i| i as u8 ) ;
33
- let dest = & mut [ 0u8 ; 40 ] ;
140
+ let ( source_buf, dest_buf) = u8_to_u8_sequential ( ) ;
141
+ let source_copy = unsafe { & * ( source_buf. as_ptr ( ) as * const [ u8 ; 40 ] ) } ;
142
+ let dest_copy = unsafe { & * ( dest_buf. as_ptr ( ) as * const [ u8 ; 40 ] ) } ;
34
143
35
144
let channel = channels. 0 ;
36
145
let config = DmaConfig :: new ( ) ;
37
- let transfer = DmaTransfer :: memory_to_memory ( config, & channel, & src, dest) ;
146
+ let transfer =
147
+ DmaTransfer :: memory_to_memory ( config, & channel, source_buf, dest_buf) ;
38
148
transfer. start ( ) . unwrap ( ) ;
39
149
transfer. wait_for_transfer_complete ( ) . unwrap ( ) ;
40
- assert_eq ! ( src , * dest , "u8 to u8 transfer failed" ) ;
150
+ assert_eq ! ( source_copy , dest_copy ) ;
41
151
42
- // u32 to u32 with data transform
43
- log:: info!( "u32 to u32 with data transform" ) ;
44
- let src = [ 0x12345678u32 ; 10 ] ;
45
- let dest = & mut [ 0u32 ; 10 ] ;
152
+ let ( source_buf, dest_buf) = u32_to_u32_transform ( ) ;
153
+ let dest_copy = unsafe { & * ( dest_buf. as_ptr ( ) as * const [ u32 ; 10 ] ) } ;
46
154
let config = DmaConfig :: new ( ) . with_data_transform (
47
155
DataTransform :: builder ( )
48
156
. swap_destination_half_words ( )
49
157
. swap_destination_half_word_byte_order ( ) ,
50
158
) ;
51
159
52
- let transfer = DmaTransfer :: memory_to_memory ( config, & channel, & src, dest) ;
160
+ let transfer =
161
+ DmaTransfer :: memory_to_memory ( config, & channel, source_buf, dest_buf) ;
53
162
54
163
transfer. start ( ) . unwrap ( ) ;
55
164
transfer. wait_for_transfer_complete ( ) . unwrap ( ) ;
56
- assert_eq ! (
57
- [ 0x78563412 ; 10 ] , * dest,
58
- "u32 to u32 with data transform failed"
59
- ) ;
165
+ let expected = [ 0x78563412 ; 10 ] ;
166
+ assert_eq ! ( expected, * dest_copy) ;
60
167
61
- // u32 to u16 with truncate
62
- log:: info!( "u32 to u16 with truncate" ) ;
63
- let src = [ 0x12345678u32 ; 10 ] ;
64
- let dest = & mut [ 0u16 ; 20 ] ;
168
+ let ( source_buf, dest_buf) = u32_to_u16_truncate ( ) ;
169
+ let dest_copy = unsafe { & * ( dest_buf. as_ptr ( ) as * const [ u16 ; 20 ] ) } ;
65
170
let config = DmaConfig :: new ( ) . with_data_transform (
66
171
DataTransform :: builder ( ) . left_align_right_truncate ( ) ,
67
172
) ;
68
- let transfer = DmaTransfer :: memory_to_memory ( config, & channel, & src, dest) ;
173
+ let transfer =
174
+ DmaTransfer :: memory_to_memory ( config, & channel, source_buf, dest_buf) ;
69
175
70
176
transfer. start ( ) . unwrap ( ) ;
71
177
transfer. wait_for_transfer_complete ( ) . unwrap ( ) ;
72
- assert_eq ! (
73
- [ 0x1234 ; 10 ] ,
74
- ( * dest) [ 0 ..10 ] ,
75
- "u32 to u16 with truncate failed"
76
- ) ;
77
- assert_eq ! ( [ 0 ; 10 ] , ( * dest) [ 10 ..20 ] , "u32 to u16 with truncate failed" ) ;
178
+ let expected = [ 0x1234 ; 10 ] ;
179
+ assert_eq ! ( expected, ( * dest_copy) [ 0 ..10 ] ) ;
78
180
79
- // u32 to u8 with unpack
80
- log:: info!( "u32 to u8 with unpack" ) ;
81
- let src = [ 0x12345678u32 ; 10 ] ;
82
- let dest = & mut [ 0u8 ; 40 ] ;
181
+ let ( source_buf, dest_buf) = u32_to_u8_unpack ( ) ;
182
+ let dest_copy = unsafe { & * ( dest_buf. as_ptr ( ) as * const [ u8 ; 40 ] ) } ;
83
183
let config =
84
184
DmaConfig :: new ( ) . with_data_transform ( DataTransform :: builder ( ) . unpack ( ) ) ;
85
- let transfer = DmaTransfer :: memory_to_memory ( config, & channel, & src, dest) ;
185
+ let transfer =
186
+ DmaTransfer :: memory_to_memory ( config, & channel, source_buf, dest_buf) ;
86
187
87
188
transfer. start ( ) . unwrap ( ) ;
88
189
transfer. wait_for_transfer_complete ( ) . unwrap ( ) ;
89
190
let expected = [ 0x78 , 0x56 , 0x34 , 0x12 ] ;
90
- assert_eq ! ( expected, ( * dest) [ 0 ..4 ] , "u32 to u8 unpack failed" ) ;
91
- assert_eq ! ( expected, ( * dest) [ 36 ..40 ] , "u32 to u8 unpack failed" ) ;
92
-
93
- // u8 to u32 with pack
94
- log:: info!( "u8 to u32 with pack" ) ;
95
- let mut src = [ 0u8 ; 40 ] ;
96
- let dest = & mut [ 0u32 ; 10 ] ;
97
- for chunk in src. chunks_mut ( 4 ) {
98
- chunk. copy_from_slice ( & [ 0x78 , 0x56 , 0x34 , 0x12 ] ) ;
99
- }
191
+ assert_eq ! ( expected, ( * dest_copy) [ 0 ..4 ] ) ;
192
+ assert_eq ! ( expected, ( * dest_copy) [ 36 ..40 ] ) ;
193
+
194
+ let ( source_buf, dest_buf) = u8_to_u32_pack ( ) ;
195
+ let dest_copy = unsafe { & * ( dest_buf. as_ptr ( ) as * const [ u32 ; 10 ] ) } ;
100
196
let config =
101
197
DmaConfig :: new ( ) . with_data_transform ( DataTransform :: builder ( ) . pack ( ) ) ;
102
- let transfer = DmaTransfer :: memory_to_memory ( config, & channel, & src, dest) ;
198
+ let transfer =
199
+ DmaTransfer :: memory_to_memory ( config, & channel, source_buf, dest_buf) ;
103
200
104
201
transfer. start ( ) . unwrap ( ) ;
105
202
transfer. wait_for_transfer_complete ( ) . unwrap ( ) ;
106
- assert_eq ! ( [ 0x12345678 ; 10 ] , ( * dest ) , "u8 to u32 with pack failed" ) ;
107
-
108
- log :: info! ( "All tests passed!" ) ;
203
+ let expected = [ 0x12345678 ; 10 ] ;
204
+ assert_eq ! ( expected , ( * dest_copy ) ) ;
205
+ assert_eq ! ( expected , ( * dest_copy ) ) ;
109
206
110
207
loop {
111
208
debug:: exit ( debug:: EXIT_SUCCESS )
0 commit comments