Skip to main content

SArrayPtr

Struct SArrayPtr 

Source
pub struct SArrayPtr<T> {
    pub direction: DmaDirection,
    /* private fields */
}
Expand description

映射单个连续内存区域的 DMA 数组。

SArrayPtr<T> 将现有的缓冲区(如栈数组或堆分配的 slice)映射为 DMA 可访问区域。 与 DArray<T> 不同,此类型提供手动缓存同步控制。

§缓存同步

重要: SArrayPtr 不会在每次访问时自动同步缓存。 你必须使用特定方法进行缓存同步:

  • to_vec(): 读取前自动失效整个范围
  • copy_from_slice(): 写入后自动刷新整个范围
  • read()/set(): 执行自动缓存同步

§生命周期

SArrayPtr 在离开作用域时自动解除 DMA 映射,但不会自动同步缓存。 必须在映射解除前手动完成必要的缓存同步操作。

§类型参数

  • T: 数组元素类型

§示例

use dma_api::{DeviceDma, DmaDirection};

let mut buffer = [0u8; 4096];
let device = DeviceDma::new(0xFFFFFFFF, &my_dma_impl);

// 映射用于 DMA 写入
let mut mapping = device.map_single_array(&buffer, 64, DmaDirection::ToDevice)?;

// 必须手动刷新缓存
mapping.copy_from_slice(&data);

// ... 启动 DMA 传输 ...

// 映射在作用域结束时自动解映射

Fields§

§direction: DmaDirection

Implementations§

Source§

impl<T> SArrayPtr<T>

Source

pub fn copy_from_slice(&mut self, src: &[T])

从 slice 复制数据到映射的缓冲区。

复制完成后刷新整个缓冲区的 CPU 缓存(ToDevice/Bidirectional)。 这是手动同步缓存的主要方式之一。

§参数
  • src: 源 slice
§Panics

如果源 slice 大于 DMA 缓冲区大小则 panic

Source

pub fn dma_addr(&self) -> DmaAddr

获取 DMA 地址。

返回设备用于访问此 DMA 缓冲区的物理/DMA 地址。

§返回

DMA 地址

Source

pub fn len(&self) -> usize

获取数组长度(元素个数)。

§返回

数组中的元素个数

Source

pub fn is_empty(&self) -> bool

检查数组是否为空。

§返回

如果数组长度为 0 返回 true,否则返回 false

Source

pub fn read(&self, index: usize) -> Option<T>

读取指定索引的元素(不自动同步缓存)。

注意: 此方法不会自动同步缓存。 如果需要缓存同步,请使用 to_vec() 方法。

§参数
  • index: 元素索引
§返回

如果索引有效返回 Some(T),否则返回 None

Source

pub fn set(&mut self, index: usize, value: T)

设置指定索引的元素值(不自动同步缓存)。

注意: 此方法不会自动同步缓存。 写入后请使用 copy_from_slice() 或手动刷新缓存。

§参数
  • index: 元素索引
  • value: 要写入的值
§Panics

如果 index >= self.len() 则 panic

Source

pub fn to_vec(&self) -> Vec<T>

将整个数组转换为 Vec(自动同步缓存)。

这是读取映射数据并同步缓存的主要方式。 读取前会自动使 CPU 缓存失效(FromDevice/Bidirectional)。

§返回

包含所有元素的 Vec

Source

pub fn prepare_read_all(&self)

Source

pub fn confirm_write_all(&self)

Trait Implementations§

Source§

impl<T> Drop for SArrayPtr<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for SArrayPtr<T>

§

impl<T> !RefUnwindSafe for SArrayPtr<T>

§

impl<T> !Send for SArrayPtr<T>

§

impl<T> !Sync for SArrayPtr<T>

§

impl<T> Unpin for SArrayPtr<T>

§

impl<T> !UnwindSafe for SArrayPtr<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.