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 #include <string.h>
15 #include <stdlib.h>
16 #include "esp_log.h"
17 #include "esp_eth.h"
18 #include "eth_phy_regs_struct.h"
19 
20 static const char *TAG = "esp_eth.phy";
21 
esp_eth_detect_phy_addr(esp_eth_mediator_t * eth,int * detected_addr)22 esp_err_t esp_eth_detect_phy_addr(esp_eth_mediator_t *eth, int *detected_addr)
23 {
24     if (!eth || !detected_addr) {
25         ESP_LOGE(TAG, "eth and detected_addr can't be null");
26         return ESP_ERR_INVALID_ARG;
27     }
28     int addr_try = 0;
29     uint32_t reg_value = 0;
30     for (; addr_try < 16; addr_try++) {
31         eth->phy_reg_read(eth, addr_try, ETH_PHY_IDR1_REG_ADDR, &reg_value);
32         if (reg_value != 0xFFFF && reg_value != 0x00) {
33             *detected_addr = addr_try;
34             break;
35         }
36     }
37     if (addr_try < 16) {
38         ESP_LOGD(TAG, "Found PHY address: %d", addr_try);
39         return ESP_OK;
40     } else {
41         ESP_LOGE(TAG, "No PHY device detected");
42         return ESP_ERR_NOT_FOUND;
43     }
44 }
45