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