usbd_conf.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. */
  4. /**
  5. ******************************************************************************
  6. * @file USB_Device/CDC_Standalone/Src/usbd_conf.c
  7. * @author MCD Application Team
  8. * @version V1.0.1
  9. * @date 26-February-2014
  10. * @brief This file implements the USB Device library callbacks and MSP
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  15. *
  16. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  17. * You may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at:
  19. *
  20. * http://www.st.com/software_license_agreement_liberty_v2
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. *
  28. ******************************************************************************
  29. */
  30. #include "usbd_core.h"
  31. #include "py/obj.h"
  32. #include "py/mphal.h"
  33. #include "irq.h"
  34. #include "usb.h"
  35. #if MICROPY_HW_USB_FS || MICROPY_HW_USB_HS
  36. #if MICROPY_HW_USB_FS
  37. PCD_HandleTypeDef pcd_fs_handle;
  38. #endif
  39. #if MICROPY_HW_USB_HS
  40. PCD_HandleTypeDef pcd_hs_handle;
  41. #endif
  42. /*******************************************************************************
  43. PCD BSP Routines
  44. *******************************************************************************/
  45. /**
  46. * @brief Initializes the PCD MSP.
  47. * @param hpcd: PCD handle
  48. * @retval None
  49. */
  50. void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) {
  51. if (hpcd->Instance == USB_OTG_FS) {
  52. #if defined(STM32H7)
  53. const uint32_t otg_alt = GPIO_AF10_OTG1_FS;
  54. #else
  55. const uint32_t otg_alt = GPIO_AF10_OTG_FS;
  56. #endif
  57. mp_hal_pin_config(pin_A11, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, otg_alt);
  58. mp_hal_pin_config_speed(pin_A11, GPIO_SPEED_FREQ_VERY_HIGH);
  59. mp_hal_pin_config(pin_A12, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, otg_alt);
  60. mp_hal_pin_config_speed(pin_A12, GPIO_SPEED_FREQ_VERY_HIGH);
  61. #if defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
  62. // USB VBUS detect pin is always A9
  63. mp_hal_pin_config(MICROPY_HW_USB_VBUS_DETECT_PIN, MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_NONE, 0);
  64. #endif
  65. #if defined(MICROPY_HW_USB_OTG_ID_PIN)
  66. // USB ID pin is always A10
  67. mp_hal_pin_config(MICROPY_HW_USB_OTG_ID_PIN, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_UP, otg_alt);
  68. #endif
  69. #if defined(STM32H7)
  70. // Keep USB clock running during sleep or else __WFI() will disable the USB
  71. __HAL_RCC_USB2_OTG_FS_CLK_SLEEP_ENABLE();
  72. __HAL_RCC_USB2_OTG_FS_ULPI_CLK_SLEEP_DISABLE();
  73. #endif
  74. // Enable USB FS Clocks
  75. __USB_OTG_FS_CLK_ENABLE();
  76. #if defined(STM32L4)
  77. // Enable VDDUSB
  78. if (__HAL_RCC_PWR_IS_CLK_DISABLED()) {
  79. __HAL_RCC_PWR_CLK_ENABLE();
  80. HAL_PWREx_EnableVddUSB();
  81. __HAL_RCC_PWR_CLK_DISABLE();
  82. } else {
  83. HAL_PWREx_EnableVddUSB();
  84. }
  85. #endif
  86. // Configure and enable USB FS interrupt
  87. NVIC_SetPriority(OTG_FS_IRQn, IRQ_PRI_OTG_FS);
  88. HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
  89. }
  90. #if MICROPY_HW_USB_HS
  91. else if (hpcd->Instance == USB_OTG_HS) {
  92. #if MICROPY_HW_USB_HS_IN_FS
  93. // Configure USB FS GPIOs
  94. mp_hal_pin_config(pin_B14, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, GPIO_AF12_OTG_HS_FS);
  95. mp_hal_pin_config_speed(pin_B14, GPIO_SPEED_FREQ_VERY_HIGH);
  96. mp_hal_pin_config(pin_B15, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, GPIO_AF12_OTG_HS_FS);
  97. mp_hal_pin_config_speed(pin_B15, GPIO_SPEED_FREQ_VERY_HIGH);
  98. #if defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
  99. // Configure VBUS Pin
  100. mp_hal_pin_config(MICROPY_HW_USB_VBUS_DETECT_PIN, MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_NONE, 0);
  101. #endif
  102. #if defined(MICROPY_HW_USB_OTG_ID_PIN)
  103. // Configure ID pin
  104. mp_hal_pin_config(MICROPY_HW_USB_OTG_ID_PIN, MP_HAL_PIN_MODE_ALT_OPEN_DRAIN, MP_HAL_PIN_PULL_UP, GPIO_AF12_OTG_HS_FS);
  105. #endif
  106. // Enable calling WFI and correct function of the embedded USB_FS_IN_HS phy
  107. __HAL_RCC_USB_OTG_HS_ULPI_CLK_SLEEP_DISABLE();
  108. __HAL_RCC_USB_OTG_HS_CLK_SLEEP_ENABLE();
  109. // Enable USB HS Clocks
  110. #if defined(STM32F723xx) || defined(STM32F733xx)
  111. // Needs to remain awake during sleep or else __WFI() will disable the USB
  112. __HAL_RCC_USB_OTG_HS_ULPI_CLK_SLEEP_ENABLE();
  113. __HAL_RCC_OTGPHYC_CLK_ENABLE();
  114. __HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE();
  115. #endif
  116. __HAL_RCC_USB_OTG_HS_CLK_ENABLE();
  117. #else // !MICROPY_HW_USB_HS_IN_FS
  118. GPIO_InitTypeDef GPIO_InitStruct;
  119. // Configure USB HS GPIOs
  120. __HAL_RCC_GPIOA_CLK_ENABLE();
  121. __HAL_RCC_GPIOB_CLK_ENABLE();
  122. __HAL_RCC_GPIOC_CLK_ENABLE();
  123. __HAL_RCC_GPIOH_CLK_ENABLE();
  124. __HAL_RCC_GPIOI_CLK_ENABLE();
  125. // CLK
  126. GPIO_InitStruct.Pin = GPIO_PIN_5;
  127. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  128. GPIO_InitStruct.Pull = GPIO_NOPULL;
  129. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  130. GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
  131. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  132. // D0
  133. GPIO_InitStruct.Pin = GPIO_PIN_3;
  134. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  135. GPIO_InitStruct.Pull = GPIO_NOPULL;
  136. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  137. GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
  138. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  139. // D1 D2 D3 D4 D5 D6 D7
  140. GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_5 |\
  141. GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
  142. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  143. GPIO_InitStruct.Pull = GPIO_NOPULL;
  144. GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
  145. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  146. // STP
  147. GPIO_InitStruct.Pin = GPIO_PIN_0;
  148. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  149. GPIO_InitStruct.Pull = GPIO_NOPULL;
  150. GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
  151. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  152. // NXT
  153. GPIO_InitStruct.Pin = GPIO_PIN_4;
  154. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  155. GPIO_InitStruct.Pull = GPIO_NOPULL;
  156. GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
  157. HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
  158. // DIR
  159. GPIO_InitStruct.Pin = GPIO_PIN_11;
  160. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  161. GPIO_InitStruct.Pull = GPIO_NOPULL;
  162. GPIO_InitStruct.Alternate = GPIO_AF10_OTG_HS;
  163. HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
  164. // Enable USB HS Clocks
  165. __USB_OTG_HS_CLK_ENABLE();
  166. __USB_OTG_HS_ULPI_CLK_ENABLE();
  167. #endif // !MICROPY_HW_USB_HS_IN_FS
  168. // Configure and enable USB HS interrupt
  169. NVIC_SetPriority(OTG_HS_IRQn, IRQ_PRI_OTG_HS);
  170. HAL_NVIC_EnableIRQ(OTG_HS_IRQn);
  171. }
  172. #endif // MICROPY_HW_USB_HS
  173. }
  174. /**
  175. * @brief DeInitializes the PCD MSP.
  176. * @param hpcd: PCD handle
  177. * @retval None
  178. */
  179. void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd) {
  180. if (hpcd->Instance == USB_OTG_FS) {
  181. /* Disable USB FS Clocks */
  182. __USB_OTG_FS_CLK_DISABLE();
  183. __SYSCFG_CLK_DISABLE();
  184. }
  185. #if MICROPY_HW_USB_HS
  186. else if (hpcd->Instance == USB_OTG_HS) {
  187. /* Disable USB FS Clocks */
  188. __USB_OTG_HS_CLK_DISABLE();
  189. __SYSCFG_CLK_DISABLE();
  190. }
  191. #endif
  192. }
  193. /*******************************************************************************
  194. LL Driver Callbacks (PCD -> USB Device Library)
  195. *******************************************************************************/
  196. /**
  197. * @brief Setup stage callback.
  198. * @param hpcd: PCD handle
  199. * @retval None
  200. */
  201. void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) {
  202. USBD_LL_SetupStage(hpcd->pData, (uint8_t *)hpcd->Setup);
  203. }
  204. /**
  205. * @brief Data Out stage callback.
  206. * @param hpcd: PCD handle
  207. * @param epnum: Endpoint Number
  208. * @retval None
  209. */
  210. void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) {
  211. USBD_LL_DataOutStage(hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
  212. }
  213. /**
  214. * @brief Data In stage callback.
  215. * @param hpcd: PCD handle
  216. * @param epnum: Endpoint Number
  217. * @retval None
  218. */
  219. void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) {
  220. USBD_LL_DataInStage(hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
  221. }
  222. /**
  223. * @brief SOF callback.
  224. * @param hpcd: PCD handle
  225. * @retval None
  226. */
  227. /*
  228. This is now handled by the USB CDC interface.
  229. void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  230. {
  231. USBD_LL_SOF(hpcd->pData);
  232. }
  233. */
  234. /**
  235. * @brief Reset callback.
  236. * @param hpcd: PCD handle
  237. * @retval None
  238. */
  239. void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd) {
  240. USBD_SpeedTypeDef speed = USBD_SPEED_FULL;
  241. // Set USB Current Speed
  242. switch (hpcd->Init.speed) {
  243. #if defined(PCD_SPEED_HIGH)
  244. case PCD_SPEED_HIGH:
  245. speed = USBD_SPEED_HIGH;
  246. break;
  247. #endif
  248. case PCD_SPEED_FULL:
  249. speed = USBD_SPEED_FULL;
  250. break;
  251. default:
  252. speed = USBD_SPEED_FULL;
  253. break;
  254. }
  255. USBD_LL_SetSpeed(hpcd->pData, speed);
  256. // Reset Device
  257. USBD_LL_Reset(hpcd->pData);
  258. }
  259. /**
  260. * @brief Suspend callback.
  261. * @param hpcd: PCD handle
  262. * @retval None
  263. */
  264. void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd) {
  265. USBD_LL_Suspend(hpcd->pData);
  266. }
  267. /**
  268. * @brief Resume callback.
  269. * @param hpcd: PCD handle
  270. * @retval None
  271. */
  272. void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd) {
  273. USBD_LL_Resume(hpcd->pData);
  274. }
  275. /**
  276. * @brief ISOC Out Incomplete callback.
  277. * @param hpcd: PCD handle
  278. * @param epnum: Endpoint Number
  279. * @retval None
  280. */
  281. void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) {
  282. USBD_LL_IsoOUTIncomplete(hpcd->pData, epnum);
  283. }
  284. /**
  285. * @brief ISOC In Incomplete callback.
  286. * @param hpcd: PCD handle
  287. * @param epnum: Endpoint Number
  288. * @retval None
  289. */
  290. void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) {
  291. USBD_LL_IsoINIncomplete(hpcd->pData, epnum);
  292. }
  293. /**
  294. * @brief Connect callback.
  295. * @param hpcd: PCD handle
  296. * @retval None
  297. */
  298. void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd) {
  299. USBD_LL_DevConnected(hpcd->pData);
  300. }
  301. /**
  302. * @brief Disconnect callback.
  303. * @param hpcd: PCD handle
  304. * @retval None
  305. */
  306. void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd) {
  307. USBD_LL_DevDisconnected(hpcd->pData);
  308. }
  309. /*******************************************************************************
  310. LL Driver Interface (USB Device Library --> PCD)
  311. *******************************************************************************/
  312. /**
  313. * @brief Initializes the Low Level portion of the Device driver.
  314. * @param pdev: Device handle
  315. * @retval USBD Status
  316. */
  317. USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev, int high_speed) {
  318. #if MICROPY_HW_USB_FS
  319. if (pdev->id == USB_PHY_FS_ID) {
  320. // Set LL Driver parameters
  321. pcd_fs_handle.Instance = USB_OTG_FS;
  322. #if MICROPY_HW_USB_ENABLE_CDC2
  323. pcd_fs_handle.Init.dev_endpoints = 6;
  324. #else
  325. pcd_fs_handle.Init.dev_endpoints = 4;
  326. #endif
  327. pcd_fs_handle.Init.use_dedicated_ep1 = 0;
  328. pcd_fs_handle.Init.ep0_mps = 0x40;
  329. pcd_fs_handle.Init.dma_enable = 0;
  330. pcd_fs_handle.Init.low_power_enable = 0;
  331. pcd_fs_handle.Init.phy_itface = PCD_PHY_EMBEDDED;
  332. pcd_fs_handle.Init.Sof_enable = 1;
  333. pcd_fs_handle.Init.speed = PCD_SPEED_FULL;
  334. #if defined(STM32L4)
  335. pcd_fs_handle.Init.lpm_enable = DISABLE;
  336. pcd_fs_handle.Init.battery_charging_enable = DISABLE;
  337. #endif
  338. #if !defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
  339. pcd_fs_handle.Init.vbus_sensing_enable = 0; // No VBUS Sensing on USB0
  340. #else
  341. pcd_fs_handle.Init.vbus_sensing_enable = 1;
  342. #endif
  343. // Link The driver to the stack
  344. pcd_fs_handle.pData = pdev;
  345. pdev->pData = &pcd_fs_handle;
  346. // Initialize LL Driver
  347. HAL_PCD_Init(&pcd_fs_handle);
  348. // We have 320 32-bit words in total to use here
  349. #if MICROPY_HW_USB_ENABLE_CDC2
  350. HAL_PCD_SetRxFiFo(&pcd_fs_handle, 128);
  351. HAL_PCD_SetTxFiFo(&pcd_fs_handle, 0, 32); // EP0
  352. HAL_PCD_SetTxFiFo(&pcd_fs_handle, 1, 64); // MSC / HID
  353. HAL_PCD_SetTxFiFo(&pcd_fs_handle, 2, 16); // CDC CMD
  354. HAL_PCD_SetTxFiFo(&pcd_fs_handle, 3, 32); // CDC DATA
  355. HAL_PCD_SetTxFiFo(&pcd_fs_handle, 4, 16); // CDC2 CMD
  356. HAL_PCD_SetTxFiFo(&pcd_fs_handle, 5, 32); // CDC2 DATA
  357. #else
  358. HAL_PCD_SetRxFiFo(&pcd_fs_handle, 128);
  359. HAL_PCD_SetTxFiFo(&pcd_fs_handle, 0, 32); // EP0
  360. HAL_PCD_SetTxFiFo(&pcd_fs_handle, 1, 64); // MSC / HID
  361. HAL_PCD_SetTxFiFo(&pcd_fs_handle, 2, 32); // CDC CMD
  362. HAL_PCD_SetTxFiFo(&pcd_fs_handle, 3, 64); // CDC DATA
  363. #endif
  364. }
  365. #endif
  366. #if MICROPY_HW_USB_HS
  367. if (pdev->id == USB_PHY_HS_ID) {
  368. #if MICROPY_HW_USB_HS_IN_FS
  369. // Set LL Driver parameters
  370. pcd_hs_handle.Instance = USB_OTG_HS;
  371. pcd_hs_handle.Init.dev_endpoints = 6;
  372. pcd_hs_handle.Init.use_dedicated_ep1 = 0;
  373. pcd_hs_handle.Init.ep0_mps = 0x40;
  374. pcd_hs_handle.Init.dma_enable = 0;
  375. pcd_hs_handle.Init.low_power_enable = 0;
  376. pcd_hs_handle.Init.lpm_enable = DISABLE;
  377. pcd_hs_handle.Init.battery_charging_enable = DISABLE;
  378. #if defined(STM32F723xx) || defined(STM32F733xx)
  379. pcd_hs_handle.Init.phy_itface = USB_OTG_HS_EMBEDDED_PHY;
  380. #else
  381. pcd_hs_handle.Init.phy_itface = PCD_PHY_EMBEDDED;
  382. #endif
  383. pcd_hs_handle.Init.Sof_enable = 1;
  384. if (high_speed) {
  385. pcd_hs_handle.Init.speed = PCD_SPEED_HIGH;
  386. } else {
  387. pcd_hs_handle.Init.speed = PCD_SPEED_HIGH_IN_FULL;
  388. }
  389. #if !defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
  390. pcd_hs_handle.Init.vbus_sensing_enable = 0; // No VBUS Sensing on USB0
  391. #else
  392. pcd_hs_handle.Init.vbus_sensing_enable = 1;
  393. #endif
  394. pcd_hs_handle.Init.use_external_vbus = 0;
  395. // Link The driver to the stack
  396. pcd_hs_handle.pData = pdev;
  397. pdev->pData = &pcd_hs_handle;
  398. // Initialize LL Driver
  399. HAL_PCD_Init(&pcd_hs_handle);
  400. // We have 1024 32-bit words in total to use here
  401. HAL_PCD_SetRxFiFo(&pcd_hs_handle, 512);
  402. HAL_PCD_SetTxFiFo(&pcd_hs_handle, 0, 32); // EP0
  403. HAL_PCD_SetTxFiFo(&pcd_hs_handle, 1, 256); // MSC / HID
  404. HAL_PCD_SetTxFiFo(&pcd_hs_handle, 2, 32); // CDC CMD
  405. HAL_PCD_SetTxFiFo(&pcd_hs_handle, 3, 64); // CDC DATA
  406. HAL_PCD_SetTxFiFo(&pcd_hs_handle, 4, 32); // CDC2 CMD
  407. HAL_PCD_SetTxFiFo(&pcd_hs_handle, 5, 64); // CDC2 DATA
  408. #else // !MICROPY_HW_USB_HS_IN_FS
  409. // Set LL Driver parameters
  410. pcd_hs_handle.Instance = USB_OTG_HS;
  411. pcd_hs_handle.Init.dev_endpoints = 6;
  412. pcd_hs_handle.Init.use_dedicated_ep1 = 0;
  413. pcd_hs_handle.Init.ep0_mps = 0x40;
  414. /* Be aware that enabling USB-DMA mode will result in data being sent only by
  415. multiple of 4 packet sizes. This is due to the fact that USB-DMA does
  416. not allow sending data from non word-aligned addresses.
  417. For this specific application, it is advised to not enable this option
  418. unless required. */
  419. pcd_hs_handle.Init.dma_enable = 0;
  420. pcd_hs_handle.Init.low_power_enable = 0;
  421. pcd_hs_handle.Init.phy_itface = PCD_PHY_ULPI;
  422. pcd_hs_handle.Init.Sof_enable = 1;
  423. pcd_hs_handle.Init.speed = PCD_SPEED_HIGH;
  424. pcd_hs_handle.Init.vbus_sensing_enable = 1;
  425. // Link The driver to the stack
  426. pcd_hs_handle.pData = pdev;
  427. pdev->pData = &pcd_hs_handle;
  428. // Initialize LL Driver
  429. HAL_PCD_Init(&pcd_hs_handle);
  430. HAL_PCD_SetRxFiFo(&pcd_hs_handle, 0x200);
  431. HAL_PCD_SetTxFiFo(&pcd_hs_handle, 0, 0x80);
  432. HAL_PCD_SetTxFiFo(&pcd_hs_handle, 1, 0x174);
  433. #endif // !MICROPY_HW_USB_HS_IN_FS
  434. }
  435. #endif // MICROPY_HW_USB_HS
  436. return USBD_OK;
  437. }
  438. /**
  439. * @brief De-Initializes the Low Level portion of the Device driver.
  440. * @param pdev: Device handle
  441. * @retval USBD Status
  442. */
  443. USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev) {
  444. HAL_PCD_DeInit(pdev->pData);
  445. return USBD_OK;
  446. }
  447. /**
  448. * @brief Starts the Low Level portion of the Device driver.
  449. * @param pdev: Device handle
  450. * @retval USBD Status
  451. */
  452. USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev) {
  453. HAL_PCD_Start(pdev->pData);
  454. return USBD_OK;
  455. }
  456. /**
  457. * @brief Stops the Low Level portion of the Device driver.
  458. * @param pdev: Device handle
  459. * @retval USBD Status
  460. */
  461. USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev) {
  462. HAL_PCD_Stop(pdev->pData);
  463. return USBD_OK;
  464. }
  465. /**
  466. * @brief Opens an endpoint of the Low Level Driver.
  467. * @param pdev: Device handle
  468. * @param ep_addr: Endpoint Number
  469. * @param ep_type: Endpoint Type
  470. * @param ep_mps: Endpoint Max Packet Size
  471. * @retval USBD Status
  472. */
  473. USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev,
  474. uint8_t ep_addr, uint8_t ep_type, uint16_t ep_mps) {
  475. HAL_PCD_EP_Open(pdev->pData, ep_addr, ep_mps, ep_type);
  476. return USBD_OK;
  477. }
  478. /**
  479. * @brief Closes an endpoint of the Low Level Driver.
  480. * @param pdev: Device handle
  481. * @param ep_addr: Endpoint Number
  482. * @retval USBD Status
  483. */
  484. USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) {
  485. HAL_PCD_EP_Close(pdev->pData, ep_addr);
  486. return USBD_OK;
  487. }
  488. /**
  489. * @brief Flushes an endpoint of the Low Level Driver.
  490. * @param pdev: Device handle
  491. * @param ep_addr: Endpoint Number
  492. * @retval USBD Status
  493. */
  494. USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) {
  495. HAL_PCD_EP_Flush(pdev->pData, ep_addr);
  496. return USBD_OK;
  497. }
  498. /**
  499. * @brief Sets a Stall condition on an endpoint of the Low Level Driver.
  500. * @param pdev: Device handle
  501. * @param ep_addr: Endpoint Number
  502. * @retval USBD Status
  503. */
  504. USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) {
  505. HAL_PCD_EP_SetStall(pdev->pData, ep_addr);
  506. return USBD_OK;
  507. }
  508. /**
  509. * @brief Clears a Stall condition on an endpoint of the Low Level Driver.
  510. * @param pdev: Device handle
  511. * @param ep_addr: Endpoint Number
  512. * @retval USBD Status
  513. */
  514. USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) {
  515. HAL_PCD_EP_ClrStall(pdev->pData, ep_addr);
  516. return USBD_OK;
  517. }
  518. /**
  519. * @brief Returns Stall condition.
  520. * @param pdev: Device handle
  521. * @param ep_addr: Endpoint Number
  522. * @retval Stall (1: yes, 0: No)
  523. */
  524. uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) {
  525. PCD_HandleTypeDef *hpcd = pdev->pData;
  526. if ((ep_addr & 0x80) == 0x80) {
  527. return hpcd->IN_ep[ep_addr & 0x7F].is_stall;
  528. } else {
  529. return hpcd->OUT_ep[ep_addr & 0x7F].is_stall;
  530. }
  531. }
  532. /**
  533. * @brief Assigns an USB address to the device
  534. * @param pdev: Device handle
  535. * @param dev_addr: USB address
  536. * @retval USBD Status
  537. */
  538. USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr) {
  539. HAL_PCD_SetAddress(pdev->pData, dev_addr);
  540. return USBD_OK;
  541. }
  542. /**
  543. * @brief Transmits data over an endpoint
  544. * @param pdev: Device handle
  545. * @param ep_addr: Endpoint Number
  546. * @param pbuf: Pointer to data to be sent
  547. * @param size: Data size
  548. * @retval USBD Status
  549. */
  550. USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev,
  551. uint8_t ep_addr, uint8_t *pbuf, uint16_t size) {
  552. HAL_PCD_EP_Transmit(pdev->pData, ep_addr, pbuf, size);
  553. return USBD_OK;
  554. }
  555. /**
  556. * @brief Prepares an endpoint for reception
  557. * @param pdev: Device handle
  558. * @param ep_addr: Endpoint Number
  559. * @param pbuf:pointer to data to be received
  560. * @param size: data size
  561. * @retval USBD Status
  562. */
  563. USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev,
  564. uint8_t ep_addr, uint8_t *pbuf, uint16_t size) {
  565. HAL_PCD_EP_Receive(pdev->pData, ep_addr, pbuf, size);
  566. return USBD_OK;
  567. }
  568. /**
  569. * @brief Returns the last transfered packet size.
  570. * @param pdev: Device handle
  571. * @param ep_addr: Endpoint Number
  572. * @retval Recived Data Size
  573. */
  574. uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr) {
  575. return HAL_PCD_EP_GetRxCount(pdev->pData, ep_addr);
  576. }
  577. /**
  578. * @brief Delay routine for the USB Device Library
  579. * @param Delay: Delay in ms
  580. * @retval None
  581. */
  582. void USBD_LL_Delay(uint32_t Delay) {
  583. HAL_Delay(Delay);
  584. }
  585. #endif // MICROPY_HW_USB_FS || MICROPY_HW_USB_HS
  586. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/