You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

101 lines
3.1 KiB

  1. /*
  2. * This file is part of the libopencm3 project.
  3. *
  4. * Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
  5. *
  6. * This library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this library. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <string.h>
  20. #include <libopencm3/cm3/common.h>
  21. #include <libopencm3/stm32/tools.h>
  22. #include <libopencm3/stm32/rcc.h>
  23. #include <libopencm3/usb/usbd.h>
  24. #include <libopencm3/usb/dwc/otg_fs.h>
  25. #include "usb_private.h"
  26. #include "usb_dwc_common.h"
  27. /* Receive FIFO size in 32-bit words. */
  28. #define RX_FIFO_SIZE 128
  29. static usbd_device *stm32f107_usbd_init(void);
  30. static struct _usbd_device usbd_dev;
  31. const struct _usbd_driver stm32f107_usb_driver = {
  32. .init = stm32f107_usbd_init,
  33. .set_address = dwc_set_address,
  34. .ep_setup = dwc_ep_setup,
  35. .ep_reset = dwc_endpoints_reset,
  36. .ep_stall_set = dwc_ep_stall_set,
  37. .ep_stall_get = dwc_ep_stall_get,
  38. .ep_nak_set = dwc_ep_nak_set,
  39. .ep_write_packet = dwc_ep_write_packet,
  40. .ep_read_packet = dwc_ep_read_packet,
  41. .poll = dwc_poll,
  42. .disconnect = dwc_disconnect,
  43. .base_address = USB_OTG_FS_BASE,
  44. .set_address_before_status = 1,
  45. .rx_fifo_size = RX_FIFO_SIZE,
  46. };
  47. /** Initialize the USB device controller hardware of the STM32. */
  48. static usbd_device *stm32f107_usbd_init(void)
  49. {
  50. rcc_periph_clock_enable(RCC_OTGFS);
  51. OTG_FS_GUSBCFG |= OTG_GUSBCFG_PHYSEL;
  52. /* Wait for AHB idle. */
  53. while (!(OTG_FS_GRSTCTL & OTG_GRSTCTL_AHBIDL));
  54. /* Do core soft reset. */
  55. OTG_FS_GRSTCTL |= OTG_GRSTCTL_CSRST;
  56. while (OTG_FS_GRSTCTL & OTG_GRSTCTL_CSRST);
  57. if (OTG_FS_CID >= OTG_CID_HAS_VBDEN) {
  58. /* Enable VBUS detection in device mode and power up the PHY. */
  59. OTG_FS_GCCFG |= OTG_GCCFG_VBDEN | OTG_GCCFG_PWRDWN;
  60. } else {
  61. /* Enable VBUS sensing in device mode and power up the PHY. */
  62. OTG_FS_GCCFG |= OTG_GCCFG_VBUSBSEN | OTG_GCCFG_PWRDWN;
  63. }
  64. /* Explicitly enable DP pullup (not all cores do this by default) */
  65. OTG_FS_DCTL &= ~OTG_DCTL_SDIS;
  66. /* Force peripheral only mode. */
  67. OTG_FS_GUSBCFG |= OTG_GUSBCFG_FDMOD | OTG_GUSBCFG_TRDT_MASK;
  68. OTG_FS_GINTSTS = OTG_GINTSTS_MMIS;
  69. /* Full speed device. */
  70. OTG_FS_DCFG |= OTG_DCFG_DSPD;
  71. /* Restart the PHY clock. */
  72. OTG_FS_PCGCCTL = 0;
  73. OTG_FS_GRXFSIZ = stm32f107_usb_driver.rx_fifo_size;
  74. usbd_dev.fifo_mem_top = stm32f107_usb_driver.rx_fifo_size;
  75. /* Unmask interrupts for TX and RX. */
  76. OTG_FS_GAHBCFG |= OTG_GAHBCFG_GINT;
  77. OTG_FS_GINTMSK = OTG_GINTMSK_ENUMDNEM |
  78. OTG_GINTMSK_RXFLVLM |
  79. OTG_GINTMSK_IEPINT |
  80. OTG_GINTMSK_USBSUSPM |
  81. OTG_GINTMSK_WUIM;
  82. OTG_FS_DAINTMSK = 0xF;
  83. OTG_FS_DIEPMSK = OTG_DIEPMSK_XFRCM;
  84. return &usbd_dev;
  85. }