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.
HAL_L476/stm32l4xx_hal_pcd_ex.c@1:d0dfbce63a89, 2017-02-24 (annotated)
- Committer:
- elmot
- Date:
- Fri Feb 24 21:13:56 2017 +0000
- Revision:
- 1:d0dfbce63a89
Ready-to-copy
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| elmot | 1:d0dfbce63a89 | 1 | /** |
| elmot | 1:d0dfbce63a89 | 2 | ****************************************************************************** |
| elmot | 1:d0dfbce63a89 | 3 | * @file stm32l4xx_hal_pcd_ex.c |
| elmot | 1:d0dfbce63a89 | 4 | * @author MCD Application Team |
| elmot | 1:d0dfbce63a89 | 5 | * @version V1.5.1 |
| elmot | 1:d0dfbce63a89 | 6 | * @date 31-May-2016 |
| elmot | 1:d0dfbce63a89 | 7 | * @brief PCD Extended HAL module driver. |
| elmot | 1:d0dfbce63a89 | 8 | * This file provides firmware functions to manage the following |
| elmot | 1:d0dfbce63a89 | 9 | * functionalities of the USB Peripheral Controller: |
| elmot | 1:d0dfbce63a89 | 10 | * + Extended features functions |
| elmot | 1:d0dfbce63a89 | 11 | * |
| elmot | 1:d0dfbce63a89 | 12 | ****************************************************************************** |
| elmot | 1:d0dfbce63a89 | 13 | * @attention |
| elmot | 1:d0dfbce63a89 | 14 | * |
| elmot | 1:d0dfbce63a89 | 15 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
| elmot | 1:d0dfbce63a89 | 16 | * |
| elmot | 1:d0dfbce63a89 | 17 | * Redistribution and use in source and binary forms, with or without modification, |
| elmot | 1:d0dfbce63a89 | 18 | * are permitted provided that the following conditions are met: |
| elmot | 1:d0dfbce63a89 | 19 | * 1. Redistributions of source code must retain the above copyright notice, |
| elmot | 1:d0dfbce63a89 | 20 | * this list of conditions and the following disclaimer. |
| elmot | 1:d0dfbce63a89 | 21 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| elmot | 1:d0dfbce63a89 | 22 | * this list of conditions and the following disclaimer in the documentation |
| elmot | 1:d0dfbce63a89 | 23 | * and/or other materials provided with the distribution. |
| elmot | 1:d0dfbce63a89 | 24 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| elmot | 1:d0dfbce63a89 | 25 | * may be used to endorse or promote products derived from this software |
| elmot | 1:d0dfbce63a89 | 26 | * without specific prior written permission. |
| elmot | 1:d0dfbce63a89 | 27 | * |
| elmot | 1:d0dfbce63a89 | 28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| elmot | 1:d0dfbce63a89 | 29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| elmot | 1:d0dfbce63a89 | 30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| elmot | 1:d0dfbce63a89 | 31 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| elmot | 1:d0dfbce63a89 | 32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| elmot | 1:d0dfbce63a89 | 33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| elmot | 1:d0dfbce63a89 | 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| elmot | 1:d0dfbce63a89 | 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| elmot | 1:d0dfbce63a89 | 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| elmot | 1:d0dfbce63a89 | 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| elmot | 1:d0dfbce63a89 | 38 | * |
| elmot | 1:d0dfbce63a89 | 39 | ****************************************************************************** |
| elmot | 1:d0dfbce63a89 | 40 | */ |
| elmot | 1:d0dfbce63a89 | 41 | |
| elmot | 1:d0dfbce63a89 | 42 | /* Includes ------------------------------------------------------------------*/ |
| elmot | 1:d0dfbce63a89 | 43 | #include "stm32l4xx_hal.h" |
| elmot | 1:d0dfbce63a89 | 44 | |
| elmot | 1:d0dfbce63a89 | 45 | /** @addtogroup STM32L4xx_HAL_Driver |
| elmot | 1:d0dfbce63a89 | 46 | * @{ |
| elmot | 1:d0dfbce63a89 | 47 | */ |
| elmot | 1:d0dfbce63a89 | 48 | #ifdef HAL_PCD_MODULE_ENABLED |
| elmot | 1:d0dfbce63a89 | 49 | |
| elmot | 1:d0dfbce63a89 | 50 | #if defined(STM32L475xx) || defined(STM32L476xx) || \ |
| elmot | 1:d0dfbce63a89 | 51 | defined(STM32L485xx) || defined(STM32L486xx) || \ |
| elmot | 1:d0dfbce63a89 | 52 | defined(STM32L432xx) || defined(STM32L433xx) || \ |
| elmot | 1:d0dfbce63a89 | 53 | defined(STM32L442xx) || defined(STM32L443xx) |
| elmot | 1:d0dfbce63a89 | 54 | |
| elmot | 1:d0dfbce63a89 | 55 | |
| elmot | 1:d0dfbce63a89 | 56 | /** @defgroup PCDEx PCDEx |
| elmot | 1:d0dfbce63a89 | 57 | * @brief PCD Extended HAL module driver |
| elmot | 1:d0dfbce63a89 | 58 | * @{ |
| elmot | 1:d0dfbce63a89 | 59 | */ |
| elmot | 1:d0dfbce63a89 | 60 | |
| elmot | 1:d0dfbce63a89 | 61 | /* Private types -------------------------------------------------------------*/ |
| elmot | 1:d0dfbce63a89 | 62 | /* Private variables ---------------------------------------------------------*/ |
| elmot | 1:d0dfbce63a89 | 63 | /* Private constants ---------------------------------------------------------*/ |
| elmot | 1:d0dfbce63a89 | 64 | /* Private macros ------------------------------------------------------------*/ |
| elmot | 1:d0dfbce63a89 | 65 | /* Private functions ---------------------------------------------------------*/ |
| elmot | 1:d0dfbce63a89 | 66 | /* Exported functions --------------------------------------------------------*/ |
| elmot | 1:d0dfbce63a89 | 67 | |
| elmot | 1:d0dfbce63a89 | 68 | /** @defgroup PCDEx_Exported_Functions PCDEx Exported Functions |
| elmot | 1:d0dfbce63a89 | 69 | * @{ |
| elmot | 1:d0dfbce63a89 | 70 | */ |
| elmot | 1:d0dfbce63a89 | 71 | |
| elmot | 1:d0dfbce63a89 | 72 | /** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions |
| elmot | 1:d0dfbce63a89 | 73 | * @brief PCDEx control functions |
| elmot | 1:d0dfbce63a89 | 74 | * |
| elmot | 1:d0dfbce63a89 | 75 | @verbatim |
| elmot | 1:d0dfbce63a89 | 76 | =============================================================================== |
| elmot | 1:d0dfbce63a89 | 77 | ##### Extended features functions ##### |
| elmot | 1:d0dfbce63a89 | 78 | =============================================================================== |
| elmot | 1:d0dfbce63a89 | 79 | [..] This section provides functions allowing to: |
| elmot | 1:d0dfbce63a89 | 80 | (+) Update FIFO configuration |
| elmot | 1:d0dfbce63a89 | 81 | |
| elmot | 1:d0dfbce63a89 | 82 | @endverbatim |
| elmot | 1:d0dfbce63a89 | 83 | * @{ |
| elmot | 1:d0dfbce63a89 | 84 | */ |
| elmot | 1:d0dfbce63a89 | 85 | #if defined (USB_OTG_FS) |
| elmot | 1:d0dfbce63a89 | 86 | /** |
| elmot | 1:d0dfbce63a89 | 87 | * @brief Set Tx FIFO |
| elmot | 1:d0dfbce63a89 | 88 | * @param hpcd: PCD handle |
| elmot | 1:d0dfbce63a89 | 89 | * @param fifo: The number of Tx fifo |
| elmot | 1:d0dfbce63a89 | 90 | * @param size: Fifo size |
| elmot | 1:d0dfbce63a89 | 91 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 92 | */ |
| elmot | 1:d0dfbce63a89 | 93 | HAL_StatusTypeDef HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo, uint16_t size) |
| elmot | 1:d0dfbce63a89 | 94 | { |
| elmot | 1:d0dfbce63a89 | 95 | uint8_t index = 0; |
| elmot | 1:d0dfbce63a89 | 96 | uint32_t Tx_Offset = 0; |
| elmot | 1:d0dfbce63a89 | 97 | |
| elmot | 1:d0dfbce63a89 | 98 | /* TXn min size = 16 words. (n : Transmit FIFO index) |
| elmot | 1:d0dfbce63a89 | 99 | When a TxFIFO is not used, the Configuration should be as follows: |
| elmot | 1:d0dfbce63a89 | 100 | case 1 : n > m and Txn is not used (n,m : Transmit FIFO indexes) |
| elmot | 1:d0dfbce63a89 | 101 | --> Txm can use the space allocated for Txn. |
| elmot | 1:d0dfbce63a89 | 102 | case2 : n < m and Txn is not used (n,m : Transmit FIFO indexes) |
| elmot | 1:d0dfbce63a89 | 103 | --> Txn should be configured with the minimum space of 16 words |
| elmot | 1:d0dfbce63a89 | 104 | The FIFO is used optimally when used TxFIFOs are allocated in the top |
| elmot | 1:d0dfbce63a89 | 105 | of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones. |
| elmot | 1:d0dfbce63a89 | 106 | When DMA is used 3n * FIFO locations should be reserved for internal DMA registers */ |
| elmot | 1:d0dfbce63a89 | 107 | |
| elmot | 1:d0dfbce63a89 | 108 | Tx_Offset = hpcd->Instance->GRXFSIZ; |
| elmot | 1:d0dfbce63a89 | 109 | |
| elmot | 1:d0dfbce63a89 | 110 | if(fifo == 0) |
| elmot | 1:d0dfbce63a89 | 111 | { |
| elmot | 1:d0dfbce63a89 | 112 | hpcd->Instance->DIEPTXF0_HNPTXFSIZ = (size << 16) | Tx_Offset; |
| elmot | 1:d0dfbce63a89 | 113 | } |
| elmot | 1:d0dfbce63a89 | 114 | else |
| elmot | 1:d0dfbce63a89 | 115 | { |
| elmot | 1:d0dfbce63a89 | 116 | Tx_Offset += (hpcd->Instance->DIEPTXF0_HNPTXFSIZ) >> 16; |
| elmot | 1:d0dfbce63a89 | 117 | for (index = 0; index < (fifo - 1); index++) |
| elmot | 1:d0dfbce63a89 | 118 | { |
| elmot | 1:d0dfbce63a89 | 119 | Tx_Offset += (hpcd->Instance->DIEPTXF[index] >> 16); |
| elmot | 1:d0dfbce63a89 | 120 | } |
| elmot | 1:d0dfbce63a89 | 121 | |
| elmot | 1:d0dfbce63a89 | 122 | /* Multiply Tx_Size by 2 to get higher performance */ |
| elmot | 1:d0dfbce63a89 | 123 | hpcd->Instance->DIEPTXF[fifo - 1] = (size << 16) | Tx_Offset; |
| elmot | 1:d0dfbce63a89 | 124 | } |
| elmot | 1:d0dfbce63a89 | 125 | |
| elmot | 1:d0dfbce63a89 | 126 | return HAL_OK; |
| elmot | 1:d0dfbce63a89 | 127 | } |
| elmot | 1:d0dfbce63a89 | 128 | |
| elmot | 1:d0dfbce63a89 | 129 | /** |
| elmot | 1:d0dfbce63a89 | 130 | * @brief Set Rx FIFO |
| elmot | 1:d0dfbce63a89 | 131 | * @param hpcd: PCD handle |
| elmot | 1:d0dfbce63a89 | 132 | * @param size: Size of Rx fifo |
| elmot | 1:d0dfbce63a89 | 133 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 134 | */ |
| elmot | 1:d0dfbce63a89 | 135 | HAL_StatusTypeDef HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef *hpcd, uint16_t size) |
| elmot | 1:d0dfbce63a89 | 136 | { |
| elmot | 1:d0dfbce63a89 | 137 | hpcd->Instance->GRXFSIZ = size; |
| elmot | 1:d0dfbce63a89 | 138 | |
| elmot | 1:d0dfbce63a89 | 139 | return HAL_OK; |
| elmot | 1:d0dfbce63a89 | 140 | } |
| elmot | 1:d0dfbce63a89 | 141 | |
| elmot | 1:d0dfbce63a89 | 142 | /** |
| elmot | 1:d0dfbce63a89 | 143 | * @brief Activate LPM feature. |
| elmot | 1:d0dfbce63a89 | 144 | * @param hpcd: PCD handle |
| elmot | 1:d0dfbce63a89 | 145 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 146 | */ |
| elmot | 1:d0dfbce63a89 | 147 | HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd) |
| elmot | 1:d0dfbce63a89 | 148 | { |
| elmot | 1:d0dfbce63a89 | 149 | USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; |
| elmot | 1:d0dfbce63a89 | 150 | |
| elmot | 1:d0dfbce63a89 | 151 | hpcd->lpm_active = ENABLE; |
| elmot | 1:d0dfbce63a89 | 152 | hpcd->LPM_State = LPM_L0; |
| elmot | 1:d0dfbce63a89 | 153 | USBx->GINTMSK |= USB_OTG_GINTMSK_LPMINTM; |
| elmot | 1:d0dfbce63a89 | 154 | USBx->GLPMCFG |= (USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL); |
| elmot | 1:d0dfbce63a89 | 155 | |
| elmot | 1:d0dfbce63a89 | 156 | return HAL_OK; |
| elmot | 1:d0dfbce63a89 | 157 | } |
| elmot | 1:d0dfbce63a89 | 158 | |
| elmot | 1:d0dfbce63a89 | 159 | /** |
| elmot | 1:d0dfbce63a89 | 160 | * @brief Deactivate LPM feature. |
| elmot | 1:d0dfbce63a89 | 161 | * @param hpcd: PCD handle |
| elmot | 1:d0dfbce63a89 | 162 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 163 | */ |
| elmot | 1:d0dfbce63a89 | 164 | HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd) |
| elmot | 1:d0dfbce63a89 | 165 | { |
| elmot | 1:d0dfbce63a89 | 166 | USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; |
| elmot | 1:d0dfbce63a89 | 167 | |
| elmot | 1:d0dfbce63a89 | 168 | hpcd->lpm_active = DISABLE; |
| elmot | 1:d0dfbce63a89 | 169 | USBx->GINTMSK &= ~USB_OTG_GINTMSK_LPMINTM; |
| elmot | 1:d0dfbce63a89 | 170 | USBx->GLPMCFG &= ~(USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL); |
| elmot | 1:d0dfbce63a89 | 171 | |
| elmot | 1:d0dfbce63a89 | 172 | return HAL_OK; |
| elmot | 1:d0dfbce63a89 | 173 | } |
| elmot | 1:d0dfbce63a89 | 174 | |
| elmot | 1:d0dfbce63a89 | 175 | /** |
| elmot | 1:d0dfbce63a89 | 176 | * @brief Handle BatteryCharging Process. |
| elmot | 1:d0dfbce63a89 | 177 | * @param hpcd: PCD handle |
| elmot | 1:d0dfbce63a89 | 178 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 179 | */ |
| elmot | 1:d0dfbce63a89 | 180 | void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd) |
| elmot | 1:d0dfbce63a89 | 181 | { |
| elmot | 1:d0dfbce63a89 | 182 | USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; |
| elmot | 1:d0dfbce63a89 | 183 | uint32_t tickstart = HAL_GetTick(); |
| elmot | 1:d0dfbce63a89 | 184 | |
| elmot | 1:d0dfbce63a89 | 185 | /* Start BCD When device is connected */ |
| elmot | 1:d0dfbce63a89 | 186 | if (USBx_DEVICE->DCTL & USB_OTG_DCTL_SDIS) |
| elmot | 1:d0dfbce63a89 | 187 | { |
| elmot | 1:d0dfbce63a89 | 188 | /* Enable DCD : Data Contact Detect */ |
| elmot | 1:d0dfbce63a89 | 189 | USBx->GCCFG |= USB_OTG_GCCFG_DCDEN; |
| elmot | 1:d0dfbce63a89 | 190 | |
| elmot | 1:d0dfbce63a89 | 191 | /* Wait Detect flag or a timeout is happen*/ |
| elmot | 1:d0dfbce63a89 | 192 | while ((USBx->GCCFG & USB_OTG_GCCFG_DCDET) == 0) |
| elmot | 1:d0dfbce63a89 | 193 | { |
| elmot | 1:d0dfbce63a89 | 194 | /* Check for the Timeout */ |
| elmot | 1:d0dfbce63a89 | 195 | if((HAL_GetTick() - tickstart ) > 1000) |
| elmot | 1:d0dfbce63a89 | 196 | { |
| elmot | 1:d0dfbce63a89 | 197 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR); |
| elmot | 1:d0dfbce63a89 | 198 | return; |
| elmot | 1:d0dfbce63a89 | 199 | } |
| elmot | 1:d0dfbce63a89 | 200 | } |
| elmot | 1:d0dfbce63a89 | 201 | |
| elmot | 1:d0dfbce63a89 | 202 | /* Right response got */ |
| elmot | 1:d0dfbce63a89 | 203 | HAL_Delay(100); |
| elmot | 1:d0dfbce63a89 | 204 | |
| elmot | 1:d0dfbce63a89 | 205 | /* Check Detect flag*/ |
| elmot | 1:d0dfbce63a89 | 206 | if (USBx->GCCFG & USB_OTG_GCCFG_DCDET) |
| elmot | 1:d0dfbce63a89 | 207 | { |
| elmot | 1:d0dfbce63a89 | 208 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION); |
| elmot | 1:d0dfbce63a89 | 209 | } |
| elmot | 1:d0dfbce63a89 | 210 | |
| elmot | 1:d0dfbce63a89 | 211 | /*Primary detection: checks if connected to Standard Downstream Port |
| elmot | 1:d0dfbce63a89 | 212 | (without charging capability) */ |
| elmot | 1:d0dfbce63a89 | 213 | USBx->GCCFG &=~ USB_OTG_GCCFG_DCDEN; |
| elmot | 1:d0dfbce63a89 | 214 | USBx->GCCFG |= USB_OTG_GCCFG_PDEN; |
| elmot | 1:d0dfbce63a89 | 215 | HAL_Delay(100); |
| elmot | 1:d0dfbce63a89 | 216 | |
| elmot | 1:d0dfbce63a89 | 217 | if (!(USBx->GCCFG & USB_OTG_GCCFG_PDET)) |
| elmot | 1:d0dfbce63a89 | 218 | { |
| elmot | 1:d0dfbce63a89 | 219 | /* Case of Standard Downstream Port */ |
| elmot | 1:d0dfbce63a89 | 220 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT); |
| elmot | 1:d0dfbce63a89 | 221 | } |
| elmot | 1:d0dfbce63a89 | 222 | else |
| elmot | 1:d0dfbce63a89 | 223 | { |
| elmot | 1:d0dfbce63a89 | 224 | /* start secondary detection to check connection to Charging Downstream |
| elmot | 1:d0dfbce63a89 | 225 | Port or Dedicated Charging Port */ |
| elmot | 1:d0dfbce63a89 | 226 | USBx->GCCFG &=~ USB_OTG_GCCFG_PDEN; |
| elmot | 1:d0dfbce63a89 | 227 | USBx->GCCFG |= USB_OTG_GCCFG_SDEN; |
| elmot | 1:d0dfbce63a89 | 228 | HAL_Delay(100); |
| elmot | 1:d0dfbce63a89 | 229 | |
| elmot | 1:d0dfbce63a89 | 230 | if ((USBx->GCCFG) & USB_OTG_GCCFG_SDET) |
| elmot | 1:d0dfbce63a89 | 231 | { |
| elmot | 1:d0dfbce63a89 | 232 | /* case Dedicated Charging Port */ |
| elmot | 1:d0dfbce63a89 | 233 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT); |
| elmot | 1:d0dfbce63a89 | 234 | } |
| elmot | 1:d0dfbce63a89 | 235 | else |
| elmot | 1:d0dfbce63a89 | 236 | { |
| elmot | 1:d0dfbce63a89 | 237 | /* case Charging Downstream Port */ |
| elmot | 1:d0dfbce63a89 | 238 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT); |
| elmot | 1:d0dfbce63a89 | 239 | } |
| elmot | 1:d0dfbce63a89 | 240 | } |
| elmot | 1:d0dfbce63a89 | 241 | /* Battery Charging capability discovery finished */ |
| elmot | 1:d0dfbce63a89 | 242 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED); |
| elmot | 1:d0dfbce63a89 | 243 | } |
| elmot | 1:d0dfbce63a89 | 244 | } |
| elmot | 1:d0dfbce63a89 | 245 | |
| elmot | 1:d0dfbce63a89 | 246 | /** |
| elmot | 1:d0dfbce63a89 | 247 | * @brief Activate BatteryCharging feature. |
| elmot | 1:d0dfbce63a89 | 248 | * @param hpcd: PCD handle |
| elmot | 1:d0dfbce63a89 | 249 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 250 | */ |
| elmot | 1:d0dfbce63a89 | 251 | HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd) |
| elmot | 1:d0dfbce63a89 | 252 | { |
| elmot | 1:d0dfbce63a89 | 253 | USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; |
| elmot | 1:d0dfbce63a89 | 254 | |
| elmot | 1:d0dfbce63a89 | 255 | hpcd->battery_charging_active = ENABLE; |
| elmot | 1:d0dfbce63a89 | 256 | USBx->GCCFG |= (USB_OTG_GCCFG_BCDEN); |
| elmot | 1:d0dfbce63a89 | 257 | |
| elmot | 1:d0dfbce63a89 | 258 | return HAL_OK; |
| elmot | 1:d0dfbce63a89 | 259 | } |
| elmot | 1:d0dfbce63a89 | 260 | |
| elmot | 1:d0dfbce63a89 | 261 | /** |
| elmot | 1:d0dfbce63a89 | 262 | * @brief Deactivate BatteryCharging feature. |
| elmot | 1:d0dfbce63a89 | 263 | * @param hpcd: PCD handle |
| elmot | 1:d0dfbce63a89 | 264 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 265 | */ |
| elmot | 1:d0dfbce63a89 | 266 | HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd) |
| elmot | 1:d0dfbce63a89 | 267 | { |
| elmot | 1:d0dfbce63a89 | 268 | USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; |
| elmot | 1:d0dfbce63a89 | 269 | hpcd->battery_charging_active = DISABLE; |
| elmot | 1:d0dfbce63a89 | 270 | USBx->GCCFG &= ~(USB_OTG_GCCFG_BCDEN); |
| elmot | 1:d0dfbce63a89 | 271 | return HAL_OK; |
| elmot | 1:d0dfbce63a89 | 272 | } |
| elmot | 1:d0dfbce63a89 | 273 | #endif /* USB_OTG_FS */ |
| elmot | 1:d0dfbce63a89 | 274 | |
| elmot | 1:d0dfbce63a89 | 275 | #if defined (USB) |
| elmot | 1:d0dfbce63a89 | 276 | /** |
| elmot | 1:d0dfbce63a89 | 277 | * @brief Configure PMA for EP |
| elmot | 1:d0dfbce63a89 | 278 | * @param hpcd : Device instance |
| elmot | 1:d0dfbce63a89 | 279 | * @param ep_addr: endpoint address |
| elmot | 1:d0dfbce63a89 | 280 | * @param ep_kind: endpoint Kind |
| elmot | 1:d0dfbce63a89 | 281 | * USB_SNG_BUF: Single Buffer used |
| elmot | 1:d0dfbce63a89 | 282 | * USB_DBL_BUF: Double Buffer used |
| elmot | 1:d0dfbce63a89 | 283 | * @param pmaadress: EP address in The PMA: In case of single buffer endpoint |
| elmot | 1:d0dfbce63a89 | 284 | * this parameter is 16-bit value providing the address |
| elmot | 1:d0dfbce63a89 | 285 | * in PMA allocated to endpoint. |
| elmot | 1:d0dfbce63a89 | 286 | * In case of double buffer endpoint this parameter |
| elmot | 1:d0dfbce63a89 | 287 | * is a 32-bit value providing the endpoint buffer 0 address |
| elmot | 1:d0dfbce63a89 | 288 | * in the LSB part of 32-bit value and endpoint buffer 1 address |
| elmot | 1:d0dfbce63a89 | 289 | * in the MSB part of 32-bit value. |
| elmot | 1:d0dfbce63a89 | 290 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 291 | */ |
| elmot | 1:d0dfbce63a89 | 292 | |
| elmot | 1:d0dfbce63a89 | 293 | HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd, |
| elmot | 1:d0dfbce63a89 | 294 | uint16_t ep_addr, |
| elmot | 1:d0dfbce63a89 | 295 | uint16_t ep_kind, |
| elmot | 1:d0dfbce63a89 | 296 | uint32_t pmaadress) |
| elmot | 1:d0dfbce63a89 | 297 | |
| elmot | 1:d0dfbce63a89 | 298 | { |
| elmot | 1:d0dfbce63a89 | 299 | PCD_EPTypeDef *ep = NULL; |
| elmot | 1:d0dfbce63a89 | 300 | |
| elmot | 1:d0dfbce63a89 | 301 | /* initialize ep structure*/ |
| elmot | 1:d0dfbce63a89 | 302 | if ((0x80 & ep_addr) == 0x80) |
| elmot | 1:d0dfbce63a89 | 303 | { |
| elmot | 1:d0dfbce63a89 | 304 | ep = &hpcd->IN_ep[ep_addr & 0x7F]; |
| elmot | 1:d0dfbce63a89 | 305 | } |
| elmot | 1:d0dfbce63a89 | 306 | else |
| elmot | 1:d0dfbce63a89 | 307 | { |
| elmot | 1:d0dfbce63a89 | 308 | ep = &hpcd->OUT_ep[ep_addr]; |
| elmot | 1:d0dfbce63a89 | 309 | } |
| elmot | 1:d0dfbce63a89 | 310 | |
| elmot | 1:d0dfbce63a89 | 311 | /* Here we check if the endpoint is single or double Buffer*/ |
| elmot | 1:d0dfbce63a89 | 312 | if (ep_kind == PCD_SNG_BUF) |
| elmot | 1:d0dfbce63a89 | 313 | { |
| elmot | 1:d0dfbce63a89 | 314 | /*Single Buffer*/ |
| elmot | 1:d0dfbce63a89 | 315 | ep->doublebuffer = 0; |
| elmot | 1:d0dfbce63a89 | 316 | /*Configure te PMA*/ |
| elmot | 1:d0dfbce63a89 | 317 | ep->pmaadress = (uint16_t)pmaadress; |
| elmot | 1:d0dfbce63a89 | 318 | } |
| elmot | 1:d0dfbce63a89 | 319 | else /*USB_DBL_BUF*/ |
| elmot | 1:d0dfbce63a89 | 320 | { |
| elmot | 1:d0dfbce63a89 | 321 | /*Double Buffer Endpoint*/ |
| elmot | 1:d0dfbce63a89 | 322 | ep->doublebuffer = 1; |
| elmot | 1:d0dfbce63a89 | 323 | /*Configure the PMA*/ |
| elmot | 1:d0dfbce63a89 | 324 | ep->pmaaddr0 = pmaadress & 0xFFFF; |
| elmot | 1:d0dfbce63a89 | 325 | ep->pmaaddr1 = (pmaadress & 0xFFFF0000) >> 16; |
| elmot | 1:d0dfbce63a89 | 326 | } |
| elmot | 1:d0dfbce63a89 | 327 | |
| elmot | 1:d0dfbce63a89 | 328 | return HAL_OK; |
| elmot | 1:d0dfbce63a89 | 329 | } |
| elmot | 1:d0dfbce63a89 | 330 | |
| elmot | 1:d0dfbce63a89 | 331 | /** |
| elmot | 1:d0dfbce63a89 | 332 | * @brief Activate BatteryCharging feature. |
| elmot | 1:d0dfbce63a89 | 333 | * @param hpcd: PCD handle |
| elmot | 1:d0dfbce63a89 | 334 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 335 | */ |
| elmot | 1:d0dfbce63a89 | 336 | HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd) |
| elmot | 1:d0dfbce63a89 | 337 | { |
| elmot | 1:d0dfbce63a89 | 338 | USB_TypeDef *USBx = hpcd->Instance; |
| elmot | 1:d0dfbce63a89 | 339 | hpcd->battery_charging_active = ENABLE; |
| elmot | 1:d0dfbce63a89 | 340 | |
| elmot | 1:d0dfbce63a89 | 341 | USBx->BCDR |= (USB_BCDR_BCDEN); |
| elmot | 1:d0dfbce63a89 | 342 | /* Enable DCD : Data Contact Detect */ |
| elmot | 1:d0dfbce63a89 | 343 | USBx->BCDR |= (USB_BCDR_DCDEN); |
| elmot | 1:d0dfbce63a89 | 344 | |
| elmot | 1:d0dfbce63a89 | 345 | return HAL_OK; |
| elmot | 1:d0dfbce63a89 | 346 | } |
| elmot | 1:d0dfbce63a89 | 347 | |
| elmot | 1:d0dfbce63a89 | 348 | /** |
| elmot | 1:d0dfbce63a89 | 349 | * @brief Deactivate BatteryCharging feature. |
| elmot | 1:d0dfbce63a89 | 350 | * @param hpcd: PCD handle |
| elmot | 1:d0dfbce63a89 | 351 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 352 | */ |
| elmot | 1:d0dfbce63a89 | 353 | HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd) |
| elmot | 1:d0dfbce63a89 | 354 | { |
| elmot | 1:d0dfbce63a89 | 355 | USB_TypeDef *USBx = hpcd->Instance; |
| elmot | 1:d0dfbce63a89 | 356 | hpcd->battery_charging_active = DISABLE; |
| elmot | 1:d0dfbce63a89 | 357 | |
| elmot | 1:d0dfbce63a89 | 358 | USBx->BCDR &= ~(USB_BCDR_BCDEN); |
| elmot | 1:d0dfbce63a89 | 359 | return HAL_OK; |
| elmot | 1:d0dfbce63a89 | 360 | } |
| elmot | 1:d0dfbce63a89 | 361 | |
| elmot | 1:d0dfbce63a89 | 362 | /** |
| elmot | 1:d0dfbce63a89 | 363 | * @brief Handle BatteryCharging Process. |
| elmot | 1:d0dfbce63a89 | 364 | * @param hpcd: PCD handle |
| elmot | 1:d0dfbce63a89 | 365 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 366 | */ |
| elmot | 1:d0dfbce63a89 | 367 | void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd) |
| elmot | 1:d0dfbce63a89 | 368 | { |
| elmot | 1:d0dfbce63a89 | 369 | USB_TypeDef *USBx = hpcd->Instance; |
| elmot | 1:d0dfbce63a89 | 370 | uint32_t tickstart = HAL_GetTick(); |
| elmot | 1:d0dfbce63a89 | 371 | |
| elmot | 1:d0dfbce63a89 | 372 | /* Wait Detect flag or a timeout is happen*/ |
| elmot | 1:d0dfbce63a89 | 373 | while ((USBx->BCDR & USB_BCDR_DCDET) == 0) |
| elmot | 1:d0dfbce63a89 | 374 | { |
| elmot | 1:d0dfbce63a89 | 375 | /* Check for the Timeout */ |
| elmot | 1:d0dfbce63a89 | 376 | if((HAL_GetTick() - tickstart ) > 1000) |
| elmot | 1:d0dfbce63a89 | 377 | { |
| elmot | 1:d0dfbce63a89 | 378 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR); |
| elmot | 1:d0dfbce63a89 | 379 | return; |
| elmot | 1:d0dfbce63a89 | 380 | } |
| elmot | 1:d0dfbce63a89 | 381 | } |
| elmot | 1:d0dfbce63a89 | 382 | |
| elmot | 1:d0dfbce63a89 | 383 | HAL_Delay(300); |
| elmot | 1:d0dfbce63a89 | 384 | |
| elmot | 1:d0dfbce63a89 | 385 | /* Data Pin Contact ? Check Detect flag */ |
| elmot | 1:d0dfbce63a89 | 386 | if (USBx->BCDR & USB_BCDR_DCDET) |
| elmot | 1:d0dfbce63a89 | 387 | { |
| elmot | 1:d0dfbce63a89 | 388 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION); |
| elmot | 1:d0dfbce63a89 | 389 | } |
| elmot | 1:d0dfbce63a89 | 390 | /* Primary detection: checks if connected to Standard Downstream Port |
| elmot | 1:d0dfbce63a89 | 391 | (without charging capability) */ |
| elmot | 1:d0dfbce63a89 | 392 | USBx->BCDR &= ~(USB_BCDR_DCDEN); |
| elmot | 1:d0dfbce63a89 | 393 | USBx->BCDR |= (USB_BCDR_PDEN); |
| elmot | 1:d0dfbce63a89 | 394 | HAL_Delay(300); |
| elmot | 1:d0dfbce63a89 | 395 | |
| elmot | 1:d0dfbce63a89 | 396 | /* If Charger detect ? */ |
| elmot | 1:d0dfbce63a89 | 397 | if (USBx->BCDR & USB_BCDR_PDET) |
| elmot | 1:d0dfbce63a89 | 398 | { |
| elmot | 1:d0dfbce63a89 | 399 | /* Start secondary detection to check connection to Charging Downstream |
| elmot | 1:d0dfbce63a89 | 400 | Port or Dedicated Charging Port */ |
| elmot | 1:d0dfbce63a89 | 401 | USBx->BCDR &= ~(USB_BCDR_PDEN); |
| elmot | 1:d0dfbce63a89 | 402 | USBx->BCDR |= (USB_BCDR_SDEN); |
| elmot | 1:d0dfbce63a89 | 403 | HAL_Delay(300); |
| elmot | 1:d0dfbce63a89 | 404 | |
| elmot | 1:d0dfbce63a89 | 405 | /* If CDP ? */ |
| elmot | 1:d0dfbce63a89 | 406 | if (USBx->BCDR & USB_BCDR_SDET) |
| elmot | 1:d0dfbce63a89 | 407 | { |
| elmot | 1:d0dfbce63a89 | 408 | /* Dedicated Downstream Port DCP */ |
| elmot | 1:d0dfbce63a89 | 409 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT); |
| elmot | 1:d0dfbce63a89 | 410 | } |
| elmot | 1:d0dfbce63a89 | 411 | else |
| elmot | 1:d0dfbce63a89 | 412 | { |
| elmot | 1:d0dfbce63a89 | 413 | /* Charging Downstream Port CDP */ |
| elmot | 1:d0dfbce63a89 | 414 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT); |
| elmot | 1:d0dfbce63a89 | 415 | |
| elmot | 1:d0dfbce63a89 | 416 | /* Battery Charging capability discovery finished |
| elmot | 1:d0dfbce63a89 | 417 | Start Enumeration*/ |
| elmot | 1:d0dfbce63a89 | 418 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED); |
| elmot | 1:d0dfbce63a89 | 419 | } |
| elmot | 1:d0dfbce63a89 | 420 | } |
| elmot | 1:d0dfbce63a89 | 421 | else /* NO */ |
| elmot | 1:d0dfbce63a89 | 422 | { |
| elmot | 1:d0dfbce63a89 | 423 | /* Standard Downstream Port */ |
| elmot | 1:d0dfbce63a89 | 424 | HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT); |
| elmot | 1:d0dfbce63a89 | 425 | } |
| elmot | 1:d0dfbce63a89 | 426 | } |
| elmot | 1:d0dfbce63a89 | 427 | |
| elmot | 1:d0dfbce63a89 | 428 | /** |
| elmot | 1:d0dfbce63a89 | 429 | * @brief Activate LPM feature. |
| elmot | 1:d0dfbce63a89 | 430 | * @param hpcd: PCD handle |
| elmot | 1:d0dfbce63a89 | 431 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 432 | */ |
| elmot | 1:d0dfbce63a89 | 433 | HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd) |
| elmot | 1:d0dfbce63a89 | 434 | { |
| elmot | 1:d0dfbce63a89 | 435 | |
| elmot | 1:d0dfbce63a89 | 436 | USB_TypeDef *USBx = hpcd->Instance; |
| elmot | 1:d0dfbce63a89 | 437 | hpcd->lpm_active = ENABLE; |
| elmot | 1:d0dfbce63a89 | 438 | hpcd->LPM_State = LPM_L0; |
| elmot | 1:d0dfbce63a89 | 439 | |
| elmot | 1:d0dfbce63a89 | 440 | USBx->LPMCSR |= (USB_LPMCSR_LMPEN); |
| elmot | 1:d0dfbce63a89 | 441 | USBx->LPMCSR |= (USB_LPMCSR_LPMACK); |
| elmot | 1:d0dfbce63a89 | 442 | |
| elmot | 1:d0dfbce63a89 | 443 | |
| elmot | 1:d0dfbce63a89 | 444 | return HAL_OK; |
| elmot | 1:d0dfbce63a89 | 445 | } |
| elmot | 1:d0dfbce63a89 | 446 | |
| elmot | 1:d0dfbce63a89 | 447 | /** |
| elmot | 1:d0dfbce63a89 | 448 | * @brief Deactivate LPM feature. |
| elmot | 1:d0dfbce63a89 | 449 | * @param hpcd: PCD handle |
| elmot | 1:d0dfbce63a89 | 450 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 451 | */ |
| elmot | 1:d0dfbce63a89 | 452 | HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd) |
| elmot | 1:d0dfbce63a89 | 453 | { |
| elmot | 1:d0dfbce63a89 | 454 | USB_TypeDef *USBx = hpcd->Instance; |
| elmot | 1:d0dfbce63a89 | 455 | |
| elmot | 1:d0dfbce63a89 | 456 | hpcd->lpm_active = DISABLE; |
| elmot | 1:d0dfbce63a89 | 457 | |
| elmot | 1:d0dfbce63a89 | 458 | USBx->LPMCSR &= ~ (USB_LPMCSR_LMPEN); |
| elmot | 1:d0dfbce63a89 | 459 | USBx->LPMCSR &= ~ (USB_LPMCSR_LPMACK); |
| elmot | 1:d0dfbce63a89 | 460 | |
| elmot | 1:d0dfbce63a89 | 461 | return HAL_OK; |
| elmot | 1:d0dfbce63a89 | 462 | } |
| elmot | 1:d0dfbce63a89 | 463 | |
| elmot | 1:d0dfbce63a89 | 464 | #endif /* USB_FS */ |
| elmot | 1:d0dfbce63a89 | 465 | |
| elmot | 1:d0dfbce63a89 | 466 | /** |
| elmot | 1:d0dfbce63a89 | 467 | * @brief Send LPM message to user layer callback. |
| elmot | 1:d0dfbce63a89 | 468 | * @param hpcd: PCD handle |
| elmot | 1:d0dfbce63a89 | 469 | * @param msg: LPM message |
| elmot | 1:d0dfbce63a89 | 470 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 471 | */ |
| elmot | 1:d0dfbce63a89 | 472 | __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg) |
| elmot | 1:d0dfbce63a89 | 473 | { |
| elmot | 1:d0dfbce63a89 | 474 | /* Prevent unused argument(s) compilation warning */ |
| elmot | 1:d0dfbce63a89 | 475 | UNUSED(hpcd); |
| elmot | 1:d0dfbce63a89 | 476 | UNUSED(msg); |
| elmot | 1:d0dfbce63a89 | 477 | |
| elmot | 1:d0dfbce63a89 | 478 | /* NOTE : This function should not be modified, when the callback is needed, |
| elmot | 1:d0dfbce63a89 | 479 | the HAL_PCDEx_LPM_Callback could be implemented in the user file |
| elmot | 1:d0dfbce63a89 | 480 | */ |
| elmot | 1:d0dfbce63a89 | 481 | } |
| elmot | 1:d0dfbce63a89 | 482 | |
| elmot | 1:d0dfbce63a89 | 483 | /** |
| elmot | 1:d0dfbce63a89 | 484 | * @brief Send BatteryCharging message to user layer callback. |
| elmot | 1:d0dfbce63a89 | 485 | * @param hpcd: PCD handle |
| elmot | 1:d0dfbce63a89 | 486 | * @param msg: LPM message |
| elmot | 1:d0dfbce63a89 | 487 | * @retval HAL status |
| elmot | 1:d0dfbce63a89 | 488 | */ |
| elmot | 1:d0dfbce63a89 | 489 | __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg) |
| elmot | 1:d0dfbce63a89 | 490 | { |
| elmot | 1:d0dfbce63a89 | 491 | /* Prevent unused argument(s) compilation warning */ |
| elmot | 1:d0dfbce63a89 | 492 | UNUSED(hpcd); |
| elmot | 1:d0dfbce63a89 | 493 | UNUSED(msg); |
| elmot | 1:d0dfbce63a89 | 494 | |
| elmot | 1:d0dfbce63a89 | 495 | /* NOTE : This function should not be modified, when the callback is needed, |
| elmot | 1:d0dfbce63a89 | 496 | the HAL_PCDEx_BCD_Callback could be implemented in the user file |
| elmot | 1:d0dfbce63a89 | 497 | */ |
| elmot | 1:d0dfbce63a89 | 498 | } |
| elmot | 1:d0dfbce63a89 | 499 | |
| elmot | 1:d0dfbce63a89 | 500 | /** |
| elmot | 1:d0dfbce63a89 | 501 | * @} |
| elmot | 1:d0dfbce63a89 | 502 | */ |
| elmot | 1:d0dfbce63a89 | 503 | |
| elmot | 1:d0dfbce63a89 | 504 | /** |
| elmot | 1:d0dfbce63a89 | 505 | * @} |
| elmot | 1:d0dfbce63a89 | 506 | */ |
| elmot | 1:d0dfbce63a89 | 507 | /** |
| elmot | 1:d0dfbce63a89 | 508 | * @} |
| elmot | 1:d0dfbce63a89 | 509 | */ |
| elmot | 1:d0dfbce63a89 | 510 | |
| elmot | 1:d0dfbce63a89 | 511 | /** |
| elmot | 1:d0dfbce63a89 | 512 | * @} |
| elmot | 1:d0dfbce63a89 | 513 | */ |
| elmot | 1:d0dfbce63a89 | 514 | |
| elmot | 1:d0dfbce63a89 | 515 | #endif /* STM32L475xx || STM32L476xx || */ |
| elmot | 1:d0dfbce63a89 | 516 | /* STM32L485xx || STM32L486xx || */ |
| elmot | 1:d0dfbce63a89 | 517 | /* STM32L432xx || STM32L433xx || */ |
| elmot | 1:d0dfbce63a89 | 518 | /* STM32L442xx || STM32L443xx */ |
| elmot | 1:d0dfbce63a89 | 519 | |
| elmot | 1:d0dfbce63a89 | 520 | #endif /* HAL_PCD_MODULE_ENABLED */ |
| elmot | 1:d0dfbce63a89 | 521 | |
| elmot | 1:d0dfbce63a89 | 522 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |