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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
|
/**
**************************************************************************
* @file usbd_core.c
* @brief usb driver
**************************************************************************
* Copyright notice & Disclaimer
*
* The software Board Support Package (BSP) that is made available to
* download from Artery official website is the copyrighted work of Artery.
* Artery authorizes customers to use, copy, and distribute the BSP
* software and its related documentation for the purpose of design and
* development in conjunction with Artery microcontrollers. Use of the
* software is governed by this copyright notice and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
*
**************************************************************************
*/
#include "usbd_core.h"
#include "usbd_sdr.h"
#include "usbd_int.h"
/** @defgroup USBD_drivers_core
* @brief usb device drivers core
* @{
*/
/** @defgroup USBD_core_private_functions
* @{
*/
#ifdef BSP_USING_USBD
/**
* @brief usb core in transfer complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval none
*/
void usbd_core_in_handler(usbd_core_type *udev, uint8_t ept_addr)
{
/* get endpoint info*/
usb_ept_info *ept_info = &udev->ept_in[ept_addr & 0x7F];
if(ept_addr == 0)
{
if(udev->ept0_sts == USB_EPT0_DATA_IN)
{
if(ept_info->rem0_len > ept_info->maxpacket)
{
ept_info->rem0_len -= ept_info->maxpacket;
usbd_ept_send(udev, 0, ept_info->trans_buf,
MIN(ept_info->rem0_len, ept_info->maxpacket));
}
/* endpoint 0 */
else if(ept_info->last_len == ept_info->maxpacket
&& ept_info->ept0_slen >= ept_info->maxpacket
&& ept_info->ept0_slen < udev->ept0_wlength)
{
ept_info->last_len = 0;
usbd_ept_send(udev, 0, 0, 0);
usbd_ept_recv(udev, ept_addr, 0, 0);
}
else
{
usbd_ctrl_recv_status(udev);
}
usbd_data_in_stage_callback(udev,0);
}
}
else if(udev->class_handler->in_handler != 0 &&
udev->conn_state == USB_CONN_STATE_CONFIGURED)
{
/* other user define endpoint */
usbd_data_in_stage_callback(udev,ept_addr);
}
}
/**
* @brief usb core out transfer complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval none
*/
void usbd_core_out_handler(usbd_core_type *udev, uint8_t ept_addr)
{
/* get endpoint info*/
usb_ept_info *ept_info = &udev->ept_out[ept_addr & 0x7F];
if(ept_addr == 0)
{
/* endpoint 0 */
if(udev->ept0_sts == USB_EPT0_DATA_OUT)
{
if(ept_info->rem0_len > ept_info->maxpacket)
{
ept_info->rem0_len -= ept_info->maxpacket;
usbd_ept_recv(udev, ept_addr, ept_info->trans_buf,
MIN(ept_info->rem0_len, ept_info->maxpacket));
}
else
{
usbd_ctrl_send_status(udev);
}
usbd_data_out_stage_callback(udev, 0);
}
}
else if(udev->class_handler->out_handler != 0 &&
udev->conn_state == USB_CONN_STATE_CONFIGURED)
{
/* other user define endpoint */
usbd_data_out_stage_callback(udev, ept_addr);
}
}
/**
* @brief usb core setup transfer complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval none
*/
void usbd_core_setup_handler(usbd_core_type *udev, uint8_t ept_num)
{
/* setup parse */
usbd_setup_request_parse(&udev->setup, udev->setup_buffer);
/* set ept0 status */
udev->ept0_sts = USB_EPT0_SETUP;
udev->ept0_wlength = udev->setup.wLength;
switch(udev->setup.bmRequestType & USB_REQ_RECIPIENT_MASK)
{
case USB_REQ_RECIPIENT_DEVICE:
/* recipient device request */
usbd_device_request(udev);
break;
case USB_REQ_RECIPIENT_INTERFACE:
/* recipient interface request */
usbd_interface_request(udev);
break;
case USB_REQ_RECIPIENT_ENDPOINT:
/* recipient endpoint request */
usbd_endpoint_request(udev);
break;
default:
break;
}
}
/**
* @brief usb control endpoint send data
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @param buffer: send data buffer
* @param len: send data length
* @retval none
*/
void usbd_ctrl_send(usbd_core_type *udev, uint8_t *buffer, uint16_t len)
{
usb_ept_info *ept_info = &udev->ept_in[0];
ept_info->ept0_slen = len;
ept_info->rem0_len = len;
udev->ept0_sts = USB_EPT0_DATA_IN;
}
/**
* @brief usb control endpoint receive data
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @param buffer: recv data buffer
* @param len: recv data length
* @retval none
*/
void usbd_ctrl_recv(usbd_core_type *udev, uint8_t *buffer, uint16_t len)
{
usb_ept_info *ept_info = &udev->ept_out[0];
ept_info->ept0_slen = len;
ept_info->rem0_len = len;
udev->ept0_sts = USB_EPT0_DATA_OUT;
usbd_ept_recv(udev, 0, buffer, len);
}
/**
* @brief usb control endpoint send in status
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_ctrl_send_status(usbd_core_type *udev)
{
udev->ept0_sts = USB_EPT0_STATUS_IN;
usbd_ept_send(udev, 0, 0, 0);
}
/**
* @brief usb control endpoint send out status
* @param udev: usb device core handler type
* @retval none
*/
void usbd_ctrl_recv_status(usbd_core_type *udev)
{
udev->ept0_sts = USB_EPT0_STATUS_OUT;
usbd_ept_recv(udev, 0, 0, 0);
}
/**
* @brief clear endpoint stall
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval none
*/
void usbd_clear_stall(usbd_core_type *udev, uint8_t ept_addr)
{
usb_ept_info *ept_info;
if(ept_addr & 0x80)
{
/* in endpoint */
ept_info = &udev->ept_in[ept_addr & 0x7F];
USB_SET_TXSTS(ept_info->eptn, USB_TX_VALID);
}
else
{
/* out endpoint */
ept_info = &udev->ept_out[ept_addr & 0x7F];
USB_SET_RXSTS(ept_info->eptn, USB_RX_VALID);
}
ept_info->stall = 0;
}
/**
* @brief usb set endpoint to stall.
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval none
*/
void usbd_set_stall(usbd_core_type *udev, uint8_t ept_addr)
{
usb_ept_info *ept_info;
if(ept_addr & 0x80)
{
/* in endpoint */
ept_info = &udev->ept_in[ept_addr & 0x7F];
USB_SET_TXSTS(ept_info->eptn, USB_TX_STALL);
}
else
{
/* out endpoint */
ept_info = &udev->ept_out[ept_addr & 0x7F];
USB_SET_RXSTS(ept_info->eptn, USB_RX_STALL)
}
ept_info->stall = 1;
}
/**
* @brief un-support device request
* @param udev: usb device core handler type
* @retval none
*/
void usbd_ctrl_unsupport(usbd_core_type *udev)
{
/* return stall status */
usbd_set_stall(udev, 0x00);
usbd_set_stall(udev, 0x80);
}
/**
* @brief usb endpoint send data
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @param buffer: send data buffer
* @param len: send data length
* @retval none
*/
void usbd_ept_send(usbd_core_type *udev, uint8_t ept_addr, uint8_t *buffer, uint16_t len)
{
/* get endpoint info struct and register */
usb_ept_info *ept_info = &udev->ept_in[ept_addr & 0x7F];
uint16_t trs_len = 0;
usbd_type *usbx = udev->usb_reg;
/* set send data buffer and length */
ept_info->trans_buf = buffer;
ept_info->total_len = len;
ept_info->trans_len = 0;
if(ept_info->total_len > ept_info->maxpacket)
{
trs_len = ept_info->maxpacket;
ept_info->total_len -= trs_len;
}
else
{
trs_len = len;
ept_info->total_len = 0;
}
ept_info->last_len = trs_len;
if(ept_info->is_double_buffer == 0)
{
/* write data to endpoint buffer */
usb_write_packet(ept_info->trans_buf, ept_info->tx_addr, trs_len);
/* set send data length */
USB_SET_TXLEN((ept_addr & 0x7F), trs_len);
}
else
{
if(usbx->ept_bit[ept_addr & 0x7F].txdts)
{
USB_SET_EPT_DOUBLE_BUF1_LEN((ept_addr & 0x7F), trs_len, DATA_TRANS_IN);
usb_write_packet(ept_info->trans_buf, ept_info->rx_addr, trs_len);
}
else
{
USB_SET_EPT_DOUBLE_BUF0_LEN((ept_addr & 0x7F), trs_len, DATA_TRANS_IN);
usb_write_packet(ept_info->trans_buf, ept_info->tx_addr, trs_len);
}
USB_FREE_DB_USER_BUFFER((ept_addr & 0x7F), DATA_TRANS_IN);
}
/* set tx status valid */
USB_SET_TXSTS((ept_addr & 0x7F), USB_TX_VALID);
}
/**
* @brief usb endpoint receive data
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @param buffer: receive data buffer
* @param len: receive data length
* @retval none
*/
void usbd_ept_recv(usbd_core_type *udev, uint8_t ept_addr, uint8_t *buffer, uint16_t len)
{
/* get endpoint info struct and register */
usb_ept_info *ept_info = &udev->ept_out[ept_addr & 0x7F];
uint32_t trs_len = 0;
/* set receive data buffer and length */
ept_info->trans_buf = buffer;
ept_info->total_len = len;
ept_info->trans_len = 0;
if(ept_info->total_len > ept_info->maxpacket)
{
trs_len = ept_info->maxpacket;
ept_info->total_len -= trs_len;
}
else
{
trs_len = len;
ept_info->total_len = 0;
}
/* set rx status valid */
USB_SET_RXSTS((ept_addr & 0x7F), USB_RX_VALID);
}
/**
* @brief usb endpoint get receive data length
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval none
*/
uint32_t usbd_get_recv_len(usbd_core_type *udev, uint8_t ept_addr)
{
usb_ept_info *ept = &udev->ept_out[ept_addr & 0x7F];
return ept->trans_len;
}
/**
* @brief enable endpoint double buffer.
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval none
*/
void usbd_ept_dbuffer_enable(usbd_core_type *udev, uint8_t ept_addr)
{
usb_ept_info *ept_info;
if((ept_addr & 0x80) == 0)
{
/* out endpoint info */
ept_info = &udev->ept_out[ept_addr & 0x7F];
}
else
{
/* in endpoint info */
ept_info = &udev->ept_in[ept_addr & 0x7F];
}
ept_info->is_double_buffer = TRUE;
}
/**
* @brief usb auto define endpoint buffer
* @param usb_ept_info: endpoint information
* @retval none
*/
void usbd_ept_buf_auto_define(usb_ept_info *ept_info)
{
if(ept_info->is_double_buffer == 0)
{
if( ept_info->inout == DATA_TRANS_IN )
{
if(ept_info->tx_addr == 0)
ept_info->tx_addr = usb_buffer_malloc(ept_info->maxpacket);
}
else
{
if(ept_info->rx_addr == 0)
ept_info->rx_addr = usb_buffer_malloc(ept_info->maxpacket);
}
}
else
{
/* double buffer auto define */
if(ept_info->tx_addr == 0)
ept_info->tx_addr = usb_buffer_malloc(ept_info->maxpacket);
if(ept_info->rx_addr == 0)
ept_info->rx_addr = usb_buffer_malloc(ept_info->maxpacket);
}
}
/**
* @brief usb custom define endpoint buffer
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @param addr: usb fifo offset address
* @retval none
*/
void usbd_ept_buf_custom_define(usbd_core_type *udev, uint8_t ept_addr,
uint32_t addr)
{
usb_ept_info *ept_info;
if((ept_addr & 0x80) == 0)
{
/* out endpoint info */
ept_info = &udev->ept_out[ept_addr & 0x7F];
}
else
{
/* in endpoint info */
ept_info = &udev->ept_in[ept_addr & 0x7F];
}
if(ept_info->is_double_buffer == 0)
{
if( ept_info->inout == DATA_TRANS_IN )
{
ept_info->tx_addr = addr;
}
else
{
ept_info->rx_addr = addr;
}
}
else
{
/* double buffer malloc */
ept_info->tx_addr = addr & 0xFFFF;
ept_info->rx_addr = (addr >> 16) & 0xFFFF;
}
}
/**
* @brief usb open endpoint
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @param ept_type: endpoint type
* @param maxpacket: endpoint support max buffer size
* @retval none
*/
void usbd_ept_open(usbd_core_type *udev, uint8_t ept_addr, uint8_t ept_type, uint16_t maxpacket)
{
usbd_type *usbx = udev->usb_reg;
usb_ept_info *ept_info;
if((ept_addr & 0x80) == 0)
{
/* out endpoint info */
ept_info = &udev->ept_out[ept_addr & 0x7F];
ept_info->inout = DATA_TRANS_OUT;
}
else
{
/* in endpoint info */
ept_info = &udev->ept_in[ept_addr & 0x7F];
ept_info->inout = DATA_TRANS_IN;
}
/* set endpoint maxpacket and type */
ept_info->maxpacket = (maxpacket + 1) & 0xFFFE;
ept_info->trans_type = ept_type;
#ifdef USB_EPT_AUTO_MALLOC_BUFFER
usbd_ept_buf_auto_define(ept_info);
#endif
/* open endpoint */
usb_ept_open(usbx, ept_info);
}
/**
* @brief usb close endpoint
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval none
*/
void usbd_ept_close(usbd_core_type *udev, uint8_t ept_addr)
{
usb_ept_info *ept_info;
if(ept_addr & 0x80)
{
/* in endpoint */
ept_info = &udev->ept_in[ept_addr & 0x7F];
}
else
{
/* out endpoint */
ept_info = &udev->ept_out[ept_addr & 0x7F];
}
/* close endpoint */
usb_ept_close(udev->usb_reg, ept_info);
}
/**
* @brief usb device connect to host
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_connect(usbd_core_type *udev)
{
usb_connect(udev->usb_reg);
}
/**
* @brief usb device disconnect to host
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_disconnect(usbd_core_type *udev)
{
usb_disconnect(udev->usb_reg);
}
/**
* @brief usb device set device address.
* @param udev: to the structure of usbd_core_type
* @param address: host assignment address
* @retval none
*/
void usbd_set_device_addr(usbd_core_type *udev, uint8_t address)
{
if(address == 0)
usb_set_address(udev->usb_reg, address);
}
/**
* @brief get usb connect state
* @param udev: to the structure of usbd_core_type
* @retval usb connect state
*/
usbd_conn_state usbd_connect_state_get(usbd_core_type *udev)
{
return udev->conn_state;
}
/**
* @brief usb device remote wakeup
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_remote_wakeup(usbd_core_type *udev)
{
/* set connect state */
udev->conn_state = udev->old_conn_state;
usb_exit_suspend(udev->usb_reg);
usb_remote_wkup_set(udev->usb_reg);
rt_thread_mdelay(10);
usb_remote_wkup_clear(udev->usb_reg);
}
/**
* @brief usb device enter suspend mode
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_enter_suspend(usbd_core_type *udev)
{
usb_enter_suspend(udev->usb_reg);
}
/**
* @brief usb endpoint structure initialization
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_ept_defaut_init(usbd_core_type *udev)
{
uint8_t i_index = 0;
/* init in endpoint info structure */
for(i_index = 0; i_index < USB_EPT_MAX_NUM; i_index ++)
{
udev->ept_in[i_index].eptn = i_index;
udev->ept_in[i_index].ept_address = i_index;
udev->ept_in[i_index].inout = DATA_TRANS_IN;
udev->ept_in[i_index].maxpacket = 0;
udev->ept_in[i_index].trans_buf = 0;
udev->ept_in[i_index].total_len = 0;
udev->ept_in[i_index].tx_addr = 0;
udev->ept_in[i_index].rx_addr = 0;
}
/* init out endpoint info structure */
for(i_index = 0; i_index < USB_EPT_MAX_NUM; i_index ++)
{
udev->ept_out[i_index].eptn = i_index;
udev->ept_out[i_index].ept_address = i_index;
udev->ept_out[i_index].inout = DATA_TRANS_OUT;
udev->ept_out[i_index].maxpacket = 0;
udev->ept_out[i_index].trans_buf = 0;
udev->ept_out[i_index].total_len = 0;
udev->ept_out[i_index].rx_addr = 0;
udev->ept_out[i_index].tx_addr = 0;
}
return;
}
/**
* @brief initializes the usb core
* @param udev: to the structure of usbd_core_type
* @param usb_reg: usb register pointer (USB)
* @param class_handler: usb class handler
* @param desc_handler: device config handler
* @param core_id: usb core id number
* @retval none
*/
void usbd_core_init(usbd_core_type *udev,
usb_reg_type *usb_reg,
uint8_t core_id)
{
/* set usb register type */
udev->usb_reg = usb_reg;
/* set usb connect state to default */
udev->conn_state = USB_CONN_STATE_DEFAULT;
/* init in endpoint info structure */
usbd_ept_defaut_init(udev);
#ifdef USB_BUFFER_SIZE_EX
/* usb buffer size extend 768-1280 byte */
usb_usbbufs_enable(usb_reg, TRUE);
#endif
/*usb register config */
usb_dev_init(udev->usb_reg);
}
#endif
/**
* @}
*/
/**
* @}
*/
|