Are you talking about C2 or S2 !? Thats a big difference!
The C seriers is very different to the S series.
Are you talking about C2 or S2 !? Thats a big difference!
The C seriers is very different to the S series.
Sorry, getting my letters mixed up. I am referring to the ESP32S2. The new board is the ESP32C3 on which at least it works when compiled in the Arduino IDE.
In pioardiuino, I created a new board config like so:
[env:esp32-c3-devkitm-1]
platform = https://github.com/pioarduino/platform-espressif32/releases/download/54.03.20/platform-espressif32.zip
framework = arduino
board = esp32-c3-devkitm-1
monitor_speed = 115200
build_flags =
-D ARDUINO_USB_CDC_ON_BOOT=1
but get the following errors:
Compiling .pio/build/esp32-c3-devkitm-1/FrameworkArduino/esp32-hal-gpio.c.o
In file included from /home/johnc/.platformio/packages/framework-arduinoespressif32/cores/esp32/Arduino.h:203,
from src/main.cpp:1:
src/main.cpp: In function 'void setup()':
/home/johnc/.platformio/packages/framework-arduinoespressif32/cores/esp32/HardwareSerial.h:382:16: error: 'USBSerial' was not declared in this scope; did you mean 'Serial'?
382 | #define Serial USBSerial
| ^~~~~~~~~
src/main.cpp:4:3: note: in expansion of macro 'Serial'
4 | Serial.begin(115200);
| ^~~~~~
src/main.cpp: In function 'void loop()':
/home/johnc/.platformio/packages/framework-arduinoespressif32/cores/esp32/HardwareSerial.h:382:16: error: 'USBSerial' was not declared in this scope; did you mean 'Serial'?
382 | #define Serial USBSerial
| ^~~~~~~~~
src/main.cpp:8:3: note: in expansion of macro 'Serial'
8 | Serial.println("Hello :-)");
| ^~~~~~
Compiling .pio/build/esp32-c3-devkitm-1/FrameworkArduino/esp32-hal-i2c-ng.c.o
Compiling .pio/build/esp32-c3-devkitm-1/FrameworkArduino/esp32-hal-i2c-slave.c.o
*** [.pio/build/esp32-c3-devkitm-1/src/main.cpp.o] Error 1
In file included from /home/johnc/.platformio/packages/framework-arduinoespressif32/cores/esp32/Arduino.h:203,
from /home/johnc/.platformio/packages/framework-arduinoespressif32/cores/esp32/chip-debug-report.cpp:17:
/home/johnc/.platformio/packages/framework-arduinoespressif32/cores/esp32/chip-debug-report.cpp: In function 'void printBeforeSetupInfo()':
/home/johnc/.platformio/packages/framework-arduinoespressif32/cores/esp32/HardwareSerial.h:382:16: error: 'USBSerial' was not declared in this scope; did you mean 'Serial'?
382 | #define Serial USBSerial
| ^~~~~~~~~
/home/johnc/.platformio/packages/framework-arduinoespressif32/cores/esp32/chip-debug-report.cpp:294:3: note: in expansion of macro 'Serial'
294 | Serial.begin(0);
| ^~~~~~
*** [.pio/build/esp32-c3-devkitm-1/FrameworkArduino/chip-debug-report.cpp.o] Error 1
================================ [FAILED] Took 4.24 seconds ================================
This is my main.cpp:
#include <Arduino.h>
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.println("Hello :-)");
delay(1000);
}
Thanks for clearification.
One of the mentioned differences is that the C3 only supports HWCDC!
Therefore you must set both flags:
build_flags=
-DARDUINO_USB_CDC_ON_BOOT=1
-DARDUINO_USB_MODE=1
The Serial object will then point to a HWCDC instance.
Yes, that was it! For the S2 I couldn’t set both flags otherwise it would not compile. For the C3 I must set both flags. It then compiles and works as it does when compiled in the Arduino IDE.
I did try to compile the USBCDC version, but it complained that USBCDC does not name a type. Seemed to have no problem with that when compiling for the S2.
#include <USB.h>
USBCDC USBSerial;
void setup() {
// put your setup code here, to run once:
USBSerial.begin();
USB.begin();
}
void loop() {
// put your main code here, to run repeatedly:
USBSerial.println("Hello :-)");
delay(1000);
}
But then again, it didn’t work on the S2 either. At least the C3 works with the Arduino Serial.begin() version.
Compiling .pio/build/esp32-c3-devkitm-1/FrameworkArduino/esp32-hal-gpio.c.o
src/main.cpp:4:1: error: 'USBCDC' does not name a type
4 | USBCDC USBSerial;
| ^~~~~~
src/main.cpp: In function 'void setup()':
src/main.cpp:7:3: error: 'USBSerial' was not declared in this scope; did you mean 'Serial'?
7 | USBSerial.begin();
| ^~~~~~~~~
| Serial
Compiling .pio/build/esp32-c3-devkitm-1/FrameworkArduino/esp32-hal-i2c-ng.c.o
src/main.cpp:8:3: error: 'USB' was not declared in this scope
8 | USB.begin();
| ^~~
src/main.cpp: In function 'void loop()':
src/main.cpp:12:3: error: 'USBSerial' was not declared in this scope; did you mean 'Serial'?
12 | USBSerial.println("Hello :-)");
| ^~~~~~~~~
| Serial
Compiling .pio/build/esp32-c3-devkitm-1/FrameworkArduino/esp32-hal-i2c-slave.c.o
Compiling .pio/build/esp32-c3-devkitm-1/FrameworkArduino/esp32-hal-i2c.c.o
Compiling .pio/build/esp32-c3-devkitm-1/FrameworkArduino/esp32-hal-ledc.c.o
*** [.pio/build/esp32-c3-devkitm-1/src/main.cpp.o] Error 1
================================ [FAILED] Took 4.30 seconds ================================
Doesn’t seem to matter whether ARDUINO_USB_MODE is set to 0 or 1.
Perhaps this is not supported on the C3?
That’s what I meant here
and here
That’s correct. If you check the datasheet for the C3 on Page 2 you’ll notice that it only supports “USB Serial/JTAG” whereas the S2 and S3 also supports “USB-OTG” (datasheet for S3).
-DARDUINO_USB_CDC_ON_BOOT “routes” the Serial object to the native USB instead of the UART port.
-DARDUINO_USB_MODE activates either USB-OTG (value “0”) or USB Serial/JTAG (value “1”). Since the C3 doesn’t have USB-OTG this value must be set to 1.
That’s starting to make sense. With the S2 supporting USB Serial/JTAG, with the correct (same?) options set (both flags enabled) it ought to work for the S2 as well. I can confirm your observation here:
When I first compile I get:
src/main.cpp: In function 'void setup()':
/home/johnc/.platformio/packages/framework-arduinoespressif32/cores/esp32/HardwareSerial.h:379:16: error: 'HWCDCSerial' was not declared in this scope
379 | #define Serial HWCDCSerial
But if I add the SOC_USB_SERIAL_JTAG_SUPPORTED flag you mention I then get:
Compiling .pio/build/AR488-ESP32-Rev4/FrameworkArduino/StreamString.cpp.o
/home/johnc/.platformio/packages/framework-arduinoespressif32/cores/esp32/HWCDC.cpp:27:10: fatal error: soc/usb_serial_jtag_struct.h: No such file or directory
27 | #include "soc/usb_serial_jtag_struct.h"
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
So pretty much as you describe. I dug around in the Espressif Arduino core and eventually found that the missing files exist in the .platformio/packages/framework-arduinoespressif32-libs/esp32s3/ directory and also the corresponding esp32p4 and esp32c3 and esp32c6 directories, but not the corresponding esp32s2 directory. As an experiment I tried copying the files across from C3 but you eventually end up with a situation where you get an undefined GPIO pin flag error, probably because peripherals sit on different pins in different variants, so evidently its not just a case of missing files, but the files in question may need to be tailored for the S2.
When communicating directly to USB CDC class port on ESP32S2, your PC needs to reenumerate USB every time the ESP32S2 is reset. Any previous serial communication is lost. You need to restart Serial Monitor on PlatformIO to allow the computer to reenumerate again the USB CDC connections to your ESP32S2 and being able to receive data again.
Re-enumaration upon reset makes sense and Serial Monitor copes with this just fine. It seems to automatially re-connect with the /dev/ttyACM0 serial port on reset in the background, which is why this is the ONLY way that that I can connect to the serial port on an ESP32S2. The difficulty is that because it requires a reset, it seems impossible to connect to the serial port of the ESP32S2 using any other software.
I think sivar2311 has identified the file omissions in the Espressif library which explain what is going on. Perhaps this is one of the reasons that contribute to the S2 not being officially supported? It would nice if there was a fix and I am wondering whether to log this as a request in the Espressif Arduino core issues log.