esp_eth_phy.c 1.5 KB

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