stm32l1xx_hal_pcd_ex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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. * + Configururation of the PMA for EP
  9. *
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "stm32l1xx_hal.h"
  41. /** @addtogroup STM32L1xx_HAL_Driver
  42. * @{
  43. */
  44. /** @defgroup PCDEx PCDEx
  45. * @brief PCDEx HAL module driver
  46. * @{
  47. */
  48. #ifdef HAL_PCD_MODULE_ENABLED
  49. /* Private typedef -----------------------------------------------------------*/
  50. /* Private define ------------------------------------------------------------*/
  51. /* Private macro -------------------------------------------------------------*/
  52. /* Private variables ---------------------------------------------------------*/
  53. /* Private function prototypes -----------------------------------------------*/
  54. /* Private functions ---------------------------------------------------------*/
  55. /** @defgroup PCDEx_Exported_Functions PCDEx Exported Functions
  56. * @{
  57. */
  58. /** @defgroup PCDEx_Exported_Functions_Group2 Extended Peripheral Control functions
  59. * @brief Extended Peripheral Control functions
  60. *
  61. @verbatim
  62. ===============================================================================
  63. ##### Peripheral Control functions #####
  64. ===============================================================================
  65. [..] This section provides functions allowing to:
  66. (+) Configure PMA for the EndPoint
  67. @endverbatim
  68. * @{
  69. */
  70. /**
  71. * @brief Configure PMA for EP
  72. * @param hpcd : Device instance
  73. * @param ep_addr: endpoint address
  74. * @param ep_kind: endpoint Kind
  75. * USB_SNG_BUF: Single Buffer used
  76. * USB_DBL_BUF: Double Buffer used
  77. * @param pmaadress: EP address in The PMA: In case of single buffer endpoint
  78. * this parameter is 16-bit value providing the address
  79. * in PMA allocated to endpoint.
  80. * In case of double buffer endpoint this parameter
  81. * is a 32-bit value providing the endpoint buffer 0 address
  82. * in the LSB part of 32-bit value and endpoint buffer 1 address
  83. * in the MSB part of 32-bit value.
  84. * @retval : status
  85. */
  86. HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd,
  87. uint16_t ep_addr,
  88. uint16_t ep_kind,
  89. uint32_t pmaadress)
  90. {
  91. PCD_EPTypeDef *ep;
  92. /* initialize ep structure*/
  93. if ((0x80 & ep_addr) == 0x80)
  94. {
  95. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  96. }
  97. else
  98. {
  99. ep = &hpcd->OUT_ep[ep_addr];
  100. }
  101. /* Here we check if the endpoint is single or double Buffer*/
  102. if (ep_kind == PCD_SNG_BUF)
  103. {
  104. /*Single Buffer*/
  105. ep->doublebuffer = 0;
  106. /*Configure te PMA*/
  107. ep->pmaadress = (uint16_t)pmaadress;
  108. }
  109. else /*USB_DBL_BUF*/
  110. {
  111. /*Double Buffer Endpoint*/
  112. ep->doublebuffer = 1;
  113. /*Configure the PMA*/
  114. ep->pmaaddr0 = pmaadress & 0xFFFF;
  115. ep->pmaaddr1 = (pmaadress & 0xFFFF0000U) >> 16;
  116. }
  117. return HAL_OK;
  118. }
  119. /**
  120. * @}
  121. */
  122. /**
  123. * @}
  124. */
  125. #endif /* HAL_PCD_MODULE_ENABLED */
  126. /**
  127. * @}
  128. */
  129. /**
  130. * @}
  131. */
  132. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  133. /** HAL_PCD_UpdatePMA
  134. * @brief Update Packet Memory Area Table for endpoints
  135. * @param hpcd: PCD handle
  136. * @retval Status
  137. */
  138. HAL_StatusTypeDef HAL_PCD_UpdatePMA( PCD_HandleTypeDef *hpcd )
  139. {
  140. HAL_StatusTypeDef ret = HAL_OK;
  141. size_t nPMASize = 0x200; // whole PMA size
  142. const size_t nEpsCount = 8; // count of hardware logical enpoints
  143. const size_t nPMAEntrySize = 8; // the size of single PMA entry for each EP
  144. uint8_t epCount = 0; // amount of used endpoints, except EP0 which is always used
  145. if( 0 != hpcd->Instance->BTABLE )
  146. return HAL_ERROR; // unsupported
  147. // Check each EP to count amount of used endpoints, except EP0
  148. for( uint8_t epidx = 1; epidx < nEpsCount; ++epidx )
  149. {
  150. // Check EP IN:
  151. if( USB_EP_TX_DIS != PCD_GET_EP_TX_STATUS( hpcd->Instance, epidx ) )
  152. {
  153. epCount++; // count all used LOGICAL endpoints
  154. }
  155. else
  156. // Check EP OUT:
  157. if( USB_EP_RX_DIS != PCD_GET_EP_RX_STATUS( hpcd->Instance, epidx ) )
  158. {
  159. epCount++; // count all used LOGICAL endpoints
  160. }
  161. }
  162. // each EP takes @nPMAEntrySize bytes in PMATable, @pmaTableOffset is a table length now
  163. // First buffer must have address greater than the table size
  164. uint16_t pmaTableOffset = nPMAEntrySize * (epCount + 1/*EP0*/);
  165. hpcd->OUT_ep[ 0 ].pmaadress = pmaTableOffset;
  166. hpcd->OUT_ep[ 0 ].pmaaddr0 = (uint16_t)nPMASize; // invalid
  167. hpcd->OUT_ep[ 0 ].pmaaddr1 = (uint16_t)nPMASize; // invalid
  168. hpcd->OUT_ep[ 0 ].doublebuffer = 0;
  169. PCD_SET_EP_RX_ADDRESS(hpcd->Instance, 0, hpcd->OUT_ep[ 0 ].pmaadress );
  170. pmaTableOffset += hpcd->OUT_ep[ 0 ].maxpacket;
  171. hpcd->IN_ep[ 0 ].pmaadress = pmaTableOffset;
  172. hpcd->IN_ep[ 0 ].pmaaddr0 = (uint16_t)nPMASize; // invalid
  173. hpcd->IN_ep[ 0 ].pmaaddr1 = (uint16_t)nPMASize; // invalid
  174. hpcd->IN_ep[ 0 ].doublebuffer = 0;
  175. PCD_SET_EP_TX_ADDRESS(hpcd->Instance, 0, hpcd->IN_ep[ 0 ].pmaadress );
  176. pmaTableOffset += hpcd->IN_ep[ 0 ].maxpacket;
  177. // Check each EP to configure PMA area for each used endpoint, except EP0
  178. // check @epCount to reduce amount of cycles
  179. for( uint8_t epidx = 1; (epidx < 8) && (epCount > 0); ++epidx )
  180. {
  181. uint8_t epUsed = 0;
  182. // for each direction: @direction = 0 for OUT, and 1 for IN
  183. for( uint8_t direction = 0; direction <= 1; direction++ )
  184. {
  185. PCD_EPTypeDef *ep = NULL;
  186. // EP OUT:
  187. if( 0 == direction )
  188. if( USB_EP_RX_DIS != PCD_GET_EP_RX_STATUS( hpcd->Instance, epidx ) )
  189. {
  190. ep = &hpcd->OUT_ep[ epidx ];
  191. }
  192. // Check EP IN:
  193. if( 0 != direction )
  194. if( USB_EP_TX_DIS != PCD_GET_EP_TX_STATUS( hpcd->Instance, epidx ) )
  195. {
  196. ep = &hpcd->IN_ep[ epidx ];
  197. }
  198. if( NULL != ep )
  199. {
  200. epUsed = !0;
  201. if( 0 == ep->doublebuffer )
  202. {
  203. if( pmaTableOffset + ep->maxpacket >= nPMASize )
  204. {
  205. ep->pmaadress = (uint16_t)pmaTableOffset; // invalid
  206. ep->pmaaddr0 = (uint16_t)nPMASize; // invalid
  207. ep->pmaaddr1 = (uint16_t)nPMASize; // invalid
  208. ret = HAL_ERROR; // error, do not interrupt, try to configure with default values
  209. }
  210. else
  211. {
  212. ep->pmaadress = (uint16_t)pmaTableOffset;
  213. ep->pmaaddr0 = (uint16_t)nPMASize; // invalid
  214. ep->pmaaddr1 = (uint16_t)nPMASize; // invalid
  215. pmaTableOffset += ep->maxpacket;
  216. }
  217. }
  218. else
  219. {
  220. if( pmaTableOffset + 2 * ep->maxpacket >= nPMASize )
  221. {
  222. ep->pmaadress = (uint16_t)pmaTableOffset; // invalid
  223. ep->pmaaddr0 = (uint16_t)nPMASize; // invalid
  224. ep->pmaaddr1 = (uint16_t)nPMASize; // invalid
  225. ret = HAL_ERROR; // error, do not interrupt, try to configure with default values
  226. }
  227. else
  228. {
  229. ep->pmaadress = (uint16_t)nPMASize; // invalid
  230. ep->pmaaddr0 = (uint16_t)pmaTableOffset;
  231. pmaTableOffset += ep->maxpacket;
  232. ep->pmaaddr1 = (uint16_t)pmaTableOffset;
  233. pmaTableOffset += ep->maxpacket;
  234. }
  235. }
  236. if( ep->is_in )
  237. PCD_SET_EP_TX_ADDRESS(hpcd->Instance, epidx, ep->pmaadress );
  238. else
  239. PCD_SET_EP_RX_ADDRESS(hpcd->Instance, epidx, ep->pmaadress );
  240. }
  241. }
  242. if( epUsed )
  243. epCount--;
  244. }
  245. return ret;
  246. }
  247. /**
  248. * @brief Retrieve the TX queued packet details
  249. * @param hpcd: PCD handle
  250. * @param ep_addr: endpoint address
  251. * @param pBuf: pointer to the transmission buffer
  252. * @param len: amount of data to be sent
  253. * @retval HAL status:
  254. HAL_OK: there is a packet queued in EP IN
  255. HAL_BUSY: there no packet queued in EP IN
  256. HAL_ERROR: invalid EP specified
  257. */
  258. HAL_StatusTypeDef HAL_PCD_EP_GetTxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t * plen )
  259. {
  260. PCD_EPTypeDef *ep;
  261. if ((0x80 & ep_addr) != 0x80)
  262. {
  263. // Error: OUT EP specified. Only EP IN must be specified
  264. return HAL_ERROR;
  265. }
  266. if( USB_EP_TX_VALID == PCD_GET_EP_TX_STATUS(hpcd->Instance, ep_addr & 0x7F ) )
  267. {
  268. if( NULL != plen )
  269. {
  270. *plen = PCD_GET_EP_TX_CNT(hpcd->Instance, ep_addr & 0x7F );
  271. }
  272. return HAL_OK;
  273. }
  274. return HAL_BUSY;
  275. }