ESP32-C3 How to know if USB console (GPIO18, GPIO19) is plugged in?

arnaudmz
Posts: 3
Joined: Sat Feb 26, 2022 1:25 pm

ESP32-C3 How to know if USB console (GPIO18, GPIO19) is plugged in?

Postby arnaudmz » Sat Feb 26, 2022 1:32 pm

Hi there.

I'd like to adapt the behaviour of my application depending on wether the native USB console is plugged or not.

Is there a software-based way to know this?
Do I have to use an extra GPIO plugged on the VUSB (with resistors to lower the voltage) ?

Regards,

Arnaud

ESP_Sprite
Posts: 9715
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-C3 How to know if USB console (GPIO18, GPIO19) is plugged in?

Postby ESP_Sprite » Sun Feb 27, 2022 5:10 am

I'm not sure if we have software for that, but the hardware makes the latest SOF received available in a register. While USB is connected, that number should increase at exactly once every millisecond. You could use that.

arnaudmz
Posts: 3
Joined: Sat Feb 26, 2022 1:25 pm

Re: ESP32-C3 How to know if USB console (GPIO18, GPIO19) is plugged in?

Postby arnaudmz » Mon Feb 28, 2022 3:39 pm

@ESP_Sprite: sounds good enough, I can live with this.

Could you provide a link to some documentation where to look for this register and give it a go?

Arnaud

arnaudmz
Posts: 3
Joined: Sat Feb 26, 2022 1:25 pm

Re: ESP32-C3 How to know if USB console (GPIO18, GPIO19) is plugged in?

Postby arnaudmz » Mon Feb 28, 2022 3:57 pm

Found it in the meantime:

Code: Select all

USB_SERIAL_JTAG_FRAM_NUM_REG
does the trick very well.

Thanks.

dongrigorio
Posts: 3
Joined: Thu Jan 19, 2023 12:52 pm

Re: ESP32-C3 How to know if USB console (GPIO18, GPIO19) is plugged in?

Postby dongrigorio » Tue Jan 31, 2023 6:54 am

For esp32-c3 returns zero if usb is not connected, otherwise returns a positive integer (10).

Code: Select all

int is_plugged_usb(void){
    uint32_t *aa = USB_SERIAL_JTAG_FRAM_NUM_REG;
    uint32_t first = *aa;
    vTaskDelay(pdMS_TO_TICKS(10));
    return (int) (*aa - first);
}

kscheff
Posts: 1
Joined: Thu Jun 20, 2024 2:47 pm

Re: ESP32-C3 How to know if USB console (GPIO18, GPIO19) is plugged in?

Postby kscheff » Wed Jul 10, 2024 8:58 am

With framework 5.2.1

Code: Select all

*(uint_32_t*)USB_SERIAL_JTAG_FRAM_NUM_REG
returns a 12-bit counter, counting down.
In my observation the counter runs in average with ~36.5ms.
When no USB was connect it shows 0, it stops counting down when USB is unplugged.

mano1979
Posts: 3
Joined: Sat Feb 24, 2018 2:45 am

Re: ESP32-C3 How to know if USB console (GPIO18, GPIO19) is plugged in?

Postby mano1979 » Wed Oct 16, 2024 10:42 am

dongrigorio wrote:
Tue Jan 31, 2023 6:54 am
For esp32-c3 returns zero if usb is not connected, otherwise returns a positive integer (10).

Code: Select all

int is_plugged_usb(void){
    uint32_t *aa = USB_SERIAL_JTAG_FRAM_NUM_REG;
    uint32_t first = *aa;
    vTaskDelay(pdMS_TO_TICKS(10));
    return (int) (*aa - first);
}
How to use this? I am trying to implement it in my arduino sketch, but i get errors.

Code: Select all

'USB_SERIAL_JTAG_FRAM_NUM_REG ' was not declared in this scope.
Where should it be placed?

Who is online

Users browsing this forum: taherrera and 63 guests