esp_eth_phy.c 1012 B

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include "esp_log.h"
  9. #include "esp_eth_driver.h"
  10. #include "eth_phy_regs_struct.h"
  11. static const char *TAG = "esp_eth.phy";
  12. esp_err_t esp_eth_detect_phy_addr(esp_eth_mediator_t *eth, int *detected_addr)
  13. {
  14. if (!eth || !detected_addr) {
  15. ESP_LOGE(TAG, "eth and detected_addr can't be null");
  16. return ESP_ERR_INVALID_ARG;
  17. }
  18. int addr_try = 0;
  19. uint32_t reg_value = 0;
  20. for (; addr_try < 16; addr_try++) {
  21. eth->phy_reg_read(eth, addr_try, ETH_PHY_IDR1_REG_ADDR, &reg_value);
  22. if (reg_value != 0xFFFF && reg_value != 0x00) {
  23. *detected_addr = addr_try;
  24. break;
  25. }
  26. }
  27. if (addr_try < 16) {
  28. ESP_LOGD(TAG, "Found PHY address: %d", addr_try);
  29. return ESP_OK;
  30. } else {
  31. ESP_LOGE(TAG, "No PHY device detected");
  32. return ESP_ERR_NOT_FOUND;
  33. }
  34. }