Skip to content

Enable "USB CDC On Boot" for S2 not working with ESP IDF component project #9326

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

Closed
1 task done
dzungpv opened this issue Mar 3, 2024 · 16 comments
Closed
1 task done
Assignees
Labels
Area: Arduino as ESP-IDF component Issues about Arduino used as component in ESP-IDF Chip: ESP32-S2 Issue is related to support of ESP32-S2 Chip Status: Solved Type: For reference Common questions & problems

Comments

@dzungpv
Copy link

dzungpv commented Mar 3, 2024

Board

Lolin S2 Mini

Device Description

Default

Hardware Configuration

Default

Version

v2.0.14

IDE Name

ESP IDF 4.4.6

Operating System

Macos 13, Windows 10

Flash frequency

40Mhz

PSRAM enabled

no

Upload speed

115200

Description

I enable USB CDC On Boot for a project building as ESP IDF component but it is not working.
The steps to enable the project here: https://docs.espressif.com/projects/arduino-esp32/en/latest/esp-idf_component.html

The way I use to enable it mention here: https://esp32.com/viewtopic.php?t=30926#p115761.
I have tried both with release 2.0.14 and latest 2.x branch with the same result .
sdkconfig i use here: https://github.com/espressif/arduino-esp32/blob/2.0.14/tools/sdk/esp32s2/sdkconfig
I add a definition in root project CMakeLists.txt:

add_definitions(
-DARDUINO_USB_MODE
-DARDUINO_USB_CDC_ON_BOOT
)

It can build and flash to the chip, but no output console like when I build the sketch with Arduino with option : "USB CDC On Boot: Enabled"

Sketch

#include <Arduino.h>

static void test_taskfunc(void * p)
{
    uint32_t tasknum = (uint32_t)p;

    while (true) {
        log_i("This is task instance %u running...", tasknum);
        
        delay(10 * random(1, 10));
    }
}

void setup()
{
    Serial0.begin(115200);
    Serial0.setDebugOutput(true);
    delay(2000);
    Serial0.println("\n==============\nStarting...\n");
    log_e("Starting with LOGs.");
    Serial0.println("==============\n\n");
    delay(750);

    for (auto i = 0; i < 5; i++) {
        TaskHandle_t th;

        // Created tasks all need to have different priorities in order to trigger lockup
        UBaseType_t taskPrio = 1 + 2 + i;

        if (pdPASS == xTaskCreate(test_taskfunc, "test_taskfunc", 4096, (void *)(i + 1), taskPrio, &th)) {
            log_i("Task instance %u created with priority %u", i+1, 3 + i);
        } else {
            log_e("Task instance %u creation FAILED", i+1);
        }
    }
}

void loop()
{
    log_i("This is the main loop running...");

    delay(1000);
}

Debug Message

No output because it is not working

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@dzungpv dzungpv added the Status: Awaiting triage Issue is waiting for triage label Mar 3, 2024
@SuGlider SuGlider added Type: Question Only question Area: Arduino as ESP-IDF component Issues about Arduino used as component in ESP-IDF and removed Status: Awaiting triage Issue is waiting for triage labels Mar 4, 2024
@SuGlider
Copy link
Collaborator

SuGlider commented Mar 4, 2024

@dzungpv - Please use it this way:
ARDUINO_USB_MODE=0 ==>for USB OTG (S2 and S3)
ARDUINO_USB_MODE=1 ==> for USB HW JTAG Serial CDC (for S3, C3, C6 and H2)
ARDUINO_USB_CDC_ON_BOOT=1 ==> it will make Serial to be the USB CDC port
ARDUINO_USB_CDC_ON_BOOT=0 ==> it will make Serial to be the UART0 port

add_definitions(
-DARDUINO_USB_MODE=0
-DARDUINO_USB_CDC_ON_BOOT=1
)

@SuGlider
Copy link
Collaborator

SuGlider commented Mar 4, 2024

Another detail, I only see the example sketch using Serial0 which is for UART0, not USB CDC.
Serial will be the USB CDC when `USB CDC On Boot=1".

@dzungpv
Copy link
Author

dzungpv commented Mar 5, 2024

@dzungpv - Please use it this way: ARDUINO_USB_MODE=0 ==>for USB OTG (S2 and S3) ARDUINO_USB_MODE=1 ==> for USB HW JTAG Serial CDC (for S3, C3, C6 and H2) ARDUINO_USB_CDC_ON_BOOT=1 ==> it will make Serial to be the USB CDC port ARDUINO_USB_CDC_ON_BOOT=0 ==> it will make Serial to be the UART0 port

add_definitions(
-DARDUINO_USB_MODE=0
-DARDUINO_USB_CDC_ON_BOOT=1
)

It is not working, could not build , it show error:

/Users/vietdzung/xxx//components/arduino-esp32/cores/esp32/main.cpp: In function 'void app_main()':
/Users/vietdzung/xxx/components/arduino-esp32/cores/esp32/main.cpp:58:5: error: 'Serial' was not declared in this scope
     Serial.begin();
     ^~~~~~
/Users/vietdzung/xxx/arduino-esp32/cores/esp32/main.cpp:58:5: note: suggested alternative: 'Serial1'
     Serial.begin();
     ^~~~~~
     Serial1
[1118/1252] Building CXX object esp-idf/arduino-esp32/CMakeFiles/__idf_arduino-esp32.dir/cores/esp32/WString.cpp.obj
ninja: build stopped: subcommand failed.

Another detail, I only see the example sketch using Serial0 which is for UART0, not USB CDC. Serial will be the USB CDC when `USB CDC On Boot=1".

There is no Serial when USB CDC ON Boot define:

HardwareSerial Serial0(0);

You can create a test project with component and test with version 2.x, idf 4.4.6 and you will see the issue.

@SuGlider
Copy link
Collaborator

SuGlider commented Mar 5, 2024

There is no Serial when USB CDC ON Boot define:

It is defined in

#if ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE //Serial used for USB CDC
USBCDC Serial(0);
#endif

But it depends on

#if CONFIG_TINYUSB_ENABLED

Please use the sdkconfig file that has all necessary Arduino Settings for the ESP32-S2
https://github.com/espressif/arduino-esp32/blob/44da992b774f76777bb2e931dd76cfcf12b9fe70/tools/sdk/esp32s2/sdkconfig

This will enable TinyUSB necessary for USB CDC.

https://github.com/espressif/arduino-esp32/blob/44da992b774f76777bb2e931dd76cfcf12b9fe70/tools/sdk/esp32s2/sdkconfig#L259C1-L272C1

#
# Arduino TinyUSB
#
CONFIG_TINYUSB_ENABLED=y

#
# Serial (CDC) driver
#
CONFIG_TINYUSB_CDC_ENABLED=y
CONFIG_TINYUSB_DESC_CDC_STRING="Espressif CDC Device"
CONFIG_TINYUSB_CDC_RX_BUFSIZE=64
CONFIG_TINYUSB_CDC_TX_BUFSIZE=64
# end of Serial (CDC) driver

@SuGlider SuGlider self-assigned this Mar 5, 2024
@dzungpv
Copy link
Author

dzungpv commented Mar 5, 2024

There is no Serial when USB CDC ON Boot define:

It is defined in

#if ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE //Serial used for USB CDC
USBCDC Serial(0);
#endif

But it depends on

#if CONFIG_TINYUSB_ENABLED

Please use the sdkconfig file that has all necessary Arduino Settings for the ESP32-S2 https://github.com/espressif/arduino-esp32/blob/44da992b774f76777bb2e931dd76cfcf12b9fe70/tools/sdk/esp32s2/sdkconfig

This will enable TinyUSB necessary for USB CDC.

https://github.com/espressif/arduino-esp32/blob/44da992b774f76777bb2e931dd76cfcf12b9fe70/tools/sdk/esp32s2/sdkconfig#L259C1-L272C1

#
# Arduino TinyUSB
#
CONFIG_TINYUSB_ENABLED=y

#
# Serial (CDC) driver
#
CONFIG_TINYUSB_CDC_ENABLED=y
CONFIG_TINYUSB_DESC_CDC_STRING="Espressif CDC Device"
CONFIG_TINYUSB_CDC_RX_BUFSIZE=64
CONFIG_TINYUSB_CDC_TX_BUFSIZE=64
# end of Serial (CDC) driver

It still not build.
The following steps I create a sample project:
1, Use hello world arduino ESP IDF project
2, Replace main.cpp with sketch in first report
3, Use SDK Config like you mention
4, Add:

# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
add_definitions(
 -DARDUINO_USB_MODE=0
 -DARDUINO_USB_CDC_ON_BOOT=1
)

to the root CMakeLists.txt
5, Checkout 2.x branch: not working, exact commit you comment above: not working, checkout 2.0.14 release: not working. Same error in the core/main.cpp:

arduino-esp32/cores/esp32/main.cpp:58:5: error: 'Serial' was not declared in this scope

Do I missed somethings? the IDF version I use 4.4.6 on the MacOS Soloma 14.2

@SuGlider
Copy link
Collaborator

SuGlider commented Mar 7, 2024

@dzungpv - Please edit the CMaketLists.txt from /main folder, instead of using add_definitions(...):
Add this entry target_compile_options(${COMPONENT_TARGET} PUBLIC -DWHATEVER=VAL ) in the CMakeLists.txt file from the /project_name/main folder, including any other necessary definitions, such as in this example:

target_compile_options(${COMPONENT_TARGET} PUBLIC
    -DARDUINO_BOARD="ESP32S2_DEV"                           <<<<<<=== Board Name (Any one, here is set as ESP32 S2 Dev Kit)
    -DARDUINO_VARIANT="esp32s2"                             <<<<<<=== Variant "folder" must match "/variants/folder" name
    -DBOARD_HAS_PSRAM                                       <<<<<=== Does the ESP32-S2 have PSRAM? Remove it if not.
    -DARDUINO_USB_MODE=0                                    <<<<<=== Activate USB OTG Mode
    -DARDUINO_USB_CDC_ON_BOOT=1                             <<<<<=== Enable Serial as USB CDC Interface
)

@SuGlider
Copy link
Collaborator

SuGlider commented Mar 7, 2024

@dzungpv - Please not that I have edited the comment above (#9326 (comment))

The way to make it work is by editing the CMakeLists.txt file from /main folder to add target_compile_options(${COMPONENT_TARGET} PUBLIC ) with the definition list for this project.

@SuGlider SuGlider added the Type: For reference Common questions & problems label Mar 7, 2024
@SuGlider
Copy link
Collaborator

SuGlider commented Mar 7, 2024

4, Add:

# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
add_definitions(
 -DARDUINO_USB_MODE=0
 -DARDUINO_USB_CDC_ON_BOOT=1
)

to the root CMakeLists.txt

The add_definitions( ) shall be added to the /main/CMakeLists.txt instead of the root folder file.

@SuGlider
Copy link
Collaborator

@dzungpv - Please let me know if the issue is solved.

@dzungpv
Copy link
Author

dzungpv commented Mar 11, 2024

@dzungpv - Please let me know if the issue is solved.

It is not working.
I get the sdkconfig from here: https://github.com/espressif/arduino-esp32/blob/2.0.14/tools/sdk/esp32s2/sdkconfig
Add : CONFIG_ESP_CONSOLE_USB_CDC=y
Add content like you mention above to main/CMakeLists.txt, not working.
I also remote CONFIG_ESP_CONSOLE_USB_CDC=y, it is the same. I delete build folder every test

@SuGlider SuGlider added Chip: ESP32-S2 Issue is related to support of ESP32-S2 Chip Status: Needs investigation We need to do some research before taking next steps on this issue and removed Type: For reference Common questions & problems Type: Question Only question labels Mar 14, 2024
@SuGlider
Copy link
Collaborator

SuGlider commented Mar 14, 2024

@dzungpv - I got it to work correctly using UART0 and USB CDC ports.
It is necessary to make some changes to CMakeLists.txt from Arduino Component.

Instead of changing CMakeLists.txt from main/ or the from /, it is necessary to apply a change to the Line 224 of the components/arduino-esp32/CMakeLists.txt file:

target_compile_options(${COMPONENT_TARGET} PUBLIC
    -DARDUINO=10812
    -DARDUINO_${idf_target_for_macro}_DEV
    -DARDUINO_ARCH_ESP32
    -DARDUINO_BOARD="${idf_target_caps}_DEV"
    -DARDUINO_VARIANT="${CONFIG_ARDUINO_VARIANT}"
    -DARDUINO_USB_MODE=0
    -DARDUINO_USB_CDC_ON_BOOT=1
    -DCORE_DEBUG_LEVEL=4
    -DCONFIG_TINYUSB_CDC_ENABLED=1
    -DCONFIG_TINYUSB_ENABLED=1
    -DESP32)

This is the diff in CMakeLists.txt:

@@ -227,6 +227,11 @@ target_compile_options(${COMPONENT_TARGET} PUBLIC
     -DARDUINO_ARCH_ESP32
     -DARDUINO_BOARD="${idf_target_caps}_DEV"
     -DARDUINO_VARIANT="${CONFIG_ARDUINO_VARIANT}"
+    -DARDUINO_USB_MODE=0
+    -DARDUINO_USB_CDC_ON_BOOT=1
+    -DCORE_DEBUG_LEVEL=4
+    -DCONFIG_TINYUSB_CDC_ENABLED=1
+    -DCONFIG_TINYUSB_ENABLED=1
     -DESP32)

It is also necessary to add another component to the project in order to have TinyUSB available.
This shall be added as components/arduino_tinyusb/.
This shall be a copy of https://github.com/espressif/esp32-arduino-lib-builder/tree/master/components/arduino_tinyusb

After adding arduino_tinyusb component and changing the components/arduino-esp32/CMakeLists.txt file, it is possbible to build the project executing idf.py build.

@SuGlider
Copy link
Collaborator

I think that this is the necessary trick... there could be a better way to get it done, but this is how I made it.

@dzungpv
Copy link
Author

dzungpv commented Mar 14, 2024

I think that this is the necessary trick... there could be a better way to get it done, but this is how I made it.

Yes it work, Just follow you step in #9326 (comment)
But additional step to make it work:

Get tinyusb lib from https://github.com/hathach/tinyusb/releases and place in root folder off arduino_tinyusb components

This very hard process, you must add this to the document.

@SuGlider
Copy link
Collaborator

Get tinyusb lib from https://github.com/hathach/tinyusb/releases and place in root folder off arduino_tinyusb components

I didn't have to clone hathach/tinyusb. I've just cloned https://github.com/espressif/esp32-arduino-lib-builder and copied its esp32-arduino-lib-builder/components/arduino_tinyusb/ folder to myProj/components.

@SuGlider SuGlider added Type: For reference Common questions & problems Status: Solved and removed Status: Needs investigation We need to do some research before taking next steps on this issue labels Mar 14, 2024
@dzungpv
Copy link
Author

dzungpv commented Mar 14, 2024

Get tinyusb lib from https://github.com/hathach/tinyusb/releases and place in root folder off arduino_tinyusb components

I didn't have to clone hathach/tinyusb. I've just cloned https://github.com/espressif/esp32-arduino-lib-builder and copied its esp32-arduino-lib-builder/components/arduino_tinyusb/ folder to myProj/components.

It show me the error not found tinyusb lib in the arduino_tinyusb/tinyusb/ , event there is a tinyusb component in the IDF 4.4.6. So I search around and found @Jason2866 sample, so I get the fix like above.

@SuGlider
Copy link
Collaborator

@dzungpv - I'll close this ticket as it seems solved. Please feel free to open it again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Arduino as ESP-IDF component Issues about Arduino used as component in ESP-IDF Chip: ESP32-S2 Issue is related to support of ESP32-S2 Chip Status: Solved Type: For reference Common questions & problems
Projects
None yet
Development

No branches or pull requests

2 participants