stm32f1xx_hal_pcd_ex.c 8.3 KB

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