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