rust-embedded / embedded-hal

A Hardware Abstraction Layer (HAL) for embedded systems
Apache License 2.0
2.01k stars 202 forks source link

Traits for MDIO and/or Ethernet MAC #425

Open tgross35 opened 2 years ago

tgross35 commented 2 years ago

Has there been any discussion on adding traits for MDIO or MAC interfaces? These are about the last common peripheral that I haven't been able to find any discourse about. It seems like there may be a place for an embedded-hal-mac crate.

MDIO is pretty simple and could probably mirror I2C. It is still useful to have even if a MAC interface is out of scope. There is some prior art here with the awesome mdio crate https://docs.rs/mdio/latest/mdio/

MAC is more complicated and might have some different possible implementations based on MII/RMII/RGMII/SGMII/etc

ryankurte commented 1 year ago

hey interesting idea, seems worthwhile to me! it might be worth looking at how folks are using smolctp and embedded-nal with RMII-based devices (for embedded use i would suggest this is the one to start with)

MDIO is pretty simple and could probably mirror I2C

could we just use the same I2C trait (with a fixed address width) to achieve this?

tgross35 commented 1 year ago

For the basic MDIO protocol (I think it makes sense to start here), the I2C trait is a good start. One of the differences is that frames are 16 bits rather than 8. The basic mdio::Read and [mdio::Write](https://docs.rs/mdio/latest/mdio/trait.Write.html` cover this nicely.

But on top of that basic specification, there are two possible protocols: clause 22 and clause 45. mdio::Read and mdio::Write traits don't specify an address bit - that is because these two clauses (and unlikely but possible future ones) specify different addressing schemes. Clause 22 has a phy address and a register address - but clause 45 has a phy address, device type, and register address. The mdio::miim::{Read, Write} traits handle clause 22, but there is no clause 45 there yet.

All in all, I think the mdio crate represents these traits pretty well, and could more or less be directly pulled into embedded_hal with just some minor tweaks. I opened a discussion for clause 45 on that repo: https://github.com/mitchmindtree/mdio/issues/2