|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2017 Cesanta Software Limited |
| 3 | + * All rights reserved |
| 4 | + */ |
| 5 | + |
| 6 | +#include <stdbool.h> |
| 7 | + |
| 8 | +#include "esp_eth.h" |
| 9 | +#include "eth_phy/phy_lan8720.h" |
| 10 | +#include "eth_phy/phy_tlk110.h" |
| 11 | +#include "tcpip_adapter.h" |
| 12 | + |
| 13 | +#include "fw/src/mgos_net.h" |
| 14 | +#include "fw/src/mgos_net_hal.h" |
| 15 | +#include "fw/src/mgos_sys_config.h" |
| 16 | + |
| 17 | +static void eth_config_pins(void) { |
| 18 | + phy_rmii_configure_data_interface_pins(); |
| 19 | + phy_rmii_smi_configure_pins(get_cfg()->eth.mdc_gpio, |
| 20 | + get_cfg()->eth.mdio_gpio); |
| 21 | +} |
| 22 | + |
| 23 | +bool mgos_ethernet_init(void) { |
| 24 | + struct sys_config_eth *ecfg = &get_cfg()->eth; |
| 25 | + if (!ecfg->enable) return true; |
| 26 | + |
| 27 | + eth_config_t config; |
| 28 | + const char *phy_model; |
| 29 | +#if defined(MGOS_ETH_PHY_LAN87x0) |
| 30 | + phy_model = "LAN87x0"; |
| 31 | + config = phy_lan8720_default_ethernet_config; |
| 32 | +#elif defined(MGOS_ETH_PHY_TLK110) |
| 33 | + phy_model = "TLK110"; |
| 34 | + config = phy_tlk110_default_ethernet_config; |
| 35 | +#else |
| 36 | +#error Unknown/unspecified PHY model |
| 37 | +#endif |
| 38 | + |
| 39 | + /* Set the PHY address in the example configuration */ |
| 40 | + config.phy_addr = ecfg->phy_addr; |
| 41 | + config.gpio_config = eth_config_pins; |
| 42 | + config.tcpip_input = tcpip_adapter_eth_input; |
| 43 | + |
| 44 | + LOG(LL_INFO, ("Eth init: %s PHY @ %d", phy_model, ecfg->phy_addr)); |
| 45 | + esp_err_t ret = esp_eth_init(&config); |
| 46 | + if (ret == ESP_OK) { |
| 47 | + esp_eth_enable(); |
| 48 | + } else { |
| 49 | + LOG(LL_ERROR, ("Ethernet init failed: %d", ret)); |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + return true; |
| 54 | +} |
| 55 | + |
| 56 | +bool mgos_eth_dev_get_ip_info(int if_instance, |
| 57 | + struct mgos_net_ip_info *ip_info) { |
| 58 | + tcpip_adapter_ip_info_t info; |
| 59 | + if (if_instance != 0 || |
| 60 | + tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &info) != ESP_OK || |
| 61 | + info.ip.addr == 0) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + ip_info->ip.sin_addr.s_addr = info.ip.addr; |
| 65 | + ip_info->netmask.sin_addr.s_addr = info.netmask.addr; |
| 66 | + ip_info->gw.sin_addr.s_addr = info.gw.addr; |
| 67 | + return true; |
| 68 | +} |
0 commit comments