Open FFBoard
Open source force feedback firmware
dwc2_common.h
Go to the documentation of this file.
1/*
2* The MIT License (MIT)
3 *
4 * Copyright (c) 2024 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#ifndef TUSB_DWC2_COMMON_H
28#define TUSB_DWC2_COMMON_H
29
30#include "common/tusb_common.h"
31#include "dwc2_type.h"
32
33// Following symbols must be defined by port header
34// - _dwc2_controller[]: array of controllers
35// - DWC2_EP_MAX: largest EP counts of all controllers
36// - dwc2_phy_init/dwc2_phy_update: phy init called before and after core reset
37// - dwc2_dcd_int_enable/dwc2_dcd_int_disable
38// - dwc2_remote_wakeup_delay
39
40#if defined(TUP_USBIP_DWC2_STM32)
41 #include "dwc2_stm32.h"
42#elif defined(TUP_USBIP_DWC2_ESP32)
43 #include "dwc2_esp32.h"
44#elif TU_CHECK_MCU(OPT_MCU_GD32VF103)
45 #include "dwc2_gd32.h"
46#elif TU_CHECK_MCU(OPT_MCU_BCM2711, OPT_MCU_BCM2835, OPT_MCU_BCM2837)
47 #include "dwc2_bcm.h"
48#elif TU_CHECK_MCU(OPT_MCU_EFM32GG)
49 #include "dwc2_efm32.h"
50#elif TU_CHECK_MCU(OPT_MCU_XMC4000)
51 #include "dwc2_xmc.h"
52#else
53 #error "Unsupported MCUs"
54#endif
55
56enum {
58};
59
60enum {
61 OTG_INT_COMMON = 0 // GINTSTS_DISCINT | GINTSTS_CONIDSTSCHNG
62};
63
64//--------------------------------------------------------------------+
65// Core/Controller
66//--------------------------------------------------------------------+
67TU_ATTR_ALWAYS_INLINE static inline dwc2_regs_t* DWC2_REG(uint8_t rhport) {
68 if (rhport >= DWC2_CONTROLLER_COUNT) {
69 // user mis-configured, ignore and use first controller
70 rhport = 0;
71 }
72 return (dwc2_regs_t*)_dwc2_controller[rhport].reg_base;
73}
74
75bool dwc2_core_is_highspeed(dwc2_regs_t* dwc2, tusb_role_t role);
76bool dwc2_core_init(uint8_t rhport, bool is_highspeed);
77void dwc2_core_handle_common_irq(uint8_t rhport, bool in_isr);
78
79//--------------------------------------------------------------------+
80// DFIFO
81//--------------------------------------------------------------------+
82TU_ATTR_ALWAYS_INLINE static inline void dfifo_flush_tx(dwc2_regs_t* dwc2, uint8_t fnum) {
83 // flush TX fifo and wait for it cleared
84 dwc2->grstctl = GRSTCTL_TXFFLSH | (fnum << GRSTCTL_TXFNUM_Pos);
85 while (dwc2->grstctl & GRSTCTL_TXFFLSH_Msk) {}
86}
87
88TU_ATTR_ALWAYS_INLINE static inline void dfifo_flush_rx(dwc2_regs_t* dwc2) {
89 // flush RX fifo and wait for it cleared
90 dwc2->grstctl = GRSTCTL_RXFFLSH;
91 while (dwc2->grstctl & GRSTCTL_RXFFLSH_Msk) {}
92}
93
94void dfifo_read_packet(dwc2_regs_t* dwc2, uint8_t* dst, uint16_t len);
95void dfifo_write_packet(dwc2_regs_t* dwc2, uint8_t fifo_num, uint8_t const* src, uint16_t len);
96
97//--------------------------------------------------------------------+
98// DMA
99//--------------------------------------------------------------------+
100
101#endif
static bool in_isr
static TU_ATTR_ALWAYS_INLINE dwc2_regs_t * DWC2_REG(uint8_t rhport)
Definition: dwc2_common.h:67
static TU_ATTR_ALWAYS_INLINE void dfifo_flush_tx(dwc2_regs_t *dwc2, uint8_t fnum)
Definition: dwc2_common.h:82
void dfifo_read_packet(dwc2_regs_t *dwc2, uint8_t *dst, uint16_t len)
Definition: dwc2_common.c:252
void dfifo_write_packet(dwc2_regs_t *dwc2, uint8_t fifo_num, uint8_t const *src, uint16_t len)
Definition: dwc2_common.c:277
@ OTG_INT_COMMON
Definition: dwc2_common.h:61
bool dwc2_core_is_highspeed(dwc2_regs_t *dwc2, tusb_role_t role)
Definition: dwc2_common.c:172
static TU_ATTR_ALWAYS_INLINE void dfifo_flush_rx(dwc2_regs_t *dwc2)
Definition: dwc2_common.h:88
bool dwc2_core_init(uint8_t rhport, bool is_highspeed)
Definition: dwc2_common.c:197
@ DWC2_CONTROLLER_COUNT
Definition: dwc2_common.h:57
void dwc2_core_handle_common_irq(uint8_t rhport, bool in_isr)
static const dwc2_controller_t _dwc2_controller[]
Definition: dwc2_esp32.h:57
volatile uint32_t grstctl
Definition: dwc2_type.h:557