Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

93 рядки
2.8 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_hs.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 512
  29. static usbd_device *stm32f207_usbd_init(void);
  30. static struct _usbd_device usbd_dev;
  31. const struct _usbd_driver stm32f207_usb_driver = {
  32. .init = stm32f207_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_HS_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 *stm32f207_usbd_init(void)
  49. {
  50. rcc_periph_clock_enable(RCC_OTGHS);
  51. OTG_HS_GINTSTS = OTG_GINTSTS_MMIS;
  52. OTG_HS_GUSBCFG |= OTG_GUSBCFG_PHYSEL;
  53. /* Enable VBUS sensing in device mode and power down the PHY. */
  54. OTG_HS_GCCFG |= OTG_GCCFG_VBUSBSEN | OTG_GCCFG_PWRDWN;
  55. /* Wait for AHB idle. */
  56. while (!(OTG_HS_GRSTCTL & OTG_GRSTCTL_AHBIDL));
  57. /* Do core soft reset. */
  58. OTG_HS_GRSTCTL |= OTG_GRSTCTL_CSRST;
  59. while (OTG_HS_GRSTCTL & OTG_GRSTCTL_CSRST);
  60. /* Force peripheral only mode. */
  61. OTG_HS_GUSBCFG |= OTG_GUSBCFG_FDMOD | OTG_GUSBCFG_TRDT_MASK;
  62. /* Full speed device. */
  63. OTG_HS_DCFG |= OTG_DCFG_DSPD;
  64. /* Restart the PHY clock. */
  65. OTG_HS_PCGCCTL = 0;
  66. OTG_HS_GRXFSIZ = stm32f207_usb_driver.rx_fifo_size;
  67. usbd_dev.fifo_mem_top = stm32f207_usb_driver.rx_fifo_size;
  68. /* Unmask interrupts for TX and RX. */
  69. OTG_HS_GAHBCFG |= OTG_GAHBCFG_GINT;
  70. OTG_HS_GINTMSK = OTG_GINTMSK_ENUMDNEM |
  71. OTG_GINTMSK_RXFLVLM |
  72. OTG_GINTMSK_IEPINT |
  73. OTG_GINTMSK_USBSUSPM |
  74. OTG_GINTMSK_WUIM;
  75. OTG_HS_DAINTMSK = 0xF;
  76. OTG_HS_DIEPMSK = OTG_DIEPMSK_XFRCM;
  77. return &usbd_dev;
  78. }