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
|
/**
**************************************************************************
* @file usbd_int.c
* @brief usb interrupt request
**************************************************************************
* 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_int.h"
/** @defgroup USBD_drivers_interrupt
* @brief usb device interrupt
* @{
*/
/** @defgroup USBD_int_private_functions
* @{
*/
#ifdef USE_OTG_DEVICE_MODE
/**
* @brief usb device interrput request handler.
* @param otgdev: to the structure of otg_core_type
* @retval none
*/
void usbd_irq_handler(otg_core_type *otgdev)
{
otg_global_type *usbx = otgdev->usb_reg;
usbd_core_type *udev = &otgdev->dev;
uint32_t intsts = usb_global_get_all_interrupt(usbx);
/* check current device mode */
if(usbx->gintsts_bit.curmode == 0)
{
/* mode mismatch interrupt */
if(intsts & USB_OTG_MODEMIS_FLAG)
{
usb_global_clear_interrupt(usbx, USB_OTG_MODEMIS_FLAG);
}
/* in endpoint interrupt */
if(intsts & USB_OTG_IEPT_FLAG)
{
usbd_inept_handler(udev);
}
/* out endpoint interrupt */
if(intsts & USB_OTG_OEPT_FLAG)
{
usbd_outept_handler(udev);
}
/* usb reset interrupt */
if(intsts & USB_OTG_USBRST_FLAG)
{
usbd_reset_handler(udev);
usbd_connectCallback(udev);
usb_global_clear_interrupt(usbx, USB_OTG_USBRST_FLAG);
}
/* sof interrupt */
if(intsts & USB_OTG_SOF_FLAG)
{
usbd_sof_handler(udev);
usbd_sof_callback(udev);
usb_global_clear_interrupt(usbx, USB_OTG_SOF_FLAG);
}
/* enumeration done interrupt */
if(intsts & USB_OTG_ENUMDONE_FLAG)
{
usbd_enumdone_handler(udev);
usb_global_clear_interrupt(usbx, USB_OTG_ENUMDONE_FLAG);
}
/* rx non-empty interrupt, indicates that there is at least one
data packet pending to be read in rx fifo */
if(intsts & USB_OTG_RXFLVL_FLAG)
{
usbd_rxflvl_handler(udev);
}
/* incomplete isochronous in transfer interrupt */
if(intsts & USB_OTG_INCOMISOIN_FLAG)
{
usbd_incomisioin_handler(udev);
usb_global_clear_interrupt(usbx, USB_OTG_INCOMISOIN_FLAG);
}
#ifndef USB_VBUS_IGNORE
/* disconnect detected interrupt */
if(intsts & USB_OTG_OTGINT_FLAG)
{
uint32_t tmp = udev->usb_reg->gotgint;
if(udev->usb_reg->gotgint_bit.sesenddet)
usbd_disconnectCallback(udev);
udev->usb_reg->gotgint = tmp;
usb_global_clear_interrupt(usbx, USB_OTG_OTGINT_FLAG);
}
#endif
/* incomplete isochronous out transfer interrupt */
if(intsts & USB_OTG_INCOMPIP_INCOMPISOOUT_FLAG)
{
usbd_incomisoout_handler(udev);
usb_global_clear_interrupt(usbx, USB_OTG_INCOMPIP_INCOMPISOOUT_FLAG);
}
/* resume/remote wakeup interrupt */
if(intsts & USB_OTG_WKUP_FLAG)
{
usbd_wakeup_handler(udev);
usb_global_clear_interrupt(usbx, USB_OTG_WKUP_FLAG);
}
/* usb suspend interrupt */
if(intsts & USB_OTG_USBSUSP_FLAG)
{
usbd_suspend_handler(udev);
usb_global_clear_interrupt(usbx, USB_OTG_USBSUSP_FLAG);
}
}
}
/**
* @brief usb write tx fifo.
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval none
*/
void usb_write_empty_txfifo(usbd_core_type *udev, uint32_t ept_num)
{
otg_global_type *usbx = udev->usb_reg;
usb_ept_info *ept_info = &udev->ept_in[ept_num];
uint32_t length = ept_info->total_len - ept_info->trans_len;
uint32_t wlen = 0;
if(length > ept_info->maxpacket)
{
length = ept_info->maxpacket;
}
wlen = (length + 3) / 4;
while((USB_INEPT(usbx, ept_num)->dtxfsts & USB_OTG_DTXFSTS_INEPTFSAV) > wlen &&
(ept_info->trans_len < ept_info->total_len) && (ept_info->total_len != 0))
{
length = ept_info->total_len - ept_info->trans_len;
if(length > ept_info->maxpacket)
{
length = ept_info->maxpacket;
}
wlen = (length + 3) / 4;
usb_write_packet(usbx, ept_info->trans_buf, ept_num, length);
ept_info->trans_buf += length;
ept_info->trans_len += length;
}
if(length <= 0)
{
OTG_DEVICE(usbx)->diepempmsk &= ~(0x1 << ept_num);
}
}
/**
* @brief usb in endpoint handler
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_inept_handler(usbd_core_type *udev)
{
otg_global_type *usbx = udev->usb_reg;
uint32_t ept_num = 0, ept_int;
uint32_t intsts;
/*get all endpoint interrut */
intsts = usb_get_all_in_interrupt(usbx);
while(intsts)
{
if(intsts & 0x1)
{
/* get endpoint interrupt flag */
ept_int = usb_ept_in_interrupt(usbx, ept_num);
/* transfer completed interrupt */
if(ept_int & USB_OTG_DIEPINT_XFERC_FLAG)
{
OTG_DEVICE(usbx)->diepempmsk &= ~(1 << ept_num);
usb_ept_in_clear(usbx, ept_num , USB_OTG_DIEPINT_XFERC_FLAG);
usbd_data_in_stage_callback(udev, ept_num);
}
/* timeout condition interrupt */
if(ept_int & USB_OTG_DIEPINT_TIMEOUT_FLAG)
{
usb_ept_in_clear(usbx, ept_num , USB_OTG_DIEPINT_TIMEOUT_FLAG);
}
/* in token received when tx fifo is empty */
if(ept_int & USB_OTG_DIEPINT_INTKNTXFEMP_FLAG)
{
usb_ept_in_clear(usbx, ept_num , USB_OTG_DIEPINT_INTKNTXFEMP_FLAG);
}
/* in endpoint nak effective */
if(ept_int & USB_OTG_DIEPINT_INEPTNAK_FLAG)
{
usb_ept_in_clear(usbx, ept_num , USB_OTG_DIEPINT_INEPTNAK_FLAG);
}
/* endpoint disable interrupt */
if(ept_int & USB_OTG_DIEPINT_EPTDISD_FLAG)
{
usb_ept_in_clear(usbx, ept_num , USB_OTG_DIEPINT_EPTDISD_FLAG);
}
/* transmit fifo empty interrupt */
if(ept_int & USB_OTG_DIEPINT_TXFEMP_FLAG)
{
usb_write_empty_txfifo(udev, ept_num);
}
}
ept_num ++;
intsts >>= 1;
}
}
/**
* @brief usb out endpoint handler
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_outept_handler(usbd_core_type *udev)
{
otg_global_type *usbx = udev->usb_reg;
uint32_t ept_num = 0, ept_int;
uint32_t intsts;
/* get all out endpoint interrupt */
intsts = usb_get_all_out_interrupt(usbx);
while(intsts)
{
if(intsts & 0x1)
{
/* get out endpoint interrupt */
ept_int = usb_ept_out_interrupt(usbx, ept_num);
/* transfer completed interrupt */
if(ept_int & USB_OTG_DOEPINT_XFERC_FLAG)
{
usb_ept_out_clear(usbx, ept_num , USB_OTG_DOEPINT_XFERC_FLAG);
usbd_data_out_stage_callback(udev, ept_num);
}
/* setup phase done interrupt */
if(ept_int & USB_OTG_DOEPINT_SETUP_FLAG)
{
usb_ept_out_clear(usbx, ept_num , USB_OTG_DOEPINT_SETUP_FLAG);
usbd_setup_phase_done_callback(udev);
if(udev->device_addr != 0)
{
OTG_DEVICE(udev->usb_reg)->dcfg_bit.devaddr = udev->device_addr;
udev->device_addr = 0;
}
}
/* endpoint disable interrupt */
if(ept_int & USB_OTG_DOEPINT_OUTTEPD_FLAG)
{
usb_ept_out_clear(usbx, ept_num , USB_OTG_DOEPINT_OUTTEPD_FLAG);
}
}
ept_num ++;
intsts >>= 1;
}
}
/**
* @brief usb enumeration done handler
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_enumdone_handler(usbd_core_type *udev)
{
otg_global_type *usbx = udev->usb_reg;
usb_ept0_setup(usbx);
usbx->gusbcfg_bit.usbtrdtim = USB_TRDTIM_16;
/* open endpoint 0 out */
usbd_ept_open(udev, 0x00, EPT_CONTROL_TYPE, 0x40);
/* open endpoint 0 in */
usbd_ept_open(udev, 0x80, EPT_CONTROL_TYPE, 0x40);
/* usb connect state set to default */
usbd_reset_callback(udev);
}
/**
* @brief usb rx non-empty handler
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_rxflvl_handler(usbd_core_type *udev)
{
otg_global_type *usbx = udev->usb_reg;
uint32_t stsp;
uint32_t count;
uint32_t pktsts;
usb_ept_info *ept_info;
/* disable rxflvl interrupt */
usb_global_interrupt_enable(usbx, USB_OTG_RXFLVL_INT, FALSE);
/* get rx status */
stsp = usbx->grxstsp;
/*get the byte count of receive */
count = (stsp & USB_OTG_GRXSTSP_BCNT) >> 4;
/* get packet status */
pktsts = (stsp &USB_OTG_GRXSTSP_PKTSTS) >> 17;
/* get endpoint infomation struct */
ept_info = &udev->ept_out[stsp & USB_OTG_GRXSTSP_EPTNUM];
/* received out data packet */
if(pktsts == USB_OUT_STS_DATA)
{
if(count != 0)
{
/* read packet to buffer */
usb_read_packet(usbx, ept_info->trans_buf, (stsp & USB_OTG_GRXSTSP_EPTNUM), count);
ept_info->trans_buf += count;
ept_info->trans_len += count;
}
}
/* setup data received */
else if ( pktsts == USB_SETUP_STS_DATA)
{
/* read packet to buffer */
usb_read_packet(usbx, udev->setup_buffer, (stsp & USB_OTG_GRXSTSP_EPTNUM), count);
ept_info->trans_len += count;
}
/* enable rxflvl interrupt */
usb_global_interrupt_enable(usbx, USB_OTG_RXFLVL_INT, TRUE);
}
/**
* @brief usb disconnect handler
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_discon_handler(usbd_core_type *udev)
{
/* disconnect callback handler */
if(udev->class_handler->event_handler != 0)
udev->class_handler->event_handler(udev, USBD_DISCONNECT_EVNET);
}
/**
* @brief usb incomplete out handler
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_incomisoout_handler(usbd_core_type *udev)
{
if(udev->class_handler->event_handler != 0)
udev->class_handler->event_handler(udev, USBD_OUTISOINCOM_EVENT);
}
/**
* @brief usb incomplete in handler
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_incomisioin_handler(usbd_core_type *udev)
{
if(udev->class_handler->event_handler != 0)
udev->class_handler->event_handler(udev, USBD_INISOINCOM_EVENT);
}
/**
* @brief usb device reset interrupt request handler.
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_reset_handler(usbd_core_type *udev)
{
otg_global_type *usbx = udev->usb_reg;
otg_device_type *dev = OTG_DEVICE(usbx);
uint32_t i_index = 0;
/* disable remote wakeup singal */
dev->dctl_bit.rwkupsig = FALSE;
/* endpoint fifo alloc */
usbd_fifo_alloc(udev);
/* flush all tx fifo */
usb_flush_tx_fifo(usbx, 0x10);
/* clear in and out endpoint interrupt flag */
for(i_index = 0; i_index < USB_EPT_MAX_NUM; i_index ++)
{
USB_INEPT(usbx, i_index)->diepint = 0xFF;
USB_OUTEPT(usbx, i_index)->doepint = 0xFF;
}
/* clear endpoint flag */
dev->daint = 0xFFFFFFFF;
/*clear endpoint interrupt mask */
dev->daintmsk = 0x10001;
/* enable out endpoint xfer, eptdis, setup interrupt mask */
dev->doepmsk_bit.xfercmsk = TRUE;
dev->doepmsk_bit.eptdismsk = TRUE;
dev->doepmsk_bit.setupmsk = TRUE;
/* enable in endpoint xfer, eptdis, timeout interrupt mask */
dev->diepmsk_bit.xfercmsk = TRUE;
dev->diepmsk_bit.eptdismsk = TRUE;
dev->diepmsk_bit.timeoutmsk = TRUE;
/* set device address to 0 */
usb_set_address(usbx, 0);
/* enable endpoint 0 */
usb_ept0_start(usbx);
/* usb connect state set to default */
udev->conn_state = USB_CONN_STATE_DEFAULT;
/* user define reset event */
if(udev->class_handler->event_handler)
udev->class_handler->event_handler(udev, USBD_RESET_EVENT);
}
/**
* @brief usb device sof interrupt request handler.
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_sof_handler(usbd_core_type *udev)
{
/* user sof handler in class define */
if(udev->class_handler->sof_handler)
udev->class_handler->sof_handler(udev);
}
/**
* @brief usb device suspend interrupt request handler.
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_suspend_handler(usbd_core_type *udev)
{
otg_global_type *usbx = udev->usb_reg;
if(OTG_DEVICE(usbx)->dsts_bit.suspsts)
{
/* save connect state */
udev->old_conn_state = udev->conn_state;
/* set current state to suspend */
udev->conn_state = USB_CONN_STATE_SUSPENDED;
/* enter suspend mode */
usbd_enter_suspend(udev);
/* user suspend handler */
if(udev->class_handler->event_handler != 0)
udev->class_handler->event_handler(udev, USBD_SUSPEND_EVENT);
}
}
/**
* @brief usb device wakup interrupt request handler.
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_wakeup_handler(usbd_core_type *udev)
{
otg_global_type *usbx = udev->usb_reg;
/* clear remote wakeup bit */
OTG_DEVICE(usbx)->dctl_bit.rwkupsig = FALSE;
/* exit suspend mode */
usb_open_phy_clk(udev->usb_reg);
/* restore connect state */
udev->conn_state = udev->old_conn_state;
/* user suspend handler */
if(udev->class_handler->event_handler != 0)
udev->class_handler->event_handler(udev, USBD_WAKEUP_EVENT);
}
__weak void usbd_reset_callback(usbd_core_type *udev)
{
}
__weak void usbd_setup_phase_done_callback(usbd_core_type *udev)
{
}
__weak void usbd_data_in_stage_callback(usbd_core_type *udev, uint32_t ept_num)
{
}
__weak void usbd_sof_callback(usbd_core_type *udev)
{
}
__weak void usbd_data_out_stage_callback(usbd_core_type *udev, uint32_t ept_num)
{
}
__weak void usbd_connectCallback(usbd_core_type *udev)
{
}
__weak void usbd_disconnectCallback(usbd_core_type *udev)
{
}
/**
* @}
*/
/**
* @}
*/
#endif
|