Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1 /**
lypinator 0:bb348c97df44 2 ******************************************************************************
lypinator 0:bb348c97df44 3 * @file stm32f4xx_hal_pcd.c
lypinator 0:bb348c97df44 4 * @author MCD Application Team
lypinator 0:bb348c97df44 5 * @brief PCD HAL module driver.
lypinator 0:bb348c97df44 6 * This file provides firmware functions to manage the following
lypinator 0:bb348c97df44 7 * functionalities of the USB Peripheral Controller:
lypinator 0:bb348c97df44 8 * + Initialization and de-initialization functions
lypinator 0:bb348c97df44 9 * + IO operation functions
lypinator 0:bb348c97df44 10 * + Peripheral Control functions
lypinator 0:bb348c97df44 11 * + Peripheral State functions
lypinator 0:bb348c97df44 12 *
lypinator 0:bb348c97df44 13 @verbatim
lypinator 0:bb348c97df44 14 ==============================================================================
lypinator 0:bb348c97df44 15 ##### How to use this driver #####
lypinator 0:bb348c97df44 16 ==============================================================================
lypinator 0:bb348c97df44 17 [..]
lypinator 0:bb348c97df44 18 The PCD HAL driver can be used as follows:
lypinator 0:bb348c97df44 19
lypinator 0:bb348c97df44 20 (#) Declare a PCD_HandleTypeDef handle structure, for example:
lypinator 0:bb348c97df44 21 PCD_HandleTypeDef hpcd;
lypinator 0:bb348c97df44 22
lypinator 0:bb348c97df44 23 (#) Fill parameters of Init structure in HCD handle
lypinator 0:bb348c97df44 24
lypinator 0:bb348c97df44 25 (#) Call HAL_PCD_Init() API to initialize the PCD peripheral (Core, Device core, ...)
lypinator 0:bb348c97df44 26
lypinator 0:bb348c97df44 27 (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
lypinator 0:bb348c97df44 28 (##) Enable the PCD/USB Low Level interface clock using
lypinator 0:bb348c97df44 29 (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
lypinator 0:bb348c97df44 30 (+++) __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); (For High Speed Mode)
lypinator 0:bb348c97df44 31
lypinator 0:bb348c97df44 32 (##) Initialize the related GPIO clocks
lypinator 0:bb348c97df44 33 (##) Configure PCD pin-out
lypinator 0:bb348c97df44 34 (##) Configure PCD NVIC interrupt
lypinator 0:bb348c97df44 35
lypinator 0:bb348c97df44 36 (#)Associate the Upper USB device stack to the HAL PCD Driver:
lypinator 0:bb348c97df44 37 (##) hpcd.pData = pdev;
lypinator 0:bb348c97df44 38
lypinator 0:bb348c97df44 39 (#)Enable PCD transmission and reception:
lypinator 0:bb348c97df44 40 (##) HAL_PCD_Start();
lypinator 0:bb348c97df44 41
lypinator 0:bb348c97df44 42 @endverbatim
lypinator 0:bb348c97df44 43 ******************************************************************************
lypinator 0:bb348c97df44 44 * @attention
lypinator 0:bb348c97df44 45 *
lypinator 0:bb348c97df44 46 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
lypinator 0:bb348c97df44 47 *
lypinator 0:bb348c97df44 48 * Redistribution and use in source and binary forms, with or without modification,
lypinator 0:bb348c97df44 49 * are permitted provided that the following conditions are met:
lypinator 0:bb348c97df44 50 * 1. Redistributions of source code must retain the above copyright notice,
lypinator 0:bb348c97df44 51 * this list of conditions and the following disclaimer.
lypinator 0:bb348c97df44 52 * 2. Redistributions in binary form must reproduce the above copyright notice,
lypinator 0:bb348c97df44 53 * this list of conditions and the following disclaimer in the documentation
lypinator 0:bb348c97df44 54 * and/or other materials provided with the distribution.
lypinator 0:bb348c97df44 55 * 3. Neither the name of STMicroelectronics nor the names of its contributors
lypinator 0:bb348c97df44 56 * may be used to endorse or promote products derived from this software
lypinator 0:bb348c97df44 57 * without specific prior written permission.
lypinator 0:bb348c97df44 58 *
lypinator 0:bb348c97df44 59 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
lypinator 0:bb348c97df44 60 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
lypinator 0:bb348c97df44 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
lypinator 0:bb348c97df44 62 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
lypinator 0:bb348c97df44 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
lypinator 0:bb348c97df44 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
lypinator 0:bb348c97df44 65 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
lypinator 0:bb348c97df44 66 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
lypinator 0:bb348c97df44 67 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
lypinator 0:bb348c97df44 68 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
lypinator 0:bb348c97df44 69 *
lypinator 0:bb348c97df44 70 ******************************************************************************
lypinator 0:bb348c97df44 71 */
lypinator 0:bb348c97df44 72
lypinator 0:bb348c97df44 73 /* Includes ------------------------------------------------------------------*/
lypinator 0:bb348c97df44 74 #include "stm32f4xx_hal.h"
lypinator 0:bb348c97df44 75
lypinator 0:bb348c97df44 76 /** @addtogroup STM32F4xx_HAL_Driver
lypinator 0:bb348c97df44 77 * @{
lypinator 0:bb348c97df44 78 */
lypinator 0:bb348c97df44 79
lypinator 0:bb348c97df44 80 /** @defgroup PCD PCD
lypinator 0:bb348c97df44 81 * @brief PCD HAL module driver
lypinator 0:bb348c97df44 82 * @{
lypinator 0:bb348c97df44 83 */
lypinator 0:bb348c97df44 84
lypinator 0:bb348c97df44 85 #ifdef HAL_PCD_MODULE_ENABLED
lypinator 0:bb348c97df44 86 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || \
lypinator 0:bb348c97df44 87 defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
lypinator 0:bb348c97df44 88 defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE) || defined(STM32F446xx) || \
lypinator 0:bb348c97df44 89 defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
lypinator 0:bb348c97df44 90 defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
lypinator 0:bb348c97df44 91 /* Private types -------------------------------------------------------------*/
lypinator 0:bb348c97df44 92 /* Private variables ---------------------------------------------------------*/
lypinator 0:bb348c97df44 93 /* Private constants ---------------------------------------------------------*/
lypinator 0:bb348c97df44 94 /* Private macros ------------------------------------------------------------*/
lypinator 0:bb348c97df44 95 /** @defgroup PCD_Private_Macros PCD Private Macros
lypinator 0:bb348c97df44 96 * @{
lypinator 0:bb348c97df44 97 */
lypinator 0:bb348c97df44 98 #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
lypinator 0:bb348c97df44 99 #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
lypinator 0:bb348c97df44 100 /**
lypinator 0:bb348c97df44 101 * @}
lypinator 0:bb348c97df44 102 */
lypinator 0:bb348c97df44 103
lypinator 0:bb348c97df44 104 /* Private functions prototypes ----------------------------------------------*/
lypinator 0:bb348c97df44 105 /** @defgroup PCD_Private_Functions PCD Private Functions
lypinator 0:bb348c97df44 106 * @{
lypinator 0:bb348c97df44 107 */
lypinator 0:bb348c97df44 108 static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
lypinator 0:bb348c97df44 109 /**
lypinator 0:bb348c97df44 110 * @}
lypinator 0:bb348c97df44 111 */
lypinator 0:bb348c97df44 112
lypinator 0:bb348c97df44 113 /* Exported functions --------------------------------------------------------*/
lypinator 0:bb348c97df44 114 /** @defgroup PCD_Exported_Functions PCD Exported Functions
lypinator 0:bb348c97df44 115 * @{
lypinator 0:bb348c97df44 116 */
lypinator 0:bb348c97df44 117
lypinator 0:bb348c97df44 118 /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
lypinator 0:bb348c97df44 119 * @brief Initialization and Configuration functions
lypinator 0:bb348c97df44 120 *
lypinator 0:bb348c97df44 121 @verbatim
lypinator 0:bb348c97df44 122 ===============================================================================
lypinator 0:bb348c97df44 123 ##### Initialization and de-initialization functions #####
lypinator 0:bb348c97df44 124 ===============================================================================
lypinator 0:bb348c97df44 125 [..] This section provides functions allowing to:
lypinator 0:bb348c97df44 126
lypinator 0:bb348c97df44 127 @endverbatim
lypinator 0:bb348c97df44 128 * @{
lypinator 0:bb348c97df44 129 */
lypinator 0:bb348c97df44 130
lypinator 0:bb348c97df44 131 /**
lypinator 0:bb348c97df44 132 * @brief Initializes the PCD according to the specified
lypinator 0:bb348c97df44 133 * parameters in the PCD_InitTypeDef and initialize the associated handle.
lypinator 0:bb348c97df44 134 * @param hpcd PCD handle
lypinator 0:bb348c97df44 135 * @retval HAL status
lypinator 0:bb348c97df44 136 */
lypinator 0:bb348c97df44 137 HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 138 {
lypinator 0:bb348c97df44 139 uint32_t i = 0U;
lypinator 0:bb348c97df44 140
lypinator 0:bb348c97df44 141 /* Check the PCD handle allocation */
lypinator 0:bb348c97df44 142 if(hpcd == NULL)
lypinator 0:bb348c97df44 143 {
lypinator 0:bb348c97df44 144 return HAL_ERROR;
lypinator 0:bb348c97df44 145 }
lypinator 0:bb348c97df44 146
lypinator 0:bb348c97df44 147 /* Check the parameters */
lypinator 0:bb348c97df44 148 assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
lypinator 0:bb348c97df44 149 /* MBED */
lypinator 0:bb348c97df44 150 if(hpcd->State == HAL_PCD_STATE_RESET)
lypinator 0:bb348c97df44 151 {
lypinator 0:bb348c97df44 152 /* Allocate lock resource and initialize it */
lypinator 0:bb348c97df44 153 hpcd->Lock = HAL_UNLOCKED;
lypinator 0:bb348c97df44 154 for (i = 0; i < hpcd->Init.dev_endpoints ; i++)
lypinator 0:bb348c97df44 155 hpcd->EPLock[i].Lock = HAL_UNLOCKED;
lypinator 0:bb348c97df44 156 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
lypinator 0:bb348c97df44 157 HAL_PCD_MspInit(hpcd);
lypinator 0:bb348c97df44 158 }
lypinator 0:bb348c97df44 159
lypinator 0:bb348c97df44 160 hpcd->State = HAL_PCD_STATE_BUSY;
lypinator 0:bb348c97df44 161 /* MBED */
lypinator 0:bb348c97df44 162
lypinator 0:bb348c97df44 163 /* Disable the Interrupts */
lypinator 0:bb348c97df44 164 __HAL_PCD_DISABLE(hpcd);
lypinator 0:bb348c97df44 165
lypinator 0:bb348c97df44 166 /*Init the Core (common init.) */
lypinator 0:bb348c97df44 167 USB_CoreInit(hpcd->Instance, hpcd->Init);
lypinator 0:bb348c97df44 168
lypinator 0:bb348c97df44 169 /* Force Device Mode*/
lypinator 0:bb348c97df44 170 USB_SetCurrentMode(hpcd->Instance , USB_OTG_DEVICE_MODE);
lypinator 0:bb348c97df44 171
lypinator 0:bb348c97df44 172 /* Init endpoints structures */
lypinator 0:bb348c97df44 173 for (i = 0U; i < 15U; i++)
lypinator 0:bb348c97df44 174 {
lypinator 0:bb348c97df44 175 /* Init ep structure */
lypinator 0:bb348c97df44 176 hpcd->IN_ep[i].is_in = 1U;
lypinator 0:bb348c97df44 177 hpcd->IN_ep[i].num = i;
lypinator 0:bb348c97df44 178 hpcd->IN_ep[i].tx_fifo_num = i;
lypinator 0:bb348c97df44 179 /* Control until ep is activated */
lypinator 0:bb348c97df44 180 hpcd->IN_ep[i].type = EP_TYPE_CTRL;
lypinator 0:bb348c97df44 181 hpcd->IN_ep[i].maxpacket = 0U;
lypinator 0:bb348c97df44 182 hpcd->IN_ep[i].xfer_buff = 0U;
lypinator 0:bb348c97df44 183 hpcd->IN_ep[i].xfer_len = 0U;
lypinator 0:bb348c97df44 184 }
lypinator 0:bb348c97df44 185
lypinator 0:bb348c97df44 186 for (i = 0U; i < 15U; i++)
lypinator 0:bb348c97df44 187 {
lypinator 0:bb348c97df44 188 hpcd->OUT_ep[i].is_in = 0U;
lypinator 0:bb348c97df44 189 hpcd->OUT_ep[i].num = i;
lypinator 0:bb348c97df44 190 hpcd->IN_ep[i].tx_fifo_num = i;
lypinator 0:bb348c97df44 191 /* Control until ep is activated */
lypinator 0:bb348c97df44 192 hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
lypinator 0:bb348c97df44 193 hpcd->OUT_ep[i].maxpacket = 0U;
lypinator 0:bb348c97df44 194 hpcd->OUT_ep[i].xfer_buff = 0U;
lypinator 0:bb348c97df44 195 hpcd->OUT_ep[i].xfer_len = 0U;
lypinator 0:bb348c97df44 196
lypinator 0:bb348c97df44 197 hpcd->Instance->DIEPTXF[i] = 0U;
lypinator 0:bb348c97df44 198 }
lypinator 0:bb348c97df44 199
lypinator 0:bb348c97df44 200 /* Init Device */
lypinator 0:bb348c97df44 201 USB_DevInit(hpcd->Instance, hpcd->Init);
lypinator 0:bb348c97df44 202
lypinator 0:bb348c97df44 203 hpcd->State= HAL_PCD_STATE_READY;
lypinator 0:bb348c97df44 204
lypinator 0:bb348c97df44 205 #ifdef USB_OTG_GLPMCFG_LPMEN
lypinator 0:bb348c97df44 206 /* Activate LPM */
lypinator 0:bb348c97df44 207 if (hpcd->Init.lpm_enable == 1U)
lypinator 0:bb348c97df44 208 {
lypinator 0:bb348c97df44 209 HAL_PCDEx_ActivateLPM(hpcd);
lypinator 0:bb348c97df44 210 }
lypinator 0:bb348c97df44 211 #endif /* USB_OTG_GLPMCFG_LPMEN */
lypinator 0:bb348c97df44 212
lypinator 0:bb348c97df44 213 #ifdef USB_OTG_GCCFG_BCDEN
lypinator 0:bb348c97df44 214 /* Activate Battery charging */
lypinator 0:bb348c97df44 215 if (hpcd->Init.battery_charging_enable == 1U)
lypinator 0:bb348c97df44 216 {
lypinator 0:bb348c97df44 217 HAL_PCDEx_ActivateBCD(hpcd);
lypinator 0:bb348c97df44 218 }
lypinator 0:bb348c97df44 219 #endif /* USB_OTG_GCCFG_BCDEN */
lypinator 0:bb348c97df44 220
lypinator 0:bb348c97df44 221 USB_DevDisconnect (hpcd->Instance);
lypinator 0:bb348c97df44 222 return HAL_OK;
lypinator 0:bb348c97df44 223 }
lypinator 0:bb348c97df44 224
lypinator 0:bb348c97df44 225 /**
lypinator 0:bb348c97df44 226 * @brief DeInitializes the PCD peripheral.
lypinator 0:bb348c97df44 227 * @param hpcd PCD handle
lypinator 0:bb348c97df44 228 * @retval HAL status
lypinator 0:bb348c97df44 229 */
lypinator 0:bb348c97df44 230 HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 231 {
lypinator 0:bb348c97df44 232 /* Check the PCD handle allocation */
lypinator 0:bb348c97df44 233 if(hpcd == NULL)
lypinator 0:bb348c97df44 234 {
lypinator 0:bb348c97df44 235 return HAL_ERROR;
lypinator 0:bb348c97df44 236 }
lypinator 0:bb348c97df44 237
lypinator 0:bb348c97df44 238 hpcd->State = HAL_PCD_STATE_BUSY;
lypinator 0:bb348c97df44 239
lypinator 0:bb348c97df44 240 /* Stop Device */
lypinator 0:bb348c97df44 241 HAL_PCD_Stop(hpcd);
lypinator 0:bb348c97df44 242
lypinator 0:bb348c97df44 243 /* DeInit the low level hardware */
lypinator 0:bb348c97df44 244 HAL_PCD_MspDeInit(hpcd);
lypinator 0:bb348c97df44 245
lypinator 0:bb348c97df44 246 hpcd->State = HAL_PCD_STATE_RESET;
lypinator 0:bb348c97df44 247
lypinator 0:bb348c97df44 248 return HAL_OK;
lypinator 0:bb348c97df44 249 }
lypinator 0:bb348c97df44 250
lypinator 0:bb348c97df44 251 /**
lypinator 0:bb348c97df44 252 * @brief Initializes the PCD MSP.
lypinator 0:bb348c97df44 253 * @param hpcd PCD handle
lypinator 0:bb348c97df44 254 * @retval None
lypinator 0:bb348c97df44 255 */
lypinator 0:bb348c97df44 256 __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 257 {
lypinator 0:bb348c97df44 258 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 259 UNUSED(hpcd);
lypinator 0:bb348c97df44 260 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 261 the HAL_PCD_MspInit could be implemented in the user file
lypinator 0:bb348c97df44 262 */
lypinator 0:bb348c97df44 263 }
lypinator 0:bb348c97df44 264
lypinator 0:bb348c97df44 265 /**
lypinator 0:bb348c97df44 266 * @brief DeInitializes PCD MSP.
lypinator 0:bb348c97df44 267 * @param hpcd PCD handle
lypinator 0:bb348c97df44 268 * @retval None
lypinator 0:bb348c97df44 269 */
lypinator 0:bb348c97df44 270 __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 271 {
lypinator 0:bb348c97df44 272 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 273 UNUSED(hpcd);
lypinator 0:bb348c97df44 274 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 275 the HAL_PCD_MspDeInit could be implemented in the user file
lypinator 0:bb348c97df44 276 */
lypinator 0:bb348c97df44 277 }
lypinator 0:bb348c97df44 278
lypinator 0:bb348c97df44 279 /**
lypinator 0:bb348c97df44 280 * @}
lypinator 0:bb348c97df44 281 */
lypinator 0:bb348c97df44 282
lypinator 0:bb348c97df44 283 /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
lypinator 0:bb348c97df44 284 * @brief Data transfers functions
lypinator 0:bb348c97df44 285 *
lypinator 0:bb348c97df44 286 @verbatim
lypinator 0:bb348c97df44 287 ===============================================================================
lypinator 0:bb348c97df44 288 ##### IO operation functions #####
lypinator 0:bb348c97df44 289 ===============================================================================
lypinator 0:bb348c97df44 290 [..]
lypinator 0:bb348c97df44 291 This subsection provides a set of functions allowing to manage the PCD data
lypinator 0:bb348c97df44 292 transfers.
lypinator 0:bb348c97df44 293
lypinator 0:bb348c97df44 294 @endverbatim
lypinator 0:bb348c97df44 295 * @{
lypinator 0:bb348c97df44 296 */
lypinator 0:bb348c97df44 297
lypinator 0:bb348c97df44 298 /**
lypinator 0:bb348c97df44 299 * @brief Start The USB OTG Device.
lypinator 0:bb348c97df44 300 * @param hpcd PCD handle
lypinator 0:bb348c97df44 301 * @retval HAL status
lypinator 0:bb348c97df44 302 */
lypinator 0:bb348c97df44 303 HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 304 {
lypinator 0:bb348c97df44 305 /* MBED */
lypinator 0:bb348c97df44 306 //__HAL_LOCK(hpcd);
lypinator 0:bb348c97df44 307 /* MBED */
lypinator 0:bb348c97df44 308 USB_DevConnect (hpcd->Instance);
lypinator 0:bb348c97df44 309 __HAL_PCD_ENABLE(hpcd);
lypinator 0:bb348c97df44 310 /* MBED */
lypinator 0:bb348c97df44 311 //__HAL_UNLOCK(hpcd);
lypinator 0:bb348c97df44 312 /* MBED */
lypinator 0:bb348c97df44 313 return HAL_OK;
lypinator 0:bb348c97df44 314 }
lypinator 0:bb348c97df44 315
lypinator 0:bb348c97df44 316 /**
lypinator 0:bb348c97df44 317 * @brief Stop The USB OTG Device.
lypinator 0:bb348c97df44 318 * @param hpcd PCD handle
lypinator 0:bb348c97df44 319 * @retval HAL status
lypinator 0:bb348c97df44 320 */
lypinator 0:bb348c97df44 321 HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 322 {
lypinator 0:bb348c97df44 323 /* MBED */
lypinator 0:bb348c97df44 324 //__HAL_LOCK(hpcd);
lypinator 0:bb348c97df44 325 /* MBED */
lypinator 0:bb348c97df44 326 __HAL_PCD_DISABLE(hpcd);
lypinator 0:bb348c97df44 327 USB_StopDevice(hpcd->Instance);
lypinator 0:bb348c97df44 328 USB_DevDisconnect(hpcd->Instance);
lypinator 0:bb348c97df44 329 /* MBED */
lypinator 0:bb348c97df44 330 //__HAL_UNLOCK(hpcd);
lypinator 0:bb348c97df44 331 /* MBED */
lypinator 0:bb348c97df44 332 return HAL_OK;
lypinator 0:bb348c97df44 333 }
lypinator 0:bb348c97df44 334
lypinator 0:bb348c97df44 335 /**
lypinator 0:bb348c97df44 336 * @brief Handles PCD interrupt request.
lypinator 0:bb348c97df44 337 * @param hpcd PCD handle
lypinator 0:bb348c97df44 338 * @retval HAL status
lypinator 0:bb348c97df44 339 */
lypinator 0:bb348c97df44 340 void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 341 {
lypinator 0:bb348c97df44 342 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
lypinator 0:bb348c97df44 343 uint32_t i = 0U, ep_intr = 0U, epint = 0U, epnum = 0U;
lypinator 0:bb348c97df44 344 uint32_t fifoemptymsk = 0U, temp = 0U;
lypinator 0:bb348c97df44 345 USB_OTG_EPTypeDef *ep;
lypinator 0:bb348c97df44 346 uint32_t hclk = 180000000U;
lypinator 0:bb348c97df44 347
lypinator 0:bb348c97df44 348 /* ensure that we are in device mode */
lypinator 0:bb348c97df44 349 if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE)
lypinator 0:bb348c97df44 350 {
lypinator 0:bb348c97df44 351 /* avoid spurious interrupt */
lypinator 0:bb348c97df44 352 if(__HAL_PCD_IS_INVALID_INTERRUPT(hpcd))
lypinator 0:bb348c97df44 353 {
lypinator 0:bb348c97df44 354 return;
lypinator 0:bb348c97df44 355 }
lypinator 0:bb348c97df44 356
lypinator 0:bb348c97df44 357 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS))
lypinator 0:bb348c97df44 358 {
lypinator 0:bb348c97df44 359 /* incorrect mode, acknowledge the interrupt */
lypinator 0:bb348c97df44 360 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
lypinator 0:bb348c97df44 361 }
lypinator 0:bb348c97df44 362
lypinator 0:bb348c97df44 363 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT))
lypinator 0:bb348c97df44 364 {
lypinator 0:bb348c97df44 365 epnum = 0U;
lypinator 0:bb348c97df44 366
lypinator 0:bb348c97df44 367 /* Read in the device interrupt bits */
lypinator 0:bb348c97df44 368 ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
lypinator 0:bb348c97df44 369
lypinator 0:bb348c97df44 370 while ( ep_intr )
lypinator 0:bb348c97df44 371 {
lypinator 0:bb348c97df44 372 if (ep_intr & 0x1U)
lypinator 0:bb348c97df44 373 {
lypinator 0:bb348c97df44 374 epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, epnum);
lypinator 0:bb348c97df44 375
lypinator 0:bb348c97df44 376 if(( epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
lypinator 0:bb348c97df44 377 {
lypinator 0:bb348c97df44 378 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
lypinator 0:bb348c97df44 379
lypinator 0:bb348c97df44 380 if(hpcd->Init.dma_enable == 1U)
lypinator 0:bb348c97df44 381 {
lypinator 0:bb348c97df44 382 hpcd->OUT_ep[epnum].xfer_count = hpcd->OUT_ep[epnum].maxpacket- (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
lypinator 0:bb348c97df44 383 hpcd->OUT_ep[epnum].xfer_buff += hpcd->OUT_ep[epnum].maxpacket;
lypinator 0:bb348c97df44 384 }
lypinator 0:bb348c97df44 385
lypinator 0:bb348c97df44 386 HAL_PCD_DataOutStageCallback(hpcd, epnum);
lypinator 0:bb348c97df44 387 if(hpcd->Init.dma_enable == 1U)
lypinator 0:bb348c97df44 388 {
lypinator 0:bb348c97df44 389 if((epnum == 0U) && (hpcd->OUT_ep[epnum].xfer_len == 0U))
lypinator 0:bb348c97df44 390 {
lypinator 0:bb348c97df44 391 /* this is ZLP, so prepare EP0 for next setup */
lypinator 0:bb348c97df44 392 USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
lypinator 0:bb348c97df44 393 }
lypinator 0:bb348c97df44 394 }
lypinator 0:bb348c97df44 395 }
lypinator 0:bb348c97df44 396
lypinator 0:bb348c97df44 397 if(( epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
lypinator 0:bb348c97df44 398 {
lypinator 0:bb348c97df44 399 /* Inform the upper layer that a setup packet is available */
lypinator 0:bb348c97df44 400 HAL_PCD_SetupStageCallback(hpcd);
lypinator 0:bb348c97df44 401 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
lypinator 0:bb348c97df44 402 }
lypinator 0:bb348c97df44 403
lypinator 0:bb348c97df44 404 if(( epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
lypinator 0:bb348c97df44 405 {
lypinator 0:bb348c97df44 406 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
lypinator 0:bb348c97df44 407 }
lypinator 0:bb348c97df44 408
lypinator 0:bb348c97df44 409 #ifdef USB_OTG_DOEPINT_OTEPSPR
lypinator 0:bb348c97df44 410 /* Clear Status Phase Received interrupt */
lypinator 0:bb348c97df44 411 if(( epint & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
lypinator 0:bb348c97df44 412 {
lypinator 0:bb348c97df44 413 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
lypinator 0:bb348c97df44 414 }
lypinator 0:bb348c97df44 415 #endif /* USB_OTG_DOEPINT_OTEPSPR */
lypinator 0:bb348c97df44 416 }
lypinator 0:bb348c97df44 417 epnum++;
lypinator 0:bb348c97df44 418 ep_intr >>= 1U;
lypinator 0:bb348c97df44 419 }
lypinator 0:bb348c97df44 420 }
lypinator 0:bb348c97df44 421
lypinator 0:bb348c97df44 422 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
lypinator 0:bb348c97df44 423 {
lypinator 0:bb348c97df44 424 /* Read in the device interrupt bits */
lypinator 0:bb348c97df44 425 ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
lypinator 0:bb348c97df44 426
lypinator 0:bb348c97df44 427 epnum = 0U;
lypinator 0:bb348c97df44 428
lypinator 0:bb348c97df44 429 while ( ep_intr )
lypinator 0:bb348c97df44 430 {
lypinator 0:bb348c97df44 431 if (ep_intr & 0x1U) /* In ITR */
lypinator 0:bb348c97df44 432 {
lypinator 0:bb348c97df44 433 epint = USB_ReadDevInEPInterrupt(hpcd->Instance, epnum);
lypinator 0:bb348c97df44 434
lypinator 0:bb348c97df44 435 if(( epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
lypinator 0:bb348c97df44 436 {
lypinator 0:bb348c97df44 437 fifoemptymsk = 0x1U << epnum;
lypinator 0:bb348c97df44 438
lypinator 0:bb348c97df44 439 /* MBED */
lypinator 0:bb348c97df44 440 atomic_clr_u32(&USBx_DEVICE->DIEPEMPMSK,fifoemptymsk);
lypinator 0:bb348c97df44 441 /* MBED */
lypinator 0:bb348c97df44 442
lypinator 0:bb348c97df44 443 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
lypinator 0:bb348c97df44 444
lypinator 0:bb348c97df44 445 if (hpcd->Init.dma_enable == 1U)
lypinator 0:bb348c97df44 446 {
lypinator 0:bb348c97df44 447 hpcd->IN_ep[epnum].xfer_buff += hpcd->IN_ep[epnum].maxpacket;
lypinator 0:bb348c97df44 448 }
lypinator 0:bb348c97df44 449
lypinator 0:bb348c97df44 450 HAL_PCD_DataInStageCallback(hpcd, epnum);
lypinator 0:bb348c97df44 451
lypinator 0:bb348c97df44 452 if (hpcd->Init.dma_enable == 1U)
lypinator 0:bb348c97df44 453 {
lypinator 0:bb348c97df44 454 /* this is ZLP, so prepare EP0 for next setup */
lypinator 0:bb348c97df44 455 if((epnum == 0U) && (hpcd->IN_ep[epnum].xfer_len == 0U))
lypinator 0:bb348c97df44 456 {
lypinator 0:bb348c97df44 457 /* prepare to rx more setup packets */
lypinator 0:bb348c97df44 458 USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
lypinator 0:bb348c97df44 459 }
lypinator 0:bb348c97df44 460 }
lypinator 0:bb348c97df44 461 }
lypinator 0:bb348c97df44 462 if(( epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
lypinator 0:bb348c97df44 463 {
lypinator 0:bb348c97df44 464 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
lypinator 0:bb348c97df44 465 }
lypinator 0:bb348c97df44 466 if(( epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
lypinator 0:bb348c97df44 467 {
lypinator 0:bb348c97df44 468 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
lypinator 0:bb348c97df44 469 }
lypinator 0:bb348c97df44 470 if(( epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
lypinator 0:bb348c97df44 471 {
lypinator 0:bb348c97df44 472 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
lypinator 0:bb348c97df44 473 }
lypinator 0:bb348c97df44 474 if(( epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
lypinator 0:bb348c97df44 475 {
lypinator 0:bb348c97df44 476 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
lypinator 0:bb348c97df44 477 }
lypinator 0:bb348c97df44 478 if(( epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
lypinator 0:bb348c97df44 479 {
lypinator 0:bb348c97df44 480 PCD_WriteEmptyTxFifo(hpcd , epnum);
lypinator 0:bb348c97df44 481 }
lypinator 0:bb348c97df44 482 }
lypinator 0:bb348c97df44 483 epnum++;
lypinator 0:bb348c97df44 484 ep_intr >>= 1U;
lypinator 0:bb348c97df44 485 }
lypinator 0:bb348c97df44 486 }
lypinator 0:bb348c97df44 487
lypinator 0:bb348c97df44 488 /* Handle Resume Interrupt */
lypinator 0:bb348c97df44 489 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
lypinator 0:bb348c97df44 490 {
lypinator 0:bb348c97df44 491 /* Clear the Remote Wake-up Signaling */
lypinator 0:bb348c97df44 492 USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
lypinator 0:bb348c97df44 493
lypinator 0:bb348c97df44 494 #ifdef USB_OTG_GLPMCFG_LPMEN
lypinator 0:bb348c97df44 495 if(hpcd->LPM_State == LPM_L1)
lypinator 0:bb348c97df44 496 {
lypinator 0:bb348c97df44 497 hpcd->LPM_State = LPM_L0;
lypinator 0:bb348c97df44 498 HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
lypinator 0:bb348c97df44 499 }
lypinator 0:bb348c97df44 500 else
lypinator 0:bb348c97df44 501 #endif /* USB_OTG_GLPMCFG_LPMEN */
lypinator 0:bb348c97df44 502 {
lypinator 0:bb348c97df44 503 HAL_PCD_ResumeCallback(hpcd);
lypinator 0:bb348c97df44 504 }
lypinator 0:bb348c97df44 505
lypinator 0:bb348c97df44 506 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
lypinator 0:bb348c97df44 507 }
lypinator 0:bb348c97df44 508
lypinator 0:bb348c97df44 509 /* Handle Suspend Interrupt */
lypinator 0:bb348c97df44 510 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
lypinator 0:bb348c97df44 511 {
lypinator 0:bb348c97df44 512 if((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
lypinator 0:bb348c97df44 513 {
lypinator 0:bb348c97df44 514
lypinator 0:bb348c97df44 515 HAL_PCD_SuspendCallback(hpcd);
lypinator 0:bb348c97df44 516 }
lypinator 0:bb348c97df44 517 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
lypinator 0:bb348c97df44 518 }
lypinator 0:bb348c97df44 519
lypinator 0:bb348c97df44 520 #ifdef USB_OTG_GLPMCFG_LPMEN
lypinator 0:bb348c97df44 521 /* Handle LPM Interrupt */
lypinator 0:bb348c97df44 522 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT))
lypinator 0:bb348c97df44 523 {
lypinator 0:bb348c97df44 524 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
lypinator 0:bb348c97df44 525 if( hpcd->LPM_State == LPM_L0)
lypinator 0:bb348c97df44 526 {
lypinator 0:bb348c97df44 527 hpcd->LPM_State = LPM_L1;
lypinator 0:bb348c97df44 528 hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >> 2U;
lypinator 0:bb348c97df44 529 HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
lypinator 0:bb348c97df44 530 }
lypinator 0:bb348c97df44 531 else
lypinator 0:bb348c97df44 532 {
lypinator 0:bb348c97df44 533 HAL_PCD_SuspendCallback(hpcd);
lypinator 0:bb348c97df44 534 }
lypinator 0:bb348c97df44 535 }
lypinator 0:bb348c97df44 536 #endif /* USB_OTG_GLPMCFG_LPMEN */
lypinator 0:bb348c97df44 537
lypinator 0:bb348c97df44 538 /* Handle Reset Interrupt */
lypinator 0:bb348c97df44 539 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
lypinator 0:bb348c97df44 540 {
lypinator 0:bb348c97df44 541 USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
lypinator 0:bb348c97df44 542 USB_FlushTxFifo(hpcd->Instance , 0x10U);
lypinator 0:bb348c97df44 543
lypinator 0:bb348c97df44 544 for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
lypinator 0:bb348c97df44 545 {
lypinator 0:bb348c97df44 546 USBx_INEP(i)->DIEPINT = 0xFFU;
lypinator 0:bb348c97df44 547 USBx_OUTEP(i)->DOEPINT = 0xFFU;
lypinator 0:bb348c97df44 548 }
lypinator 0:bb348c97df44 549 USBx_DEVICE->DAINT = 0xFFFFFFFFU;
lypinator 0:bb348c97df44 550 USBx_DEVICE->DAINTMSK |= 0x10001U;
lypinator 0:bb348c97df44 551
lypinator 0:bb348c97df44 552 if(hpcd->Init.use_dedicated_ep1)
lypinator 0:bb348c97df44 553 {
lypinator 0:bb348c97df44 554 USBx_DEVICE->DOUTEP1MSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
lypinator 0:bb348c97df44 555 USBx_DEVICE->DINEP1MSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
lypinator 0:bb348c97df44 556 }
lypinator 0:bb348c97df44 557 else
lypinator 0:bb348c97df44 558 {
lypinator 0:bb348c97df44 559 #ifdef USB_OTG_DOEPINT_OTEPSPR
lypinator 0:bb348c97df44 560 USBx_DEVICE->DOEPMSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM | USB_OTG_DOEPMSK_OTEPSPRM);
lypinator 0:bb348c97df44 561 #else
lypinator 0:bb348c97df44 562 USBx_DEVICE->DOEPMSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
lypinator 0:bb348c97df44 563 #endif /* USB_OTG_DOEPINT_OTEPSPR */
lypinator 0:bb348c97df44 564 USBx_DEVICE->DIEPMSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
lypinator 0:bb348c97df44 565 }
lypinator 0:bb348c97df44 566
lypinator 0:bb348c97df44 567 /* Set Default Address to 0 */
lypinator 0:bb348c97df44 568 USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
lypinator 0:bb348c97df44 569
lypinator 0:bb348c97df44 570 /* setup EP0 to receive SETUP packets */
lypinator 0:bb348c97df44 571 USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
lypinator 0:bb348c97df44 572
lypinator 0:bb348c97df44 573 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
lypinator 0:bb348c97df44 574 }
lypinator 0:bb348c97df44 575
lypinator 0:bb348c97df44 576 /* Handle Enumeration done Interrupt */
lypinator 0:bb348c97df44 577 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
lypinator 0:bb348c97df44 578 {
lypinator 0:bb348c97df44 579 USB_ActivateSetup(hpcd->Instance);
lypinator 0:bb348c97df44 580 hpcd->Instance->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
lypinator 0:bb348c97df44 581
lypinator 0:bb348c97df44 582 if ( USB_GetDevSpeed(hpcd->Instance) == USB_OTG_SPEED_HIGH)
lypinator 0:bb348c97df44 583 {
lypinator 0:bb348c97df44 584 hpcd->Init.speed = USB_OTG_SPEED_HIGH;
lypinator 0:bb348c97df44 585 hpcd->Init.ep0_mps = USB_OTG_HS_MAX_PACKET_SIZE ;
lypinator 0:bb348c97df44 586 hpcd->Instance->GUSBCFG |= (uint32_t)((USBD_HS_TRDT_VALUE << 10U) & USB_OTG_GUSBCFG_TRDT);
lypinator 0:bb348c97df44 587 }
lypinator 0:bb348c97df44 588 else
lypinator 0:bb348c97df44 589 {
lypinator 0:bb348c97df44 590 hpcd->Init.speed = USB_OTG_SPEED_FULL;
lypinator 0:bb348c97df44 591 hpcd->Init.ep0_mps = USB_OTG_FS_MAX_PACKET_SIZE ;
lypinator 0:bb348c97df44 592
lypinator 0:bb348c97df44 593 /* The USBTRD is configured according to the tables below, depending on AHB frequency
lypinator 0:bb348c97df44 594 used by application. In the low AHB frequency range it is used to stretch enough the USB response
lypinator 0:bb348c97df44 595 time to IN tokens, the USB turnaround time, so to compensate for the longer AHB read access
lypinator 0:bb348c97df44 596 latency to the Data FIFO */
lypinator 0:bb348c97df44 597
lypinator 0:bb348c97df44 598 /* Get hclk frequency value */
lypinator 0:bb348c97df44 599 hclk = HAL_RCC_GetHCLKFreq();
lypinator 0:bb348c97df44 600
lypinator 0:bb348c97df44 601 if((hclk >= 14200000U)&&(hclk < 15000000U))
lypinator 0:bb348c97df44 602 {
lypinator 0:bb348c97df44 603 /* hclk Clock Range between 14.2-15 MHz */
lypinator 0:bb348c97df44 604 hpcd->Instance->GUSBCFG |= (uint32_t)((0xFU << 10U) & USB_OTG_GUSBCFG_TRDT);
lypinator 0:bb348c97df44 605 }
lypinator 0:bb348c97df44 606
lypinator 0:bb348c97df44 607 else if((hclk >= 15000000U)&&(hclk < 16000000U))
lypinator 0:bb348c97df44 608 {
lypinator 0:bb348c97df44 609 /* hclk Clock Range between 15-16 MHz */
lypinator 0:bb348c97df44 610 hpcd->Instance->GUSBCFG |= (uint32_t)((0xEU << 10U) & USB_OTG_GUSBCFG_TRDT);
lypinator 0:bb348c97df44 611 }
lypinator 0:bb348c97df44 612
lypinator 0:bb348c97df44 613 else if((hclk >= 16000000U)&&(hclk < 17200000U))
lypinator 0:bb348c97df44 614 {
lypinator 0:bb348c97df44 615 /* hclk Clock Range between 16-17.2 MHz */
lypinator 0:bb348c97df44 616 hpcd->Instance->GUSBCFG |= (uint32_t)((0xDU << 10U) & USB_OTG_GUSBCFG_TRDT);
lypinator 0:bb348c97df44 617 }
lypinator 0:bb348c97df44 618
lypinator 0:bb348c97df44 619 else if((hclk >= 17200000U)&&(hclk < 18500000U))
lypinator 0:bb348c97df44 620 {
lypinator 0:bb348c97df44 621 /* hclk Clock Range between 17.2-18.5 MHz */
lypinator 0:bb348c97df44 622 hpcd->Instance->GUSBCFG |= (uint32_t)((0xCU << 10U) & USB_OTG_GUSBCFG_TRDT);
lypinator 0:bb348c97df44 623 }
lypinator 0:bb348c97df44 624
lypinator 0:bb348c97df44 625 else if((hclk >= 18500000U)&&(hclk < 20000000U))
lypinator 0:bb348c97df44 626 {
lypinator 0:bb348c97df44 627 /* hclk Clock Range between 18.5-20 MHz */
lypinator 0:bb348c97df44 628 hpcd->Instance->GUSBCFG |= (uint32_t)((0xBU << 10U) & USB_OTG_GUSBCFG_TRDT);
lypinator 0:bb348c97df44 629 }
lypinator 0:bb348c97df44 630
lypinator 0:bb348c97df44 631 else if((hclk >= 20000000U)&&(hclk < 21800000U))
lypinator 0:bb348c97df44 632 {
lypinator 0:bb348c97df44 633 /* hclk Clock Range between 20-21.8 MHz */
lypinator 0:bb348c97df44 634 hpcd->Instance->GUSBCFG |= (uint32_t)((0xAU << 10U) & USB_OTG_GUSBCFG_TRDT);
lypinator 0:bb348c97df44 635 }
lypinator 0:bb348c97df44 636
lypinator 0:bb348c97df44 637 else if((hclk >= 21800000U)&&(hclk < 24000000U))
lypinator 0:bb348c97df44 638 {
lypinator 0:bb348c97df44 639 /* hclk Clock Range between 21.8-24 MHz */
lypinator 0:bb348c97df44 640 hpcd->Instance->GUSBCFG |= (uint32_t)((0x9U << 10U) & USB_OTG_GUSBCFG_TRDT);
lypinator 0:bb348c97df44 641 }
lypinator 0:bb348c97df44 642
lypinator 0:bb348c97df44 643 else if((hclk >= 24000000U)&&(hclk < 27700000U))
lypinator 0:bb348c97df44 644 {
lypinator 0:bb348c97df44 645 /* hclk Clock Range between 24-27.7 MHz */
lypinator 0:bb348c97df44 646 hpcd->Instance->GUSBCFG |= (uint32_t)((0x8U << 10U) & USB_OTG_GUSBCFG_TRDT);
lypinator 0:bb348c97df44 647 }
lypinator 0:bb348c97df44 648
lypinator 0:bb348c97df44 649 else if((hclk >= 27700000U)&&(hclk < 32000000U))
lypinator 0:bb348c97df44 650 {
lypinator 0:bb348c97df44 651 /* hclk Clock Range between 27.7-32 MHz */
lypinator 0:bb348c97df44 652 hpcd->Instance->GUSBCFG |= (uint32_t)((0x7U << 10U) & USB_OTG_GUSBCFG_TRDT);
lypinator 0:bb348c97df44 653 }
lypinator 0:bb348c97df44 654
lypinator 0:bb348c97df44 655 else /* if(hclk >= 32000000) */
lypinator 0:bb348c97df44 656 {
lypinator 0:bb348c97df44 657 /* hclk Clock Range between 32-180 MHz */
lypinator 0:bb348c97df44 658 hpcd->Instance->GUSBCFG |= (uint32_t)((0x6U << 10U) & USB_OTG_GUSBCFG_TRDT);
lypinator 0:bb348c97df44 659 }
lypinator 0:bb348c97df44 660 }
lypinator 0:bb348c97df44 661
lypinator 0:bb348c97df44 662 HAL_PCD_ResetCallback(hpcd);
lypinator 0:bb348c97df44 663
lypinator 0:bb348c97df44 664 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
lypinator 0:bb348c97df44 665 }
lypinator 0:bb348c97df44 666
lypinator 0:bb348c97df44 667 /* Handle RxQLevel Interrupt */
lypinator 0:bb348c97df44 668 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL))
lypinator 0:bb348c97df44 669 {
lypinator 0:bb348c97df44 670 USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
lypinator 0:bb348c97df44 671
lypinator 0:bb348c97df44 672 temp = USBx->GRXSTSP;
lypinator 0:bb348c97df44 673
lypinator 0:bb348c97df44 674 ep = &hpcd->OUT_ep[temp & USB_OTG_GRXSTSP_EPNUM];
lypinator 0:bb348c97df44 675
lypinator 0:bb348c97df44 676 if(((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17U) == STS_DATA_UPDT)
lypinator 0:bb348c97df44 677 {
lypinator 0:bb348c97df44 678 if((temp & USB_OTG_GRXSTSP_BCNT) != 0U)
lypinator 0:bb348c97df44 679 {
lypinator 0:bb348c97df44 680 USB_ReadPacket(USBx, ep->xfer_buff, (temp & USB_OTG_GRXSTSP_BCNT) >> 4U);
lypinator 0:bb348c97df44 681 ep->xfer_buff += (temp & USB_OTG_GRXSTSP_BCNT) >> 4U;
lypinator 0:bb348c97df44 682 ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4U;
lypinator 0:bb348c97df44 683 }
lypinator 0:bb348c97df44 684 }
lypinator 0:bb348c97df44 685 else if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17U) == STS_SETUP_UPDT)
lypinator 0:bb348c97df44 686 {
lypinator 0:bb348c97df44 687 USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8U);
lypinator 0:bb348c97df44 688 ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4U;
lypinator 0:bb348c97df44 689 }
lypinator 0:bb348c97df44 690 USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
lypinator 0:bb348c97df44 691 }
lypinator 0:bb348c97df44 692
lypinator 0:bb348c97df44 693 /* Handle SOF Interrupt */
lypinator 0:bb348c97df44 694 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF))
lypinator 0:bb348c97df44 695 {
lypinator 0:bb348c97df44 696 HAL_PCD_SOFCallback(hpcd);
lypinator 0:bb348c97df44 697 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
lypinator 0:bb348c97df44 698 }
lypinator 0:bb348c97df44 699
lypinator 0:bb348c97df44 700 /* Handle Incomplete ISO IN Interrupt */
lypinator 0:bb348c97df44 701 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR))
lypinator 0:bb348c97df44 702 {
lypinator 0:bb348c97df44 703 HAL_PCD_ISOINIncompleteCallback(hpcd, epnum);
lypinator 0:bb348c97df44 704 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
lypinator 0:bb348c97df44 705 }
lypinator 0:bb348c97df44 706
lypinator 0:bb348c97df44 707 /* Handle Incomplete ISO OUT Interrupt */
lypinator 0:bb348c97df44 708 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
lypinator 0:bb348c97df44 709 {
lypinator 0:bb348c97df44 710 HAL_PCD_ISOOUTIncompleteCallback(hpcd, epnum);
lypinator 0:bb348c97df44 711 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
lypinator 0:bb348c97df44 712 }
lypinator 0:bb348c97df44 713
lypinator 0:bb348c97df44 714 /* Handle Connection event Interrupt */
lypinator 0:bb348c97df44 715 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT))
lypinator 0:bb348c97df44 716 {
lypinator 0:bb348c97df44 717 HAL_PCD_ConnectCallback(hpcd);
lypinator 0:bb348c97df44 718 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
lypinator 0:bb348c97df44 719 }
lypinator 0:bb348c97df44 720
lypinator 0:bb348c97df44 721 /* Handle Disconnection event Interrupt */
lypinator 0:bb348c97df44 722 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT))
lypinator 0:bb348c97df44 723 {
lypinator 0:bb348c97df44 724 temp = hpcd->Instance->GOTGINT;
lypinator 0:bb348c97df44 725
lypinator 0:bb348c97df44 726 if((temp & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET)
lypinator 0:bb348c97df44 727 {
lypinator 0:bb348c97df44 728 HAL_PCD_DisconnectCallback(hpcd);
lypinator 0:bb348c97df44 729 }
lypinator 0:bb348c97df44 730 hpcd->Instance->GOTGINT |= temp;
lypinator 0:bb348c97df44 731 }
lypinator 0:bb348c97df44 732 }
lypinator 0:bb348c97df44 733 }
lypinator 0:bb348c97df44 734
lypinator 0:bb348c97df44 735 /**
lypinator 0:bb348c97df44 736 * @brief Data OUT stage callback.
lypinator 0:bb348c97df44 737 * @param hpcd PCD handle
lypinator 0:bb348c97df44 738 * @param epnum endpoint number
lypinator 0:bb348c97df44 739 * @retval None
lypinator 0:bb348c97df44 740 */
lypinator 0:bb348c97df44 741 __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
lypinator 0:bb348c97df44 742 {
lypinator 0:bb348c97df44 743 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 744 UNUSED(hpcd);
lypinator 0:bb348c97df44 745 UNUSED(epnum);
lypinator 0:bb348c97df44 746 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 747 the HAL_PCD_DataOutStageCallback could be implemented in the user file
lypinator 0:bb348c97df44 748 */
lypinator 0:bb348c97df44 749 }
lypinator 0:bb348c97df44 750
lypinator 0:bb348c97df44 751 /**
lypinator 0:bb348c97df44 752 * @brief Data IN stage callback.
lypinator 0:bb348c97df44 753 * @param hpcd PCD handle
lypinator 0:bb348c97df44 754 * @param epnum endpoint number
lypinator 0:bb348c97df44 755 * @retval None
lypinator 0:bb348c97df44 756 */
lypinator 0:bb348c97df44 757 __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
lypinator 0:bb348c97df44 758 {
lypinator 0:bb348c97df44 759 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 760 UNUSED(hpcd);
lypinator 0:bb348c97df44 761 UNUSED(epnum);
lypinator 0:bb348c97df44 762 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 763 the HAL_PCD_DataInStageCallback could be implemented in the user file
lypinator 0:bb348c97df44 764 */
lypinator 0:bb348c97df44 765 }
lypinator 0:bb348c97df44 766 /**
lypinator 0:bb348c97df44 767 * @brief Setup stage callback.
lypinator 0:bb348c97df44 768 * @param hpcd PCD handle
lypinator 0:bb348c97df44 769 * @retval None
lypinator 0:bb348c97df44 770 */
lypinator 0:bb348c97df44 771 __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 772 {
lypinator 0:bb348c97df44 773 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 774 UNUSED(hpcd);
lypinator 0:bb348c97df44 775 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 776 the HAL_PCD_SetupStageCallback could be implemented in the user file
lypinator 0:bb348c97df44 777 */
lypinator 0:bb348c97df44 778 }
lypinator 0:bb348c97df44 779
lypinator 0:bb348c97df44 780 /**
lypinator 0:bb348c97df44 781 * @brief USB Start Of Frame callback.
lypinator 0:bb348c97df44 782 * @param hpcd PCD handle
lypinator 0:bb348c97df44 783 * @retval None
lypinator 0:bb348c97df44 784 */
lypinator 0:bb348c97df44 785 __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 786 {
lypinator 0:bb348c97df44 787 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 788 UNUSED(hpcd);
lypinator 0:bb348c97df44 789 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 790 the HAL_PCD_SOFCallback could be implemented in the user file
lypinator 0:bb348c97df44 791 */
lypinator 0:bb348c97df44 792 }
lypinator 0:bb348c97df44 793
lypinator 0:bb348c97df44 794 /**
lypinator 0:bb348c97df44 795 * @brief USB Reset callback.
lypinator 0:bb348c97df44 796 * @param hpcd PCD handle
lypinator 0:bb348c97df44 797 * @retval None
lypinator 0:bb348c97df44 798 */
lypinator 0:bb348c97df44 799 __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 800 {
lypinator 0:bb348c97df44 801 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 802 UNUSED(hpcd);
lypinator 0:bb348c97df44 803 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 804 the HAL_PCD_ResetCallback could be implemented in the user file
lypinator 0:bb348c97df44 805 */
lypinator 0:bb348c97df44 806 }
lypinator 0:bb348c97df44 807
lypinator 0:bb348c97df44 808 /**
lypinator 0:bb348c97df44 809 * @brief Suspend event callback.
lypinator 0:bb348c97df44 810 * @param hpcd PCD handle
lypinator 0:bb348c97df44 811 * @retval None
lypinator 0:bb348c97df44 812 */
lypinator 0:bb348c97df44 813 __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 814 {
lypinator 0:bb348c97df44 815 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 816 UNUSED(hpcd);
lypinator 0:bb348c97df44 817 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 818 the HAL_PCD_SuspendCallback could be implemented in the user file
lypinator 0:bb348c97df44 819 */
lypinator 0:bb348c97df44 820 }
lypinator 0:bb348c97df44 821
lypinator 0:bb348c97df44 822 /**
lypinator 0:bb348c97df44 823 * @brief Resume event callback.
lypinator 0:bb348c97df44 824 * @param hpcd PCD handle
lypinator 0:bb348c97df44 825 * @retval None
lypinator 0:bb348c97df44 826 */
lypinator 0:bb348c97df44 827 __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 828 {
lypinator 0:bb348c97df44 829 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 830 UNUSED(hpcd);
lypinator 0:bb348c97df44 831 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 832 the HAL_PCD_ResumeCallback could be implemented in the user file
lypinator 0:bb348c97df44 833 */
lypinator 0:bb348c97df44 834 }
lypinator 0:bb348c97df44 835
lypinator 0:bb348c97df44 836 /**
lypinator 0:bb348c97df44 837 * @brief Incomplete ISO OUT callback.
lypinator 0:bb348c97df44 838 * @param hpcd PCD handle
lypinator 0:bb348c97df44 839 * @param epnum endpoint number
lypinator 0:bb348c97df44 840 * @retval None
lypinator 0:bb348c97df44 841 */
lypinator 0:bb348c97df44 842 __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
lypinator 0:bb348c97df44 843 {
lypinator 0:bb348c97df44 844 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 845 UNUSED(hpcd);
lypinator 0:bb348c97df44 846 UNUSED(epnum);
lypinator 0:bb348c97df44 847 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 848 the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
lypinator 0:bb348c97df44 849 */
lypinator 0:bb348c97df44 850 }
lypinator 0:bb348c97df44 851
lypinator 0:bb348c97df44 852 /**
lypinator 0:bb348c97df44 853 * @brief Incomplete ISO IN callback.
lypinator 0:bb348c97df44 854 * @param hpcd PCD handle
lypinator 0:bb348c97df44 855 * @param epnum endpoint number
lypinator 0:bb348c97df44 856 * @retval None
lypinator 0:bb348c97df44 857 */
lypinator 0:bb348c97df44 858 __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
lypinator 0:bb348c97df44 859 {
lypinator 0:bb348c97df44 860 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 861 UNUSED(hpcd);
lypinator 0:bb348c97df44 862 UNUSED(epnum);
lypinator 0:bb348c97df44 863 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 864 the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
lypinator 0:bb348c97df44 865 */
lypinator 0:bb348c97df44 866 }
lypinator 0:bb348c97df44 867
lypinator 0:bb348c97df44 868 /**
lypinator 0:bb348c97df44 869 * @brief Connection event callback.
lypinator 0:bb348c97df44 870 * @param hpcd PCD handle
lypinator 0:bb348c97df44 871 * @retval None
lypinator 0:bb348c97df44 872 */
lypinator 0:bb348c97df44 873 __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 874 {
lypinator 0:bb348c97df44 875 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 876 UNUSED(hpcd);
lypinator 0:bb348c97df44 877 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 878 the HAL_PCD_ConnectCallback could be implemented in the user file
lypinator 0:bb348c97df44 879 */
lypinator 0:bb348c97df44 880 }
lypinator 0:bb348c97df44 881
lypinator 0:bb348c97df44 882 /**
lypinator 0:bb348c97df44 883 * @brief Disconnection event callback.
lypinator 0:bb348c97df44 884 * @param hpcd PCD handle
lypinator 0:bb348c97df44 885 * @retval None
lypinator 0:bb348c97df44 886 */
lypinator 0:bb348c97df44 887 __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 888 {
lypinator 0:bb348c97df44 889 /* Prevent unused argument(s) compilation warning */
lypinator 0:bb348c97df44 890 UNUSED(hpcd);
lypinator 0:bb348c97df44 891 /* NOTE : This function Should not be modified, when the callback is needed,
lypinator 0:bb348c97df44 892 the HAL_PCD_DisconnectCallback could be implemented in the user file
lypinator 0:bb348c97df44 893 */
lypinator 0:bb348c97df44 894 }
lypinator 0:bb348c97df44 895
lypinator 0:bb348c97df44 896 /**
lypinator 0:bb348c97df44 897 * @}
lypinator 0:bb348c97df44 898 */
lypinator 0:bb348c97df44 899
lypinator 0:bb348c97df44 900 /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
lypinator 0:bb348c97df44 901 * @brief management functions
lypinator 0:bb348c97df44 902 *
lypinator 0:bb348c97df44 903 @verbatim
lypinator 0:bb348c97df44 904 ===============================================================================
lypinator 0:bb348c97df44 905 ##### Peripheral Control functions #####
lypinator 0:bb348c97df44 906 ===============================================================================
lypinator 0:bb348c97df44 907 [..]
lypinator 0:bb348c97df44 908 This subsection provides a set of functions allowing to control the PCD data
lypinator 0:bb348c97df44 909 transfers.
lypinator 0:bb348c97df44 910
lypinator 0:bb348c97df44 911 @endverbatim
lypinator 0:bb348c97df44 912 * @{
lypinator 0:bb348c97df44 913 */
lypinator 0:bb348c97df44 914
lypinator 0:bb348c97df44 915 /**
lypinator 0:bb348c97df44 916 * @brief Connect the USB device.
lypinator 0:bb348c97df44 917 * @param hpcd PCD handle
lypinator 0:bb348c97df44 918 * @retval HAL status
lypinator 0:bb348c97df44 919 */
lypinator 0:bb348c97df44 920 HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 921 {
lypinator 0:bb348c97df44 922 __HAL_LOCK(hpcd);
lypinator 0:bb348c97df44 923 USB_DevConnect(hpcd->Instance);
lypinator 0:bb348c97df44 924 __HAL_UNLOCK(hpcd);
lypinator 0:bb348c97df44 925 return HAL_OK;
lypinator 0:bb348c97df44 926 }
lypinator 0:bb348c97df44 927
lypinator 0:bb348c97df44 928 /**
lypinator 0:bb348c97df44 929 * @brief Disconnect the USB device.
lypinator 0:bb348c97df44 930 * @param hpcd PCD handle
lypinator 0:bb348c97df44 931 * @retval HAL status
lypinator 0:bb348c97df44 932 */
lypinator 0:bb348c97df44 933 HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 934 {
lypinator 0:bb348c97df44 935 __HAL_LOCK(hpcd);
lypinator 0:bb348c97df44 936 USB_DevDisconnect(hpcd->Instance);
lypinator 0:bb348c97df44 937 __HAL_UNLOCK(hpcd);
lypinator 0:bb348c97df44 938 return HAL_OK;
lypinator 0:bb348c97df44 939 }
lypinator 0:bb348c97df44 940
lypinator 0:bb348c97df44 941 /**
lypinator 0:bb348c97df44 942 * @brief Set the USB Device address.
lypinator 0:bb348c97df44 943 * @param hpcd PCD handle
lypinator 0:bb348c97df44 944 * @param address new device address
lypinator 0:bb348c97df44 945 * @retval HAL status
lypinator 0:bb348c97df44 946 */
lypinator 0:bb348c97df44 947 HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
lypinator 0:bb348c97df44 948 {
lypinator 0:bb348c97df44 949 __HAL_LOCK(hpcd);
lypinator 0:bb348c97df44 950 USB_SetDevAddress(hpcd->Instance, address);
lypinator 0:bb348c97df44 951 __HAL_UNLOCK(hpcd);
lypinator 0:bb348c97df44 952 return HAL_OK;
lypinator 0:bb348c97df44 953 }
lypinator 0:bb348c97df44 954 /**
lypinator 0:bb348c97df44 955 * @brief Open and configure an endpoint.
lypinator 0:bb348c97df44 956 * @param hpcd PCD handle
lypinator 0:bb348c97df44 957 * @param ep_addr endpoint address
lypinator 0:bb348c97df44 958 * @param ep_mps endpoint max packet size
lypinator 0:bb348c97df44 959 * @param ep_type endpoint type
lypinator 0:bb348c97df44 960 * @retval HAL status
lypinator 0:bb348c97df44 961 */
lypinator 0:bb348c97df44 962 HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
lypinator 0:bb348c97df44 963 {
lypinator 0:bb348c97df44 964 HAL_StatusTypeDef ret = HAL_OK;
lypinator 0:bb348c97df44 965 USB_OTG_EPTypeDef *ep;
lypinator 0:bb348c97df44 966
lypinator 0:bb348c97df44 967 if ((ep_addr & 0x80) == 0x80)
lypinator 0:bb348c97df44 968 {
lypinator 0:bb348c97df44 969 ep = &hpcd->IN_ep[ep_addr & 0x7F];
lypinator 0:bb348c97df44 970 }
lypinator 0:bb348c97df44 971 else
lypinator 0:bb348c97df44 972 {
lypinator 0:bb348c97df44 973 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
lypinator 0:bb348c97df44 974 }
lypinator 0:bb348c97df44 975 ep->num = ep_addr & 0x7F;
lypinator 0:bb348c97df44 976
lypinator 0:bb348c97df44 977 ep->is_in = (0x80 & ep_addr) != 0;
lypinator 0:bb348c97df44 978 ep->maxpacket = ep_mps;
lypinator 0:bb348c97df44 979 ep->type = ep_type;
lypinator 0:bb348c97df44 980 if (ep->is_in)
lypinator 0:bb348c97df44 981 {
lypinator 0:bb348c97df44 982 /* Assign a Tx FIFO */
lypinator 0:bb348c97df44 983 ep->tx_fifo_num = ep->num;
lypinator 0:bb348c97df44 984 }
lypinator 0:bb348c97df44 985 /* Set initial data PID. */
lypinator 0:bb348c97df44 986 if (ep_type == EP_TYPE_BULK )
lypinator 0:bb348c97df44 987 {
lypinator 0:bb348c97df44 988 ep->data_pid_start = 0U;
lypinator 0:bb348c97df44 989 }
lypinator 0:bb348c97df44 990
lypinator 0:bb348c97df44 991 /* MBED */
lypinator 0:bb348c97df44 992 __HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
lypinator 0:bb348c97df44 993 USB_ActivateEndpoint(hpcd->Instance , ep);
lypinator 0:bb348c97df44 994 __HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
lypinator 0:bb348c97df44 995 /* MBED */
lypinator 0:bb348c97df44 996 return ret;
lypinator 0:bb348c97df44 997 }
lypinator 0:bb348c97df44 998
lypinator 0:bb348c97df44 999
lypinator 0:bb348c97df44 1000 /**
lypinator 0:bb348c97df44 1001 * @brief Deactivate an endpoint.
lypinator 0:bb348c97df44 1002 * @param hpcd PCD handle
lypinator 0:bb348c97df44 1003 * @param ep_addr endpoint address
lypinator 0:bb348c97df44 1004 * @retval HAL status
lypinator 0:bb348c97df44 1005 */
lypinator 0:bb348c97df44 1006 HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
lypinator 0:bb348c97df44 1007 {
lypinator 0:bb348c97df44 1008 USB_OTG_EPTypeDef *ep;
lypinator 0:bb348c97df44 1009
lypinator 0:bb348c97df44 1010 if ((ep_addr & 0x80) == 0x80)
lypinator 0:bb348c97df44 1011 {
lypinator 0:bb348c97df44 1012 ep = &hpcd->IN_ep[ep_addr & 0x7F];
lypinator 0:bb348c97df44 1013 }
lypinator 0:bb348c97df44 1014 else
lypinator 0:bb348c97df44 1015 {
lypinator 0:bb348c97df44 1016 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
lypinator 0:bb348c97df44 1017 }
lypinator 0:bb348c97df44 1018 ep->num = ep_addr & 0x7F;
lypinator 0:bb348c97df44 1019
lypinator 0:bb348c97df44 1020 ep->is_in = (0x80 & ep_addr) != 0;
lypinator 0:bb348c97df44 1021
lypinator 0:bb348c97df44 1022 /* MBED */
lypinator 0:bb348c97df44 1023 __HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
lypinator 0:bb348c97df44 1024 USB_DeactivateEndpoint(hpcd->Instance , ep);
lypinator 0:bb348c97df44 1025 __HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
lypinator 0:bb348c97df44 1026 /* MBED */
lypinator 0:bb348c97df44 1027 return HAL_OK;
lypinator 0:bb348c97df44 1028 }
lypinator 0:bb348c97df44 1029
lypinator 0:bb348c97df44 1030
lypinator 0:bb348c97df44 1031 /**
lypinator 0:bb348c97df44 1032 * @brief Receive an amount of data.
lypinator 0:bb348c97df44 1033 * @param hpcd PCD handle
lypinator 0:bb348c97df44 1034 * @param ep_addr endpoint address
lypinator 0:bb348c97df44 1035 * @param pBuf pointer to the reception buffer
lypinator 0:bb348c97df44 1036 * @param len amount of data to be received
lypinator 0:bb348c97df44 1037 * @retval HAL status
lypinator 0:bb348c97df44 1038 */
lypinator 0:bb348c97df44 1039 HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
lypinator 0:bb348c97df44 1040 {
lypinator 0:bb348c97df44 1041 USB_OTG_EPTypeDef *ep;
lypinator 0:bb348c97df44 1042
lypinator 0:bb348c97df44 1043 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
lypinator 0:bb348c97df44 1044
lypinator 0:bb348c97df44 1045 /*setup and start the Xfer */
lypinator 0:bb348c97df44 1046 ep->xfer_buff = pBuf;
lypinator 0:bb348c97df44 1047 ep->xfer_len = len;
lypinator 0:bb348c97df44 1048 ep->xfer_count = 0U;
lypinator 0:bb348c97df44 1049 ep->is_in = 0U;
lypinator 0:bb348c97df44 1050 ep->num = ep_addr & 0x7F;
lypinator 0:bb348c97df44 1051
lypinator 0:bb348c97df44 1052 if (hpcd->Init.dma_enable == 1U)
lypinator 0:bb348c97df44 1053 {
lypinator 0:bb348c97df44 1054 ep->dma_addr = (uint32_t)pBuf;
lypinator 0:bb348c97df44 1055 }
lypinator 0:bb348c97df44 1056
lypinator 0:bb348c97df44 1057 /* MBED */
lypinator 0:bb348c97df44 1058 __HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
lypinator 0:bb348c97df44 1059 /* MBED */
lypinator 0:bb348c97df44 1060
lypinator 0:bb348c97df44 1061 if ((ep_addr & 0x7F) == 0)
lypinator 0:bb348c97df44 1062 {
lypinator 0:bb348c97df44 1063 USB_EP0StartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
lypinator 0:bb348c97df44 1064 }
lypinator 0:bb348c97df44 1065 else
lypinator 0:bb348c97df44 1066 {
lypinator 0:bb348c97df44 1067 USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
lypinator 0:bb348c97df44 1068 }
lypinator 0:bb348c97df44 1069 /* MBED */
lypinator 0:bb348c97df44 1070 __HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
lypinator 0:bb348c97df44 1071 /* MBED */
lypinator 0:bb348c97df44 1072
lypinator 0:bb348c97df44 1073 return HAL_OK;
lypinator 0:bb348c97df44 1074 }
lypinator 0:bb348c97df44 1075
lypinator 0:bb348c97df44 1076 /**
lypinator 0:bb348c97df44 1077 * @brief Get Received Data Size.
lypinator 0:bb348c97df44 1078 * @param hpcd PCD handle
lypinator 0:bb348c97df44 1079 * @param ep_addr endpoint address
lypinator 0:bb348c97df44 1080 * @retval Data Size
lypinator 0:bb348c97df44 1081 */
lypinator 0:bb348c97df44 1082 uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
lypinator 0:bb348c97df44 1083 {
lypinator 0:bb348c97df44 1084 /* MBED */
lypinator 0:bb348c97df44 1085 return hpcd->OUT_ep[ep_addr & 0x7FU].xfer_count;
lypinator 0:bb348c97df44 1086 /* MBED */
lypinator 0:bb348c97df44 1087 }
lypinator 0:bb348c97df44 1088 /**
lypinator 0:bb348c97df44 1089 * @brief Send an amount of data.
lypinator 0:bb348c97df44 1090 * @param hpcd PCD handle
lypinator 0:bb348c97df44 1091 * @param ep_addr endpoint address
lypinator 0:bb348c97df44 1092 * @param pBuf pointer to the transmission buffer
lypinator 0:bb348c97df44 1093 * @param len amount of data to be sent
lypinator 0:bb348c97df44 1094 * @retval HAL status
lypinator 0:bb348c97df44 1095 */
lypinator 0:bb348c97df44 1096 HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
lypinator 0:bb348c97df44 1097 {
lypinator 0:bb348c97df44 1098 USB_OTG_EPTypeDef *ep;
lypinator 0:bb348c97df44 1099
lypinator 0:bb348c97df44 1100 ep = &hpcd->IN_ep[ep_addr & 0x7F];
lypinator 0:bb348c97df44 1101
lypinator 0:bb348c97df44 1102 /*setup and start the Xfer */
lypinator 0:bb348c97df44 1103 ep->xfer_buff = pBuf;
lypinator 0:bb348c97df44 1104 ep->xfer_len = len;
lypinator 0:bb348c97df44 1105 ep->xfer_count = 0U;
lypinator 0:bb348c97df44 1106 ep->is_in = 1U;
lypinator 0:bb348c97df44 1107 ep->num = ep_addr & 0x7F;
lypinator 0:bb348c97df44 1108
lypinator 0:bb348c97df44 1109 if (hpcd->Init.dma_enable == 1U)
lypinator 0:bb348c97df44 1110 {
lypinator 0:bb348c97df44 1111 ep->dma_addr = (uint32_t)pBuf;
lypinator 0:bb348c97df44 1112 }
lypinator 0:bb348c97df44 1113
lypinator 0:bb348c97df44 1114 /* MBED */
lypinator 0:bb348c97df44 1115 __HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
lypinator 0:bb348c97df44 1116 /* MBED */
lypinator 0:bb348c97df44 1117
lypinator 0:bb348c97df44 1118 if ((ep_addr & 0x7F) == 0)
lypinator 0:bb348c97df44 1119 {
lypinator 0:bb348c97df44 1120 USB_EP0StartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
lypinator 0:bb348c97df44 1121 }
lypinator 0:bb348c97df44 1122 else
lypinator 0:bb348c97df44 1123 {
lypinator 0:bb348c97df44 1124 USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
lypinator 0:bb348c97df44 1125 }
lypinator 0:bb348c97df44 1126
lypinator 0:bb348c97df44 1127 /* MBED */
lypinator 0:bb348c97df44 1128 __HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
lypinator 0:bb348c97df44 1129 /* MBED */
lypinator 0:bb348c97df44 1130
lypinator 0:bb348c97df44 1131 return HAL_OK;
lypinator 0:bb348c97df44 1132 }
lypinator 0:bb348c97df44 1133
lypinator 0:bb348c97df44 1134 /**
lypinator 0:bb348c97df44 1135 * @brief Set a STALL condition over an endpoint.
lypinator 0:bb348c97df44 1136 * @param hpcd PCD handle
lypinator 0:bb348c97df44 1137 * @param ep_addr endpoint address
lypinator 0:bb348c97df44 1138 * @retval HAL status
lypinator 0:bb348c97df44 1139 */
lypinator 0:bb348c97df44 1140 HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
lypinator 0:bb348c97df44 1141 {
lypinator 0:bb348c97df44 1142 USB_OTG_EPTypeDef *ep;
lypinator 0:bb348c97df44 1143
lypinator 0:bb348c97df44 1144 if ((0x80 & ep_addr) == 0x80)
lypinator 0:bb348c97df44 1145 {
lypinator 0:bb348c97df44 1146 ep = &hpcd->IN_ep[ep_addr & 0x7F];
lypinator 0:bb348c97df44 1147 }
lypinator 0:bb348c97df44 1148 else
lypinator 0:bb348c97df44 1149 {
lypinator 0:bb348c97df44 1150 ep = &hpcd->OUT_ep[ep_addr];
lypinator 0:bb348c97df44 1151 }
lypinator 0:bb348c97df44 1152
lypinator 0:bb348c97df44 1153 ep->is_stall = 1U;
lypinator 0:bb348c97df44 1154 ep->num = ep_addr & 0x7F;
lypinator 0:bb348c97df44 1155 ep->is_in = ((ep_addr & 0x80) == 0x80);
lypinator 0:bb348c97df44 1156
lypinator 0:bb348c97df44 1157
lypinator 0:bb348c97df44 1158 /* MBED */
lypinator 0:bb348c97df44 1159 __HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
lypinator 0:bb348c97df44 1160 /* MBED */
lypinator 0:bb348c97df44 1161 USB_EPSetStall(hpcd->Instance , ep);
lypinator 0:bb348c97df44 1162 if((ep_addr & 0x7F) == 0)
lypinator 0:bb348c97df44 1163 {
lypinator 0:bb348c97df44 1164 USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
lypinator 0:bb348c97df44 1165 }
lypinator 0:bb348c97df44 1166 /* MBED */
lypinator 0:bb348c97df44 1167 __HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
lypinator 0:bb348c97df44 1168 /* MBED */
lypinator 0:bb348c97df44 1169
lypinator 0:bb348c97df44 1170 return HAL_OK;
lypinator 0:bb348c97df44 1171 }
lypinator 0:bb348c97df44 1172
lypinator 0:bb348c97df44 1173 /**
lypinator 0:bb348c97df44 1174 * @brief Clear a STALL condition over in an endpoint.
lypinator 0:bb348c97df44 1175 * @param hpcd PCD handle
lypinator 0:bb348c97df44 1176 * @param ep_addr endpoint address
lypinator 0:bb348c97df44 1177 * @retval HAL status
lypinator 0:bb348c97df44 1178 */
lypinator 0:bb348c97df44 1179 HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
lypinator 0:bb348c97df44 1180 {
lypinator 0:bb348c97df44 1181 USB_OTG_EPTypeDef *ep;
lypinator 0:bb348c97df44 1182
lypinator 0:bb348c97df44 1183 if ((0x80 & ep_addr) == 0x80)
lypinator 0:bb348c97df44 1184 {
lypinator 0:bb348c97df44 1185 ep = &hpcd->IN_ep[ep_addr & 0x7F];
lypinator 0:bb348c97df44 1186 }
lypinator 0:bb348c97df44 1187 else
lypinator 0:bb348c97df44 1188 {
lypinator 0:bb348c97df44 1189 ep = &hpcd->OUT_ep[ep_addr];
lypinator 0:bb348c97df44 1190 }
lypinator 0:bb348c97df44 1191
lypinator 0:bb348c97df44 1192 ep->is_stall = 0U;
lypinator 0:bb348c97df44 1193 ep->num = ep_addr & 0x7F;
lypinator 0:bb348c97df44 1194 ep->is_in = ((ep_addr & 0x80) == 0x80);
lypinator 0:bb348c97df44 1195
lypinator 0:bb348c97df44 1196 /* MBED */
lypinator 0:bb348c97df44 1197 __HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
lypinator 0:bb348c97df44 1198 USB_EPClearStall(hpcd->Instance , ep);
lypinator 0:bb348c97df44 1199 __HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
lypinator 0:bb348c97df44 1200 /* MBED */
lypinator 0:bb348c97df44 1201
lypinator 0:bb348c97df44 1202 return HAL_OK;
lypinator 0:bb348c97df44 1203 }
lypinator 0:bb348c97df44 1204
lypinator 0:bb348c97df44 1205 /**
lypinator 0:bb348c97df44 1206 * @brief Flush an endpoint.
lypinator 0:bb348c97df44 1207 * @param hpcd PCD handle
lypinator 0:bb348c97df44 1208 * @param ep_addr endpoint address
lypinator 0:bb348c97df44 1209 * @retval HAL status
lypinator 0:bb348c97df44 1210 */
lypinator 0:bb348c97df44 1211 HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
lypinator 0:bb348c97df44 1212 {
lypinator 0:bb348c97df44 1213 /* MBED */
lypinator 0:bb348c97df44 1214 __HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
lypinator 0:bb348c97df44 1215 /* MBED */
lypinator 0:bb348c97df44 1216
lypinator 0:bb348c97df44 1217 if ((ep_addr & 0x80) == 0x80)
lypinator 0:bb348c97df44 1218 {
lypinator 0:bb348c97df44 1219 USB_FlushTxFifo(hpcd->Instance, ep_addr & 0x7F);
lypinator 0:bb348c97df44 1220 }
lypinator 0:bb348c97df44 1221 else
lypinator 0:bb348c97df44 1222 {
lypinator 0:bb348c97df44 1223 USB_FlushRxFifo(hpcd->Instance);
lypinator 0:bb348c97df44 1224 }
lypinator 0:bb348c97df44 1225
lypinator 0:bb348c97df44 1226 /* MBED */
lypinator 0:bb348c97df44 1227 __HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
lypinator 0:bb348c97df44 1228 /* MBED */
lypinator 0:bb348c97df44 1229
lypinator 0:bb348c97df44 1230 return HAL_OK;
lypinator 0:bb348c97df44 1231 }
lypinator 0:bb348c97df44 1232
lypinator 0:bb348c97df44 1233 /**
lypinator 0:bb348c97df44 1234 * @brief Activate remote wakeup signalling.
lypinator 0:bb348c97df44 1235 * @param hpcd PCD handle
lypinator 0:bb348c97df44 1236 * @retval HAL status
lypinator 0:bb348c97df44 1237 */
lypinator 0:bb348c97df44 1238 HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 1239 {
lypinator 0:bb348c97df44 1240 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
lypinator 0:bb348c97df44 1241
lypinator 0:bb348c97df44 1242 if((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
lypinator 0:bb348c97df44 1243 {
lypinator 0:bb348c97df44 1244 /* Activate Remote wakeup signaling */
lypinator 0:bb348c97df44 1245 USBx_DEVICE->DCTL |= USB_OTG_DCTL_RWUSIG;
lypinator 0:bb348c97df44 1246 }
lypinator 0:bb348c97df44 1247 return HAL_OK;
lypinator 0:bb348c97df44 1248 }
lypinator 0:bb348c97df44 1249
lypinator 0:bb348c97df44 1250 /**
lypinator 0:bb348c97df44 1251 * @brief De-activate remote wakeup signalling.
lypinator 0:bb348c97df44 1252 * @param hpcd PCD handle
lypinator 0:bb348c97df44 1253 * @retval HAL status
lypinator 0:bb348c97df44 1254 */
lypinator 0:bb348c97df44 1255 HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 1256 {
lypinator 0:bb348c97df44 1257 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
lypinator 0:bb348c97df44 1258
lypinator 0:bb348c97df44 1259 /* De-activate Remote wakeup signaling */
lypinator 0:bb348c97df44 1260 USBx_DEVICE->DCTL &= ~(USB_OTG_DCTL_RWUSIG);
lypinator 0:bb348c97df44 1261 return HAL_OK;
lypinator 0:bb348c97df44 1262 }
lypinator 0:bb348c97df44 1263 /**
lypinator 0:bb348c97df44 1264 * @}
lypinator 0:bb348c97df44 1265 */
lypinator 0:bb348c97df44 1266
lypinator 0:bb348c97df44 1267 /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
lypinator 0:bb348c97df44 1268 * @brief Peripheral State functions
lypinator 0:bb348c97df44 1269 *
lypinator 0:bb348c97df44 1270 @verbatim
lypinator 0:bb348c97df44 1271 ===============================================================================
lypinator 0:bb348c97df44 1272 ##### Peripheral State functions #####
lypinator 0:bb348c97df44 1273 ===============================================================================
lypinator 0:bb348c97df44 1274 [..]
lypinator 0:bb348c97df44 1275 This subsection permits to get in run-time the status of the peripheral
lypinator 0:bb348c97df44 1276 and the data flow.
lypinator 0:bb348c97df44 1277
lypinator 0:bb348c97df44 1278 @endverbatim
lypinator 0:bb348c97df44 1279 * @{
lypinator 0:bb348c97df44 1280 */
lypinator 0:bb348c97df44 1281
lypinator 0:bb348c97df44 1282 /**
lypinator 0:bb348c97df44 1283 * @brief Return the PCD handle state.
lypinator 0:bb348c97df44 1284 * @param hpcd PCD handle
lypinator 0:bb348c97df44 1285 * @retval HAL state
lypinator 0:bb348c97df44 1286 */
lypinator 0:bb348c97df44 1287 PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
lypinator 0:bb348c97df44 1288 {
lypinator 0:bb348c97df44 1289 return hpcd->State;
lypinator 0:bb348c97df44 1290 }
lypinator 0:bb348c97df44 1291 /**
lypinator 0:bb348c97df44 1292 * @}
lypinator 0:bb348c97df44 1293 */
lypinator 0:bb348c97df44 1294
lypinator 0:bb348c97df44 1295 /**
lypinator 0:bb348c97df44 1296 * @}
lypinator 0:bb348c97df44 1297 */
lypinator 0:bb348c97df44 1298
lypinator 0:bb348c97df44 1299 /* Private functions ---------------------------------------------------------*/
lypinator 0:bb348c97df44 1300 /** @addtogroup PCD_Private_Functions
lypinator 0:bb348c97df44 1301 * @{
lypinator 0:bb348c97df44 1302 */
lypinator 0:bb348c97df44 1303
lypinator 0:bb348c97df44 1304 /**
lypinator 0:bb348c97df44 1305 * @brief Check FIFO for the next packet to be loaded.
lypinator 0:bb348c97df44 1306 * @param hpcd PCD handle
lypinator 0:bb348c97df44 1307 * @param epnum endpoint number
lypinator 0:bb348c97df44 1308 * @retval HAL status
lypinator 0:bb348c97df44 1309 */
lypinator 0:bb348c97df44 1310 static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
lypinator 0:bb348c97df44 1311 {
lypinator 0:bb348c97df44 1312 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
lypinator 0:bb348c97df44 1313 USB_OTG_EPTypeDef *ep;
lypinator 0:bb348c97df44 1314 uint32_t len;
lypinator 0:bb348c97df44 1315 uint32_t len32b;
lypinator 0:bb348c97df44 1316 uint32_t fifoemptymsk = 0U;
lypinator 0:bb348c97df44 1317
lypinator 0:bb348c97df44 1318 ep = &hpcd->IN_ep[epnum];
lypinator 0:bb348c97df44 1319 len = ep->xfer_len - ep->xfer_count;
lypinator 0:bb348c97df44 1320
lypinator 0:bb348c97df44 1321 if (len > ep->maxpacket)
lypinator 0:bb348c97df44 1322 {
lypinator 0:bb348c97df44 1323 len = ep->maxpacket;
lypinator 0:bb348c97df44 1324 }
lypinator 0:bb348c97df44 1325
lypinator 0:bb348c97df44 1326
lypinator 0:bb348c97df44 1327 len32b = (len + 3U) / 4U;
lypinator 0:bb348c97df44 1328
lypinator 0:bb348c97df44 1329 while (((USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) > len32b) &&
lypinator 0:bb348c97df44 1330 (ep->xfer_count < ep->xfer_len) &&
lypinator 0:bb348c97df44 1331 (ep->xfer_len != 0U))
lypinator 0:bb348c97df44 1332 {
lypinator 0:bb348c97df44 1333 /* Write the FIFO */
lypinator 0:bb348c97df44 1334 len = ep->xfer_len - ep->xfer_count;
lypinator 0:bb348c97df44 1335
lypinator 0:bb348c97df44 1336 if (len > ep->maxpacket)
lypinator 0:bb348c97df44 1337 {
lypinator 0:bb348c97df44 1338 len = ep->maxpacket;
lypinator 0:bb348c97df44 1339 }
lypinator 0:bb348c97df44 1340 len32b = (len + 3U) / 4U;
lypinator 0:bb348c97df44 1341
lypinator 0:bb348c97df44 1342 USB_WritePacket(USBx, ep->xfer_buff, epnum, len, hpcd->Init.dma_enable);
lypinator 0:bb348c97df44 1343
lypinator 0:bb348c97df44 1344 ep->xfer_buff += len;
lypinator 0:bb348c97df44 1345 ep->xfer_count += len;
lypinator 0:bb348c97df44 1346 }
lypinator 0:bb348c97df44 1347
lypinator 0:bb348c97df44 1348 if (ep->xfer_count >= ep->xfer_len)
lypinator 0:bb348c97df44 1349 {
lypinator 0:bb348c97df44 1350 fifoemptymsk = 0x1U << epnum;
lypinator 0:bb348c97df44 1351 /* MBED */
lypinator 0:bb348c97df44 1352 atomic_clr_u32(&USBx_DEVICE->DIEPEMPMSK, fifoemptymsk);
lypinator 0:bb348c97df44 1353 /* MBED */
lypinator 0:bb348c97df44 1354 }
lypinator 0:bb348c97df44 1355
lypinator 0:bb348c97df44 1356 return HAL_OK;
lypinator 0:bb348c97df44 1357 }
lypinator 0:bb348c97df44 1358
lypinator 0:bb348c97df44 1359 /**
lypinator 0:bb348c97df44 1360 * @}
lypinator 0:bb348c97df44 1361 */
lypinator 0:bb348c97df44 1362 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||
lypinator 0:bb348c97df44 1363 STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Rx ||
lypinator 0:bb348c97df44 1364 STM32F412Vx || STM32F412Cx || STM32F413xx || STM32F423xx */
lypinator 0:bb348c97df44 1365 #endif /* HAL_PCD_MODULE_ENABLED */
lypinator 0:bb348c97df44 1366 /**
lypinator 0:bb348c97df44 1367 * @}
lypinator 0:bb348c97df44 1368 */
lypinator 0:bb348c97df44 1369
lypinator 0:bb348c97df44 1370 /**
lypinator 0:bb348c97df44 1371 * @}
lypinator 0:bb348c97df44 1372 */
lypinator 0:bb348c97df44 1373
lypinator 0:bb348c97df44 1374 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/