Skip to content

Commit 2ff208b

Browse files
committed
add dwc2 driver for esp32
1 parent 55eebb9 commit 2ff208b

File tree

8 files changed

+3683
-0
lines changed

8 files changed

+3683
-0
lines changed

src/portable/synopsys/dwc2/dcd_dwc2.c

Lines changed: 1189 additions & 0 deletions
Large diffs are not rendered by default.

src/portable/synopsys/dwc2/dwc2_bcm.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
#ifndef _TUSB_DWC2_BCM_H_
28+
#define _TUSB_DWC2_BCM_H_
29+
30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
33+
34+
#include "broadcom/defines.h"
35+
#include "broadcom/interrupts.h"
36+
#include "broadcom/caches.h"
37+
38+
#define DWC2_EP_MAX 8
39+
40+
static const dwc2_controller_t _dwc2_controller[] =
41+
{
42+
{ .reg_base = USB_OTG_GLOBAL_BASE, .irqnum = USB_IRQn, .ep_count = DWC2_EP_MAX, .ep_fifo_size = 4096 }
43+
};
44+
45+
#define dcache_clean(_addr, _size) data_clean(_addr, _size)
46+
#define dcache_invalidate(_addr, _size) data_invalidate(_addr, _size)
47+
#define dcache_clean_invalidate(_addr, _size) data_clean_and_invalidate(_addr, _size)
48+
49+
TU_ATTR_ALWAYS_INLINE
50+
static inline void dwc2_dcd_int_enable(uint8_t rhport)
51+
{
52+
BP_EnableIRQ(_dwc2_controller[rhport].irqnum);
53+
}
54+
55+
TU_ATTR_ALWAYS_INLINE
56+
static inline void dwc2_dcd_int_disable (uint8_t rhport)
57+
{
58+
BP_DisableIRQ(_dwc2_controller[rhport].irqnum);
59+
}
60+
61+
static inline void dwc2_remote_wakeup_delay(void)
62+
{
63+
// try to delay for 1 ms
64+
// TODO implement later
65+
}
66+
67+
// MCU specific PHY init, called BEFORE core reset
68+
static inline void dwc2_phy_init(dwc2_regs_t * dwc2, uint8_t hs_phy_type)
69+
{
70+
(void) dwc2;
71+
(void) hs_phy_type;
72+
73+
// nothing to do
74+
}
75+
76+
// MCU specific PHY update, it is called AFTER init() and core reset
77+
static inline void dwc2_phy_update(dwc2_regs_t * dwc2, uint8_t hs_phy_type)
78+
{
79+
(void) dwc2;
80+
(void) hs_phy_type;
81+
82+
// nothing to do
83+
}
84+
85+
#ifdef __cplusplus
86+
}
87+
#endif
88+
89+
#endif
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2021 Rafael Silva (@perigoso)
5+
* Copyright (c) 2021, Ha Thach (tinyusb.org)
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*
25+
* This file is part of the TinyUSB stack.
26+
*/
27+
28+
#ifndef _DWC2_EFM32_H_
29+
#define _DWC2_EFM32_H_
30+
31+
#ifdef __cplusplus
32+
extern "C" {
33+
#endif
34+
35+
#include "em_device.h"
36+
37+
// EFM32 has custom control register before DWC registers
38+
#define DWC2_REG_BASE (USB_BASE + offsetof(USB_TypeDef, GOTGCTL))
39+
#define DWC2_EP_MAX 7
40+
41+
static const dwc2_controller_t _dwc2_controller[] =
42+
{
43+
{ .reg_base = DWC2_REG_BASE, .irqnum = USB_IRQn, .ep_count = DWC2_EP_MAX, .ep_fifo_size = 2048 }
44+
};
45+
46+
TU_ATTR_ALWAYS_INLINE
47+
static inline void dwc2_dcd_int_enable(uint8_t rhport)
48+
{
49+
NVIC_EnableIRQ(_dwc2_controller[rhport].irqnum);
50+
}
51+
52+
TU_ATTR_ALWAYS_INLINE
53+
static inline void dwc2_dcd_int_disable (uint8_t rhport)
54+
{
55+
NVIC_DisableIRQ(_dwc2_controller[rhport].irqnum);
56+
}
57+
58+
static inline void dwc2_remote_wakeup_delay(void)
59+
{
60+
// try to delay for 1 ms
61+
// uint32_t count = SystemCoreClock / 1000;
62+
// while ( count-- ) __NOP();
63+
}
64+
65+
// MCU specific PHY init, called BEFORE core reset
66+
static inline void dwc2_phy_init(dwc2_regs_t * dwc2, uint8_t hs_phy_type)
67+
{
68+
(void) dwc2;
69+
(void) hs_phy_type;
70+
71+
// Enable PHY
72+
USB->ROUTE = USB_ROUTE_PHYPEN;
73+
}
74+
75+
// MCU specific PHY update, it is called AFTER init() and core reset
76+
static inline void dwc2_phy_update(dwc2_regs_t * dwc2, uint8_t hs_phy_type)
77+
{
78+
(void) dwc2;
79+
(void) hs_phy_type;
80+
81+
// EFM32 Manual: turn around must be 5 (reset & default value)
82+
// dwc2->gusbcfg = (dwc2->gusbcfg & ~GUSBCFG_TRDT_Msk) | (5u << GUSBCFG_TRDT_Pos);
83+
}
84+
85+
#ifdef __cplusplus
86+
}
87+
#endif
88+
89+
#endif
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 _DWC2_ESP32_H_
29+
#define _DWC2_ESP32_H_
30+
31+
#ifdef __cplusplus
32+
extern "C" {
33+
#endif
34+
35+
#include "esp_intr_alloc.h"
36+
#include "soc/periph_defs.h"
37+
//#include "soc/usb_periph.h"
38+
39+
#define DWC2_REG_BASE 0x60080000UL
40+
#define DWC2_EP_MAX 6 // USB_OUT_EP_NUM. TODO ESP32Sx only has 5 tx fifo (5 endpoint IN)
41+
42+
static const dwc2_controller_t _dwc2_controller[] =
43+
{
44+
{ .reg_base = DWC2_REG_BASE, .irqnum = 0, .ep_count = DWC2_EP_MAX, .ep_fifo_size = 1024 }
45+
};
46+
47+
static intr_handle_t usb_ih;
48+
49+
static void dcd_int_handler_wrap(void* arg)
50+
{
51+
(void) arg;
52+
dcd_int_handler(0);
53+
}
54+
55+
TU_ATTR_ALWAYS_INLINE
56+
static inline void dwc2_dcd_int_enable (uint8_t rhport)
57+
{
58+
(void) rhport;
59+
esp_intr_alloc(ETS_USB_INTR_SOURCE, ESP_INTR_FLAG_LOWMED, dcd_int_handler_wrap, NULL, &usb_ih);
60+
}
61+
62+
TU_ATTR_ALWAYS_INLINE
63+
static inline void dwc2_dcd_int_disable (uint8_t rhport)
64+
{
65+
(void) rhport;
66+
esp_intr_free(usb_ih);
67+
}
68+
69+
static inline void dwc2_remote_wakeup_delay(void)
70+
{
71+
vTaskDelay(pdMS_TO_TICKS(1));
72+
}
73+
74+
// MCU specific PHY init, called BEFORE core reset
75+
static inline void dwc2_phy_init(dwc2_regs_t * dwc2, uint8_t hs_phy_type)
76+
{
77+
(void) dwc2;
78+
(void) hs_phy_type;
79+
80+
// nothing to do
81+
}
82+
83+
// MCU specific PHY update, it is called AFTER init() and core reset
84+
static inline void dwc2_phy_update(dwc2_regs_t * dwc2, uint8_t hs_phy_type)
85+
{
86+
(void) dwc2;
87+
(void) hs_phy_type;
88+
89+
// nothing to do
90+
}
91+
92+
#ifdef __cplusplus
93+
}
94+
#endif
95+
96+
#endif /* _DWC2_ESP32_H_ */
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 DWC2_GD32_H_
29+
#define DWC2_GD32_H_
30+
31+
#ifdef __cplusplus
32+
extern "C" {
33+
#endif
34+
35+
#define DWC2_REG_BASE 0x50000000UL
36+
#define DWC2_EP_MAX 4
37+
38+
static const dwc2_controller_t _dwc2_controller[] =
39+
{
40+
{ .reg_base = DWC2_REG_BASE, .irqnum = 86, .ep_count = DWC2_EP_MAX, .ep_fifo_size = 1280 }
41+
};
42+
43+
extern uint32_t SystemCoreClock;
44+
45+
// The GD32VF103 is a RISC-V MCU, which implements the ECLIC Core-Local
46+
// Interrupt Controller by Nuclei. It is nearly API compatible to the
47+
// NVIC used by ARM MCUs.
48+
#define ECLIC_INTERRUPT_ENABLE_BASE 0xD2001001UL
49+
50+
TU_ATTR_ALWAYS_INLINE
51+
static inline void __eclic_enable_interrupt (uint32_t irq) {
52+
*(volatile uint8_t*)(ECLIC_INTERRUPT_ENABLE_BASE + (irq * 4)) = 1;
53+
}
54+
55+
TU_ATTR_ALWAYS_INLINE
56+
static inline void __eclic_disable_interrupt (uint32_t irq){
57+
*(volatile uint8_t*)(ECLIC_INTERRUPT_ENABLE_BASE + (irq * 4)) = 0;
58+
}
59+
60+
TU_ATTR_ALWAYS_INLINE
61+
static inline void dwc2_dcd_int_enable(uint8_t rhport)
62+
{
63+
__eclic_enable_interrupt(_dwc2_controller[rhport].irqnum);
64+
}
65+
66+
TU_ATTR_ALWAYS_INLINE
67+
static inline void dwc2_dcd_int_disable (uint8_t rhport)
68+
{
69+
__eclic_disable_interrupt(_dwc2_controller[rhport].irqnum);
70+
}
71+
72+
static inline void dwc2_remote_wakeup_delay(void)
73+
{
74+
// try to delay for 1 ms
75+
uint32_t count = SystemCoreClock / 1000;
76+
while ( count-- ) __asm volatile ("nop");
77+
}
78+
79+
// MCU specific PHY init, called BEFORE core reset
80+
static inline void dwc2_phy_init(dwc2_regs_t * dwc2, uint8_t hs_phy_type)
81+
{
82+
(void) dwc2;
83+
(void) hs_phy_type;
84+
85+
// nothing to do
86+
}
87+
88+
// MCU specific PHY update, it is called AFTER init() and core reset
89+
static inline void dwc2_phy_update(dwc2_regs_t * dwc2, uint8_t hs_phy_type)
90+
{
91+
(void) dwc2;
92+
(void) hs_phy_type;
93+
94+
// nothing to do
95+
}
96+
97+
#ifdef __cplusplus
98+
}
99+
#endif
100+
101+
#endif /* DWC2_GD32_H_ */

0 commit comments

Comments
 (0)