Skip to content
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

ESP32-C3 native USB CDC stops workng when Wifi is used #6264

Closed
1 task done
ladyada opened this issue Feb 12, 2022 · 16 comments · Fixed by #6287
Closed
1 task done

ESP32-C3 native USB CDC stops workng when Wifi is used #6264

ladyada opened this issue Feb 12, 2022 · 16 comments · Fixed by #6287
Assignees
Labels
Area: ESP-IDF related ESP-IDF related issues

Comments

@ladyada
Copy link
Contributor

ladyada commented Feb 12, 2022

Board

ESP32-C3 dev board

Device Description

espressif ESP32-C3 devKitC-02-v1.1

Hardware Configuration

raw USB cable on pins 18, 19, 5V and gnd

Version

latest master

IDE Name

Arduino IDE

Operating System

Windows 10

Flash frequency

80MHz

PSRAM enabled

no

Upload speed

native

Description

A plain blink/serial print sketch will print to the usb native CDC port just fine.
Running anything with WiFi such as the Wifiscan example distributed with esp core, causes the USB CDC to disconnect and no longer enumerate until a hard reset
both blink/serial and wifiscan work ok if using the usb to serial CP2012n converter port.

Sketch

Working just fine either native or usb-serial port:

int led = 4;

void setup() {
  Serial.begin(115200);
  pinMode(led, OUTPUT);
}

int i=0;
void loop() {
  Serial.printf("hello %d!\n\r", i++);
  
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(100);               // wait for a second
}

Works only when using the cp2102n-usb-serial port. when enabling built in CDC on boot, USB port disappears once scan start:

/*
 *  This sketch demonstrates how to scan WiFi networks.
 *  The API is almost the same as with the WiFi Shield library,
 *  the most obvious difference being the different file you need to include:
 */
#include "WiFi.h"

void setup()
{
    Serial.begin(115200);

    // Set WiFi to station mode and disconnect from an AP if it was previously connected
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    delay(100);

    Serial.println("Setup done");
}

void loop()
{
    Serial.println("scan start");

    // WiFi.scanNetworks will return the number of networks found
    int n = WiFi.scanNetworks();
    Serial.println("scan done");
    if (n == 0) {
        Serial.println("no networks found");
    } else {
        Serial.print(n);
        Serial.println(" networks found");
        for (int i = 0; i < n; ++i) {
            // Print SSID and RSSI for each network found
            Serial.print(i + 1);
            Serial.print(": ");
            Serial.print(WiFi.SSID(i));
            Serial.print(" (");
            Serial.print(WiFi.RSSI(i));
            Serial.print(")");
            Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
            delay(10);
        }
    }
    Serial.println("");

    // Wait a bit before scanning again
    delay(5000);
}

Debug Message

monitoring the uart output when USB CDC on Boot is enabled, shows the core is still running, just USB task no longer active:

23:00:27.558 -> ESP-ROM:esp32c3-api1-20210207
23:00:27.558 -> Build:Feb  7 2021
23:00:27.558 -> rst:0x1 (POWERON),boot:0xc (SPI_FAST_FLASH_BOOT)
23:00:27.558 -> SPIWP:0xee
23:00:27.558 -> mode:DIO, clock div:1
23:00:27.558 -> load:0x3fcd6100,len:0x38c
23:00:27.558 -> load:0x403ce000,len:0x6a8
23:00:27.558 -> load:0x403d0000,len:0x2370
23:00:27.558 -> SHA-256 comparison failed:
23:00:27.605 -> Calculated: 912c8ece44dc10c27ac7d0fbc21c881181838b5f87f42e05d6797a997e755775
23:00:27.605 -> Expected: e48257ed72b4ed90fbd1c7429f3866f76aab94952c8871b8e364f6e503cebde8
23:00:27.605 -> Attempting to boot anyway...
23:00:27.605 -> entry 0x403ce000
23:00:27.749 -> [   170][D][WiFiGeneric.cpp:831] _eventCallback(): Arduino Event: 0 - WIFI_READY
23:00:27.749 -> [   209][V][WiFiGeneric.cpp:272] _arduino_event_cb(): STA Started
23:00:27.796 -> [   210][D][WiFiGeneric.cpp:831] _eventCallback(): Arduino Event: 2 - STA_START
23:00:30.681 -> [  3135][V][WiFiGeneric.cpp:306] _arduino_event_cb(): SCAN Done: ID: 128, Status: 0, Results: 10
23:00:30.681 -> [  3136][D][WiFiGeneric.cpp:831] _eventCallback(): Arduino Event: 1 - SCAN_DONE
...

last message on usb CDC before disappearing is
23:04:22.557 -> [ 5170][D][WiFiGeneric.cpp:831] _eventCallback(): Arduino Event: 0 - WIFI_READY

Other Steps to Reproduce

running micropython on an esp32-c3 with no cp2102n (only native usb cdc), wifi scan works just fine so it does not appear to be a hardware issue.

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

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@ladyada ladyada added the Status: Awaiting triage Issue is waiting for triage label Feb 12, 2022
@me-no-dev
Copy link
Member

@Spritetm PTAL :)

@dronecz
Copy link

dronecz commented Feb 14, 2022

I have same issue with support >2.0.0 . It is working in 2.0.0 . I tried WiFiScan & WiFiClientBasic sketch in Arduino 1.8.13 and 1.8.19

Here is USB info when I´m not using WiFi:
273477186_694392891728137_23442693498025922_n

and here is when I´m using WiFi:
273541236_658675642110969_5502742059488279544_n.

@ladyada
Copy link
Contributor Author

ladyada commented Feb 14, 2022

oh good idea, yes i can verify it works in 2.0.0

17:57:44.411 -> I (239) esp_image: segment 4: paddr=000b5844 vaddr=40389b98 size[  4282][D][WiFiGeneric.cpp:808] _eventCallback(): Arduino Event: 0 - WIFI_READY
17:57:44.550 -> [  4396][V][WiFiGeneric.cpp:272] _arduino_event_cb(): STA Started
17:57:44.550 -> [  4397][D][WiFiGeneric.cpp:808] _eventCallback(): Arduino Event: 2 - STA_START
17:57:44.644 -> Setup done
17:57:44.644 -> scan start
17:57:49.538 -> [  9410][V][WiFiGeneric.cpp:306] _arduino_event_cb(): SCAN Done: ID: 128, Status: 0, Results: 6
17:57:49.538 -> [  9410][D][WiFiGeneric.cpp:808] _eventCallback(): Arduino Event: 1 - SCAN_DONE
17:57:49.538 -> scan done
17:57:49.538 -> 6 networks found
...

@me-no-dev
Copy link
Member

We are looking into the issue. I hope to have results by tomorrow

@Spritetm
Copy link
Member

Looks like it's an ESP-IDF (4.4) issue. Long story short: ESP-IDF disables the USB PHY when WiFi is started if it thinks it's not being used, as it has a very slight impact (-1dB or so) on three of the WiFi channels. It seems it's a bit too pro-active on that, and it also disables it when the USB-serial-JTAG device is selected as a secondary console, which it is in the Arduino default setup. This, as the meme goes, kills the USB.

We'll get this fixed in ESP-IDF. Meanwhile, here's a workaround you can use:

#include "WiFi.h"                                                                                         

//there's an include for this but it doesn't define the function if it doesn't think it needs it, so manually declare the function
extern "C" void phy_bbpll_en_usb(bool en);

void setup()
{
    [...]
    WiFi.mode(WIFI_STA); //or any other statement that initializes the WiFi subsystem
    phy_bbpll_en_usb(true); //this brings the USB serial-jtag back to life. Suggest doing this immediately after wifi startup.
    [...]
}

@me-no-dev
Copy link
Member

Could you please try #6287 and see if that fixes the issue? cc @Jason2866

@dronecz
Copy link

dronecz commented Feb 15, 2022

I did test this fix with 2.0.1 and 2.0.2 and I can confirm that issue with USB enumeration is gone.

@me-no-dev
Copy link
Member

Thanks for confirming @dronecz . I am merging the fix :)

@ladyada
Copy link
Contributor Author

ladyada commented Feb 16, 2022

also verified, wifi scan works great now! still having some SSID connection issues (oddly enough, works great and fast in micropython but not arduino?) but will open another issue if i can figure out whats up

@me-no-dev
Copy link
Member

@ladyada probably something in sdkconfig? Could you compare with micropython?

Jason2866 added a commit to Jason2866/arduino-esp32 that referenced this issue Mar 16, 2022
* Fix wdt OTA update

* Fix ESP32-Solo WDT on HTTP OTA update

Co-authored-by: Theo Arends <11044339+arendst@users.noreply.github.com>

* Fix ESP32-Solo WDT on HTTP OTA update

* Fix ESP32-Solo WDT on HTTP OTA update

Co-authored-by: Theo Arends <11044339+arendst@users.noreply.github.com>

* IDF master c69f0ec32

* Fix WDT only for solo core

* IDF master 58022f859

* IDF master 606557b48

* IDF master 8131d6f46

* IDF master 59aa60d52

* IDF master 83956ebba

* IDF master 83956ebbae

* IDF master b1c3ee71c5

* IDF master 2c49af9e75

* IDF master eb1a66e7c9

* IDF master a20df743f1

* Fix build compilation due to changes in the HW_TIMER's structs

* Fix compilation warnings and errors with USB

* Update USBCDC.cpp

* Update CMakeLists.txt

* IDF master b63ec47238

* Update libarduino_tinyusb.a

* Update HWCDC.cpp

* IDF master 5f38b766a8

* IDF master b1f851b8f8

* IDF master 61299f879e

* Delete tools/sdk/esp32/include/coap directory

* Delete tools/sdk/esp32c3/include/coap directory

* Delete tools/sdk/esp32s2/include/coap directory

* solve conflicts

* force Dynamic Buffer instead of Static ones

* Remove debug if

* Resolve conflicts

* Revert espressif#5861

* add GPIO defines for C3

* Edit sd_diskio to check card status

* Update CMakeLists.txt

* Delete libraries/RainMaker directory

* Update README.md

* Update README.md

* Revert "RMT refactor"

* Fixes Lib Builder compiling errors

* Fixes Lib Builder compiling errors

* Revert "Revert "RMT refactor""

* Update esp32-hal-rmt.c

* Update esp32-hal-rmt.c

* Update esp32-hal-rmt.c

* Update esp32-hal-rmt.c

* Update esp32-hal-gpio.c

* Bugfix of ff_sd_status

* Fix I2C Slave Compile

I2C Slave currently doesn't compile for projects where Arduino is an IDF component.  This adds missing conditionals.

* add missing c3 define

when disable HAL lock is set

* add ESP32C3 `esp32_adc2gpio`

* Fixes UART MODBUS and Loopback issue

* Update esp32-hal-uart.c

* Revert "Fixing interrupts in LEDC (espressif#6160)"

This reverts commit a593206.

* Update esp32-hal.h

* use Tasmota github

* Use Tasmota platform for CI

* tasmota eaptool v3.2

* add missing c3 define

* enable SPI hal for S3

* revert enable spi for S3

* Update Esp.cpp

* Update Esp.cpp

* fix S3 uart gpios

* Update HardwareSerial.cpp

* Update Esp.cpp

* Update Esp.cpp

* Bootloader is at 0x0000 for S3

* Update esp32-hal-cpu.c

* Update esp32-hal-spi.c

* Update install-platformio-esp32.sh

* Fix replace() failing

* Update CMakeLists.txt

* remove Rainmaker

* Update CMakeLists.txt

* try to find lib builder fail

* Update CMakeLists.txt

* Update idf_component.yml

* Update esp32-hal-ledc.c

* Update esp32-hal-gpio.c

* Update esp32-hal-gpio.h

* Update boards.txt

* Update idf_component.yml

* Update esp32-hal-gpio.c

* Update esp32-hal-ledc.c

* Update idf_component.yml

* Add files via upload

* Update HTTPClient.cpp

* Update HTTPClient.h

* Fixes RMT examples and adds ESP32-S3 config

* Fixes RMT examples and adds ESP32-S3 config

* fix compile

* fix compile 2

* fix compile 3

* revert cookie changes

* revert cookie jar support

* cookie jar

* cookie jar

* Support mbedtls 2.28.x

* Update esp32c3.rom.ld

* GPIO refactoring

GPIO now using ESP-IDF API on all chips.
LEDC interrupt fix removed - no longer needed.
Edited pins_arduino.h in variants according to changes in gpio.

* Update esp32-hal-gpio.c

* Update esp32-hal-ledc.c

* Update HTTPClient.cpp

* Update ssl_client.cpp

* Update boards.txt

* Update esp32-hal-gpio.c

* Update esp32-hal-ledc.c

* Edited analog channels functions

* Update esp32-hal-gpio.c

* Update esp32-hal-gpio.h

* Update esp32-hal-ledc.c

* Fix CDC+JTAG is disabled when WiFi is used on ESP32-C3

Fixes: espressif#6264
Thanks @Spritetm

* ADC esp32s2 attenuation fix for DAC pins

* Revert "ADC esp32s2 attenuation fix for DAC pins"

* revert6134

espressif#6134

* revert2

* revert3

* revert

* Update HardwareSerial.cpp

* Update HardwareSerial.h

* Update esp32-hal-uart.c

* Update esp32-hal-uart.h

* S3

* Update esp32-hal-adc.c

* Fix ethenet

* align with arduino upstream

* Adds C++ std::function to Serial.onReceive() PR6364 upstream

* Update esp32-hal-uart.c

* Update HardwareSerial.cpp

* Update esp32-hal-uart.c

* Update boards.txt

* Update HardwareSerial.cpp

* Update HardwareSerial.h

* Update WiFiAP.cpp

* Update WiFiGeneric.cpp

* Update WiFiGeneric.h

* Update WiFiSTA.cpp

* Update WiFiSTA.h

* Update HardwareSerial.cpp

* Update HardwareSerial.h

* Delete libraries/RainMaker directory

* remove rainmaker

* Fixes USB CDC setRxBufferSize(), begin(), _onRX()

* Fixes SetRxBufferSize(0) with end()

* Fixes reset when 2x call to end()

* publish.yml: Limit the running scope of the publish Workflow. (espressif#6428)

1. Don't run the publish test result workflow on the master
branch.
2. Run only on Pull Requests to be able to publish the result as a PR comment.
3. Avoid running when the triggering workflow was skipped, this will
   cause a failure as no file will be uploaded.

Signed-off-by: Abdelatif Guettouche <abdelatif.guettouche@espressif.com>

* Add missing include in AsyncUDP.h (espressif#6412)

In my project I'm getting the error 
```
In file included from lib/Discovery/Discovery.cpp:2:
C:/Users/David/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src/AsyncUDP.h:47:1: error: expected class-name before '{' token
```

Adding a reference to Stream.h fixes it.

* Some board variant fixes (espressif#6411)

* make work with rev C pcb

* use #define for easy testing

* Added another overloaded WiFiSTAClass::begin() function that provides… (espressif#6398)

Summary

The examples demonstrate how to create a WPA2 Enterprise connection, but it requires using various direct esp_idf functions. This patch is intended to create another overloaded version of the WiFi.begin() function that allows a user to create a WPA2 Enterprise connection in much the same way as different kinds of connections.

My only question for the core maintainers is whether I should leave those #ifdef's in there. I added them so that it was easy to disable all the code I added via defines from my platformio.ini file, but they technically aren't necessary.

Impact

This should make it easier for novice users to create WPA2 Enterprise connections. For my university, I didn't need a root certificate or the client certificate or client key, so I haven't been able to debug those scenarios, but I built the begin functions to allow any one of those to be used, if needed.

I can confirm that eduroam-style WPA2 Enterprise networks that only require authentication with a username and password works as expected.

* Fix boot freeze when trying to init PSRAM on Pico D4

* Revert "Added another overloaded WiFiSTAClass::begin() function that provides… (espressif#6398)"

This reverts commit d977359.

* fix rainmaker merge

Co-authored-by: Theo Arends <11044339+arendst@users.noreply.github.com>
Co-authored-by: me-no-dev <me-no-dev@users.noreply.github.com>
Co-authored-by: me-no-dev <hristo@espressif.com>
Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Co-authored-by: Jason2866 <jason2866@github.com>
Co-authored-by: Rodrigo Garcia <Rodrigo.Garcia@espressif.com>
Co-authored-by: mrengineer7777 <44048235+mrengineer7777@users.noreply.github.com>
Co-authored-by: s-hadinger <49731213+s-hadinger@users.noreply.github.com>
Co-authored-by: Abdelatif Guettouche <abdelatif.guettouche@espressif.com>
Co-authored-by: Limor "Ladyada" Fried <limor@ladyada.net>
Co-authored-by: John P. Swensen <jpswensen@gmail.com>
Co-authored-by: Stephan Hadinger <stephan.hadinger@gmail.com>
@kostuch
Copy link

kostuch commented Apr 5, 2022

Hi,
I faced similar situation, when using BLE on ESP32-C3.
Connections and cabling are fine (if I flash chip with simple blink code, everything is ok).
But when I use code which connect to remote BLE device, the built-in USB become unavailable.
Device manager on windows then shows it as unknown device with message "A request for the USB device descriptor failed."
Normally (when ESP is flashed with simple blink) it shows COM port without problems.
Tried to add phy_bbpll_en_usb(true); after ble init, but it didn't help.
Any clue?

@emeb
Copy link

emeb commented Apr 8, 2022

Seeing a similar problem in ESP-IDF V5.0 with WiFi. Adding "phy_bbpll_en_usb(true);" after WiFi is started up doesn't result in working USB on the ESP32C3. Any suggestions on how to bypass this?

@emeb
Copy link

emeb commented Apr 10, 2022

I think I've got it figured out - the key is to add the phy_bbpll_en_usb(true); call prior to any wifi/network setup. Seems it's a flag to the OS that the USB shouldn't be disabled, rather than a command to enable it after it's been turned off.

@me-no-dev
Copy link
Member

thanks @emeb I'll give this a go.

@kostuch
Copy link

kostuch commented Apr 11, 2022

thanks @emeb I'll give this a go.

Could you also check described BLE-USB conflict, please?

arendst added a commit to arendst/Tasmota that referenced this issue Apr 11, 2022
@DreamPWJ
Copy link

phy_bbpll_en_usb(true); doesn't work, is there any other solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: ESP-IDF related ESP-IDF related issues
Projects
Development

Successfully merging a pull request may close this issue.

8 participants