stm32f1xx_hal_pcd_ex.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_pcd_ex.c
  4. * @author MCD Application Team
  5. * @brief PCD Extended HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the USB Peripheral Controller:
  8. * + Extended features functions
  9. *
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * Copyright (c) 2016 STMicroelectronics.
  14. * All rights reserved.
  15. *
  16. * This software is licensed under terms that can be found in the LICENSE file
  17. * in the root directory of this software component.
  18. * If no LICENSE file comes with this software, it is provided AS-IS.
  19. *
  20. ******************************************************************************
  21. */
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "stm32f1xx_hal.h"
  24. /** @addtogroup STM32F1xx_HAL_Driver
  25. * @{
  26. */
  27. /** @defgroup PCDEx PCDEx
  28. * @brief PCD Extended HAL module driver
  29. * @{
  30. */
  31. #ifdef HAL_PCD_MODULE_ENABLED
  32. #if defined (USB) || defined (USB_OTG_FS)
  33. /* Private types -------------------------------------------------------------*/
  34. /* Private variables ---------------------------------------------------------*/
  35. /* Private constants ---------------------------------------------------------*/
  36. /* Private macros ------------------------------------------------------------*/
  37. /* Private functions ---------------------------------------------------------*/
  38. /* Exported functions --------------------------------------------------------*/
  39. /** @defgroup PCDEx_Exported_Functions PCDEx Exported Functions
  40. * @{
  41. */
  42. /** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions
  43. * @brief PCDEx control functions
  44. *
  45. @verbatim
  46. ===============================================================================
  47. ##### Extended features functions #####
  48. ===============================================================================
  49. [..] This section provides functions allowing to:
  50. (+) Update FIFO configuration
  51. @endverbatim
  52. * @{
  53. */
  54. #if defined (USB_OTG_FS)
  55. /**
  56. * @brief Set Tx FIFO
  57. * @param hpcd PCD handle
  58. * @param fifo The number of Tx fifo
  59. * @param size Fifo size
  60. * @retval HAL status
  61. */
  62. HAL_StatusTypeDef HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo, uint16_t size)
  63. {
  64. uint8_t i;
  65. uint32_t Tx_Offset;
  66. /* TXn min size = 16 words. (n : Transmit FIFO index)
  67. When a TxFIFO is not used, the Configuration should be as follows:
  68. case 1 : n > m and Txn is not used (n,m : Transmit FIFO indexes)
  69. --> Txm can use the space allocated for Txn.
  70. case2 : n < m and Txn is not used (n,m : Transmit FIFO indexes)
  71. --> Txn should be configured with the minimum space of 16 words
  72. The FIFO is used optimally when used TxFIFOs are allocated in the top
  73. of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones.
  74. When DMA is used 3n * FIFO locations should be reserved for internal DMA registers */
  75. Tx_Offset = hpcd->Instance->GRXFSIZ;
  76. if (fifo == 0U)
  77. {
  78. hpcd->Instance->DIEPTXF0_HNPTXFSIZ = ((uint32_t)size << 16) | Tx_Offset;
  79. }
  80. else
  81. {
  82. Tx_Offset += (hpcd->Instance->DIEPTXF0_HNPTXFSIZ) >> 16;
  83. for (i = 0U; i < (fifo - 1U); i++)
  84. {
  85. Tx_Offset += (hpcd->Instance->DIEPTXF[i] >> 16);
  86. }
  87. /* Multiply Tx_Size by 2 to get higher performance */
  88. hpcd->Instance->DIEPTXF[fifo - 1U] = ((uint32_t)size << 16) | Tx_Offset;
  89. }
  90. return HAL_OK;
  91. }
  92. /**
  93. * @brief Set Rx FIFO
  94. * @param hpcd PCD handle
  95. * @param size Size of Rx fifo
  96. * @retval HAL status
  97. */
  98. HAL_StatusTypeDef HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef *hpcd, uint16_t size)
  99. {
  100. hpcd->Instance->GRXFSIZ = size;
  101. return HAL_OK;
  102. }
  103. #endif /* defined (USB_OTG_FS) */
  104. #if defined (USB)
  105. /**
  106. * @brief Configure PMA for EP
  107. * @param hpcd Device instance
  108. * @param ep_addr endpoint address
  109. * @param ep_kind endpoint Kind
  110. * USB_SNG_BUF: Single Buffer used
  111. * USB_DBL_BUF: Double Buffer used
  112. * @param pmaadress: EP address in The PMA: In case of single buffer endpoint
  113. * this parameter is 16-bit value providing the address
  114. * in PMA allocated to endpoint.
  115. * In case of double buffer endpoint this parameter
  116. * is a 32-bit value providing the endpoint buffer 0 address
  117. * in the LSB part of 32-bit value and endpoint buffer 1 address
  118. * in the MSB part of 32-bit value.
  119. * @retval HAL status
  120. */
  121. HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd, uint16_t ep_addr,
  122. uint16_t ep_kind, uint32_t pmaadress)
  123. {
  124. PCD_EPTypeDef *ep;
  125. /* initialize ep structure*/
  126. if ((0x80U & ep_addr) == 0x80U)
  127. {
  128. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  129. }
  130. else
  131. {
  132. ep = &hpcd->OUT_ep[ep_addr];
  133. }
  134. /* Here we check if the endpoint is single or double Buffer*/
  135. if (ep_kind == PCD_SNG_BUF)
  136. {
  137. /* Single Buffer */
  138. ep->doublebuffer = 0U;
  139. /* Configure the PMA */
  140. ep->pmaadress = (uint16_t)pmaadress;
  141. }
  142. #if (USE_USB_DOUBLE_BUFFER == 1U)
  143. else /* USB_DBL_BUF */
  144. {
  145. /* Double Buffer Endpoint */
  146. ep->doublebuffer = 1U;
  147. /* Configure the PMA */
  148. ep->pmaaddr0 = (uint16_t)(pmaadress & 0xFFFFU);
  149. ep->pmaaddr1 = (uint16_t)((pmaadress & 0xFFFF0000U) >> 16);
  150. }
  151. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  152. return HAL_OK;
  153. }
  154. /**
  155. * @brief Software Device Connection,
  156. * this function is not required by USB OTG FS peripheral, it is used
  157. * only by USB Device FS peripheral.
  158. * @param hpcd PCD handle
  159. * @param state connection state (0 : disconnected / 1: connected)
  160. * @retval None
  161. */
  162. __weak void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
  163. {
  164. /* Prevent unused argument(s) compilation warning */
  165. UNUSED(hpcd);
  166. UNUSED(state);
  167. /* NOTE : This function Should not be modified, when the callback is needed,
  168. the HAL_PCDEx_SetConnectionState could be implemented in the user file
  169. */
  170. }
  171. #endif /* defined (USB) */
  172. /**
  173. * @brief Send LPM message to user layer callback.
  174. * @param hpcd PCD handle
  175. * @param msg LPM message
  176. * @retval HAL status
  177. */
  178. __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
  179. {
  180. /* Prevent unused argument(s) compilation warning */
  181. UNUSED(hpcd);
  182. UNUSED(msg);
  183. /* NOTE : This function should not be modified, when the callback is needed,
  184. the HAL_PCDEx_LPM_Callback could be implemented in the user file
  185. */
  186. }
  187. /**
  188. * @brief Send BatteryCharging message to user layer callback.
  189. * @param hpcd PCD handle
  190. * @param msg LPM message
  191. * @retval HAL status
  192. */
  193. __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
  194. {
  195. /* Prevent unused argument(s) compilation warning */
  196. UNUSED(hpcd);
  197. UNUSED(msg);
  198. /* NOTE : This function should not be modified, when the callback is needed,
  199. the HAL_PCDEx_BCD_Callback could be implemented in the user file
  200. */
  201. }
  202. /**
  203. * @}
  204. */
  205. /**
  206. * @}
  207. */
  208. #endif /* defined (USB) || defined (USB_OTG_FS) */
  209. #endif /* HAL_PCD_MODULE_ENABLED */
  210. /**
  211. * @}
  212. */
  213. /**
  214. * @}
  215. */