ESPHome 2026.3.0
Loading...
Searching...
No Matches
esp_eth_phy_jl1101.c
Go to the documentation of this file.
1// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
17#ifdef USE_ESP32
18
19#include <string.h>
20#include <stdlib.h>
21#include <sys/cdefs.h>
22#include "esp_log.h"
23#include "esp_eth.h"
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"
31
32#if defined(USE_ETHERNET_JL1101) && (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2) || !defined(PLATFORMIO))
33
34static const char *TAG = "jl1101";
35#define PHY_CHECK(a, str, goto_tag, ...) \
36 do { \
37 if (!(a)) { \
38 ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
39 goto goto_tag; \
40 } \
41 } while (0)
42
43/***************Vendor Specific Register***************/
44
49typedef union {
50 struct {
51 uint16_t page_select : 8; /* Select register page, default is 0 */
52 uint16_t reserved : 8; /* Reserved */
53 };
54 uint16_t val;
55} psr_reg_t;
56#define ETH_PHY_PSR_REG_ADDR (0x1F)
57
58typedef struct {
59 esp_eth_phy_t parent;
60 esp_eth_mediator_t *eth;
61 int addr;
62 uint32_t reset_timeout_ms;
63 uint32_t autonego_timeout_ms;
64 eth_link_t link_status;
65 int reset_gpio_num;
66} phy_jl1101_t;
67
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);
72 return ESP_OK;
73err:
74 return ESP_FAIL;
75}
76
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;
81 bmcr_reg_t bmcr;
82 bmsr_reg_t bmsr;
83 uint32_t peer_pause_ability = false;
84 anlpar_reg_t anlpar;
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",
87 err);
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;
91 /* check if link status changed */
92 if (jl1101->link_status != link) {
93 /* when link up, read negotiation result */
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",
96 err);
97 if (bmcr.speed_select) {
98 speed = ETH_SPEED_100M;
99 } else {
100 speed = ETH_SPEED_10M;
101 }
102 if (bmcr.duplex_mode) {
103 duplex = ETH_DUPLEX_FULL;
104 } else {
105 duplex = ETH_DUPLEX_HALF;
106 }
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);
109 /* if we're in duplex mode, and peer has the flow control ability */
110 if (duplex == ETH_DUPLEX_FULL && anlpar.symmetric_pause) {
111 peer_pause_ability = 1;
112 } else {
113 peer_pause_ability = 0;
114 }
115 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_PAUSE, (void *) peer_pause_ability) == ESP_OK,
116 "change pause ability failed", err);
117 }
118 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_LINK, (void *) link) == ESP_OK, "change link failed", err);
119 jl1101->link_status = link;
120 }
121 return ESP_OK;
122err:
123 return ESP_FAIL;
124}
125
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);
129 jl1101->eth = eth;
130 return ESP_OK;
131err:
132 return ESP_ERR_INVALID_ARG;
133}
134
135static esp_err_t jl1101_get_link(esp_eth_phy_t *phy) {
136 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
137 /* Updata information about link, speed, duplex */
138 PHY_CHECK(jl1101_update_link_duplex_speed(jl1101) == ESP_OK, "update link duplex speed failed", err);
139 return ESP_OK;
140err:
141 return ESP_FAIL;
142}
143
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);
150 /* Wait for reset complete */
151 uint32_t to = 0;
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",
155 err);
156 if (!bmcr.reset) {
157 break;
158 }
159 }
160 PHY_CHECK(to < jl1101->reset_timeout_ms / 50, "reset timeout", err);
161 return ESP_OK;
162err:
163 return ESP_FAIL;
164}
165
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); // insert min input assert time
173 gpio_set_level(jl1101->reset_gpio_num, 1);
174 }
175 return ESP_OK;
176}
177
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;
181 /* in case any link status has changed, let's assume we're in link down status */
182 jl1101->link_status = ETH_LINK_DOWN;
183 /* Restart auto negotiation */
184 bmcr_reg_t bmcr = {
185 .speed_select = 1, /* 100Mbps */
186 .duplex_mode = 1, /* Full Duplex */
187 .en_auto_nego = 1, /* Auto Negotiation */
188 .restart_auto_nego = 1 /* Restart Auto Negotiation */
189 };
190 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK, "write BMCR failed", err);
191 /* Wait for auto negotiation complete */
192 bmsr_reg_t bmsr;
193 uint32_t to = 0;
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",
197 err);
198 if (bmsr.auto_nego_complete) {
199 break;
200 }
201 }
202 /* Auto negotiation failed, maybe no network cable plugged in, so output a warning */
203 if (to >= jl1101->autonego_timeout_ms / 100) {
204 ESP_LOGW(TAG, "auto negotiation timeout");
205 }
206 return ESP_OK;
207err:
208 return ESP_FAIL;
209}
210
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;
214 bmcr_reg_t bmcr;
215 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, "read BMCR failed",
216 err);
217 if (!enable) {
218 /* Enable IEEE Power Down Mode */
219 bmcr.power_down = 1;
220 } else {
221 /* Disable IEEE Power Down Mode */
222 bmcr.power_down = 0;
223 }
224 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK, "write BMCR failed", err);
225 if (!enable) {
226 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, "read BMCR failed",
227 err);
228 PHY_CHECK(bmcr.power_down == 1, "power down failed", err);
229 } else {
230 /* wait for power up complete */
231 uint32_t to = 0;
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",
235 err);
236 if (bmcr.power_down == 0) {
237 break;
238 }
239 }
240 PHY_CHECK(to < jl1101->reset_timeout_ms / 10, "power up timeout", err);
241 }
242 return ESP_OK;
243err:
244 return ESP_FAIL;
245}
246
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);
249 jl1101->addr = addr;
250 return ESP_OK;
251}
252
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;
257 return ESP_OK;
258err:
259 return ESP_ERR_INVALID_ARG;
260}
261
262static esp_err_t jl1101_del(esp_eth_phy_t *phy) {
263 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
264 free(jl1101);
265 return ESP_OK;
266}
267
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;
271 /* Set PAUSE function ability */
272 anar_reg_t anar;
273 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_ANAR_REG_ADDR, &(anar.val)) == ESP_OK, "read ANAR failed",
274 err);
275 if (ability) {
276 anar.asymmetric_pause = 1;
277 anar.symmetric_pause = 1;
278 } else {
279 anar.asymmetric_pause = 0;
280 anar.symmetric_pause = 0;
281 }
282 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_ANAR_REG_ADDR, anar.val) == ESP_OK, "write ANAR failed", err);
283 return ESP_OK;
284err:
285 return ESP_FAIL;
286}
287
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;
291 // Detect PHY address
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);
294 }
295 /* Power on Ethernet PHY */
296 PHY_CHECK(jl1101_pwrctl(phy, true) == ESP_OK, "power control failed", err);
297 /* Reset Ethernet PHY */
298 PHY_CHECK(jl1101_reset(phy) == ESP_OK, "reset failed", err);
299 /* Check PHY ID */
300 phyidr1_reg_t id1;
301 phyidr2_reg_t id2;
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);
305 return ESP_OK;
306err:
307 return ESP_FAIL;
308}
309
310static esp_err_t jl1101_deinit(esp_eth_phy_t *phy) {
311 /* Power off Ethernet PHY */
312 PHY_CHECK(jl1101_pwrctl(phy, false) == ESP_OK, "power control failed", err);
313 return ESP_OK;
314err:
315 return ESP_FAIL;
316}
317
318esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config) {
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;
339
340 return &(jl1101->parent);
341err:
342 return NULL;
343}
344
345#endif /* USE_ARDUINO */
346#endif /* USE_ESP32 */
uint16_t reserved
esp_eth_phy_t * esp_eth_phy_new_jl1101(const eth_phy_config_t *config)
int speed
Definition fan.h:3
mopeka_std_values val[3]
static void uint32_t