Skip to content
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

Make the creation of TxRingEntry and RxRingEntry const #22

Merged
merged 1 commit into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ impl Clone for Descriptor {

impl Default for Descriptor {
fn default() -> Self {
Descriptor {
desc: Aligned([0; 4]),
}
Self::new()
}
}

impl Descriptor {
pub const fn new() -> Self {
Self {
desc: Aligned([0; 4]),
}
}

fn r(&self, n: usize) -> &RO<u32> {
let ro = &self.desc.deref()[n] as *const _ as *const RO<u32>;
unsafe { &*ro }
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ mod smi;
pub use ring::RingEntry;
mod desc;
mod rx;
pub use rx::{RxDescriptor, RxError};
use rx::{RxPacket, RxRing, RxRingEntry};
pub use rx::{RxDescriptor, RxError, RxRingEntry};
use rx::{RxPacket, RxRing};
mod tx;
pub use tx::{TxDescriptor, TxError};
use tx::{TxRing, TxRingEntry};
use tx::TxRing;
pub use tx::{TxDescriptor, TxError, TxRingEntry};
pub mod setup;
pub use setup::EthPins;
use setup::{
Expand Down
26 changes: 21 additions & 5 deletions src/ring.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use aligned::{Aligned, A8};
use core::ops::{Deref, DerefMut};

use crate::MTU;
use crate::{RxDescriptor, TxDescriptor, MTU};

pub trait RingDescriptor {
fn setup(&mut self, buffer: *const u8, len: usize, next: Option<&Self>);
Expand All @@ -23,18 +23,34 @@ impl<T: Clone + RingDescriptor> Clone for RingEntry<T> {

impl<T: Clone + RingDescriptor + Default> Default for RingEntry<T> {
fn default() -> Self {
Self::new()
RingEntry {
desc: Aligned([T::default()]),
buffer: Aligned([0; MTU]),
}
}
}

impl<T: Clone + RingDescriptor + Default> RingEntry<T> {
pub fn new() -> Self {
impl RingEntry<TxDescriptor> {
/// Creates a RingEntry with a TxDescriptor.
pub const fn new() -> Self {
RingEntry {
desc: Aligned([T::default()]),
desc: Aligned([TxDescriptor::new()]),
buffer: Aligned([0; MTU]),
}
}
}

impl RingEntry<RxDescriptor> {
/// Creates a RingEntry with a RxDescriptor.
pub const fn new() -> Self {
RingEntry {
desc: Aligned([RxDescriptor::new()]),
buffer: Aligned([0; MTU]),
}
}
}

impl<T: Clone + RingDescriptor> RingEntry<T> {
pub(crate) fn setup(&mut self, next: Option<&Self>) {
let buffer = self.buffer.as_ptr();
let len = self.buffer.len();
Expand Down
17 changes: 12 additions & 5 deletions src/rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ pub struct RxDescriptor {

impl Default for RxDescriptor {
fn default() -> Self {
let mut desc = Descriptor::default();
unsafe {
desc.write(1, RXDESC_1_RCH);
}
RxDescriptor { desc }
Self::new()
}
}

impl RxDescriptor {
/// Creates an zeroed RxDescriptor.
pub const fn new() -> Self {
Self {
desc: Descriptor::new(),
}
}

/// Is owned by the DMA engine?
fn is_owned(&self) -> bool {
(self.desc.read(0) & RXDESC_0_OWN) == RXDESC_0_OWN
Expand Down Expand Up @@ -126,6 +129,10 @@ pub type RxRingEntry = RingEntry<RxDescriptor>;

impl RingDescriptor for RxDescriptor {
fn setup(&mut self, buffer: *const u8, len: usize, next: Option<&Self>) {
// Defer this initialization to this function, so we can have `RingEntry` on bss.
unsafe {
self.desc.write(1, RXDESC_1_RCH);
}
self.set_buffer1(buffer, len);
match next {
Some(next) => self.set_buffer2(&next.desc as *const Descriptor as *const u8),
Expand Down
18 changes: 13 additions & 5 deletions src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ pub struct TxDescriptor {

impl Default for TxDescriptor {
fn default() -> Self {
let mut desc = Descriptor::default();
unsafe {
desc.write(0, TXDESC_0_TCH | TXDESC_0_IC | TXDESC_0_FS | TXDESC_0_LS);
}
TxDescriptor { desc }
Self::new()
}
}

impl TxDescriptor {
/// Creates an zeroed TxDescriptor.
pub const fn new() -> Self {
Self {
desc: Descriptor::new(),
}
}

/// Is owned by the DMA engine?
fn is_owned(&self) -> bool {
(self.desc.read(0) & TXDESC_0_OWN) == TXDESC_0_OWN
Expand Down Expand Up @@ -115,6 +118,11 @@ pub type TxRingEntry = RingEntry<TxDescriptor>;

impl RingDescriptor for TxDescriptor {
fn setup(&mut self, buffer: *const u8, _len: usize, next: Option<&Self>) {
// Defer this initialization to this function, so we can have `RingEntry` on bss.
unsafe {
self.desc
.write(0, TXDESC_0_TCH | TXDESC_0_IC | TXDESC_0_FS | TXDESC_0_LS);
}
self.set_buffer1(buffer);
match next {
Some(next) => self.set_buffer2(&next.desc as *const Descriptor as *const u8),
Expand Down