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.
 
 
 

1004 lines
28 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_ll_usb.c
  4. * @author MCD Application Team
  5. * @brief USB Low Layer HAL module driver.
  6. *
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the USB Peripheral Controller:
  9. * + Initialization/de-initialization functions
  10. * + I/O operation functions
  11. * + Peripheral Control functions
  12. * + Peripheral State functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. (#) Fill parameters of Init structure in USB_OTG_CfgTypeDef structure.
  20. (#) Call USB_CoreInit() API to initialize the USB Core peripheral.
  21. (#) The upper HAL HCD/PCD driver will call the right routines for its internal processes.
  22. @endverbatim
  23. ******************************************************************************
  24. * @attention
  25. *
  26. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  27. * All rights reserved.</center></h2>
  28. *
  29. * This software component is licensed by ST under BSD 3-Clause license,
  30. * the "License"; You may not use this file except in compliance with the
  31. * License. You may obtain a copy of the License at:
  32. * opensource.org/licenses/BSD-3-Clause
  33. *
  34. ******************************************************************************
  35. */
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32l1xx_hal.h"
  38. /** @addtogroup STM32L1xx_LL_USB_DRIVER
  39. * @{
  40. */
  41. #if defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED)
  42. #if defined (USB)
  43. /* Private typedef -----------------------------------------------------------*/
  44. /* Private define ------------------------------------------------------------*/
  45. /* Private macro -------------------------------------------------------------*/
  46. /* Private variables ---------------------------------------------------------*/
  47. /* Private function prototypes -----------------------------------------------*/
  48. /* Private functions ---------------------------------------------------------*/
  49. /**
  50. * @brief Initializes the USB Core
  51. * @param USBx: USB Instance
  52. * @param cfg : pointer to a USB_CfgTypeDef structure that contains
  53. * the configuration information for the specified USBx peripheral.
  54. * @retval HAL status
  55. */
  56. HAL_StatusTypeDef USB_CoreInit(USB_TypeDef *USBx, USB_CfgTypeDef cfg)
  57. {
  58. /* Prevent unused argument(s) compilation warning */
  59. UNUSED(USBx);
  60. UNUSED(cfg);
  61. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  62. only by USB OTG FS peripheral.
  63. - This function is added to ensure compatibility across platforms.
  64. */
  65. return HAL_OK;
  66. }
  67. /**
  68. * @brief USB_EnableGlobalInt
  69. * Enables the controller's Global Int in the AHB Config reg
  70. * @param USBx : Selected device
  71. * @retval HAL status
  72. */
  73. HAL_StatusTypeDef USB_EnableGlobalInt(USB_TypeDef *USBx)
  74. {
  75. uint32_t winterruptmask;
  76. /* Clear pending interrupts */
  77. USBx->ISTR = 0U;
  78. /* Set winterruptmask variable */
  79. winterruptmask = USB_CNTR_CTRM | USB_CNTR_WKUPM |
  80. USB_CNTR_SUSPM | USB_CNTR_ERRM |
  81. USB_CNTR_SOFM | USB_CNTR_ESOFM |
  82. USB_CNTR_RESETM;
  83. /* Set interrupt mask */
  84. USBx->CNTR = (uint16_t)winterruptmask;
  85. return HAL_OK;
  86. }
  87. /**
  88. * @brief USB_DisableGlobalInt
  89. * Disable the controller's Global Int in the AHB Config reg
  90. * @param USBx : Selected device
  91. * @retval HAL status
  92. */
  93. HAL_StatusTypeDef USB_DisableGlobalInt(USB_TypeDef *USBx)
  94. {
  95. uint32_t winterruptmask;
  96. /* Set winterruptmask variable */
  97. winterruptmask = USB_CNTR_CTRM | USB_CNTR_WKUPM |
  98. USB_CNTR_SUSPM | USB_CNTR_ERRM |
  99. USB_CNTR_SOFM | USB_CNTR_ESOFM |
  100. USB_CNTR_RESETM;
  101. /* Clear interrupt mask */
  102. USBx->CNTR &= (uint16_t)(~winterruptmask);
  103. return HAL_OK;
  104. }
  105. /**
  106. * @brief USB_SetCurrentMode : Set functional mode
  107. * @param USBx : Selected device
  108. * @param mode : current core mode
  109. * This parameter can be one of the these values:
  110. * @arg USB_DEVICE_MODE: Peripheral mode
  111. * @retval HAL status
  112. */
  113. HAL_StatusTypeDef USB_SetCurrentMode(USB_TypeDef *USBx, USB_ModeTypeDef mode)
  114. {
  115. /* Prevent unused argument(s) compilation warning */
  116. UNUSED(USBx);
  117. UNUSED(mode);
  118. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  119. only by USB OTG FS peripheral.
  120. - This function is added to ensure compatibility across platforms.
  121. */
  122. return HAL_OK;
  123. }
  124. /**
  125. * @brief USB_DevInit : Initializes the USB controller registers
  126. * for device mode
  127. * @param USBx : Selected device
  128. * @param cfg : pointer to a USB_CfgTypeDef structure that contains
  129. * the configuration information for the specified USBx peripheral.
  130. * @retval HAL status
  131. */
  132. HAL_StatusTypeDef USB_DevInit(USB_TypeDef *USBx, USB_CfgTypeDef cfg)
  133. {
  134. /* Prevent unused argument(s) compilation warning */
  135. UNUSED(cfg);
  136. /* Init Device */
  137. /*CNTR_FRES = 1*/
  138. USBx->CNTR = (uint16_t)USB_CNTR_FRES;
  139. /*CNTR_FRES = 0*/
  140. USBx->CNTR = 0U;
  141. /*Clear pending interrupts*/
  142. USBx->ISTR = 0U;
  143. /*Set Btable Address*/
  144. USBx->BTABLE = BTABLE_ADDRESS;
  145. return HAL_OK;
  146. }
  147. /**
  148. * @brief USB_SetDevSpeed :Initializes the device speed
  149. * depending on the PHY type and the enumeration speed of the device.
  150. * @param USBx Selected device
  151. * @param speed device speed
  152. * @retval Hal status
  153. */
  154. HAL_StatusTypeDef USB_SetDevSpeed(USB_TypeDef *USBx, uint8_t speed)
  155. {
  156. /* Prevent unused argument(s) compilation warning */
  157. UNUSED(USBx);
  158. UNUSED(speed);
  159. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  160. only by USB OTG FS peripheral.
  161. - This function is added to ensure compatibility across platforms.
  162. */
  163. return HAL_OK;
  164. }
  165. /**
  166. * @brief USB_FlushTxFifo : Flush a Tx FIFO
  167. * @param USBx : Selected device
  168. * @param num : FIFO number
  169. * This parameter can be a value from 1 to 15
  170. 15 means Flush all Tx FIFOs
  171. * @retval HAL status
  172. */
  173. HAL_StatusTypeDef USB_FlushTxFifo(USB_TypeDef *USBx, uint32_t num)
  174. {
  175. /* Prevent unused argument(s) compilation warning */
  176. UNUSED(USBx);
  177. UNUSED(num);
  178. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  179. only by USB OTG FS peripheral.
  180. - This function is added to ensure compatibility across platforms.
  181. */
  182. return HAL_OK;
  183. }
  184. /**
  185. * @brief USB_FlushRxFifo : Flush Rx FIFO
  186. * @param USBx : Selected device
  187. * @retval HAL status
  188. */
  189. HAL_StatusTypeDef USB_FlushRxFifo(USB_TypeDef *USBx)
  190. {
  191. /* Prevent unused argument(s) compilation warning */
  192. UNUSED(USBx);
  193. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  194. only by USB OTG FS peripheral.
  195. - This function is added to ensure compatibility across platforms.
  196. */
  197. return HAL_OK;
  198. }
  199. /**
  200. * @brief Activate and configure an endpoint
  201. * @param USBx : Selected device
  202. * @param ep: pointer to endpoint structure
  203. * @retval HAL status
  204. */
  205. HAL_StatusTypeDef USB_ActivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep)
  206. {
  207. HAL_StatusTypeDef ret = HAL_OK;
  208. uint16_t wEpRegVal;
  209. wEpRegVal = PCD_GET_ENDPOINT(USBx, ep->num) & USB_EP_T_MASK;
  210. /* initialize Endpoint */
  211. switch (ep->type)
  212. {
  213. case EP_TYPE_CTRL:
  214. wEpRegVal |= USB_EP_CONTROL;
  215. break;
  216. case EP_TYPE_BULK:
  217. wEpRegVal |= USB_EP_BULK;
  218. break;
  219. case EP_TYPE_INTR:
  220. wEpRegVal |= USB_EP_INTERRUPT;
  221. break;
  222. case EP_TYPE_ISOC:
  223. wEpRegVal |= USB_EP_ISOCHRONOUS;
  224. break;
  225. default:
  226. ret = HAL_ERROR;
  227. break;
  228. }
  229. PCD_SET_ENDPOINT(USBx, ep->num, (wEpRegVal | USB_EP_CTR_RX | USB_EP_CTR_TX));
  230. PCD_SET_EP_ADDRESS(USBx, ep->num, ep->num);
  231. if (ep->doublebuffer == 0U)
  232. {
  233. if (ep->is_in != 0U)
  234. {
  235. /*Set the endpoint Transmit buffer address */
  236. PCD_SET_EP_TX_ADDRESS(USBx, ep->num, ep->pmaadress);
  237. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  238. if (ep->type != EP_TYPE_ISOC)
  239. {
  240. /* Configure NAK status for the Endpoint */
  241. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
  242. }
  243. else
  244. {
  245. /* Configure TX Endpoint to disabled state */
  246. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  247. }
  248. }
  249. else
  250. {
  251. /*Set the endpoint Receive buffer address */
  252. PCD_SET_EP_RX_ADDRESS(USBx, ep->num, ep->pmaadress);
  253. /*Set the endpoint Receive buffer counter*/
  254. PCD_SET_EP_RX_CNT(USBx, ep->num, ep->maxpacket);
  255. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  256. /* Configure VALID status for the Endpoint*/
  257. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
  258. }
  259. }
  260. /*Double Buffer*/
  261. else
  262. {
  263. /* Set the endpoint as double buffered */
  264. PCD_SET_EP_DBUF(USBx, ep->num);
  265. /* Set buffer address for double buffered mode */
  266. PCD_SET_EP_DBUF_ADDR(USBx, ep->num, ep->pmaaddr0, ep->pmaaddr1);
  267. if (ep->is_in == 0U)
  268. {
  269. /* Clear the data toggle bits for the endpoint IN/OUT */
  270. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  271. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  272. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
  273. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  274. }
  275. else
  276. {
  277. /* Clear the data toggle bits for the endpoint IN/OUT */
  278. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  279. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  280. if (ep->type != EP_TYPE_ISOC)
  281. {
  282. /* Configure NAK status for the Endpoint */
  283. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
  284. }
  285. else
  286. {
  287. /* Configure TX Endpoint to disabled state */
  288. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  289. }
  290. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
  291. }
  292. }
  293. return ret;
  294. }
  295. /**
  296. * @brief De-activate and de-initialize an endpoint
  297. * @param USBx : Selected device
  298. * @param ep: pointer to endpoint structure
  299. * @retval HAL status
  300. */
  301. HAL_StatusTypeDef USB_DeactivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep)
  302. {
  303. if (ep->doublebuffer == 0U)
  304. {
  305. if (ep->is_in != 0U)
  306. {
  307. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  308. /* Configure DISABLE status for the Endpoint*/
  309. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  310. }
  311. else
  312. {
  313. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  314. /* Configure DISABLE status for the Endpoint*/
  315. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
  316. }
  317. }
  318. /*Double Buffer*/
  319. else
  320. {
  321. if (ep->is_in == 0U)
  322. {
  323. /* Clear the data toggle bits for the endpoint IN/OUT*/
  324. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  325. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  326. /* Reset value of the data toggle bits for the endpoint out*/
  327. PCD_TX_DTOG(USBx, ep->num);
  328. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
  329. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  330. }
  331. else
  332. {
  333. /* Clear the data toggle bits for the endpoint IN/OUT*/
  334. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  335. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  336. PCD_RX_DTOG(USBx, ep->num);
  337. /* Configure DISABLE status for the Endpoint*/
  338. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  339. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
  340. }
  341. }
  342. return HAL_OK;
  343. }
  344. /**
  345. * @brief USB_EPStartXfer : setup and starts a transfer over an EP
  346. * @param USBx : Selected device
  347. * @param ep: pointer to endpoint structure
  348. * @retval HAL status
  349. */
  350. HAL_StatusTypeDef USB_EPStartXfer(USB_TypeDef *USBx, USB_EPTypeDef *ep)
  351. {
  352. uint16_t pmabuffer;
  353. uint32_t len;
  354. uint16_t wEPVal;
  355. /* IN endpoint */
  356. if (ep->is_in == 1U)
  357. {
  358. /*Multi packet transfer*/
  359. if (ep->xfer_len > ep->maxpacket)
  360. {
  361. len = ep->maxpacket;
  362. }
  363. else
  364. {
  365. len = ep->xfer_len;
  366. }
  367. /* configure and validate Tx endpoint */
  368. if (ep->doublebuffer == 0U)
  369. {
  370. USB_WritePMA(USBx, ep->xfer_buff, ep->pmaadress, (uint16_t)len);
  371. PCD_SET_EP_TX_CNT(USBx, ep->num, len);
  372. }
  373. else
  374. {
  375. /*double buffer bulk management */
  376. if (ep->type == EP_TYPE_BULK)
  377. {
  378. if (ep->xfer_len_db > ep->maxpacket)
  379. {
  380. /*enable double buffer */
  381. PCD_SET_EP_DBUF(USBx, ep->num);
  382. len = ep->maxpacket;
  383. /*each Time to write in PMA xfer_len_db will */
  384. ep->xfer_len_db -= len;
  385. /* Fill the two first buffer in the Buffer0 & Buffer1*/
  386. if ((PCD_GET_ENDPOINT(USBx, ep->num) & USB_EP_DTOG_TX) != 0U)
  387. {
  388. /* Set the Double buffer counter for pmabuffer1 */
  389. PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
  390. pmabuffer = ep->pmaaddr1;
  391. /*Write the user buffer to USB PMA */
  392. USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
  393. ep->xfer_buff += len;
  394. if (ep->xfer_len_db > ep->maxpacket)
  395. {
  396. len = ep->maxpacket;
  397. ep->xfer_len_db -= len;
  398. }
  399. else
  400. {
  401. len = ep->xfer_len_db;
  402. ep->xfer_len_db = 0;
  403. }
  404. /* Set the Double buffer counter for pmabuffer0 */
  405. PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len);
  406. pmabuffer = ep->pmaaddr0;
  407. /*Write the user buffer to USB PMA */
  408. USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
  409. }
  410. else
  411. {
  412. /* Set the Double buffer counter for pmabuffer0 */
  413. PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len);
  414. pmabuffer = ep->pmaaddr0;
  415. /*Write the user buffer to USB PMA */
  416. USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
  417. ep->xfer_buff += len;
  418. if (ep->xfer_len_db > ep->maxpacket)
  419. {
  420. len = ep->maxpacket;
  421. ep->xfer_len_db -= len;
  422. }
  423. else
  424. {
  425. len = ep->xfer_len_db;
  426. ep->xfer_len_db = 0;
  427. }
  428. /* Set the Double buffer counter for pmabuffer1 */
  429. PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
  430. pmabuffer = ep->pmaaddr1;
  431. /*Write the user buffer to USB PMA */
  432. USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
  433. }
  434. }
  435. /*auto Switch to single buffer mode when transfer <Mps no need to manage in double buffer*/
  436. else
  437. {
  438. len = ep->xfer_len_db;
  439. /*disable double buffer mode */
  440. PCD_CLEAR_EP_DBUF(USBx, ep->num);
  441. /*Set Tx count with nbre of byte to be transmitted */
  442. PCD_SET_EP_TX_CNT(USBx, ep->num, len);
  443. pmabuffer = ep->pmaaddr0;
  444. /*Write the user buffer to USB PMA */
  445. USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
  446. }
  447. }/*end if bulk double buffer */
  448. /*mange isochronous double buffer IN mode */
  449. else
  450. {
  451. /* Write the data to the USB endpoint */
  452. if ((PCD_GET_ENDPOINT(USBx, ep->num) & USB_EP_DTOG_TX) != 0U)
  453. {
  454. /* Set the Double buffer counter for pmabuffer1 */
  455. PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
  456. pmabuffer = ep->pmaaddr1;
  457. }
  458. else
  459. {
  460. /* Set the Double buffer counter for pmabuffer0 */
  461. PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len);
  462. pmabuffer = ep->pmaaddr0;
  463. }
  464. USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
  465. PCD_FreeUserBuffer(USBx, ep->num, ep->is_in);
  466. }
  467. }
  468. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_VALID);
  469. }
  470. else /* OUT endpoint */
  471. {
  472. if (ep->doublebuffer == 0U)
  473. {
  474. /* Multi packet transfer*/
  475. if (ep->xfer_len > ep->maxpacket)
  476. {
  477. len = ep->maxpacket;
  478. ep->xfer_len -= len;
  479. }
  480. else
  481. {
  482. len = ep->xfer_len;
  483. ep->xfer_len = 0U;
  484. }
  485. /* configure and validate Rx endpoint */
  486. /*Set RX buffer count*/
  487. PCD_SET_EP_RX_CNT(USBx, ep->num, len);
  488. }
  489. else
  490. {
  491. /*First Transfer Coming From HAL_PCD_EP_Receive & From ISR*/
  492. /*Set the Double buffer counter*/
  493. if (ep->type == EP_TYPE_BULK)
  494. {
  495. PCD_SET_EP_DBUF_CNT(USBx, ep->num, ep->is_in, ep->maxpacket);
  496. /*Coming from ISR*/
  497. if (ep->xfer_count != 0U)
  498. {
  499. /* update last value to check if there is blocking state*/
  500. wEPVal = PCD_GET_ENDPOINT(USBx, ep->num);
  501. /*Blocking State */
  502. if ((((wEPVal & USB_EP_DTOG_RX) != 0U) && ((wEPVal & USB_EP_DTOG_TX) != 0U)) ||
  503. (((wEPVal & USB_EP_DTOG_RX) == 0U) && ((wEPVal & USB_EP_DTOG_TX) == 0U)))
  504. {
  505. PCD_FreeUserBuffer(USBx, ep->num, 0U);
  506. }
  507. }
  508. }
  509. /*iso out double */
  510. else if (ep->type == EP_TYPE_ISOC)
  511. {
  512. /* Multi packet transfer*/
  513. if (ep->xfer_len > ep->maxpacket)
  514. {
  515. len = ep->maxpacket;
  516. ep->xfer_len -= len;
  517. }
  518. else
  519. {
  520. len = ep->xfer_len;
  521. ep->xfer_len = 0U;
  522. }
  523. PCD_SET_EP_DBUF_CNT(USBx, ep->num, ep->is_in, len);
  524. }
  525. else
  526. {
  527. return HAL_ERROR;
  528. }
  529. }
  530. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
  531. }
  532. return HAL_OK;
  533. }
  534. /**
  535. * @brief USB_WritePacket : Writes a packet into the Tx FIFO associated
  536. * with the EP/channel
  537. * @param USBx : Selected device
  538. * @param src : pointer to source buffer
  539. * @param ch_ep_num : endpoint or host channel number
  540. * @param len : Number of bytes to write
  541. * @retval HAL status
  542. */
  543. HAL_StatusTypeDef USB_WritePacket(USB_TypeDef *USBx, uint8_t *src, uint8_t ch_ep_num, uint16_t len)
  544. {
  545. /* Prevent unused argument(s) compilation warning */
  546. UNUSED(USBx);
  547. UNUSED(src);
  548. UNUSED(ch_ep_num);
  549. UNUSED(len);
  550. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  551. only by USB OTG FS peripheral.
  552. - This function is added to ensure compatibility across platforms.
  553. */
  554. return HAL_OK;
  555. }
  556. /**
  557. * @brief USB_ReadPacket : read a packet from the Tx FIFO associated
  558. * with the EP/channel
  559. * @param USBx : Selected device
  560. * @param dest : destination pointer
  561. * @param len : Number of bytes to read
  562. * @retval pointer to destination buffer
  563. */
  564. void *USB_ReadPacket(USB_TypeDef *USBx, uint8_t *dest, uint16_t len)
  565. {
  566. /* Prevent unused argument(s) compilation warning */
  567. UNUSED(USBx);
  568. UNUSED(dest);
  569. UNUSED(len);
  570. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  571. only by USB OTG FS peripheral.
  572. - This function is added to ensure compatibility across platforms.
  573. */
  574. return ((void *)NULL);
  575. }
  576. /**
  577. * @brief USB_EPSetStall : set a stall condition over an EP
  578. * @param USBx : Selected device
  579. * @param ep: pointer to endpoint structure
  580. * @retval HAL status
  581. */
  582. HAL_StatusTypeDef USB_EPSetStall(USB_TypeDef *USBx, USB_EPTypeDef *ep)
  583. {
  584. if (ep->is_in != 0U)
  585. {
  586. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_STALL);
  587. }
  588. else
  589. {
  590. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_STALL);
  591. }
  592. return HAL_OK;
  593. }
  594. /**
  595. * @brief USB_EPClearStall : Clear a stall condition over an EP
  596. * @param USBx : Selected device
  597. * @param ep: pointer to endpoint structure
  598. * @retval HAL status
  599. */
  600. HAL_StatusTypeDef USB_EPClearStall(USB_TypeDef *USBx, USB_EPTypeDef *ep)
  601. {
  602. if (ep->doublebuffer == 0U)
  603. {
  604. if (ep->is_in != 0U)
  605. {
  606. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  607. if (ep->type != EP_TYPE_ISOC)
  608. {
  609. /* Configure NAK status for the Endpoint */
  610. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
  611. }
  612. }
  613. else
  614. {
  615. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  616. /* Configure VALID status for the Endpoint*/
  617. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
  618. }
  619. }
  620. return HAL_OK;
  621. }
  622. /**
  623. * @brief USB_StopDevice : Stop the usb device mode
  624. * @param USBx : Selected device
  625. * @retval HAL status
  626. */
  627. HAL_StatusTypeDef USB_StopDevice(USB_TypeDef *USBx)
  628. {
  629. /* disable all interrupts and force USB reset */
  630. USBx->CNTR = (uint16_t)USB_CNTR_FRES;
  631. /* clear interrupt status register */
  632. USBx->ISTR = 0U;
  633. /* switch-off device */
  634. USBx->CNTR = (uint16_t)(USB_CNTR_FRES | USB_CNTR_PDWN);
  635. return HAL_OK;
  636. }
  637. /**
  638. * @brief USB_SetDevAddress : Stop the usb device mode
  639. * @param USBx : Selected device
  640. * @param address : new device address to be assigned
  641. * This parameter can be a value from 0 to 255
  642. * @retval HAL status
  643. */
  644. HAL_StatusTypeDef USB_SetDevAddress(USB_TypeDef *USBx, uint8_t address)
  645. {
  646. if (address == 0U)
  647. {
  648. /* set device address and enable function */
  649. USBx->DADDR = (uint16_t)USB_DADDR_EF;
  650. }
  651. return HAL_OK;
  652. }
  653. /**
  654. * @brief USB_DevConnect : Connect the USB device by enabling the pull-up/pull-down
  655. * @param USBx : Selected device
  656. * @retval HAL status
  657. */
  658. HAL_StatusTypeDef USB_DevConnect(USB_TypeDef *USBx)
  659. {
  660. /* Prevent unused argument(s) compilation warning */
  661. UNUSED(USBx);
  662. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  663. only by USB OTG FS peripheral.
  664. - This function is added to ensure compatibility across platforms.
  665. */
  666. return HAL_OK;
  667. }
  668. /**
  669. * @brief USB_DevDisconnect : Disconnect the USB device by disabling the pull-up/pull-down
  670. * @param USBx : Selected device
  671. * @retval HAL status
  672. */
  673. HAL_StatusTypeDef USB_DevDisconnect(USB_TypeDef *USBx)
  674. {
  675. /* Prevent unused argument(s) compilation warning */
  676. UNUSED(USBx);
  677. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  678. only by USB OTG FS peripheral.
  679. - This function is added to ensure compatibility across platforms.
  680. */
  681. return HAL_OK;
  682. }
  683. /**
  684. * @brief USB_ReadInterrupts: return the global USB interrupt status
  685. * @param USBx : Selected device
  686. * @retval HAL status
  687. */
  688. uint32_t USB_ReadInterrupts(USB_TypeDef *USBx)
  689. {
  690. uint32_t tmpreg;
  691. tmpreg = USBx->ISTR;
  692. return tmpreg;
  693. }
  694. /**
  695. * @brief USB_ReadDevAllOutEpInterrupt: return the USB device OUT endpoints interrupt status
  696. * @param USBx : Selected device
  697. * @retval HAL status
  698. */
  699. uint32_t USB_ReadDevAllOutEpInterrupt(USB_TypeDef *USBx)
  700. {
  701. /* Prevent unused argument(s) compilation warning */
  702. UNUSED(USBx);
  703. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  704. only by USB OTG FS peripheral.
  705. - This function is added to ensure compatibility across platforms.
  706. */
  707. return (0);
  708. }
  709. /**
  710. * @brief USB_ReadDevAllInEpInterrupt: return the USB device IN endpoints interrupt status
  711. * @param USBx : Selected device
  712. * @retval HAL status
  713. */
  714. uint32_t USB_ReadDevAllInEpInterrupt(USB_TypeDef *USBx)
  715. {
  716. /* Prevent unused argument(s) compilation warning */
  717. UNUSED(USBx);
  718. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  719. only by USB OTG FS peripheral.
  720. - This function is added to ensure compatibility across platforms.
  721. */
  722. return (0);
  723. }
  724. /**
  725. * @brief Returns Device OUT EP Interrupt register
  726. * @param USBx : Selected device
  727. * @param epnum : endpoint number
  728. * This parameter can be a value from 0 to 15
  729. * @retval Device OUT EP Interrupt register
  730. */
  731. uint32_t USB_ReadDevOutEPInterrupt(USB_TypeDef *USBx, uint8_t epnum)
  732. {
  733. /* Prevent unused argument(s) compilation warning */
  734. UNUSED(USBx);
  735. UNUSED(epnum);
  736. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  737. only by USB OTG FS peripheral.
  738. - This function is added to ensure compatibility across platforms.
  739. */
  740. return (0);
  741. }
  742. /**
  743. * @brief Returns Device IN EP Interrupt register
  744. * @param USBx : Selected device
  745. * @param epnum : endpoint number
  746. * This parameter can be a value from 0 to 15
  747. * @retval Device IN EP Interrupt register
  748. */
  749. uint32_t USB_ReadDevInEPInterrupt(USB_TypeDef *USBx, uint8_t epnum)
  750. {
  751. /* Prevent unused argument(s) compilation warning */
  752. UNUSED(USBx);
  753. UNUSED(epnum);
  754. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  755. only by USB OTG FS peripheral.
  756. - This function is added to ensure compatibility across platforms.
  757. */
  758. return (0);
  759. }
  760. /**
  761. * @brief USB_ClearInterrupts: clear a USB interrupt
  762. * @param USBx Selected device
  763. * @param interrupt flag
  764. * @retval None
  765. */
  766. void USB_ClearInterrupts(USB_TypeDef *USBx, uint32_t interrupt)
  767. {
  768. /* Prevent unused argument(s) compilation warning */
  769. UNUSED(USBx);
  770. UNUSED(interrupt);
  771. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  772. only by USB OTG FS peripheral.
  773. - This function is added to ensure compatibility across platforms.
  774. */
  775. }
  776. /**
  777. * @brief Prepare the EP0 to start the first control setup
  778. * @param USBx Selected device
  779. * @param psetup pointer to setup packet
  780. * @retval HAL status
  781. */
  782. HAL_StatusTypeDef USB_EP0_OutStart(USB_TypeDef *USBx, uint8_t *psetup)
  783. {
  784. /* Prevent unused argument(s) compilation warning */
  785. UNUSED(USBx);
  786. UNUSED(psetup);
  787. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  788. only by USB OTG FS peripheral.
  789. - This function is added to ensure compatibility across platforms.
  790. */
  791. return HAL_OK;
  792. }
  793. /**
  794. * @brief USB_ActivateRemoteWakeup : active remote wakeup signalling
  795. * @param USBx Selected device
  796. * @retval HAL status
  797. */
  798. HAL_StatusTypeDef USB_ActivateRemoteWakeup(USB_TypeDef *USBx)
  799. {
  800. USBx->CNTR |= (uint16_t)USB_CNTR_RESUME;
  801. return HAL_OK;
  802. }
  803. /**
  804. * @brief USB_DeActivateRemoteWakeup : de-active remote wakeup signalling
  805. * @param USBx Selected device
  806. * @retval HAL status
  807. */
  808. HAL_StatusTypeDef USB_DeActivateRemoteWakeup(USB_TypeDef *USBx)
  809. {
  810. USBx->CNTR &= (uint16_t)(~USB_CNTR_RESUME);
  811. return HAL_OK;
  812. }
  813. /**
  814. * @brief Copy a buffer from user memory area to packet memory area (PMA)
  815. * @param USBx USB peripheral instance register address.
  816. * @param pbUsrBuf pointer to user memory area.
  817. * @param wPMABufAddr address into PMA.
  818. * @param wNBytes: no. of bytes to be copied.
  819. * @retval None
  820. */
  821. void USB_WritePMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
  822. {
  823. uint32_t n = ((uint32_t)wNBytes + 1U) >> 1;
  824. uint32_t BaseAddr = (uint32_t)USBx;
  825. uint32_t i, temp1, temp2;
  826. __IO uint16_t *pdwVal;
  827. uint8_t *pBuf = pbUsrBuf;
  828. pdwVal = (__IO uint16_t *)(BaseAddr + 0x400U + ((uint32_t)wPMABufAddr * PMA_ACCESS));
  829. for (i = n; i != 0U; i--)
  830. {
  831. temp1 = *pBuf;
  832. pBuf++;
  833. temp2 = temp1 | ((uint16_t)((uint16_t) *pBuf << 8));
  834. *pdwVal = (uint16_t)temp2;
  835. pdwVal++;
  836. #if PMA_ACCESS > 1U
  837. pdwVal++;
  838. #endif
  839. pBuf++;
  840. }
  841. }
  842. /**
  843. * @brief Copy data from packet memory area (PMA) to user memory buffer
  844. * @param USBx: USB peripheral instance register address.
  845. * @param pbUsrBuf pointer to user memory area.
  846. * @param wPMABufAddr address into PMA.
  847. * @param wNBytes: no. of bytes to be copied.
  848. * @retval None
  849. */
  850. void USB_ReadPMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
  851. {
  852. uint32_t n = (uint32_t)wNBytes >> 1;
  853. uint32_t BaseAddr = (uint32_t)USBx;
  854. uint32_t i, temp;
  855. __IO uint16_t *pdwVal;
  856. uint8_t *pBuf = pbUsrBuf;
  857. pdwVal = (__IO uint16_t *)(BaseAddr + 0x400U + ((uint32_t)wPMABufAddr * PMA_ACCESS));
  858. for (i = n; i != 0U; i--)
  859. {
  860. temp = *(__IO uint16_t *)pdwVal;
  861. pdwVal++;
  862. *pBuf = (uint8_t)((temp >> 0) & 0xFFU);
  863. pBuf++;
  864. *pBuf = (uint8_t)((temp >> 8) & 0xFFU);
  865. pBuf++;
  866. #if PMA_ACCESS > 1U
  867. pdwVal++;
  868. #endif
  869. }
  870. if ((wNBytes % 2U) != 0U)
  871. {
  872. temp = *pdwVal;
  873. *pBuf = (uint8_t)((temp >> 0) & 0xFFU);
  874. }
  875. }
  876. /**
  877. * @}
  878. */
  879. /**
  880. * @}
  881. */
  882. #endif /* defined (USB) */
  883. #endif /* defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED) */
  884. /**
  885. * @}
  886. */
  887. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/