|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2019, hathach for Adafruit |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#include "tusb_option.h" |
| 26 | + |
| 27 | +#if defined(ARDUINO_ARCH_ESP32) && TUSB_OPT_DEVICE_ENABLED |
| 28 | + |
| 29 | +#include "sdkconfig.h" |
| 30 | + |
| 31 | +#include "soc/soc.h" |
| 32 | +#include "soc/efuse_reg.h" |
| 33 | +#include "soc/rtc_cntl_reg.h" |
| 34 | +#include "soc/usb_struct.h" |
| 35 | +#include "soc/usb_reg.h" |
| 36 | +#include "soc/usb_wrap_reg.h" |
| 37 | +#include "soc/usb_wrap_struct.h" |
| 38 | +#include "soc/usb_periph.h" |
| 39 | +#include "soc/periph_defs.h" |
| 40 | +#include "soc/timer_group_struct.h" |
| 41 | +#include "soc/system_reg.h" |
| 42 | + |
| 43 | +#include "hal/usb_hal.h" |
| 44 | + |
| 45 | +// compiler error with gpio_ll_get_drive_capability() |
| 46 | +// invalid conversion from 'uint32_t' {aka 'unsigned int'} to 'gpio_drive_cap_t' [-fpermissive] |
| 47 | +// #include "hal/gpio_ll.h" |
| 48 | + |
| 49 | +#include "freertos/FreeRTOS.h" |
| 50 | +#include "freertos/task.h" |
| 51 | + |
| 52 | +#include "driver/gpio.h" |
| 53 | +#include "driver/periph_ctrl.h" |
| 54 | + |
| 55 | +#include "esp_efuse.h" |
| 56 | +#include "esp_efuse_table.h" |
| 57 | +#include "esp_rom_gpio.h" |
| 58 | + |
| 59 | +#include "esp32-hal.h" |
| 60 | + |
| 61 | +#include "esp32s2/rom/usb/usb_persist.h" |
| 62 | +#include "esp32s2/rom/usb/usb_dc.h" |
| 63 | +#include "esp32s2/rom/usb/chip_usb_dw_wrapper.h" |
| 64 | + |
| 65 | +#include "arduino/Adafruit_TinyUSB_API.h" |
| 66 | +#include "arduino/Adafruit_USBD_Device.h" |
| 67 | + |
| 68 | +//--------------------------------------------------------------------+ |
| 69 | +// MACRO TYPEDEF CONSTANT ENUM DECLARATION |
| 70 | +//--------------------------------------------------------------------+ |
| 71 | + |
| 72 | +#define USBD_STACK_SZ (4096) |
| 73 | + |
| 74 | +//--------------------------------------------------------------------+ |
| 75 | +// Forward USB interrupt events to TinyUSB IRQ Handler |
| 76 | +//--------------------------------------------------------------------+ |
| 77 | +// extern "C" void USBD_IRQHandler(void) { |
| 78 | +// tud_int_handler(0); |
| 79 | +//} |
| 80 | + |
| 81 | +//--------------------------------------------------------------------+ |
| 82 | +// Porting API |
| 83 | +//--------------------------------------------------------------------+ |
| 84 | + |
| 85 | +// USB Device Driver task |
| 86 | +// This top level thread processes all usb events and invokes callbacks |
| 87 | +static void usb_device_task(void *param) { |
| 88 | + (void)param; |
| 89 | + while(1) tud_task(); // RTOS forever loop |
| 90 | +} |
| 91 | + |
| 92 | +typedef enum { |
| 93 | + RESTART_NO_PERSIST, |
| 94 | + RESTART_PERSIST, |
| 95 | + RESTART_BOOTLOADER, |
| 96 | + RESTART_BOOTLOADER_DFU, |
| 97 | + RESTART_TYPE_MAX |
| 98 | +} restart_type_t; |
| 99 | + |
| 100 | +static bool usb_persist_enabled = false; |
| 101 | +static restart_type_t usb_persist_mode = RESTART_NO_PERSIST; |
| 102 | + |
| 103 | +static void IRAM_ATTR usb_persist_shutdown_handler(void) |
| 104 | +{ |
| 105 | + if(usb_persist_mode != RESTART_NO_PERSIST){ |
| 106 | + if (usb_persist_enabled) { |
| 107 | + usb_dc_prepare_persist(); |
| 108 | + } |
| 109 | + if (usb_persist_mode == RESTART_BOOTLOADER) { |
| 110 | + //USB CDC Download |
| 111 | + if (usb_persist_enabled) { |
| 112 | + chip_usb_set_persist_flags(USBDC_PERSIST_ENA); |
| 113 | + } else { |
| 114 | + periph_module_reset(PERIPH_USB_MODULE); |
| 115 | + periph_module_enable(PERIPH_USB_MODULE); |
| 116 | + } |
| 117 | + REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT); |
| 118 | + } else if (usb_persist_mode == RESTART_BOOTLOADER_DFU) { |
| 119 | + //DFU Download |
| 120 | + chip_usb_set_persist_flags(USBDC_BOOT_DFU); |
| 121 | + REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT); |
| 122 | + } else if (usb_persist_enabled) { |
| 123 | + //USB Persist reboot |
| 124 | + chip_usb_set_persist_flags(USBDC_PERSIST_ENA); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +static void configure_pins(usb_hal_context_t *usb) |
| 130 | +{ |
| 131 | + for (const usb_iopin_dsc_t *iopin = usb_periph_iopins; iopin->pin != -1; ++iopin) { |
| 132 | + if ((usb->use_external_phy) || (iopin->ext_phy_only == 0)) { |
| 133 | + esp_rom_gpio_pad_select_gpio(iopin->pin); |
| 134 | + if (iopin->is_output) { |
| 135 | + esp_rom_gpio_connect_out_signal(iopin->pin, iopin->func, false, false); |
| 136 | + } else { |
| 137 | + esp_rom_gpio_connect_in_signal(iopin->pin, iopin->func, false); |
| 138 | + if ((iopin->pin != GPIO_FUNC_IN_LOW) && (iopin->pin != GPIO_FUNC_IN_HIGH)) { |
| 139 | + // workaround for compiler error with including "hal/gpio_ll.h" |
| 140 | + // gpio_ll_input_enable(&GPIO, (gpio_num_t) iopin->pin); |
| 141 | + PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[iopin->pin]); |
| 142 | + } |
| 143 | + } |
| 144 | + esp_rom_gpio_pad_unhold(iopin->pin); |
| 145 | + } |
| 146 | + } |
| 147 | + if (!usb->use_external_phy) { |
| 148 | + gpio_set_drive_capability((gpio_num_t) USBPHY_DM_NUM, GPIO_DRIVE_CAP_3); |
| 149 | + gpio_set_drive_capability((gpio_num_t) USBPHY_DP_NUM, GPIO_DRIVE_CAP_3); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +void TinyUSB_Port_InitDevice(uint8_t rhport) { |
| 154 | + (void)rhport; |
| 155 | + |
| 156 | + // from esp32-hal_tinyusb |
| 157 | + bool usb_did_persist = (USB_WRAP.date.val == USBDC_PERSIST_ENA); |
| 158 | + |
| 159 | + //if(usb_did_persist && usb_persist_enabled){ |
| 160 | + // Enable USB/IO_MUX peripheral reset, if coming from persistent reboot |
| 161 | + REG_CLR_BIT(RTC_CNTL_USB_CONF_REG, RTC_CNTL_IO_MUX_RESET_DISABLE); |
| 162 | + REG_CLR_BIT(RTC_CNTL_USB_CONF_REG, RTC_CNTL_USB_RESET_DISABLE); |
| 163 | + //} else |
| 164 | + if(!usb_did_persist || !usb_persist_enabled){ |
| 165 | + // Reset USB module |
| 166 | + periph_module_reset(PERIPH_USB_MODULE); |
| 167 | + periph_module_enable(PERIPH_USB_MODULE); |
| 168 | + } |
| 169 | + |
| 170 | + esp_register_shutdown_handler(usb_persist_shutdown_handler); |
| 171 | + |
| 172 | + usb_hal_context_t hal = { |
| 173 | + .use_external_phy = false |
| 174 | + }; |
| 175 | + usb_hal_init(&hal); |
| 176 | + configure_pins(&hal); |
| 177 | + |
| 178 | + tusb_init(); |
| 179 | + |
| 180 | + // Create a task for tinyusb device stack |
| 181 | + xTaskCreate(usb_device_task, "usbd", USBD_STACK_SZ, NULL, configMAX_PRIORITIES - 1, NULL); |
| 182 | +} |
| 183 | + |
| 184 | +void TinyUSB_Port_EnterDFU(void) { |
| 185 | + // Reset to Bootloader |
| 186 | + usb_persist_mode = RESTART_BOOTLOADER; |
| 187 | + esp_restart(); |
| 188 | +} |
| 189 | + |
| 190 | +uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16]) { |
| 191 | + uint32_t *serial_32 = (uint32_t *)serial_id; |
| 192 | + |
| 193 | + /* Get the MAC address */ |
| 194 | + const uint32_t mac0 = __builtin_bswap32(REG_GET_FIELD(EFUSE_RD_MAC_SPI_SYS_0_REG, EFUSE_MAC_0)); |
| 195 | + const uint16_t mac1 = __builtin_bswap16((uint16_t) REG_GET_FIELD(EFUSE_RD_MAC_SPI_SYS_1_REG, EFUSE_MAC_1)); |
| 196 | + |
| 197 | + memcpy(serial_id, &mac1, 2); |
| 198 | + memcpy(serial_id+2, &mac0, 4); |
| 199 | + |
| 200 | + return 6; |
| 201 | +} |
| 202 | + |
| 203 | +extern "C" void yield(void) { |
| 204 | + TinyUSB_Device_FlushCDC(); |
| 205 | + vPortYield(); |
| 206 | +} |
| 207 | + |
| 208 | +#endif // USE_TINYUSB |
0 commit comments