The newest ESP32 can run Linux and it's getting close to a Raspberry Pi

The newest ESP32 can run Linux and it’s getting close to a Raspberry Pi

最新的 ESP32 可以运行 Linux,且性能正逐渐逼近树莓派

Espressif has a new ESP32, the ESP32-S31, and that ESP32 comes with a gigabit Ethernet MAC, a USB 2.0 High-Speed host controller, an SD card interface with two slots, and a camera input. It’s a pretty standard set of features for a Linux single-board computer, but a rather strange list of things to find on a microcontroller. 乐鑫(Espressif)推出了一款全新的 ESP32 芯片——ESP32-S31。该芯片配备了千兆以太网 MAC、USB 2.0 高速主机控制器、双槽 SD 卡接口以及摄像头输入接口。对于 Linux 单板计算机(SBC)来说,这些功能相当标准,但出现在微控制器(MCU)上却显得有些不同寻常。

I’ve spent a lot of time with microcontrollers, and it’s typical to find a couple of cores, a small amount of RAM, GPIO pins, and maybe Wi-Fi on one. Something more advanced typically uses a Raspberry Pi, yet the ESP32-S31 supports a whole lot more than your typical microcontroller setup. Espressif has also made a big change to the CPU that makes it more like an SBC than ever. 我与微控制器打交道多年,通常它们只具备几个核心、少量的内存、GPIO 引脚,或许还有 Wi-Fi。更高级的应用通常会选择树莓派,但 ESP32-S31 的支持能力远超典型的微控制器配置。乐鑫对 CPU 进行了重大改进,使其比以往任何时候都更像一台单板计算机。

The biggest news about the ESP32-S31 is its networking capabilities, packing a 1000 Mbps Ethernet MAC and a big jump over the slower 100 Mbps found on the ESP32-P4. It uses an 8P8C connector (commonly referred to as RJ45) and connects to an external PHY that utilizes the chip’s RGMII interface. RGMII is specifically what enables gigabit; otherwise, it would use standard RMII. 关于 ESP32-S31 最重磅的消息是其网络能力:它内置了 1000 Mbps 的以太网 MAC,相比 ESP32-P4 的 100 Mbps 有了巨大飞跃。它使用 8P8C 连接器(通常称为 RJ45),并通过芯片的 RGMII 接口连接到外部 PHY。RGMII 正是实现千兆速度的关键,否则它只能使用标准的 RMII。

On top of that, then, there’s USB, but it’s not “standard” USB. The S31 official developer board has a USB 2.0 High-Speed OTG controller, and it’s a full-size Type-A socket that can supply 500 mA in host OTG mode. Alongside that sits an SDIO host controller with two slots, a DVP camera interface taking 8 to 16 bits, and a parallel LCD controller that supports up to 24-bit RGB as well as I8080 and MOTO6800 panels. 除此之外,它还具备 USB 功能,但并非“标准”USB。S31 官方开发板配备了 USB 2.0 高速 OTG 控制器,并提供了一个全尺寸 Type-A 插座,在主机 OTG 模式下可提供 500 mA 电流。此外,它还拥有一个双槽 SDIO 主机控制器、一个 8 到 16 位的 DVP 摄像头接口,以及一个支持高达 24 位 RGB、I8080 和 MOTO6800 面板的并行 LCD 控制器。

There are two I2S controllers with hardware Bluetooth audio sync, fourteen capacitive touch channels, a CAN FD controller, and four MCPWM units. Just from that list alone, it’s clear that this is a new type of ESP32, and not one that we’ve really seen before. Wired networking? A USB host? Camera, display, audio? Everything here is more akin to an SBC than a microcontroller, but that’s still not all. 它还拥有两个支持硬件蓝牙音频同步的 I2S 控制器、十四个电容式触摸通道、一个 CAN FD 控制器和四个 MCPWM 单元。仅从这份清单就能看出,这是一种我们从未见过的全新 ESP32。有线网络?USB 主机?摄像头、显示屏、音频?这里的一切都更像单板计算机而非微控制器,但这还不是全部。

On the CPU side, there are two 32-bit RISC-V cores running at up to 320 MHz and a floating point unit on each, a 128-bit SIMD data path for one of them, and a separate 40 MHz low-power core, too. Each core gets 32KB of L1 instruction cache and they share 64KB of data cache. Memory gets even more interesting: the S31 supports up to 64 MB of PSRAM over a 250 MHz 8-bit DDR interface, and supports up to 256MB of flash, too. On the ESP32-S3 the PSRAM interface was shared with flash, making this a rather big step-up. 在 CPU 方面,它拥有两个主频高达 320 MHz 的 32 位 RISC-V 核心,每个核心都配有浮点运算单元,其中一个核心还具备 128 位 SIMD 数据通路,此外还有一个独立的 40 MHz 低功耗核心。每个核心拥有 32KB 的 L1 指令缓存,并共享 64KB 的数据缓存。内存配置更为有趣:S31 通过 250 MHz 的 8 位 DDR 接口支持高达 64 MB 的 PSRAM,并支持最高 256MB 的闪存。在 ESP32-S3 上,PSRAM 接口与闪存是共享的,因此这算是一个相当大的进步。

Espressif has been using the term “MMU” for quite a long time, including on the ESP32-S3 and the ESP32-P4. However, the MMU in that case was a block that maps external flash and PSRAM to address space, nowhere near the complexity of a modern MMU that can also do things like process isolation and page tables. As a result, when Espressif said the S31 has MMU support, it appeared to be more of the same. 乐鑫长期以来一直使用“MMU”这一术语,包括在 ESP32-S3 和 ESP32-P4 上。然而,之前的 MMU 只是一个将外部闪存和 PSRAM 映射到地址空间的模块,其复杂程度远不及能够处理进程隔离和页表的现代 MMU。因此,当乐鑫宣称 S31 具备 MMU 支持时,起初人们以为这只是老调重弹。

However, there’s a big difference this time around. In the S31’s datasheet, Espressif says that the cores do “Sv32 two-level page-table address translation” and implement “Machine, Supervisor and User privilege modes.” Sv32 is the actual RISC-V paging scheme, and having all three privilege modes is what enables a regular kernel to run in supervisor mode with firmware underneath it in machine mode. And this is how RISC-V-based Linux machines work. 然而,这次有很大的不同。在 S31 的数据手册中,乐鑫表示其核心支持“Sv32 两级页表地址转换”,并实现了“机器(Machine)、监督(Supervisor)和用户(User)特权模式”。Sv32 是真正的 RISC-V 分页方案,而拥有这三种特权模式,正是让常规内核在监督模式下运行、同时底层固件在机器模式下运行的前提。这正是基于 RISC-V 的 Linux 机器的工作方式。

Unlike previous hacky attempts at getting Linux running on past ESP32 microcontrollers, the S31 has a lot more out of the box to natively support it. For starters, Espressif already published a Linux BSP back in August, built on Buildroot and U-Boot, and it includes a kernel, device tree, and root file system. Espressif says that it’s “not yet recommended for production use,” but the point is that it works. 与以往在旧款 ESP32 微控制器上运行 Linux 的“黑客式”尝试不同,S31 开箱即具备更多原生支持。首先,乐鑫已于 8 月发布了基于 Buildroot 和 U-Boot 的 Linux BSP,其中包含了内核、设备树和根文件系统。虽然乐鑫表示“尚不建议用于生产环境”,但重点是它确实可以运行。

That also hasn’t stopped the community from iterating fast. One port sees Linux 6.18 running in supervisor mode, complete with working drivers for practically all of the hardware. There’s another, based on Linux 7.1, that supports an 800x480 LCD panel connected, capable of showing a console. Being able to plug in a display, use wired Ethernet, and use a mouse and keyboard sound a lot more akin to an SBC than a microcontroller. 这并没有阻止社区的快速迭代。一个移植版本已能在监督模式下运行 Linux 6.18,且几乎所有硬件驱动程序都能正常工作。另一个基于 Linux 7.1 的版本则支持连接 800x480 的 LCD 面板,并能显示控制台。能够外接显示器、使用有线以太网以及鼠标键盘,这听起来确实更像是一台单板计算机,而非微控制器。

The S31 starts to lag behind when looking at the RAM it carried: there’s 512KB of SRAM on chip, and two variants are available with either 16MB or 32MB of in-package PSRAM. The maximum memory it can carry is 64MB, and there’s no DRAM controller anywhere to be found. Compared to the likes of the Milk-V Duo S with its 512MB of RAM, you’re reminded by the competition very quickly that the ESP32-S31 is a microcontroller. 在内存方面,S31 开始显露劣势:芯片内置 512KB SRAM,并提供 16MB 或 32MB 封装内 PSRAM 两种版本。其最大内存容量仅为 64MB,且没有任何 DRAM 控制器。与拥有 512MB 内存的 Milk-V Duo S 等产品相比,竞争对手很快就会提醒你:ESP32-S31 终究还是一款微控制器。

This is exactly why those Linux builds execute the kernel from flash, in order to avoid spending precious PSRAM on it. As well, there’s no GPU or NPU on-board, and while the ESP32-P4 has video encode and MIPI interfaces, those are traded out for additional radios on the S31. You still get a JPEG codec, a 2D pixel-processing accelerator, and 2D DMA, but that’s not really enough to show a full Linux desktop. 这正是为什么那些 Linux 构建版本选择从闪存执行内核,以避免占用宝贵的 PSRAM。此外,板载没有 GPU 或 NPU;虽然 ESP32-P4 具备视频编码和 MIPI 接口,但 S31 为了增加无线电功能而舍弃了这些。你仍然可以使用 JPEG 编解码器、2D 像素处理加速器和 2D DMA,但这不足以支撑完整的 Linux 桌面环境。