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_ll_usb.c
lypinator 0:bb348c97df44 4 * @author MCD Application Team
lypinator 0:bb348c97df44 5 * @brief USB Low Layer HAL module driver.
lypinator 0:bb348c97df44 6 *
lypinator 0:bb348c97df44 7 * This file provides firmware functions to manage the following
lypinator 0:bb348c97df44 8 * functionalities of the USB Peripheral Controller:
lypinator 0:bb348c97df44 9 * + Initialization/de-initialization functions
lypinator 0:bb348c97df44 10 * + I/O operation functions
lypinator 0:bb348c97df44 11 * + Peripheral Control functions
lypinator 0:bb348c97df44 12 * + Peripheral State functions
lypinator 0:bb348c97df44 13 *
lypinator 0:bb348c97df44 14 @verbatim
lypinator 0:bb348c97df44 15 ==============================================================================
lypinator 0:bb348c97df44 16 ##### How to use this driver #####
lypinator 0:bb348c97df44 17 ==============================================================================
lypinator 0:bb348c97df44 18 [..]
lypinator 0:bb348c97df44 19 (#) Fill parameters of Init structure in USB_OTG_CfgTypeDef structure.
lypinator 0:bb348c97df44 20
lypinator 0:bb348c97df44 21 (#) Call USB_CoreInit() API to initialize the USB Core peripheral.
lypinator 0:bb348c97df44 22
lypinator 0:bb348c97df44 23 (#) The upper HAL HCD/PCD driver will call the right routines for its internal processes.
lypinator 0:bb348c97df44 24
lypinator 0:bb348c97df44 25 @endverbatim
lypinator 0:bb348c97df44 26 ******************************************************************************
lypinator 0:bb348c97df44 27 * @attention
lypinator 0:bb348c97df44 28 *
lypinator 0:bb348c97df44 29 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
lypinator 0:bb348c97df44 30 *
lypinator 0:bb348c97df44 31 * Redistribution and use in source and binary forms, with or without modification,
lypinator 0:bb348c97df44 32 * are permitted provided that the following conditions are met:
lypinator 0:bb348c97df44 33 * 1. Redistributions of source code must retain the above copyright notice,
lypinator 0:bb348c97df44 34 * this list of conditions and the following disclaimer.
lypinator 0:bb348c97df44 35 * 2. Redistributions in binary form must reproduce the above copyright notice,
lypinator 0:bb348c97df44 36 * this list of conditions and the following disclaimer in the documentation
lypinator 0:bb348c97df44 37 * and/or other materials provided with the distribution.
lypinator 0:bb348c97df44 38 * 3. Neither the name of STMicroelectronics nor the names of its contributors
lypinator 0:bb348c97df44 39 * may be used to endorse or promote products derived from this software
lypinator 0:bb348c97df44 40 * without specific prior written permission.
lypinator 0:bb348c97df44 41 *
lypinator 0:bb348c97df44 42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
lypinator 0:bb348c97df44 43 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
lypinator 0:bb348c97df44 44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
lypinator 0:bb348c97df44 45 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
lypinator 0:bb348c97df44 46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
lypinator 0:bb348c97df44 47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
lypinator 0:bb348c97df44 48 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
lypinator 0:bb348c97df44 49 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
lypinator 0:bb348c97df44 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
lypinator 0:bb348c97df44 51 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
lypinator 0:bb348c97df44 52 *
lypinator 0:bb348c97df44 53 ******************************************************************************
lypinator 0:bb348c97df44 54 */
lypinator 0:bb348c97df44 55
lypinator 0:bb348c97df44 56 /* Includes ------------------------------------------------------------------*/
lypinator 0:bb348c97df44 57 #include "stm32f4xx_hal.h"
lypinator 0:bb348c97df44 58
lypinator 0:bb348c97df44 59 /** @addtogroup STM32F4xx_LL_USB_DRIVER
lypinator 0:bb348c97df44 60 * @{
lypinator 0:bb348c97df44 61 */
lypinator 0:bb348c97df44 62
lypinator 0:bb348c97df44 63 #if defined(HAL_PCD_MODULE_ENABLED) || defined(HAL_HCD_MODULE_ENABLED)
lypinator 0:bb348c97df44 64 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || \
lypinator 0:bb348c97df44 65 defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
lypinator 0:bb348c97df44 66 defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE) || defined(STM32F446xx) || \
lypinator 0:bb348c97df44 67 defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
lypinator 0:bb348c97df44 68 defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
lypinator 0:bb348c97df44 69 /* Private typedef -----------------------------------------------------------*/
lypinator 0:bb348c97df44 70 /* Private define ------------------------------------------------------------*/
lypinator 0:bb348c97df44 71 /* Private macro -------------------------------------------------------------*/
lypinator 0:bb348c97df44 72 /* Private variables ---------------------------------------------------------*/
lypinator 0:bb348c97df44 73 /* Private function prototypes -----------------------------------------------*/
lypinator 0:bb348c97df44 74 /* Private functions ---------------------------------------------------------*/
lypinator 0:bb348c97df44 75 static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx);
lypinator 0:bb348c97df44 76
lypinator 0:bb348c97df44 77 /* Exported functions --------------------------------------------------------*/
lypinator 0:bb348c97df44 78
lypinator 0:bb348c97df44 79 /** @defgroup LL_USB_Exported_Functions USB Low Layer Exported Functions
lypinator 0:bb348c97df44 80 * @{
lypinator 0:bb348c97df44 81 */
lypinator 0:bb348c97df44 82
lypinator 0:bb348c97df44 83 /** @defgroup LL_USB_Group1 Initialization/de-initialization functions
lypinator 0:bb348c97df44 84 * @brief Initialization and Configuration functions
lypinator 0:bb348c97df44 85 *
lypinator 0:bb348c97df44 86 @verbatim
lypinator 0:bb348c97df44 87 ===============================================================================
lypinator 0:bb348c97df44 88 ##### Initialization/de-initialization functions #####
lypinator 0:bb348c97df44 89 ===============================================================================
lypinator 0:bb348c97df44 90 [..] This section provides functions allowing to:
lypinator 0:bb348c97df44 91
lypinator 0:bb348c97df44 92 @endverbatim
lypinator 0:bb348c97df44 93 * @{
lypinator 0:bb348c97df44 94 */
lypinator 0:bb348c97df44 95
lypinator 0:bb348c97df44 96 /**
lypinator 0:bb348c97df44 97 * @brief Initializes the USB Core
lypinator 0:bb348c97df44 98 * @param USBx USB Instance
lypinator 0:bb348c97df44 99 * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
lypinator 0:bb348c97df44 100 * the configuration information for the specified USBx peripheral.
lypinator 0:bb348c97df44 101 * @retval HAL status
lypinator 0:bb348c97df44 102 */
lypinator 0:bb348c97df44 103 HAL_StatusTypeDef USB_CoreInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
lypinator 0:bb348c97df44 104 {
lypinator 0:bb348c97df44 105 if (cfg.phy_itface == USB_OTG_ULPI_PHY)
lypinator 0:bb348c97df44 106 {
lypinator 0:bb348c97df44 107
lypinator 0:bb348c97df44 108 USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
lypinator 0:bb348c97df44 109
lypinator 0:bb348c97df44 110 /* Init The ULPI Interface */
lypinator 0:bb348c97df44 111 USBx->GUSBCFG &= ~(USB_OTG_GUSBCFG_TSDPS | USB_OTG_GUSBCFG_ULPIFSLS | USB_OTG_GUSBCFG_PHYSEL);
lypinator 0:bb348c97df44 112
lypinator 0:bb348c97df44 113 /* Select vbus source */
lypinator 0:bb348c97df44 114 USBx->GUSBCFG &= ~(USB_OTG_GUSBCFG_ULPIEVBUSD | USB_OTG_GUSBCFG_ULPIEVBUSI);
lypinator 0:bb348c97df44 115 if(cfg.use_external_vbus == 1U)
lypinator 0:bb348c97df44 116 {
lypinator 0:bb348c97df44 117 USBx->GUSBCFG |= USB_OTG_GUSBCFG_ULPIEVBUSD;
lypinator 0:bb348c97df44 118 }
lypinator 0:bb348c97df44 119 /* Reset after a PHY select */
lypinator 0:bb348c97df44 120 USB_CoreReset(USBx);
lypinator 0:bb348c97df44 121 }
lypinator 0:bb348c97df44 122 else /* FS interface (embedded Phy) */
lypinator 0:bb348c97df44 123 {
lypinator 0:bb348c97df44 124 /* Select FS Embedded PHY */
lypinator 0:bb348c97df44 125 USBx->GUSBCFG |= USB_OTG_GUSBCFG_PHYSEL;
lypinator 0:bb348c97df44 126
lypinator 0:bb348c97df44 127 /* Reset after a PHY select and set Host mode */
lypinator 0:bb348c97df44 128 USB_CoreReset(USBx);
lypinator 0:bb348c97df44 129
lypinator 0:bb348c97df44 130 /* Deactivate the power down*/
lypinator 0:bb348c97df44 131 USBx->GCCFG = USB_OTG_GCCFG_PWRDWN;
lypinator 0:bb348c97df44 132 }
lypinator 0:bb348c97df44 133
lypinator 0:bb348c97df44 134 if(cfg.dma_enable == ENABLE)
lypinator 0:bb348c97df44 135 {
lypinator 0:bb348c97df44 136 USBx->GAHBCFG |= USB_OTG_GAHBCFG_HBSTLEN_2;
lypinator 0:bb348c97df44 137 USBx->GAHBCFG |= USB_OTG_GAHBCFG_DMAEN;
lypinator 0:bb348c97df44 138 }
lypinator 0:bb348c97df44 139
lypinator 0:bb348c97df44 140 return HAL_OK;
lypinator 0:bb348c97df44 141 }
lypinator 0:bb348c97df44 142
lypinator 0:bb348c97df44 143 /**
lypinator 0:bb348c97df44 144 * @brief USB_EnableGlobalInt
lypinator 0:bb348c97df44 145 * Enables the controller's Global Int in the AHB Config reg
lypinator 0:bb348c97df44 146 * @param USBx Selected device
lypinator 0:bb348c97df44 147 * @retval HAL status
lypinator 0:bb348c97df44 148 */
lypinator 0:bb348c97df44 149 HAL_StatusTypeDef USB_EnableGlobalInt(USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 150 {
lypinator 0:bb348c97df44 151 USBx->GAHBCFG |= USB_OTG_GAHBCFG_GINT;
lypinator 0:bb348c97df44 152 return HAL_OK;
lypinator 0:bb348c97df44 153 }
lypinator 0:bb348c97df44 154
lypinator 0:bb348c97df44 155
lypinator 0:bb348c97df44 156 /**
lypinator 0:bb348c97df44 157 * @brief USB_DisableGlobalInt
lypinator 0:bb348c97df44 158 * Disable the controller's Global Int in the AHB Config reg
lypinator 0:bb348c97df44 159 * @param USBx Selected device
lypinator 0:bb348c97df44 160 * @retval HAL status
lypinator 0:bb348c97df44 161 */
lypinator 0:bb348c97df44 162 HAL_StatusTypeDef USB_DisableGlobalInt(USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 163 {
lypinator 0:bb348c97df44 164 USBx->GAHBCFG &= ~USB_OTG_GAHBCFG_GINT;
lypinator 0:bb348c97df44 165 return HAL_OK;
lypinator 0:bb348c97df44 166 }
lypinator 0:bb348c97df44 167
lypinator 0:bb348c97df44 168 /**
lypinator 0:bb348c97df44 169 * @brief USB_SetCurrentMode : Set functional mode
lypinator 0:bb348c97df44 170 * @param USBx Selected device
lypinator 0:bb348c97df44 171 * @param mode current core mode
lypinator 0:bb348c97df44 172 * This parameter can be one of these values:
lypinator 0:bb348c97df44 173 * @arg USB_OTG_DEVICE_MODE: Peripheral mode
lypinator 0:bb348c97df44 174 * @arg USB_OTG_HOST_MODE: Host mode
lypinator 0:bb348c97df44 175 * @arg USB_OTG_DRD_MODE: Dual Role Device mode
lypinator 0:bb348c97df44 176 * @retval HAL status
lypinator 0:bb348c97df44 177 */
lypinator 0:bb348c97df44 178 HAL_StatusTypeDef USB_SetCurrentMode(USB_OTG_GlobalTypeDef *USBx , USB_OTG_ModeTypeDef mode)
lypinator 0:bb348c97df44 179 {
lypinator 0:bb348c97df44 180 USBx->GUSBCFG &= ~(USB_OTG_GUSBCFG_FHMOD | USB_OTG_GUSBCFG_FDMOD);
lypinator 0:bb348c97df44 181
lypinator 0:bb348c97df44 182 if ( mode == USB_OTG_HOST_MODE)
lypinator 0:bb348c97df44 183 {
lypinator 0:bb348c97df44 184 USBx->GUSBCFG |= USB_OTG_GUSBCFG_FHMOD;
lypinator 0:bb348c97df44 185 }
lypinator 0:bb348c97df44 186 else if ( mode == USB_OTG_DEVICE_MODE)
lypinator 0:bb348c97df44 187 {
lypinator 0:bb348c97df44 188 USBx->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD;
lypinator 0:bb348c97df44 189 }
lypinator 0:bb348c97df44 190 HAL_Delay(50U);
lypinator 0:bb348c97df44 191
lypinator 0:bb348c97df44 192 return HAL_OK;
lypinator 0:bb348c97df44 193 }
lypinator 0:bb348c97df44 194
lypinator 0:bb348c97df44 195 /**
lypinator 0:bb348c97df44 196 * @brief USB_DevInit : Initializes the USB_OTG controller registers
lypinator 0:bb348c97df44 197 * for device mode
lypinator 0:bb348c97df44 198 * @param USBx Selected device
lypinator 0:bb348c97df44 199 * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
lypinator 0:bb348c97df44 200 * the configuration information for the specified USBx peripheral.
lypinator 0:bb348c97df44 201 * @retval HAL status
lypinator 0:bb348c97df44 202 */
lypinator 0:bb348c97df44 203 HAL_StatusTypeDef USB_DevInit (USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
lypinator 0:bb348c97df44 204 {
lypinator 0:bb348c97df44 205 uint32_t i = 0U;
lypinator 0:bb348c97df44 206
lypinator 0:bb348c97df44 207 /*Activate VBUS Sensing B */
lypinator 0:bb348c97df44 208 #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
lypinator 0:bb348c97df44 209 defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
lypinator 0:bb348c97df44 210 USBx->GCCFG |= USB_OTG_GCCFG_VBDEN;
lypinator 0:bb348c97df44 211
lypinator 0:bb348c97df44 212 if (cfg.vbus_sensing_enable == 0U)
lypinator 0:bb348c97df44 213 {
lypinator 0:bb348c97df44 214 /* Deactivate VBUS Sensing B */
lypinator 0:bb348c97df44 215 USBx->GCCFG &= ~USB_OTG_GCCFG_VBDEN;
lypinator 0:bb348c97df44 216
lypinator 0:bb348c97df44 217 /* B-peripheral session valid override enable*/
lypinator 0:bb348c97df44 218 USBx->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN;
lypinator 0:bb348c97df44 219 USBx->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL;
lypinator 0:bb348c97df44 220 }
lypinator 0:bb348c97df44 221 #else
lypinator 0:bb348c97df44 222 if (cfg.vbus_sensing_enable == 0U)
lypinator 0:bb348c97df44 223 {
lypinator 0:bb348c97df44 224 USBx->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS;
lypinator 0:bb348c97df44 225 }
lypinator 0:bb348c97df44 226 else
lypinator 0:bb348c97df44 227 {
lypinator 0:bb348c97df44 228 /* Enable VBUS */
lypinator 0:bb348c97df44 229 USBx->GCCFG |= USB_OTG_GCCFG_VBUSBSEN;
lypinator 0:bb348c97df44 230 }
lypinator 0:bb348c97df44 231 #endif /* STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Rx || STM32F412Vx || STM32F412Cx || STM32F413xx || STM32F423xx */
lypinator 0:bb348c97df44 232
lypinator 0:bb348c97df44 233 /* Restart the Phy Clock */
lypinator 0:bb348c97df44 234 USBx_PCGCCTL = 0U;
lypinator 0:bb348c97df44 235
lypinator 0:bb348c97df44 236 /* Device mode configuration */
lypinator 0:bb348c97df44 237 USBx_DEVICE->DCFG |= DCFG_FRAME_INTERVAL_80;
lypinator 0:bb348c97df44 238
lypinator 0:bb348c97df44 239 if(cfg.phy_itface == USB_OTG_ULPI_PHY)
lypinator 0:bb348c97df44 240 {
lypinator 0:bb348c97df44 241 if(cfg.speed == USB_OTG_SPEED_HIGH)
lypinator 0:bb348c97df44 242 {
lypinator 0:bb348c97df44 243 /* Set High speed phy */
lypinator 0:bb348c97df44 244 USB_SetDevSpeed (USBx , USB_OTG_SPEED_HIGH);
lypinator 0:bb348c97df44 245 }
lypinator 0:bb348c97df44 246 else
lypinator 0:bb348c97df44 247 {
lypinator 0:bb348c97df44 248 /* set High speed phy in Full speed mode */
lypinator 0:bb348c97df44 249 USB_SetDevSpeed (USBx , USB_OTG_SPEED_HIGH_IN_FULL);
lypinator 0:bb348c97df44 250 }
lypinator 0:bb348c97df44 251 }
lypinator 0:bb348c97df44 252 else
lypinator 0:bb348c97df44 253 {
lypinator 0:bb348c97df44 254 /* Set Full speed phy */
lypinator 0:bb348c97df44 255 USB_SetDevSpeed (USBx , USB_OTG_SPEED_FULL);
lypinator 0:bb348c97df44 256 }
lypinator 0:bb348c97df44 257
lypinator 0:bb348c97df44 258 /* Flush the FIFOs */
lypinator 0:bb348c97df44 259 USB_FlushTxFifo(USBx , 0x10U); /* all Tx FIFOs */
lypinator 0:bb348c97df44 260 USB_FlushRxFifo(USBx);
lypinator 0:bb348c97df44 261
lypinator 0:bb348c97df44 262 /* Clear all pending Device Interrupts */
lypinator 0:bb348c97df44 263 USBx_DEVICE->DIEPMSK = 0U;
lypinator 0:bb348c97df44 264 USBx_DEVICE->DOEPMSK = 0U;
lypinator 0:bb348c97df44 265 USBx_DEVICE->DAINT = 0xFFFFFFFFU;
lypinator 0:bb348c97df44 266 USBx_DEVICE->DAINTMSK = 0U;
lypinator 0:bb348c97df44 267
lypinator 0:bb348c97df44 268 for (i = 0U; i < cfg.dev_endpoints; i++)
lypinator 0:bb348c97df44 269 {
lypinator 0:bb348c97df44 270 if ((USBx_INEP(i)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
lypinator 0:bb348c97df44 271 {
lypinator 0:bb348c97df44 272 USBx_INEP(i)->DIEPCTL = (USB_OTG_DIEPCTL_EPDIS | USB_OTG_DIEPCTL_SNAK);
lypinator 0:bb348c97df44 273 }
lypinator 0:bb348c97df44 274 else
lypinator 0:bb348c97df44 275 {
lypinator 0:bb348c97df44 276 USBx_INEP(i)->DIEPCTL = 0U;
lypinator 0:bb348c97df44 277 }
lypinator 0:bb348c97df44 278
lypinator 0:bb348c97df44 279 USBx_INEP(i)->DIEPTSIZ = 0U;
lypinator 0:bb348c97df44 280 USBx_INEP(i)->DIEPINT = 0xFFU;
lypinator 0:bb348c97df44 281 }
lypinator 0:bb348c97df44 282
lypinator 0:bb348c97df44 283 for (i = 0U; i < cfg.dev_endpoints; i++)
lypinator 0:bb348c97df44 284 {
lypinator 0:bb348c97df44 285 if ((USBx_OUTEP(i)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
lypinator 0:bb348c97df44 286 {
lypinator 0:bb348c97df44 287 USBx_OUTEP(i)->DOEPCTL = (USB_OTG_DOEPCTL_EPDIS | USB_OTG_DOEPCTL_SNAK);
lypinator 0:bb348c97df44 288 }
lypinator 0:bb348c97df44 289 else
lypinator 0:bb348c97df44 290 {
lypinator 0:bb348c97df44 291 USBx_OUTEP(i)->DOEPCTL = 0U;
lypinator 0:bb348c97df44 292 }
lypinator 0:bb348c97df44 293
lypinator 0:bb348c97df44 294 USBx_OUTEP(i)->DOEPTSIZ = 0U;
lypinator 0:bb348c97df44 295 USBx_OUTEP(i)->DOEPINT = 0xFFU;
lypinator 0:bb348c97df44 296 }
lypinator 0:bb348c97df44 297
lypinator 0:bb348c97df44 298 USBx_DEVICE->DIEPMSK &= ~(USB_OTG_DIEPMSK_TXFURM);
lypinator 0:bb348c97df44 299
lypinator 0:bb348c97df44 300 if (cfg.dma_enable == 1U)
lypinator 0:bb348c97df44 301 {
lypinator 0:bb348c97df44 302 /*Set threshold parameters */
lypinator 0:bb348c97df44 303 USBx_DEVICE->DTHRCTL = (USB_OTG_DTHRCTL_TXTHRLEN_6 | USB_OTG_DTHRCTL_RXTHRLEN_6);
lypinator 0:bb348c97df44 304 USBx_DEVICE->DTHRCTL |= (USB_OTG_DTHRCTL_RXTHREN | USB_OTG_DTHRCTL_ISOTHREN | USB_OTG_DTHRCTL_NONISOTHREN);
lypinator 0:bb348c97df44 305
lypinator 0:bb348c97df44 306 i= USBx_DEVICE->DTHRCTL;
lypinator 0:bb348c97df44 307 }
lypinator 0:bb348c97df44 308
lypinator 0:bb348c97df44 309 /* Disable all interrupts. */
lypinator 0:bb348c97df44 310 USBx->GINTMSK = 0U;
lypinator 0:bb348c97df44 311
lypinator 0:bb348c97df44 312 /* Clear any pending interrupts */
lypinator 0:bb348c97df44 313 USBx->GINTSTS = 0xBFFFFFFFU;
lypinator 0:bb348c97df44 314
lypinator 0:bb348c97df44 315 /* Enable the common interrupts */
lypinator 0:bb348c97df44 316 if (cfg.dma_enable == DISABLE)
lypinator 0:bb348c97df44 317 {
lypinator 0:bb348c97df44 318 USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
lypinator 0:bb348c97df44 319 }
lypinator 0:bb348c97df44 320
lypinator 0:bb348c97df44 321 /* Enable interrupts matching to the Device mode ONLY */
lypinator 0:bb348c97df44 322 USBx->GINTMSK |= (USB_OTG_GINTMSK_USBSUSPM | USB_OTG_GINTMSK_USBRST |\
lypinator 0:bb348c97df44 323 USB_OTG_GINTMSK_ENUMDNEM | USB_OTG_GINTMSK_IEPINT |\
lypinator 0:bb348c97df44 324 USB_OTG_GINTMSK_OEPINT | USB_OTG_GINTMSK_IISOIXFRM|\
lypinator 0:bb348c97df44 325 USB_OTG_GINTMSK_PXFRM_IISOOXFRM | USB_OTG_GINTMSK_WUIM);
lypinator 0:bb348c97df44 326
lypinator 0:bb348c97df44 327 if(cfg.Sof_enable)
lypinator 0:bb348c97df44 328 {
lypinator 0:bb348c97df44 329 USBx->GINTMSK |= USB_OTG_GINTMSK_SOFM;
lypinator 0:bb348c97df44 330 }
lypinator 0:bb348c97df44 331
lypinator 0:bb348c97df44 332 if (cfg.vbus_sensing_enable == ENABLE)
lypinator 0:bb348c97df44 333 {
lypinator 0:bb348c97df44 334 USBx->GINTMSK |= (USB_OTG_GINTMSK_SRQIM | USB_OTG_GINTMSK_OTGINT);
lypinator 0:bb348c97df44 335 }
lypinator 0:bb348c97df44 336
lypinator 0:bb348c97df44 337 return HAL_OK;
lypinator 0:bb348c97df44 338 }
lypinator 0:bb348c97df44 339
lypinator 0:bb348c97df44 340
lypinator 0:bb348c97df44 341 /**
lypinator 0:bb348c97df44 342 * @brief USB_OTG_FlushTxFifo : Flush a Tx FIFO
lypinator 0:bb348c97df44 343 * @param USBx Selected device
lypinator 0:bb348c97df44 344 * @param num FIFO number
lypinator 0:bb348c97df44 345 * This parameter can be a value from 1 to 15
lypinator 0:bb348c97df44 346 15 means Flush all Tx FIFOs
lypinator 0:bb348c97df44 347 * @retval HAL status
lypinator 0:bb348c97df44 348 */
lypinator 0:bb348c97df44 349 HAL_StatusTypeDef USB_FlushTxFifo (USB_OTG_GlobalTypeDef *USBx, uint32_t num )
lypinator 0:bb348c97df44 350 {
lypinator 0:bb348c97df44 351 uint32_t count = 0;
lypinator 0:bb348c97df44 352
lypinator 0:bb348c97df44 353 USBx->GRSTCTL = ( USB_OTG_GRSTCTL_TXFFLSH |(uint32_t)( num << 6));
lypinator 0:bb348c97df44 354
lypinator 0:bb348c97df44 355 do
lypinator 0:bb348c97df44 356 {
lypinator 0:bb348c97df44 357 if (++count > 200000)
lypinator 0:bb348c97df44 358 {
lypinator 0:bb348c97df44 359 return HAL_TIMEOUT;
lypinator 0:bb348c97df44 360 }
lypinator 0:bb348c97df44 361 }
lypinator 0:bb348c97df44 362 while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_TXFFLSH) == USB_OTG_GRSTCTL_TXFFLSH);
lypinator 0:bb348c97df44 363
lypinator 0:bb348c97df44 364 return HAL_OK;
lypinator 0:bb348c97df44 365 }
lypinator 0:bb348c97df44 366
lypinator 0:bb348c97df44 367
lypinator 0:bb348c97df44 368 /**
lypinator 0:bb348c97df44 369 * @brief USB_FlushRxFifo : Flush Rx FIFO
lypinator 0:bb348c97df44 370 * @param USBx Selected device
lypinator 0:bb348c97df44 371 * @retval HAL status
lypinator 0:bb348c97df44 372 */
lypinator 0:bb348c97df44 373 HAL_StatusTypeDef USB_FlushRxFifo(USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 374 {
lypinator 0:bb348c97df44 375 uint32_t count = 0;
lypinator 0:bb348c97df44 376
lypinator 0:bb348c97df44 377 USBx->GRSTCTL = USB_OTG_GRSTCTL_RXFFLSH;
lypinator 0:bb348c97df44 378
lypinator 0:bb348c97df44 379 do
lypinator 0:bb348c97df44 380 {
lypinator 0:bb348c97df44 381 if (++count > 200000)
lypinator 0:bb348c97df44 382 {
lypinator 0:bb348c97df44 383 return HAL_TIMEOUT;
lypinator 0:bb348c97df44 384 }
lypinator 0:bb348c97df44 385 }
lypinator 0:bb348c97df44 386 while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_RXFFLSH) == USB_OTG_GRSTCTL_RXFFLSH);
lypinator 0:bb348c97df44 387
lypinator 0:bb348c97df44 388 return HAL_OK;
lypinator 0:bb348c97df44 389 }
lypinator 0:bb348c97df44 390
lypinator 0:bb348c97df44 391 /**
lypinator 0:bb348c97df44 392 * @brief USB_SetDevSpeed :Initializes the DevSpd field of DCFG register
lypinator 0:bb348c97df44 393 * depending the PHY type and the enumeration speed of the device.
lypinator 0:bb348c97df44 394 * @param USBx Selected device
lypinator 0:bb348c97df44 395 * @param speed device speed
lypinator 0:bb348c97df44 396 * This parameter can be one of these values:
lypinator 0:bb348c97df44 397 * @arg USB_OTG_SPEED_HIGH: High speed mode
lypinator 0:bb348c97df44 398 * @arg USB_OTG_SPEED_HIGH_IN_FULL: High speed core in Full Speed mode
lypinator 0:bb348c97df44 399 * @arg USB_OTG_SPEED_FULL: Full speed mode
lypinator 0:bb348c97df44 400 * @arg USB_OTG_SPEED_LOW: Low speed mode
lypinator 0:bb348c97df44 401 * @retval Hal status
lypinator 0:bb348c97df44 402 */
lypinator 0:bb348c97df44 403 HAL_StatusTypeDef USB_SetDevSpeed(USB_OTG_GlobalTypeDef *USBx , uint8_t speed)
lypinator 0:bb348c97df44 404 {
lypinator 0:bb348c97df44 405 USBx_DEVICE->DCFG |= speed;
lypinator 0:bb348c97df44 406 return HAL_OK;
lypinator 0:bb348c97df44 407 }
lypinator 0:bb348c97df44 408
lypinator 0:bb348c97df44 409 /**
lypinator 0:bb348c97df44 410 * @brief USB_GetDevSpeed :Return the Dev Speed
lypinator 0:bb348c97df44 411 * @param USBx Selected device
lypinator 0:bb348c97df44 412 * @retval speed : device speed
lypinator 0:bb348c97df44 413 * This parameter can be one of these values:
lypinator 0:bb348c97df44 414 * @arg USB_OTG_SPEED_HIGH: High speed mode
lypinator 0:bb348c97df44 415 * @arg USB_OTG_SPEED_FULL: Full speed mode
lypinator 0:bb348c97df44 416 * @arg USB_OTG_SPEED_LOW: Low speed mode
lypinator 0:bb348c97df44 417 */
lypinator 0:bb348c97df44 418 uint8_t USB_GetDevSpeed(USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 419 {
lypinator 0:bb348c97df44 420 uint8_t speed = 0U;
lypinator 0:bb348c97df44 421
lypinator 0:bb348c97df44 422 if((USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD) == DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ)
lypinator 0:bb348c97df44 423 {
lypinator 0:bb348c97df44 424 speed = USB_OTG_SPEED_HIGH;
lypinator 0:bb348c97df44 425 }
lypinator 0:bb348c97df44 426 else if (((USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD) == DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ)||
lypinator 0:bb348c97df44 427 ((USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD) == DSTS_ENUMSPD_FS_PHY_48MHZ))
lypinator 0:bb348c97df44 428 {
lypinator 0:bb348c97df44 429 speed = USB_OTG_SPEED_FULL;
lypinator 0:bb348c97df44 430 }
lypinator 0:bb348c97df44 431 else if((USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD) == DSTS_ENUMSPD_LS_PHY_6MHZ)
lypinator 0:bb348c97df44 432 {
lypinator 0:bb348c97df44 433 speed = USB_OTG_SPEED_LOW;
lypinator 0:bb348c97df44 434 }
lypinator 0:bb348c97df44 435
lypinator 0:bb348c97df44 436 return speed;
lypinator 0:bb348c97df44 437 }
lypinator 0:bb348c97df44 438
lypinator 0:bb348c97df44 439 /**
lypinator 0:bb348c97df44 440 * @brief Activate and configure an endpoint
lypinator 0:bb348c97df44 441 * @param USBx Selected device
lypinator 0:bb348c97df44 442 * @param ep pointer to endpoint structure
lypinator 0:bb348c97df44 443 * @retval HAL status
lypinator 0:bb348c97df44 444 */
lypinator 0:bb348c97df44 445 HAL_StatusTypeDef USB_ActivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
lypinator 0:bb348c97df44 446 {
lypinator 0:bb348c97df44 447 if (ep->is_in == 1U)
lypinator 0:bb348c97df44 448 {
lypinator 0:bb348c97df44 449 USBx_DEVICE->DAINTMSK |= USB_OTG_DAINTMSK_IEPM & ((1U << (ep->num)));
lypinator 0:bb348c97df44 450
lypinator 0:bb348c97df44 451 if (((USBx_INEP(ep->num)->DIEPCTL) & USB_OTG_DIEPCTL_USBAEP) == 0U)
lypinator 0:bb348c97df44 452 {
lypinator 0:bb348c97df44 453 USBx_INEP(ep->num)->DIEPCTL |= ((ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ ) | (ep->type << 18U) |\
lypinator 0:bb348c97df44 454 ((ep->num) << 22U) | (USB_OTG_DIEPCTL_SD0PID_SEVNFRM) | (USB_OTG_DIEPCTL_USBAEP));
lypinator 0:bb348c97df44 455 }
lypinator 0:bb348c97df44 456 }
lypinator 0:bb348c97df44 457 else
lypinator 0:bb348c97df44 458 {
lypinator 0:bb348c97df44 459 USBx_DEVICE->DAINTMSK |= USB_OTG_DAINTMSK_OEPM & ((1U << (ep->num)) << 16U);
lypinator 0:bb348c97df44 460
lypinator 0:bb348c97df44 461 if (((USBx_OUTEP(ep->num)->DOEPCTL) & USB_OTG_DOEPCTL_USBAEP) == 0U)
lypinator 0:bb348c97df44 462 {
lypinator 0:bb348c97df44 463 USBx_OUTEP(ep->num)->DOEPCTL |= ((ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ ) | (ep->type << 18U) |\
lypinator 0:bb348c97df44 464 (USB_OTG_DIEPCTL_SD0PID_SEVNFRM)| (USB_OTG_DOEPCTL_USBAEP));
lypinator 0:bb348c97df44 465 }
lypinator 0:bb348c97df44 466 }
lypinator 0:bb348c97df44 467 return HAL_OK;
lypinator 0:bb348c97df44 468 }
lypinator 0:bb348c97df44 469 /**
lypinator 0:bb348c97df44 470 * @brief Activate and configure a dedicated endpoint
lypinator 0:bb348c97df44 471 * @param USBx Selected device
lypinator 0:bb348c97df44 472 * @param ep pointer to endpoint structure
lypinator 0:bb348c97df44 473 * @retval HAL status
lypinator 0:bb348c97df44 474 */
lypinator 0:bb348c97df44 475 HAL_StatusTypeDef USB_ActivateDedicatedEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
lypinator 0:bb348c97df44 476 {
lypinator 0:bb348c97df44 477 static __IO uint32_t debug = 0U;
lypinator 0:bb348c97df44 478
lypinator 0:bb348c97df44 479 /* Read DEPCTLn register */
lypinator 0:bb348c97df44 480 if (ep->is_in == 1U)
lypinator 0:bb348c97df44 481 {
lypinator 0:bb348c97df44 482 if (((USBx_INEP(ep->num)->DIEPCTL) & USB_OTG_DIEPCTL_USBAEP) == 0U)
lypinator 0:bb348c97df44 483 {
lypinator 0:bb348c97df44 484 USBx_INEP(ep->num)->DIEPCTL |= ((ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ ) | (ep->type << 18U) |\
lypinator 0:bb348c97df44 485 ((ep->num) << 22U) | (USB_OTG_DIEPCTL_SD0PID_SEVNFRM) | (USB_OTG_DIEPCTL_USBAEP));
lypinator 0:bb348c97df44 486 }
lypinator 0:bb348c97df44 487
lypinator 0:bb348c97df44 488
lypinator 0:bb348c97df44 489 debug |= ((ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ ) | (ep->type << 18U) |\
lypinator 0:bb348c97df44 490 ((ep->num) << 22U) | (USB_OTG_DIEPCTL_SD0PID_SEVNFRM) | (USB_OTG_DIEPCTL_USBAEP));
lypinator 0:bb348c97df44 491
lypinator 0:bb348c97df44 492 USBx_DEVICE->DEACHMSK |= USB_OTG_DAINTMSK_IEPM & ((1U << (ep->num)));
lypinator 0:bb348c97df44 493 }
lypinator 0:bb348c97df44 494 else
lypinator 0:bb348c97df44 495 {
lypinator 0:bb348c97df44 496 if (((USBx_OUTEP(ep->num)->DOEPCTL) & USB_OTG_DOEPCTL_USBAEP) == 0U)
lypinator 0:bb348c97df44 497 {
lypinator 0:bb348c97df44 498 USBx_OUTEP(ep->num)->DOEPCTL |= ((ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ ) | (ep->type << 18U) |\
lypinator 0:bb348c97df44 499 ((ep->num) << 22U) | (USB_OTG_DOEPCTL_USBAEP));
lypinator 0:bb348c97df44 500
lypinator 0:bb348c97df44 501 debug = (uint32_t)(((uint32_t )USBx) + USB_OTG_OUT_ENDPOINT_BASE + (0U)*USB_OTG_EP_REG_SIZE);
lypinator 0:bb348c97df44 502 debug = (uint32_t )&USBx_OUTEP(ep->num)->DOEPCTL;
lypinator 0:bb348c97df44 503 debug |= ((ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ ) | (ep->type << 18U) |\
lypinator 0:bb348c97df44 504 ((ep->num) << 22U) | (USB_OTG_DOEPCTL_USBAEP));
lypinator 0:bb348c97df44 505 }
lypinator 0:bb348c97df44 506
lypinator 0:bb348c97df44 507 USBx_DEVICE->DEACHMSK |= USB_OTG_DAINTMSK_OEPM & ((1U << (ep->num)) << 16U);
lypinator 0:bb348c97df44 508 }
lypinator 0:bb348c97df44 509
lypinator 0:bb348c97df44 510 return HAL_OK;
lypinator 0:bb348c97df44 511 }
lypinator 0:bb348c97df44 512 /**
lypinator 0:bb348c97df44 513 * @brief De-activate and de-initialize an endpoint
lypinator 0:bb348c97df44 514 * @param USBx Selected device
lypinator 0:bb348c97df44 515 * @param ep pointer to endpoint structure
lypinator 0:bb348c97df44 516 * @retval HAL status
lypinator 0:bb348c97df44 517 */
lypinator 0:bb348c97df44 518 HAL_StatusTypeDef USB_DeactivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
lypinator 0:bb348c97df44 519 {
lypinator 0:bb348c97df44 520 uint32_t count = 0U;
lypinator 0:bb348c97df44 521
lypinator 0:bb348c97df44 522 /* Disable the IN endpoint */
lypinator 0:bb348c97df44 523 if (ep->is_in == 1U)
lypinator 0:bb348c97df44 524 {
lypinator 0:bb348c97df44 525 USBx_INEP(ep->num)->DIEPCTL &= ~USB_OTG_DIEPCTL_USBAEP;
lypinator 0:bb348c97df44 526
lypinator 0:bb348c97df44 527 /* sets the NAK bit for the IN endpoint */
lypinator 0:bb348c97df44 528 USBx_INEP(ep->num)->DIEPCTL = USB_OTG_DIEPCTL_SNAK;
lypinator 0:bb348c97df44 529
lypinator 0:bb348c97df44 530 /* Disable IN endpoint */
lypinator 0:bb348c97df44 531 USBx_INEP(ep->num)->DIEPCTL = USB_OTG_DIEPCTL_EPDIS;
lypinator 0:bb348c97df44 532
lypinator 0:bb348c97df44 533 do
lypinator 0:bb348c97df44 534 {
lypinator 0:bb348c97df44 535 if (++count > 200000U)
lypinator 0:bb348c97df44 536 {
lypinator 0:bb348c97df44 537 return HAL_TIMEOUT;
lypinator 0:bb348c97df44 538 }
lypinator 0:bb348c97df44 539 }
lypinator 0:bb348c97df44 540
lypinator 0:bb348c97df44 541 /*Wait for EPDISD endpoint disabled interrupt*/
lypinator 0:bb348c97df44 542 while ((USBx_INEP(ep->num)->DIEPINT & USB_OTG_DIEPCTL_EPDIS) == USB_OTG_DIEPCTL_EPDIS);
lypinator 0:bb348c97df44 543
lypinator 0:bb348c97df44 544
lypinator 0:bb348c97df44 545 /* Flush any data remaining in the TxFIFO */
lypinator 0:bb348c97df44 546 USB_FlushTxFifo(USBx , 0x10U);
lypinator 0:bb348c97df44 547
lypinator 0:bb348c97df44 548 /* Disable endpoint interrupts */
lypinator 0:bb348c97df44 549 USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_IEPM & ((1U << (ep->num))));
lypinator 0:bb348c97df44 550
lypinator 0:bb348c97df44 551 }
lypinator 0:bb348c97df44 552 else /* Disable the OUT endpoint */
lypinator 0:bb348c97df44 553 {
lypinator 0:bb348c97df44 554
lypinator 0:bb348c97df44 555 USBx_OUTEP(ep->num)->DOEPCTL &= ~USB_OTG_DOEPCTL_USBAEP;
lypinator 0:bb348c97df44 556
lypinator 0:bb348c97df44 557 /* sets the NAK bit for the OUT endpoint */
lypinator 0:bb348c97df44 558 USBx_OUTEP(ep->num)->DOEPCTL = USB_OTG_DOEPCTL_SNAK;
lypinator 0:bb348c97df44 559
lypinator 0:bb348c97df44 560 /* Disable OUT endpoint */
lypinator 0:bb348c97df44 561 USBx_OUTEP(ep->num)->DOEPCTL = USB_OTG_DOEPCTL_EPDIS;
lypinator 0:bb348c97df44 562
lypinator 0:bb348c97df44 563 do
lypinator 0:bb348c97df44 564 {
lypinator 0:bb348c97df44 565 if (++count > 200000U)
lypinator 0:bb348c97df44 566 {
lypinator 0:bb348c97df44 567 return HAL_TIMEOUT;
lypinator 0:bb348c97df44 568 }
lypinator 0:bb348c97df44 569 }
lypinator 0:bb348c97df44 570
lypinator 0:bb348c97df44 571 /*Wait for EPDISD endpoint disabled interrupt*/
lypinator 0:bb348c97df44 572 while ((USBx_OUTEP(ep->num)->DOEPINT & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS);
lypinator 0:bb348c97df44 573
lypinator 0:bb348c97df44 574 /* Set the "Clear the Global OUT NAK bit" to disable global OUT NAK mode */
lypinator 0:bb348c97df44 575 USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGONAK;
lypinator 0:bb348c97df44 576
lypinator 0:bb348c97df44 577 /* Disable endpoint interrupts */
lypinator 0:bb348c97df44 578 USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((1U << (ep->num)) << 16U));
lypinator 0:bb348c97df44 579 }
lypinator 0:bb348c97df44 580 return HAL_OK;
lypinator 0:bb348c97df44 581 }
lypinator 0:bb348c97df44 582
lypinator 0:bb348c97df44 583 /**
lypinator 0:bb348c97df44 584 * @brief De-activate and de-initialize a dedicated endpoint
lypinator 0:bb348c97df44 585 * @param USBx Selected device
lypinator 0:bb348c97df44 586 * @param ep pointer to endpoint structure
lypinator 0:bb348c97df44 587 * @retval HAL status
lypinator 0:bb348c97df44 588 */
lypinator 0:bb348c97df44 589 HAL_StatusTypeDef USB_DeactivateDedicatedEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
lypinator 0:bb348c97df44 590 {
lypinator 0:bb348c97df44 591 uint32_t count = 0U;
lypinator 0:bb348c97df44 592
lypinator 0:bb348c97df44 593 /* Disable the IN endpoint */
lypinator 0:bb348c97df44 594 if (ep->is_in == 1U)
lypinator 0:bb348c97df44 595 {
lypinator 0:bb348c97df44 596 USBx_INEP(ep->num)->DIEPCTL &= ~USB_OTG_DIEPCTL_USBAEP;
lypinator 0:bb348c97df44 597
lypinator 0:bb348c97df44 598 /* sets the NAK bit for the IN endpoint */
lypinator 0:bb348c97df44 599 USBx_INEP(ep->num)->DIEPCTL = USB_OTG_DIEPCTL_SNAK;
lypinator 0:bb348c97df44 600
lypinator 0:bb348c97df44 601 /* Disable IN endpoint */
lypinator 0:bb348c97df44 602 USBx_INEP(ep->num)->DIEPCTL = USB_OTG_DIEPCTL_EPDIS;
lypinator 0:bb348c97df44 603
lypinator 0:bb348c97df44 604 do
lypinator 0:bb348c97df44 605 {
lypinator 0:bb348c97df44 606 if (++count > 200000U)
lypinator 0:bb348c97df44 607 {
lypinator 0:bb348c97df44 608 return HAL_TIMEOUT;
lypinator 0:bb348c97df44 609 }
lypinator 0:bb348c97df44 610 }
lypinator 0:bb348c97df44 611
lypinator 0:bb348c97df44 612 /*Wait for EPDISD endpoint disabled interrupt*/
lypinator 0:bb348c97df44 613 while ((USBx_INEP(ep->num)->DIEPINT & USB_OTG_DIEPCTL_EPDIS) == USB_OTG_DIEPCTL_EPDIS);
lypinator 0:bb348c97df44 614
lypinator 0:bb348c97df44 615
lypinator 0:bb348c97df44 616 /* Flush any data remaining in the TxFIFO */
lypinator 0:bb348c97df44 617 USB_FlushTxFifo(USBx , 0x10U);
lypinator 0:bb348c97df44 618
lypinator 0:bb348c97df44 619 /* Disable endpoint interrupts */
lypinator 0:bb348c97df44 620 USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_IEPM & ((1U << (ep->num))));
lypinator 0:bb348c97df44 621
lypinator 0:bb348c97df44 622 }
lypinator 0:bb348c97df44 623 else /* Disable the OUT endpoint */
lypinator 0:bb348c97df44 624 {
lypinator 0:bb348c97df44 625
lypinator 0:bb348c97df44 626 USBx_OUTEP(ep->num)->DOEPCTL &= ~USB_OTG_DOEPCTL_USBAEP;
lypinator 0:bb348c97df44 627
lypinator 0:bb348c97df44 628 /* sets the NAK bit for the OUT endpoint */
lypinator 0:bb348c97df44 629 USBx_OUTEP(ep->num)->DOEPCTL = USB_OTG_DOEPCTL_SNAK;
lypinator 0:bb348c97df44 630
lypinator 0:bb348c97df44 631 /* Disable OUT endpoint */
lypinator 0:bb348c97df44 632 USBx_OUTEP(ep->num)->DOEPCTL = USB_OTG_DOEPCTL_EPDIS;
lypinator 0:bb348c97df44 633
lypinator 0:bb348c97df44 634 do
lypinator 0:bb348c97df44 635 {
lypinator 0:bb348c97df44 636 if (++count > 200000U)
lypinator 0:bb348c97df44 637 {
lypinator 0:bb348c97df44 638 return HAL_TIMEOUT;
lypinator 0:bb348c97df44 639 }
lypinator 0:bb348c97df44 640 }
lypinator 0:bb348c97df44 641
lypinator 0:bb348c97df44 642 /*Wait for EPDISD endpoint disabled interrupt*/
lypinator 0:bb348c97df44 643 while ((USBx_OUTEP(ep->num)->DOEPINT & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS);
lypinator 0:bb348c97df44 644
lypinator 0:bb348c97df44 645 /* Set the "Clear the Global OUT NAK bit" to disable global OUT NAK mode */
lypinator 0:bb348c97df44 646 USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGONAK;
lypinator 0:bb348c97df44 647
lypinator 0:bb348c97df44 648 /* Disable endpoint interrupts */
lypinator 0:bb348c97df44 649 USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((1U << (ep->num)) << 16U));
lypinator 0:bb348c97df44 650 }
lypinator 0:bb348c97df44 651 return HAL_OK;
lypinator 0:bb348c97df44 652
lypinator 0:bb348c97df44 653 }
lypinator 0:bb348c97df44 654
lypinator 0:bb348c97df44 655 /**
lypinator 0:bb348c97df44 656 * @brief USB_EPStartXfer : setup and starts a transfer over an EP
lypinator 0:bb348c97df44 657 * @param USBx Selected device
lypinator 0:bb348c97df44 658 * @param ep pointer to endpoint structure
lypinator 0:bb348c97df44 659 * @param dma USB dma enabled or disabled
lypinator 0:bb348c97df44 660 * This parameter can be one of these values:
lypinator 0:bb348c97df44 661 * 0 : DMA feature not used
lypinator 0:bb348c97df44 662 * 1 : DMA feature used
lypinator 0:bb348c97df44 663 * @retval HAL status
lypinator 0:bb348c97df44 664 */
lypinator 0:bb348c97df44 665 HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDef *ep, uint8_t dma)
lypinator 0:bb348c97df44 666 {
lypinator 0:bb348c97df44 667 uint16_t pktcnt = 0U;
lypinator 0:bb348c97df44 668
lypinator 0:bb348c97df44 669 /* IN endpoint */
lypinator 0:bb348c97df44 670 if (ep->is_in == 1U)
lypinator 0:bb348c97df44 671 {
lypinator 0:bb348c97df44 672 /* Zero Length Packet? */
lypinator 0:bb348c97df44 673 if (ep->xfer_len == 0U)
lypinator 0:bb348c97df44 674 {
lypinator 0:bb348c97df44 675 USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
lypinator 0:bb348c97df44 676 USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1U << 19U)) ;
lypinator 0:bb348c97df44 677 USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
lypinator 0:bb348c97df44 678 }
lypinator 0:bb348c97df44 679 else
lypinator 0:bb348c97df44 680 {
lypinator 0:bb348c97df44 681 /* Program the transfer size and packet count
lypinator 0:bb348c97df44 682 * as follows: xfersize = N * maxpacket +
lypinator 0:bb348c97df44 683 * short_packet pktcnt = N + (short_packet
lypinator 0:bb348c97df44 684 * exist ? 1 : 0)
lypinator 0:bb348c97df44 685 */
lypinator 0:bb348c97df44 686 USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
lypinator 0:bb348c97df44 687 USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
lypinator 0:bb348c97df44 688 USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (((ep->xfer_len + ep->maxpacket -1U)/ ep->maxpacket) << 19U)) ;
lypinator 0:bb348c97df44 689 USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_XFRSIZ & ep->xfer_len);
lypinator 0:bb348c97df44 690
lypinator 0:bb348c97df44 691 if (ep->type == EP_TYPE_ISOC)
lypinator 0:bb348c97df44 692 {
lypinator 0:bb348c97df44 693 USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_MULCNT);
lypinator 0:bb348c97df44 694 USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_MULCNT & (1U << 29U));
lypinator 0:bb348c97df44 695 }
lypinator 0:bb348c97df44 696 }
lypinator 0:bb348c97df44 697
lypinator 0:bb348c97df44 698 if (dma == 1U)
lypinator 0:bb348c97df44 699 {
lypinator 0:bb348c97df44 700 USBx_INEP(ep->num)->DIEPDMA = (uint32_t)(ep->dma_addr);
lypinator 0:bb348c97df44 701 }
lypinator 0:bb348c97df44 702 else
lypinator 0:bb348c97df44 703 {
lypinator 0:bb348c97df44 704 if (ep->type != EP_TYPE_ISOC)
lypinator 0:bb348c97df44 705 {
lypinator 0:bb348c97df44 706 /* Enable the Tx FIFO Empty Interrupt for this EP */
lypinator 0:bb348c97df44 707 if (ep->xfer_len > 0U)
lypinator 0:bb348c97df44 708 {
lypinator 0:bb348c97df44 709 /* MBED */
lypinator 0:bb348c97df44 710 atomic_set_u32(&USBx_DEVICE->DIEPEMPMSK, 1U << ep->num);
lypinator 0:bb348c97df44 711 /* MBED */
lypinator 0:bb348c97df44 712 }
lypinator 0:bb348c97df44 713 }
lypinator 0:bb348c97df44 714 }
lypinator 0:bb348c97df44 715
lypinator 0:bb348c97df44 716 if (ep->type == EP_TYPE_ISOC)
lypinator 0:bb348c97df44 717 {
lypinator 0:bb348c97df44 718 if ((USBx_DEVICE->DSTS & ( 1U << 8U )) == 0U)
lypinator 0:bb348c97df44 719 {
lypinator 0:bb348c97df44 720 USBx_INEP(ep->num)->DIEPCTL |= USB_OTG_DIEPCTL_SODDFRM;
lypinator 0:bb348c97df44 721 }
lypinator 0:bb348c97df44 722 else
lypinator 0:bb348c97df44 723 {
lypinator 0:bb348c97df44 724 USBx_INEP(ep->num)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM;
lypinator 0:bb348c97df44 725 }
lypinator 0:bb348c97df44 726 }
lypinator 0:bb348c97df44 727
lypinator 0:bb348c97df44 728 /* EP enable, IN data in FIFO */
lypinator 0:bb348c97df44 729 USBx_INEP(ep->num)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
lypinator 0:bb348c97df44 730
lypinator 0:bb348c97df44 731 if (ep->type == EP_TYPE_ISOC)
lypinator 0:bb348c97df44 732 {
lypinator 0:bb348c97df44 733 USB_WritePacket(USBx, ep->xfer_buff, ep->num, ep->xfer_len, dma);
lypinator 0:bb348c97df44 734 }
lypinator 0:bb348c97df44 735 }
lypinator 0:bb348c97df44 736 else /* OUT endpoint */
lypinator 0:bb348c97df44 737 {
lypinator 0:bb348c97df44 738 /* Program the transfer size and packet count as follows:
lypinator 0:bb348c97df44 739 * pktcnt = N
lypinator 0:bb348c97df44 740 * xfersize = N * maxpacket
lypinator 0:bb348c97df44 741 */
lypinator 0:bb348c97df44 742 USBx_OUTEP(ep->num)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_XFRSIZ);
lypinator 0:bb348c97df44 743 USBx_OUTEP(ep->num)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_PKTCNT);
lypinator 0:bb348c97df44 744
lypinator 0:bb348c97df44 745 if (ep->xfer_len == 0U)
lypinator 0:bb348c97df44 746 {
lypinator 0:bb348c97df44 747 USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & ep->maxpacket);
lypinator 0:bb348c97df44 748 USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19U));
lypinator 0:bb348c97df44 749 }
lypinator 0:bb348c97df44 750 else
lypinator 0:bb348c97df44 751 {
lypinator 0:bb348c97df44 752 pktcnt = (ep->xfer_len + ep->maxpacket -1U)/ ep->maxpacket;
lypinator 0:bb348c97df44 753 USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (pktcnt << 19U));
lypinator 0:bb348c97df44 754 USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & (ep->maxpacket * pktcnt));
lypinator 0:bb348c97df44 755 }
lypinator 0:bb348c97df44 756
lypinator 0:bb348c97df44 757 if (dma == 1U)
lypinator 0:bb348c97df44 758 {
lypinator 0:bb348c97df44 759 USBx_OUTEP(ep->num)->DOEPDMA = (uint32_t)ep->xfer_buff;
lypinator 0:bb348c97df44 760 }
lypinator 0:bb348c97df44 761
lypinator 0:bb348c97df44 762 if (ep->type == EP_TYPE_ISOC)
lypinator 0:bb348c97df44 763 {
lypinator 0:bb348c97df44 764 if ((USBx_DEVICE->DSTS & ( 1U << 8U )) == 0U)
lypinator 0:bb348c97df44 765 {
lypinator 0:bb348c97df44 766 USBx_OUTEP(ep->num)->DOEPCTL |= USB_OTG_DOEPCTL_SODDFRM;
lypinator 0:bb348c97df44 767 }
lypinator 0:bb348c97df44 768 else
lypinator 0:bb348c97df44 769 {
lypinator 0:bb348c97df44 770 USBx_OUTEP(ep->num)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM;
lypinator 0:bb348c97df44 771 }
lypinator 0:bb348c97df44 772 }
lypinator 0:bb348c97df44 773 /* EP enable */
lypinator 0:bb348c97df44 774 USBx_OUTEP(ep->num)->DOEPCTL |= (USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
lypinator 0:bb348c97df44 775 }
lypinator 0:bb348c97df44 776 return HAL_OK;
lypinator 0:bb348c97df44 777 }
lypinator 0:bb348c97df44 778
lypinator 0:bb348c97df44 779 /**
lypinator 0:bb348c97df44 780 * @brief USB_EP0StartXfer : setup and starts a transfer over the EP 0
lypinator 0:bb348c97df44 781 * @param USBx Selected device
lypinator 0:bb348c97df44 782 * @param ep pointer to endpoint structure
lypinator 0:bb348c97df44 783 * @param dma USB dma enabled or disabled
lypinator 0:bb348c97df44 784 * This parameter can be one of these values:
lypinator 0:bb348c97df44 785 * 0 : DMA feature not used
lypinator 0:bb348c97df44 786 * 1 : DMA feature used
lypinator 0:bb348c97df44 787 * @retval HAL status
lypinator 0:bb348c97df44 788 */
lypinator 0:bb348c97df44 789 HAL_StatusTypeDef USB_EP0StartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDef *ep, uint8_t dma)
lypinator 0:bb348c97df44 790 {
lypinator 0:bb348c97df44 791 /* IN endpoint */
lypinator 0:bb348c97df44 792 if (ep->is_in == 1U)
lypinator 0:bb348c97df44 793 {
lypinator 0:bb348c97df44 794 /* Zero Length Packet? */
lypinator 0:bb348c97df44 795 if (ep->xfer_len == 0U)
lypinator 0:bb348c97df44 796 {
lypinator 0:bb348c97df44 797 USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
lypinator 0:bb348c97df44 798 USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1U << 19U)) ;
lypinator 0:bb348c97df44 799 USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
lypinator 0:bb348c97df44 800 }
lypinator 0:bb348c97df44 801 else
lypinator 0:bb348c97df44 802 {
lypinator 0:bb348c97df44 803 /* Program the transfer size and packet count
lypinator 0:bb348c97df44 804 * as follows: xfersize = N * maxpacket +
lypinator 0:bb348c97df44 805 * short_packet pktcnt = N + (short_packet
lypinator 0:bb348c97df44 806 * exist ? 1 : 0)
lypinator 0:bb348c97df44 807 */
lypinator 0:bb348c97df44 808 USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
lypinator 0:bb348c97df44 809 USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
lypinator 0:bb348c97df44 810
lypinator 0:bb348c97df44 811 if(ep->xfer_len > ep->maxpacket)
lypinator 0:bb348c97df44 812 {
lypinator 0:bb348c97df44 813 ep->xfer_len = ep->maxpacket;
lypinator 0:bb348c97df44 814 }
lypinator 0:bb348c97df44 815 USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1U << 19U)) ;
lypinator 0:bb348c97df44 816 USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_XFRSIZ & ep->xfer_len);
lypinator 0:bb348c97df44 817
lypinator 0:bb348c97df44 818 }
lypinator 0:bb348c97df44 819
lypinator 0:bb348c97df44 820 /* EP enable, IN data in FIFO */
lypinator 0:bb348c97df44 821 USBx_INEP(ep->num)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
lypinator 0:bb348c97df44 822
lypinator 0:bb348c97df44 823 if (dma == 1)
lypinator 0:bb348c97df44 824 {
lypinator 0:bb348c97df44 825 USBx_INEP(ep->num)->DIEPDMA = (uint32_t)(ep->dma_addr);
lypinator 0:bb348c97df44 826 }
lypinator 0:bb348c97df44 827 else
lypinator 0:bb348c97df44 828 {
lypinator 0:bb348c97df44 829 /* Enable the Tx FIFO Empty Interrupt for this EP */
lypinator 0:bb348c97df44 830 if (ep->xfer_len > 0U)
lypinator 0:bb348c97df44 831 {
lypinator 0:bb348c97df44 832 /* MBED */
lypinator 0:bb348c97df44 833 atomic_set_u32(&USBx_DEVICE->DIEPEMPMSK, 1U << (ep->num));
lypinator 0:bb348c97df44 834 /* MBED */
lypinator 0:bb348c97df44 835 }
lypinator 0:bb348c97df44 836 }
lypinator 0:bb348c97df44 837 }
lypinator 0:bb348c97df44 838
lypinator 0:bb348c97df44 839 else /* OUT endpoint */
lypinator 0:bb348c97df44 840 {
lypinator 0:bb348c97df44 841 /* Program the transfer size and packet count as follows:
lypinator 0:bb348c97df44 842 * pktcnt = N
lypinator 0:bb348c97df44 843 * xfersize = N * maxpacket
lypinator 0:bb348c97df44 844 */
lypinator 0:bb348c97df44 845 USBx_OUTEP(ep->num)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_XFRSIZ);
lypinator 0:bb348c97df44 846 USBx_OUTEP(ep->num)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_PKTCNT);
lypinator 0:bb348c97df44 847
lypinator 0:bb348c97df44 848 if (ep->xfer_len > 0U)
lypinator 0:bb348c97df44 849 {
lypinator 0:bb348c97df44 850 ep->xfer_len = ep->maxpacket;
lypinator 0:bb348c97df44 851 }
lypinator 0:bb348c97df44 852
lypinator 0:bb348c97df44 853 USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19U));
lypinator 0:bb348c97df44 854 USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & (ep->maxpacket));
lypinator 0:bb348c97df44 855
lypinator 0:bb348c97df44 856
lypinator 0:bb348c97df44 857 if (dma == 1U)
lypinator 0:bb348c97df44 858 {
lypinator 0:bb348c97df44 859 USBx_OUTEP(ep->num)->DOEPDMA = (uint32_t)(ep->xfer_buff);
lypinator 0:bb348c97df44 860 }
lypinator 0:bb348c97df44 861
lypinator 0:bb348c97df44 862 /* EP enable */
lypinator 0:bb348c97df44 863 USBx_OUTEP(ep->num)->DOEPCTL |= (USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
lypinator 0:bb348c97df44 864 }
lypinator 0:bb348c97df44 865 return HAL_OK;
lypinator 0:bb348c97df44 866 }
lypinator 0:bb348c97df44 867
lypinator 0:bb348c97df44 868 /**
lypinator 0:bb348c97df44 869 * @brief USB_WritePacket : Writes a packet into the Tx FIFO associated
lypinator 0:bb348c97df44 870 * with the EP/channel
lypinator 0:bb348c97df44 871 * @param USBx Selected device
lypinator 0:bb348c97df44 872 * @param src pointer to source buffer
lypinator 0:bb348c97df44 873 * @param ch_ep_num endpoint or host channel number
lypinator 0:bb348c97df44 874 * @param len Number of bytes to write
lypinator 0:bb348c97df44 875 * @param dma USB dma enabled or disabled
lypinator 0:bb348c97df44 876 * This parameter can be one of these values:
lypinator 0:bb348c97df44 877 * 0 : DMA feature not used
lypinator 0:bb348c97df44 878 * 1 : DMA feature used
lypinator 0:bb348c97df44 879 * @retval HAL status
lypinator 0:bb348c97df44 880 */
lypinator 0:bb348c97df44 881 HAL_StatusTypeDef USB_WritePacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *src, uint8_t ch_ep_num, uint16_t len, uint8_t dma)
lypinator 0:bb348c97df44 882 {
lypinator 0:bb348c97df44 883 uint32_t count32b = 0U , i = 0U;
lypinator 0:bb348c97df44 884
lypinator 0:bb348c97df44 885 if (dma == 0U)
lypinator 0:bb348c97df44 886 {
lypinator 0:bb348c97df44 887 count32b = (len + 3U) / 4U;
lypinator 0:bb348c97df44 888 for (i = 0U; i < count32b; i++, src += 4U)
lypinator 0:bb348c97df44 889 {
lypinator 0:bb348c97df44 890 USBx_DFIFO(ch_ep_num) = *((__packed uint32_t *)src);
lypinator 0:bb348c97df44 891 }
lypinator 0:bb348c97df44 892 }
lypinator 0:bb348c97df44 893 return HAL_OK;
lypinator 0:bb348c97df44 894 }
lypinator 0:bb348c97df44 895
lypinator 0:bb348c97df44 896 /**
lypinator 0:bb348c97df44 897 * @brief USB_ReadPacket : read a packet from the Tx FIFO associated
lypinator 0:bb348c97df44 898 * with the EP/channel
lypinator 0:bb348c97df44 899 * @param USBx Selected device
lypinator 0:bb348c97df44 900 * @param src source pointer
lypinator 0:bb348c97df44 901 * @param ch_ep_num endpoint or host channel number
lypinator 0:bb348c97df44 902 * @param len Number of bytes to read
lypinator 0:bb348c97df44 903 * @param dma USB dma enabled or disabled
lypinator 0:bb348c97df44 904 * This parameter can be one of these values:
lypinator 0:bb348c97df44 905 * 0 : DMA feature not used
lypinator 0:bb348c97df44 906 * 1 : DMA feature used
lypinator 0:bb348c97df44 907 * @retval pointer to destination buffer
lypinator 0:bb348c97df44 908 */
lypinator 0:bb348c97df44 909 void *USB_ReadPacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *dest, uint16_t len)
lypinator 0:bb348c97df44 910 {
lypinator 0:bb348c97df44 911 uint32_t i=0U;
lypinator 0:bb348c97df44 912 uint32_t count32b = (len + 3U) / 4U;
lypinator 0:bb348c97df44 913
lypinator 0:bb348c97df44 914 for ( i = 0U; i < count32b; i++, dest += 4U )
lypinator 0:bb348c97df44 915 {
lypinator 0:bb348c97df44 916 *(__packed uint32_t *)dest = USBx_DFIFO(0U);
lypinator 0:bb348c97df44 917
lypinator 0:bb348c97df44 918 }
lypinator 0:bb348c97df44 919 return ((void *)dest);
lypinator 0:bb348c97df44 920 }
lypinator 0:bb348c97df44 921
lypinator 0:bb348c97df44 922 /**
lypinator 0:bb348c97df44 923 * @brief USB_EPSetStall : set a stall condition over an EP
lypinator 0:bb348c97df44 924 * @param USBx Selected device
lypinator 0:bb348c97df44 925 * @param ep pointer to endpoint structure
lypinator 0:bb348c97df44 926 * @retval HAL status
lypinator 0:bb348c97df44 927 */
lypinator 0:bb348c97df44 928 HAL_StatusTypeDef USB_EPSetStall(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDef *ep)
lypinator 0:bb348c97df44 929 {
lypinator 0:bb348c97df44 930 if (ep->is_in == 1U)
lypinator 0:bb348c97df44 931 {
lypinator 0:bb348c97df44 932 if (((USBx_INEP(ep->num)->DIEPCTL) & USB_OTG_DIEPCTL_EPENA) == 0U)
lypinator 0:bb348c97df44 933 {
lypinator 0:bb348c97df44 934 USBx_INEP(ep->num)->DIEPCTL &= ~(USB_OTG_DIEPCTL_EPDIS);
lypinator 0:bb348c97df44 935 }
lypinator 0:bb348c97df44 936 USBx_INEP(ep->num)->DIEPCTL |= USB_OTG_DIEPCTL_STALL;
lypinator 0:bb348c97df44 937 }
lypinator 0:bb348c97df44 938 else
lypinator 0:bb348c97df44 939 {
lypinator 0:bb348c97df44 940 if (((USBx_OUTEP(ep->num)->DOEPCTL) & USB_OTG_DOEPCTL_EPENA) == 0U)
lypinator 0:bb348c97df44 941 {
lypinator 0:bb348c97df44 942 USBx_OUTEP(ep->num)->DOEPCTL &= ~(USB_OTG_DOEPCTL_EPDIS);
lypinator 0:bb348c97df44 943 }
lypinator 0:bb348c97df44 944 USBx_OUTEP(ep->num)->DOEPCTL |= USB_OTG_DOEPCTL_STALL;
lypinator 0:bb348c97df44 945 }
lypinator 0:bb348c97df44 946 return HAL_OK;
lypinator 0:bb348c97df44 947 }
lypinator 0:bb348c97df44 948
lypinator 0:bb348c97df44 949
lypinator 0:bb348c97df44 950 /**
lypinator 0:bb348c97df44 951 * @brief USB_EPClearStall : Clear a stall condition over an EP
lypinator 0:bb348c97df44 952 * @param USBx Selected device
lypinator 0:bb348c97df44 953 * @param ep pointer to endpoint structure
lypinator 0:bb348c97df44 954 * @retval HAL status
lypinator 0:bb348c97df44 955 */
lypinator 0:bb348c97df44 956 HAL_StatusTypeDef USB_EPClearStall(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
lypinator 0:bb348c97df44 957 {
lypinator 0:bb348c97df44 958 if (ep->is_in == 1U)
lypinator 0:bb348c97df44 959 {
lypinator 0:bb348c97df44 960 USBx_INEP(ep->num)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
lypinator 0:bb348c97df44 961 if (ep->type == EP_TYPE_INTR || ep->type == EP_TYPE_BULK)
lypinator 0:bb348c97df44 962 {
lypinator 0:bb348c97df44 963 USBx_INEP(ep->num)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM; /* DATA0 */
lypinator 0:bb348c97df44 964 }
lypinator 0:bb348c97df44 965 }
lypinator 0:bb348c97df44 966 else
lypinator 0:bb348c97df44 967 {
lypinator 0:bb348c97df44 968 USBx_OUTEP(ep->num)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
lypinator 0:bb348c97df44 969 if (ep->type == EP_TYPE_INTR || ep->type == EP_TYPE_BULK)
lypinator 0:bb348c97df44 970 {
lypinator 0:bb348c97df44 971 USBx_OUTEP(ep->num)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM; /* DATA0 */
lypinator 0:bb348c97df44 972 }
lypinator 0:bb348c97df44 973 }
lypinator 0:bb348c97df44 974 return HAL_OK;
lypinator 0:bb348c97df44 975 }
lypinator 0:bb348c97df44 976
lypinator 0:bb348c97df44 977 /**
lypinator 0:bb348c97df44 978 * @brief USB_StopDevice : Stop the usb device mode
lypinator 0:bb348c97df44 979 * @param USBx Selected device
lypinator 0:bb348c97df44 980 * @retval HAL status
lypinator 0:bb348c97df44 981 */
lypinator 0:bb348c97df44 982 HAL_StatusTypeDef USB_StopDevice(USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 983 {
lypinator 0:bb348c97df44 984 uint32_t i;
lypinator 0:bb348c97df44 985
lypinator 0:bb348c97df44 986 /* Clear Pending interrupt */
lypinator 0:bb348c97df44 987 for (i = 0U; i < 15U ; i++)
lypinator 0:bb348c97df44 988 {
lypinator 0:bb348c97df44 989 USBx_INEP(i)->DIEPINT = 0xFFU;
lypinator 0:bb348c97df44 990 USBx_OUTEP(i)->DOEPINT = 0xFFU;
lypinator 0:bb348c97df44 991 }
lypinator 0:bb348c97df44 992 USBx_DEVICE->DAINT = 0xFFFFFFFFU;
lypinator 0:bb348c97df44 993
lypinator 0:bb348c97df44 994 /* Clear interrupt masks */
lypinator 0:bb348c97df44 995 USBx_DEVICE->DIEPMSK = 0U;
lypinator 0:bb348c97df44 996 USBx_DEVICE->DOEPMSK = 0U;
lypinator 0:bb348c97df44 997 USBx_DEVICE->DAINTMSK = 0U;
lypinator 0:bb348c97df44 998
lypinator 0:bb348c97df44 999 /* Flush the FIFO */
lypinator 0:bb348c97df44 1000 USB_FlushRxFifo(USBx);
lypinator 0:bb348c97df44 1001 USB_FlushTxFifo(USBx , 0x10U);
lypinator 0:bb348c97df44 1002
lypinator 0:bb348c97df44 1003 return HAL_OK;
lypinator 0:bb348c97df44 1004 }
lypinator 0:bb348c97df44 1005
lypinator 0:bb348c97df44 1006 /**
lypinator 0:bb348c97df44 1007 * @brief USB_SetDevAddress : Stop the usb device mode
lypinator 0:bb348c97df44 1008 * @param USBx Selected device
lypinator 0:bb348c97df44 1009 * @param address new device address to be assigned
lypinator 0:bb348c97df44 1010 * This parameter can be a value from 0 to 255
lypinator 0:bb348c97df44 1011 * @retval HAL status
lypinator 0:bb348c97df44 1012 */
lypinator 0:bb348c97df44 1013 HAL_StatusTypeDef USB_SetDevAddress (USB_OTG_GlobalTypeDef *USBx, uint8_t address)
lypinator 0:bb348c97df44 1014 {
lypinator 0:bb348c97df44 1015 USBx_DEVICE->DCFG &= ~ (USB_OTG_DCFG_DAD);
lypinator 0:bb348c97df44 1016 USBx_DEVICE->DCFG |= (address << 4U) & USB_OTG_DCFG_DAD ;
lypinator 0:bb348c97df44 1017
lypinator 0:bb348c97df44 1018 return HAL_OK;
lypinator 0:bb348c97df44 1019 }
lypinator 0:bb348c97df44 1020
lypinator 0:bb348c97df44 1021 /**
lypinator 0:bb348c97df44 1022 * @brief USB_DevConnect : Connect the USB device by enabling the pull-up/pull-down
lypinator 0:bb348c97df44 1023 * @param USBx Selected device
lypinator 0:bb348c97df44 1024 * @retval HAL status
lypinator 0:bb348c97df44 1025 */
lypinator 0:bb348c97df44 1026 HAL_StatusTypeDef USB_DevConnect (USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 1027 {
lypinator 0:bb348c97df44 1028 USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_SDIS ;
lypinator 0:bb348c97df44 1029 HAL_Delay(3U);
lypinator 0:bb348c97df44 1030
lypinator 0:bb348c97df44 1031 return HAL_OK;
lypinator 0:bb348c97df44 1032 }
lypinator 0:bb348c97df44 1033
lypinator 0:bb348c97df44 1034 /**
lypinator 0:bb348c97df44 1035 * @brief USB_DevDisconnect : Disconnect the USB device by disabling the pull-up/pull-down
lypinator 0:bb348c97df44 1036 * @param USBx Selected device
lypinator 0:bb348c97df44 1037 * @retval HAL status
lypinator 0:bb348c97df44 1038 */
lypinator 0:bb348c97df44 1039 HAL_StatusTypeDef USB_DevDisconnect (USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 1040 {
lypinator 0:bb348c97df44 1041 USBx_DEVICE->DCTL |= USB_OTG_DCTL_SDIS ;
lypinator 0:bb348c97df44 1042 HAL_Delay(3U);
lypinator 0:bb348c97df44 1043
lypinator 0:bb348c97df44 1044 return HAL_OK;
lypinator 0:bb348c97df44 1045 }
lypinator 0:bb348c97df44 1046
lypinator 0:bb348c97df44 1047 /**
lypinator 0:bb348c97df44 1048 * @brief USB_ReadInterrupts: return the global USB interrupt status
lypinator 0:bb348c97df44 1049 * @param USBx Selected device
lypinator 0:bb348c97df44 1050 * @retval HAL status
lypinator 0:bb348c97df44 1051 */
lypinator 0:bb348c97df44 1052 uint32_t USB_ReadInterrupts (USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 1053 {
lypinator 0:bb348c97df44 1054 uint32_t v = 0U;
lypinator 0:bb348c97df44 1055
lypinator 0:bb348c97df44 1056 v = USBx->GINTSTS;
lypinator 0:bb348c97df44 1057 v &= USBx->GINTMSK;
lypinator 0:bb348c97df44 1058 return v;
lypinator 0:bb348c97df44 1059 }
lypinator 0:bb348c97df44 1060
lypinator 0:bb348c97df44 1061 /**
lypinator 0:bb348c97df44 1062 * @brief USB_ReadDevAllOutEpInterrupt: return the USB device OUT endpoints interrupt status
lypinator 0:bb348c97df44 1063 * @param USBx Selected device
lypinator 0:bb348c97df44 1064 * @retval HAL status
lypinator 0:bb348c97df44 1065 */
lypinator 0:bb348c97df44 1066 uint32_t USB_ReadDevAllOutEpInterrupt (USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 1067 {
lypinator 0:bb348c97df44 1068 uint32_t v;
lypinator 0:bb348c97df44 1069 v = USBx_DEVICE->DAINT;
lypinator 0:bb348c97df44 1070 v &= USBx_DEVICE->DAINTMSK;
lypinator 0:bb348c97df44 1071 return ((v & 0xffff0000U) >> 16U);
lypinator 0:bb348c97df44 1072 }
lypinator 0:bb348c97df44 1073
lypinator 0:bb348c97df44 1074 /**
lypinator 0:bb348c97df44 1075 * @brief USB_ReadDevAllInEpInterrupt: return the USB device IN endpoints interrupt status
lypinator 0:bb348c97df44 1076 * @param USBx Selected device
lypinator 0:bb348c97df44 1077 * @retval HAL status
lypinator 0:bb348c97df44 1078 */
lypinator 0:bb348c97df44 1079 uint32_t USB_ReadDevAllInEpInterrupt (USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 1080 {
lypinator 0:bb348c97df44 1081 uint32_t v;
lypinator 0:bb348c97df44 1082 v = USBx_DEVICE->DAINT;
lypinator 0:bb348c97df44 1083 v &= USBx_DEVICE->DAINTMSK;
lypinator 0:bb348c97df44 1084 return ((v & 0xFFFFU));
lypinator 0:bb348c97df44 1085 }
lypinator 0:bb348c97df44 1086
lypinator 0:bb348c97df44 1087 /**
lypinator 0:bb348c97df44 1088 * @brief Returns Device OUT EP Interrupt register
lypinator 0:bb348c97df44 1089 * @param USBx Selected device
lypinator 0:bb348c97df44 1090 * @param epnum endpoint number
lypinator 0:bb348c97df44 1091 * This parameter can be a value from 0 to 15
lypinator 0:bb348c97df44 1092 * @retval Device OUT EP Interrupt register
lypinator 0:bb348c97df44 1093 */
lypinator 0:bb348c97df44 1094 uint32_t USB_ReadDevOutEPInterrupt (USB_OTG_GlobalTypeDef *USBx , uint8_t epnum)
lypinator 0:bb348c97df44 1095 {
lypinator 0:bb348c97df44 1096 uint32_t v;
lypinator 0:bb348c97df44 1097 v = USBx_OUTEP(epnum)->DOEPINT;
lypinator 0:bb348c97df44 1098 v &= USBx_DEVICE->DOEPMSK;
lypinator 0:bb348c97df44 1099 return v;
lypinator 0:bb348c97df44 1100 }
lypinator 0:bb348c97df44 1101
lypinator 0:bb348c97df44 1102 /**
lypinator 0:bb348c97df44 1103 * @brief Returns Device IN EP Interrupt register
lypinator 0:bb348c97df44 1104 * @param USBx Selected device
lypinator 0:bb348c97df44 1105 * @param epnum endpoint number
lypinator 0:bb348c97df44 1106 * This parameter can be a value from 0 to 15
lypinator 0:bb348c97df44 1107 * @retval Device IN EP Interrupt register
lypinator 0:bb348c97df44 1108 */
lypinator 0:bb348c97df44 1109 uint32_t USB_ReadDevInEPInterrupt (USB_OTG_GlobalTypeDef *USBx , uint8_t epnum)
lypinator 0:bb348c97df44 1110 {
lypinator 0:bb348c97df44 1111 uint32_t v, msk, emp;
lypinator 0:bb348c97df44 1112
lypinator 0:bb348c97df44 1113 msk = USBx_DEVICE->DIEPMSK;
lypinator 0:bb348c97df44 1114 emp = USBx_DEVICE->DIEPEMPMSK;
lypinator 0:bb348c97df44 1115 msk |= ((emp >> epnum) & 0x1U) << 7U;
lypinator 0:bb348c97df44 1116 v = USBx_INEP(epnum)->DIEPINT & msk;
lypinator 0:bb348c97df44 1117 return v;
lypinator 0:bb348c97df44 1118 }
lypinator 0:bb348c97df44 1119
lypinator 0:bb348c97df44 1120 /**
lypinator 0:bb348c97df44 1121 * @brief USB_ClearInterrupts: clear a USB interrupt
lypinator 0:bb348c97df44 1122 * @param USBx Selected device
lypinator 0:bb348c97df44 1123 * @param interrupt interrupt flag
lypinator 0:bb348c97df44 1124 * @retval None
lypinator 0:bb348c97df44 1125 */
lypinator 0:bb348c97df44 1126 void USB_ClearInterrupts (USB_OTG_GlobalTypeDef *USBx, uint32_t interrupt)
lypinator 0:bb348c97df44 1127 {
lypinator 0:bb348c97df44 1128 USBx->GINTSTS |= interrupt;
lypinator 0:bb348c97df44 1129 }
lypinator 0:bb348c97df44 1130
lypinator 0:bb348c97df44 1131 /**
lypinator 0:bb348c97df44 1132 * @brief Returns USB core mode
lypinator 0:bb348c97df44 1133 * @param USBx Selected device
lypinator 0:bb348c97df44 1134 * @retval return core mode : Host or Device
lypinator 0:bb348c97df44 1135 * This parameter can be one of these values:
lypinator 0:bb348c97df44 1136 * 0 : Host
lypinator 0:bb348c97df44 1137 * 1 : Device
lypinator 0:bb348c97df44 1138 */
lypinator 0:bb348c97df44 1139 uint32_t USB_GetMode(USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 1140 {
lypinator 0:bb348c97df44 1141 return ((USBx->GINTSTS ) & 0x1U);
lypinator 0:bb348c97df44 1142 }
lypinator 0:bb348c97df44 1143
lypinator 0:bb348c97df44 1144
lypinator 0:bb348c97df44 1145 /**
lypinator 0:bb348c97df44 1146 * @brief Activate EP0 for Setup transactions
lypinator 0:bb348c97df44 1147 * @param USBx Selected device
lypinator 0:bb348c97df44 1148 * @retval HAL status
lypinator 0:bb348c97df44 1149 */
lypinator 0:bb348c97df44 1150 HAL_StatusTypeDef USB_ActivateSetup (USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 1151 {
lypinator 0:bb348c97df44 1152 /* Set the MPS of the IN EP based on the enumeration speed */
lypinator 0:bb348c97df44 1153 USBx_INEP(0U)->DIEPCTL &= ~USB_OTG_DIEPCTL_MPSIZ;
lypinator 0:bb348c97df44 1154
lypinator 0:bb348c97df44 1155 if((USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD) == DSTS_ENUMSPD_LS_PHY_6MHZ)
lypinator 0:bb348c97df44 1156 {
lypinator 0:bb348c97df44 1157 USBx_INEP(0U)->DIEPCTL |= 3U;
lypinator 0:bb348c97df44 1158 }
lypinator 0:bb348c97df44 1159 USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGINAK;
lypinator 0:bb348c97df44 1160
lypinator 0:bb348c97df44 1161 return HAL_OK;
lypinator 0:bb348c97df44 1162 }
lypinator 0:bb348c97df44 1163
lypinator 0:bb348c97df44 1164
lypinator 0:bb348c97df44 1165 /**
lypinator 0:bb348c97df44 1166 * @brief Prepare the EP0 to start the first control setup
lypinator 0:bb348c97df44 1167 * @param USBx Selected device
lypinator 0:bb348c97df44 1168 * @param dma USB dma enabled or disabled
lypinator 0:bb348c97df44 1169 * This parameter can be one of these values:
lypinator 0:bb348c97df44 1170 * 0 : DMA feature not used
lypinator 0:bb348c97df44 1171 * 1 : DMA feature used
lypinator 0:bb348c97df44 1172 * @param psetup pointer to setup packet
lypinator 0:bb348c97df44 1173 * @retval HAL status
lypinator 0:bb348c97df44 1174 */
lypinator 0:bb348c97df44 1175 HAL_StatusTypeDef USB_EP0_OutStart(USB_OTG_GlobalTypeDef *USBx, uint8_t dma, uint8_t *psetup)
lypinator 0:bb348c97df44 1176 {
lypinator 0:bb348c97df44 1177 USBx_OUTEP(0U)->DOEPTSIZ = 0U;
lypinator 0:bb348c97df44 1178 USBx_OUTEP(0U)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19U)) ;
lypinator 0:bb348c97df44 1179 USBx_OUTEP(0U)->DOEPTSIZ |= (3U * 8U);
lypinator 0:bb348c97df44 1180 USBx_OUTEP(0U)->DOEPTSIZ |= USB_OTG_DOEPTSIZ_STUPCNT;
lypinator 0:bb348c97df44 1181
lypinator 0:bb348c97df44 1182 if (dma == 1U)
lypinator 0:bb348c97df44 1183 {
lypinator 0:bb348c97df44 1184 USBx_OUTEP(0U)->DOEPDMA = (uint32_t)psetup;
lypinator 0:bb348c97df44 1185 /* EP enable */
lypinator 0:bb348c97df44 1186 USBx_OUTEP(0U)->DOEPCTL = 0x80008000U;
lypinator 0:bb348c97df44 1187 }
lypinator 0:bb348c97df44 1188
lypinator 0:bb348c97df44 1189 return HAL_OK;
lypinator 0:bb348c97df44 1190 }
lypinator 0:bb348c97df44 1191
lypinator 0:bb348c97df44 1192
lypinator 0:bb348c97df44 1193 /**
lypinator 0:bb348c97df44 1194 * @brief Reset the USB Core (needed after USB clock settings change)
lypinator 0:bb348c97df44 1195 * @param USBx Selected device
lypinator 0:bb348c97df44 1196 * @retval HAL status
lypinator 0:bb348c97df44 1197 */
lypinator 0:bb348c97df44 1198 static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 1199 {
lypinator 0:bb348c97df44 1200 uint32_t count = 0U;
lypinator 0:bb348c97df44 1201
lypinator 0:bb348c97df44 1202 /* Wait for AHB master IDLE state. */
lypinator 0:bb348c97df44 1203 do
lypinator 0:bb348c97df44 1204 {
lypinator 0:bb348c97df44 1205 if (++count > 200000U)
lypinator 0:bb348c97df44 1206 {
lypinator 0:bb348c97df44 1207 return HAL_TIMEOUT;
lypinator 0:bb348c97df44 1208 }
lypinator 0:bb348c97df44 1209 }
lypinator 0:bb348c97df44 1210 while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
lypinator 0:bb348c97df44 1211
lypinator 0:bb348c97df44 1212 /* Core Soft Reset */
lypinator 0:bb348c97df44 1213 count = 0U;
lypinator 0:bb348c97df44 1214 USBx->GRSTCTL |= USB_OTG_GRSTCTL_CSRST;
lypinator 0:bb348c97df44 1215
lypinator 0:bb348c97df44 1216 do
lypinator 0:bb348c97df44 1217 {
lypinator 0:bb348c97df44 1218 if (++count > 200000U)
lypinator 0:bb348c97df44 1219 {
lypinator 0:bb348c97df44 1220 return HAL_TIMEOUT;
lypinator 0:bb348c97df44 1221 }
lypinator 0:bb348c97df44 1222 }
lypinator 0:bb348c97df44 1223 while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_CSRST) == USB_OTG_GRSTCTL_CSRST);
lypinator 0:bb348c97df44 1224
lypinator 0:bb348c97df44 1225 return HAL_OK;
lypinator 0:bb348c97df44 1226 }
lypinator 0:bb348c97df44 1227
lypinator 0:bb348c97df44 1228
lypinator 0:bb348c97df44 1229 /**
lypinator 0:bb348c97df44 1230 * @brief USB_HostInit : Initializes the USB OTG controller registers
lypinator 0:bb348c97df44 1231 * for Host mode
lypinator 0:bb348c97df44 1232 * @param USBx Selected device
lypinator 0:bb348c97df44 1233 * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
lypinator 0:bb348c97df44 1234 * the configuration information for the specified USBx peripheral.
lypinator 0:bb348c97df44 1235 * @retval HAL status
lypinator 0:bb348c97df44 1236 */
lypinator 0:bb348c97df44 1237 HAL_StatusTypeDef USB_HostInit (USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
lypinator 0:bb348c97df44 1238 {
lypinator 0:bb348c97df44 1239 uint32_t i;
lypinator 0:bb348c97df44 1240
lypinator 0:bb348c97df44 1241 /* Restart the Phy Clock */
lypinator 0:bb348c97df44 1242 USBx_PCGCCTL = 0U;
lypinator 0:bb348c97df44 1243
lypinator 0:bb348c97df44 1244 /* Activate VBUS Sensing B */
lypinator 0:bb348c97df44 1245 #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
lypinator 0:bb348c97df44 1246 defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
lypinator 0:bb348c97df44 1247 USBx->GCCFG |= USB_OTG_GCCFG_VBDEN;
lypinator 0:bb348c97df44 1248 #else
lypinator 0:bb348c97df44 1249 USBx->GCCFG &=~ (USB_OTG_GCCFG_VBUSASEN);
lypinator 0:bb348c97df44 1250 USBx->GCCFG &=~ (USB_OTG_GCCFG_VBUSBSEN);
lypinator 0:bb348c97df44 1251 USBx->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS;
lypinator 0:bb348c97df44 1252 #endif /* STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Rx || STM32F412Vx || STM32F412Cx || STM32F413xx || STM32F423xx */
lypinator 0:bb348c97df44 1253
lypinator 0:bb348c97df44 1254 /* Disable the FS/LS support mode only */
lypinator 0:bb348c97df44 1255 if((cfg.speed == USB_OTG_SPEED_FULL)&&
lypinator 0:bb348c97df44 1256 (USBx != USB_OTG_FS))
lypinator 0:bb348c97df44 1257 {
lypinator 0:bb348c97df44 1258 USBx_HOST->HCFG |= USB_OTG_HCFG_FSLSS;
lypinator 0:bb348c97df44 1259 }
lypinator 0:bb348c97df44 1260 else
lypinator 0:bb348c97df44 1261 {
lypinator 0:bb348c97df44 1262 USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSS);
lypinator 0:bb348c97df44 1263 }
lypinator 0:bb348c97df44 1264
lypinator 0:bb348c97df44 1265 /* Make sure the FIFOs are flushed. */
lypinator 0:bb348c97df44 1266 USB_FlushTxFifo(USBx, 0x10U); /* all Tx FIFOs */
lypinator 0:bb348c97df44 1267 USB_FlushRxFifo(USBx);
lypinator 0:bb348c97df44 1268
lypinator 0:bb348c97df44 1269 /* Clear all pending HC Interrupts */
lypinator 0:bb348c97df44 1270 for (i = 0U; i < cfg.Host_channels; i++)
lypinator 0:bb348c97df44 1271 {
lypinator 0:bb348c97df44 1272 USBx_HC(i)->HCINT = 0xFFFFFFFFU;
lypinator 0:bb348c97df44 1273 USBx_HC(i)->HCINTMSK = 0U;
lypinator 0:bb348c97df44 1274 }
lypinator 0:bb348c97df44 1275
lypinator 0:bb348c97df44 1276 /* Enable VBUS driving */
lypinator 0:bb348c97df44 1277 USB_DriveVbus(USBx, 1U);
lypinator 0:bb348c97df44 1278
lypinator 0:bb348c97df44 1279 HAL_Delay(200U);
lypinator 0:bb348c97df44 1280
lypinator 0:bb348c97df44 1281 /* Disable all interrupts. */
lypinator 0:bb348c97df44 1282 USBx->GINTMSK = 0U;
lypinator 0:bb348c97df44 1283
lypinator 0:bb348c97df44 1284 /* Clear any pending interrupts */
lypinator 0:bb348c97df44 1285 USBx->GINTSTS = 0xFFFFFFFFU;
lypinator 0:bb348c97df44 1286
lypinator 0:bb348c97df44 1287 if(USBx == USB_OTG_FS)
lypinator 0:bb348c97df44 1288 {
lypinator 0:bb348c97df44 1289 /* set Rx FIFO size */
lypinator 0:bb348c97df44 1290 USBx->GRXFSIZ = 0x80U;
lypinator 0:bb348c97df44 1291 USBx->DIEPTXF0_HNPTXFSIZ = (uint32_t )(((0x60U << 16U)& USB_OTG_NPTXFD) | 0x80U);
lypinator 0:bb348c97df44 1292 USBx->HPTXFSIZ = (uint32_t )(((0x40U << 16U)& USB_OTG_HPTXFSIZ_PTXFD) | 0xE0U);
lypinator 0:bb348c97df44 1293 }
lypinator 0:bb348c97df44 1294 else
lypinator 0:bb348c97df44 1295 {
lypinator 0:bb348c97df44 1296 /* set Rx FIFO size */
lypinator 0:bb348c97df44 1297 USBx->GRXFSIZ = 0x200U;
lypinator 0:bb348c97df44 1298 USBx->DIEPTXF0_HNPTXFSIZ = (uint32_t )(((0x100U << 16U)& USB_OTG_NPTXFD) | 0x200U);
lypinator 0:bb348c97df44 1299 USBx->HPTXFSIZ = (uint32_t )(((0xE0U << 16U)& USB_OTG_HPTXFSIZ_PTXFD) | 0x300U);
lypinator 0:bb348c97df44 1300 }
lypinator 0:bb348c97df44 1301
lypinator 0:bb348c97df44 1302 /* Enable the common interrupts */
lypinator 0:bb348c97df44 1303 if (cfg.dma_enable == DISABLE)
lypinator 0:bb348c97df44 1304 {
lypinator 0:bb348c97df44 1305 USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
lypinator 0:bb348c97df44 1306 }
lypinator 0:bb348c97df44 1307
lypinator 0:bb348c97df44 1308 /* Enable interrupts matching to the Host mode ONLY */
lypinator 0:bb348c97df44 1309 USBx->GINTMSK |= (USB_OTG_GINTMSK_PRTIM | USB_OTG_GINTMSK_HCIM |\
lypinator 0:bb348c97df44 1310 USB_OTG_GINTMSK_SOFM |USB_OTG_GINTSTS_DISCINT|\
lypinator 0:bb348c97df44 1311 USB_OTG_GINTMSK_PXFRM_IISOOXFRM | USB_OTG_GINTMSK_WUIM);
lypinator 0:bb348c97df44 1312
lypinator 0:bb348c97df44 1313 return HAL_OK;
lypinator 0:bb348c97df44 1314 }
lypinator 0:bb348c97df44 1315
lypinator 0:bb348c97df44 1316 /**
lypinator 0:bb348c97df44 1317 * @brief USB_InitFSLSPClkSel : Initializes the FSLSPClkSel field of the
lypinator 0:bb348c97df44 1318 * HCFG register on the PHY type and set the right frame interval
lypinator 0:bb348c97df44 1319 * @param USBx Selected device
lypinator 0:bb348c97df44 1320 * @param freq clock frequency
lypinator 0:bb348c97df44 1321 * This parameter can be one of these values:
lypinator 0:bb348c97df44 1322 * HCFG_48_MHZ : Full Speed 48 MHz Clock
lypinator 0:bb348c97df44 1323 * HCFG_6_MHZ : Low Speed 6 MHz Clock
lypinator 0:bb348c97df44 1324 * @retval HAL status
lypinator 0:bb348c97df44 1325 */
lypinator 0:bb348c97df44 1326 HAL_StatusTypeDef USB_InitFSLSPClkSel(USB_OTG_GlobalTypeDef *USBx , uint8_t freq)
lypinator 0:bb348c97df44 1327 {
lypinator 0:bb348c97df44 1328 USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSPCS);
lypinator 0:bb348c97df44 1329 USBx_HOST->HCFG |= (freq & USB_OTG_HCFG_FSLSPCS);
lypinator 0:bb348c97df44 1330
lypinator 0:bb348c97df44 1331 if (freq == HCFG_48_MHZ)
lypinator 0:bb348c97df44 1332 {
lypinator 0:bb348c97df44 1333 USBx_HOST->HFIR = 48000U;
lypinator 0:bb348c97df44 1334 }
lypinator 0:bb348c97df44 1335 else if (freq == HCFG_6_MHZ)
lypinator 0:bb348c97df44 1336 {
lypinator 0:bb348c97df44 1337 USBx_HOST->HFIR = 6000U;
lypinator 0:bb348c97df44 1338 }
lypinator 0:bb348c97df44 1339 return HAL_OK;
lypinator 0:bb348c97df44 1340 }
lypinator 0:bb348c97df44 1341
lypinator 0:bb348c97df44 1342 /**
lypinator 0:bb348c97df44 1343 * @brief USB_OTG_ResetPort : Reset Host Port
lypinator 0:bb348c97df44 1344 * @param USBx Selected device
lypinator 0:bb348c97df44 1345 * @retval HAL status
lypinator 0:bb348c97df44 1346 * @note (1)The application must wait at least 10 ms
lypinator 0:bb348c97df44 1347 * before clearing the reset bit.
lypinator 0:bb348c97df44 1348 */
lypinator 0:bb348c97df44 1349 HAL_StatusTypeDef USB_ResetPort(USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 1350 {
lypinator 0:bb348c97df44 1351 __IO uint32_t hprt0;
lypinator 0:bb348c97df44 1352
lypinator 0:bb348c97df44 1353 hprt0 = USBx_HPRT0;
lypinator 0:bb348c97df44 1354
lypinator 0:bb348c97df44 1355 hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
lypinator 0:bb348c97df44 1356 USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
lypinator 0:bb348c97df44 1357
lypinator 0:bb348c97df44 1358 USBx_HPRT0 = (USB_OTG_HPRT_PRST | hprt0);
lypinator 0:bb348c97df44 1359 HAL_Delay (10U); /* See Note #1 */
lypinator 0:bb348c97df44 1360 USBx_HPRT0 = ((~USB_OTG_HPRT_PRST) & hprt0);
lypinator 0:bb348c97df44 1361 return HAL_OK;
lypinator 0:bb348c97df44 1362 }
lypinator 0:bb348c97df44 1363
lypinator 0:bb348c97df44 1364 /**
lypinator 0:bb348c97df44 1365 * @brief USB_DriveVbus : activate or de-activate vbus
lypinator 0:bb348c97df44 1366 * @param state VBUS state
lypinator 0:bb348c97df44 1367 * This parameter can be one of these values:
lypinator 0:bb348c97df44 1368 * 0 : VBUS Active
lypinator 0:bb348c97df44 1369 * 1 : VBUS Inactive
lypinator 0:bb348c97df44 1370 * @retval HAL status
lypinator 0:bb348c97df44 1371 */
lypinator 0:bb348c97df44 1372 HAL_StatusTypeDef USB_DriveVbus (USB_OTG_GlobalTypeDef *USBx, uint8_t state)
lypinator 0:bb348c97df44 1373 {
lypinator 0:bb348c97df44 1374 __IO uint32_t hprt0;
lypinator 0:bb348c97df44 1375
lypinator 0:bb348c97df44 1376 hprt0 = USBx_HPRT0;
lypinator 0:bb348c97df44 1377 hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
lypinator 0:bb348c97df44 1378 USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
lypinator 0:bb348c97df44 1379
lypinator 0:bb348c97df44 1380 if (((hprt0 & USB_OTG_HPRT_PPWR) == 0U) && (state == 1U))
lypinator 0:bb348c97df44 1381 {
lypinator 0:bb348c97df44 1382 USBx_HPRT0 = (USB_OTG_HPRT_PPWR | hprt0);
lypinator 0:bb348c97df44 1383 }
lypinator 0:bb348c97df44 1384 if (((hprt0 & USB_OTG_HPRT_PPWR) == USB_OTG_HPRT_PPWR) && (state == 0U))
lypinator 0:bb348c97df44 1385 {
lypinator 0:bb348c97df44 1386 USBx_HPRT0 = ((~USB_OTG_HPRT_PPWR) & hprt0);
lypinator 0:bb348c97df44 1387 }
lypinator 0:bb348c97df44 1388 return HAL_OK;
lypinator 0:bb348c97df44 1389 }
lypinator 0:bb348c97df44 1390
lypinator 0:bb348c97df44 1391 /**
lypinator 0:bb348c97df44 1392 * @brief Return Host Core speed
lypinator 0:bb348c97df44 1393 * @param USBx Selected device
lypinator 0:bb348c97df44 1394 * @retval speed : Host speed
lypinator 0:bb348c97df44 1395 * This parameter can be one of these values:
lypinator 0:bb348c97df44 1396 * @arg USB_OTG_SPEED_HIGH: High speed mode
lypinator 0:bb348c97df44 1397 * @arg USB_OTG_SPEED_FULL: Full speed mode
lypinator 0:bb348c97df44 1398 * @arg USB_OTG_SPEED_LOW: Low speed mode
lypinator 0:bb348c97df44 1399 */
lypinator 0:bb348c97df44 1400 uint32_t USB_GetHostSpeed (USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 1401 {
lypinator 0:bb348c97df44 1402 __IO uint32_t hprt0;
lypinator 0:bb348c97df44 1403
lypinator 0:bb348c97df44 1404 hprt0 = USBx_HPRT0;
lypinator 0:bb348c97df44 1405 return ((hprt0 & USB_OTG_HPRT_PSPD) >> 17U);
lypinator 0:bb348c97df44 1406 }
lypinator 0:bb348c97df44 1407
lypinator 0:bb348c97df44 1408 /**
lypinator 0:bb348c97df44 1409 * @brief Return Host Current Frame number
lypinator 0:bb348c97df44 1410 * @param USBx Selected device
lypinator 0:bb348c97df44 1411 * @retval current frame number
lypinator 0:bb348c97df44 1412 */
lypinator 0:bb348c97df44 1413 uint32_t USB_GetCurrentFrame (USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 1414 {
lypinator 0:bb348c97df44 1415 return (USBx_HOST->HFNUM & USB_OTG_HFNUM_FRNUM);
lypinator 0:bb348c97df44 1416 }
lypinator 0:bb348c97df44 1417
lypinator 0:bb348c97df44 1418 /**
lypinator 0:bb348c97df44 1419 * @brief Initialize a host channel
lypinator 0:bb348c97df44 1420 * @param USBx Selected device
lypinator 0:bb348c97df44 1421 * @param ch_num Channel number
lypinator 0:bb348c97df44 1422 * This parameter can be a value from 1 to 15
lypinator 0:bb348c97df44 1423 * @param epnum Endpoint number
lypinator 0:bb348c97df44 1424 * This parameter can be a value from 1 to 15
lypinator 0:bb348c97df44 1425 * @param dev_address Current device address
lypinator 0:bb348c97df44 1426 * This parameter can be a value from 0 to 255
lypinator 0:bb348c97df44 1427 * @param speed Current device speed
lypinator 0:bb348c97df44 1428 * This parameter can be one of these values:
lypinator 0:bb348c97df44 1429 * @arg USB_OTG_SPEED_HIGH: High speed mode
lypinator 0:bb348c97df44 1430 * @arg USB_OTG_SPEED_FULL: Full speed mode
lypinator 0:bb348c97df44 1431 * @arg USB_OTG_SPEED_LOW: Low speed mode
lypinator 0:bb348c97df44 1432 * @param ep_type Endpoint Type
lypinator 0:bb348c97df44 1433 * This parameter can be one of these values:
lypinator 0:bb348c97df44 1434 * @arg EP_TYPE_CTRL: Control type
lypinator 0:bb348c97df44 1435 * @arg EP_TYPE_ISOC: Isochronous type
lypinator 0:bb348c97df44 1436 * @arg EP_TYPE_BULK: Bulk type
lypinator 0:bb348c97df44 1437 * @arg EP_TYPE_INTR: Interrupt type
lypinator 0:bb348c97df44 1438 * @param mps Max Packet Size
lypinator 0:bb348c97df44 1439 * This parameter can be a value from 0 to32K
lypinator 0:bb348c97df44 1440 * @retval HAL state
lypinator 0:bb348c97df44 1441 */
lypinator 0:bb348c97df44 1442 HAL_StatusTypeDef USB_HC_Init(USB_OTG_GlobalTypeDef *USBx,
lypinator 0:bb348c97df44 1443 uint8_t ch_num,
lypinator 0:bb348c97df44 1444 uint8_t epnum,
lypinator 0:bb348c97df44 1445 uint8_t dev_address,
lypinator 0:bb348c97df44 1446 uint8_t speed,
lypinator 0:bb348c97df44 1447 uint8_t ep_type,
lypinator 0:bb348c97df44 1448 uint16_t mps)
lypinator 0:bb348c97df44 1449 {
lypinator 0:bb348c97df44 1450
lypinator 0:bb348c97df44 1451 /* Clear old interrupt conditions for this host channel. */
lypinator 0:bb348c97df44 1452 USBx_HC(ch_num)->HCINT = 0xFFFFFFFFU;
lypinator 0:bb348c97df44 1453
lypinator 0:bb348c97df44 1454 /* Enable channel interrupts required for this transfer. */
lypinator 0:bb348c97df44 1455 switch (ep_type)
lypinator 0:bb348c97df44 1456 {
lypinator 0:bb348c97df44 1457 case EP_TYPE_CTRL:
lypinator 0:bb348c97df44 1458 case EP_TYPE_BULK:
lypinator 0:bb348c97df44 1459
lypinator 0:bb348c97df44 1460 USBx_HC(ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |\
lypinator 0:bb348c97df44 1461 USB_OTG_HCINTMSK_STALLM |\
lypinator 0:bb348c97df44 1462 USB_OTG_HCINTMSK_TXERRM |\
lypinator 0:bb348c97df44 1463 USB_OTG_HCINTMSK_DTERRM |\
lypinator 0:bb348c97df44 1464 USB_OTG_HCINTMSK_AHBERR |\
lypinator 0:bb348c97df44 1465 USB_OTG_HCINTMSK_NAKM ;
lypinator 0:bb348c97df44 1466
lypinator 0:bb348c97df44 1467 if (epnum & 0x80U)
lypinator 0:bb348c97df44 1468 {
lypinator 0:bb348c97df44 1469 USBx_HC(ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
lypinator 0:bb348c97df44 1470 }
lypinator 0:bb348c97df44 1471 else
lypinator 0:bb348c97df44 1472 {
lypinator 0:bb348c97df44 1473 if(USBx != USB_OTG_FS)
lypinator 0:bb348c97df44 1474 {
lypinator 0:bb348c97df44 1475 USBx_HC(ch_num)->HCINTMSK |= (USB_OTG_HCINTMSK_NYET | USB_OTG_HCINTMSK_ACKM);
lypinator 0:bb348c97df44 1476 }
lypinator 0:bb348c97df44 1477 }
lypinator 0:bb348c97df44 1478 break;
lypinator 0:bb348c97df44 1479
lypinator 0:bb348c97df44 1480 case EP_TYPE_INTR:
lypinator 0:bb348c97df44 1481
lypinator 0:bb348c97df44 1482 USBx_HC(ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |\
lypinator 0:bb348c97df44 1483 USB_OTG_HCINTMSK_STALLM |\
lypinator 0:bb348c97df44 1484 USB_OTG_HCINTMSK_TXERRM |\
lypinator 0:bb348c97df44 1485 USB_OTG_HCINTMSK_DTERRM |\
lypinator 0:bb348c97df44 1486 USB_OTG_HCINTMSK_NAKM |\
lypinator 0:bb348c97df44 1487 USB_OTG_HCINTMSK_AHBERR |\
lypinator 0:bb348c97df44 1488 USB_OTG_HCINTMSK_FRMORM ;
lypinator 0:bb348c97df44 1489
lypinator 0:bb348c97df44 1490 if (epnum & 0x80U)
lypinator 0:bb348c97df44 1491 {
lypinator 0:bb348c97df44 1492 USBx_HC(ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
lypinator 0:bb348c97df44 1493 }
lypinator 0:bb348c97df44 1494
lypinator 0:bb348c97df44 1495 break;
lypinator 0:bb348c97df44 1496 case EP_TYPE_ISOC:
lypinator 0:bb348c97df44 1497
lypinator 0:bb348c97df44 1498 USBx_HC(ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |\
lypinator 0:bb348c97df44 1499 USB_OTG_HCINTMSK_ACKM |\
lypinator 0:bb348c97df44 1500 USB_OTG_HCINTMSK_AHBERR |\
lypinator 0:bb348c97df44 1501 USB_OTG_HCINTMSK_FRMORM ;
lypinator 0:bb348c97df44 1502
lypinator 0:bb348c97df44 1503 if (epnum & 0x80U)
lypinator 0:bb348c97df44 1504 {
lypinator 0:bb348c97df44 1505 USBx_HC(ch_num)->HCINTMSK |= (USB_OTG_HCINTMSK_TXERRM | USB_OTG_HCINTMSK_BBERRM);
lypinator 0:bb348c97df44 1506 }
lypinator 0:bb348c97df44 1507 break;
lypinator 0:bb348c97df44 1508 }
lypinator 0:bb348c97df44 1509
lypinator 0:bb348c97df44 1510 /* Enable the top level host channel interrupt. */
lypinator 0:bb348c97df44 1511 USBx_HOST->HAINTMSK |= (1 << ch_num);
lypinator 0:bb348c97df44 1512
lypinator 0:bb348c97df44 1513 /* Make sure host channel interrupts are enabled. */
lypinator 0:bb348c97df44 1514 USBx->GINTMSK |= USB_OTG_GINTMSK_HCIM;
lypinator 0:bb348c97df44 1515
lypinator 0:bb348c97df44 1516 /* Program the HCCHAR register */
lypinator 0:bb348c97df44 1517 USBx_HC(ch_num)->HCCHAR = (((dev_address << 22U) & USB_OTG_HCCHAR_DAD) |\
lypinator 0:bb348c97df44 1518 (((epnum & 0x7FU)<< 11U) & USB_OTG_HCCHAR_EPNUM)|\
lypinator 0:bb348c97df44 1519 ((((epnum & 0x80U) == 0x80U)<< 15U) & USB_OTG_HCCHAR_EPDIR)|\
lypinator 0:bb348c97df44 1520 (((speed == USB_OTG_SPEED_LOW)<< 17U) & USB_OTG_HCCHAR_LSDEV)|\
lypinator 0:bb348c97df44 1521 ((ep_type << 18U) & USB_OTG_HCCHAR_EPTYP)|\
lypinator 0:bb348c97df44 1522 (mps & USB_OTG_HCCHAR_MPSIZ));
lypinator 0:bb348c97df44 1523
lypinator 0:bb348c97df44 1524 if (ep_type == EP_TYPE_INTR)
lypinator 0:bb348c97df44 1525 {
lypinator 0:bb348c97df44 1526 USBx_HC(ch_num)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM ;
lypinator 0:bb348c97df44 1527 }
lypinator 0:bb348c97df44 1528
lypinator 0:bb348c97df44 1529 return HAL_OK;
lypinator 0:bb348c97df44 1530 }
lypinator 0:bb348c97df44 1531
lypinator 0:bb348c97df44 1532 /**
lypinator 0:bb348c97df44 1533 * @brief Start a transfer over a host channel
lypinator 0:bb348c97df44 1534 * @param USBx Selected device
lypinator 0:bb348c97df44 1535 * @param hc pointer to host channel structure
lypinator 0:bb348c97df44 1536 * @param dma USB dma enabled or disabled
lypinator 0:bb348c97df44 1537 * This parameter can be one of these values:
lypinator 0:bb348c97df44 1538 * 0 : DMA feature not used
lypinator 0:bb348c97df44 1539 * 1 : DMA feature used
lypinator 0:bb348c97df44 1540 * @retval HAL state
lypinator 0:bb348c97df44 1541 */
lypinator 0:bb348c97df44 1542 #if defined (__CC_ARM) /*!< ARM Compiler */
lypinator 0:bb348c97df44 1543 #pragma O0
lypinator 0:bb348c97df44 1544 #elif defined (__GNUC__) /*!< GNU Compiler */
lypinator 0:bb348c97df44 1545 #pragma GCC optimize ("O0")
lypinator 0:bb348c97df44 1546 #endif /* __CC_ARM */
lypinator 0:bb348c97df44 1547 HAL_StatusTypeDef USB_HC_StartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_HCTypeDef *hc, uint8_t dma)
lypinator 0:bb348c97df44 1548 {
lypinator 0:bb348c97df44 1549 uint8_t is_oddframe = 0;
lypinator 0:bb348c97df44 1550 uint16_t len_words = 0;
lypinator 0:bb348c97df44 1551 uint16_t num_packets = 0;
lypinator 0:bb348c97df44 1552 uint16_t max_hc_pkt_count = 256;
lypinator 0:bb348c97df44 1553 uint32_t tmpreg = 0U;
lypinator 0:bb348c97df44 1554
lypinator 0:bb348c97df44 1555 if((USBx != USB_OTG_FS) && (hc->speed == USB_OTG_SPEED_HIGH))
lypinator 0:bb348c97df44 1556 {
lypinator 0:bb348c97df44 1557 if((dma == 0) && (hc->do_ping == 1U))
lypinator 0:bb348c97df44 1558 {
lypinator 0:bb348c97df44 1559 USB_DoPing(USBx, hc->ch_num);
lypinator 0:bb348c97df44 1560 return HAL_OK;
lypinator 0:bb348c97df44 1561 }
lypinator 0:bb348c97df44 1562 else if(dma == 1)
lypinator 0:bb348c97df44 1563 {
lypinator 0:bb348c97df44 1564 USBx_HC(hc->ch_num)->HCINTMSK &= ~(USB_OTG_HCINTMSK_NYET | USB_OTG_HCINTMSK_ACKM);
lypinator 0:bb348c97df44 1565 hc->do_ping = 0U;
lypinator 0:bb348c97df44 1566 }
lypinator 0:bb348c97df44 1567 }
lypinator 0:bb348c97df44 1568
lypinator 0:bb348c97df44 1569 /* Compute the expected number of packets associated to the transfer */
lypinator 0:bb348c97df44 1570 if (hc->xfer_len > 0U)
lypinator 0:bb348c97df44 1571 {
lypinator 0:bb348c97df44 1572 num_packets = (hc->xfer_len + hc->max_packet - 1U) / hc->max_packet;
lypinator 0:bb348c97df44 1573
lypinator 0:bb348c97df44 1574 if (num_packets > max_hc_pkt_count)
lypinator 0:bb348c97df44 1575 {
lypinator 0:bb348c97df44 1576 num_packets = max_hc_pkt_count;
lypinator 0:bb348c97df44 1577 hc->xfer_len = num_packets * hc->max_packet;
lypinator 0:bb348c97df44 1578 }
lypinator 0:bb348c97df44 1579 }
lypinator 0:bb348c97df44 1580 else
lypinator 0:bb348c97df44 1581 {
lypinator 0:bb348c97df44 1582 num_packets = 1;
lypinator 0:bb348c97df44 1583 }
lypinator 0:bb348c97df44 1584 if (hc->ep_is_in)
lypinator 0:bb348c97df44 1585 {
lypinator 0:bb348c97df44 1586 hc->xfer_len = num_packets * hc->max_packet;
lypinator 0:bb348c97df44 1587 }
lypinator 0:bb348c97df44 1588
lypinator 0:bb348c97df44 1589 /* Initialize the HCTSIZn register */
lypinator 0:bb348c97df44 1590 USBx_HC(hc->ch_num)->HCTSIZ = (((hc->xfer_len) & USB_OTG_HCTSIZ_XFRSIZ)) |\
lypinator 0:bb348c97df44 1591 ((num_packets << 19U) & USB_OTG_HCTSIZ_PKTCNT) |\
lypinator 0:bb348c97df44 1592 (((hc->data_pid) << 29U) & USB_OTG_HCTSIZ_DPID);
lypinator 0:bb348c97df44 1593
lypinator 0:bb348c97df44 1594 if (dma)
lypinator 0:bb348c97df44 1595 {
lypinator 0:bb348c97df44 1596 /* xfer_buff MUST be 32-bits aligned */
lypinator 0:bb348c97df44 1597 USBx_HC(hc->ch_num)->HCDMA = (uint32_t)hc->xfer_buff;
lypinator 0:bb348c97df44 1598 }
lypinator 0:bb348c97df44 1599
lypinator 0:bb348c97df44 1600 is_oddframe = (USBx_HOST->HFNUM & 0x01) ? 0 : 1;
lypinator 0:bb348c97df44 1601 USBx_HC(hc->ch_num)->HCCHAR &= ~USB_OTG_HCCHAR_ODDFRM;
lypinator 0:bb348c97df44 1602 USBx_HC(hc->ch_num)->HCCHAR |= (is_oddframe << 29);
lypinator 0:bb348c97df44 1603
lypinator 0:bb348c97df44 1604 /* Set host channel enable */
lypinator 0:bb348c97df44 1605 tmpreg = USBx_HC(hc->ch_num)->HCCHAR;
lypinator 0:bb348c97df44 1606 tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
lypinator 0:bb348c97df44 1607 tmpreg |= USB_OTG_HCCHAR_CHENA;
lypinator 0:bb348c97df44 1608 USBx_HC(hc->ch_num)->HCCHAR = tmpreg;
lypinator 0:bb348c97df44 1609
lypinator 0:bb348c97df44 1610 if (dma == 0) /* Slave mode */
lypinator 0:bb348c97df44 1611 {
lypinator 0:bb348c97df44 1612 if((hc->ep_is_in == 0U) && (hc->xfer_len > 0U))
lypinator 0:bb348c97df44 1613 {
lypinator 0:bb348c97df44 1614 switch(hc->ep_type)
lypinator 0:bb348c97df44 1615 {
lypinator 0:bb348c97df44 1616 /* Non periodic transfer */
lypinator 0:bb348c97df44 1617 case EP_TYPE_CTRL:
lypinator 0:bb348c97df44 1618 case EP_TYPE_BULK:
lypinator 0:bb348c97df44 1619
lypinator 0:bb348c97df44 1620 len_words = (hc->xfer_len + 3) / 4;
lypinator 0:bb348c97df44 1621
lypinator 0:bb348c97df44 1622 /* check if there is enough space in FIFO space */
lypinator 0:bb348c97df44 1623 if(len_words > (USBx->HNPTXSTS & 0xFFFF))
lypinator 0:bb348c97df44 1624 {
lypinator 0:bb348c97df44 1625 /* need to process data in nptxfempty interrupt */
lypinator 0:bb348c97df44 1626 USBx->GINTMSK |= USB_OTG_GINTMSK_NPTXFEM;
lypinator 0:bb348c97df44 1627 }
lypinator 0:bb348c97df44 1628 break;
lypinator 0:bb348c97df44 1629 /* Periodic transfer */
lypinator 0:bb348c97df44 1630 case EP_TYPE_INTR:
lypinator 0:bb348c97df44 1631 case EP_TYPE_ISOC:
lypinator 0:bb348c97df44 1632 len_words = (hc->xfer_len + 3) / 4;
lypinator 0:bb348c97df44 1633 /* check if there is enough space in FIFO space */
lypinator 0:bb348c97df44 1634 if(len_words > (USBx_HOST->HPTXSTS & 0xFFFF)) /* split the transfer */
lypinator 0:bb348c97df44 1635 {
lypinator 0:bb348c97df44 1636 /* need to process data in ptxfempty interrupt */
lypinator 0:bb348c97df44 1637 USBx->GINTMSK |= USB_OTG_GINTMSK_PTXFEM;
lypinator 0:bb348c97df44 1638 }
lypinator 0:bb348c97df44 1639 break;
lypinator 0:bb348c97df44 1640
lypinator 0:bb348c97df44 1641 default:
lypinator 0:bb348c97df44 1642 break;
lypinator 0:bb348c97df44 1643 }
lypinator 0:bb348c97df44 1644
lypinator 0:bb348c97df44 1645 /* Write packet into the Tx FIFO. */
lypinator 0:bb348c97df44 1646 USB_WritePacket(USBx, hc->xfer_buff, hc->ch_num, hc->xfer_len, 0);
lypinator 0:bb348c97df44 1647 /* MBED */
lypinator 0:bb348c97df44 1648 hc->xfer_count = hc->xfer_len;
lypinator 0:bb348c97df44 1649 /* MBED */
lypinator 0:bb348c97df44 1650 }
lypinator 0:bb348c97df44 1651 }
lypinator 0:bb348c97df44 1652
lypinator 0:bb348c97df44 1653 return HAL_OK;
lypinator 0:bb348c97df44 1654 }
lypinator 0:bb348c97df44 1655
lypinator 0:bb348c97df44 1656 /**
lypinator 0:bb348c97df44 1657 * @brief Read all host channel interrupts status
lypinator 0:bb348c97df44 1658 * @param USBx Selected device
lypinator 0:bb348c97df44 1659 * @retval HAL state
lypinator 0:bb348c97df44 1660 */
lypinator 0:bb348c97df44 1661 uint32_t USB_HC_ReadInterrupt (USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 1662 {
lypinator 0:bb348c97df44 1663 return ((USBx_HOST->HAINT) & 0xFFFFU);
lypinator 0:bb348c97df44 1664 }
lypinator 0:bb348c97df44 1665
lypinator 0:bb348c97df44 1666 /**
lypinator 0:bb348c97df44 1667 * @brief Halt a host channel
lypinator 0:bb348c97df44 1668 * @param USBx Selected device
lypinator 0:bb348c97df44 1669 * @param hc_num Host Channel number
lypinator 0:bb348c97df44 1670 * This parameter can be a value from 1 to 15
lypinator 0:bb348c97df44 1671 * @retval HAL state
lypinator 0:bb348c97df44 1672 */
lypinator 0:bb348c97df44 1673 HAL_StatusTypeDef USB_HC_Halt(USB_OTG_GlobalTypeDef *USBx , uint8_t hc_num)
lypinator 0:bb348c97df44 1674 {
lypinator 0:bb348c97df44 1675 uint32_t count = 0U;
lypinator 0:bb348c97df44 1676
lypinator 0:bb348c97df44 1677 /* Check for space in the request queue to issue the halt. */
lypinator 0:bb348c97df44 1678 if (((((USBx_HC(hc_num)->HCCHAR) & USB_OTG_HCCHAR_EPTYP) >> 18) == HCCHAR_CTRL) || (((((USBx_HC(hc_num)->HCCHAR) &
lypinator 0:bb348c97df44 1679 USB_OTG_HCCHAR_EPTYP) >> 18) == HCCHAR_BULK)))
lypinator 0:bb348c97df44 1680 {
lypinator 0:bb348c97df44 1681 USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHDIS;
lypinator 0:bb348c97df44 1682
lypinator 0:bb348c97df44 1683 if ((USBx->HNPTXSTS & 0xFF0000U) == 0U)
lypinator 0:bb348c97df44 1684 {
lypinator 0:bb348c97df44 1685 USBx_HC(hc_num)->HCCHAR &= ~USB_OTG_HCCHAR_CHENA;
lypinator 0:bb348c97df44 1686 USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
lypinator 0:bb348c97df44 1687 do
lypinator 0:bb348c97df44 1688 {
lypinator 0:bb348c97df44 1689 if (++count > 1000U)
lypinator 0:bb348c97df44 1690 {
lypinator 0:bb348c97df44 1691 break;
lypinator 0:bb348c97df44 1692 }
lypinator 0:bb348c97df44 1693 }
lypinator 0:bb348c97df44 1694 while ((USBx_HC(hc_num)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
lypinator 0:bb348c97df44 1695 }
lypinator 0:bb348c97df44 1696 else
lypinator 0:bb348c97df44 1697 {
lypinator 0:bb348c97df44 1698 USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
lypinator 0:bb348c97df44 1699 }
lypinator 0:bb348c97df44 1700 }
lypinator 0:bb348c97df44 1701 else
lypinator 0:bb348c97df44 1702 {
lypinator 0:bb348c97df44 1703 USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHDIS;
lypinator 0:bb348c97df44 1704
lypinator 0:bb348c97df44 1705 if ((USBx_HOST->HPTXSTS & 0xFFFFU) == 0U)
lypinator 0:bb348c97df44 1706 {
lypinator 0:bb348c97df44 1707 USBx_HC(hc_num)->HCCHAR &= ~USB_OTG_HCCHAR_CHENA;
lypinator 0:bb348c97df44 1708 USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
lypinator 0:bb348c97df44 1709 do
lypinator 0:bb348c97df44 1710 {
lypinator 0:bb348c97df44 1711 if (++count > 1000U)
lypinator 0:bb348c97df44 1712 {
lypinator 0:bb348c97df44 1713 break;
lypinator 0:bb348c97df44 1714 }
lypinator 0:bb348c97df44 1715 }
lypinator 0:bb348c97df44 1716 while ((USBx_HC(hc_num)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
lypinator 0:bb348c97df44 1717 }
lypinator 0:bb348c97df44 1718 else
lypinator 0:bb348c97df44 1719 {
lypinator 0:bb348c97df44 1720 USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
lypinator 0:bb348c97df44 1721 }
lypinator 0:bb348c97df44 1722 }
lypinator 0:bb348c97df44 1723
lypinator 0:bb348c97df44 1724 return HAL_OK;
lypinator 0:bb348c97df44 1725 }
lypinator 0:bb348c97df44 1726
lypinator 0:bb348c97df44 1727 /**
lypinator 0:bb348c97df44 1728 * @brief Initiate Do Ping protocol
lypinator 0:bb348c97df44 1729 * @param USBx Selected device
lypinator 0:bb348c97df44 1730 * @param hc_num Host Channel number
lypinator 0:bb348c97df44 1731 * This parameter can be a value from 1 to 15
lypinator 0:bb348c97df44 1732 * @retval HAL state
lypinator 0:bb348c97df44 1733 */
lypinator 0:bb348c97df44 1734 HAL_StatusTypeDef USB_DoPing(USB_OTG_GlobalTypeDef *USBx , uint8_t ch_num)
lypinator 0:bb348c97df44 1735 {
lypinator 0:bb348c97df44 1736 uint8_t num_packets = 1U;
lypinator 0:bb348c97df44 1737 uint32_t tmpreg = 0U;
lypinator 0:bb348c97df44 1738
lypinator 0:bb348c97df44 1739 USBx_HC(ch_num)->HCTSIZ = ((num_packets << 19U) & USB_OTG_HCTSIZ_PKTCNT) |\
lypinator 0:bb348c97df44 1740 USB_OTG_HCTSIZ_DOPING;
lypinator 0:bb348c97df44 1741
lypinator 0:bb348c97df44 1742 /* Set host channel enable */
lypinator 0:bb348c97df44 1743 tmpreg = USBx_HC(ch_num)->HCCHAR;
lypinator 0:bb348c97df44 1744 tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
lypinator 0:bb348c97df44 1745 tmpreg |= USB_OTG_HCCHAR_CHENA;
lypinator 0:bb348c97df44 1746 USBx_HC(ch_num)->HCCHAR = tmpreg;
lypinator 0:bb348c97df44 1747
lypinator 0:bb348c97df44 1748 return HAL_OK;
lypinator 0:bb348c97df44 1749 }
lypinator 0:bb348c97df44 1750
lypinator 0:bb348c97df44 1751 /**
lypinator 0:bb348c97df44 1752 * @brief Stop Host Core
lypinator 0:bb348c97df44 1753 * @param USBx Selected device
lypinator 0:bb348c97df44 1754 * @retval HAL state
lypinator 0:bb348c97df44 1755 */
lypinator 0:bb348c97df44 1756 HAL_StatusTypeDef USB_StopHost(USB_OTG_GlobalTypeDef *USBx)
lypinator 0:bb348c97df44 1757 {
lypinator 0:bb348c97df44 1758 uint8_t i;
lypinator 0:bb348c97df44 1759 uint32_t count = 0U;
lypinator 0:bb348c97df44 1760 uint32_t value;
lypinator 0:bb348c97df44 1761
lypinator 0:bb348c97df44 1762 USB_DisableGlobalInt(USBx);
lypinator 0:bb348c97df44 1763
lypinator 0:bb348c97df44 1764 /* Flush FIFO */
lypinator 0:bb348c97df44 1765 USB_FlushTxFifo(USBx, 0x10U);
lypinator 0:bb348c97df44 1766 USB_FlushRxFifo(USBx);
lypinator 0:bb348c97df44 1767
lypinator 0:bb348c97df44 1768 /* Flush out any leftover queued requests. */
lypinator 0:bb348c97df44 1769 for (i = 0; i <= 15; i++)
lypinator 0:bb348c97df44 1770 {
lypinator 0:bb348c97df44 1771
lypinator 0:bb348c97df44 1772 value = USBx_HC(i)->HCCHAR ;
lypinator 0:bb348c97df44 1773 value |= USB_OTG_HCCHAR_CHDIS;
lypinator 0:bb348c97df44 1774 value &= ~USB_OTG_HCCHAR_CHENA;
lypinator 0:bb348c97df44 1775 value &= ~USB_OTG_HCCHAR_EPDIR;
lypinator 0:bb348c97df44 1776 USBx_HC(i)->HCCHAR = value;
lypinator 0:bb348c97df44 1777 }
lypinator 0:bb348c97df44 1778
lypinator 0:bb348c97df44 1779 /* Halt all channels to put them into a known state. */
lypinator 0:bb348c97df44 1780 for (i = 0; i <= 15; i++)
lypinator 0:bb348c97df44 1781 {
lypinator 0:bb348c97df44 1782 value = USBx_HC(i)->HCCHAR ;
lypinator 0:bb348c97df44 1783
lypinator 0:bb348c97df44 1784 value |= USB_OTG_HCCHAR_CHDIS;
lypinator 0:bb348c97df44 1785 value |= USB_OTG_HCCHAR_CHENA;
lypinator 0:bb348c97df44 1786 value &= ~USB_OTG_HCCHAR_EPDIR;
lypinator 0:bb348c97df44 1787
lypinator 0:bb348c97df44 1788 USBx_HC(i)->HCCHAR = value;
lypinator 0:bb348c97df44 1789 do
lypinator 0:bb348c97df44 1790 {
lypinator 0:bb348c97df44 1791 if (++count > 1000U)
lypinator 0:bb348c97df44 1792 {
lypinator 0:bb348c97df44 1793 break;
lypinator 0:bb348c97df44 1794 }
lypinator 0:bb348c97df44 1795 }
lypinator 0:bb348c97df44 1796 while ((USBx_HC(i)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
lypinator 0:bb348c97df44 1797 }
lypinator 0:bb348c97df44 1798
lypinator 0:bb348c97df44 1799 /* Clear any pending Host interrupts */
lypinator 0:bb348c97df44 1800 USBx_HOST->HAINT = 0xFFFFFFFFU;
lypinator 0:bb348c97df44 1801 USBx->GINTSTS = 0xFFFFFFFFU;
lypinator 0:bb348c97df44 1802 USB_EnableGlobalInt(USBx);
lypinator 0:bb348c97df44 1803 return HAL_OK;
lypinator 0:bb348c97df44 1804 }
lypinator 0:bb348c97df44 1805 /**
lypinator 0:bb348c97df44 1806 * @}
lypinator 0:bb348c97df44 1807 */
lypinator 0:bb348c97df44 1808 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||
lypinator 0:bb348c97df44 1809 STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Rx ||
lypinator 0:bb348c97df44 1810 STM32F412Vx || STM32F412Cx || STM32F413xx || STM32F423xx */
lypinator 0:bb348c97df44 1811 #endif /* defined(HAL_PCD_MODULE_ENABLED) || defined(HAL_HCD_MODULE_ENABLED) */
lypinator 0:bb348c97df44 1812
lypinator 0:bb348c97df44 1813 /**
lypinator 0:bb348c97df44 1814 * @}
lypinator 0:bb348c97df44 1815 */
lypinator 0:bb348c97df44 1816
lypinator 0:bb348c97df44 1817 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/