Skip to content

Commit d4dd9f9

Browse files
author
Johannes Draaijer
committed
Make a macro for the unsafe pin traits
#![deny(missing_docs)] Placate clippy #![deny(missing_docs)]
1 parent 343e2fe commit d4dd9f9

File tree

8 files changed

+61
-19
lines changed

8 files changed

+61
-19
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! For initialisation, see [`new`], and [`new_with_mii`]
44
#![no_std]
5+
#![deny(missing_docs)]
56

67
/// Re-export
78
#[cfg(feature = "stm32f7xx-hal")]

src/mac/miim.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@ use crate::{
1010
};
1111

1212
/// MDIO pin types.
13+
///
14+
/// # Safety
15+
/// Only pins specified as ETH_MDIO in a part's reference manual
16+
/// may implement this trait
1317
pub unsafe trait MdioPin {}
18+
1419
/// MDC pin types.
20+
///
21+
/// # Safety
22+
/// Only pins specified as ETH_MDC in a part's reference manual
23+
/// may implement this trait
1524
pub unsafe trait MdcPin {}
1625

1726
#[inline(always)]
@@ -70,10 +79,12 @@ where
7079
Mdio: MdioPin,
7180
Mdc: MdcPin,
7281
{
82+
/// Read MII register `reg` from the PHY at address `phy`
7383
pub fn read(&mut self, phy: u8, reg: u8) -> u16 {
7484
miim_read(&mut self.mac.eth_mac, phy, reg)
7585
}
7686

87+
/// Write the value `data` to MII register `reg` to the PHY at address `phy`
7788
pub fn write(&mut self, phy: u8, reg: u8, data: u16) {
7889
miim_write(&mut self.mac.eth_mac, phy, reg, data)
7990
}

src/mac/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Ethernet MAC driver implementation
2+
13
use core::ops::Deref;
24

35
use crate::{
@@ -215,12 +217,14 @@ where
215217
MDIO: MdioPin,
216218
MDC: MdcPin,
217219
{
220+
/// Read MII register `reg` from the PHY at address `phy`
218221
pub fn read(&mut self, phy: u8, reg: u8) -> u16 {
219222
self.eth_mac
220223
.mii(&mut self.mdio, &mut self.mdc)
221224
.read(phy, reg)
222225
}
223226

227+
/// Write the value `data` to MII register `reg` to the PHY at address `phy`
224228
pub fn write(&mut self, phy: u8, reg: u8, data: u16) {
225229
self.eth_mac
226230
.mii(&mut self.mdio, &mut self.mdc)

src/ring.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub trait RingDescriptor {
77
fn setup(&mut self, buffer: *const u8, len: usize, next: Option<&Self>);
88
}
99

10+
/// An entry in a DMA Descriptor ring
1011
pub struct RingEntry<T: Clone + RingDescriptor> {
1112
desc: Aligned<A8, [T; 1]>,
1213
buffer: Aligned<A8, [u8; MTU]>,
@@ -31,6 +32,9 @@ impl<T: Clone + RingDescriptor + Default> Default for RingEntry<T> {
3132
}
3233

3334
impl RingEntry<TxDescriptor> {
35+
/// The initial value of a TxRingDescriptor
36+
pub const INIT: Self = Self::new();
37+
3438
/// Creates a RingEntry with a TxDescriptor.
3539
pub const fn new() -> Self {
3640
RingEntry {
@@ -41,6 +45,9 @@ impl RingEntry<TxDescriptor> {
4145
}
4246

4347
impl RingEntry<RxDescriptor> {
48+
/// The initial value of an RxRingDescriptor
49+
pub const INIT: Self = Self::new();
50+
4451
/// Creates a RingEntry with a RxDescriptor.
4552
pub const fn new() -> Self {
4653
RingEntry {

src/rx.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ use crate::{
1111
ring::{RingDescriptor, RingEntry},
1212
};
1313

14+
/// Errors that can occur during RX
1415
#[derive(Debug, PartialEq)]
1516
pub enum RxError {
17+
/// Receiving would block
1618
WouldBlock,
19+
/// The received packet was truncated
1720
Truncated,
21+
/// An error occured with the DMA
1822
DmaError,
1923
}
2024

@@ -39,6 +43,7 @@ const RXDESC_1_RER: u32 = 1 << 15;
3943

4044
#[repr(C)]
4145
#[derive(Clone)]
46+
/// An RX DMA Descriptor
4247
pub struct RxDescriptor {
4348
desc: Descriptor,
4449
}
@@ -120,6 +125,7 @@ impl RxDescriptor {
120125
}
121126
}
122127

128+
/// An RX DMA Ring Descriptor entry
123129
pub type RxRingEntry = RingEntry<RxDescriptor>;
124130

125131
impl RingDescriptor for RxDescriptor {

src/setup.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
//! Pin definitions and setup functionality
2+
//!
3+
//! This module contains the unsafe traits that determine
4+
//! which pins can have a specific function, and provides
5+
//! functionality for setting up clocks and the MAC peripheral
6+
17
#[cfg(feature = "stm32f4xx-hal")]
28
use stm32f4xx_hal::{
39
bb,
@@ -142,33 +148,35 @@ pub(crate) fn setup() {
142148
});
143149
}
144150

145-
/// RMII Reference Clock.
146-
pub unsafe trait RmiiRefClk {}
147-
148-
/// RMII RX Data Valid.
149-
pub unsafe trait RmiiCrsDv {}
150-
151-
/// RMII TX Enable.
152-
pub unsafe trait RmiiTxEN {}
153-
154-
/// RMII TXD0.
155-
pub unsafe trait RmiiTxD0 {}
156-
157-
/// RMII TXD1.
158-
pub unsafe trait RmiiTxD1 {}
159-
160-
/// RMII RXD0.
161-
pub unsafe trait RmiiRxD0 {}
151+
macro_rules ! pin_trait {
152+
($([$name:ident, $doc:literal, $rm_name:literal]),*) => {
153+
$(
154+
#[doc = concat!($doc, "\n# Safety\nOnly pins specified as `ETH_RMII_", $rm_name, "` in a part's Reference Manual\nmay implement this trait.")]
155+
pub unsafe trait $name {}
156+
)*
157+
}
158+
}
162159

163-
/// RMII RXD1.
164-
pub unsafe trait RmiiRxD1 {}
160+
pin_trait!(
161+
[RmiiRefClk, "RMII Reference Clock", "REF_CLK"],
162+
[RmiiCrsDv, "RMII Rx Data Valid", "CRS_DV"],
163+
[RmiiTxEN, "RMII TX Enable", "TX_EN"],
164+
[RmiiTxD0, "RMII TX Data Pin 0", "TXD0"],
165+
[RmiiTxD1, "RMII TX Data Pin 1", "TXD1"],
166+
[RmiiRxD0, "RMII RX Data Pin 0", "RXD0"],
167+
[RmiiRxD1, "RMII RX Data Pin 1", "RXD1"]
168+
);
165169

166170
/// Trait needed to setup the pins for the Ethernet peripheral.
167171
pub trait AlternateVeryHighSpeed {
168172
/// Puts the pin in the Alternate Function 11 with Very High Speed.
169173
fn into_af11_very_high_speed(self);
170174
}
171175

176+
/// A struct that represents a combination of pins to be used
177+
/// as RMII pins for the ethernet peripheral(s)
178+
// NOTE(missing_docs): all fields of this struct are self-explanatory
179+
#[allow(missing_docs)]
172180
pub struct EthPins<REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1> {
173181
pub ref_clk: REFCLK,
174182
pub crs: CRS,

src/smoltcp_phy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ impl<'a, 'rx, 'tx, 'b> Device<'a> for &'b mut EthernetDMA<'rx, 'tx> {
4242
}
4343
}
4444

45+
/// An Ethernet RX token that can be consumed in order to receive
46+
/// an ethernet packet.
4547
pub struct EthRxToken<'a> {
4648
packet: RxPacket<'a>,
4749
}

src/tx.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ const TXDESC_0_ES: u32 = 1 << 15;
3131
const TXDESC_1_TBS_SHIFT: usize = 0;
3232
const TXDESC_1_TBS_MASK: u32 = 0x0fff << TXDESC_1_TBS_SHIFT;
3333

34+
/// Errors that can occur during Ethernet TX
3435
#[derive(Debug, PartialEq)]
3536
pub enum TxError {
3637
/// Ring buffer is full
3738
WouldBlock,
3839
}
3940

41+
/// A TX DMA Ring Descriptor
4042
#[repr(C)]
4143
#[derive(Clone)]
4244
pub struct TxDescriptor {
@@ -112,6 +114,7 @@ impl TxDescriptor {
112114
}
113115
}
114116

117+
/// A TX DMA Ring Descriptor entry
115118
pub type TxRingEntry = RingEntry<TxDescriptor>;
116119

117120
impl RingDescriptor for TxDescriptor {

0 commit comments

Comments
 (0)