Skip to content

Instantly share code, notes, and snippets.

@rojer
Created July 12, 2017 17:38
Show Gist options
  • Save rojer/2cc82b43ed01419b46ba62ed0bc98d28 to your computer and use it in GitHub Desktop.
Save rojer/2cc82b43ed01419b46ba62ed0bc98d28 to your computer and use it in GitHub Desktop.
Ethernet WIP
commit 415a36d6377ed1e9d037c28a4f8f732b73867f19
Author: Deomid Ryabkov <rojer@cesanta.com>
Date: Tue Jul 11 12:06:38 2017 +0100
Ethernet WIP
diff --git a/fw/examples/c_hello/mos.yml b/fw/examples/c_hello/mos.yml
index 64ac1f1..4ef2d06 100644
--- a/fw/examples/c_hello/mos.yml
+++ b/fw/examples/c_hello/mos.yml
@@ -15,6 +15,7 @@ config_schema:
- ["hello", "o", {"title": "Hello app settings"}]
- ["hello.who", "s", "world", {"title": "Who to say hello to"}]
libs:
+ - origin: https://github.com/mongoose-os-libs/ethernet
- origin: https://github.com/mongoose-os-libs/http-server
- origin: https://github.com/mongoose-os-libs/ota-http-server
- origin: https://github.com/mongoose-os-libs/rpc-loopback
diff --git a/fw/examples/c_hello/src/main.c b/fw/examples/c_hello/src/main.c
index edc8e4b..474db54 100644
--- a/fw/examples/c_hello/src/main.c
+++ b/fw/examples/c_hello/src/main.c
@@ -6,6 +6,8 @@
#include "fw/src/mgos_sys_config.h"
#include "fw/src/mgos_timers.h"
+#include "tcpip_adapter.h"
+
#if CS_PLATFORM == CS_P_ESP8266
/* On ESP-12E there is a blue LED connected to GPIO2 (aka U1TX). */
#define LED_GPIO 2
@@ -15,7 +17,7 @@
#elif CS_PLATFORM == CS_P_ESP32
/* Unfortunately, there is no LED on DevKitC, so this is random GPIO. */
#define LED_GPIO 17
-#define BUTTON_GPIO 0 /* Usually a "Flash" button. */
+#define BUTTON_GPIO 34 /* Usually a "Flash" button. */
#define BUTTON_PULL MGOS_GPIO_PULL_UP
#define BUTTON_EDGE MGOS_GPIO_INT_EDGE_POS
#elif CS_PLATFORM == CS_P_CC3200
@@ -42,7 +44,11 @@
static void blink_timer_cb(void *arg) {
bool current_level = mgos_gpio_toggle(LED_GPIO);
- LOG(LL_INFO, ("%s", (current_level ? "Tick" : "Tock")));
+ tcpip_adapter_ip_info_t ip;
+ memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t));
+ esp_err_t ret = tcpip_adapter_get_ip_info(ESP_IF_ETH, &ip);
+ LOG(LL_INFO,
+ ("%s %d " IPSTR, (current_level ? "Tick" : "Tock"), ret, IP2STR(&ip.ip)));
(void) arg;
}
@@ -59,7 +65,8 @@ enum mgos_app_init_result mgos_app_init(void) {
{ /* Set up the blinky timer. */
mgos_gpio_set_mode(LED_GPIO, MGOS_GPIO_MODE_OUTPUT);
- mgos_set_timer(1000 /* ms */, true /* repeat */, blink_timer_cb, NULL);
+ // mgos_set_timer(1000 /* ms */, true /* repeat */, blink_timer_cb,
+ // NULL);
}
{ /* Set up a button handler */
diff --git a/fw/platforms/esp32/sdk.version b/fw/platforms/esp32/sdk.version
index 017a1e9..eb7e219 100644
--- a/fw/platforms/esp32/sdk.version
+++ b/fw/platforms/esp32/sdk.version
@@ -1 +1 @@
-docker.cesanta.com/esp32-build:2.0-r10
+docker.cesanta.com/esp32-build:2.0-r11
diff --git a/fw/platforms/esp32/sdkconfig b/fw/platforms/esp32/sdkconfig
index b11b888..733e1d8 100644
--- a/fw/platforms/esp32/sdkconfig
+++ b/fw/platforms/esp32/sdkconfig
@@ -181,7 +181,11 @@ CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE=y
CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION=
CONFIG_ESP32_PHY_MAX_WIFI_TX_POWER=20
CONFIG_ESP32_PHY_MAX_TX_POWER=20
-CONFIG_ETHERNET=
+CONFIG_ETHERNET=y
+CONFIG_DMA_RX_BUF_NUM=10
+CONFIG_DMA_TX_BUF_NUM=10
+CONFIG_EMAC_L2_TO_L3_RX_BUF_MODE=
+CONFIG_EMAC_TASK_PRIORITY=20
#
# FreeRTOS
diff --git a/fw/platforms/esp32/src/esp32_main.c b/fw/platforms/esp32/src/esp32_main.c
index 5d26119..599a533 100644
--- a/fw/platforms/esp32/src/esp32_main.c
+++ b/fw/platforms/esp32/src/esp32_main.c
@@ -65,6 +65,7 @@ esp_err_t event_handler(void *ctx, system_event_t *event) {
case SYSTEM_EVENT_AP_STACONNECTED:
case SYSTEM_EVENT_AP_STADISCONNECTED:
case SYSTEM_EVENT_SCAN_DONE:
+ case SYSTEM_EVENT_ETH_GOT_IP:
return esp32_wifi_ev(event);
break;
default:
diff --git a/fw/platforms/esp32/src/esp32_wifi.c b/fw/platforms/esp32/src/esp32_wifi.c
index e1089ef..37ada94 100644
--- a/fw/platforms/esp32/src/esp32_wifi.c
+++ b/fw/platforms/esp32/src/esp32_wifi.c
@@ -88,6 +88,7 @@ esp_err_t esp32_wifi_ev(system_event_t *ev) {
mg_ev = MGOS_WIFI_CONNECTED;
break;
case SYSTEM_EVENT_STA_GOT_IP:
+ case SYSTEM_EVENT_ETH_GOT_IP:
mg_ev = MGOS_WIFI_IP_ACQUIRED;
s_sta_state = "got ip";
break;
diff --git a/mos_libs/ethernet/mos.yml b/mos_libs/ethernet/mos.yml
new file mode 100644
index 0000000..687e652
--- /dev/null
+++ b/mos_libs/ethernet/mos.yml
@@ -0,0 +1,15 @@
+author: mongoose-os
+description: Ethernet support
+type: lib
+version: 1.0
+
+config_schema:
+ - ["eth", "o", {title: "Ethernet settings"}]
+ - ["eth.enable", "b", false, {title: "Enable Ethernet interface"}]
+
+tags:
+ - c
+ - ethernet
+ - hw
+
+skeleton_version: 2017-06-16
diff --git a/mos_libs/ethernet/mos_esp32.yml b/mos_libs/ethernet/mos_esp32.yml
new file mode 100644
index 0000000..953adf7
--- /dev/null
+++ b/mos_libs/ethernet/mos_esp32.yml
@@ -0,0 +1,11 @@
+sources:
+ - src/esp32
+
+cdefs:
+ # 8720 or 8710 in RMII mode.
+ MGOS_ETH_PHY_LAN87x0: 1
+
+config_schema:
+ - ["eth.phy_addr", "i", 0, {title: "RMII PHY address"}]
+ - ["eth.mdc_gpio", "i", 23, {title: "GPIO to use for RMII MDC signal"}]
+ - ["eth.mdio_gpio", "i", 18, {title: "GPIO to use for RMII MDIO signal"}]
diff --git a/mos_libs/ethernet/src/esp32/esp32_eth.c b/mos_libs/ethernet/src/esp32/esp32_eth.c
new file mode 100644
index 0000000..5d1c39a
--- /dev/null
+++ b/mos_libs/ethernet/src/esp32/esp32_eth.c
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2014-2017 Cesanta Software Limited
+ * All rights reserved
+ */
+
+#include <stdbool.h>
+
+#include "esp_eth.h"
+#include "eth_phy/phy_lan8720.h"
+#include "eth_phy/phy_tlk110.h"
+#include "tcpip_adapter.h"
+
+#include "fw/src/mgos_sys_config.h"
+
+static void eth_config_pins(void) {
+ phy_rmii_configure_data_interface_pins();
+ phy_rmii_smi_configure_pins(get_cfg()->eth.mdc_gpio,
+ get_cfg()->eth.mdio_gpio);
+}
+
+bool mgos_ethernet_init(void) {
+ struct sys_config_eth *ecfg = &get_cfg()->eth;
+ if (!ecfg->enable) return true;
+
+ eth_config_t config;
+ const char *phy_model;
+#if defined(MGOS_ETH_PHY_LAN87x0)
+ phy_model = "LAN87x0";
+ config = phy_lan8720_default_ethernet_config;
+#elif defined(MGOS_ETH_PHY_TLK110)
+ phy_model = "TLK110";
+ config = phy_tlk110_default_ethernet_config;
+#else
+#error Unknown/unspecified PHY model
+#endif
+
+ /* Set the PHY address in the example configuration */
+ config.phy_addr = ecfg->phy_addr;
+ config.gpio_config = eth_config_pins;
+ config.tcpip_input = tcpip_adapter_eth_input;
+
+ LOG(LL_INFO, ("Ethernet init: %s @ %d", phy_model, ecfg->phy_addr));
+ esp_err_t ret = esp_eth_init(&config);
+ if (ret == ESP_OK) {
+ esp_eth_enable();
+ } else {
+ LOG(LL_ERROR, ("Ethernet init failed: %d", ret));
+ return false;
+ }
+
+ return true;
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment