From d311144a00762deb023a73e2580fd0beeed78ed0 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 17 Apr 2020 18:56:09 +0200 Subject: [PATCH] soc/usb: use new headers in LL, move some code out of LL * Keep only USB related register operations in the LL. * Move pad-related logic into the driver. * Driver is now responsible for enabling the peripheral. --- .../soc/src/esp32s2/include/hal/usb_ll.h | 58 +++++++------------ components/soc/src/esp32s2/usb_hal.c | 7 ++- components/tinyusb/CMakeLists.txt | 2 +- components/tinyusb/port/esp32s2/src/tinyusb.c | 33 +++++++++++ 4 files changed, 60 insertions(+), 40 deletions(-) diff --git a/components/soc/src/esp32s2/include/hal/usb_ll.h b/components/soc/src/esp32s2/include/hal/usb_ll.h index 001fa071a..5496d7a6d 100644 --- a/components/soc/src/esp32s2/include/hal/usb_ll.h +++ b/components/soc/src/esp32s2/include/hal/usb_ll.h @@ -15,46 +15,28 @@ #include "soc/soc.h" #include "soc/system_reg.h" -#include "esp32s2/rom/gpio.h" #include "soc/gpio_sig_map.h" #include "soc/usb_periph.h" -void gpio_pad_input_enable(uint32_t pin); - -static inline void usb_ll_init(bool external_phy) +static inline void usb_ll_int_phy_enable(void) { - REG_SET_BIT(DPORT_PERIP_CLK_EN0_REG, DPORT_USB_CLK_EN); - REG_CLR_BIT(DPORT_PERIP_RST_EN0_REG, DPORT_USB_RST); - - if (external_phy) { - REG_SET_BIT(DR_REG_USB_WRAP_BASE, BIT(18)); //set usb_pad_enable - REG_SET_BIT(DR_REG_USB_WRAP_BASE, BIT(2)); // set reg_phy_sel (external phy) - gpio_output_set_high(0x10, 0, 0x1E, 0xE); - } else { - REG_SET_BIT(DR_REG_USB_WRAP_BASE, BIT(18)); //set usb_pad_enable - REG_CLR_BIT(DR_REG_USB_WRAP_BASE, BIT(2)); // clear reg_phy_sel (internal phy) - //drive strength needs to be 3 for full speed - REG_SET_FIELD(GPIO_PIN19_REG, GPIO_PIN19_PAD_DRIVER, 3); - REG_SET_FIELD(GPIO_PIN20_REG, GPIO_PIN20_PAD_DRIVER, 3); - } - - int i = 0; - while (usb_periph_iopins[i].pin != -1) { - if ((external_phy) || (usb_periph_iopins[i].ext_phy_only == 0)) { - gpio_pad_select_gpio(usb_periph_iopins[i].pin); - if (usb_periph_iopins[i].is_output) { - gpio_matrix_out(usb_periph_iopins[i].pin, usb_periph_iopins[i].func, false, false); - } else { - gpio_matrix_in(usb_periph_iopins[i].pin, usb_periph_iopins[i].func, false); - gpio_pad_input_enable(usb_periph_iopins[i].pin); - } - gpio_pad_unhold(usb_periph_iopins[i].pin); - } - i++; - } - REG_SET_BIT(DR_REG_USB_WRAP_BASE, BIT(12)); //pull override - REG_SET_BIT(DR_REG_USB_WRAP_BASE, BIT(13)); //dp pullup - REG_CLR_BIT(DR_REG_USB_WRAP_BASE, BIT(14)); //dp pulldwn - REG_CLR_BIT(DR_REG_USB_WRAP_BASE, BIT(15)); //dm pullup - REG_CLR_BIT(DR_REG_USB_WRAP_BASE, BIT(16)); //dm pulldown + USB_WRAP.otg_conf.pad_enable = 1; + USB_WRAP.otg_conf.phy_sel = 0; +} + +static inline void usb_ll_ext_phy_enable(void) +{ + USB_WRAP.otg_conf.pad_enable = 1; + USB_WRAP.otg_conf.phy_sel = 1; +} + +static inline void usb_ll_int_phy_pullup_conf(bool dp_pu, bool dp_pd, bool dm_pu, bool dm_pd) +{ + usb_wrap_otg_conf_reg_t conf = USB_WRAP.otg_conf; + conf.pad_pull_override = 1; + conf.dp_pullup = dp_pu; + conf.dp_pulldown = dp_pd; + conf.dm_pullup = dm_pu; + conf.dm_pulldown = dp_pd; + USB_WRAP.otg_conf = conf; } diff --git a/components/soc/src/esp32s2/usb_hal.c b/components/soc/src/esp32s2/usb_hal.c index f3cdbeed4..c0188804c 100644 --- a/components/soc/src/esp32s2/usb_hal.c +++ b/components/soc/src/esp32s2/usb_hal.c @@ -18,5 +18,10 @@ void usb_hal_init(usb_hal_context_t *usb) { - usb_ll_init(usb->use_external_phy); + if (usb->use_external_phy) { + usb_ll_ext_phy_enable(); + } else { + usb_ll_int_phy_enable(); + usb_ll_int_phy_pullup_conf(true, false, false, false); + } } diff --git a/components/tinyusb/CMakeLists.txt b/components/tinyusb/CMakeLists.txt index aff0b97ec..fc8dd5019 100644 --- a/components/tinyusb/CMakeLists.txt +++ b/components/tinyusb/CMakeLists.txt @@ -1,4 +1,4 @@ -idf_component_register( REQUIRES esp_rom freertos soc) +idf_component_register(REQUIRES esp_rom freertos soc driver) if(CONFIG_USB_ENABLED) idf_component_get_property( FREERTOS_ORIG_INCLUDE_PATH freertos ORIG_INCLUDE_PATH) diff --git a/components/tinyusb/port/esp32s2/src/tinyusb.c b/components/tinyusb/port/esp32s2/src/tinyusb.c index 1e46cf24d..6c5792a99 100644 --- a/components/tinyusb/port/esp32s2/src/tinyusb.c +++ b/components/tinyusb/port/esp32s2/src/tinyusb.c @@ -14,6 +14,35 @@ #include "tinyusb.h" #include "hal/usb_hal.h" +#include "soc/usb_periph.h" +#include "driver/periph_ctrl.h" +#include "driver/gpio.h" +#include "esp32s2/rom/gpio.h" + +static void configure_pins(usb_hal_context_t *usb) +{ + /* usb_periph_iopins currently configures USB_OTG as USB Device. + * Introduce additional parameters in usb_hal_context_t when adding support + * for USB Host. + */ + for (const usb_iopin_dsc_t* iopin = usb_periph_iopins; iopin->pin != -1; ++iopin) { + if ((usb->use_external_phy) || (iopin->ext_phy_only == 0)) { + gpio_pad_select_gpio(iopin->pin); + if (iopin->is_output) { + gpio_matrix_out(iopin->pin, iopin->func, false, false); + } else { + gpio_matrix_in(iopin->pin, iopin->func, false); + gpio_pad_input_enable(iopin->pin); + } + gpio_pad_unhold(iopin->pin); + } + } + if (!usb->use_external_phy) { + gpio_set_drive_capability(USBPHY_DP_NUM, GPIO_DRIVE_CAP_3); + gpio_set_drive_capability(USBPHY_DP_NUM, GPIO_DRIVE_CAP_3); + } +} + /** * @brief Initializes the tinyUSB driver. @@ -31,11 +60,15 @@ esp_err_t tinyusb_driver_install(const tinyusb_config_t *config) tusb_desc_device_t *descriptor; char **string_descriptor; + periph_module_reset(PERIPH_USB_MODULE); + periph_module_enable(PERIPH_USB_MODULE); + // Hal init usb_hal_context_t hal = { .use_external_phy = config->external_phy }; usb_hal_init(&hal); + configure_pins(&hal); if (config->descriptor == NULL) { descriptor = &descriptor_kconfig;