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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-07-09 QT-one first version
*/
#include "drv_usbd.h"
#ifdef RT_USING_USB_DEVICE
#if !defined(BSP_USING_USBD)
#error "Please define at least one BSP_USING_USBD"
#endif
#if defined(BSP_USING_USBD)
#include "usb_port.h"
#endif
struct ht32_usbd
{
char *name;
USBDCore_TypeDef *p_usbd_code;
IRQn_Type irq;
};
__ALIGN4 USBDCore_TypeDef p_usbd;
/* internal mount point */
static struct ht32_usbd *p_usbd_instance = RT_NULL;
/* Endpoint Function List */
static struct ep_id endpoint_pool[] =
{
{0x00, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED },
{0x01, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
{0x02, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
{0x03, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
{0x04, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
{0x05, USB_EP_ATTR_ISOC, USB_DIR_IN, 64, ID_UNASSIGNED},
{0x06, USB_EP_ATTR_ISOC, USB_DIR_OUT, 64, ID_UNASSIGNED},
{0x07, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 64, ID_UNASSIGNED},
};
/* usbd Peripheral List */
static struct ht32_usbd usbd_config[] =
{
#ifdef BSP_USING_USBD
{
.name = BSP_USING_USBD_NAME,
.p_usbd_code = NULL,
.irq = USB_IRQn,
},
#endif
};
/* Start of Frame (SOF) interrupt callbacks */
void usbd_sof_callback(USBDCore_TypeDef *pCore)
{
udcd_t udcd = (udcd_t)pCore->pdata;
rt_usbd_sof_handler(udcd);
}
/* USB reset interrupt */
void usbd_reset_callback(USBDCore_TypeDef *pCore)
{
udcd_t udcd = (udcd_t)pCore->pdata;
rt_usbd_reset_handler(udcd);
}
/* USB Suspend (Disconnect) Interrupt */
void usbd_suspend_callback(USBDCore_TypeDef *pCore)
{
udcd_t udcd = (udcd_t)pCore->pdata;
rt_usbd_disconnect_handler(udcd);
}
/* USB recovery (reconnect) interrupt */
void usbd_resume_callback(USBDCore_TypeDef *pCore)
{
udcd_t udcd = (udcd_t)pCore->pdata;
rt_usbd_connect_handler(udcd);
}
/* USB endpoint 0 interrupt */
void usbd_setup_callback(USBDCore_TypeDef *pCore)
{
udcd_t udcd = (udcd_t)pCore->pdata;
rt_usbd_ep0_setup_handler(udcd, (struct urequest *)&pCore->Device.Request);
}
/* Endpoint 0 input interrupt£¨Can be classified as other endpoint input interrupt£© */
void usbd_ep0_in_callback(USBDCore_TypeDef *pCore)
{
udcd_t udcd = (udcd_t)pCore->pdata;
rt_usbd_ep0_in_handler(udcd);
}
/* Endpoint 0 output interrupt£¨Can be classified as other endpoint input interrupt£© */
void usbd_ep0_out_callback(USBDCore_TypeDef *pCore)
{
udcd_t udcd = (udcd_t)pCore->pdata;
pCore->Device.Transfer.sByteLength = pCore->Device.Request.wLength;
rt_usbd_ep0_out_handler(udcd, pCore->Device.Transfer.sByteLength);
}
/* Other endpoint input interrupt */
void usbd_ep_in_callback(USBDCore_TypeDef *pCore, USBD_EPTn_Enum EPTn)
{
udcd_t udcd = (udcd_t)pCore->pdata;
pCore->Device.Transfer.sByteLength = 0;
rt_usbd_ep_in_handler(udcd, EPTn | 0x80, pCore->Device.Transfer.sByteLength);
}
/* Other Endpoint Output Interrupt */
void usbd_ep_out_callback(USBDCore_TypeDef *pCore, USBD_EPTn_Enum EPTn)
{
udcd_t udcd = (udcd_t)pCore->pdata;
rt_usbd_ep_out_handler(udcd, EPTn, pCore->ept_io->trans_len);
}
/* Setting the address */
static rt_err_t ht32_set_address(rt_uint8_t address)
{
/* Setting the device address */
p_usbd_instance->p_usbd_code->Info.CurrentStatus = USER_USB_STATE_ADDRESS;
API_USB_SET_ADDR(address);
return RT_EOK;
}
/* Configuration settings */
static rt_err_t ht32_set_config(rt_uint8_t address)
{
return RT_EOK;
}
/* endpoint pause */
static rt_err_t ht32_ep_set_stall(rt_uint8_t address)
{
if (0 == (address & 0x7f))
API_USB_EPTn_SEND_STALL((USBD_EPTn_Enum)(address & 0x7f));
else
API_USB_EPTn_SET_HALT((USBD_EPTn_Enum)(address & 0x7f));
return RT_EOK;
}
/* Endpoint reboot*/
static rt_err_t ht32_ep_clear_stall(rt_uint8_t address)
{
if (0 != (address & 0x7f))
{
API_USB_EPTn_CLR_HALT((USBD_EPTn_Enum)(address & 0x7f));
API_USB_EPTn_CLR_DTG((USBD_EPTn_Enum)(address & 0x7f));
}
return RT_EOK;
}
/* endpoint enable */
static rt_err_t ht32_ep_enable(struct uendpoint *ep)
{
/* Functions not found in the firmware library at the moment */
RT_ASSERT(ep != RT_NULL);
RT_ASSERT(ep->ep_desc != RT_NULL);
usbd_ep_enable(p_usbd_instance->p_usbd_code, ep->ep_desc->bEndpointAddress);
return RT_EOK;
}
/* endpoint incapacity */
static rt_err_t ht32_ep_disable(struct uendpoint *ep)
{
/* Functions not found in the firmware library at the moment */
RT_ASSERT(ep != RT_NULL);
RT_ASSERT(ep->ep_desc != RT_NULL);
usbd_ep_disable(p_usbd_instance->p_usbd_code, ep->ep_desc->bEndpointAddress);
return RT_EOK;
}
/* Endpoint Receive Data Preparation */
static rt_ssize_t ht32_ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size)
{
USBDCore_EPTReadOUTData((USBD_EPTn_Enum)(address & 0x7f), (uint32_t *)buffer, size);
return size;
}
/* Endpoint reading data */
static rt_ssize_t ht32_ep_read(rt_uint8_t address, void *buffer)
{
/* Do not read data from this function */
rt_size_t size = 0;
RT_ASSERT(buffer != RT_NULL);
return size;
}
/* Endpoint Write Data */
static rt_ssize_t ht32_ep_write(rt_uint8_t address, void *buffer, rt_size_t size)
{
/* Use this function to write data to a USB endpoint */
return USBDCore_EPTWriteINData((USBD_EPTn_Enum)(address & 0x7f), (uint32_t *)buffer, size);
}
/* Endpoint 0 transmit status */
static rt_err_t ht32_ep0_send_status(void)
{
uint8_t Date = 0;
/* State of send endpoint 0 */
API_USB_EPTn_WRITE_IN(USBD_EPT0, (u32*)&Date, 0);
return RT_EOK;
}
/* USB pause */
static rt_err_t ht32_suspend(void)
{
return RT_EOK;
}
/* USB awakens */
static rt_err_t ht32_wakeup(void)
{
return RT_EOK;
}
/* USB device interface function */
const static struct udcd_ops _udc_ops =
{
.set_address = ht32_set_address,
.set_config = ht32_set_config,
.ep_set_stall = ht32_ep_set_stall,
.ep_clear_stall = ht32_ep_clear_stall,
.ep_enable = ht32_ep_enable,
.ep_disable = ht32_ep_disable,
.ep_read_prepare = ht32_ep_read_prepare,
.ep_read = ht32_ep_read,
.ep_write = ht32_ep_write,
.ep0_send_status = ht32_ep0_send_status,
.suspend = ht32_suspend,
.wakeup = ht32_wakeup,
};
static void usbd_mainroutine(void)
{
USBDCore_MainRoutine(p_usbd_instance->p_usbd_code);
}
/* USB Device Initialisation Functions */
static rt_err_t ht32_dcd_init(rt_device_t device)
{
/* USB object and interface initialisation, and turning on USB interrupts */
USB_Configuration(p_usbd_instance->p_usbd_code);
rt_thread_idle_sethook(usbd_mainroutine);
return RT_EOK;
}
/* USB device registration function */
int ht32_usbd_register(void)
{
rt_size_t obj_num;
rt_err_t result = 0;
int index;
USBDCore_TypeDef *p_usbd_core = &p_usbd;
/* Calculate how many USB devices */
obj_num = sizeof(usbd_config) / sizeof(struct ht32_usbd);
for (index = 0; index < obj_num; index++)
{
/* Request a udcd object memory and clear it. */
udcd_t udcd = (udcd_t)rt_malloc(sizeof(struct udcd));
if (udcd == RT_NULL)
{
rt_kprintf("udcd malloc failed\r\n");
return -RT_ERROR;
}
rt_memset((void *)udcd, 0, sizeof(struct udcd));
/* Assigning a value to the requested udcd object */
udcd->parent.type = RT_Device_Class_USBDevice;
udcd->parent.init = ht32_dcd_init;
udcd->parent.user_data = p_usbd_core;
udcd->ops = &_udc_ops;
p_usbd_core->pdata = udcd;
usbd_config[index].p_usbd_code = p_usbd_core;
/* register endpoint infomation */
udcd->ep_pool = endpoint_pool;
udcd->ep0.id = &endpoint_pool[0];
result = rt_device_register((rt_device_t)udcd, usbd_config[index].name, 0);
RT_ASSERT(result == RT_EOK);
p_usbd_instance = &usbd_config[index];
result = rt_usb_device_init();
RT_ASSERT(result == RT_EOK);
}
return result;
}
INIT_DEVICE_EXPORT(ht32_usbd_register);
void USB_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
/* interrupt callback function */
usbd_irq_handler(p_usbd_instance->p_usbd_code);
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* RT_USING_USB_DEVICE */
|