✨ ESP32-S2 and ESP32-S3 support (arduino-esp32 2.0.7, ESP-IDF 4.4) (#28781)

* ✨ ESP32-S3 support (arduino-esp32 2.0.7, ESP-IDF 4.4)

Add BOARD_ESPRESSIF_ESP32S3 and env:esp32s3_idf4: timers, PWM, servos, ADC1 and the I2S stepper stream on the S3, native USB serial as -1 with the emergency parser, M997 restart, and S3-safe M43 pin handling.

The WiFi serial port is now -3 on every ESP32 and can be used in any serial port slot. WiFi modem sleep is turned off to keep WebSocket replies from stalling.

* ✨ ESP32-S2 support

Add the ESP32-S2 to the non-classic ESP32 paths used by the S3.

- Native USB (SERIAL_PORT -1) is the USB OTG port through TinyUSB (USBCDC). Output longer than its 64-byte TX FIFO is sent in pieces.
- Two UARTs: serial port 2 is a sanity-check error.
- ADC1 at the chip's full 13-bit width.
- PWM on GPIO 0-45 (46 is input only); M43 skips the flash, PSRAM, native USB and UART0 pins.
- On a single-core build the IDF doesn't set the spinlock owner, so hal.isr_on() never re-enabled interrupts. Count isr_off() instead.

New board BOARD_ESPRESSIF_ESP32S2, env esp32s2_idf4 and CI tests.
This commit is contained in:
ellensp 2026-11-23 14:38:10 +13:00 • committed by GitHub
parent 3f1bbe1a7d
commit 6ed05030df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
35 changed files with 960 additions and 144 deletions

View file

@ -86,6 +86,8 @@ jobs:
- esp32_idf4
- mks_tinybee
- mks_dlc32_v2_1
- esp32s2_idf4
- esp32s3_idf4
# Teensy 2
#- at90usb1286_cdc

View file

@ -76,11 +76,12 @@
/**
* Select the serial port on the board to use for communication with the host.
* This allows the connection of wireless adapters (for instance) to non-default port pins.
* Serial port -1 is the USB emulated serial port, if available.
* Serial port -1 is the USB emulated serial port, if available (ESP32-S2/S3: the native USB port).
* Serial port -2 is the built-in Bluetooth (Classic SPP) on ESP32, not together with WiFi.
* ESP32: Serial port -3 is the WiFi serial port (WIFISUPPORT or ESP3D_WIFISUPPORT).
* Note: The first serial port (-1 or 0) will always be used by the Arduino bootloader.
*
* :[-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
* :[-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
*/
#define SERIAL_PORT 0
@ -102,8 +103,8 @@
/**
* Select a secondary serial port on the board to use for communication with the host.
* -2 is Ethernet on Teensy 4.1 boards, and the built-in Bluetooth (Classic SPP) on ESP32.
* ESP3D_WIFISUPPORT requires -1, which carries G-code from ESP3D to Marlin.
* :[-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
* ESP3D_WIFISUPPORT requires a serial port set to -3, which carries G-code from ESP3D to Marlin.
* :[-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
*/
//#define SERIAL_PORT_2 -1
//#define BAUDRATE_2 250000 // :[2400, 9600, 19200, 38400, 57600, 115200, 250000, 500000, 1000000] Enable to override BAUDRATE
@ -111,8 +112,8 @@
/**
* Select a third serial port on the board to use for communication with the host.
* Currently supported for AVR, DUE, SAMD51, LPC1768/9, STM32/STM32F1/HC32, and Teensy 4.x,
* and ESP32 for Bluetooth (-2).
* :[-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
* and ESP32 (-2 Bluetooth, -3 the WiFi serial port; ESP32-S2/S3: -1, the native USB port).
* :[-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
*/
//#define SERIAL_PORT_3 1
//#define BAUDRATE_3 250000 // :[2400, 9600, 19200, 38400, 57600, 115200, 250000, 500000, 1000000] Enable to override BAUDRATE

View file

@ -33,59 +33,4 @@ MSerialT flushableSerial(ENABLED(EMERGENCY_PARSER), 0);
MSerialT flushableSerial2(false, 2); // Not a host port
#endif
#if ENABLED(EMERGENCY_PARSER)
#include "../../feature/e_parser.h"
#define RX_NEXT(i) (((i) + 1) & (RX_BUFFER_SIZE - 1))
void FlushableHardwareSerial::rx_start() {
MSerialT * const s = static_cast<MSerialT*>(this);
if (!s->emergency_parser_enabled()) return;
rx_head = rx_tail = 0;
s->emergency_state = EmergencyParser::State::EP_RESET;
rx_active = true;
// Higher priority than the Marlin loop, so it runs like a receive interrupt does on other HALs
if (!rx_task) xTaskCreatePinnedToCore(rx_task_loop, "SerialRX", 2048, this, 2, &rx_task, CONFIG_ARDUINO_RUNNING_CORE);
}
// Leave bytes in the core's buffer when rx_buffer is full
void FlushableHardwareSerial::rx_poll() {
while (rx_active && RX_NEXT(rx_head) != rx_tail && HardwareSerial::available() > 0) {
const int c = HardwareSerial::read();
if (c < 0) break;
emergency_parser.update(static_cast<MSerialT*>(this)->emergency_state, uint8_t(c));
rx_buffer[rx_head] = uint8_t(c);
rx_head = RX_NEXT(rx_head);
}
}
void FlushableHardwareSerial::rx_task_loop(void *arg) {
FlushableHardwareSerial * const s = static_cast<FlushableHardwareSerial*>(arg);
for (;;) {
s->rx_poll();
vTaskDelay(1);
}
}
int FlushableHardwareSerial::available() {
if (!rx_task) return HardwareSerial::available();
return (rx_head - rx_tail) & (RX_BUFFER_SIZE - 1);
}
int FlushableHardwareSerial::peek() {
if (!rx_task) return HardwareSerial::peek();
return rx_head == rx_tail ? -1 : rx_buffer[rx_tail];
}
int FlushableHardwareSerial::read() {
if (!rx_task) return HardwareSerial::read();
if (rx_head == rx_tail) return -1;
const uint8_t c = rx_buffer[rx_tail];
rx_tail = RX_NEXT(rx_tail);
return c;
}
#endif // EMERGENCY_PARSER
#endif

View file

@ -26,30 +26,17 @@
#include "../shared/Marduino.h"
#include "../../core/serial_hook.h"
class FlushableHardwareSerial : public HardwareSerial {
#if ENABLED(EMERGENCY_PARSER)
#include "SerialRxTask.h"
class FlushableHardwareSerial;
typedef SerialRxTask<HardwareSerial, FlushableHardwareSerial> FlushableBaseSerial;
#else
typedef HardwareSerial FlushableBaseSerial;
#endif
class FlushableHardwareSerial : public FlushableBaseSerial {
public:
FlushableHardwareSerial(int uart_nr) : HardwareSerial(uart_nr) {}
#if ENABLED(EMERGENCY_PARSER)
// The core gives no access to received bytes from its UART interrupt, so a task
// on Marlin's core moves them into rx_buffer, passing each to the emergency parser.
template <typename... Args>
void begin(Args... args) { rx_active = false; HardwareSerial::begin(args...); rx_start(); }
template <typename... Args>
void end(Args... args) { rx_active = false; HardwareSerial::end(args...); }
int available();
int peek();
int read();
private:
uint8_t rx_buffer[RX_BUFFER_SIZE];
volatile uint16_t rx_head = 0, rx_tail = 0;
volatile bool rx_active = false;
TaskHandle_t rx_task = nullptr;
void rx_start();
void rx_poll();
static void rx_task_loop(void *arg);
#endif
FlushableHardwareSerial(int uart_nr) : FlushableBaseSerial(uart_nr) {}
};
extern Serial1Class<FlushableHardwareSerial> flushableSerial;

View file

@ -53,6 +53,9 @@
// ------------------------
portMUX_TYPE MarlinHAL::spinlock = portMUX_INITIALIZER_UNLOCKED;
#if CONFIG_FREERTOS_UNICORE
volatile uint8_t MarlinHAL::isr_off_count = 0;
#endif
// ------------------------
// Local defines
@ -75,6 +78,14 @@ esp_adc_cal_characteristics_t characteristics[ADC_ATTEN_MAX];
adc_atten_t attenuations[ADC1_CHANNEL_MAX] = {};
uint32_t thresholds[ADC_ATTEN_MAX];
#if ESP32_CLASSIC
#define ADC1_WIDTH ADC_WIDTH_BIT_12
#define ADC1_RAW_MAX 4095
#else
#define ADC1_WIDTH adc_bits_width_t(ADC_WIDTH_BIT_DEFAULT) // 13-bit on the ESP32-S2, 12-bit on the ESP32-S3
#define ADC1_RAW_MAX ((1 << SOC_ADC_MAX_BITWIDTH) - 1)
#endif
volatile int numPWMUsed = 0;
volatile struct { pin_t pin; int value; } pwmState[MAX_PWM_PINS];
@ -123,6 +134,17 @@ struct {
#endif
#if HAS_NATIVE_USB_SERIAL && CONFIG_IDF_TARGET_ESP32S3
// On the ESP32-S3 the ESP-IDF console writes to the native USB port as well as UART0.
// Keep ESP-IDF log lines off the native USB port: a host reads them as G-code (see NativeUSBSerial.h).
static int log_to_uart0(const char *fmt, va_list args) {
char buf[160];
const int len = vsnprintf(buf, sizeof(buf), fmt, args);
if (len > 0) flushableSerial.write((const uint8_t*)buf, _MIN(len, int(sizeof(buf) - 1)));
return len;
}
#endif
#if HAS_TMC_HW_SERIAL
// ESP32 uses a GPIO matrix that allows pins to be assigned to hardware serial ports.
@ -141,6 +163,9 @@ struct {
#endif
void MarlinHAL::init_board() {
#if HAS_NATIVE_USB_SERIAL && CONFIG_IDF_TARGET_ESP32S3
esp_log_set_vprintf(log_to_uart0);
#endif
#if ENABLED(USE_ESP32_TASK_WDT)
esp_task_wdt_init(10, true);
#endif
@ -191,6 +216,9 @@ uint8_t MarlinHAL::get_reset_source() {
void MarlinHAL::reboot() { ESP.restart(); }
// Restart, as there's no in-application firmware update
void flashFirmware(const int16_t) { hal.reboot(); }
// return free memory between end of heap (or end bss) and whatever is current
int MarlinHAL::freeMemory() { return ESP.getFreeHeap(); }
@ -224,6 +252,14 @@ int MarlinHAL::freeMemory() { return ESP.getFreeHeap(); }
// https://docs.espressif.com/projects/esp-idf/en/release-v4.4/esp32/api-reference/peripherals/adc.html
adc1_channel_t get_channel(int pin) {
#if !ESP32_CLASSIC
// Other ESP32 variants map ADC1 to different pins
for (uint8_t c = 0; c < ADC1_CHANNEL_MAX; ++c) {
gpio_num_t io;
if (adc1_pad_get_io_num(adc1_channel_t(c), &io) == ESP_OK && io == pin) return adc1_channel_t(c);
}
return ADC1_CHANNEL_MAX;
#endif
switch (pin) {
case 39: return ADC1_CHANNEL_3;
case 36: return ADC1_CHANNEL_0;
@ -246,7 +282,7 @@ void adc1_set_attenuation(adc1_channel_t chan, adc_atten_t atten) {
void MarlinHAL::adc_init() {
// Configure ADC
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_width(ADC1_WIDTH);
// Configure channels only if used as (re-)configuring a pin for ADC that is used elsewhere might have adverse effects
TERN_(HAS_TEMP_ADC_0, adc1_set_attenuation(get_channel(TEMP_0_PIN), ADC_ATTEN_11db));
@ -270,10 +306,10 @@ void MarlinHAL::adc_init() {
// Calculate ADC characteristics (i.e., gain and offset factors for each attenuation level)
for (int i = 0; i < ADC_ATTEN_MAX; i++) {
esp_adc_cal_characterize(ADC_UNIT_1, (adc_atten_t)i, ADC_WIDTH_BIT_12, V_REF, &characteristics[i]);
esp_adc_cal_characterize(ADC_UNIT_1, (adc_atten_t)i, ADC1_WIDTH, V_REF, &characteristics[i]);
// Change attenuation 100mV below the calibrated threshold
thresholds[i] = esp_adc_cal_raw_to_voltage(4095, &characteristics[i]);
thresholds[i] = esp_adc_cal_raw_to_voltage(ADC1_RAW_MAX, &characteristics[i]);
}
}

View file

@ -54,28 +54,46 @@
#include "BluetoothSerialPort.h"
#endif
#if SERIAL_PORT == -2
#if HAS_NATIVE_USB_SERIAL
#include "NativeUSBSerial.h"
#endif
// The WiFi serial port: G-code from ESP3D, or from the WebSocket at /ws
#if ENABLED(ESP3D_WIFISUPPORT)
typedef ForwardSerial1Class< decltype(Serial2Socket) > DefaultSerial1;
extern DefaultSerial1 MSerial0;
#define WIFI_HOST_SERIAL MSerial0
#elif ENABLED(WIFISUPPORT)
#define WIFI_HOST_SERIAL webSocketSerial
#endif
// Serial port -1 is the native USB port (ESP32-S2, ESP32-S3), -2 is Bluetooth (ESP32), -3 is the WiFi serial port
#if SERIAL_PORT == -3
#define MYSERIAL1 WIFI_HOST_SERIAL
#elif SERIAL_PORT == -2
#define MYSERIAL1 btSerial
#elif SERIAL_PORT == -1
#define MYSERIAL1 nativeUSBSerial
#else
#define MYSERIAL1 flushableSerial
#endif
#if SERIAL_PORT_2 == -2
#if SERIAL_PORT_2 == -3
#define MYSERIAL2 WIFI_HOST_SERIAL
#elif SERIAL_PORT_2 == -2
#define MYSERIAL2 btSerial
#elif ENABLED(ESP3D_WIFISUPPORT)
typedef ForwardSerial1Class< decltype(Serial2Socket) > DefaultSerial1;
extern DefaultSerial1 MSerial0;
#define MYSERIAL2 MSerial0
#elif ENABLED(WIFISUPPORT)
#define MYSERIAL2 webSocketSerial
#endif
#if SERIAL_PORT_2 == 2
#elif SERIAL_PORT_2 == -1
#define MYSERIAL2 nativeUSBSerial
#elif SERIAL_PORT_2 == 2
#define MYSERIAL2 flushableSerial2
#endif
#if SERIAL_PORT_3 == -2
#if SERIAL_PORT_3 == -3
#define MYSERIAL3 WIFI_HOST_SERIAL
#elif SERIAL_PORT_3 == -2
#define MYSERIAL3 btSerial
#elif SERIAL_PORT_3 == -1
#define MYSERIAL3 nativeUSBSerial
#endif
#if LCD_SERIAL_PORT == 2
@ -89,8 +107,18 @@
#define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
#define PWM_FREQUENCY 1000U // Default PWM frequency when set_pwm_duty() is called without set_pwm_frequency()
#define PWM_RESOLUTION 10U // Default PWM bit resolution
#define CHANNEL_MAX_NUM 15U // max PWM channel # to allocate (7 to only use low speed, 15 to use low & high)
#define MAX_PWM_IOPIN 33U // hardware pwm pins < 34
#if ESP32_CLASSIC
#define CHANNEL_MAX_NUM 15U // max PWM channel # to allocate (7 to only use low speed, 15 to use low & high)
#define MAX_PWM_IOPIN 33U // hardware pwm pins < 34
#else
#include <soc/soc_caps.h>
#define CHANNEL_MAX_NUM (SOC_LEDC_CHANNEL_NUM - 1U) // max PWM channel # to allocate
#if CONFIG_IDF_TARGET_ESP32S2
#define MAX_PWM_IOPIN 45U // GPIO 46 is input only
#else
#define MAX_PWM_IOPIN (SOC_GPIO_PIN_COUNT - 1U) // all GPIOs can output PWM
#endif
#endif
#ifndef MAX_EXPANDER_BITS
#define MAX_EXPANDER_BITS 32 // I2S expander bit width (max 32)
#endif
@ -126,6 +154,12 @@ void tmc_hw_serial_begin(HardwareSerial &serial, const uint32_t baud);
void analogWrite(const pin_t pin, const uint16_t value, const uint32_t freq=PWM_FREQUENCY, const uint16_t res=8);
// M997 restarts the firmware
#ifndef PLATFORM_M997_SUPPORT
#define PLATFORM_M997_SUPPORT
#endif
void flashFirmware(const int16_t);
//
// Pin Mapping for M42, M43, M226
//
@ -214,9 +248,17 @@ public:
// Interrupts
static portMUX_TYPE spinlock;
static bool isr_state() { return spinlock.owner == portMUX_FREE_VAL; }
static void isr_on() { if (spinlock.owner != portMUX_FREE_VAL) portEXIT_CRITICAL(&spinlock); }
static void isr_off() { portENTER_CRITICAL(&spinlock); }
#if CONFIG_FREERTOS_UNICORE
// A single-core build doesn't set the spinlock owner, so count the isr_off() calls
static volatile uint8_t isr_off_count;
static bool isr_state() { return !isr_off_count; }
static void isr_on() { if (isr_off_count) { isr_off_count--; portEXIT_CRITICAL(&spinlock); } }
static void isr_off() { portENTER_CRITICAL(&spinlock); isr_off_count++; }
#else
static bool isr_state() { return spinlock.owner == portMUX_FREE_VAL; }
static void isr_on() { if (spinlock.owner != portMUX_FREE_VAL) portEXIT_CRITICAL(&spinlock); }
static void isr_off() { portENTER_CRITICAL(&spinlock); }
#endif
static void delay_ms(const int ms) { delay(ms); }

View file

@ -0,0 +1,33 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2026 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifdef ARDUINO_ARCH_ESP32
#include "../../inc/MarlinConfig.h"
#if HAS_NATIVE_USB_SERIAL
#include "NativeUSBSerial.h"
Serial1Class<NativeUSBSerial> nativeUSBSerial(ENABLED(EMERGENCY_PARSER));
#endif
#endif // ARDUINO_ARCH_ESP32

View file

@ -0,0 +1,134 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2026 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#pragma once
#include "../shared/Marduino.h"
#include "../../core/serial_hook.h"
#if CONFIG_IDF_TARGET_ESP32S3
#include <HWCDC.h>
typedef HWCDC NativeUSBPort;
#else
#include <USB.h>
typedef USBCDC NativeUSBPort;
#endif
#if ENABLED(EMERGENCY_PARSER)
#include "SerialRxTask.h"
class NativeUSBSerial;
typedef SerialRxTask<NativeUSBPort, NativeUSBSerial> NativeUSBBaseSerial;
#else
typedef NativeUSBPort NativeUSBBaseSerial;
#endif
#if CONFIG_IDF_TARGET_ESP32S3
/**
* Native USB CDC serial port (USB Serial/JTAG) of the ESP32-S3
*/
class NativeUSBSerial : public NativeUSBBaseSerial {
public:
void begin(const long baud) {
setTxBufferSize(4096);
NativeUSBBaseSerial::begin(baud);
// A USB bus reset starts a new enumeration, so wait for the host to send again
onEvent(ARDUINO_HW_CDC_BUS_RESET_EVENT, [](void *arg, esp_event_base_t, int32_t, void*) {
static_cast<NativeUSBSerial*>(arg)->host_sent = false;
});
}
int available() override {
const int n = NativeUSBBaseSerial::available();
if (n) host_sent = true;
return n;
}
int read() override { host_sent = true; return NativeUSBBaseSerial::read(); }
// The port gives no DTR, so there's no way to know whether a program has it open.
// Output written while it's closed is held until a program opens it, and Linux echoes
// it back before the program has set the port to raw, so Marlin would read its own
// output (e.g., the boot messages) as G-code. So drop output until the host has sent
// something: printer hosts send a command as soon as they open the port.
//
// HWCDC::write waits for buffer space while the host isn't reading, so don't let it.
// Allow 1ms for a host that is reading to make room, then drop the output, and
// keep dropping without waiting until there is room again.
using HWCDC::write;
size_t write(const uint8_t *buffer, size_t size) override {
if (!host_sent) return size;
if (availableForWrite() < int(size)) {
if (!dropping) delay(1);
if (availableForWrite() < int(size)) { dropping = true; return size; }
}
dropping = false;
return HWCDC::write(buffer, size);
}
bool connected() { return true; } // Don't hold up startup waiting for a host
private:
volatile bool host_sent = false;
bool dropping = false;
};
#else
/**
* Native USB CDC serial port (USB OTG, TinyUSB) of the ESP32-S2
*/
class NativeUSBSerial : public NativeUSBBaseSerial {
public:
void begin(const long baud) {
NativeUSBBaseSerial::begin(baud);
USB.begin();
}
// USBCDC::write drops output while no program has the port open (DTR off),
// but waits for buffer space while one has it open and isn't reading, so
// don't let it. Allow 1ms for a host that is reading to make room, then drop
// the output, and keep dropping without waiting until there is room again.
// The TX FIFO is only 64 bytes, so send longer output in pieces that fit.
using USBCDC::write;
size_t write(const uint8_t c) override { return write(&c, 1); }
size_t write(const uint8_t *buffer, size_t size) override {
if (!*this) return size;
for (size_t sent = 0; sent < size;) {
int room = availableForWrite();
if (room <= 0 && !dropping) { delay(1); room = availableForWrite(); }
if (room <= 0) { dropping = true; break; }
dropping = false;
const size_t n = _MIN(size - sent, size_t(room));
USBCDC::write(buffer + sent, n);
sent += n;
}
return size;
}
bool connected() { return true; } // Don't hold up startup waiting for a host
private:
bool dropping = false;
};
#endif
extern Serial1Class<NativeUSBSerial> nativeUSBSerial;

View file

@ -0,0 +1,99 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2026 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#pragma once
#include "../../core/serial_hook.h"
/**
* For EMERGENCY_PARSER on a port from the Arduino-ESP32 core. The core gives no access
* to received bytes from its interrupt, so a task on Marlin's core moves them into
* rx_buffer, passing each to the emergency parser. Marlin reads from rx_buffer.
*
* PortT is the core's serial class. FinalT is the class derived from this one, which
* is wrapped in a Serial1Class holding the parser state.
*/
template <class PortT, class FinalT>
class SerialRxTask : public PortT {
public:
using PortT::PortT;
template <typename... Args>
void begin(Args... args) { rx_active = false; PortT::begin(args...); rx_start(); }
template <typename... Args>
void end(Args... args) { rx_active = false; PortT::end(args...); }
int available() {
if (!rx_task) return PortT::available();
return (rx_head - rx_tail) & (RX_BUFFER_SIZE - 1);
}
int peek() {
if (!rx_task) return PortT::peek();
return rx_head == rx_tail ? -1 : rx_buffer[rx_tail];
}
int read() {
if (!rx_task) return PortT::read();
if (rx_head == rx_tail) return -1;
const uint8_t c = rx_buffer[rx_tail];
rx_tail = rx_next(rx_tail);
return c;
}
private:
uint8_t rx_buffer[RX_BUFFER_SIZE];
volatile uint16_t rx_head = 0, rx_tail = 0;
volatile bool rx_active = false;
TaskHandle_t rx_task = nullptr;
static uint16_t rx_next(const uint16_t i) { return (i + 1) & (RX_BUFFER_SIZE - 1); }
Serial1Class<FinalT>* wrapper() { return static_cast<Serial1Class<FinalT>*>(static_cast<FinalT*>(this)); }
void rx_start() {
if (!wrapper()->emergency_parser_enabled()) return;
rx_head = rx_tail = 0;
wrapper()->emergency_state = EmergencyParser::State::EP_RESET;
rx_active = true;
// Higher priority than the Marlin loop, so it runs like a receive interrupt does on other HALs
if (!rx_task) xTaskCreatePinnedToCore(rx_task_loop, "SerialRX", 2048, this, 2, &rx_task, CONFIG_ARDUINO_RUNNING_CORE);
}
// Leave bytes in the core's buffer when rx_buffer is full
void rx_poll() {
while (rx_active && rx_next(rx_head) != rx_tail && PortT::available() > 0) {
const int c = PortT::read();
if (c < 0) break;
emergency_parser.update(wrapper()->emergency_state, uint8_t(c));
rx_buffer[rx_head] = uint8_t(c);
rx_head = rx_next(rx_head);
}
}
static void rx_task_loop(void *arg) {
SerialRxTask * const s = static_cast<SerialRxTask*>(arg);
for (;;) {
s->rx_poll();
vTaskDelay(1);
}
}
};

View file

@ -35,7 +35,7 @@ Servo::Servo() {}
int8_t Servo::attach(const int inPin) {
if (inPin > 0) pin = inPin;
channel = get_pwm_channel(pin, 50U, 16U);
channel = get_pwm_channel(pin, 50U, SERVO_PWM_BITS);
return channel; // -1 if no PWM avail.
}

View file

@ -23,6 +23,13 @@
#include <Arduino.h>
#if ESP32_CLASSIC
#define SERVO_PWM_BITS 16
#else
#include <soc/soc_caps.h>
#define SERVO_PWM_BITS SOC_LEDC_TIMER_BIT_WIDE_NUM // 14 on the ESP32-S2 and ESP32-S3
#endif
class Servo {
static const int MIN_ANGLE = 0,
MAX_ANGLE = 180,
@ -30,7 +37,7 @@ class Servo {
MAX_PULSE_WIDTH = 2400, // Longest pulse sent to a servo
TAU_MSEC = 20,
TAU_USEC = (TAU_MSEC * 1000),
MAX_COMPARE = _BV(16) - 1; // 65535
MAX_COMPARE = _BV(SERVO_PWM_BITS) - 1; // 65535 on the original ESP32
public:
Servo();

View file

@ -86,7 +86,11 @@
#define extDigitalWrite(IO,V) digitalWrite(IO,V)
// PWM outputs
#define PWM_PIN(P) (P < 34 || P > 127) // NOTE Pins >= 34 are input only on ESP32, so they can't be used for output.
#if ESP32_CLASSIC
#define PWM_PIN(P) (P < 34 || P > 127) // NOTE Pins >= 34 are input only on ESP32, so they can't be used for output.
#else
#define PWM_PIN(P) (P <= MAX_PWM_IOPIN || P > 127)
#endif
// Toggle pin value
#define TOGGLE(IO) WRITE(IO, !READ(IO))

View file

@ -28,9 +28,16 @@
#include "i2s.h"
#include "../shared/Marduino.h"
#include <driver/periph_ctrl.h>
#include <rom/lldesc.h>
#include <soc/i2s_struct.h>
#if ESP32_CLASSIC
#include <driver/periph_ctrl.h>
#include <rom/lldesc.h>
#include <soc/i2s_struct.h>
#else
// Other ESP32 variants send the stream through the ESP-IDF I2S driver
#include <driver/i2s.h>
#include <rom/gpio.h>
#include <soc/gpio_sig_map.h>
#endif
#include <freertos/queue.h>
#include "../../module/stepper.h"
@ -39,6 +46,14 @@
#define I2S_SAMPLE_SIZE 4 // 4 bytes, 32 bits per sample
#define DMA_SAMPLE_COUNT DMA_BUF_LEN / I2S_SAMPLE_SIZE // number of samples per buffer
// output value
uint32_t i2s_port_data = 0;
// Samples pushed so far, for timing the stepper events
static uint32_t sample_count = 0;
#if ESP32_CLASSIC
typedef enum {
I2S_NUM_0 = 0x0, /*!< I2S 0*/
I2S_NUM_1 = 0x1, /*!< I2S 1*/
@ -57,12 +72,6 @@ static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX] = {portMUX_INITIALIZER_UNLOCKED, p
static i2s_dev_t* I2S[I2S_NUM_MAX] = {&I2S0, &I2S1};
static i2s_dma_t dma;
// output value
uint32_t i2s_port_data = 0;
// Samples pushed so far, for timing the stepper events
static uint32_t sample_count = 0;
#define I2S_ENTER_CRITICAL() portENTER_CRITICAL(&i2s_spinlock[i2s_num])
#define I2S_EXIT_CRITICAL() portEXIT_CRITICAL(&i2s_spinlock[i2s_num])
@ -141,6 +150,17 @@ static void IRAM_ATTR i2s_intr_handler_default(void *arg) {
I2S0.int_clr.val = I2S0.int_st.val;
}
#else // !ESP32_CLASSIC
static struct {
uint32_t *current;
uint32_t rw_pos;
} dma;
static uint32_t i2s_buffer[DMA_SAMPLE_COUNT];
#endif // !ESP32_CLASSIC
static void i2s_push_idle(hal_timer_t n);
/**
@ -159,6 +179,8 @@ void stepperTask(void *parameter) {
}
}
#if ESP32_CLASSIC
int i2s_init() {
periph_module_enable(PERIPH_I2S0_MODULE);
@ -319,6 +341,48 @@ int i2s_init() {
return i2s_start(I2S_NUM_0);
}
#elif ENABLED(I2S_STEPPER_STREAM) // !ESP32_CLASSIC
/**
* The same stream as the original ESP32: each 32-bit sample is one stereo frame
* of two 16-bit slots, high half first, at STEPPER_TIMER_RATE (250 kHz, BCK 8 MHz).
* WS is inverted so it rises at the start of each frame, latching the 74HC595s.
* MCLK = 128 * 250 kHz = 32 MHz divides the 160 MHz PLL exactly.
*/
int i2s_init() {
dma.current = i2s_buffer;
dma.rw_pos = 0;
i2s_config_t cfg = {};
cfg.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_TX);
cfg.sample_rate = STEPPER_TIMER_RATE;
cfg.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
cfg.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
cfg.communication_format = I2S_COMM_FORMAT_STAND_MSB;
cfg.dma_buf_count = DMA_BUF_COUNT;
cfg.dma_buf_len = DMA_SAMPLE_COUNT;
cfg.use_apll = false;
cfg.tx_desc_auto_clear = false;
cfg.mclk_multiple = I2S_MCLK_MULTIPLE_128;
if (i2s_driver_install(I2S_NUM_0, &cfg, 0, nullptr) != ESP_OK) return -1;
i2s_pin_config_t pins = {};
pins.mck_io_num = I2S_PIN_NO_CHANGE;
pins.bck_io_num = I2S_BCK;
pins.ws_io_num = I2S_WS;
pins.data_out_num = I2S_DATA;
pins.data_in_num = I2S_PIN_NO_CHANGE;
if (i2s_set_pin(I2S_NUM_0, &pins) != ESP_OK) return -1;
gpio_matrix_out(I2S_WS, I2S0O_WS_OUT_IDX, true, false); // Invert WS
// Create the task that will feed the buffer
xTaskCreatePinnedToCore(stepperTask, "StepperTask", 10000, nullptr, 1, nullptr, CONFIG_ARDUINO_RUNNING_CORE); // run I2S stepper task on same core as rest of Marlin
return 0;
}
#endif // !ESP32_CLASSIC
void i2s_write(uint8_t pin, uint8_t val) {
#if ENABLED(I2S_STEPPER_SPLIT_STREAM)
if (pin >= 16) {
@ -336,6 +400,8 @@ uint8_t i2s_state(uint8_t pin) {
return TEST(i2s_port_data, pin);
}
#if ESP32_CLASSIC
// Make sure the current DMA buffer has room, taking the next free one when it's full
static void i2s_buffer_ready() {
if (!dma.current || dma.rw_pos >= DMA_SAMPLE_COUNT) {
@ -344,6 +410,22 @@ static void i2s_buffer_ready() {
}
}
#else
// Send the buffer to the I2S driver when it's full. This blocks while all DMA buffers are full.
static void i2s_buffer_ready() {
if (dma.rw_pos < DMA_SAMPLE_COUNT) return;
// The driver sends the low 16 bits of a sample first, so swap the halves
for (uint32_t i = 0; i < DMA_SAMPLE_COUNT; ++i) i2s_buffer[i] = (i2s_buffer[i] << 16) | (i2s_buffer[i] >> 16);
size_t written;
i2s_write(I2S_NUM_0, i2s_buffer, sizeof(i2s_buffer), &written, portMAX_DELAY);
dma.rw_pos = 0;
}
#endif
void i2s_push_sample() {
i2s_buffer_ready();

View file

@ -21,6 +21,20 @@
*/
#pragma once
#if __has_include(<sdkconfig.h>)
#include <sdkconfig.h>
#endif
// The original ESP32. arduino-esp32 1.0.x (ESP-IDF 3) has no CONFIG_IDF_TARGET_* macros.
#if defined(CONFIG_IDF_TARGET_ESP32) || !defined(CONFIG_IDF_TARGET)
#define ESP32_CLASSIC 1
#endif
// On ESP32-S2 and ESP32-S3, serial port -1 is the native USB CDC port
#if (CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3) && (SERIAL_PORT == -1 || SERIAL_PORT_2 == -1 || SERIAL_PORT_3 == -1)
#define HAS_NATIVE_USB_SERIAL 1
#endif
//
// Board-specific options need to be defined before HAL.h
//

View file

@ -71,6 +71,8 @@
#if ENABLED(BLUETOOTH)
#error "BLUETOOTH is for AT90USB. On ESP32 set SERIAL_PORT, SERIAL_PORT_2 or SERIAL_PORT_3 to -2 for Bluetooth."
#elif HAS_BLUETOOTH_SERIAL && !ESP32_CLASSIC
#error "Bluetooth (-2) requires the original ESP32. Other ESP32 chips have no Bluetooth Classic."
#elif HAS_BLUETOOTH_SERIAL && ANY(WIFISUPPORT, ESP3D_WIFISUPPORT)
#error "ESP32 doesn't have enough RAM for Bluetooth (-2) together with WIFISUPPORT or ESP3D_WIFISUPPORT."
#endif
@ -83,3 +85,32 @@
#error "Bluetooth (-2) requires arduino-esp32 1.0.6 or newer. Use an environment with a newer espressif32 platform."
#endif
#endif
#if ENABLED(I2S_STEPPER_SPLIT_STREAM) && !ESP32_CLASSIC
#error "I2S_STEPPER_SPLIT_STREAM is only available on the original ESP32."
#elif ENABLED(I2S_STEPPER_STREAM) && !ESP32_CLASSIC && !(defined(I2S_WS) && defined(I2S_BCK) && defined(I2S_DATA))
#error "I2S_STEPPER_STREAM requires I2S_WS, I2S_BCK and I2S_DATA."
#endif
#if MB(ESPRESSIF_ESP32S3) && !CONFIG_IDF_TARGET_ESP32S3
#error "BOARD_ESPRESSIF_ESP32S3 requires an ESP32-S3 environment (env:esp32s3_idf4)."
#elif MB(ESPRESSIF_ESP32S2) && !CONFIG_IDF_TARGET_ESP32S2
#error "BOARD_ESPRESSIF_ESP32S2 requires an ESP32-S2 environment (env:esp32s2_idf4)."
#endif
#if CONFIG_IDF_TARGET_ESP32S2
// The ESP32-S2 has two UARTs
#if SERIAL_PORT == 2 || SERIAL_PORT_2 == 2 || SERIAL_PORT_3 == 2 || LCD_SERIAL_PORT == 2 || defined(HARDWARE_SERIAL2_RX)
#error "The ESP32-S2 has no UART2. Use serial port 0, -1 (native USB) or -3 (WiFi)."
#elif HAS_NATIVE_USB_SERIAL && ARDUINO_USB_CDC_ON_BOOT
#error "Serial port -1 requires ARDUINO_USB_CDC_ON_BOOT=0 on the ESP32-S2."
#endif
#endif
#if !(CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3) && SERIAL_PORT_2 == -1 && ANY(WIFISUPPORT, ESP3D_WIFISUPPORT)
#error "The WiFi serial port is now -3 (e.g., SERIAL_PORT_2 -3). Serial port -1 is the native USB port (ESP32-S2 and ESP32-S3 only)."
#elif !(CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3) && (SERIAL_PORT == -1 || SERIAL_PORT_2 == -1 || SERIAL_PORT_3 == -1)
#error "Serial port -1 (native USB) requires an ESP32-S2 or ESP32-S3."
#elif (SERIAL_PORT == -3 || SERIAL_PORT_2 == -3 || SERIAL_PORT_3 == -3) && NONE(WIFISUPPORT, ESP3D_WIFISUPPORT)
#error "Serial port -3 (WiFi) requires WIFISUPPORT or ESP3D_WIFISUPPORT."
#endif

View file

@ -35,7 +35,11 @@
int8_t channel_for_pin(const uint8_t pin);
struct PinsDebug : PinsDebugBase<PinsDebug> {
static constexpr uint16_t count = NUM_DIGITAL_PINS;
#if CONFIG_IDF_TARGET_ESP32S3 && ESP_IDF_VERSION_MAJOR < 5
static constexpr uint16_t count = NUM_DIGITAL_PINS + 1; // arduino-esp32 2.x S3 variant omits GPIO 48
#else
static constexpr uint16_t count = NUM_DIGITAL_PINS;
#endif
static constexpr uint8_t name_pad = 16; // space needed to be pretty if not first name assigned to a pin
/**
@ -46,21 +50,33 @@ struct PinsDebug : PinsDebugBase<PinsDebug> {
static bool is_valid(const pin_t pin) { return WITHIN(pin, 0, pin_t(count - 1)) && GPIO_IS_VALID_GPIO(pin); }
/**
* M43 must never drive the SPI flash pins (GPIO 6-11), which crashes the chip,
* or the UART0 pins (GPIO 1, 3) of the host serial port, which then stops working.
* M43 must never drive the SPI flash pins, which crashes the chip, or the pins
* of a host serial port, which then stops working.
*/
static bool never_touch(const uint16_t index) { return WITHIN(index, 6, 11) || index == 1 || index == 3; }
static bool never_touch(const uint16_t index) {
#if CONFIG_IDF_TARGET_ESP32S3
// Flash and octal PSRAM (GPIO 26-37), native USB (GPIO 19, 20), UART0 (GPIO 43, 44)
return WITHIN(index, 26, 37) || index == 19 || index == 20 || index == 43 || index == 44;
#elif CONFIG_IDF_TARGET_ESP32S2
// Flash and PSRAM (GPIO 26-32), native USB (GPIO 19, 20), UART0 (GPIO 43, 44)
return WITHIN(index, 26, 32) || index == 19 || index == 20 || index == 43 || index == 44;
#else
// Flash (GPIO 6-11), UART0 (GPIO 1, 3)
return WITHIN(index, 6, 11) || index == 1 || index == 3;
#endif
}
/**
* ADC channels are not a contiguous range of GPIOs, so the channel and the pin
* are different numbers. ESP32 analogRead() takes a GPIO, and the shared code
* passes it whatever this returns, so return the pin. print_analog reports
* the actual channel.
*
* An analogRead() switches the pin to the ADC, so a never_touch pin isn't
* analog here. The ESP32-S2/S3 native USB pins (GPIO 19, 20) are also ADC2 inputs.
*/
static int16_t analog_index(const pin_t pin) {
return (is_valid(pin) && digitalPinToAnalogChannel(pin) >= 0) ? int8_t(pin) : -1;
}
static bool is_analog(const pin_t pin) { return is_valid(pin) && digitalPinToAnalogChannel(pin) >= 0; }
static bool is_analog(const pin_t pin) { return is_valid(pin) && !never_touch(pin) && digitalPinToAnalogChannel(pin) >= 0; }
static int16_t analog_index(const pin_t pin) { return is_analog(pin) ? int8_t(pin) : -1; }
static void print_analog(const pin_t pin) {
char buffer[12];

View file

@ -29,6 +29,10 @@
#include "../../inc/MarlinConfig.h"
#if !ESP32_CLASSIC
#include <hal/timer_ll.h>
#endif
// ------------------------
// Local defines
// ------------------------
@ -55,25 +59,34 @@ const tTimerConfig timer_config[NUM_HARDWARE_TIMERS] = {
void IRAM_ATTR timer_isr(void *para) {
const tTimerConfig& timer = timer_config[(int)para];
// Retrieve the interrupt status and the counter value
// from the timer that reported the interrupt
uint32_t intr_status = TG[timer.group]->int_st_timers.val;
TG[timer.group]->hw_timer[timer.idx].update = 1;
#if ESP32_CLASSIC
// Retrieve the interrupt status and the counter value
// from the timer that reported the interrupt
uint32_t intr_status = TG[timer.group]->int_st_timers.val;
TG[timer.group]->hw_timer[timer.idx].update = 1;
// Clear the interrupt
if (intr_status & BIT(timer.idx)) {
switch (timer.idx) {
case TIMER_0: TG[timer.group]->int_clr_timers.t0 = 1; break;
case TIMER_1: TG[timer.group]->int_clr_timers.t1 = 1; break;
case TIMER_MAX: break;
// Clear the interrupt
if (intr_status & BIT(timer.idx)) {
switch (timer.idx) {
case TIMER_0: TG[timer.group]->int_clr_timers.t0 = 1; break;
case TIMER_1: TG[timer.group]->int_clr_timers.t1 = 1; break;
case TIMER_MAX: break;
}
}
}
#else
// Other ESP32 variants (IDF 4.4+) have a different register layout
timer_ll_clear_intr_status(TG[timer.group], timer.idx);
#endif
timer.fn();
// After the alarm has been triggered
// Enable it again so it gets triggered the next time
TG[timer.group]->hw_timer[timer.idx].config.alarm_en = TIMER_ALARM_EN;
#if ESP32_CLASSIC
TG[timer.group]->hw_timer[timer.idx].config.alarm_en = TIMER_ALARM_EN;
#else
timer_ll_set_alarm_enable(TG[timer.group], timer.idx, true);
#endif
}
/**
@ -91,6 +104,9 @@ void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
config.alarm_en = TIMER_ALARM_EN;
config.intr_type = TIMER_INTR_LEVEL;
config.auto_reload = TIMER_AUTORELOAD_EN;
#if SOC_TIMER_GROUP_SUPPORT_XTAL
config.clk_src = TIMER_SRC_CLK_APB; // HAL_TIMER_RATE is the APB clock
#endif
// Select and initialize the timer
timer_init(timer.group, timer.idx, &config);
@ -165,7 +181,7 @@ void HAL_timer_disable_interrupt(const uint8_t timer_num) {
bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
const tTimerConfig timer = timer_config[timer_num];
return TG[timer.group]->int_ena.val | BIT(timer_num);
return TERN(ESP32_CLASSIC, TG[timer.group]->int_ena.val, TG[timer.group]->int_ena_timers.val) | BIT(timer_num);
}
#endif // ARDUINO_ARCH_ESP32

View file

@ -43,6 +43,7 @@ void wifi_init() {
SERIAL_ECHO_MSG("Starting WiFi...");
WiFi.mode(WIFI_STA);
WiFi.setSleep(false); // Modem sleep delays inbound packets; a late ACK stalls the WebSocket until lwIP retransmits
WiFi.begin(WIFI_SSID, WIFI_PWD);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {

View file

@ -550,6 +550,8 @@
#define BOARD_MM_JOKER 7011 // MagicMaker JOKER based on ESP32 (with I2S stepper stream)
#define BOARD_MKS_DLC32_V2_1 7012 // MKS DLC32 V2.1 CNC/laser controller based on ESP32 (with I2S stepper stream)
#define BOARD_BTT_RODENT_V1 7013 // BigTreeTech Rodent V1.0/V1.1 CNC controller based on ESP32 (with I2S stepper stream)
#define BOARD_ESPRESSIF_ESP32S3 7014 // Generic ESP32-S3
#define BOARD_ESPRESSIF_ESP32S2 7015 // Generic ESP32-S2
//
// SAMD51 ARM Cortex-M4

View file

@ -4531,8 +4531,8 @@ static_assert(_PLUS_TEST(3), "DEFAULT_MAX_ACCELERATION values must be positive."
#error "Enable only one of WIFISUPPORT or ESP3D_WIFISUPPORT."
#elif ENABLED(ESP3D_WIFISUPPORT) && DISABLED(ARDUINO_ARCH_ESP32)
#error "ESP3D_WIFISUPPORT requires an ESP32 motherboard."
#elif ENABLED(ESP3D_WIFISUPPORT) && (!defined(SERIAL_PORT_2) || SERIAL_PORT_2 != -1)
#error "ESP3D_WIFISUPPORT requires SERIAL_PORT_2 -1, which carries G-code from ESP3D to Marlin."
#elif ENABLED(ESP3D_WIFISUPPORT) && SERIAL_PORT != -3 && (!defined(SERIAL_PORT_2) || SERIAL_PORT_2 != -3) && (!defined(SERIAL_PORT_3) || SERIAL_PORT_3 != -3)
#error "ESP3D_WIFISUPPORT requires a serial port set to -3 (e.g., SERIAL_PORT_2 -3), which carries G-code from ESP3D to Marlin."
#endif
// Only the native ESP32 WiFi needs credentials at compile time. An add-on module

View file

@ -0,0 +1,119 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2026 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#pragma once
/**
* Espressif ESP32-S2 (Tensilica Xtensa LX7, single core) pin assignments
* for an ESP32-S2-Saola-1, ESP32-S2-DevKitM-1 or similar dev board.
*
* Avoided: GPIO 0, 45, 46 (strapping), 19/20 (native USB),
* 26-32 (flash and PSRAM), 18 (RGB LED), 43/44 (UART0 to the USB-UART bridge).
* Thermistors use ADC1 (GPIO 1-10), which keeps working with WiFi on.
*/
#include "env_validate.h"
#define BOARD_INFO_NAME "Espressif ESP32-S2"
//
// Limit Switches
//
#define X_MIN_PIN 38
#define Y_MIN_PIN 39
#define Z_MIN_PIN 40
//
// Steppers
//
#if ENABLED(I2S_STEPPER_STREAM)
// I2S stream to 74HC595 shift registers (expander pins 128+)
#define I2S_WS 4 // 74HC595 latch (STCP)
#define I2S_BCK 5 // 74HC595 clock (SHCP)
#define I2S_DATA 6 // 74HC595 data (DS)
#define X_STEP_PIN 128
#define X_DIR_PIN 129
#define X_ENABLE_PIN 130
#define Y_STEP_PIN 131
#define Y_DIR_PIN 132
#define Y_ENABLE_PIN 133
#define Z_STEP_PIN 134
#define Z_DIR_PIN 135
#define Z_ENABLE_PIN 136
#define E0_STEP_PIN 137
#define E0_DIR_PIN 138
#define E0_ENABLE_PIN 139
#else
#define X_STEP_PIN 4
#define X_DIR_PIN 5
#define X_ENABLE_PIN 21
#define Y_STEP_PIN 6
#define Y_DIR_PIN 7
#define Y_ENABLE_PIN X_ENABLE_PIN
#define Z_STEP_PIN 15
#define Z_DIR_PIN 16
#define Z_ENABLE_PIN X_ENABLE_PIN
#define E0_STEP_PIN 17
#define E0_DIR_PIN 8
#define E0_ENABLE_PIN X_ENABLE_PIN
#endif
//
// Temperature Sensors
//
#define TEMP_0_PIN 1 // Analog Input (ADC1)
#define TEMP_BED_PIN 2 // Analog Input (ADC1)
//
// Heaters / Fans
//
#define HEATER_0_PIN 14
#define HEATER_BED_PIN 41
#define FAN0_PIN 42
//
// Servos
//
#ifndef SERVO0_PIN
#define SERVO0_PIN 9
#endif
//
// MicroSD card (FSPI default pins)
//
#define SD_SS_PIN 34
#define SD_MOSI_PIN 35
#define SD_SCK_PIN 36
#define SD_MISO_PIN 37
//
// Onboard WS2812 RGB LED
//
#ifndef BOARD_NEOPIXEL_PIN
#define BOARD_NEOPIXEL_PIN 18
#endif

View file

@ -0,0 +1,124 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2026 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#pragma once
/**
* Espressif ESP32-S3 (Tensilica Xtensa LX7) pin assignments
* for an ESP32-S3-DevKitC-1 or similar dev board.
*
* Avoided: GPIO 0, 3, 45, 46 (strapping), 19/20 (native USB),
* 26-32 (flash), 33-37 (octal PSRAM on N8R8/N16R8 modules),
* 38/48 (RGB LED), 43/44 (UART0 to the USB-UART bridge).
* Thermistors use ADC1 (GPIO 1-10), which keeps working with WiFi on.
*/
#include "env_validate.h"
#define BOARD_INFO_NAME "Espressif ESP32-S3"
//
// Limit Switches
//
#define X_MIN_PIN 39
#define Y_MIN_PIN 40
#define Z_MIN_PIN 41
//
// Steppers
//
#if ENABLED(I2S_STEPPER_STREAM)
// I2S stream to 74HC595 shift registers (expander pins 128+)
#define I2S_WS 4 // 74HC595 latch (STCP)
#define I2S_BCK 5 // 74HC595 clock (SHCP)
#define I2S_DATA 6 // 74HC595 data (DS)
#define X_STEP_PIN 128
#define X_DIR_PIN 129
#define X_ENABLE_PIN 130
#define Y_STEP_PIN 131
#define Y_DIR_PIN 132
#define Y_ENABLE_PIN 133
#define Z_STEP_PIN 134
#define Z_DIR_PIN 135
#define Z_ENABLE_PIN 136
#define E0_STEP_PIN 137
#define E0_DIR_PIN 138
#define E0_ENABLE_PIN 139
#else
#define X_STEP_PIN 4
#define X_DIR_PIN 5
#define X_ENABLE_PIN 21
#define Y_STEP_PIN 6
#define Y_DIR_PIN 7
#define Y_ENABLE_PIN X_ENABLE_PIN
#define Z_STEP_PIN 15
#define Z_DIR_PIN 16
#define Z_ENABLE_PIN X_ENABLE_PIN
#define E0_STEP_PIN 17
#define E0_DIR_PIN 18
#define E0_ENABLE_PIN X_ENABLE_PIN
#endif
//
// Temperature Sensors
//
#define TEMP_0_PIN 1 // Analog Input (ADC1)
#define TEMP_BED_PIN 2 // Analog Input (ADC1)
//
// Heaters / Fans
//
#define HEATER_0_PIN 14
#define HEATER_BED_PIN 47
#define FAN0_PIN 42
//
// Servos
//
#ifndef SERVO0_PIN
#define SERVO0_PIN 9
#endif
//
// MicroSD card (FSPI default pins)
//
#define SD_SS_PIN 10
#define SD_MOSI_PIN 11
#define SD_SCK_PIN 12
#define SD_MISO_PIN 13
//
// Onboard WS2812 RGB LED (GPIO 38 on some DevKitC-1 revisions)
//
#ifndef BOARD_NEOPIXEL_PIN
#define BOARD_NEOPIXEL_PIN 48
#endif

View file

@ -927,6 +927,10 @@
#include "esp32/pins_MKS_DLC32_V2_1.h" // ESP32 env:mks_dlc32_v2_1
#elif MB(BTT_RODENT_V1)
#include "esp32/pins_BTT_RODENT_V1.h" // ESP32 env:esp32_idf3
#elif MB(ESPRESSIF_ESP32S3)
#include "esp32/pins_ESP32S3.h" // ESP32-S3 env:esp32s3_idf4
#elif MB(ESPRESSIF_ESP32S2)
#include "esp32/pins_ESP32S2.h" // ESP32-S2 env:esp32s2_idf4
//
// Adafruit Grand Central M4 (SAMD51 ARM Cortex-M4)

View file

@ -46,8 +46,8 @@ struct filepos_t {
filepos_t() : position(0), cluster(0) {}
};
// Avoid conflict with RP2040 / newlib fcntl.h
#ifdef __PLAT_RP2040__
// Avoid conflict with RP2040 / ESP32 newlib fcntl.h
#if defined(__PLAT_RP2040__) || defined(ARDUINO_ARCH_ESP32)
#undef O_RDONLY
#undef O_WRONLY
#undef O_RDWR

View file

@ -8,7 +8,7 @@
[config:base]
motherboard = BOARD_ESPRESSIF_ESP32
emergency_parser = on
serial_port_2 = -1
serial_port_2 = -3
tx_buffer_size = 64
wifi_ssid = "ssid"
wifi_pwd = "password"

View file

@ -0,0 +1,11 @@
#
# Marlin Firmware
# Test: esp32s2_idf4/config-01.ini
#
# Generic ESP32-S2 with EEPROM and SD
#
[config:base]
motherboard = BOARD_ESPRESSIF_ESP32S2
eeprom_settings = on
sdsupport = on

View file

@ -0,0 +1,11 @@
#
# Marlin Firmware
# Test: esp32s2_idf4/config-02.ini
#
# Generic ESP32-S2 with the I2S stepper stream and a servo
#
[config:base]
motherboard = BOARD_ESPRESSIF_ESP32S2
i2s_stepper_stream = on
num_servos = 1

View file

@ -0,0 +1,10 @@
#
# Marlin Firmware
# Test: esp32s2_idf4/config-03.ini
#
# Generic ESP32-S2 with the native USB port as SERIAL_PORT
#
[config:base]
motherboard = BOARD_ESPRESSIF_ESP32S2
serial_port = -1

View file

@ -0,0 +1,18 @@
#
# Marlin Firmware
# Test: esp32s2_idf4/config-04.ini
#
# Generic ESP32-S2 with UART, native USB and WiFi serial ports and the emergency parser
#
[config:base]
motherboard = BOARD_ESPRESSIF_ESP32S2
emergency_parser = on
tx_buffer_size = 64
serial_port_2 = -1
serial_port_3 = -3
wifi_ssid = "ssid"
wifi_pwd = "password"
wifisupport = on
websupport = on
otasupport = on

View file

@ -0,0 +1,11 @@
#
# Marlin Firmware
# Test: esp32s3_idf4/config-01.ini
#
# Generic ESP32-S3 with EEPROM and SD
#
[config:base]
motherboard = BOARD_ESPRESSIF_ESP32S3
eeprom_settings = on
sdsupport = on

View file

@ -0,0 +1,11 @@
#
# Marlin Firmware
# Test: esp32s3_idf4/config-02.ini
#
# Generic ESP32-S3 with the I2S stepper stream and a servo
#
[config:base]
motherboard = BOARD_ESPRESSIF_ESP32S3
i2s_stepper_stream = on
num_servos = 1

View file

@ -0,0 +1,10 @@
#
# Marlin Firmware
# Test: esp32s3_idf4/config-03.ini
#
# Generic ESP32-S3 with the native USB port as SERIAL_PORT
#
[config:base]
motherboard = BOARD_ESPRESSIF_ESP32S3
serial_port = -1

View file

@ -0,0 +1,18 @@
#
# Marlin Firmware
# Test: esp32s3_idf4/config-04.ini
#
# Generic ESP32-S3 with UART, native USB and WiFi serial ports and the emergency parser
#
[config:base]
motherboard = BOARD_ESPRESSIF_ESP32S3
emergency_parser = on
tx_buffer_size = 64
serial_port_2 = -1
serial_port_3 = -3
wifi_ssid = "ssid"
wifi_pwd = "password"
wifisupport = on
websupport = on
otasupport = on

View file

@ -8,7 +8,7 @@
[config:base]
motherboard = BOARD_MKS_TINYBEE
tx_buffer_size = 64
serial_port_2 = -1
serial_port_2 = -3
baudrate_2 = 250000
esp3d_wifisupport = on
websupport = on

View file

@ -16,7 +16,7 @@
platform = espressif32@2.1.0
platform_packages = espressif/toolchain-xtensa-esp32s3
board = esp32dev
build_flags = ${common.build_flags} -DCORE_DEBUG_LEVEL=0 -std=gnu++17 -DARDUINO_ARCH_ESP32
build_flags = ${common.build_flags} -DCORE_DEBUG_LEVEL=0 -std=gnu++17 -DARDUINO_ARCH_ESP32 -DPLATFORM_M997_SUPPORT
build_unflags = -std=gnu11 -std=gnu++11 -fsingle-precision-constant -Wno-register
build_src_filter = ${common.default_src_filter} +<src/HAL/ESP32>
lib_ignore = NativeEthernet, AsyncTCP_RP2040W
@ -33,6 +33,23 @@ monitor_filters = colorize, time, send_on_enter, log2file, esp32_exception_dec
extends = env:esp32_idf3
platform = espressif32@6.1.0
#
# Espressif ESP32-S3 (arduino-esp32 2.0.7, ESP-IDF 4.4)
#
[env:esp32s3_idf4]
extends = env:esp32_idf4
board = esp32-s3-devkitc-1
# The S3 has no Bluetooth Classic
lib_ignore = ${env:esp32_idf3.lib_ignore}
BluetoothSerial
#
# Espressif ESP32-S2 (arduino-esp32 2.0.7, ESP-IDF 4.4)
#
[env:esp32s2_idf4]
extends = env:esp32_idf4
board = esp32-s2-saola-1
[env:FYSETC_E4]
extends = env:esp32_idf3
board_build.partitions = default_16MB.csv