Skip to content

How can I tell the Rust compiler &mut [u8] has changed after a DMA operation #537

@schultetwin1

Description

@schultetwin1

Hi all!

I'm trying to force the Rust compiler to assume the contents of a &mut [u8] has changed after a DMA operation.

In the std library, calls to things like std::fs::File::read take a &mut [u8] and after the call, the contents of that slice have changed. The Rust compiler knows it can not assume the contents of that buffer after the call to read.

I need to do something similar with DMA. After a DMA operation has been completed (and hardware has written bytes to memory), the Rust compiler must assume the buf has changed.

My current solution (below) is to use asm!. Will that do the trick? Is there a more portable way?

Something like:

fn dma_read(&mut self, buf: &mut [u8]) {
    // Zero out buffer
    buf.fill(0);

    let addr = buf.as_mut_ptr() as usize;
    let len = buf.len();

    // Setup DMA via MMIO
    self.reg.dma_addr.set(addr);
    self.reg.dma_len.set(len);

    // Start DMA via MMIO
    self.reg.dma_start.set(1);

    // Wait for DMA to complete via MMIO
    while self.reg.dma_done.get() != 1 {}

    // Insert an `asm!` block to force Rust to assume `buf` has been modified
    unsafe { core::arch::asm!("mov {},{}", out(reg) _, in(reg) addr) };

    // Read "buf"
    // At this point, how can I ensure the Rust compiler will assume buf has been modified?
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy