stm32f1xx_hal_pcd_ex.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_pcd_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended PCD HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the USB Peripheral Controller:
  8. * + Extended features functions: Update FIFO configuration,
  9. * PMA configuration for EPs
  10. *
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  15. *
  16. * Redistribution and use in source and binary forms, with or without modification,
  17. * are permitted provided that the following conditions are met:
  18. * 1. Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  33. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  35. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ******************************************************************************
  39. */
  40. /* Includes ------------------------------------------------------------------*/
  41. #include "stm32f1xx_hal.h"
  42. /** @addtogroup STM32F1xx_HAL_Driver
  43. * @{
  44. */
  45. #ifdef HAL_PCD_MODULE_ENABLED
  46. #if defined(STM32F102x6) || defined(STM32F102xB) || \
  47. defined(STM32F103x6) || defined(STM32F103xB) || \
  48. defined(STM32F103xE) || defined(STM32F103xG) || \
  49. defined(STM32F105xC) || defined(STM32F107xC)
  50. /** @defgroup PCDEx PCDEx
  51. * @brief PCD Extended HAL module driver
  52. * @{
  53. */
  54. /* Private types -------------------------------------------------------------*/
  55. /* Private variables ---------------------------------------------------------*/
  56. /* Private constants ---------------------------------------------------------*/
  57. /* Private macros ------------------------------------------------------------*/
  58. /* Private functions ---------------------------------------------------------*/
  59. /* Exported functions --------------------------------------------------------*/
  60. /** @defgroup PCDEx_Exported_Functions PCDEx Exported Functions
  61. * @{
  62. */
  63. /** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions
  64. * @brief PCDEx control functions
  65. *
  66. @verbatim
  67. ===============================================================================
  68. ##### Extended Peripheral Control functions #####
  69. ===============================================================================
  70. [..] This section provides functions allowing to:
  71. (+) Update FIFO (USB_OTG_FS)
  72. (+) Update PMA configuration (USB)
  73. @endverbatim
  74. * @{
  75. */
  76. #if defined (USB_OTG_FS)
  77. /**
  78. * @brief Set Tx FIFO
  79. * @param hpcd: PCD handle
  80. * @param fifo: The number of Tx fifo
  81. * @param size: Fifo size
  82. * @retval HAL status
  83. */
  84. HAL_StatusTypeDef HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo, uint16_t size)
  85. {
  86. uint8_t index = 0;
  87. uint32_t Tx_Offset = 0U;
  88. /* TXn min size = 16 words. (n : Transmit FIFO index)
  89. When a TxFIFO is not used, the Configuration should be as follows:
  90. case 1 : n > m and Txn is not used (n,m : Transmit FIFO indexes)
  91. --> Txm can use the space allocated for Txn.
  92. case2 : n < m and Txn is not used (n,m : Transmit FIFO indexes)
  93. --> Txn should be configured with the minimum space of 16 words
  94. The FIFO is used optimally when used TxFIFOs are allocated in the top
  95. of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones.
  96. When DMA is used 3n * FIFO locations should be reserved for internal DMA registers */
  97. Tx_Offset = hpcd->Instance->GRXFSIZ;
  98. if(fifo == 0U)
  99. {
  100. hpcd->Instance->DIEPTXF0_HNPTXFSIZ = (size << 16U) | Tx_Offset;
  101. }
  102. else
  103. {
  104. Tx_Offset += (hpcd->Instance->DIEPTXF0_HNPTXFSIZ) >> 16U;
  105. for(index = 0; index < (fifo - 1); index++)
  106. {
  107. Tx_Offset += (hpcd->Instance->DIEPTXF[index] >> 16U);
  108. }
  109. /* Multiply Tx_Size by 2 to get higher performance */
  110. hpcd->Instance->DIEPTXF[fifo - 1U] = (size << 16U) | Tx_Offset;
  111. }
  112. return HAL_OK;
  113. }
  114. /**
  115. * @brief Set Rx FIFO
  116. * @param hpcd: PCD handle
  117. * @param size: Size of Rx fifo
  118. * @retval HAL status
  119. */
  120. HAL_StatusTypeDef HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef *hpcd, uint16_t size)
  121. {
  122. hpcd->Instance->GRXFSIZ = size;
  123. return HAL_OK;
  124. }
  125. #endif /* USB_OTG_FS */
  126. #if defined (USB)
  127. /**
  128. * @brief Configure PMA for EP
  129. * @param hpcd : Device instance
  130. * @param ep_addr: endpoint address
  131. * @param ep_kind: endpoint Kind
  132. * USB_SNG_BUF: Single Buffer used
  133. * USB_DBL_BUF: Double Buffer used
  134. * @param pmaadress: EP address in The PMA: In case of single buffer endpoint
  135. * this parameter is 16-bit value providing the address
  136. * in PMA allocated to endpoint.
  137. * In case of double buffer endpoint this parameter
  138. * is a 32-bit value providing the endpoint buffer 0 address
  139. * in the LSB part of 32-bit value and endpoint buffer 1 address
  140. * in the MSB part of 32-bit value.
  141. * @retval HAL status
  142. */
  143. HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd,
  144. uint16_t ep_addr,
  145. uint16_t ep_kind,
  146. uint32_t pmaadress)
  147. {
  148. PCD_EPTypeDef *ep = NULL;
  149. /* initialize ep structure*/
  150. if ((ep_addr & 0x80U) == 0x80U)
  151. {
  152. ep = &hpcd->IN_ep[ep_addr & 0x7FU];
  153. }
  154. else
  155. {
  156. ep = &hpcd->OUT_ep[ep_addr];
  157. }
  158. /* Here we check if the endpoint is single or double Buffer*/
  159. if (ep_kind == PCD_SNG_BUF)
  160. {
  161. /*Single Buffer*/
  162. ep->doublebuffer = 0U;
  163. /*Configure te PMA*/
  164. ep->pmaadress = (uint16_t)pmaadress;
  165. }
  166. else /*USB_DBL_BUF*/
  167. {
  168. /*Double Buffer Endpoint*/
  169. ep->doublebuffer = 1U;
  170. /*Configure the PMA*/
  171. ep->pmaaddr0 = pmaadress & 0x0000FFFFU;
  172. ep->pmaaddr1 = (pmaadress & 0xFFFF0000U) >> 16U;
  173. }
  174. return HAL_OK;
  175. }
  176. #endif /* USB */
  177. /**
  178. * @}
  179. */
  180. /** @defgroup PCDEx_Exported_Functions_Group2 Peripheral State functions
  181. * @brief Manage device connection state
  182. * @{
  183. */
  184. /**
  185. * @brief Software Device Connection,
  186. * this function is not required by USB OTG FS peripheral, it is used
  187. * only by USB Device FS peripheral.
  188. * @param hpcd: PCD handle
  189. * @param state: connection state (0 : disconnected / 1: connected)
  190. * @retval None
  191. */
  192. __weak void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
  193. {
  194. /* Prevent unused argument(s) compilation warning */
  195. UNUSED(hpcd);
  196. UNUSED(state);
  197. /* NOTE : This function Should not be modified, when the callback is needed,
  198. the HAL_PCDEx_SetConnectionState could be implemented in the user file
  199. */
  200. }
  201. /**
  202. * @}
  203. */
  204. /**
  205. * @}
  206. */
  207. /**
  208. * @}
  209. */
  210. #endif /* STM32F102x6 || STM32F102xB || */
  211. /* STM32F103x6 || STM32F103xB || */
  212. /* STM32F103xE || STM32F103xG || */
  213. /* STM32F105xC || STM32F107xC */
  214. #endif /* HAL_PCD_MODULE_ENABLED */
  215. /**
  216. * @}
  217. */
  218. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/