19 #include <sys/cdefs.h> 22 #if ESP_IDF_VERSION_MAJOR >= 5 23 #include "esp_eth_phy_802_3.h" 25 #include "eth_phy_regs_struct.h" 27 #include "freertos/FreeRTOS.h" 28 #include "freertos/task.h" 29 #include "driver/gpio.h" 30 #include "esp_rom_gpio.h" 31 #include "esp_rom_sys.h" 33 static const char *TAG =
"jl1101";
34 #define PHY_CHECK(a, str, goto_tag, ...) \ 37 ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ 50 uint16_t page_select : 8;
55 #define ETH_PHY_PSR_REG_ADDR (0x1F) 59 esp_eth_mediator_t *eth;
61 uint32_t reset_timeout_ms;
62 uint32_t autonego_timeout_ms;
63 eth_link_t link_status;
67 static esp_err_t jl1101_page_select(phy_jl1101_t *jl1101, uint32_t page) {
68 esp_eth_mediator_t *eth = jl1101->eth;
69 psr_reg_t psr = {.page_select = page};
70 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_PSR_REG_ADDR, psr.val) == ESP_OK,
"write PSR failed", err);
76 static esp_err_t jl1101_update_link_duplex_speed(phy_jl1101_t *jl1101) {
77 esp_eth_mediator_t *eth = jl1101->eth;
78 eth_speed_t
speed = ETH_SPEED_10M;
79 eth_duplex_t duplex = ETH_DUPLEX_HALF;
82 uint32_t peer_pause_ability =
false;
84 PHY_CHECK(jl1101_page_select(jl1101, 0) == ESP_OK,
"select page 0 failed", err);
85 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK,
"read BMSR failed",
87 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_ANLPAR_REG_ADDR, &(anlpar.val)) == ESP_OK,
88 "read ANLPAR failed", err);
89 eth_link_t link = bmsr.link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
91 if (jl1101->link_status != link) {
93 if (link == ETH_LINK_UP) {
94 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK,
"read BMCR failed",
96 if (bmcr.speed_select) {
97 speed = ETH_SPEED_100M;
99 speed = ETH_SPEED_10M;
101 if (bmcr.duplex_mode) {
102 duplex = ETH_DUPLEX_FULL;
104 duplex = ETH_DUPLEX_HALF;
106 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_SPEED, (
void *) speed) == ESP_OK,
"change speed failed", err);
107 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_DUPLEX, (
void *) duplex) == ESP_OK,
"change duplex failed", err);
109 if (duplex == ETH_DUPLEX_FULL && anlpar.symmetric_pause) {
110 peer_pause_ability = 1;
112 peer_pause_ability = 0;
114 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_PAUSE, (
void *) peer_pause_ability) == ESP_OK,
115 "change pause ability failed", err);
117 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_LINK, (
void *) link) == ESP_OK,
"change link failed", err);
118 jl1101->link_status = link;
125 static esp_err_t jl1101_set_mediator(esp_eth_phy_t *phy, esp_eth_mediator_t *eth) {
126 PHY_CHECK(eth,
"can't set mediator to null", err);
127 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
131 return ESP_ERR_INVALID_ARG;
134 static esp_err_t jl1101_get_link(esp_eth_phy_t *phy) {
135 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
137 PHY_CHECK(jl1101_update_link_duplex_speed(jl1101) == ESP_OK,
"update link duplex speed failed", err);
143 static esp_err_t jl1101_reset(esp_eth_phy_t *phy) {
144 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
145 jl1101->link_status = ETH_LINK_DOWN;
146 esp_eth_mediator_t *eth = jl1101->eth;
147 bmcr_reg_t bmcr = {.reset = 1};
148 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK,
"write BMCR failed", err);
151 for (to = 0; to < jl1101->reset_timeout_ms / 50; to++) {
152 vTaskDelay(pdMS_TO_TICKS(50));
153 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK,
"read BMCR failed",
159 PHY_CHECK(to < jl1101->reset_timeout_ms / 50,
"reset timeout", err);
165 static esp_err_t jl1101_reset_hw(esp_eth_phy_t *phy) {
166 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
167 if (jl1101->reset_gpio_num >= 0) {
168 esp_rom_gpio_pad_select_gpio(jl1101->reset_gpio_num);
169 gpio_set_direction(jl1101->reset_gpio_num, GPIO_MODE_OUTPUT);
170 gpio_set_level(jl1101->reset_gpio_num, 0);
171 esp_rom_delay_us(100);
172 gpio_set_level(jl1101->reset_gpio_num, 1);
177 #if ESP_IDF_VERSION_MAJOR >= 5 178 static esp_err_t jl1101_negotiate(esp_eth_phy_t *phy, eth_phy_autoneg_cmd_t
cmd,
bool *nego_state) {
180 static esp_err_t jl1101_negotiate(esp_eth_phy_t *phy) {
182 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
183 esp_eth_mediator_t *eth = jl1101->eth;
185 jl1101->link_status = ETH_LINK_DOWN;
191 .restart_auto_nego = 1
193 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK,
"write BMCR failed", err);
197 for (to = 0; to < jl1101->autonego_timeout_ms / 100; to++) {
198 vTaskDelay(pdMS_TO_TICKS(100));
199 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK,
"read BMSR failed",
201 if (bmsr.auto_nego_complete) {
206 if (to >= jl1101->autonego_timeout_ms / 100) {
207 ESP_LOGW(TAG,
"auto negotiation timeout");
214 static esp_err_t jl1101_pwrctl(esp_eth_phy_t *phy,
bool enable) {
215 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
216 esp_eth_mediator_t *eth = jl1101->eth;
218 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK,
"read BMCR failed",
227 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK,
"write BMCR failed", err);
229 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK,
"read BMCR failed",
231 PHY_CHECK(bmcr.power_down == 1,
"power down failed", err);
235 for (to = 0; to < jl1101->reset_timeout_ms / 10; to++) {
236 vTaskDelay(pdMS_TO_TICKS(10));
237 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK,
"read BMCR failed",
239 if (bmcr.power_down == 0) {
243 PHY_CHECK(to < jl1101->reset_timeout_ms / 10,
"power up timeout", err);
250 static esp_err_t jl1101_set_addr(esp_eth_phy_t *phy, uint32_t addr) {
251 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
256 static esp_err_t jl1101_get_addr(esp_eth_phy_t *phy, uint32_t *addr) {
257 PHY_CHECK(addr,
"addr can't be null", err);
258 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
259 *addr = jl1101->addr;
262 return ESP_ERR_INVALID_ARG;
265 static esp_err_t jl1101_del(esp_eth_phy_t *phy) {
266 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
271 static esp_err_t jl1101_advertise_pause_ability(esp_eth_phy_t *phy, uint32_t ability) {
272 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
273 esp_eth_mediator_t *eth = jl1101->eth;
276 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_ANAR_REG_ADDR, &(anar.val)) == ESP_OK,
"read ANAR failed",
279 anar.asymmetric_pause = 1;
280 anar.symmetric_pause = 1;
282 anar.asymmetric_pause = 0;
283 anar.symmetric_pause = 0;
285 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_ANAR_REG_ADDR, anar.val) == ESP_OK,
"write ANAR failed", err);
291 static esp_err_t jl1101_init(esp_eth_phy_t *phy) {
292 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
293 esp_eth_mediator_t *eth = jl1101->eth;
295 if (jl1101->addr == ESP_ETH_PHY_ADDR_AUTO) {
296 #if ESP_IDF_VERSION_MAJOR >= 5 297 PHY_CHECK(esp_eth_phy_802_3_detect_phy_addr(eth, &jl1101->addr) == ESP_OK,
"Detect PHY address failed", err);
299 PHY_CHECK(esp_eth_detect_phy_addr(eth, &jl1101->addr) == ESP_OK,
"Detect PHY address failed", err);
303 PHY_CHECK(jl1101_pwrctl(phy,
true) == ESP_OK,
"power control failed", err);
305 PHY_CHECK(jl1101_reset(phy) == ESP_OK,
"reset failed", err);
309 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_IDR1_REG_ADDR, &(id1.val)) == ESP_OK,
"read ID1 failed", err);
310 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_IDR2_REG_ADDR, &(id2.val)) == ESP_OK,
"read ID2 failed", err);
311 PHY_CHECK(id1.oui_msb == 0x937C && id2.oui_lsb == 0x10 && id2.vendor_model == 0x2,
"wrong chip ID", err);
317 static esp_err_t jl1101_deinit(esp_eth_phy_t *phy) {
319 PHY_CHECK(jl1101_pwrctl(phy,
false) == ESP_OK,
"power control failed", err);
326 PHY_CHECK(config,
"can't set phy config to null", err);
327 phy_jl1101_t *jl1101 = calloc(1,
sizeof(phy_jl1101_t));
328 PHY_CHECK(jl1101,
"calloc jl1101 failed", err);
329 jl1101->addr = config->phy_addr;
330 jl1101->reset_gpio_num = config->reset_gpio_num;
331 jl1101->reset_timeout_ms = config->reset_timeout_ms;
332 jl1101->link_status = ETH_LINK_DOWN;
333 jl1101->autonego_timeout_ms = config->autonego_timeout_ms;
334 jl1101->parent.reset = jl1101_reset;
335 jl1101->parent.reset_hw = jl1101_reset_hw;
336 jl1101->parent.init = jl1101_init;
337 jl1101->parent.deinit = jl1101_deinit;
338 jl1101->parent.set_mediator = jl1101_set_mediator;
339 #if ESP_IDF_VERSION_MAJOR >= 5 340 jl1101->parent.autonego_ctrl = jl1101_negotiate;
342 jl1101->parent.negotiate = jl1101_negotiate;
344 jl1101->parent.get_link = jl1101_get_link;
345 jl1101->parent.pwrctl = jl1101_pwrctl;
346 jl1101->parent.get_addr = jl1101_get_addr;
347 jl1101->parent.set_addr = jl1101_set_addr;
348 jl1101->parent.advertise_pause_ability = jl1101_advertise_pause_ability;
349 jl1101->parent.del = jl1101_del;
351 return &(jl1101->parent);
esp_eth_phy_t * esp_eth_phy_new_jl1101(const eth_phy_config_t *config)