ESPHome 2026.5.0-dev
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(6, 0, 0) || \
33 ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2) || !defined(PLATFORMIO))
34
35static const char *TAG = "jl1101";
36#define PHY_CHECK(a, str, goto_tag, ...) \
37 do { \
38 if (!(a)) { \
39 ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
40 goto goto_tag; \
41 } \
42 } while (0)
43
44/***************Vendor Specific Register***************/
45
50typedef union {
51 struct {
52 uint16_t page_select : 8; /* Select register page, default is 0 */
53 uint16_t reserved : 8; /* Reserved */
54 };
55 uint16_t val;
56} psr_reg_t;
57#define ETH_PHY_PSR_REG_ADDR (0x1F)
58
59typedef struct {
60 esp_eth_phy_t parent;
61 esp_eth_mediator_t *eth;
62 int addr;
63 uint32_t reset_timeout_ms;
64 uint32_t autonego_timeout_ms;
65 eth_link_t link_status;
66 int reset_gpio_num;
67} phy_jl1101_t;
68
69static esp_err_t jl1101_page_select(phy_jl1101_t *jl1101, uint32_t page) {
70 esp_eth_mediator_t *eth = jl1101->eth;
71 psr_reg_t psr = {.page_select = page};
72 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_PSR_REG_ADDR, psr.val) == ESP_OK, "write PSR failed", err);
73 return ESP_OK;
74err:
75 return ESP_FAIL;
76}
77
78static esp_err_t jl1101_update_link_duplex_speed(phy_jl1101_t *jl1101) {
79 esp_eth_mediator_t *eth = jl1101->eth;
82 bmcr_reg_t bmcr;
83 bmsr_reg_t bmsr;
84 uint32_t peer_pause_ability = false;
85 anlpar_reg_t anlpar;
86 PHY_CHECK(jl1101_page_select(jl1101, 0) == ESP_OK, "select page 0 failed", err);
87 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK, "read BMSR failed",
88 err);
89 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_ANLPAR_REG_ADDR, &(anlpar.val)) == ESP_OK,
90 "read ANLPAR failed", err);
91 eth_link_t link = bmsr.link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
92 /* check if link status changed */
93 if (jl1101->link_status != link) {
94 /* when link up, read negotiation result */
95 if (link == ETH_LINK_UP) {
96 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, "read BMCR failed",
97 err);
98 if (bmcr.speed_select) {
100 } else {
102 }
103 if (bmcr.duplex_mode) {
104 duplex = ETH_DUPLEX_FULL;
105 } else {
106 duplex = ETH_DUPLEX_HALF;
107 }
108 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_SPEED, (void *) speed) == ESP_OK, "change speed failed", err);
109 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_DUPLEX, (void *) duplex) == ESP_OK, "change duplex failed", err);
110 /* if we're in duplex mode, and peer has the flow control ability */
111 if (duplex == ETH_DUPLEX_FULL && anlpar.symmetric_pause) {
112 peer_pause_ability = 1;
113 } else {
114 peer_pause_ability = 0;
115 }
116 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_PAUSE, (void *) peer_pause_ability) == ESP_OK,
117 "change pause ability failed", err);
118 }
119 PHY_CHECK(eth->on_state_changed(eth, ETH_STATE_LINK, (void *) link) == ESP_OK, "change link failed", err);
120 jl1101->link_status = link;
121 }
122 return ESP_OK;
123err:
124 return ESP_FAIL;
125}
126
127static esp_err_t jl1101_set_mediator(esp_eth_phy_t *phy, esp_eth_mediator_t *eth) {
128 PHY_CHECK(eth, "can't set mediator to null", err);
129 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
130 jl1101->eth = eth;
131 return ESP_OK;
132err:
133 return ESP_ERR_INVALID_ARG;
134}
135
136static esp_err_t jl1101_get_link(esp_eth_phy_t *phy) {
137 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
138 /* Updata information about link, speed, duplex */
139 PHY_CHECK(jl1101_update_link_duplex_speed(jl1101) == ESP_OK, "update link duplex speed failed", err);
140 return ESP_OK;
141err:
142 return ESP_FAIL;
143}
144
145static esp_err_t jl1101_reset(esp_eth_phy_t *phy) {
146 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
147 jl1101->link_status = ETH_LINK_DOWN;
148 esp_eth_mediator_t *eth = jl1101->eth;
149 bmcr_reg_t bmcr = {.reset = 1};
150 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK, "write BMCR failed", err);
151 /* Wait for reset complete */
152 uint32_t to = 0;
153 for (to = 0; to < jl1101->reset_timeout_ms / 50; to++) {
154 vTaskDelay(pdMS_TO_TICKS(50));
155 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, "read BMCR failed",
156 err);
157 if (!bmcr.reset) {
158 break;
159 }
160 }
161 PHY_CHECK(to < jl1101->reset_timeout_ms / 50, "reset timeout", err);
162 return ESP_OK;
163err:
164 return ESP_FAIL;
165}
166
167static esp_err_t jl1101_reset_hw(esp_eth_phy_t *phy) {
168 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
169 if (jl1101->reset_gpio_num >= 0) {
170 esp_rom_gpio_pad_select_gpio(jl1101->reset_gpio_num);
171 gpio_set_direction(jl1101->reset_gpio_num, GPIO_MODE_OUTPUT);
172 gpio_set_level(jl1101->reset_gpio_num, 0);
173 esp_rom_delay_us(100); // insert min input assert time
174 gpio_set_level(jl1101->reset_gpio_num, 1);
175 }
176 return ESP_OK;
177}
178
179static esp_err_t jl1101_negotiate(esp_eth_phy_t *phy, eth_phy_autoneg_cmd_t cmd, bool *nego_state) {
180 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
181 esp_eth_mediator_t *eth = jl1101->eth;
182 /* in case any link status has changed, let's assume we're in link down status */
183 jl1101->link_status = ETH_LINK_DOWN;
184 /* Restart auto negotiation */
185 bmcr_reg_t bmcr = {
186 .speed_select = 1, /* 100Mbps */
187 .duplex_mode = 1, /* Full Duplex */
188 .en_auto_nego = 1, /* Auto Negotiation */
189 .restart_auto_nego = 1 /* Restart Auto Negotiation */
190 };
191 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK, "write BMCR failed", err);
192 /* Wait for auto negotiation complete */
193 bmsr_reg_t bmsr;
194 uint32_t to = 0;
195 for (to = 0; to < jl1101->autonego_timeout_ms / 100; to++) {
196 vTaskDelay(pdMS_TO_TICKS(100));
197 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMSR_REG_ADDR, &(bmsr.val)) == ESP_OK, "read BMSR failed",
198 err);
199 if (bmsr.auto_nego_complete) {
200 break;
201 }
202 }
203 /* Auto negotiation failed, maybe no network cable plugged in, so output a warning */
204 if (to >= jl1101->autonego_timeout_ms / 100) {
205 ESP_LOGW(TAG, "auto negotiation timeout");
206 }
207 return ESP_OK;
208err:
209 return ESP_FAIL;
210}
211
212static esp_err_t jl1101_pwrctl(esp_eth_phy_t *phy, bool enable) {
213 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
214 esp_eth_mediator_t *eth = jl1101->eth;
215 bmcr_reg_t bmcr;
216 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, "read BMCR failed",
217 err);
218 if (!enable) {
219 /* Enable IEEE Power Down Mode */
220 bmcr.power_down = 1;
221 } else {
222 /* Disable IEEE Power Down Mode */
223 bmcr.power_down = 0;
224 }
225 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, bmcr.val) == ESP_OK, "write BMCR failed", err);
226 if (!enable) {
227 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, "read BMCR failed",
228 err);
229 PHY_CHECK(bmcr.power_down == 1, "power down failed", err);
230 } else {
231 /* wait for power up complete */
232 uint32_t to = 0;
233 for (to = 0; to < jl1101->reset_timeout_ms / 10; to++) {
234 vTaskDelay(pdMS_TO_TICKS(10));
235 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_BMCR_REG_ADDR, &(bmcr.val)) == ESP_OK, "read BMCR failed",
236 err);
237 if (bmcr.power_down == 0) {
238 break;
239 }
240 }
241 PHY_CHECK(to < jl1101->reset_timeout_ms / 10, "power up timeout", err);
242 }
243 return ESP_OK;
244err:
245 return ESP_FAIL;
246}
247
248static esp_err_t jl1101_set_addr(esp_eth_phy_t *phy, uint32_t addr) {
249 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
250 jl1101->addr = addr;
251 return ESP_OK;
252}
253
254static esp_err_t jl1101_get_addr(esp_eth_phy_t *phy, uint32_t *addr) {
255 PHY_CHECK(addr, "addr can't be null", err);
256 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
257 *addr = jl1101->addr;
258 return ESP_OK;
259err:
260 return ESP_ERR_INVALID_ARG;
261}
262
263static esp_err_t jl1101_del(esp_eth_phy_t *phy) {
264 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
265 free(jl1101);
266 return ESP_OK;
267}
268
269static esp_err_t jl1101_advertise_pause_ability(esp_eth_phy_t *phy, uint32_t ability) {
270 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
271 esp_eth_mediator_t *eth = jl1101->eth;
272 /* Set PAUSE function ability */
273 anar_reg_t anar;
274 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_ANAR_REG_ADDR, &(anar.val)) == ESP_OK, "read ANAR failed",
275 err);
276 if (ability) {
277 anar.asymmetric_pause = 1;
278 anar.symmetric_pause = 1;
279 } else {
280 anar.asymmetric_pause = 0;
281 anar.symmetric_pause = 0;
282 }
283 PHY_CHECK(eth->phy_reg_write(eth, jl1101->addr, ETH_PHY_ANAR_REG_ADDR, anar.val) == ESP_OK, "write ANAR failed", err);
284 return ESP_OK;
285err:
286 return ESP_FAIL;
287}
288
289static esp_err_t jl1101_init(esp_eth_phy_t *phy) {
290 phy_jl1101_t *jl1101 = __containerof(phy, phy_jl1101_t, parent);
291 esp_eth_mediator_t *eth = jl1101->eth;
292 // Detect PHY address
293 if (jl1101->addr == ESP_ETH_PHY_ADDR_AUTO) {
294 PHY_CHECK(esp_eth_phy_802_3_detect_phy_addr(eth, &jl1101->addr) == ESP_OK, "Detect PHY address failed", err);
295 }
296 /* Power on Ethernet PHY */
297 PHY_CHECK(jl1101_pwrctl(phy, true) == ESP_OK, "power control failed", err);
298 /* Reset Ethernet PHY */
299 PHY_CHECK(jl1101_reset(phy) == ESP_OK, "reset failed", err);
300 /* Check PHY ID */
301 phyidr1_reg_t id1;
302 phyidr2_reg_t id2;
303 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_IDR1_REG_ADDR, &(id1.val)) == ESP_OK, "read ID1 failed", err);
304 PHY_CHECK(eth->phy_reg_read(eth, jl1101->addr, ETH_PHY_IDR2_REG_ADDR, &(id2.val)) == ESP_OK, "read ID2 failed", err);
305 PHY_CHECK(id1.oui_msb == 0x937C && id2.oui_lsb == 0x10 && id2.vendor_model == 0x2, "wrong chip ID", err);
306 return ESP_OK;
307err:
308 return ESP_FAIL;
309}
310
311static esp_err_t jl1101_deinit(esp_eth_phy_t *phy) {
312 /* Power off Ethernet PHY */
313 PHY_CHECK(jl1101_pwrctl(phy, false) == ESP_OK, "power control failed", err);
314 return ESP_OK;
315err:
316 return ESP_FAIL;
317}
318
319esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config) {
320 PHY_CHECK(config, "can't set phy config to null", err);
321 phy_jl1101_t *jl1101 = calloc(1, sizeof(phy_jl1101_t));
322 PHY_CHECK(jl1101, "calloc jl1101 failed", err);
323 jl1101->addr = config->phy_addr;
324 jl1101->reset_gpio_num = config->reset_gpio_num;
325 jl1101->reset_timeout_ms = config->reset_timeout_ms;
326 jl1101->link_status = ETH_LINK_DOWN;
327 jl1101->autonego_timeout_ms = config->autonego_timeout_ms;
328 jl1101->parent.reset = jl1101_reset;
329 jl1101->parent.reset_hw = jl1101_reset_hw;
330 jl1101->parent.init = jl1101_init;
331 jl1101->parent.deinit = jl1101_deinit;
332 jl1101->parent.set_mediator = jl1101_set_mediator;
333 jl1101->parent.autonego_ctrl = jl1101_negotiate;
334 jl1101->parent.get_link = jl1101_get_link;
335 jl1101->parent.pwrctl = jl1101_pwrctl;
336 jl1101->parent.get_addr = jl1101_get_addr;
337 jl1101->parent.set_addr = jl1101_set_addr;
338 jl1101->parent.advertise_pause_ability = jl1101_advertise_pause_ability;
339 jl1101->parent.del = jl1101_del;
340
341 return &(jl1101->parent);
342err:
343 return NULL;
344}
345
346#endif /* USE_ARDUINO */
347#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