phy_common.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "eth_phy/phy.h"
  14. #include "eth_phy/phy_reg.h"
  15. #include "driver/gpio.h"
  16. #include "esp_log.h"
  17. static const char *TAG = "phy_common";
  18. void phy_rmii_configure_data_interface_pins(void)
  19. {
  20. // CRS_DRV to GPIO27
  21. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO27_U, FUNC_GPIO27_EMAC_RX_DV);
  22. // TXD0 to GPIO19
  23. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO19_U, FUNC_GPIO19_EMAC_TXD0);
  24. // TX_EN to GPIO21
  25. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO21_U, FUNC_GPIO21_EMAC_TX_EN);
  26. // TXD1 to GPIO22
  27. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO22_U, FUNC_GPIO22_EMAC_TXD1);
  28. // RXD0 to GPIO25
  29. gpio_set_direction(25, GPIO_MODE_INPUT);
  30. // RXD1 to GPIO26
  31. gpio_set_direction(26, GPIO_MODE_INPUT);
  32. // RMII CLK to GPIO0
  33. gpio_set_direction(0, GPIO_MODE_INPUT);
  34. }
  35. void phy_rmii_smi_configure_pins(uint8_t mdc_gpio, uint8_t mdio_gpio)
  36. {
  37. gpio_matrix_out(mdc_gpio, EMAC_MDC_O_IDX, 0, 0);
  38. gpio_matrix_out(mdio_gpio, EMAC_MDO_O_IDX, 0, 0);
  39. gpio_matrix_in(mdio_gpio, EMAC_MDI_I_IDX, 0);
  40. }
  41. void phy_mii_enable_flow_ctrl(void)
  42. {
  43. uint32_t data = esp_eth_smi_read(MII_AUTO_NEG_ADVERTISEMENT_REG);
  44. data |= MII_ASM_DIR | MII_PAUSE;
  45. esp_eth_smi_write(MII_AUTO_NEG_ADVERTISEMENT_REG, data);
  46. }
  47. bool phy_mii_check_link_status(void)
  48. {
  49. if ((esp_eth_smi_read(MII_BASIC_MODE_STATUS_REG) & MII_LINK_STATUS)) {
  50. ESP_LOGD(TAG, "phy_mii_check_link_status(UP)");
  51. return true;
  52. } else {
  53. ESP_LOGD(TAG, "phy_mii_check_link_status(DOWN)");
  54. return false;
  55. }
  56. }
  57. bool phy_mii_get_partner_pause_enable(void)
  58. {
  59. if((esp_eth_smi_read(MII_PHY_LINK_PARTNER_ABILITY_REG) & MII_PARTNER_PAUSE)) {
  60. ESP_LOGD(TAG, "phy_mii_get_partner_pause_enable(TRUE)");
  61. return true;
  62. } else {
  63. ESP_LOGD(TAG, "phy_mii_get_partner_pause_enable(FALSE)");
  64. return false;
  65. }
  66. }