24#include "esp_eth_phy_802_3.h"
25#include "freertos/FreeRTOS.h"
26#include "freertos/task.h"
27#include "driver/gpio.h"
28#include "esp_rom_gpio.h"
29#include "esp_rom_sys.h"
30#include "esp_idf_version.h"
32#if defined(USE_ETHERNET_JL1101) && (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2) || !defined(PLATFORMIO))
34static const char *TAG =
"jl1101";
35#define PHY_CHECK(a, str, goto_tag, ...) \
38 ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
51 uint16_t page_select : 8;
56#define ETH_PHY_PSR_REG_ADDR (0x1F)
60 esp_eth_mediator_t *eth;
64 eth_link_t link_status;
68static esp_err_t jl1101_page_select(phy_jl1101_t *jl1101,
uint32_t page) {
69 esp_eth_mediator_t *eth = jl1101->eth;
70 psr_reg_t psr = {.page_select = page};
71 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_PSR_REG_ADDR, psr.val) == ESP_OK,
"write PSR failed", err);
77static esp_err_t jl1101_update_link_duplex_speed(phy_jl1101_t *jl1101) {
78 esp_eth_mediator_t *eth = jl1101->eth;
79 eth_speed_t
speed = ETH_SPEED_10M;
80 eth_duplex_t duplex = ETH_DUPLEX_HALF;
85 PHY_CHECK(jl1101_page_select(jl1101, 0) == ESP_OK,
"select page 0 failed", err);
86 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK,
"read BMSR failed",
88 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_ANLPAR_REG_ADDR, &(anlpar.val)) == ESP_OK,
89 "read ANLPAR failed", err);
90 eth_link_t link = bmsr.link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
92 if (jl1101->link_status != link) {
94 if (link == ETH_LINK_UP) {
95 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK,
"read BMCR failed",
97 if (bmcr.speed_select) {
98 speed = ETH_SPEED_100M;
100 speed = ETH_SPEED_10M;
102 if (bmcr.duplex_mode) {
103 duplex = ETH_DUPLEX_FULL;
105 duplex = ETH_DUPLEX_HALF;
107 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_SPEED, (
void *)
speed) == ESP_OK,
"change speed failed", err);
108 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_DUPLEX, (
void *) duplex) == ESP_OK,
"change duplex failed", err);
110 if (duplex == ETH_DUPLEX_FULL && anlpar.symmetric_pause) {
111 peer_pause_ability = 1;
113 peer_pause_ability = 0;
115 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_PAUSE, (
void *) peer_pause_ability) == ESP_OK,
116 "change pause ability failed", err);
118 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_LINK, (
void *) link) == ESP_OK,
"change link failed", err);
119 jl1101->link_status = link;
126static esp_err_t jl1101_set_mediator(esp_eth_phy_t *phy, esp_eth_mediator_t *eth) {
127 PHY_CHECK(eth,
"can't set mediator to null", err);
128 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
132 return ESP_ERR_INVALID_ARG;
135static esp_err_t jl1101_get_link(esp_eth_phy_t *phy) {
136 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
138 PHY_CHECK(jl1101_update_link_duplex_speed(jl1101) == ESP_OK,
"update link duplex speed failed", err);
144static esp_err_t jl1101_reset(esp_eth_phy_t *phy) {
145 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
146 jl1101->link_status = ETH_LINK_DOWN;
147 esp_eth_mediator_t *eth = jl1101->eth;
148 bmcr_reg_t bmcr = {.reset = 1};
149 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK,
"write BMCR failed", err);
152 for (to = 0; to < jl1101->reset_timeout_ms / 50; to++) {
153 vTaskDelay(pdMS_TO_TICKS(50));
154 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK,
"read BMCR failed",
160 PHY_CHECK(to < jl1101->reset_timeout_ms / 50,
"reset timeout", err);
166static esp_err_t jl1101_reset_hw(esp_eth_phy_t *phy) {
167 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
168 if (jl1101->reset_gpio_num >= 0) {
169 esp_rom_gpio_pad_select_gpio(jl1101->reset_gpio_num);
170 gpio_set_direction(jl1101->reset_gpio_num, GPIO_MODE_OUTPUT);
171 gpio_set_level(jl1101->reset_gpio_num, 0);
172 esp_rom_delay_us(100);
173 gpio_set_level(jl1101->reset_gpio_num, 1);
178static esp_err_t jl1101_negotiate(esp_eth_phy_t *phy, eth_phy_autoneg_cmd_t cmd,
bool *nego_state) {
179 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
180 esp_eth_mediator_t *eth = jl1101->eth;
182 jl1101->link_status = ETH_LINK_DOWN;
188 .restart_auto_nego = 1
190 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK,
"write BMCR failed", err);
194 for (to = 0; to < jl1101->autonego_timeout_ms / 100; to++) {
195 vTaskDelay(pdMS_TO_TICKS(100));
196 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK,
"read BMSR failed",
198 if (bmsr.auto_nego_complete) {
203 if (to >= jl1101->autonego_timeout_ms / 100) {
204 ESP_LOGW(TAG,
"auto negotiation timeout");
211static esp_err_t jl1101_pwrctl(esp_eth_phy_t *phy,
bool enable) {
212 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
213 esp_eth_mediator_t *eth = jl1101->eth;
215 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK,
"read BMCR failed",
224 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK,
"write BMCR failed", err);
226 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK,
"read BMCR failed",
228 PHY_CHECK(bmcr.power_down == 1,
"power down failed", err);
232 for (to = 0; to < jl1101->reset_timeout_ms / 10; to++) {
233 vTaskDelay(pdMS_TO_TICKS(10));
234 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK,
"read BMCR failed",
236 if (bmcr.power_down == 0) {
240 PHY_CHECK(to < jl1101->reset_timeout_ms / 10,
"power up timeout", err);
247static esp_err_t jl1101_set_addr(esp_eth_phy_t *phy,
uint32_t addr) {
248 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
253static esp_err_t jl1101_get_addr(esp_eth_phy_t *phy,
uint32_t *addr) {
254 PHY_CHECK(addr,
"addr can't be null", err);
255 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
256 *addr = jl1101->addr;
259 return ESP_ERR_INVALID_ARG;
262static esp_err_t jl1101_del(esp_eth_phy_t *phy) {
263 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
268static esp_err_t jl1101_advertise_pause_ability(esp_eth_phy_t *phy,
uint32_t ability) {
269 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
270 esp_eth_mediator_t *eth = jl1101->eth;
273 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_ANAR_REG_ADDR, &(anar.val)) == ESP_OK,
"read ANAR failed",
276 anar.asymmetric_pause = 1;
277 anar.symmetric_pause = 1;
279 anar.asymmetric_pause = 0;
280 anar.symmetric_pause = 0;
282 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_ANAR_REG_ADDR, anar.val) == ESP_OK,
"write ANAR failed", err);
288static esp_err_t jl1101_init(esp_eth_phy_t *phy) {
289 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
290 esp_eth_mediator_t *eth = jl1101->eth;
292 if (jl1101->addr == ESP_ETH_PHY_ADDR_AUTO) {
293 PHY_CHECK(esp_eth_phy_802_3_detect_phy_addr(eth, &jl1101->addr) == ESP_OK,
"Detect PHY address failed", err);
296 PHY_CHECK(jl1101_pwrctl(phy,
true) == ESP_OK,
"power control failed", err);
298 PHY_CHECK(jl1101_reset(phy) == ESP_OK,
"reset failed", err);
302 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_IDR1_REG_ADDR, &(id1.val)) == ESP_OK,
"read ID1 failed", err);
303 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_IDR2_REG_ADDR, &(id2.val)) == ESP_OK,
"read ID2 failed", err);
304 PHY_CHECK(id1.oui_msb == 0x937C && id2.oui_lsb == 0x10 && id2.vendor_model == 0x2,
"wrong chip ID", err);
310static esp_err_t jl1101_deinit(esp_eth_phy_t *phy) {
312 PHY_CHECK(jl1101_pwrctl(phy,
false) == ESP_OK,
"power control failed", err);
319 PHY_CHECK(config,
"can't set phy config to null", err);
320 phy_jl1101_t *jl1101 = calloc(1,
sizeof(phy_jl1101_t));
321 PHY_CHECK(jl1101,
"calloc jl1101 failed", err);
322 jl1101->addr = config->phy_addr;
323 jl1101->reset_gpio_num = config->reset_gpio_num;
324 jl1101->reset_timeout_ms = config->reset_timeout_ms;
325 jl1101->link_status = ETH_LINK_DOWN;
326 jl1101->autonego_timeout_ms = config->autonego_timeout_ms;
327 jl1101->parent.reset = jl1101_reset;
328 jl1101->parent.reset_hw = jl1101_reset_hw;
329 jl1101->parent.init = jl1101_init;
330 jl1101->parent.deinit = jl1101_deinit;
331 jl1101->parent.set_mediator = jl1101_set_mediator;
332 jl1101->parent.autonego_ctrl = jl1101_negotiate;
333 jl1101->parent.get_link = jl1101_get_link;
334 jl1101->parent.pwrctl = jl1101_pwrctl;
335 jl1101->parent.get_addr = jl1101_get_addr;
336 jl1101->parent.set_addr = jl1101_set_addr;
337 jl1101->parent.advertise_pause_ability = jl1101_advertise_pause_ability;
338 jl1101->parent.del = jl1101_del;
340 return &(jl1101->parent);
esp_eth_phy_t * esp_eth_phy_new_jl1101(const eth_phy_config_t *config)