FIX CONFIG: BARO (#12476)
[betaflight.git] / src / main / drivers / at32 / usb / usbd_core.c
blobe3b8b8751210070bb151a684d45dfbfa805127b1
1 /**
2 **************************************************************************
3 * @file usbd_core.c
4 * @brief usb device driver
5 **************************************************************************
6 * Copyright notice & Disclaimer
8 * The software Board Support Package (BSP) that is made available to
9 * download from Artery official website is the copyrighted work of Artery.
10 * Artery authorizes customers to use, copy, and distribute the BSP
11 * software and its related documentation for the purpose of design and
12 * development in conjunction with Artery microcontrollers. Use of the
13 * software is governed by this copyright notice and the following disclaimer.
15 * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
16 * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
17 * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
18 * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
19 * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
22 **************************************************************************
25 #include "platform.h"
27 #include "usb_core.h"
28 #include "usbd_core.h"
29 #include "usbd_sdr.h"
31 /** @addtogroup AT32F435_437_middlewares_usbd_drivers
32 * @{
35 /** @defgroup USBD_drivers_core
36 * @brief usb device drivers core
37 * @{
40 /** @defgroup USBD_core_private_functions
41 * @{
44 /**
45 * @brief usb core in transfer complete handler
46 * @param udev: to the structure of usbd_core_type
47 * @param ept_addr: endpoint number
48 * @retval none
50 void usbd_core_in_handler(usbd_core_type *udev, uint8_t ept_addr)
52 /* get endpoint info*/
53 usb_ept_info *ept_info = &udev->ept_in[ept_addr & 0x7F];
55 if(ept_addr == 0)
57 if(udev->ept0_sts == USB_EPT0_DATA_IN)
59 if(ept_info->rem0_len > ept_info->maxpacket)
61 ept_info->rem0_len -= ept_info->maxpacket;
62 usbd_ept_send(udev, 0, ept_info->trans_buf,
63 MIN(ept_info->rem0_len, ept_info->maxpacket));
65 /* endpoint 0 */
66 else if(ept_info->last_len == ept_info->maxpacket
67 && ept_info->ept0_slen >= ept_info->maxpacket
68 && ept_info->ept0_slen < udev->ept0_wlength)
70 ept_info->last_len = 0;
71 usbd_ept_send(udev, 0, 0, 0);
72 usbd_ept_recv(udev, ept_addr, 0, 0);
74 else
77 if(udev->class_handler->ept0_tx_handler != 0 &&
78 udev->conn_state == USB_CONN_STATE_CONFIGURED)
80 udev->class_handler->ept0_tx_handler(udev);
82 usbd_ctrl_recv_status(udev);
87 else if(udev->class_handler->in_handler != 0 &&
88 udev->conn_state == USB_CONN_STATE_CONFIGURED)
90 /* other user define endpoint */
91 udev->class_handler->in_handler(udev, ept_addr);
95 /**
96 * @brief usb core out transfer complete handler
97 * @param udev: to the structure of usbd_core_type
98 * @param ept_addr: endpoint number
99 * @retval none
101 void usbd_core_out_handler(usbd_core_type *udev, uint8_t ept_addr)
103 /* get endpoint info*/
104 usb_ept_info *ept_info = &udev->ept_out[ept_addr & 0x7F];
106 if(ept_addr == 0)
108 /* endpoint 0 */
109 if(udev->ept0_sts == USB_EPT0_DATA_OUT)
111 if(ept_info->rem0_len > ept_info->maxpacket)
113 ept_info->rem0_len -= ept_info->maxpacket;
114 usbd_ept_recv(udev, ept_addr, ept_info->trans_buf,
115 MIN(ept_info->rem0_len, ept_info->maxpacket));
117 else
119 if(udev->class_handler->ept0_rx_handler != 0)
121 udev->class_handler->ept0_rx_handler(udev);
123 usbd_ctrl_send_status(udev);
127 else if(udev->class_handler->out_handler != 0 &&
128 udev->conn_state == USB_CONN_STATE_CONFIGURED)
130 /* other user define endpoint */
131 udev->class_handler->out_handler(udev, ept_addr);
136 * @brief usb core setup transfer complete handler
137 * @param udev: to the structure of usbd_core_type
138 * @param ept_addr: endpoint number
139 * @retval none
141 void usbd_core_setup_handler(usbd_core_type *udev, uint8_t ept_num)
143 UNUSED(ept_num);
145 /* setup parse */
146 usbd_setup_request_parse(&udev->setup, udev->setup_buffer);
148 /* set ept0 status */
149 udev->ept0_sts = USB_EPT0_SETUP;
150 udev->ept0_wlength = udev->setup.wLength;
152 switch(udev->setup.bmRequestType & USB_REQ_RECIPIENT_MASK)
154 case USB_REQ_RECIPIENT_DEVICE:
155 /* recipient device request */
156 usbd_device_request(udev);
157 break;
158 case USB_REQ_RECIPIENT_INTERFACE:
159 /* recipient interface request */
160 usbd_interface_request(udev);
161 break;
162 case USB_REQ_RECIPIENT_ENDPOINT:
163 /* recipient endpoint request */
164 usbd_endpoint_request(udev);
165 break;
166 default:
167 break;
172 * @brief usb control endpoint send data
173 * @param udev: to the structure of usbd_core_type
174 * @param ept_addr: endpoint number
175 * @param buffer: send data buffer
176 * @param len: send data length
177 * @retval none
179 void usbd_ctrl_send(usbd_core_type *udev, uint8_t *buffer, uint16_t len)
181 usb_ept_info *ept_info = &udev->ept_in[0];
183 ept_info->ept0_slen = len;
184 ept_info->rem0_len = len;
185 udev->ept0_sts = USB_EPT0_DATA_IN;
187 usbd_ept_send(udev, 0, buffer, len);
191 * @brief usb control endpoint recv data
192 * @param udev: to the structure of usbd_core_type
193 * @param ept_addr: endpoint number
194 * @param buffer: recv data buffer
195 * @param len: recv data length
196 * @retval none
198 void usbd_ctrl_recv(usbd_core_type *udev, uint8_t *buffer, uint16_t len)
200 usb_ept_info *ept_info = &udev->ept_out[0];
202 ept_info->ept0_slen = len;
203 ept_info->rem0_len = len;
204 udev->ept0_sts = USB_EPT0_DATA_OUT;
206 usbd_ept_recv(udev, 0, buffer, len);
210 * @brief usb control endpoint send in status
211 * @param udev: to the structure of usbd_core_type
212 * @retval none
214 void usbd_ctrl_send_status(usbd_core_type *udev)
216 udev->ept0_sts = USB_EPT0_STATUS_IN;
218 usbd_ept_send(udev, 0, 0, 0);
222 * @brief usb control endpoint send out status
223 * @param udev: to the structure of usbd_core_type
224 * @retval none
226 void usbd_ctrl_recv_status(usbd_core_type *udev)
228 udev->ept0_sts = USB_EPT0_STATUS_OUT;
230 usbd_ept_recv(udev, 0, 0, 0);
234 * @brief clear endpoint stall
235 * @param udev: to the structure of usbd_core_type
236 * @param ept_addr: endpoint number
237 * @retval none
239 void usbd_clear_stall(usbd_core_type *udev, uint8_t ept_addr)
241 usb_ept_info *ept_info;
242 usb_reg_type *usbx = udev->usb_reg;
244 if(ept_addr & 0x80)
246 /* in endpoint */
247 ept_info = &udev->ept_in[ept_addr & 0x7F];
249 else
251 /* out endpoint */
252 ept_info = &udev->ept_out[ept_addr & 0x7F];
254 usb_ept_clear_stall(usbx, ept_info);
255 ept_info->stall = 0;
259 * @brief usb set endpoint to stall status
260 * @param udev: to the structure of usbd_core_type
261 * @param ept_addr: endpoint number
262 * @retval none
264 void usbd_set_stall(usbd_core_type *udev, uint8_t ept_addr)
266 usb_ept_info *ept_info;
267 usb_reg_type *usbx = udev->usb_reg;
269 if(ept_addr & 0x80)
271 /* in endpoint */
272 ept_info = &udev->ept_in[ept_addr & 0x7F];
274 else
276 /* out endpoint */
277 ept_info = &udev->ept_out[ept_addr & 0x7F];
279 usb_ept_stall(usbx, ept_info);
281 ept_info->stall = 1;
285 * @brief un-support device request
286 * @param udev: to the structure of usbd_core_type
287 * @retval none
289 void usbd_ctrl_unsupport(usbd_core_type *udev)
291 /* return stall status */
292 usbd_set_stall(udev, 0x00);
293 usbd_set_stall(udev, 0x80);
297 * @brief get endpoint receive data length
298 * @param udev: to the structure of usbd_core_type
299 * @param ept_addr: endpoint number
300 * @retval data receive len
302 uint32_t usbd_get_recv_len(usbd_core_type *udev, uint8_t ept_addr)
304 usb_ept_info *ept = &udev->ept_out[ept_addr & 0x7F];
305 return ept->trans_len;
309 * @brief usb open endpoint
310 * @param udev: to the structure of usbd_core_type
311 * @param ept_addr: endpoint number
312 * @param ept_type: endpoint type
313 * @param maxpacket: endpoint support max buffer size
314 * @retval none
316 void usbd_ept_open(usbd_core_type *udev, uint8_t ept_addr, uint8_t ept_type, uint16_t maxpacket)
318 usb_reg_type *usbx = udev->usb_reg;
319 usb_ept_info *ept_info;
321 if((ept_addr & 0x80) == 0)
323 /* out endpoint info */
324 ept_info = &udev->ept_out[ept_addr & 0x7F];
325 ept_info->inout = EPT_DIR_OUT;
327 else
329 /* in endpoint info */
330 ept_info = &udev->ept_in[ept_addr & 0x7F];
331 ept_info->inout = EPT_DIR_IN;
334 /* set endpoint maxpacket and type */
335 ept_info->maxpacket = maxpacket;
336 ept_info->trans_type = ept_type;
338 /* open endpoint */
339 usb_ept_open(usbx, ept_info);
343 * @brief usb close endpoint
344 * @param udev: to the structure of usbd_core_type
345 * @param ept_addr: endpoint number
346 * @retval none
348 void usbd_ept_close(usbd_core_type *udev, uint8_t ept_addr)
350 usb_ept_info *ept_info;
351 if(ept_addr & 0x80)
353 /* in endpoint */
354 ept_info = &udev->ept_in[ept_addr & 0x7F];
356 else
358 /* out endpoint */
359 ept_info = &udev->ept_out[ept_addr & 0x7F];
362 /* close endpoint */
363 usb_ept_close(udev->usb_reg, ept_info);
367 * @brief usb device connect to host
368 * @param udev: to the structure of usbd_core_type
369 * @retval none
371 void usbd_connect(usbd_core_type *udev)
373 usb_connect(udev->usb_reg);
377 * @brief usb device disconnect to host
378 * @param udev: to the structure of usbd_core_type
379 * @retval none
381 void usbd_disconnect(usbd_core_type *udev)
383 usb_disconnect(udev->usb_reg);
387 * @brief usb device set device address.
388 * @param udev: to the structure of usbd_core_type
389 * @param address: host assignment address
390 * @retval none
392 void usbd_set_device_addr(usbd_core_type *udev, uint8_t address)
394 usb_set_address(udev->usb_reg, address);
398 * @brief usb endpoint structure initialization
399 * @param udev: to the structure of usbd_core_type
400 * @retval none
402 void usb_ept_default_init(usbd_core_type *udev)
404 uint8_t i_index = 0;
405 /* init in endpoint info structure */
406 for(i_index = 0; i_index < USB_EPT_MAX_NUM; i_index ++)
408 udev->ept_in[i_index].eptn = i_index;
409 udev->ept_in[i_index].ept_address = i_index;
410 udev->ept_in[i_index].inout = EPT_DIR_IN;
411 udev->ept_in[i_index].maxpacket = 0;
412 udev->ept_in[i_index].trans_buf = 0;
413 udev->ept_in[i_index].total_len = 0;
416 /* init out endpoint info structure */
417 for(i_index = 0; i_index < USB_EPT_MAX_NUM; i_index ++)
419 udev->ept_out[i_index].eptn = i_index;
420 udev->ept_out[i_index].ept_address = i_index;
421 udev->ept_out[i_index].inout = EPT_DIR_OUT;
422 udev->ept_out[i_index].maxpacket = 0;
423 udev->ept_out[i_index].trans_buf = 0;
424 udev->ept_out[i_index].total_len = 0;
429 * @brief endpoint send data
430 * @param udev: to the structure of usbd_core_type
431 * @param ept_addr: endpoint number
432 * @param buffer: send data buffer
433 * @param len: send data length
434 * @retval none
436 void usbd_ept_send(usbd_core_type *udev, uint8_t ept_addr, uint8_t *buffer, uint16_t len)
438 /* get endpoint info struct and register */
439 usb_reg_type *usbx = udev->usb_reg;
440 usb_ept_info *ept_info = &udev->ept_in[ept_addr & 0x7F];
441 otg_eptin_type *ept_in = USB_INEPT(usbx, ept_info->eptn);
442 otg_device_type *dev = OTG_DEVICE(usbx);
443 uint32_t pktcnt;
445 /* set send data buffer and length */
446 ept_info->trans_buf = buffer;
447 ept_info->total_len = len;
448 ept_info->trans_len = 0;
450 /* transfer data len is zero */
451 if(ept_info->total_len == 0)
453 ept_in->dieptsiz_bit.pktcnt = 1;
454 ept_in->dieptsiz_bit.xfersize = 0;
456 else
458 if((ept_addr & 0x7F) == 0) // endpoint 0
460 /* endpoint 0 */
461 if(ept_info->total_len > ept_info->maxpacket)
463 ept_info->total_len = ept_info->maxpacket;
466 /* set transfer size */
467 ept_in->dieptsiz_bit.xfersize = ept_info->total_len;
469 /* set packet count */
470 ept_in->dieptsiz_bit.pktcnt = 1;
472 ept_info->last_len = ept_info->total_len;
474 else
476 /* other endpoint */
478 /* packet count */
479 pktcnt = (ept_info->total_len + ept_info->maxpacket - 1) / ept_info->maxpacket;
481 /* set transfer size */
482 ept_in->dieptsiz_bit.xfersize = ept_info->total_len;
484 /* set packet count */
485 ept_in->dieptsiz_bit.pktcnt = pktcnt;
487 if(ept_info->trans_type == EPT_ISO_TYPE)
489 ept_in->dieptsiz_bit.mc = 1;
494 if(ept_info->trans_type != EPT_ISO_TYPE)
496 if(ept_info->total_len > 0)
498 /* set in endpoint tx fifo empty interrupt mask */
499 dev->diepempmsk |= 1 << ept_info->eptn;
503 if(ept_info->trans_type == EPT_ISO_TYPE)
505 if((dev->dsts_bit.soffn & 0x1) == 0)
507 ept_in->diepctl_bit.setd1pid = TRUE;
509 else
511 ept_in->diepctl_bit.setd0pid = TRUE;
515 /* clear endpoint nak */
516 ept_in->diepctl_bit.cnak = TRUE;
518 /* endpoint enable */
519 ept_in->diepctl_bit.eptena = TRUE;
521 if(ept_info->trans_type == EPT_ISO_TYPE)
523 /* write data to fifo */
524 usb_write_packet(usbx, ept_info->trans_buf, ept_info->eptn, ept_info->total_len);
529 * @brief endpoint receive data
530 * @param udev: to the structure of usbd_core_type
531 * @param ept_addr: endpoint number
532 * @param buffer: receive data buffer
533 * @param len: receive data length
534 * @retval none
536 void usbd_ept_recv(usbd_core_type *udev, uint8_t ept_addr, uint8_t *buffer, uint16_t len)
538 /* get endpoint info struct and register */
539 usb_reg_type *usbx = udev->usb_reg;
540 usb_ept_info *ept_info = &udev->ept_out[ept_addr & 0x7F];
541 otg_eptout_type *ept_out = USB_OUTEPT(usbx, ept_info->eptn);
542 otg_device_type *dev = OTG_DEVICE(usbx);
543 uint32_t pktcnt;
545 /* set receive data buffer and length */
546 ept_info->trans_buf = buffer;
547 ept_info->total_len = len;
548 ept_info->trans_len = 0;
550 if((ept_addr & 0x7F) == 0)
552 /* endpoint 0 */
553 ept_info->total_len = ept_info->maxpacket;
556 if(ept_info->total_len == 0 || ((ept_addr & 0x7F) == 0))
558 /* set transfer size */
559 ept_out->doeptsiz_bit.xfersize = ept_info->maxpacket;
561 /* set packet count */
562 ept_out->doeptsiz_bit.pktcnt = 1;
564 else
566 pktcnt = (ept_info->total_len + ept_info->maxpacket - 1) / ept_info->maxpacket;
568 /* set transfer size */
569 ept_out->doeptsiz_bit.xfersize = ept_info->maxpacket * pktcnt;
571 /* set packet count */
572 ept_out->doeptsiz_bit.pktcnt = pktcnt;
575 if(ept_info->trans_type == EPT_ISO_TYPE)
577 if((dev->dsts_bit.soffn & 0x01) == 0)
579 ept_out->doepctl_bit.setd1pid = TRUE;
581 else
583 ept_out->doepctl_bit.setd0pid = TRUE;
587 /* clear endpoint nak */
588 ept_out->doepctl_bit.cnak = TRUE;
590 /* endpoint enable */
591 ept_out->doepctl_bit.eptena = TRUE;
595 * @brief get usb connect state
596 * @param udev: to the structure of usbd_core_type
597 * @retval usb connect state
599 usbd_conn_state usbd_connect_state_get(usbd_core_type *udev)
601 return udev->conn_state;
605 * @brief usb device remote wakeup
606 * @param udev: to the structure of usbd_core_type
607 * @retval none
609 void usbd_remote_wakeup(usbd_core_type *udev)
611 /* check device is in suspend mode */
612 if(usb_suspend_status_get(udev->usb_reg) == 1)
614 /* set connect state */
615 udev->conn_state = udev->old_conn_state;
617 /* open phy clock */
618 usb_open_phy_clk(udev->usb_reg);
620 /* set remote wakeup */
621 usb_remote_wkup_set(udev->usb_reg);
623 /* delay 10 ms */
624 usb_delay_ms(10);
626 /* clear remote wakup */
627 usb_remote_wkup_clear(udev->usb_reg);
632 * @brief usb device enter suspend mode
633 * @param udev: to the structure of usbd_core_type
634 * @retval none
636 void usbd_enter_suspend(usbd_core_type *udev)
638 /* check device is in suspend mode */
639 if(usb_suspend_status_get(udev->usb_reg) == 1)
641 /* stop phy clk */
642 usb_stop_phy_clk(udev->usb_reg);
647 * @brief usb device flush in endpoint fifo
648 * @param udev: to the structure of usbd_core_type
649 * @param ept_num: endpoint number
650 * @retval none
652 void usbd_flush_tx_fifo(usbd_core_type *udev, uint8_t ept_num)
654 /* flush endpoint tx fifo */
655 usb_flush_tx_fifo(udev->usb_reg, ept_num & 0x1F);
659 * @brief usb device endpoint fifo alloc
660 * @param udev: to the structure of usbd_core_type
661 * @retval none
663 void usbd_fifo_alloc(usbd_core_type *udev)
665 usb_reg_type *usbx = udev->usb_reg;
667 if(usbx == OTG1_GLOBAL)
669 /* set receive fifo size */
670 usb_set_rx_fifo(usbx, USBD_RX_SIZE);
672 /* set endpoint0 tx fifo size */
673 usb_set_tx_fifo(usbx, USB_EPT0, USBD_EP0_TX_SIZE);
675 /* set endpoint1 tx fifo size */
676 usb_set_tx_fifo(usbx, USB_EPT1, USBD_EP1_TX_SIZE);
678 /* set endpoint2 tx fifo size */
679 usb_set_tx_fifo(usbx, USB_EPT2, USBD_EP2_TX_SIZE);
681 /* set endpoint3 tx fifo size */
682 usb_set_tx_fifo(usbx, USB_EPT3, USBD_EP3_TX_SIZE);
684 if(USB_EPT_MAX_NUM == 8)
686 /* set endpoint4 tx fifo size */
687 usb_set_tx_fifo(usbx, USB_EPT4, USBD_EP4_TX_SIZE);
689 /* set endpoint5 tx fifo size */
690 usb_set_tx_fifo(usbx, USB_EPT5, USBD_EP5_TX_SIZE);
692 /* set endpoint6 tx fifo size */
693 usb_set_tx_fifo(usbx, USB_EPT6, USBD_EP6_TX_SIZE);
695 /* set endpoint7 tx fifo size */
696 usb_set_tx_fifo(usbx, USB_EPT7, USBD_EP7_TX_SIZE);
699 #ifdef OTG2_GLOBAL
700 if(usbx == OTG2_GLOBAL)
702 /* set receive fifo size */
703 usb_set_rx_fifo(usbx, USBD2_RX_SIZE);
705 /* set endpoint0 tx fifo size */
706 usb_set_tx_fifo(usbx, USB_EPT0, USBD2_EP0_TX_SIZE);
708 /* set endpoint1 tx fifo size */
709 usb_set_tx_fifo(usbx, USB_EPT1, USBD2_EP1_TX_SIZE);
711 /* set endpoint2 tx fifo size */
712 usb_set_tx_fifo(usbx, USB_EPT2, USBD2_EP2_TX_SIZE);
714 /* set endpoint3 tx fifo size */
715 usb_set_tx_fifo(usbx, USB_EPT3, USBD2_EP3_TX_SIZE);
717 if(USB_EPT_MAX_NUM == 8)
719 /* set endpoint4 tx fifo size */
720 usb_set_tx_fifo(usbx, USB_EPT4, USBD2_EP4_TX_SIZE);
722 /* set endpoint5 tx fifo size */
723 usb_set_tx_fifo(usbx, USB_EPT5, USBD2_EP5_TX_SIZE);
725 /* set endpoint6 tx fifo size */
726 usb_set_tx_fifo(usbx, USB_EPT6, USBD2_EP6_TX_SIZE);
728 /* set endpoint7 tx fifo size */
729 usb_set_tx_fifo(usbx, USB_EPT7, USBD2_EP7_TX_SIZE);
732 #endif
736 * @brief usb device core initialization
737 * @param udev: to the structure of usbd_core_type
738 * @param usb_reg: usb otgfs peripheral global register
739 * this parameter can be one of the following values:
740 * OTG1_GLOBAL , OTG2_GLOBAL
741 * @param class_handler: usb class handler
742 * @param desc_handler: device config handler
743 * @param core_id: usb core id number
744 * @retval usb_sts_type
746 usb_sts_type usbd_core_init(usbd_core_type *udev,
747 usb_reg_type *usb_reg,
748 usbd_class_handler *class_handler,
749 usbd_desc_handler *desc_handler,
750 uint8_t core_id)
752 UNUSED(core_id);
753 usb_reg_type *usbx;
754 otg_device_type *dev;
755 otg_eptin_type *ept_in;
756 otg_eptout_type *ept_out;
757 uint32_t i_index;
759 udev->usb_reg = usb_reg;
760 usbx = usb_reg;
761 dev = OTG_DEVICE(usbx);
763 /* set connect state */
764 udev->conn_state = USB_CONN_STATE_DEFAULT;
766 /* device class config */
767 udev->device_addr = 0;
768 udev->class_handler = class_handler;
769 udev->desc_handler = desc_handler;
770 /* set device disconnect */
771 usbd_disconnect(udev);
773 /* set endpoint to default status */
774 usb_ept_default_init(udev);
776 /* disable usb global interrupt */
777 usb_interrupt_disable(usbx);
779 /* init global register */
780 usb_global_init(usbx);
782 /* set device mode */
783 usb_global_set_mode(usbx, OTG_DEVICE_MODE);
785 /* open phy clock */
786 usb_open_phy_clk(udev->usb_reg);
788 /* set periodic frame interval */
789 dev->dcfg_bit.perfrint = DCFG_PERFRINT_80;
791 /* set device speed to full-speed */
792 dev->dcfg_bit.devspd = USB_DCFG_FULL_SPEED;
794 /* flush all tx fifo */
795 usb_flush_tx_fifo(usbx, 16);
797 /* flush share rx fifo */
798 usb_flush_rx_fifo(usbx);
800 /* clear all endpoint interrupt flag and mask */
801 dev->daint = 0xFFFFFFFF;
802 dev->daintmsk = 0;
803 dev->diepmsk = 0;
804 dev->doepmsk = 0;
806 for(i_index = 0; i_index < USB_EPT_MAX_NUM; i_index ++)
808 usbx->dieptxfn[i_index] = 0;
811 /* endpoint fifo alloc */
812 usbd_fifo_alloc(udev);
814 /* disable all in endpoint */
815 for(i_index = 0; i_index < USB_EPT_MAX_NUM; i_index ++)
817 ept_in = USB_INEPT(usbx, i_index);
818 if(ept_in->diepctl_bit.eptena)
820 ept_in->diepctl = 0;
821 ept_in->diepctl_bit.eptdis = TRUE;
822 ept_in->diepctl_bit.snak = TRUE;
824 else
826 ept_in->diepctl = 0;
828 ept_in->dieptsiz = 0;
829 ept_in->diepint = 0xFF;
832 /* disable all out endpoint */
833 for(i_index = 0; i_index < USB_EPT_MAX_NUM; i_index ++)
835 ept_out = USB_OUTEPT(usbx, i_index);
836 if(ept_out->doepctl_bit.eptena)
838 ept_out->doepctl = 0;
839 ept_out->doepctl_bit.eptdis = TRUE;
840 ept_out->doepctl_bit.snak = TRUE;
842 else
844 ept_out->doepctl = 0;
846 ept_out->doeptsiz = 0;
847 ept_out->doepint = 0xFF;
849 dev->diepmsk_bit.txfifoudrmsk = TRUE;
851 /* clear global interrupt and mask */
852 usbx->gintmsk = 0;
853 usbx->gintsts = 0xBFFFFFFF;
855 /* enable global interrupt mask */
856 usbx->gintmsk = USB_OTG_SOF_INT | USB_OTG_RXFLVL_INT |
857 USB_OTG_USBSUSP_INT | USB_OTG_USBRST_INT |
858 USB_OTG_ENUMDONE_INT | USB_OTG_IEPT_INT |
859 USB_OTG_OEPT_INT | USB_OTG_INCOMISOIN_INT |
860 USB_OTG_INCOMPIP_INCOMPISOOUT_INT | USB_OTG_WKUP_INT |
861 USB_OTG_OTGINT_INT;
863 /* usb connect */
864 usbd_connect(udev);
866 /* enable global interrupt */
867 usb_interrupt_enable(usbx);
869 return USB_OK;
874 * @}
878 * @}
882 * @}