commit 1a13428c9d39f733c27cb276c4ef963c771ba3e9 Author: Jakob Gegeniger Date: Sat Nov 16 10:38:58 2024 +0100 Init: Create Project from eth2ap example diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..8d7b92d --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,47 @@ +FROM espressif/idf + +ARG DEBIAN_FRONTEND=nointeractive +ARG CONTAINER_USER=esp +ARG USER_UID=1050 +ARG USER_GID=$USER_UID + +RUN apt-get update \ + && apt install -y -q \ + cmake \ + git \ + libglib2.0-0 \ + libnuma1 \ + libpixman-1-0 \ + && rm -rf /var/lib/apt/lists/* + +# QEMU +ENV QEMU_REL=esp_develop_8.2.0_20240122 +ENV QEMU_SHA256=e7c72ef5705ad1444d391711088c8717fc89f42e9bf6d1487f9c2a326b8cfa83 +ENV QEMU_DIST=qemu-xtensa-softmmu-${QEMU_REL}-x86_64-linux-gnu.tar.xz +ENV QEMU_URL=https://github.com/espressif/qemu/releases/download/esp-develop-8.2.0-20240122/${QEMU_DIST} + +ENV LC_ALL=C.UTF-8 +ENV LANG=C.UTF-8 + +RUN wget --no-verbose ${QEMU_URL} \ + && echo "${QEMU_SHA256} *${QEMU_DIST}" | sha256sum --check --strict - \ + && tar -xf $QEMU_DIST -C /opt \ + && rm ${QEMU_DIST} + +ENV PATH=/opt/qemu/bin:${PATH} + +RUN groupadd --gid $USER_GID $CONTAINER_USER \ + && adduser --uid $USER_UID --gid $USER_GID --disabled-password --gecos "" ${CONTAINER_USER} \ + && usermod -a -G root $CONTAINER_USER && usermod -a -G dialout $CONTAINER_USER + +RUN chmod -R 775 /opt/esp/python_env/ + +USER ${CONTAINER_USER} +ENV USER=${CONTAINER_USER} +WORKDIR /home/${CONTAINER_USER} + +RUN echo "source /opt/esp/idf/export.sh > /dev/null 2>&1" >> ~/.bashrc + +ENTRYPOINT [ "/opt/esp/entrypoint.sh" ] + +CMD ["/bin/bash", "-c"] \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..09d3b42 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,36 @@ +{ + "name": "ESP-IDF QEMU", + "build": { + "dockerfile": "Dockerfile" + }, + "customizations": { + "vscode": { + "settings": { + "terminal.integrated.defaultProfile.linux": "bash", + "idf.espIdfPath": "/opt/esp/idf", + "idf.customExtraPaths": "", + "idf.pythonBinPath": "/opt/esp/python_env/idf5.4_py3.12_env/bin/python", + "idf.toolsPath": "/opt/esp", + "idf.gitPath": "/usr/bin/git" + }, + "extensions": [ + "espressif.esp-idf-extension" + ] + }, + "codespaces": { + "settings": { + "terminal.integrated.defaultProfile.linux": "bash", + "idf.espIdfPath": "/opt/esp/idf", + "idf.customExtraPaths": "", + "idf.pythonBinPath": "/opt/esp/python_env/idf5.4_py3.12_env/bin/python", + "idf.toolsPath": "/opt/esp", + "idf.gitPath": "/usr/bin/git" + }, + "extensions": [ + "espressif.esp-idf-extension", + "espressif.esp-idf-web" + ] + } + }, + "runArgs": ["--privileged"] +} \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..5610178 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,23 @@ +{ + "configurations": [ + { + "name": "ESP-IDF", + "compilerPath": "${config:idf.toolsPathWin}\\tools\\xtensa-esp-elf\\esp-13.2.0_20240530\\xtensa-esp-elf\\bin\\xtensa-esp32-elf-gcc.exe", + "compileCommands": "${config:idf.buildPath}/compile_commands.json", + "includePath": [ + "${config:idf.espIdfPath}/components/**", + "${config:idf.espIdfPathWin}/components/**", + "${workspaceFolder}/**" + ], + "browse": { + "path": [ + "${config:idf.espIdfPath}/components", + "${config:idf.espIdfPathWin}/components", + "${workspaceFolder}" + ], + "limitSymbolsToIncludedHeaders": true + } + } + ], + "version": 4 +} diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..2511a38 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "gdbtarget", + "request": "attach", + "name": "Eclipse CDT GDB Adapter" + }, + { + "type": "espidf", + "name": "Launch", + "request": "launch" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f7119b3 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,17 @@ +{ + "C_Cpp.intelliSenseEngine": "default", + "idf.adapterTargetName": "esp32", + "idf.customExtraPaths": "C:\\Tools\\ESP\\tools\\xtensa-esp-elf-gdb\\14.2_20240403\\xtensa-esp-elf-gdb\\bin;C:\\Tools\\ESP\\tools\\riscv32-esp-elf-gdb\\14.2_20240403\\riscv32-esp-elf-gdb\\bin;C:\\Tools\\ESP\\tools\\xtensa-esp-elf\\esp-13.2.0_20240530\\xtensa-esp-elf\\bin;C:\\Tools\\ESP\\tools\\riscv32-esp-elf\\esp-13.2.0_20240530\\riscv32-esp-elf\\bin;C:\\Tools\\ESP\\tools\\esp32ulp-elf\\2.38_20240113\\esp32ulp-elf\\bin;C:\\Tools\\ESP\\tools\\cmake\\3.24.0\\bin;C:\\Tools\\ESP\\tools\\openocd-esp32\\v0.12.0-esp32-20240318\\openocd-esp32\\bin;C:\\Tools\\ESP\\tools\\ninja\\1.11.1;C:\\Tools\\ESP\\tools\\idf-exe\\1.0.3;C:\\Tools\\ESP\\tools\\ccache\\4.8\\ccache-4.8-windows-x86_64;C:\\Tools\\ESP\\tools\\dfu-util\\0.11\\dfu-util-0.11-win64;C:\\Tools\\ESP\\tools\\esp-rom-elfs\\20240305", + "idf.customExtraVars": { + "OPENOCD_SCRIPTS": "C:\\Tools\\ESP\\tools\\openocd-esp32\\v0.12.0-esp32-20240318/openocd-esp32/share/openocd/scripts", + "IDF_CCACHE_ENABLE": "1", + "ESP_ROM_ELF_DIR": "C:\\Tools\\ESP\\tools\\esp-rom-elfs\\20240305/" + }, + "idf.espIdfPathWin": "C:\\Tools\\ESP-IDF\\v5.3\\esp-idf", + "idf.openOcdConfigs": [ + "board/esp32-wrover-kit-3.3v.cfg" + ], + "idf.portWin": "COM4", + "idf.pythonBinPathWin": "C:\\Tools\\ESP\\python_env\\idf5.3_py3.11_env\\Scripts\\python.exe", + "idf.toolsPathWin": "C:\\Tools\\ESP" +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..1dc7915 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,259 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Build - Build project", + "type": "shell", + "command": "${config:idf.pythonBinPath} ${config:idf.espIdfPath}/tools/idf.py build", + "windows": { + "command": "${config:idf.pythonBinPathWin} ${config:idf.espIdfPathWin}\\tools\\idf.py build", + "options": { + "env": { + "PATH": "${env:PATH};${config:idf.customExtraPaths}" + } + } + }, + "options": { + "env": { + "PATH": "${env:PATH}:${config:idf.customExtraPaths}" + } + }, + "problemMatcher": [ + { + "owner": "cpp", + "fileLocation": [ + "autoDetect", + "${workspaceFolder}" + ], + "pattern": { + "regexp": "^(.*?):(\\d+):(\\d*):?\\s+(?:fatal\\s+)?(warning|error):\\s+(.*)$", + "file": 1, + "line": 2, + "column": 3, + "severity": 4, + "message": 5 + } + } + ], + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "label": "Set ESP-IDF Target", + "type": "shell", + "command": "${command:espIdf.setTarget}", + "problemMatcher": { + "owner": "cpp", + "fileLocation": [ + "autoDetect", + "${workspaceFolder}" + ], + "pattern": { + "regexp": "^(.*?):(\\d+):(\\d*):?\\s+(?:fatal\\s+)?(warning|error):\\s+(.*)$", + "file": 1, + "line": 2, + "column": 3, + "severity": 4, + "message": 5 + } + } + }, + { + "label": "Clean - Clean the project", + "type": "shell", + "command": "${config:idf.pythonBinPath} ${config:idf.espIdfPath}/tools/idf.py fullclean", + "windows": { + "command": "${config:idf.pythonBinPathWin} ${config:idf.espIdfPathWin}\\tools\\idf.py fullclean", + "options": { + "env": { + "PATH": "${env:PATH};${config:idf.customExtraPaths}" + } + } + }, + "options": { + "env": { + "PATH": "${env:PATH}:${config:idf.customExtraPaths}" + } + }, + "problemMatcher": [ + { + "owner": "cpp", + "fileLocation": [ + "autoDetect", + "${workspaceFolder}" + ], + "pattern": { + "regexp": "^(.*?):(\\d+):(\\d*):?\\s+(?:fatal\\s+)?(warning|error):\\s+(.*)$", + "file": 1, + "line": 2, + "column": 3, + "severity": 4, + "message": 5 + } + } + ] + }, + { + "label": "Flash - Flash the device", + "type": "shell", + "command": "${config:idf.pythonBinPath} ${config:idf.espIdfPath}/tools/idf.py -p ${config:idf.port} -b ${config:idf.flashBaudRate} flash", + "windows": { + "command": "${config:idf.pythonBinPathWin} ${config:idf.espIdfPathWin}\\tools\\idf.py flash -p ${config:idf.portWin} -b ${config:idf.flashBaudRate}", + "options": { + "env": { + "PATH": "${env:PATH};${config:idf.customExtraPaths}" + } + } + }, + "options": { + "env": { + "PATH": "${env:PATH}:${config:idf.customExtraPaths}" + } + }, + "problemMatcher": [ + { + "owner": "cpp", + "fileLocation": [ + "autoDetect", + "${workspaceFolder}" + ], + "pattern": { + "regexp": "^(.*?):(\\d+):(\\d*):?\\s+(?:fatal\\s+)?(warning|error):\\s+(.*)$", + "file": 1, + "line": 2, + "column": 3, + "severity": 4, + "message": 5 + } + } + ] + }, + { + "label": "Monitor: Start the monitor", + "type": "shell", + "command": "${config:idf.pythonBinPath} ${config:idf.espIdfPath}/tools/idf.py -p ${config:idf.port} monitor", + "windows": { + "command": "${config:idf.pythonBinPathWin} ${config:idf.espIdfPathWin}\\tools\\idf.py -p ${config:idf.portWin} monitor", + "options": { + "env": { + "PATH": "${env:PATH};${config:idf.customExtraPaths}" + } + } + }, + "options": { + "env": { + "PATH": "${env:PATH}:${config:idf.customExtraPaths}" + } + }, + "problemMatcher": [ + { + "owner": "cpp", + "fileLocation": [ + "autoDetect", + "${workspaceFolder}" + ], + "pattern": { + "regexp": "^(.*?):(\\d+):(\\d*):?\\s+(?:fatal\\s+)?(warning|error):\\s+(.*)$", + "file": 1, + "line": 2, + "column": 3, + "severity": 4, + "message": 5 + } + } + ], + "dependsOn": "Flash - Flash the device" + }, + { + "label": "OpenOCD: Start openOCD", + "type": "shell", + "presentation": { + "echo": true, + "reveal": "never", + "focus": false, + "panel": "new" + }, + "command": "openocd -s ${command:espIdf.getOpenOcdScriptValue} ${command:espIdf.getOpenOcdConfigs}", + "windows": { + "command": "openocd.exe -s ${command:espIdf.getOpenOcdScriptValue} ${command:espIdf.getOpenOcdConfigs}", + "options": { + "env": { + "PATH": "${env:PATH};${config:idf.customExtraPaths}" + } + } + }, + "options": { + "env": { + "PATH": "${env:PATH}:${config:idf.customExtraPaths}" + } + }, + "problemMatcher": { + "owner": "cpp", + "fileLocation": [ + "autoDetect", + "${workspaceFolder}" + ], + "pattern": { + "regexp": "^(.*?):(\\d+):(\\d*):?\\s+(?:fatal\\s+)?(warning|error):\\s+(.*)$", + "file": 1, + "line": 2, + "column": 3, + "severity": 4, + "message": 5 + } + } + }, + { + "label": "adapter", + "type": "shell", + "command": "${config:idf.pythonBinPath}", + "isBackground": true, + "options": { + "env": { + "PATH": "${env:PATH}:${config:idf.customExtraPaths}", + "PYTHONPATH": "${command:espIdf.getExtensionPath}/esp_debug_adapter/debug_adapter" + } + }, + "problemMatcher": { + "background": { + "beginsPattern": "\bDEBUG_ADAPTER_STARTED\b", + "endsPattern": "DEBUG_ADAPTER_READY2CONNECT", + "activeOnStart": true + }, + "pattern": { + "regexp": "(\\d+)-(\\d+)-(\\d+)\\s(\\d+):(\\d+):(\\d+),(\\d+)\\s-(.+)\\s(ERROR)", + "file": 8, + "line": 2, + "column": 3, + "severity": 4, + "message": 9 + } + }, + "args": [ + "${command:espIdf.getExtensionPath}/esp_debug_adapter/debug_adapter_main.py", + "-e", + "${workspaceFolder}/build/${command:espIdf.getProjectName}.elf", + "-s", + "$OPENOCD_SCRIPTS", + "-dn", + "esp32", + "-om", + "connect_to_instance", + "-t", + "xtensa-esp32-elf-" + + ], + "windows": { + "command": "${config:idf.pythonBinPathWin}", + "options": { + "env": { + "PATH": "${env:PATH};${config:idf.customExtraPaths}", + "PYTHONPATH": "${command:espIdf.getExtensionPath}/esp_debug_adapter/debug_adapter" + } + } + } + } + ] +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..0678268 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(X_WatchController-FW) diff --git a/README.md b/README.md new file mode 100644 index 0000000..861ddc8 --- /dev/null +++ b/README.md @@ -0,0 +1,114 @@ +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | + +# eth2ap Example +(See the README.md file in the upper level 'examples' directory for more information about examples. To try a more complex application about Ethernet to WiFi data forwarding, please go to [iot-solution](https://github.com/espressif/esp-iot-solution/tree/release/v1.0/examples/eth2wifi).) + +**Note:** This example uses some internal APIs (e.g. `esp_wifi_internal_tx`) which might get changed between minor versions of ESP-IDF. + +## Overview +![eth2ap](eth2ap.png) + +The similarities on MAC layer between Ethernet and Wi-Fi make it easy to forward packets from Ethernet to Wi-Fi and vice versa. This example illustrates how to implement a simple "router" which only supports forwarding packets between Ethernet port and Wi-Fi AP interface. In this case, the Ethernet should play the role of WAN (i.e. it can access outside network) so that a mobile device could get access to the Internet when it gets connected to ESP32 through Wi-Fi. + +**Note:** In this example, ESP32 works like a *bridge* between Ethernet and Wi-Fi, and it won't perform any actions on Layer3 and higher layer, which means there's no need to initialize the TCP/IP stack. + +## How to use example + +### Hardware Required + +To run this example, it's recommended that you have an official ESP32 Ethernet development board - [ESP32-Ethernet-Kit](https://docs.espressif.com/projects/esp-idf/en/latest/hw-reference/get-started-ethernet-kit.html). This example should also work for 3rd party ESP32 board as long as it's integrated with a supported Ethernet PHY chip. Up until now, ESP-IDF supports up to four Ethernet PHY: `LAN8720`, `IP101`, `DP83848` and `RTL8201`, additional PHY drivers should be implemented by users themselves. + +Besides that, `esp_eth` component can drive third-party Ethernet module which integrates MAC and PHY and provides common communication interface (e.g. SPI, USB, etc). This example will take the **DM9051** as an example, illustrating how to install the Ethernet driver in the same manner. + +#### Pin Assignment + +See common pin assignments for Ethernet examples from [upper level](../README.md#common-pin-assignments). + +### Configure the project + +``` +idf.py menuconfig +``` + +In addition to the common configurations for Ethernet examples from [upper level](../README.md#common-configurations), you might also need to update the default value of following configurations: + +In the `Example Configuration` menu: +* Set the SSID and password for Wi-Fi ap interface under `Wi-Fi SSID` and `Wi-Fi Password`. +* Set the maximum connection number under `Maximum STA connections`. + +### Build, Flash, and Run + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT build flash monitor +``` + +(Replace PORT with the name of the serial port to use.) + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +### Step 1: Initialize Ethernet and Wi-Fi (AP mode) + +```bash +I (508) example: Power On Ethernet PHY +I (518) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +I (518) emac: emac reset done +I (518) example: Ethernet Started +...... +I (538) wifi: wifi driver task: 3ffc7fbc, prio:23, stack:3584, core=0 +I (538) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +I (538) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +I (568) wifi: wifi firmware version: ec61a20 +I (568) wifi: config NVS flash: enabled +I (568) wifi: config nano formating: disabled +I (568) wifi: Init dynamic tx buffer num: 32 +I (568) wifi: Init data frame dynamic rx buffer num: 32 +I (578) wifi: Init management frame dynamic rx buffer num: 32 +I (588) wifi: Init management short buffer num: 32 +I (588) wifi: Init static rx buffer size: 1600 +I (588) wifi: Init static rx buffer num: 10 +I (598) wifi: Init dynamic rx buffer num: 32 +``` + +### Step 2: Ethernet Connects to Router/Switch/PC (with DHCP server enabled) + +```bash +I (4518) example: Ethernet Link Up +``` + +### Step 3: Start Wi-Fi AP + +```bash +I (4618) phy: phy_version: 4100, 2a5dd04, Jan 23 2019, 21:00:07, 0, 0 +I (4618) wifi: mode : softAP (30:ae:a4:c6:87:5b) +I (4628) wifi: Total power save buffer number: 16 +I (4628) wifi: Init max length of beacon: 752/752 +I (4628) wifi: Init max length of beacon: 752/752 +``` + +### Step 4: Wi-Fi station (e.g. mobile phone) connects to ESP32's Wi-Fi + +```bash +I (10168) wifi: new:<1,0>, old:<1,0>, ap:<1,1>, sta:<255,255>, prof:1 +I (10168) wifi: station: c4:0b:cb:ec:9a:84 join, AID=1, bgn, 20 +I (10258) example: AP got a station connected +``` + +Now your mobile phone should get access to the Internet. + +## Troubleshooting + +See common troubleshooting for Ethernet examples from [upper level](../README.md#common-troubleshooting). + +* If you got error message like `WiFi send packet failed` when running the example, you may need to enlarge the value of `FLOW_CONTROL_WIFI_SEND_DELAY_MS` in "ethernet_example_main.c", because Ethernet process packets faster than Wi-Fi on ESP32. +* If you got error message like `send flow control message failed or timeout` when running the example, you may need to enlarge the value of `FLOW_CONTROL_QUEUE_LENGTH` in "ethernet_example_main". +* Wi-Fi station doesn't receive any IP via DHCP? + * All Layer 3 (TCP/IP functions) on the ESP32 are disabled, including the SoftAP DHCP server. This means that devices must be able to access another DHCP server (for example on a Wi-Fi router connected via ethernet) or should use statically assigned IP addresses. + +(For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you as soon as possible.) diff --git a/eth2ap.png b/eth2ap.png new file mode 100644 index 0000000..39e59aa Binary files /dev/null and b/eth2ap.png differ diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt new file mode 100644 index 0000000..ff186a4 --- /dev/null +++ b/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "ethernet_example_main.c" + INCLUDE_DIRS ".") diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild new file mode 100644 index 0000000..885ccf6 --- /dev/null +++ b/main/Kconfig.projbuild @@ -0,0 +1,28 @@ +menu "Example Configuration" + + config EXAMPLE_WIFI_SSID + string "Wi-Fi SSID" + default "eth2ap" + help + Set the SSID of Wi-Fi ap interface. + + config EXAMPLE_WIFI_PASSWORD + string "Wi-Fi Password" + default "12345678" + help + Set the password of Wi-Fi ap interface. + + config EXAMPLE_WIFI_CHANNEL + int "WiFi channel" + range 1 13 + default 1 + help + Set the channel of Wi-Fi ap. + + config EXAMPLE_MAX_STA_CONN + int "Maximum STA connections" + default 4 + help + Maximum number of the station that allowed to connect to current Wi-Fi hotspot. + +endmenu diff --git a/main/ethernet_example_main.c b/main/ethernet_example_main.c new file mode 100644 index 0000000..27ed39a --- /dev/null +++ b/main/ethernet_example_main.c @@ -0,0 +1,222 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +/* eth2ap (Ethernet to Wi-Fi AP packet forwarding) Example + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ +#include +#include +#include "sdkconfig.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/queue.h" +#include "esp_event.h" +#include "esp_log.h" +#include "esp_eth_driver.h" +#include "esp_wifi.h" +#include "nvs_flash.h" +#include "esp_private/wifi.h" +#include "ethernet_init.h" + +static const char *TAG = "eth2ap_example"; +static esp_eth_handle_t s_eth_handle = NULL; +static QueueHandle_t flow_control_queue = NULL; +static bool s_sta_is_connected = false; +static bool s_ethernet_is_connected = false; +static uint8_t s_eth_mac[6]; + +#define FLOW_CONTROL_QUEUE_TIMEOUT_MS (100) +#define FLOW_CONTROL_QUEUE_LENGTH (40) +#define FLOW_CONTROL_WIFI_SEND_TIMEOUT_MS (100) + +typedef struct { + void *packet; + uint16_t length; +} flow_control_msg_t; + +// Forward packets from Wi-Fi to Ethernet +static esp_err_t pkt_wifi2eth(void *buffer, uint16_t len, void *eb) +{ + if (s_ethernet_is_connected) { + if (esp_eth_transmit(s_eth_handle, buffer, len) != ESP_OK) { + ESP_LOGE(TAG, "Ethernet send packet failed"); + } + } + esp_wifi_internal_free_rx_buffer(eb); + return ESP_OK; +} + +// Forward packets from Ethernet to Wi-Fi +// Note that, Ethernet works faster than Wi-Fi on ESP32, +// so we need to add an extra queue to balance their speed difference. +static esp_err_t pkt_eth2wifi(esp_eth_handle_t eth_handle, uint8_t *buffer, uint32_t len, void *priv) +{ + esp_err_t ret = ESP_OK; + flow_control_msg_t msg = { + .packet = buffer, + .length = len + }; + if (xQueueSend(flow_control_queue, &msg, pdMS_TO_TICKS(FLOW_CONTROL_QUEUE_TIMEOUT_MS)) != pdTRUE) { + ESP_LOGE(TAG, "send flow control message failed or timeout"); + free(buffer); + ret = ESP_FAIL; + } + return ret; +} + +// This task will fetch the packet from the queue, and then send out through Wi-Fi. +// Wi-Fi handles packets slower than Ethernet, we might add some delay between each transmitting. +static void eth2wifi_flow_control_task(void *args) +{ + flow_control_msg_t msg; + int res = 0; + uint32_t timeout = 0; + while (1) { + if (xQueueReceive(flow_control_queue, &msg, pdMS_TO_TICKS(FLOW_CONTROL_QUEUE_TIMEOUT_MS)) == pdTRUE) { + timeout = 0; + if (s_sta_is_connected && msg.length) { + do { + vTaskDelay(pdMS_TO_TICKS(timeout)); + timeout += 2; + res = esp_wifi_internal_tx(WIFI_IF_AP, msg.packet, msg.length); + } while (res && timeout < FLOW_CONTROL_WIFI_SEND_TIMEOUT_MS); + if (res != ESP_OK) { + ESP_LOGE(TAG, "WiFi send packet failed: %d", res); + } + } + free(msg.packet); + } + } + vTaskDelete(NULL); +} + +// Event handler for Ethernet +static void eth_event_handler(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + switch (event_id) { + case ETHERNET_EVENT_CONNECTED: + ESP_LOGI(TAG, "Ethernet Link Up"); + s_ethernet_is_connected = true; + esp_eth_ioctl(s_eth_handle, ETH_CMD_G_MAC_ADDR, s_eth_mac); + esp_wifi_set_mac(WIFI_IF_AP, s_eth_mac); + ESP_ERROR_CHECK(esp_wifi_start()); + break; + case ETHERNET_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "Ethernet Link Down"); + s_ethernet_is_connected = false; + ESP_ERROR_CHECK(esp_wifi_stop()); + break; + case ETHERNET_EVENT_START: + ESP_LOGI(TAG, "Ethernet Started"); + break; + case ETHERNET_EVENT_STOP: + ESP_LOGI(TAG, "Ethernet Stopped"); + break; + default: + break; + } +} + +// Event handler for Wi-Fi +static void wifi_event_handler(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + static uint8_t s_con_cnt = 0; + switch (event_id) { + case WIFI_EVENT_AP_STACONNECTED: + ESP_LOGI(TAG, "Wi-Fi AP got a station connected"); + if (!s_con_cnt) { + s_sta_is_connected = true; + esp_wifi_internal_reg_rxcb(WIFI_IF_AP, pkt_wifi2eth); + } + s_con_cnt++; + break; + case WIFI_EVENT_AP_STADISCONNECTED: + ESP_LOGI(TAG, "Wi-Fi AP got a station disconnected"); + s_con_cnt--; + if (!s_con_cnt) { + s_sta_is_connected = false; + esp_wifi_internal_reg_rxcb(WIFI_IF_AP, NULL); + } + break; + default: + break; + } +} + +static void initialize_ethernet(void) +{ + uint8_t eth_port_cnt = 0; + esp_eth_handle_t *eth_handles; + ESP_ERROR_CHECK(example_eth_init(ð_handles, ð_port_cnt)); + if (eth_port_cnt > 1) { + ESP_LOGW(TAG, "multiple Ethernet devices detected, the first initialized is to be used!"); + } + s_eth_handle = eth_handles[0]; + free(eth_handles); + ESP_ERROR_CHECK(esp_eth_update_input_path(s_eth_handle, pkt_eth2wifi, NULL)); + bool eth_promiscuous = true; + ESP_ERROR_CHECK(esp_eth_ioctl(s_eth_handle, ETH_CMD_S_PROMISCUOUS, ð_promiscuous)); + ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler, NULL)); + ESP_ERROR_CHECK(esp_eth_start(s_eth_handle)); +} + +static void initialize_wifi(void) +{ + ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, NULL)); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + wifi_config_t wifi_config = { + .ap = { + .ssid = CONFIG_EXAMPLE_WIFI_SSID, + .ssid_len = strlen(CONFIG_EXAMPLE_WIFI_SSID), + .password = CONFIG_EXAMPLE_WIFI_PASSWORD, + .max_connection = CONFIG_EXAMPLE_MAX_STA_CONN, + .authmode = WIFI_AUTH_WPA_WPA2_PSK, + .channel = CONFIG_EXAMPLE_WIFI_CHANNEL // default: channel 1 + }, + }; + if (strlen(CONFIG_EXAMPLE_WIFI_PASSWORD) == 0) { + wifi_config.ap.authmode = WIFI_AUTH_OPEN; + } + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); + ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config)); +} + +static esp_err_t initialize_flow_control(void) +{ + flow_control_queue = xQueueCreate(FLOW_CONTROL_QUEUE_LENGTH, sizeof(flow_control_msg_t)); + if (!flow_control_queue) { + ESP_LOGE(TAG, "create flow control queue failed"); + return ESP_FAIL; + } + BaseType_t ret = xTaskCreate(eth2wifi_flow_control_task, "flow_ctl", 2048, NULL, (tskIDLE_PRIORITY + 2), NULL); + if (ret != pdTRUE) { + ESP_LOGE(TAG, "create flow control task failed"); + return ESP_FAIL; + } + return ESP_OK; +} + +void app_main(void) +{ + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); + } + ESP_ERROR_CHECK(ret); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + ESP_ERROR_CHECK(initialize_flow_control()); + initialize_wifi(); + initialize_ethernet(); +} diff --git a/main/idf_component.yml b/main/idf_component.yml new file mode 100644 index 0000000..8453ecc --- /dev/null +++ b/main/idf_component.yml @@ -0,0 +1,3 @@ +dependencies: + ethernet_init: + path: ${IDF_PATH}/examples/ethernet/basic/components/ethernet_init