Open FFBoard
Open source force feedback firmware
dwc2_esp32.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2021, Ha Thach (tinyusb.org)
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 * This file is part of the TinyUSB stack.
25 */
26
27
28#ifndef TUSB_DWC2_ESP32_H_
29#define TUSB_DWC2_ESP32_H_
30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
35#include "freertos/FreeRTOS.h"
36#include "freertos/task.h"
37
38#include "esp_intr_alloc.h"
39#include "soc/periph_defs.h"
40#include "soc/usb_wrap_struct.h"
41
42#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
43#define DWC2_FS_REG_BASE 0x60080000UL
44#define DWC2_EP_MAX 7
45
46static const dwc2_controller_t _dwc2_controller[] = {
47 { .reg_base = DWC2_FS_REG_BASE, .irqnum = ETS_USB_INTR_SOURCE, .ep_count = 7, .ep_in_count = 5, .ep_fifo_size = 1024 }
48};
49
50#elif TU_CHECK_MCU(OPT_MCU_ESP32P4)
51#define DWC2_FS_REG_BASE 0x50040000UL
52#define DWC2_HS_REG_BASE 0x50000000UL
53#define DWC2_EP_MAX 16
54
55// On ESP32 for consistency we associate
56// - Port0 to OTG_FS, and Port1 to OTG_HS
57static const dwc2_controller_t _dwc2_controller[] = {
58{ .reg_base = DWC2_FS_REG_BASE, .irqnum = ETS_USB_OTG11_CH0_INTR_SOURCE, .ep_count = 7, .ep_in_count = 5, .ep_fifo_size = 1024 },
59{ .reg_base = DWC2_HS_REG_BASE, .irqnum = ETS_USB_OTG_INTR_SOURCE, .ep_count = 16, .ep_in_count = 8, .ep_fifo_size = 4096 }
60};
61#endif
62
63//--------------------------------------------------------------------+
64//
65//--------------------------------------------------------------------+
66static intr_handle_t usb_ih[TU_ARRAY_SIZE(_dwc2_controller)];
67
68static void dwc2_int_handler_wrap(void* arg) {
69 const uint8_t rhport = tu_u16_low((uint16_t)(uintptr_t)arg);
70 const tusb_role_t role = (tusb_role_t) tu_u16_high((uint16_t)(uintptr_t)arg);
71#if CFG_TUD_ENABLED
72 if (role == TUSB_ROLE_DEVICE) {
73 dcd_int_handler(rhport);
74 }
75#endif
76#if CFG_TUH_ENABLED
77 if (role == TUSB_ROLE_HOST) {
78 hcd_int_handler(rhport, true);
79 }
80#endif
81}
82
83TU_ATTR_ALWAYS_INLINE static inline void dwc2_int_set(uint8_t rhport, tusb_role_t role, bool enabled) {
84 if (enabled) {
85 esp_intr_alloc(_dwc2_controller[rhport].irqnum, ESP_INTR_FLAG_LOWMED,
86 dwc2_int_handler_wrap, (void*)(uintptr_t)tu_u16(role, rhport), &usb_ih[rhport]);
87 } else {
88 esp_intr_free(usb_ih[rhport]);
89 }
90}
91
92#define dwc2_dcd_int_enable(_rhport) dwc2_int_set(_rhport, TUSB_ROLE_DEVICE, true)
93#define dwc2_dcd_int_disable(_rhport) dwc2_int_set(_rhport, TUSB_ROLE_DEVICE, false)
94
95TU_ATTR_ALWAYS_INLINE static inline void dwc2_remote_wakeup_delay(void) {
96 vTaskDelay(pdMS_TO_TICKS(1));
97}
98
99// MCU specific PHY init, called BEFORE core reset
100TU_ATTR_ALWAYS_INLINE static inline void dwc2_phy_init(dwc2_regs_t* dwc2, uint8_t hs_phy_type) {
101 (void)dwc2;
102 (void)hs_phy_type;
103 // maybe usb_utmi_hal_init()
104
105}
106
107// MCU specific PHY update, it is called AFTER init() and core reset
108TU_ATTR_ALWAYS_INLINE static inline void dwc2_phy_update(dwc2_regs_t* dwc2, uint8_t hs_phy_type) {
109 (void)dwc2;
110 (void)hs_phy_type;
111 // maybe usb_utmi_hal_disable()
112}
113
114#ifdef __cplusplus
115}
116#endif
117
118#endif
void dcd_int_handler(uint8_t rhport)
Definition: dcd_ft9xx.c:954
static TU_ATTR_ALWAYS_INLINE void dwc2_int_set(uint8_t rhport, tusb_role_t role, bool enabled)
Definition: dwc2_esp32.h:83
static const dwc2_controller_t _dwc2_controller[]
Definition: dwc2_esp32.h:57
static TU_ATTR_ALWAYS_INLINE void dwc2_remote_wakeup_delay(void)
Definition: dwc2_esp32.h:95
static TU_ATTR_ALWAYS_INLINE void dwc2_phy_init(dwc2_regs_t *dwc2, uint8_t hs_phy_type)
Definition: dwc2_esp32.h:100
static intr_handle_t usb_ih[TU_ARRAY_SIZE(_dwc2_controller)]
Definition: dwc2_esp32.h:66
static void dwc2_int_handler_wrap(void *arg)
Definition: dwc2_esp32.h:68
static TU_ATTR_ALWAYS_INLINE void dwc2_phy_update(dwc2_regs_t *dwc2, uint8_t hs_phy_type)
Definition: dwc2_esp32.h:108
void hcd_int_handler(uint8_t rhport, bool in_isr)
Definition: hcd_max3421.c:1002
static TU_ATTR_ALWAYS_INLINE uint8_t tu_u16_low(uint16_t ui16)
Definition: tusb_common.h:146
static TU_ATTR_ALWAYS_INLINE uint8_t tu_u16_high(uint16_t ui16)
Definition: tusb_common.h:145
static TU_ATTR_ALWAYS_INLINE uint16_t tu_u16(uint8_t high, uint8_t low)
Definition: tusb_common.h:133