Skip to content

Commit 0a76657

Browse files
committed
Less raw ptr wrangling for clearer semantics
1 parent 0f32c59 commit 0a76657

5 files changed

Lines changed: 16 additions & 14 deletions

File tree

hal/src/dmac/channel/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
3434
#![allow(unused_braces)]
3535

36-
use core::marker::PhantomData;
36+
use core::{marker::PhantomData, ptr::NonNull};
3737

3838
use atsamd_hal_macros::{hal_cfg, hal_macro_helper};
3939

@@ -342,9 +342,9 @@ impl<Id: ChId, S: Status> Channel<Id, S> {
342342
// SAFETY This is safe as we are only reading the descriptor's address,
343343
// and not actually writing any data to it. We also assume the descriptor
344344
// will never be moved.
345-
descriptor as *mut _
345+
NonNull::new(descriptor)
346346
} else {
347-
core::ptr::null_mut()
347+
None
348348
};
349349

350350
unsafe {
@@ -842,7 +842,7 @@ pub(crate) unsafe fn write_descriptor<Src: Buffer, Dst: Buffer<Beat = Src::Beat>
842842
descriptor: &mut DmacDescriptor,
843843
source: &mut Src,
844844
destination: &mut Dst,
845-
next: *mut DmacDescriptor,
845+
next: Option<NonNull<DmacDescriptor>>,
846846
) {
847847
let src_ptr = source.dma_ptr();
848848
let src_inc = source.incrementing();
@@ -864,7 +864,7 @@ pub(crate) unsafe fn write_descriptor<Src: Buffer, Dst: Buffer<Beat = Src::Beat>
864864
.with_beatsize(Src::Beat::BEATSIZE)
865865
.with_valid(true);
866866

867-
// Seems like we need a fence before the buffer pointer escaped the current
867+
// Seems like we need a fence before the buffer pointer "escapes" the current
868868
// function. I don't claim to fully understand why, or what the gnarly LLVM
869869
// optimization details might be. But seems like the buffer pointers must be
870870
// written somewhere with a _volatile_ access, _after_ an (asm) compiler
@@ -875,7 +875,7 @@ pub(crate) unsafe fn write_descriptor<Src: Buffer, Dst: Buffer<Beat = Src::Beat>
875875
core::ptr::from_mut(descriptor).write_volatile(DmacDescriptor {
876876
// Next descriptor address: 0x0 terminates the transaction (no linked list),
877877
// any other address points to the next block descriptor
878-
descaddr: next,
878+
descaddr: next.map(|n| n.as_ptr()).unwrap_or(core::ptr::null_mut()),
879879
// Source address: address of the last beat transfer source in block
880880
srcaddr: src_ptr as *mut _,
881881
// Destination address: address of the last beat transfer destination in block

hal/src/sercom/i2c/async_api.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ mod dma {
434434
use crate::dmac::{channel, sram::DmacDescriptor};
435435
use crate::sercom::dma::SharedSliceBuffer;
436436
use Operation::{Read, Write};
437+
use core::ptr::{self, NonNull};
437438

438439
const NUM_LINKED_TRANSFERS: usize = 16;
439440

@@ -485,7 +486,7 @@ mod dma {
485486
.unwrap_or_else(|_| panic!("BUG: DMAC descriptors overflow"));
486487
let last_descriptor = descriptors.last_mut().unwrap();
487488
let next_ptr =
488-
(last_descriptor as *mut DmacDescriptor).wrapping_add(1);
489+
NonNull::new((ptr::from_mut(last_descriptor)).wrapping_add(1));
489490

490491
unsafe {
491492
channel::write_descriptor(
@@ -509,7 +510,7 @@ mod dma {
509510
.unwrap_or_else(|_| panic!("BUG: DMAC descriptors overflow"));
510511
let last_descriptor = descriptors.last_mut().unwrap();
511512
let next_ptr =
512-
(last_descriptor as *mut DmacDescriptor).wrapping_add(1);
513+
NonNull::new((ptr::from_mut(last_descriptor)).wrapping_add(1));
513514

514515
let mut bytes = SharedSliceBuffer::from_slice(bytes);
515516
unsafe {

hal/src/sercom/i2c/impl_ehal.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ mod dma {
326326
address: u8,
327327
operations: &mut [i2c::Operation<'_>],
328328
) -> Result<(), Self::Error> {
329+
use core::ptr::{self, NonNull};
329330
use i2c::Operation::{Read, Write};
330331

331332
const NUM_LINKED_TRANSFERS: usize = 16;
@@ -375,7 +376,7 @@ mod dma {
375376
.unwrap_or_else(|_| panic!("BUG: DMAC descriptors overflow"));
376377
let last_descriptor = descriptors.last_mut().unwrap();
377378
let next_ptr =
378-
(last_descriptor as *mut DmacDescriptor).wrapping_add(1);
379+
NonNull::new((ptr::from_mut(last_descriptor)).wrapping_add(1));
379380

380381
unsafe {
381382
channel::write_descriptor(
@@ -399,7 +400,7 @@ mod dma {
399400
.unwrap_or_else(|_| panic!("BUG: DMAC descriptors overflow"));
400401
let last_descriptor = descriptors.last_mut().unwrap();
401402
let next_ptr =
402-
(last_descriptor as *mut DmacDescriptor).wrapping_add(1);
403+
NonNull::new((ptr::from_mut(last_descriptor)).wrapping_add(1));
403404

404405
let mut bytes = SharedSliceBuffer::from_slice(bytes);
405406
unsafe {

hal/src/sercom/spi/async_api/dma.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ where
343343
&mut sercom_ptr,
344344
&mut sink,
345345
// Add a null descriptor pointer to end the transfer.
346-
core::ptr::null_mut(),
346+
None,
347347
);
348348
}
349349

@@ -361,7 +361,7 @@ where
361361
&mut source,
362362
&mut sercom_ptr,
363363
// Add a null descriptor pointer to end the transfer.
364-
core::ptr::null_mut(),
364+
None,
365365
);
366366
}
367367

hal/src/sercom/spi/impl_ehal/dma.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ where
210210
&mut sercom_ptr,
211211
&mut sink,
212212
// Add a null descriptor pointer to end the transfer.
213-
core::ptr::null_mut(),
213+
None,
214214
);
215215
}
216216

@@ -228,7 +228,7 @@ where
228228
&mut source,
229229
&mut sercom_ptr,
230230
// Add a null descriptor pointer to end the transfer.
231-
core::ptr::null_mut(),
231+
None,
232232
);
233233
}
234234

0 commit comments

Comments
 (0)