PHY Interface Adaptation
Software Architecture
The Ameba Ethernet module integrates the Lightweight TCP/IP stack (LwIP) and MAC driver. The correspondence to OSI and TCP/IP models is shown below:
Ethernet Software Architecture
The SDK implements the L2 MAC driver and the RMII operations, and provides the L1 RTL8201F driver.
Adaptation Steps
To adapt another PHY chip, implement the eth_phy_ops interface table and define the corresponding device instance.
Implement PHY Operations
Implement the key functions in struct eth_phy_ops based on the PHY datasheet.
Note
Standard PHY registers are defined in
ameba_phy.h.Registers specific to the PHY chip can be defined directly in a custom
ameba_phy_custom.cfile, which must be added to CMakeLists.txt.
Taking RTL8201F as an example:
OPS Member |
Function |
Description |
Why Required |
|---|---|---|---|
|
|
Init & ID Check Read PHY ID and match with |
Prevents matching wrong hardware; ensures PHY is powered and MDIO is working. |
|
|
Soft Reset Write BMCR reset bit and wait for stabilization. |
Ensures the PHY is in a known initial state and clears earlier faulty configuration or state. |
|
|
Ref Clock Config Switch to Page 7 to set RMII clock direction (Input/Output). |
The MAC and the PHY of an RMII interface must share the 50 MHz reference clock. A wrong direction prevents the link from being established. |
|
|
Link Config Configure the Auto-Neg advertised capability, or force the speed and duplex mode. |
Determines how the PHY connects to the peer switch or router. Without this interface, 10M/100M or duplex mode cannot be set. |
Define MDIO Bus Interface and PHY Instance
Define the MDIO bus read/write interface and create the eth_phy_dev instance, specifying the PHY address.
// 1. Define MDIO bus operations
// Use low-level read/write functions provided by SDK
const struct eth_mdio_ops eth_mdio_bus_default = {
.mdio_read = Ethernet_ReadPhyReg,
.mdio_write = Ethernet_WritePhyReg,
};
// 2. Define PHY device instance
struct eth_phy_dev eth_phy_dev_custom = {
.bus = ð_mdio_bus_default, // Bind bus interface
.addr = 0x01, // [Important] PHY Address must match schematic
.ops = &custom_phy_ops, // Bind operations implemented above
};
Register and Initialize
Call Ethernet_StructInit() to fill default handle configuration and bind the custom PHY. The following is a configuration fragment, not complete hardware initialization:
void User_Ethernet_Config(ETH_InitTypeDef *eth_handle)
{
Ethernet_StructInit(eth_handle, ð_phy_dev_custom);
// Fill descriptor, buffer, and application configuration afterwards
}
The handle, PHY instance, and MDIO operations table must remain valid while the driver is in use.