Skip to content

Commit

Permalink
feat(dummy_phy): created a dummy PHY instace for direct EMAC to EMAC
Browse files Browse the repository at this point in the history
  • Loading branch information
kostaond committed Dec 17, 2023
1 parent 4484f21 commit c87a56e
Show file tree
Hide file tree
Showing 22 changed files with 974 additions and 0 deletions.
3 changes: 3 additions & 0 deletions eth_dummy_phy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
idf_component_register(SRCS "src/esp_eth_phy_dummy.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES log esp_eth)
201 changes: 201 additions & 0 deletions eth_dummy_phy/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/

TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

1. Definitions.

"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.

"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.

"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.

"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.

"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.

"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.

"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).

"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.

"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."

"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.

2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.

3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.

4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:

(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and

(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and

(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and

(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.

You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.

5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.

6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.

7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.

8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.

9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Empty file added eth_dummy_phy/README.md
Empty file.
9 changes: 9 additions & 0 deletions eth_dummy_phy/examples/iperf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)

set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/ethernet/iperf/main
./ethernet_init)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(ethernet_iperf)
3 changes: 3 additions & 0 deletions eth_dummy_phy/examples/iperf/ethernet_init/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
idf_component_register(SRCS "eth_dummy_phy_init.c"
PRIV_REQUIRES esp_eth
INCLUDE_DIRS "include")
26 changes: 26 additions & 0 deletions eth_dummy_phy/examples/iperf/ethernet_init/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
menu "Example Connection Configuration"

choice EXAMPLE_RMII_CLK_DEVICE_TYPE
prompt "RMII CLK"
default EXAMPLE_RMII_CLK_SOURCE_DEV
help
Configure device type. ESP32 device which is RMII CLK source needs to wait
with its Ethernet initialization for the "RMII CLK Sink Device" since
the RMII CLK input pin (GPIO0) is also used as a boot strap pin. If
the "RMII CLK Source Device" didn't wait, the "RMII CLK Sink Device"
could boot into incorrect mode.

config EXAMPLE_RMII_CLK_SOURCE_DEV
bool "RMII CLK Source Device"
config EXAMPLE_RMII_CLK_SINK_DEV
bool "RMII CLK Sink Device"
endchoice

config EXAMPLE_CLK_SINK_READY_GPIO
int "RMII CLK Sink Device is ready GPIO"
default 4
help
GPIO number at which the "RMII CLK Sink Device" is ready and so the "RMII
CLK Source Device" can continue in its Ethernet initialization.

endmenu
132 changes: 132 additions & 0 deletions eth_dummy_phy/examples/iperf/ethernet_init/eth_dummy_phy_init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/* Ethernet Basic 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 <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_check.h"
#include "esp_log.h"
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "hal/gpio_hal.h"
#include "esp_system.h"
#include "ethernet_init.h"
#include "esp_eth_phy_dummy.h"

#define STARTUP_DELAY_MS 500

static const char *TAG = "emac2emac_init";

#if CONFIG_EXAMPLE_RMII_CLK_SOURCE_DEV
static bool s_init_done;

IRAM_ATTR static void gpio_isr_handler(void *arg)
{
BaseType_t high_task_wakeup = pdFALSE;
TaskHandle_t task_handle = (TaskHandle_t)arg;

if (gpio_get_level(CONFIG_EXAMPLE_CLK_SINK_READY_GPIO) == 1) {
vTaskNotifyGiveFromISR(task_handle, &high_task_wakeup);
if (high_task_wakeup != pdFALSE) {
portYIELD_FROM_ISR();
}
} else if (s_init_done) {
esp_restart();
}
}
#endif // CONFIG_EXAMPLE_RMII_CLK_SOURCE_DEV

esp_err_t example_eth_init(esp_eth_handle_t *eth_handles_out[], uint8_t *eth_cnt_out)
{
esp_err_t ret = ESP_OK;
ESP_GOTO_ON_FALSE(eth_handles_out != NULL && eth_cnt_out != NULL, ESP_ERR_INVALID_ARG,
err, TAG, "invalid arguments: initialized handles array or number of interfaces");
esp_eth_handle_t *eth_handle = calloc(1, sizeof(esp_eth_handle_t));
ESP_GOTO_ON_FALSE(eth_handle != NULL, ESP_ERR_NO_MEM, err, TAG, "no memory");
#if CONFIG_EXAMPLE_RMII_CLK_SOURCE_DEV
// ESP32 device which is RMII CLK source needs to wait with its Ethernet initialization for the "RMII CLK
// Sink Device" since the RMII CLK input pin (GPIO0) is also used as a boot strap pin. If the "RMII CLK Source Device" didn't wait,
// the "RMII CLK Sink Device" could boot into incorrect mode.
esp_rom_gpio_pad_select_gpio(EMAC_CLK_OUT_180_GPIO);
gpio_set_pull_mode(EMAC_CLK_OUT_180_GPIO, GPIO_FLOATING); // to not affect GPIO0 (so the Sink Device could be flashed)
gpio_install_isr_service(0);
esp_rom_gpio_pad_select_gpio(CONFIG_EXAMPLE_CLK_SINK_READY_GPIO);
gpio_set_direction(CONFIG_EXAMPLE_CLK_SINK_READY_GPIO, GPIO_MODE_INPUT);
gpio_set_pull_mode(CONFIG_EXAMPLE_CLK_SINK_READY_GPIO, GPIO_PULLDOWN_ONLY);
gpio_set_intr_type(CONFIG_EXAMPLE_CLK_SINK_READY_GPIO, GPIO_INTR_ANYEDGE);
gpio_intr_enable(CONFIG_EXAMPLE_CLK_SINK_READY_GPIO);
TaskHandle_t task_handle = xTaskGetHandle(pcTaskGetName(NULL));
gpio_isr_handler_add(CONFIG_EXAMPLE_CLK_SINK_READY_GPIO, gpio_isr_handler, task_handle);
ESP_LOGW(TAG, "waiting for RMII CLK sink device interrupt");
ESP_LOGW(TAG, "if RMII CLK sink device is already running, reset it by `EN` button");
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
ESP_LOGI(TAG, "starting Ethernet initialization");
#else
esp_rom_gpio_pad_select_gpio(CONFIG_EXAMPLE_CLK_SINK_READY_GPIO);
gpio_set_direction(CONFIG_EXAMPLE_CLK_SINK_READY_GPIO, GPIO_MODE_OUTPUT);
gpio_set_pull_mode(CONFIG_EXAMPLE_CLK_SINK_READY_GPIO, GPIO_FLOATING);
gpio_set_level(CONFIG_EXAMPLE_CLK_SINK_READY_GPIO, 0);
vTaskDelay(pdMS_TO_TICKS(STARTUP_DELAY_MS)); // give some time to the "RMII CLK Source Device" get to wait mode
gpio_set_level(CONFIG_EXAMPLE_CLK_SINK_READY_GPIO, 1);
#endif // CONFIG_EXAMPLE_RMII_CLK_SOURCE_DEV

// --- Initialize Ethernet driver ---
// Init common MAC and PHY configs to default
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();

// Update PHY config based on board specific configuration
phy_config.reset_gpio_num = -1;

// Init vendor specific MAC config to default
eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
// Update vendor specific MAC config based on board configuration
// No SMI, speed/duplex must be statically configured the same at both devices
esp32_emac_config.smi_mdc_gpio_num = -1;
esp32_emac_config.smi_mdio_gpio_num = -1;
#if CONFIG_EXAMPLE_RMII_CLK_SOURCE_DEV
esp32_emac_config.clock_config.rmii.clock_mode = EMAC_CLK_OUT;
esp32_emac_config.clock_config.rmii.clock_gpio = EMAC_CLK_OUT_180_GPIO;
#else
esp32_emac_config.clock_config.rmii.clock_mode = EMAC_CLK_EXT_IN;
esp32_emac_config.clock_config.rmii.clock_gpio = EMAC_CLK_IN_GPIO;
#endif // CONFIG_EXAMPLE_RMII_CLK_SOURCE_DEV

// Create new ESP32 Ethernet MAC instance
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
// Create dummy PHY instance
esp_eth_phy_t *phy = esp_eth_phy_new_dummy(&phy_config);

// Init Ethernet driver to default and install it
esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
#if CONFIG_EXAMPLE_RMII_CLK_SINK_DEV
// The "RMII CLK Sink Device" performs multiple EMAC init attempts since the "RMII CLK Source Device" may not be fully initialized yet
int i;
for (i = 1; i < 5; i++) {
ESP_LOGI(TAG, "Ethernet driver install attempt: %i", i);
if (esp_eth_driver_install(&config, eth_handle) == ESP_OK) {
break;
}
vTaskDelay(pdMS_TO_TICKS(100));
}
ESP_GOTO_ON_FALSE(i < 5, ESP_FAIL, err, TAG, "Ethernet driver install failed");
#else
ESP_GOTO_ON_ERROR(esp_eth_driver_install(&config, eth_handle), err, TAG, "Ethernet driver install failed");
s_init_done = true;
#endif // CONFIG_EXAMPLE_RMII_CLK_SINK_DEV
*eth_handles_out = eth_handle;
*eth_cnt_out = 1;
err:
return ret;
}
5 changes: 5 additions & 0 deletions eth_dummy_phy/examples/iperf/ethernet_init/idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependencies:
espressif/eth_dummy_phy:
version: '*'
# For local development use the local copy of the component
override_path: '../../../'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#include "esp_eth_driver.h"
#include "esp_err.h"

esp_err_t example_eth_init(esp_eth_handle_t *eth_handles_out[], uint8_t *eth_cnt_out);
Loading

0 comments on commit c87a56e

Please sign in to comment.