Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esp32s3 openeth ethernet driver #13527

Merged
merged 10 commits into from
Oct 13, 2024
15 changes: 10 additions & 5 deletions Documentation/platforms/xtensa/esp32/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -627,25 +627,30 @@ Please check for usage examples using the :doc:`ESP32 DevKitC </platforms/xtensa
Using QEMU
==========

First follow the instructions `here <https://github.com/espressif/qemu/wiki>`__ to build QEMU.
Get or build QEMU from `here <https://github.com/espressif/qemu/wiki>`__.

Enable the ``ESP32_QEMU_IMAGE`` config found in :menuselection:`Board Selection --> ESP32 binary image for QEMU`.

Download the bootloader and the partition table from https://github.com/espressif/esp-nuttx-bootloader/releases
and place them in a directory, say ``../esp-bins``.
Enable ``ESP32_APP_FORMAT_LEGACY``.

Build and generate the QEMU image::

$ make ESPTOOL_BINDIR=../esp-bins
$ make bootloader
$ make ESPTOOL_BINDIR=.

A QEMU-compatible ``nuttx.merged.bin`` binary image will be created. It can be run as::

$ qemu-system-xtensa -nographic -machine esp32 -drive file=nuttx.merged.bin,if=mtd,format=raw

QEMU for ESP32 does not correctly define the chip revision as v3.0 so you have two options:

- #define ``ESP32_IGNORE_CHIP_REVISION_CHECK`` in ``arch/xtensa/src/esp32/esp32_start.c``
- Emulate the efuse as described `here <https://github.com/espressif/esp-toolchain-docs/blob/main/qemu/esp32/README.md#emulating-esp32-eco3>`__.

QEMU Networking
---------------

Networking is possible using the openeth MAC driver. Enable ``ESP32_OPENETH`` option and set the nic in QEMU:
Networking is possible using the openeth MAC driver. Enable ``ESP32_OPENETH`` option and set the nic in QEMU::

$ qemu-system-xtensa -nographic -machine esp32 -drive file=nuttx.merged.bin,if=mtd,format=raw -nic user,model=open_eth

Expand Down
25 changes: 25 additions & 0 deletions Documentation/platforms/xtensa/esp32s3/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,31 @@ possible to track the root cause of the crash. Saving this output to a file and
The above output shows the backtrace of the tasks. By checking it, it is possible to track the
functions that were being executed when the crash occurred.

Using QEMU
==========

Get or build QEMU from `here <https://github.com/espressif/qemu/wiki>`__. The minimum supported version is 9.0.0.

Enable the ``ESP32S3_QEMU_IMAGE`` config found in :menuselection:`Board Selection --> ESP32S3 binary image for QEMU`.

Enable ``ESP32S3_APP_FORMAT_LEGACY``.

Build and generate the QEMU image::

$ make bootloader
$ make ESPTOOL_BINDIR=.

A QEMU-compatible ``nuttx.merged.bin`` binary image will be created. It can be run as::

$ qemu-system-xtensa -nographic -machine esp32s3 -drive file=nuttx.merged.bin,if=mtd,format=raw

QEMU Networking
---------------

Networking is possible using the openeth MAC driver. Enable ``ESP32S3_OPENETH`` option and set the nic in QEMU:

$ qemu-system-xtensa -nographic -machine esp32s3 -drive file=nuttx.merged.bin,if=mtd,format=raw -nic user,model=open_eth

Peripheral Support
==================

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* arch/xtensa/src/esp32/esp32_openeth.c
* arch/xtensa/src/common/espressif/esp_openeth.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
Expand Down Expand Up @@ -30,10 +30,7 @@
#include <netinet/if_ether.h>
#include <nuttx/net/netdev_lowerhalf.h>

#include "hardware/esp32_soc.h"
#include "esp32_irq.h"

#ifdef CONFIG_ESP32_OPENETH
#include <chip.h>

/****************************************************************************
* Pre-processor Definitions
Expand All @@ -43,7 +40,7 @@

/* DMA buffers configuration */
#define DMA_BUF_SIZE 1600
#define RX_BUF_COUNT CONFIG_ESP32_OPENETH_DMA_RX_BUFFER_NUM

/* Only need 1 TX buf because packets are transmitted immediately */
#define TX_BUF_COUNT 1

Expand Down Expand Up @@ -439,7 +436,7 @@ static int openeth_set_addr(uint8_t *addr)
****************************************************************************/

/****************************************************************************
* Name: esp32_openeth_initialize
* Name: esp_openeth_initialize
*
* Description:
* Initialize the openeth driver
Expand All @@ -452,7 +449,7 @@ static int openeth_set_addr(uint8_t *addr)
*
****************************************************************************/

int esp32_openeth_initialize(void)
int esp_openeth_initialize(void)
{
int ret;
struct openeth_priv_s *priv = &g_openeth;
Expand All @@ -462,8 +459,9 @@ int esp32_openeth_initialize(void)

if (REG_READ(OPENETH_MODER_REG) != OPENETH_MODER_DEFAULT)
{
nerr("CONFIG_ESP32_OPENETH should only be used when running in QEMU.");
nerr("When running the app on the ESP32, use ESP32 EMAC instead.");
nerr("Openeth should only be used when running in QEMU.");
nerr("When running the app on the real hardware,"
"use the real MAC instead.");
abort();
}

Expand Down Expand Up @@ -512,8 +510,8 @@ int esp32_openeth_initialize(void)

/* Setup interrupts */

priv->cpuint = esp32_setup_irq(0, ESP32_PERIPH_EMAC,
1, ESP32_CPUINT_LEVEL);
priv->cpuint = OPENETH_SETUP_IRQ(0, OPENETH_PERIPH_MAC,
1, OPENETH_CPUINT_LEVEL);
if (priv->cpuint < 0)
{
nerr("ERROR: Failed allocate interrupt\n");
Expand All @@ -531,7 +529,7 @@ int esp32_openeth_initialize(void)

/* Attach the interrupt */

ret = irq_attach(ESP32_IRQ_EMAC, openeth_isr_handler, priv);
ret = irq_attach(OPENETH_IRQ_MAC, openeth_isr_handler, priv);

/* Register the device with the OS so that socket IOCTLs can be
* performed.
Expand All @@ -552,5 +550,3 @@ int esp32_openeth_initialize(void)
nerr("Failed initializing ret = %d", ret);
abort();
}

#endif /* CONFIG_ESP32_OPENETH */
2 changes: 1 addition & 1 deletion arch/xtensa/src/esp32/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ endif
endif

ifeq ($(CONFIG_ESP32_OPENETH),y)
CHIP_CSRCS += esp32_openeth.c
CHIP_CSRCS += esp_openeth.c
endif

#############################################################################
Expand Down
13 changes: 13 additions & 0 deletions arch/xtensa/src/esp32/chip.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,23 @@
#include "chip_macros.h"
#include "chip_memory.h"

#if defined(CONFIG_ESP32_OPENETH) && !defined(__ASSEMBLY__)
#include "hardware/esp32_soc.h"
#include "esp32_irq.h"
#endif

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#if defined(CONFIG_ESP32_OPENETH)
#define OPENETH_PERIPH_MAC ESP32_PERIPH_EMAC
#define OPENETH_CPUINT_LEVEL ESP32_CPUINT_LEVEL
#define OPENETH_IRQ_MAC ESP32_IRQ_EMAC
#define OPENETH_SETUP_IRQ esp32_setup_irq
#define RX_BUF_COUNT CONFIG_ESP32_OPENETH_DMA_RX_BUFFER_NUM
#endif

/****************************************************************************
* Public Data
****************************************************************************/
Expand Down
21 changes: 21 additions & 0 deletions arch/xtensa/src/esp32s3/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,27 @@ config ESP32S3_WIFI_LISTEN_INTERVAL

endmenu # ESP32S3_WIFI

config ESP32S3_OPENETH
bool "Opencores Ethernet MAC"
default n
depends on !ESP32S3_WIFI
select NET
select SCHED_WORKQUEUE
---help---
Enable ESP32S3 ethernet opencores support for use with QEMU.
Disable this if you are using the real device.

if ESP32S3_OPENETH

config ESP32S3_OPENETH_DMA_RX_BUFFER_NUM
int "Number of Ethernet DMA Rx buffers"
range 1 64
default 4
---help---
Number of DMA receive buffers, each buffer is 1600 bytes.

endif # ESP32S3_OPENETH

menu "BLE Configuration"
depends on ESP32S3_BLE

Expand Down
4 changes: 4 additions & 0 deletions arch/xtensa/src/esp32s3/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ endif
CHIP_CSRCS += esp32s3_pm.c
endif

ifeq ($(CONFIG_ESP32S3_OPENETH),y)
CHIP_CSRCS += esp_openeth.c
endif

#############################################################################
# Espressif HAL for 3rd Party Platforms
#############################################################################
Expand Down
13 changes: 13 additions & 0 deletions arch/xtensa/src/esp32s3/chip.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,23 @@
#include "chip_macros.h"
#include "chip_memory.h"

#if defined(CONFIG_ESP32S3_OPENETH) && !defined(__ASSEMBLY__)
#include "hardware/esp32s3_soc.h"
#include "esp32s3_irq.h"
#endif

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#if defined(CONFIG_ESP32S3_OPENETH)
#define OPENETH_PERIPH_MAC ESP32S3_PERIPH_MAC
#define OPENETH_CPUINT_LEVEL ESP32S3_CPUINT_LEVEL
#define OPENETH_IRQ_MAC ESP32S3_IRQ_MAC
#define OPENETH_SETUP_IRQ esp32s3_setup_irq
#define RX_BUF_COUNT CONFIG_ESP32S3_OPENETH_DMA_RX_BUFFER_NUM
#endif

/****************************************************************************
* Public Data
****************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion arch/xtensa/src/esp32s3/hardware/esp32s3_soc.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
****************************************************************************/

#define DR_REG_USB_BASE 0x60080000

#define DR_REG_EMAC_BASE 0x600CD000
#define DR_REG_ASSIST_DEBUG_BASE 0x600CE000
#define DR_REG_WORLD_CNTL_BASE 0x600D0000
#define DR_REG_DPORT_END 0x600D3FFC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ CONFIG_ARCH_STACKDUMP=y
CONFIG_ARCH_XTENSA=y
CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y
CONFIG_ESP32_APP_FORMAT_LEGACY=y
CONFIG_ESP32_OPENETH=y
CONFIG_ESP32_QEMU_IMAGE=y
CONFIG_ESP32_UART0=y
Expand Down
2 changes: 1 addition & 1 deletion boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ int board_ws2812_initialize(
*
****************************************************************************/
#ifdef CONFIG_ESP32_OPENETH
xiaoxiang781216 marked this conversation as resolved.
Show resolved Hide resolved
int esp32_openeth_initialize(void);
int esp_openeth_initialize(void);
#endif

#endif /* __ASSEMBLY__ */
Expand Down
2 changes: 1 addition & 1 deletion boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ int esp32_bringup(void)
#endif

#ifdef CONFIG_ESP32_OPENETH
ret = esp32_openeth_initialize();
ret = esp_openeth_initialize();
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: Failed to initialize Open ETH ethernet.\n");
Expand Down
7 changes: 7 additions & 0 deletions boards/xtensa/esp32s3/common/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ config ESP32S3_MERGE_BINS
This is only useful when the path to binary files (e.g. bootloader)
is provided via the ESPTOOL_BINDIR variable.

config ESP32S3_QEMU_IMAGE
bool "ESP32S3 binary image for QEMU"
default n
select ESP32S3_MERGE_BINS
---help---
Create a binary flash image used for QEMU.

config ESP32S3_SPEED_UP_ISR
bool "Speed up ISR"
default n
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
# CONFIG_ARCH_LEDS is not set
# CONFIG_NSH_ARGCAT is not set
# CONFIG_NSH_CMDOPT_HEXDUMP is not set
CONFIG_ARCH="xtensa"
CONFIG_ARCH_BOARD="esp32s3-devkit"
CONFIG_ARCH_BOARD_COMMON=y
CONFIG_ARCH_BOARD_ESP32S3_DEVKIT=y
CONFIG_ARCH_CHIP="esp32s3"
CONFIG_ARCH_CHIP_ESP32S3=y
CONFIG_ARCH_CHIP_ESP32S3WROOM1N4=y
CONFIG_ARCH_INTERRUPTSTACK=2048
CONFIG_ARCH_STACKDUMP=y
CONFIG_ARCH_XTENSA=y
CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y
CONFIG_ESP32S3_APP_FORMAT_LEGACY=y
CONFIG_ESP32S3_OPENETH=y
CONFIG_ESP32S3_QEMU_IMAGE=y
CONFIG_ESP32S3_UART0=y
CONFIG_FS_PROCFS=y
CONFIG_HAVE_CXX=y
CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_IDLETHREAD_STACKSIZE=3072
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INIT_STACKSIZE=3072
CONFIG_INTELHEX_BINARY=y
CONFIG_NETDB_DNSCLIENT=y
CONFIG_NETDB_DNSSERVER_IPv4ADDR=0x0a000203
CONFIG_NETDEV_LATEINIT=y
CONFIG_NETDEV_WORK_THREAD=y
CONFIG_NETINIT_DRIPADDR=0x0a000202
CONFIG_NETINIT_IPADDR=0x0a000215
CONFIG_NET_BINDTODEVICE=y
CONFIG_NET_ICMP_NPOLLWAITERS=4
CONFIG_NET_ICMP_SOCKET=y
CONFIG_NET_ROUTE=y
CONFIG_NET_TCP=y
CONFIG_NET_TCPBACKLOG=y
CONFIG_NET_TCP_NPOLLWAITERS=8
CONFIG_NET_TCP_WRITE_BUFFERS=y
CONFIG_NET_UDP=y
CONFIG_NET_UDP_NPOLLWAITERS=8
CONFIG_NET_UDP_WRITE_BUFFERS=y
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_LINELEN=64
CONFIG_NSH_READLINE=y
CONFIG_PREALLOC_TIMERS=4
CONFIG_RAM_SIZE=114688
CONFIG_RAM_START=0x20000000
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_LPWORK=y
CONFIG_SCHED_WAITPID=y
CONFIG_START_DAY=6
CONFIG_START_MONTH=12
CONFIG_START_YEAR=2011
CONFIG_SYSLOG_BUFFER=y
CONFIG_SYSTEM_NSH=y
CONFIG_SYSTEM_PING=y
CONFIG_UART0_SERIAL_CONSOLE=y
4 changes: 4 additions & 0 deletions boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3-devkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,9 @@ int esp32s3_lan9250_initialize(int port);
int esp32s3_lan9250_uninitialize(int port);
#endif

#ifdef CONFIG_ESP32S3_OPENETH
int esp_openeth_initialize(void);
#endif

#endif /* __ASSEMBLY__ */
#endif /* __BOARDS_XTENSA_ESP32S3_ESP32S3_DEVKIT_SRC_ESP32S3_DEVKIT_H */
8 changes: 8 additions & 0 deletions boards/xtensa/esp32s3/esp32s3-devkit/src/esp32s3_bringup.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,14 @@ int esp32s3_bringup(void)

#endif

#ifdef CONFIG_ESP32S3_OPENETH
ret = esp_openeth_initialize();
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: Failed to initialize Open ETH ethernet.\n");
}
#endif

#ifdef CONFIG_DEV_GPIO
ret = esp32s3_gpio_init();
if (ret < 0)
Expand Down
2 changes: 1 addition & 1 deletion tools/ci/testlist/xtensa-01.dat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/xtensa/esp32

# We do not set ESPTOOL_BINDIR in this build
-esp32-devkitc:qemu-openeth
-esp32-devkitc:qemu_openeth
Loading
Loading