PageRenderTime 28ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/external/mbed-os/features/unsupported/USBDevice/USBDevice/TARGET_STM/USBHAL_STM32F303ZE.h

https://github.com/adamgreen/gcc4mbed
C Header | 128 lines | 92 code | 6 blank | 30 comment | 0 complexity | 687a29233f02fe43fb6df1d4d9b9a3ad MD5 | raw file
  1. /* Copyright (c) 2016 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without
  5. * restriction, including without limitation the rights to use, copy, modify, merge, publish,
  6. * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #ifndef USBHAL_STM32F303ZE_H
  19. #define USBHAL_STM32F303ZE_H
  20. #define USBHAL_IRQn USB_LP_CAN_RX0_IRQn
  21. /* must be multiple of 4 bytes */
  22. #define NB_ENDPOINT 8
  23. #define MAXTRANSFER_SIZE 0x200
  24. #define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3)
  25. #if (FIFO_USB_RAM_SIZE > 0x500)
  26. #error "FIFO dimensioning incorrect"
  27. #endif
  28. typedef struct
  29. {
  30. USBHAL *inst;
  31. void (USBHAL::*bus_reset)(void);
  32. void (USBHAL::*sof)(int frame);
  33. void (USBHAL::*connect_change)(unsigned int connected);
  34. void (USBHAL::*suspend_change)(unsigned int suspended);
  35. void (USBHAL::*ep0_setup)(void);
  36. void (USBHAL::*ep0_in)(void);
  37. void (USBHAL::*ep0_out)(void);
  38. void (USBHAL::*ep0_read)(void);
  39. bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
  40. bool (USBHAL::*epCallback[6])(void);
  41. uint8_t epComplete[2*NB_ENDPOINT];
  42. /* memorize dummy buffer used for reception */
  43. uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
  44. uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
  45. gpio_t usb_switch;
  46. }USBHAL_Private_t;
  47. uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo)
  48. {
  49. return 1024;
  50. }
  51. void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state){
  52. USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
  53. gpio_write(&(priv->usb_switch),state);
  54. }
  55. void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) {
  56. USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
  57. USBHAL *obj= priv->inst;
  58. uint32_t sofnum = (hpcd->Instance->FNR) & USB_FNR_FN;
  59. void (USBHAL::*func)(int frame) = priv->sof;
  60. /* fix me call with same frame number */
  61. (obj->*func)(sofnum);
  62. }
  63. USBHAL * USBHAL::instance;
  64. USBHAL::USBHAL(void) {
  65. /* init parameter */
  66. USBHAL_Private_t *HALPriv = new(USBHAL_Private_t);
  67. hpcd.Instance = USB;
  68. /* initialized Init to zero (constructor does not zero initialized the
  69. * area */
  70. /* initialized all field of init including 0 field */
  71. /* constructor does not fill with zero */
  72. memset(&hpcd.Init, 0, sizeof(hpcd.Init));
  73. hpcd.Init.dev_endpoints = NB_ENDPOINT;
  74. hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0;
  75. hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
  76. hpcd.Init.Sof_enable = 1;
  77. hpcd.Init.speed = PCD_SPEED_FULL;
  78. /* pass instance for usage inside call back */
  79. HALPriv->inst = this;
  80. HALPriv->bus_reset = &USBHAL::busReset;
  81. HALPriv->suspend_change = &USBHAL::suspendStateChanged;
  82. HALPriv->connect_change = &USBHAL::connectStateChanged;
  83. HALPriv->sof = &USBHAL::SOF;
  84. HALPriv->ep0_setup = &USBHAL::EP0setupCallback;
  85. HALPriv->ep_realise = &USBHAL::realiseEndpoint;
  86. HALPriv->ep0_in = &USBHAL::EP0in;
  87. HALPriv->ep0_out = &USBHAL::EP0out;
  88. HALPriv->ep0_read = &USBHAL::EP0read;
  89. hpcd.pData = (void*)HALPriv;
  90. HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback;
  91. HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback;
  92. HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback;
  93. HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback;
  94. HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback;
  95. HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback;
  96. instance = this;
  97. __HAL_RCC_GPIOA_CLK_ENABLE();
  98. /* Configure USB DM pin. This is optional, and maintained only for user guidance. */
  99. pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF14_USB));
  100. pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF14_USB));
  101. __HAL_RCC_GPIOG_CLK_ENABLE();
  102. gpio_init_out(&HALPriv->usb_switch,PG_6);
  103. /* Enable USB Clock */
  104. __HAL_RCC_USB_CLK_ENABLE();
  105. /* Enable SYSCFG Clock */
  106. __HAL_RCC_SYSCFG_CLK_ENABLE();
  107. hpcd.State = HAL_PCD_STATE_RESET;
  108. HAL_PCD_Init(&hpcd);
  109. /* hardcoded size of FIFO according definition*/
  110. HAL_PCDEx_PMAConfig(&hpcd , 0x00 , PCD_SNG_BUF, 0x30);
  111. HAL_PCDEx_PMAConfig(&hpcd , 0x80 , PCD_SNG_BUF, 0x70);
  112. #if 1
  113. HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_DBL_BUF, 0x018000b0);
  114. #else
  115. HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_SNG_BUF, 0x180);
  116. #endif
  117. HAL_PCDEx_PMAConfig(&hpcd , 0x83, PCD_SNG_BUF, 0xb0);
  118. NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr);
  119. NVIC_SetPriority(USBHAL_IRQn, 1);
  120. HAL_PCD_Start(&hpcd);
  121. }
  122. #endif