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

Adds USB to Peripheral Manager - Arduino Core 3.0.0 #8335

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions cores/esp32/HWCDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3

#include "esp32-hal.h"
#include "esp32-hal-periman.h"
#include "HWCDC.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "freertos/ringbuf.h"
#include "esp_intr_alloc.h"
#include "soc/periph_defs.h"
#include "soc/io_mux_reg.h"
#pragma GCC diagnostic ignored "-Wvolatile"
#include "hal/usb_serial_jtag_ll.h"
#pragma GCC diagnostic warning "-Wvolatile"
Expand Down Expand Up @@ -171,6 +173,28 @@ void HWCDC::onEvent(arduino_hw_cdc_event_t event, esp_event_handler_t callback){
arduino_hw_cdc_event_handler_register_with(ARDUINO_HW_CDC_EVENTS, event, callback, this);
}

bool HWCDC::deinit(void * busptr)
{
// avoid any recursion issue with Peripheral Manager perimanSetPinBus() call
static bool running = false;
if (running) return true;
running = true;
// Setting USB D+ D- pins
bool retCode = true;
retCode &= perimanSetPinBus(USB_DM_GPIO_NUM, ESP32_BUS_TYPE_INIT, NULL);
retCode &= perimanSetPinBus(USB_DP_GPIO_NUM, ESP32_BUS_TYPE_INIT, NULL);
if (retCode) {
// Force the host to re-enumerate (BUS_RESET)
pinMode(USB_DM_GPIO_NUM, OUTPUT_OPEN_DRAIN);
pinMode(USB_DP_GPIO_NUM, OUTPUT_OPEN_DRAIN);
digitalWrite(USB_DM_GPIO_NUM, LOW);
digitalWrite(USB_DP_GPIO_NUM, LOW);
}
// release the flag
running = false;
return retCode;
}

void HWCDC::begin(unsigned long baud)
{
if(tx_lock == NULL) {
Expand All @@ -187,6 +211,14 @@ void HWCDC::begin(unsigned long baud)
end();
return;
}
if (perimanSetBusDeinit(ESP32_BUS_TYPE_USB, HWCDC::deinit)) {
// Setting USB D+ D- pins
perimanSetPinBus(USB_DM_GPIO_NUM, ESP32_BUS_TYPE_USB, (void *) this);
perimanSetPinBus(USB_DP_GPIO_NUM, ESP32_BUS_TYPE_USB, (void *) this);
} else {
log_e("Serial JTAG Pins can't be set into Peripheral Manager.");
}

usb_serial_jtag_ll_txfifo_flush();
}

Expand All @@ -206,6 +238,7 @@ void HWCDC::end()
esp_event_loop_delete(arduino_hw_cdc_event_loop_handle);
arduino_hw_cdc_event_loop_handle = NULL;
}
HWCDC::deinit(this);
}

void HWCDC::setTxTimeoutMs(uint32_t timeout){
Expand Down
3 changes: 3 additions & 0 deletions cores/esp32/HWCDC.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ typedef union {

class HWCDC: public Stream
{
private:
static bool deinit(void * busptr);

public:
HWCDC();
~HWCDC();
Expand Down
13 changes: 13 additions & 0 deletions cores/esp32/esp32-hal-tinyusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "esp_rom_gpio.h"

#include "esp32-hal.h"
#include "esp32-hal-periman.h"

#include "esp32-hal-tinyusb.h"
#if CONFIG_IDF_TARGET_ESP32S2
Expand All @@ -56,6 +57,11 @@ typedef struct {
bool external_phy;
} tinyusb_config_t;

static bool usb_otg_deinit(void * busptr) {
// Once USB OTG is initialized, its GPIOs are assigned and it shall never be deinited
return false;
}

static void configure_pins(usb_hal_context_t *usb)
{
for (const usb_iopin_dsc_t *iopin = usb_periph_iopins; iopin->pin != -1; ++iopin) {
Expand All @@ -75,6 +81,13 @@ static void configure_pins(usb_hal_context_t *usb)
if (!usb->use_external_phy) {
gpio_set_drive_capability(USBPHY_DM_NUM, GPIO_DRIVE_CAP_3);
gpio_set_drive_capability(USBPHY_DP_NUM, GPIO_DRIVE_CAP_3);
if (perimanSetBusDeinit(ESP32_BUS_TYPE_USB, usb_otg_deinit)) {
// Bus Pointer is not used anyway - once the USB GPIOs are assigned, they can't be detached
perimanSetPinBus(USBPHY_DM_NUM, ESP32_BUS_TYPE_USB, (void *) usb);
perimanSetPinBus(USBPHY_DP_NUM, ESP32_BUS_TYPE_USB, (void *) usb);
} else {
log_e("USB OTG Pins can't be set into Peripheral Manager.");
}
}
}

Expand Down