Mbed OS Reference
Loading...
Searching...
No Matches
GenericEthPhy.h
1/* Copyright (c) 2024 Jamie Smith
2* SPDX-License-Identifier: Apache-2.0
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef MBED_OS_GENERICETHPHY_H
18#define MBED_OS_GENERICETHPHY_H
19
20#include "CompositeEMAC.h"
21#include "DigitalOut.h"
22
23#include <optional>
24
25namespace mbed {
26
27namespace GenPhyRegs {
28/// Generic Ethernet phy register definitions
29/// @{
30inline constexpr uint8_t BMCR = 0; ///< Basic Mode Control Register
31
32inline constexpr size_t BMCR_SW_RST_Pos = 15;
33inline constexpr uint16_t BMCR_SW_RST_Msk = 1 << BMCR_SW_RST_Pos;
34
35inline constexpr size_t BMCR_SPEED_100M_Pos = 13;
36inline constexpr uint16_t BMCR_SPEED_100M_Msk = 1 << BMCR_SPEED_100M_Pos;
37
38inline constexpr size_t BMCR_ANEG_EN_Pos = 12;
39inline constexpr uint16_t BMCR_ANEG_EN_Msk = 1 << BMCR_ANEG_EN_Pos;
40
41inline constexpr size_t BMCR_ANEG_RESTART_Pos = 9;
42inline constexpr uint16_t BMCR_ANEG_RESTART_Msk = 1 << BMCR_ANEG_RESTART_Pos;
43
44inline constexpr size_t BMCR_DUPLEX_FULL_Pos = 8;
45inline constexpr uint16_t BMCR_DUPLEX_FULL_Msk = 1 << BMCR_DUPLEX_FULL_Pos;
46
47inline constexpr uint8_t BMSR = 1; ///< Basic Mode Status Register
48
49inline constexpr size_t BMCR_LINK_UP_Pos = 2;
50inline constexpr uint16_t BMCR_LINK_UP_Msk = 1 << BMCR_LINK_UP_Pos;
51
52inline constexpr uint8_t PHYIDR1 = 2; ///< PHY ID Register 1
53inline constexpr uint8_t PHYIDR2 = 3; ///< PHY ID Register 2
54inline constexpr uint8_t ANAR = 4; ///< AutoNegotiation Advertisement Register
55
56inline constexpr size_t ANAR_100BTX_FD_Pos = 8;
57inline constexpr uint16_t ANAR_100BTX_FD_Msk = 1 << ANAR_100BTX_FD_Pos;
58
59inline constexpr size_t ANAR_100BTX_Pos = 7;
60inline constexpr uint16_t ANAR_100BTX_Msk = 1 << ANAR_100BTX_Pos;
61
62inline constexpr size_t ANAR_10BT_FD_Pos = 6;
63inline constexpr uint16_t ANAR_10BT_FD_Msk = 1 << ANAR_10BT_FD_Pos;
64
65inline constexpr size_t ANAR_10BT_Pos = 5;
66inline constexpr uint16_t ANAR_10BT_Msk = 1 << ANAR_10BT_Pos;
67
68/// Value for ANAR[4:0] that specifies Ethernet as the protocol
69inline constexpr uint16_t ANAR_PROTOCOL_IEE_802_3U_Val = 1;
70
71inline constexpr uint8_t ANLPAR = 5; ///< AutoNegotiation Link Partner Advertisement Register
72/// @}
73}
74
75/**
76 * @brief Driver for a generic, Ethernet-standard compliant PHY chip.
77 *
78 * Passed a configuration which sets most attributes of the phy.
79 * May be extended to handle chip-specific quirks.
80 */
81class GenericEthPhy : public mbed::CompositeEMAC::PHYDriver {
82public:
83 /// Configuration structure for a generic Ethernet PHY
84 struct Config {
85 /// 24-bit OUI of the organization that produced the ethernet PHY.
86 uint32_t OUI;
87
88 /// Range of 5-bit model number of the phy. This plus the OUI is used to verify that the
89 /// chip in hardware matches what's expected.
90 uint8_t model_min;
91 uint8_t model_max;
92
93 /// MDIO address of the phy chip.
94 /// NOTE: 0 is *supposed* to be reserved as the general call address but lots of phy chips use
95 /// it anyway.
96 uint8_t address;
97
98 /// Time to hold reset low on this phy
99 std::chrono::microseconds resetLowTime = std::chrono::microseconds(100);
100
101 /// Time to wait after resetting the phy before trying to talk to it.
102 /// Also used as the time to wait for power on reset if there is no reset pin.
103 /// Note that standards compliant PHYs must reset in <500ms.
104 std::chrono::microseconds resetHighTime = std::chrono::milliseconds(500);
105
106 /// Whether autonegotiation shall be enabled on the phy.
107 /// NOTE: If this is set to false, the other end of the connection MUST be manually configured to match.
108 /// Otherwise you will get sporadic data loss due to an Ethernet duplex mismatch!
109 /// However, setting this to false will make the Ethernet link come up much faster (milliseconds instead of seconds, at least if you don't do DHCP)
110 bool autonegEnabled = true;
111
112 /// Whether the PHY should advertise full duplex modes
114
115 /// Whether the PHY should advertise half duplex modes.
116 /// Half duplex allows running only over 1 or 2 twisted pairs, but introduces the possibility of collisions.
118
119 /// Whether the PHY should advertise 100Mbit modes
120 bool advertise100M = true;
121
122 /// Whether the PHY should advertise 10Mbit modes.
123 /// 10Mbit can be a fallback if signal conditions are poor, but this fallback may not be desired.
124 bool advertise10M = true;
125
126 /// If autonegEnabled is false, this gives the fixed link settings to advertise.
127 std::pair<CompositeEMAC::LinkSpeed, CompositeEMAC::Duplex> fixedEthSettings = {CompositeEMAC::LinkSpeed::LINK_100MBIT, CompositeEMAC::Duplex::FULL};
128 };
129
130protected:
131
132 Config const &config;
133
134 std::optional<DigitalOut> resetDigitalOut;
135
136public:
137 GenericEthPhy(Config const &config):
138 config(config)
139 {}
140
141 CompositeEMAC::ErrCode init() override;
142
143 CompositeEMAC::ErrCode checkLinkStatus(bool &status) override;
144
145 CompositeEMAC::ErrCode checkLinkType(CompositeEMAC::LinkSpeed &speed, CompositeEMAC::Duplex &duplex) override;
146};
147
148};
149
150#endif //MBED_OS_GENERICETHPHY_H
Driver for a generic, Ethernet-standard compliant PHY chip.
Configuration structure for a generic Ethernet PHY.
uint8_t model_min
Range of 5-bit model number of the phy.
bool advertise100M
Whether the PHY should advertise 100Mbit modes.
bool autonegEnabled
Whether autonegotiation shall be enabled on the phy.
bool advertiseFullDuplex
Whether the PHY should advertise full duplex modes.
std::chrono::microseconds resetHighTime
Time to wait after resetting the phy before trying to talk to it.
bool advertiseHalfDuplex
Whether the PHY should advertise half duplex modes.
std::chrono::microseconds resetLowTime
Time to hold reset low on this phy.
std::pair< CompositeEMAC::LinkSpeed, CompositeEMAC::Duplex > fixedEthSettings
If autonegEnabled is false, this gives the fixed link settings to advertise.
uint32_t OUI
24-bit OUI of the organization that produced the ethernet PHY.
bool advertise10M
Whether the PHY should advertise 10Mbit modes.
uint8_t address
MDIO address of the phy chip.