Skip to content

Commit dc0208e

Browse files
committed
Add option USB CDC to be used for Serial (starting USB early)
1 parent f58cba0 commit dc0208e

File tree

9 files changed

+33
-3
lines changed

9 files changed

+33
-3
lines changed

boards.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
menu.UploadSpeed=Upload Speed
22
menu.UploadMode=Upload Mode
3+
menu.SerialMode=Serial Connected To
34
menu.CPUFreq=CPU Frequency
45
menu.FlashFreq=Flash Frequency
56
menu.FlashMode=Flash Mode
@@ -30,6 +31,7 @@ esp32s2.build.core=esp32
3031
esp32s2.build.variant=esp32s2
3132
esp32s2.build.board=ESP32S2_DEV
3233

34+
esp32s2.build.serial=0
3335
esp32s2.build.f_cpu=240000000L
3436
esp32s2.build.flash_size=4MB
3537
esp32s2.build.flash_freq=80m
@@ -38,6 +40,11 @@ esp32s2.build.boot=qio
3840
esp32s2.build.partitions=default
3941
esp32s2.build.defines=
4042

43+
esp32s2.menu.SerialMode.default=UART0
44+
esp32s2.menu.SerialMode.default.build.serial=0
45+
esp32s2.menu.SerialMode.cdc=USB CDC
46+
esp32s2.menu.SerialMode.cdc.build.serial=1
47+
4148
esp32s2.menu.UploadMode.default=Default Uart
4249
esp32s2.menu.UploadMode.default.upload.flags=
4350
esp32s2.menu.UploadMode.default.upload.tool=esptool_py

cores/esp32/HardwareSerial.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@
3737
#endif
3838

3939
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
40+
#if ARDUINO_SERIAL_PORT //Serial used for USB CDC
41+
HardwareSerial Serial0(0);
42+
#else
4043
HardwareSerial Serial(0);
44+
#endif
4145
HardwareSerial Serial1(1);
4246
#if CONFIG_IDF_TARGET_ESP32
4347
HardwareSerial Serial2(2);

cores/esp32/HardwareSerial.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,13 @@ class HardwareSerial: public Stream
109109
extern void serialEventRun(void) __attribute__((weak));
110110

111111
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
112+
#if ARDUINO_SERIAL_PORT //Serial used for USB CDC
113+
#include "USB.h"
114+
#include "USBCDC.h"
115+
extern HardwareSerial Serial0;
116+
#else
112117
extern HardwareSerial Serial;
113-
extern HardwareSerial Serial1;
118+
#endif
114119
#if CONFIG_IDF_TARGET_ESP32
115120
extern HardwareSerial Serial2;
116121
#endif
File renamed without changes.
File renamed without changes.

libraries/USB/src/USBCDC.cpp renamed to cores/esp32/USBCDC.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ USBCDC::operator bool() const
329329
return connected;
330330
}
331331

332+
#if ARDUINO_SERIAL_PORT //Serial used for USB CDC
333+
USBCDC Serial(0);
334+
#endif
335+
332336
#endif /* CONFIG_USB_CDC_ENABLED */
333337

334338
#endif /* CONFIG_USB_ENABLED */

libraries/USB/src/USBCDC.h renamed to cores/esp32/USBCDC.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ class USBCDC: public Stream
126126

127127
};
128128

129-
extern void serialEventRun(void) __attribute__((weak));
129+
#if ARDUINO_SERIAL_PORT //Serial used for USB CDC
130+
extern USBCDC Serial;
131+
#endif
130132

131133
#endif /* CONFIG_USB_CDC_ENABLED */

cores/esp32/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#include "freertos/task.h"
33
#include "esp_task_wdt.h"
44
#include "Arduino.h"
5+
#if ARDUINO_SERIAL_PORT //Serial used for USB CDC
6+
#include "USB.h"
7+
#endif
58

69
TaskHandle_t loopTaskHandle = NULL;
710

@@ -36,6 +39,9 @@ void loopTask(void *pvParameters)
3639

3740
extern "C" void app_main()
3841
{
42+
#if ARDUINO_SERIAL_PORT //Serial used for USB CDC
43+
USB.begin();
44+
#endif
3945
loopTaskWDTEnabled = false;
4046
initArduino();
4147
xTaskCreateUniversal(loopTask, "loopTask", 8192, NULL, 1, &loopTaskHandle, CONFIG_ARDUINO_RUNNING_CORE);

platform.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ compiler.cpp.flags.esp32=-mlongcalls -Wno-frame-address -ffunction-sections -fda
2929
compiler.S.flags.esp32=-ffunction-sections -fdata-sections -fstrict-volatile-bitfields -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wno-unused-parameter -Wno-sign-compare -ggdb -mfix-esp32-psram-cache-issue -mfix-esp32-psram-cache-strategy=memw -Og -fstack-protector -x assembler-with-cpp -MMD -c
3030
compiler.c.elf.flags.esp32=-mlongcalls -T esp32.rom.ld -T esp32.rom.libgcc.ld -T esp32.rom.newlib-data.ld -T esp32.rom.syscalls.ld -T esp32_out.ld -T esp32.project.ld -T esp32.peripherals.ld -Wl,--cref -fno-rtti -fno-lto -u esp_app_desc -u vfs_include_syscalls_impl -u pthread_include_pthread_impl -u pthread_include_pthread_cond_impl -u pthread_include_pthread_local_storage_impl -u app_main -u call_user_start_cpu0 -u ld_include_panic_highint_hdl -mfix-esp32-psram-cache-issue -mfix-esp32-psram-cache-strategy=memw -Wl,--gc-sections -Wl,--undefined=uxTopUsedPriority -u newlib_include_locks_impl -u newlib_include_heap_impl -u newlib_include_syscalls_impl -u newlib_include_pthread_impl -u __cxa_guard_dummy
3131
compiler.ar.flags.esp32=cru
32+
build.extra_flags.esp32=-DARDUINO_SERIAL_PORT=0
3233
#
3334
# ESP32 Support End
3435
#
@@ -43,6 +44,7 @@ compiler.cpp.flags.esp32s2=-mlongcalls -ffunction-sections -fdata-sections -fstr
4344
compiler.S.flags.esp32s2=-ffunction-sections -fdata-sections -fstrict-volatile-bitfields -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wno-unused-parameter -Wno-sign-compare -ggdb -Og -fstack-protector -x assembler-with-cpp -MMD -c
4445
compiler.c.elf.flags.esp32s2=-mlongcalls -T esp32s2.rom.ld -T esp32s2.rom.libgcc.ld -T esp32s2.rom.newlib-data.ld -T esp32s2.rom.newlib-funcs.ld -T esp32s2.rom.spiflash.ld -T esp32s2_out.ld -T esp32s2.project.ld -T esp32s2.peripherals.ld -Wl,--cref -fno-rtti -fno-lto -u esp_app_desc -u vfs_include_syscalls_impl -u pthread_include_pthread_impl -u pthread_include_pthread_cond_impl -u pthread_include_pthread_local_storage_impl -u app_main -u call_user_start_cpu0 -u ld_include_panic_highint_hdl -Wl,--gc-sections -Wl,--undefined=uxTopUsedPriority -u newlib_include_locks_impl -u newlib_include_heap_impl -u newlib_include_syscalls_impl -u newlib_include_pthread_impl -u __cxa_guard_dummy
4546
compiler.ar.flags.esp32s2=cru
47+
build.extra_flags.esp32s2=-DARDUINO_SERIAL_PORT={build.serial}
4648
#
4749
# ESP32S2 Support End
4850
#
@@ -87,7 +89,7 @@ build.flash_mode=dio
8789
build.boot=bootloader
8890
build.code_debug=0
8991
build.defines=
90-
build.extra_flags=-DESP32 -DCORE_DEBUG_LEVEL={build.code_debug} {build.defines}
92+
build.extra_flags=-DESP32 -DCORE_DEBUG_LEVEL={build.code_debug} {build.defines} {build.extra_flags.{build.mcu}}
9193

9294
# Check if custom partitions exist
9395
recipe.hooks.prebuild.1.pattern=bash -c "[ ! -f {build.source.path}/partitions.csv ] || cp -f {build.source.path}/partitions.csv {build.path}/partitions.csv"

0 commit comments

Comments
 (0)