Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
stm32l4xx_hal_pcd_ex.c
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l4xx_hal_pcd_ex.c 00004 * @author MCD Application Team 00005 * @version V1.5.1 00006 * @date 31-May-2016 00007 * @brief PCD Extended HAL module driver. 00008 * This file provides firmware functions to manage the following 00009 * functionalities of the USB Peripheral Controller: 00010 * + Extended features functions 00011 * 00012 ****************************************************************************** 00013 * @attention 00014 * 00015 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 00016 * 00017 * Redistribution and use in source and binary forms, with or without modification, 00018 * are permitted provided that the following conditions are met: 00019 * 1. Redistributions of source code must retain the above copyright notice, 00020 * this list of conditions and the following disclaimer. 00021 * 2. Redistributions in binary form must reproduce the above copyright notice, 00022 * this list of conditions and the following disclaimer in the documentation 00023 * and/or other materials provided with the distribution. 00024 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00025 * may be used to endorse or promote products derived from this software 00026 * without specific prior written permission. 00027 * 00028 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00029 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00030 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00031 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00032 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00033 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00034 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00035 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00036 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00037 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00038 * 00039 ****************************************************************************** 00040 */ 00041 00042 /* Includes ------------------------------------------------------------------*/ 00043 #include "stm32l4xx_hal.h" 00044 00045 /** @addtogroup STM32L4xx_HAL_Driver 00046 * @{ 00047 */ 00048 #ifdef HAL_PCD_MODULE_ENABLED 00049 00050 #if defined(STM32L475xx) || defined(STM32L476xx) || \ 00051 defined(STM32L485xx) || defined(STM32L486xx) || \ 00052 defined(STM32L432xx) || defined(STM32L433xx) || \ 00053 defined(STM32L442xx) || defined(STM32L443xx) 00054 00055 00056 /** @defgroup PCDEx PCDEx 00057 * @brief PCD Extended HAL module driver 00058 * @{ 00059 */ 00060 00061 /* Private types -------------------------------------------------------------*/ 00062 /* Private variables ---------------------------------------------------------*/ 00063 /* Private constants ---------------------------------------------------------*/ 00064 /* Private macros ------------------------------------------------------------*/ 00065 /* Private functions ---------------------------------------------------------*/ 00066 /* Exported functions --------------------------------------------------------*/ 00067 00068 /** @defgroup PCDEx_Exported_Functions PCDEx Exported Functions 00069 * @{ 00070 */ 00071 00072 /** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions 00073 * @brief PCDEx control functions 00074 * 00075 @verbatim 00076 =============================================================================== 00077 ##### Extended features functions ##### 00078 =============================================================================== 00079 [..] This section provides functions allowing to: 00080 (+) Update FIFO configuration 00081 00082 @endverbatim 00083 * @{ 00084 */ 00085 #if defined (USB_OTG_FS) 00086 /** 00087 * @brief Set Tx FIFO 00088 * @param hpcd: PCD handle 00089 * @param fifo: The number of Tx fifo 00090 * @param size: Fifo size 00091 * @retval HAL status 00092 */ 00093 HAL_StatusTypeDef HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo, uint16_t size) 00094 { 00095 uint8_t index = 0; 00096 uint32_t Tx_Offset = 0; 00097 00098 /* TXn min size = 16 words. (n : Transmit FIFO index) 00099 When a TxFIFO is not used, the Configuration should be as follows: 00100 case 1 : n > m and Txn is not used (n,m : Transmit FIFO indexes) 00101 --> Txm can use the space allocated for Txn. 00102 case2 : n < m and Txn is not used (n,m : Transmit FIFO indexes) 00103 --> Txn should be configured with the minimum space of 16 words 00104 The FIFO is used optimally when used TxFIFOs are allocated in the top 00105 of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones. 00106 When DMA is used 3n * FIFO locations should be reserved for internal DMA registers */ 00107 00108 Tx_Offset = hpcd->Instance->GRXFSIZ; 00109 00110 if(fifo == 0) 00111 { 00112 hpcd->Instance->DIEPTXF0_HNPTXFSIZ = (size << 16) | Tx_Offset; 00113 } 00114 else 00115 { 00116 Tx_Offset += (hpcd->Instance->DIEPTXF0_HNPTXFSIZ) >> 16; 00117 for (index = 0; index < (fifo - 1); index++) 00118 { 00119 Tx_Offset += (hpcd->Instance->DIEPTXF[index] >> 16); 00120 } 00121 00122 /* Multiply Tx_Size by 2 to get higher performance */ 00123 hpcd->Instance->DIEPTXF[fifo - 1] = (size << 16) | Tx_Offset; 00124 } 00125 00126 return HAL_OK; 00127 } 00128 00129 /** 00130 * @brief Set Rx FIFO 00131 * @param hpcd: PCD handle 00132 * @param size: Size of Rx fifo 00133 * @retval HAL status 00134 */ 00135 HAL_StatusTypeDef HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef *hpcd, uint16_t size) 00136 { 00137 hpcd->Instance->GRXFSIZ = size; 00138 00139 return HAL_OK; 00140 } 00141 00142 /** 00143 * @brief Activate LPM feature. 00144 * @param hpcd: PCD handle 00145 * @retval HAL status 00146 */ 00147 HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd) 00148 { 00149 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; 00150 00151 hpcd->lpm_active = ENABLE; 00152 hpcd->LPM_State = LPM_L0; 00153 USBx->GINTMSK |= USB_OTG_GINTMSK_LPMINTM; 00154 USBx->GLPMCFG |= (USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL); 00155 00156 return HAL_OK; 00157 } 00158 00159 /** 00160 * @brief Deactivate LPM feature. 00161 * @param hpcd: PCD handle 00162 * @retval HAL status 00163 */ 00164 HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd) 00165 { 00166 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; 00167 00168 hpcd->lpm_active = DISABLE; 00169 USBx->GINTMSK &= ~USB_OTG_GINTMSK_LPMINTM; 00170 USBx->GLPMCFG &= ~(USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL); 00171 00172 return HAL_OK; 00173 } 00174 00175 /** 00176 * @brief Handle BatteryCharging Process. 00177 * @param hpcd: PCD handle 00178 * @retval HAL status 00179 */ 00180 void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd) 00181 { 00182 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; 00183 uint32_t tickstart = HAL_GetTick(); 00184 00185 /* Start BCD When device is connected */ 00186 if (USBx_DEVICE->DCTL & USB_OTG_DCTL_SDIS) 00187 { 00188 /* Enable DCD : Data Contact Detect */ 00189 USBx->GCCFG |= USB_OTG_GCCFG_DCDEN; 00190 00191 /* Wait Detect flag or a timeout is happen*/ 00192 while ((USBx->GCCFG & USB_OTG_GCCFG_DCDET) == 0) 00193 { 00194 /* Check for the Timeout */ 00195 if((HAL_GetTick() - tickstart ) > 1000) 00196 { 00197 HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR); 00198 return; 00199 } 00200 } 00201 00202 /* Right response got */ 00203 HAL_Delay(100); 00204 00205 /* Check Detect flag*/ 00206 if (USBx->GCCFG & USB_OTG_GCCFG_DCDET) 00207 { 00208 HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION); 00209 } 00210 00211 /*Primary detection: checks if connected to Standard Downstream Port 00212 (without charging capability) */ 00213 USBx->GCCFG &=~ USB_OTG_GCCFG_DCDEN; 00214 USBx->GCCFG |= USB_OTG_GCCFG_PDEN; 00215 HAL_Delay(100); 00216 00217 if (!(USBx->GCCFG & USB_OTG_GCCFG_PDET)) 00218 { 00219 /* Case of Standard Downstream Port */ 00220 HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT); 00221 } 00222 else 00223 { 00224 /* start secondary detection to check connection to Charging Downstream 00225 Port or Dedicated Charging Port */ 00226 USBx->GCCFG &=~ USB_OTG_GCCFG_PDEN; 00227 USBx->GCCFG |= USB_OTG_GCCFG_SDEN; 00228 HAL_Delay(100); 00229 00230 if ((USBx->GCCFG) & USB_OTG_GCCFG_SDET) 00231 { 00232 /* case Dedicated Charging Port */ 00233 HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT); 00234 } 00235 else 00236 { 00237 /* case Charging Downstream Port */ 00238 HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT); 00239 } 00240 } 00241 /* Battery Charging capability discovery finished */ 00242 HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED); 00243 } 00244 } 00245 00246 /** 00247 * @brief Activate BatteryCharging feature. 00248 * @param hpcd: PCD handle 00249 * @retval HAL status 00250 */ 00251 HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd) 00252 { 00253 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; 00254 00255 hpcd->battery_charging_active = ENABLE; 00256 USBx->GCCFG |= (USB_OTG_GCCFG_BCDEN); 00257 00258 return HAL_OK; 00259 } 00260 00261 /** 00262 * @brief Deactivate BatteryCharging feature. 00263 * @param hpcd: PCD handle 00264 * @retval HAL status 00265 */ 00266 HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd) 00267 { 00268 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; 00269 hpcd->battery_charging_active = DISABLE; 00270 USBx->GCCFG &= ~(USB_OTG_GCCFG_BCDEN); 00271 return HAL_OK; 00272 } 00273 #endif /* USB_OTG_FS */ 00274 00275 #if defined (USB) 00276 /** 00277 * @brief Configure PMA for EP 00278 * @param hpcd : Device instance 00279 * @param ep_addr: endpoint address 00280 * @param ep_kind: endpoint Kind 00281 * USB_SNG_BUF: Single Buffer used 00282 * USB_DBL_BUF: Double Buffer used 00283 * @param pmaadress: EP address in The PMA: In case of single buffer endpoint 00284 * this parameter is 16-bit value providing the address 00285 * in PMA allocated to endpoint. 00286 * In case of double buffer endpoint this parameter 00287 * is a 32-bit value providing the endpoint buffer 0 address 00288 * in the LSB part of 32-bit value and endpoint buffer 1 address 00289 * in the MSB part of 32-bit value. 00290 * @retval HAL status 00291 */ 00292 00293 HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd, 00294 uint16_t ep_addr, 00295 uint16_t ep_kind, 00296 uint32_t pmaadress) 00297 00298 { 00299 PCD_EPTypeDef *ep = NULL; 00300 00301 /* initialize ep structure*/ 00302 if ((0x80 & ep_addr) == 0x80) 00303 { 00304 ep = &hpcd->IN_ep[ep_addr & 0x7F]; 00305 } 00306 else 00307 { 00308 ep = &hpcd->OUT_ep[ep_addr]; 00309 } 00310 00311 /* Here we check if the endpoint is single or double Buffer*/ 00312 if (ep_kind == PCD_SNG_BUF) 00313 { 00314 /*Single Buffer*/ 00315 ep->doublebuffer = 0; 00316 /*Configure te PMA*/ 00317 ep->pmaadress = (uint16_t)pmaadress; 00318 } 00319 else /*USB_DBL_BUF*/ 00320 { 00321 /*Double Buffer Endpoint*/ 00322 ep->doublebuffer = 1; 00323 /*Configure the PMA*/ 00324 ep->pmaaddr0 = pmaadress & 0xFFFF; 00325 ep->pmaaddr1 = (pmaadress & 0xFFFF0000) >> 16; 00326 } 00327 00328 return HAL_OK; 00329 } 00330 00331 /** 00332 * @brief Activate BatteryCharging feature. 00333 * @param hpcd: PCD handle 00334 * @retval HAL status 00335 */ 00336 HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd) 00337 { 00338 USB_TypeDef *USBx = hpcd->Instance; 00339 hpcd->battery_charging_active = ENABLE; 00340 00341 USBx->BCDR |= (USB_BCDR_BCDEN); 00342 /* Enable DCD : Data Contact Detect */ 00343 USBx->BCDR |= (USB_BCDR_DCDEN); 00344 00345 return HAL_OK; 00346 } 00347 00348 /** 00349 * @brief Deactivate BatteryCharging feature. 00350 * @param hpcd: PCD handle 00351 * @retval HAL status 00352 */ 00353 HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd) 00354 { 00355 USB_TypeDef *USBx = hpcd->Instance; 00356 hpcd->battery_charging_active = DISABLE; 00357 00358 USBx->BCDR &= ~(USB_BCDR_BCDEN); 00359 return HAL_OK; 00360 } 00361 00362 /** 00363 * @brief Handle BatteryCharging Process. 00364 * @param hpcd: PCD handle 00365 * @retval HAL status 00366 */ 00367 void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd) 00368 { 00369 USB_TypeDef *USBx = hpcd->Instance; 00370 uint32_t tickstart = HAL_GetTick(); 00371 00372 /* Wait Detect flag or a timeout is happen*/ 00373 while ((USBx->BCDR & USB_BCDR_DCDET) == 0) 00374 { 00375 /* Check for the Timeout */ 00376 if((HAL_GetTick() - tickstart ) > 1000) 00377 { 00378 HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR); 00379 return; 00380 } 00381 } 00382 00383 HAL_Delay(300); 00384 00385 /* Data Pin Contact ? Check Detect flag */ 00386 if (USBx->BCDR & USB_BCDR_DCDET) 00387 { 00388 HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION); 00389 } 00390 /* Primary detection: checks if connected to Standard Downstream Port 00391 (without charging capability) */ 00392 USBx->BCDR &= ~(USB_BCDR_DCDEN); 00393 USBx->BCDR |= (USB_BCDR_PDEN); 00394 HAL_Delay(300); 00395 00396 /* If Charger detect ? */ 00397 if (USBx->BCDR & USB_BCDR_PDET) 00398 { 00399 /* Start secondary detection to check connection to Charging Downstream 00400 Port or Dedicated Charging Port */ 00401 USBx->BCDR &= ~(USB_BCDR_PDEN); 00402 USBx->BCDR |= (USB_BCDR_SDEN); 00403 HAL_Delay(300); 00404 00405 /* If CDP ? */ 00406 if (USBx->BCDR & USB_BCDR_SDET) 00407 { 00408 /* Dedicated Downstream Port DCP */ 00409 HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT); 00410 } 00411 else 00412 { 00413 /* Charging Downstream Port CDP */ 00414 HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT); 00415 00416 /* Battery Charging capability discovery finished 00417 Start Enumeration*/ 00418 HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED); 00419 } 00420 } 00421 else /* NO */ 00422 { 00423 /* Standard Downstream Port */ 00424 HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT); 00425 } 00426 } 00427 00428 /** 00429 * @brief Activate LPM feature. 00430 * @param hpcd: PCD handle 00431 * @retval HAL status 00432 */ 00433 HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd) 00434 { 00435 00436 USB_TypeDef *USBx = hpcd->Instance; 00437 hpcd->lpm_active = ENABLE; 00438 hpcd->LPM_State = LPM_L0; 00439 00440 USBx->LPMCSR |= (USB_LPMCSR_LMPEN); 00441 USBx->LPMCSR |= (USB_LPMCSR_LPMACK); 00442 00443 00444 return HAL_OK; 00445 } 00446 00447 /** 00448 * @brief Deactivate LPM feature. 00449 * @param hpcd: PCD handle 00450 * @retval HAL status 00451 */ 00452 HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd) 00453 { 00454 USB_TypeDef *USBx = hpcd->Instance; 00455 00456 hpcd->lpm_active = DISABLE; 00457 00458 USBx->LPMCSR &= ~ (USB_LPMCSR_LMPEN); 00459 USBx->LPMCSR &= ~ (USB_LPMCSR_LPMACK); 00460 00461 return HAL_OK; 00462 } 00463 00464 #endif /* USB_FS */ 00465 00466 /** 00467 * @brief Send LPM message to user layer callback. 00468 * @param hpcd: PCD handle 00469 * @param msg: LPM message 00470 * @retval HAL status 00471 */ 00472 __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg) 00473 { 00474 /* Prevent unused argument(s) compilation warning */ 00475 UNUSED(hpcd); 00476 UNUSED(msg); 00477 00478 /* NOTE : This function should not be modified, when the callback is needed, 00479 the HAL_PCDEx_LPM_Callback could be implemented in the user file 00480 */ 00481 } 00482 00483 /** 00484 * @brief Send BatteryCharging message to user layer callback. 00485 * @param hpcd: PCD handle 00486 * @param msg: LPM message 00487 * @retval HAL status 00488 */ 00489 __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg) 00490 { 00491 /* Prevent unused argument(s) compilation warning */ 00492 UNUSED(hpcd); 00493 UNUSED(msg); 00494 00495 /* NOTE : This function should not be modified, when the callback is needed, 00496 the HAL_PCDEx_BCD_Callback could be implemented in the user file 00497 */ 00498 } 00499 00500 /** 00501 * @} 00502 */ 00503 00504 /** 00505 * @} 00506 */ 00507 /** 00508 * @} 00509 */ 00510 00511 /** 00512 * @} 00513 */ 00514 00515 #endif /* STM32L475xx || STM32L476xx || */ 00516 /* STM32L485xx || STM32L486xx || */ 00517 /* STM32L432xx || STM32L433xx || */ 00518 /* STM32L442xx || STM32L443xx */ 00519 00520 #endif /* HAL_PCD_MODULE_ENABLED */ 00521 00522 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 10:59:58 by
