Skip to content

Commit 99b3ef3

Browse files
committed
Make ethernet::DesRing generic over the number of buffers
1 parent 0371abb commit 99b3ef3

File tree

5 files changed

+45
-45
lines changed

5 files changed

+45
-45
lines changed

examples/ethernet-nucleo-h743zi2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const MAC_ADDRESS: [u8; 6] = [0x02, 0x00, 0x11, 0x22, 0x33, 0x44];
4343

4444
/// Ethernet descriptor rings are a global singleton
4545
#[link_section = ".sram3.eth"]
46-
static mut DES_RING: ethernet::DesRing = ethernet::DesRing::new();
46+
static mut DES_RING: ethernet::DesRing<4, 4> = ethernet::DesRing::new();
4747

4848
// the program entry point
4949
#[entry]

examples/ethernet-rtic-stm32h735g-dk.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const MAC_ADDRESS: [u8; 6] = [0x02, 0x00, 0x11, 0x22, 0x33, 0x44];
5656

5757
/// Ethernet descriptor rings are a global singleton
5858
#[link_section = ".axisram.eth"]
59-
static mut DES_RING: ethernet::DesRing = ethernet::DesRing::new();
59+
static mut DES_RING: ethernet::DesRing<4, 4> = ethernet::DesRing::new();
6060

6161
/// Net storage with static initialisation - another global singleton
6262
pub struct NetStorageStatic<'a> {
@@ -74,13 +74,13 @@ static mut STORE: NetStorageStatic = NetStorageStatic {
7474
};
7575

7676
pub struct Net<'a> {
77-
iface: EthernetInterface<'a, ethernet::EthernetDMA<'a>>,
77+
iface: EthernetInterface<'a, ethernet::EthernetDMA<'a, 4, 4>>,
7878
sockets: SocketSet<'a>,
7979
}
8080
impl<'a> Net<'a> {
8181
pub fn new(
8282
store: &'static mut NetStorageStatic<'a>,
83-
ethdev: ethernet::EthernetDMA<'a>,
83+
ethdev: ethernet::EthernetDMA<'a, 4, 4>,
8484
ethernet_addr: EthernetAddress,
8585
) -> Self {
8686
// Set IP address

examples/ethernet-rtic-stm32h747i-disco.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const MAC_ADDRESS: [u8; 6] = [0x02, 0x00, 0x11, 0x22, 0x33, 0x44];
5656

5757
/// Ethernet descriptor rings are a global singleton
5858
#[link_section = ".sram3.eth"]
59-
static mut DES_RING: ethernet::DesRing = ethernet::DesRing::new();
59+
static mut DES_RING: ethernet::DesRing<4, 4> = ethernet::DesRing::new();
6060

6161
/// Net storage with static initialisation - another global singleton
6262
pub struct NetStorageStatic<'a> {
@@ -74,13 +74,13 @@ static mut STORE: NetStorageStatic = NetStorageStatic {
7474
};
7575

7676
pub struct Net<'a> {
77-
iface: EthernetInterface<'a, ethernet::EthernetDMA<'a>>,
77+
iface: EthernetInterface<'a, ethernet::EthernetDMA<'a, 4, 4>>,
7878
sockets: SocketSet<'a>,
7979
}
8080
impl<'a> Net<'a> {
8181
pub fn new(
8282
store: &'static mut NetStorageStatic<'a>,
83-
ethdev: ethernet::EthernetDMA<'a>,
83+
ethdev: ethernet::EthernetDMA<'a, 4, 4>,
8484
ethernet_addr: EthernetAddress,
8585
) -> Self {
8686
// Set IP address

examples/ethernet-stm32h747i-disco.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const MAC_ADDRESS: [u8; 6] = [0x02, 0x00, 0x11, 0x22, 0x33, 0x44];
2828

2929
/// Ethernet descriptor rings are a global singleton
3030
#[link_section = ".sram3.eth"]
31-
static mut DES_RING: ethernet::DesRing = ethernet::DesRing::new();
31+
static mut DES_RING: ethernet::DesRing<4, 4> = ethernet::DesRing::new();
3232

3333
// the program entry point
3434
#[entry]

src/ethernet/eth.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ use crate::ethernet::StationManagement;
3939
// 6 DMAC, 6 SMAC, 4 q tag, 2 ethernet type II, 1500 ip MTU, 4 CRC, 2
4040
// padding
4141
const ETH_BUF_SIZE: usize = 1536;
42-
const ETH_NUM_TD: usize = 4;
43-
const ETH_NUM_RD: usize = 4;
4442

4543
/// Transmit and Receive Descriptor fields
4644
#[allow(dead_code)]
@@ -95,22 +93,22 @@ impl TDes {
9593

9694
/// Store a ring of TDes and associated buffers
9795
#[repr(C, packed)]
98-
struct TDesRing {
99-
td: [TDes; ETH_NUM_TD],
100-
tbuf: [[u32; ETH_BUF_SIZE / 4]; ETH_NUM_TD],
96+
struct TDesRing<const TD: usize> {
97+
td: [TDes; TD],
98+
tbuf: [[u32; ETH_BUF_SIZE / 4]; TD],
10199
tdidx: usize,
102100
}
103101

104-
impl TDesRing {
102+
impl<const TD: usize> TDesRing<TD> {
105103
const fn new() -> Self {
106104
Self {
107105
td: [TDes {
108106
tdes0: 0,
109107
tdes1: 0,
110108
tdes2: 0,
111109
tdes3: 0,
112-
}; ETH_NUM_TD],
113-
tbuf: [[0; ETH_BUF_SIZE / 4]; ETH_NUM_TD],
110+
}; TD],
111+
tbuf: [[0; ETH_BUF_SIZE / 4]; TD],
114112
tdidx: 0,
115113
}
116114
}
@@ -166,7 +164,7 @@ impl TDesRing {
166164
cortex_m::asm::dsb();
167165

168166
// Move the tail pointer (TPR) to the next descriptor
169-
let x = (x + 1) % ETH_NUM_TD;
167+
let x = (x + 1) % TD;
170168
unsafe {
171169
let dma = &*stm32::ETHERNET_DMA::ptr();
172170
dma.dmactx_dtpr
@@ -241,22 +239,22 @@ impl RDes {
241239

242240
/// Store a ring of RDes and associated buffers
243241
#[repr(C, packed)]
244-
struct RDesRing {
245-
rd: [RDes; ETH_NUM_RD],
246-
rbuf: [[u32; ETH_BUF_SIZE / 4]; ETH_NUM_RD],
242+
struct RDesRing<const RD: usize> {
243+
rd: [RDes; RD],
244+
rbuf: [[u32; ETH_BUF_SIZE / 4]; RD],
247245
rdidx: usize,
248246
}
249247

250-
impl RDesRing {
248+
impl<const RD: usize> RDesRing<RD> {
251249
const fn new() -> Self {
252250
Self {
253251
rd: [RDes {
254252
rdes0: 0,
255253
rdes1: 0,
256254
rdes2: 0,
257255
rdes3: 0,
258-
}; ETH_NUM_RD],
259-
rbuf: [[0; ETH_BUF_SIZE / 4]; ETH_NUM_RD],
256+
}; RD],
257+
rbuf: [[0; ETH_BUF_SIZE / 4]; RD],
260258
rdidx: 0,
261259
}
262260
}
@@ -325,7 +323,7 @@ impl RDesRing {
325323
}
326324

327325
// Update active descriptor
328-
self.rdidx = (x + 1) % ETH_NUM_RD;
326+
self.rdidx = (x + 1) % RD;
329327
}
330328

331329
/// Access the buffer pointed to by the next RDes
@@ -347,12 +345,12 @@ impl RDesRing {
347345
}
348346
}
349347

350-
pub struct DesRing {
351-
tx: TDesRing,
352-
rx: RDesRing,
348+
pub struct DesRing<const TD: usize, const RD: usize> {
349+
tx: TDesRing<TD>,
350+
rx: RDesRing<RD>,
353351
}
354-
impl DesRing {
355-
pub const fn new() -> DesRing {
352+
impl<const TD: usize, const RD: usize> DesRing<TD, RD> {
353+
pub const fn new() -> Self {
356354
DesRing {
357355
tx: TDesRing::new(),
358356
rx: RDesRing::new(),
@@ -363,8 +361,8 @@ impl DesRing {
363361
///
364362
/// Ethernet DMA
365363
///
366-
pub struct EthernetDMA<'a> {
367-
ring: &'a mut DesRing,
364+
pub struct EthernetDMA<'a, const TD: usize, const RD: usize> {
365+
ring: &'a mut DesRing<TD, RD>,
368366
eth_dma: stm32::ETHERNET_DMA,
369367
}
370368

@@ -393,15 +391,15 @@ pub struct EthernetMAC {
393391
/// # Safety
394392
///
395393
/// `EthernetDMA` shall not be moved as it is initialised here
396-
pub unsafe fn new_unchecked<'a>(
394+
pub unsafe fn new_unchecked<'a, const TD: usize, const RD: usize>(
397395
eth_mac: stm32::ETHERNET_MAC,
398396
eth_mtl: stm32::ETHERNET_MTL,
399397
eth_dma: stm32::ETHERNET_DMA,
400-
ring: &'a mut DesRing,
398+
ring: &'a mut DesRing<TD, RD>,
401399
mac_addr: EthernetAddress,
402400
prec: rec::Eth1Mac,
403401
clocks: &CoreClocks,
404-
) -> (EthernetDMA<'a>, EthernetMAC) {
402+
) -> (EthernetDMA<'a, TD, RD>, EthernetMAC) {
405403
// RCC
406404
{
407405
let rcc = &*stm32::RCC::ptr();
@@ -713,9 +711,9 @@ impl StationManagement for EthernetMAC {
713711
}
714712

715713
/// Define TxToken type and implement consume method
716-
pub struct TxToken<'a>(&'a mut TDesRing);
714+
pub struct TxToken<'a, const TD: usize>(&'a mut TDesRing<TD>);
717715

718-
impl<'a> phy::TxToken for TxToken<'a> {
716+
impl<'a, const TD: usize> phy::TxToken for TxToken<'a, TD> {
719717
fn consume<R, F>(
720718
self,
721719
_timestamp: Instant,
@@ -734,9 +732,9 @@ impl<'a> phy::TxToken for TxToken<'a> {
734732
}
735733

736734
/// Define RxToken type and implement consume method
737-
pub struct RxToken<'a>(&'a mut RDesRing);
735+
pub struct RxToken<'a, const RD: usize>(&'a mut RDesRing<RD>);
738736

739-
impl<'a> phy::RxToken for RxToken<'a> {
737+
impl<'a, const RD: usize> phy::RxToken for RxToken<'a, RD> {
740738
fn consume<R, F>(self, _timestamp: Instant, f: F) -> smoltcp::Result<R>
741739
where
742740
F: FnOnce(&mut [u8]) -> smoltcp::Result<R>,
@@ -748,9 +746,11 @@ impl<'a> phy::RxToken for RxToken<'a> {
748746
}
749747

750748
/// Implement the smoltcp Device interface
751-
impl<'a> phy::Device<'a> for EthernetDMA<'_> {
752-
type RxToken = RxToken<'a>;
753-
type TxToken = TxToken<'a>;
749+
impl<'a, const TD: usize, const RD: usize> phy::Device<'a>
750+
for EthernetDMA<'_, TD, RD>
751+
{
752+
type RxToken = RxToken<'a, RD>;
753+
type TxToken = TxToken<'a, TD>;
754754

755755
// Clippy false positive because DeviceCapabilities is non-exhaustive
756756
#[allow(clippy::field_reassign_with_default)]
@@ -759,11 +759,11 @@ impl<'a> phy::Device<'a> for EthernetDMA<'_> {
759759
// ethernet frame type II (6 smac, 6 dmac, 2 ethertype),
760760
// sans CRC (4), 1500 IP MTU
761761
caps.max_transmission_unit = 1514;
762-
caps.max_burst_size = Some(core::cmp::min(ETH_NUM_TD, ETH_NUM_RD));
762+
caps.max_burst_size = Some(core::cmp::min(TD, RD));
763763
caps
764764
}
765765

766-
fn receive(&mut self) -> Option<(RxToken, TxToken)> {
766+
fn receive(&mut self) -> Option<(RxToken<RD>, TxToken<TD>)> {
767767
// Skip all queued packets with errors.
768768
while self.ring.rx.available() && !self.ring.rx.valid() {
769769
self.ring.rx.release()
@@ -776,7 +776,7 @@ impl<'a> phy::Device<'a> for EthernetDMA<'_> {
776776
}
777777
}
778778

779-
fn transmit(&mut self) -> Option<TxToken> {
779+
fn transmit(&mut self) -> Option<TxToken<TD>> {
780780
if self.ring.tx.available() {
781781
Some(TxToken(&mut self.ring.tx))
782782
} else {
@@ -785,7 +785,7 @@ impl<'a> phy::Device<'a> for EthernetDMA<'_> {
785785
}
786786
}
787787

788-
impl EthernetDMA<'_> {
788+
impl<const TD: usize, const RD: usize> EthernetDMA<'_, TD, RD> {
789789
/// Return the number of packets dropped since this method was
790790
/// last called
791791
pub fn number_packets_dropped(&self) -> u32 {

0 commit comments

Comments
 (0)