1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/*
* Copyright (c) 2026, sakumisu
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "usbd_core.h"
#include "usbh_core.h"
#include "usb_dwc2_param.h"
#include "rtthread.h"
#include "cybsp.h"
#include "cy_device.h"
#if defined (COMPONENT_CM55)
#if !defined(CONFIG_USB_DCACHE_ENABLE)
#error "Please enable CONFIG_USB_DCACHE_ENABLE and put USB_NOCACHE_RAM_SECTION to section ".cy_socmem_data" for CM55"
#endif
#else
#define CONFIG_USB_DWC2_DMA_ENABLE
#endif
const struct dwc2_user_params param_common = {
.phy_type = DWC2_PHY_TYPE_PARAM_UTMI,
#ifdef CONFIG_USB_DWC2_DMA_ENABLE
.device_dma_enable = true,
#else
.device_dma_enable = false,
#endif
.device_dma_desc_enable = false,
.device_rx_fifo_size = 0x21F,
.device_tx_fifo_size = {
[0] = 16, // 64 byte
[1] = 0x300, // 1024 byte, tripple buffer
[2] = 0x300, // 1024 byte, tripple buffer
[3] = 0x300, // 1024 byte, tripple buffer
[4] = 0x300, // 1024 byte, tripple buffer
[5] = 0x300, // 1024 byte, tripple buffer
[6] = 0x300, // 1024 byte, tripple buffer
[7] = 0x300, // 1024 byte, tripple buffer
[8] = 0x300, // 1024 byte, tripple buffer
[9] = 0,
[10] = 0,
[11] = 0,
[12] = 0,
[13] = 0,
[14] = 0,
[15] = 0 },
.host_dma_desc_enable = false,
.host_rx_fifo_size = 0x21F,
.host_nperio_tx_fifo_size = 0x100, // 512 byte, double buffer
.host_perio_tx_fifo_size = 0x400, // 1024 byte, four buffer
.device_gccfg = 0,
.host_gccfg = 0
};
#ifndef CONFIG_USB_DWC2_CUSTOM_PARAM
void dwc2_get_user_params(uint32_t reg_base, struct dwc2_user_params *params)
{
memcpy(params, ¶m_common, sizeof(struct dwc2_user_params));
#ifdef CONFIG_USB_DWC2_CUSTOM_FIFO
struct usb_dwc2_user_fifo_config s_dwc2_fifo_config;
dwc2_get_user_fifo_config(reg_base, &s_dwc2_fifo_config);
params->device_rx_fifo_size = s_dwc2_fifo_config.device_rx_fifo_size;
for (uint8_t i = 0; i < MAX_EPS_CHANNELS; i++) {
params->device_tx_fifo_size[i] = s_dwc2_fifo_config.device_tx_fifo_size[i];
}
#endif
}
#endif
void USBHS_DEVICE_IRQHandler(void)
{
USBD_IRQHandler(0);
}
void usb_dc_low_level_init(uint8_t busid)
{
USBHS_SS->SUBSYSTEM_CTL = (1 << USBHS_SS_SUBSYSTEM_CTL_AHB_MASTER_SYNC_Pos) | USBHS_SS_SUBSYSTEM_CTL_SS_ENABLE_Msk;
USBHS_SS->PHY_FUNC_CTL_1 |= (7 << USBHS_SS_PHY_FUNC_CTL_1_PLL_FSEL_Pos);
USBHS_SS->PHY_FUNC_CTL_2 |= (USBHS_SS_PHY_FUNC_CTL_2_RES_TUNING_SEL_Msk | USBHS_SS_PHY_FUNC_CTL_2_EFUSE_SEL_Msk);
rt_thread_mdelay(200); // Wait for PHY stable
cy_stc_sysint_t usb_int_cfg = {
.intrSrc = usbhs_interrupt_usbhsctrl_IRQn,
.intrPriority = 3
};
/* Install the interrupt service routine */
Cy_SysInt_Init(&usb_int_cfg, USBHS_DEVICE_IRQHandler);
NVIC_EnableIRQ(usbhs_interrupt_usbhsctrl_IRQn);
}
void usb_dc_low_level_deinit(uint8_t busid)
{
NVIC_DisableIRQ(usbhs_interrupt_usbhsctrl_IRQn);
}
void usbd_dwc2_delay_ms(uint8_t ms)
{
rt_thread_mdelay(ms);
}
uint32_t usbd_dwc2_get_system_clock(void)
{
return SystemCoreClock;
}
void USBHS_HOST_IRQHandler(void)
{
USBH_IRQHandler(0);
}
void usb_hc_low_level_init(struct usbh_bus *bus)
{
USBHS_SS->SUBSYSTEM_CTL = (1 << USBHS_SS_SUBSYSTEM_CTL_AHB_MASTER_SYNC_Pos) | USBHS_SS_SUBSYSTEM_CTL_USB_MODE_Msk | USBHS_SS_SUBSYSTEM_CTL_SS_ENABLE_Msk;
USBHS_SS->PHY_FUNC_CTL_1 |= (7 << USBHS_SS_PHY_FUNC_CTL_1_PLL_FSEL_Pos);
USBHS_SS->PHY_FUNC_CTL_2 |= (USBHS_SS_PHY_FUNC_CTL_2_RES_TUNING_SEL_Msk | USBHS_SS_PHY_FUNC_CTL_2_EFUSE_SEL_Msk);
rt_thread_mdelay(200); // Wait for PHY stable
cy_stc_sysint_t usb_int_cfg = {
.intrSrc = usbhs_interrupt_usbhsctrl_IRQn,
.intrPriority = 3
};
/* Install the interrupt service routine */
Cy_SysInt_Init(&usb_int_cfg, USBHS_HOST_IRQHandler);
NVIC_EnableIRQ(usbhs_interrupt_usbhsctrl_IRQn);
}
void usb_hc_low_level_deinit(struct usbh_bus *bus)
{
NVIC_DisableIRQ(usbhs_interrupt_usbhsctrl_IRQn);
}
#ifdef CONFIG_USB_DCACHE_ENABLE
void usb_dcache_clean(uintptr_t addr, size_t size)
{
SCB_CleanDCache_by_Addr((void *)addr, size);
}
void usb_dcache_invalidate(uintptr_t addr, size_t size)
{
SCB_InvalidateDCache_by_Addr((void *)addr, size);
}
void usb_dcache_flush(uintptr_t addr, size_t size)
{
SCB_CleanInvalidateDCache_by_Addr((void *)addr, size);
}
#endif
|