Subversion Repositories LedShow

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 9
Line 2... Line 2...
2
  ******************************************************************************
2
  ******************************************************************************
3
  * @file    stm32f1xx_ll_usb.c
3
  * @file    stm32f1xx_ll_usb.c
4
  * @author  MCD Application Team
4
  * @author  MCD Application Team
5
  * @brief   USB Low Layer HAL module driver.
5
  * @brief   USB Low Layer HAL module driver.
6
  *
6
  *
7
  *          This file provides firmware functions to manage the following
7
  *          This file provides firmware functions to manage the following
8
  *          functionalities of the USB Peripheral Controller:
8
  *          functionalities of the USB Peripheral Controller:
9
  *           + Initialization/de-initialization functions
9
  *           + Initialization/de-initialization functions
10
  *           + I/O operation functions
10
  *           + I/O operation functions
11
  *           + Peripheral Control functions
11
  *           + Peripheral Control functions
12
  *           + Peripheral State functions
12
  *           + Peripheral State functions
13
  *
13
  *
14
  @verbatim
14
  @verbatim
15
  ==============================================================================
15
  ==============================================================================
16
                    ##### How to use this driver #####
16
                    ##### How to use this driver #####
17
  ==============================================================================
17
  ==============================================================================
18
    [..]
18
    [..]
19
      (#) Fill parameters of Init structure in USB_OTG_CfgTypeDef structure.
19
      (#) Fill parameters of Init structure in USB_OTG_CfgTypeDef structure.
20
 
20
 
21
      (#) Call USB_CoreInit() API to initialize the USB Core peripheral.
21
      (#) Call USB_CoreInit() API to initialize the USB Core peripheral.
22
 
22
 
23
      (#) The upper HAL HCD/PCD driver will call the right routines for its internal processes.
23
      (#) The upper HAL HCD/PCD driver will call the right routines for its internal processes.
24
 
24
 
25
  @endverbatim
25
  @endverbatim
26
  ******************************************************************************
26
  ******************************************************************************
27
  * @attention
27
  * @attention
28
  *
28
  *
29
  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
29
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
30
  *
-
 
31
  * Redistribution and use in source and binary forms, with or without modification,
-
 
32
  * are permitted provided that the following conditions are met:
-
 
33
  *   1. Redistributions of source code must retain the above copyright notice,
-
 
34
  *      this list of conditions and the following disclaimer.
-
 
35
  *   2. Redistributions in binary form must reproduce the above copyright notice,
-
 
36
  *      this list of conditions and the following disclaimer in the documentation
-
 
37
  *      and/or other materials provided with the distribution.
-
 
38
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
-
 
39
  *      may be used to endorse or promote products derived from this software
-
 
40
  *      without specific prior written permission.
30
  * All rights reserved.</center></h2>
41
  *
31
  *
42
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32
  * This software component is licensed by ST under BSD 3-Clause license,
43
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-
 
44
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-
 
45
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
-
 
46
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-
 
47
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33
  * the "License"; You may not use this file except in compliance with the
48
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34
  * License. You may obtain a copy of the License at:
49
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-
 
50
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-
 
51
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
  *                        opensource.org/licenses/BSD-3-Clause
52
  *
36
  *
53
  ******************************************************************************
37
  ******************************************************************************
54
  */
38
  */
55
 
39
 
56
/* Includes ------------------------------------------------------------------*/
40
/* Includes ------------------------------------------------------------------*/
57
#include "stm32f1xx_hal.h"
41
#include "stm32f1xx_hal.h"
58
 
42
 
59
/** @addtogroup STM32F1xx_HAL_Driver
43
/** @addtogroup STM32F1xx_LL_USB_DRIVER
60
  * @{
-
 
61
  */
-
 
62
 
-
 
63
/** @defgroup USB_LL USB Low Layer
-
 
64
  * @brief Low layer module for USB_FS and USB_OTG_FS drivers
-
 
65
  * @{
44
  * @{
66
  */
45
  */
67
 
46
 
68
#if defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED)
47
#if defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED)
69
 
-
 
70
#if defined(STM32F102x6) || defined(STM32F102xB) || \
48
#if defined (USB) || defined (USB_OTG_FS)
71
    defined(STM32F103x6) || defined(STM32F103xB) || \
49
/* Private typedef -----------------------------------------------------------*/
72
    defined(STM32F103xE) || defined(STM32F103xG) || \
50
/* Private define ------------------------------------------------------------*/
73
    defined(STM32F105xC) || defined(STM32F107xC)
-
 
74
 
-
 
75
/* Private types -------------------------------------------------------------*/
51
/* Private macro -------------------------------------------------------------*/
76
/* Private variables ---------------------------------------------------------*/
52
/* Private variables ---------------------------------------------------------*/
77
/* Private constants ---------------------------------------------------------*/
53
/* Private function prototypes -----------------------------------------------*/
78
/* Private macros ------------------------------------------------------------*/
-
 
79
/* Private functions ---------------------------------------------------------*/
54
/* Private functions ---------------------------------------------------------*/
80
#if defined (USB_OTG_FS)
55
#if defined (USB_OTG_FS)
81
/** @defgroup USB_LL_Private_Functions USB Low Layer Private Functions
-
 
82
  * @{
-
 
83
  */
-
 
84
static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx);
56
static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx);
85
/**
-
 
86
  * @}
-
 
87
  */
-
 
88
#endif /* USB_OTG_FS */
-
 
89
 
57
 
90
/* Exported functions --------------------------------------------------------*/
58
/* Exported functions --------------------------------------------------------*/
91
/** @defgroup USB_LL_Exported_Functions USB Low Layer Exported Functions
59
/** @defgroup USB_LL_Exported_Functions USB Low Layer Exported Functions
92
  * @{
60
  * @{
93
  */
61
  */
94
 
62
 
95
/** @defgroup USB_LL_Exported_Functions_Group1 Peripheral Control functions
63
/** @defgroup USB_LL_Exported_Functions_Group1 Initialization/de-initialization functions
96
 *  @brief   management functions
64
  *  @brief    Initialization and Configuration functions
97
 *
65
  *
98
@verbatim
66
@verbatim
99
 ===============================================================================
67
 ===============================================================================
100
                      ##### Peripheral Control functions #####
68
                      ##### Initialization/de-initialization functions #####
101
 ===============================================================================  
69
 ===============================================================================
102
    [..]
-
 
103
    This subsection provides a set of functions allowing to control the PCD data
-
 
104
    transfers.
-
 
105
 
70
 
106
@endverbatim
71
@endverbatim
107
  * @{
72
  * @{
108
  */
73
  */
109
 
-
 
110
/*==============================================================================
-
 
111
    USB OTG FS peripheral available on STM32F105xx and STM32F107xx devices
-
 
112
==============================================================================*/
-
 
113
#if defined (USB_OTG_FS)
-
 
114
 
74
 
115
/**
75
/**
116
  * @brief  Initializes the USB Core
76
  * @brief  Initializes the USB Core
117
  * @param  USBx: USB Instance
77
  * @param  USBx USB Instance
118
  * @param  cfg : pointer to a USB_OTG_CfgTypeDef structure that contains
78
  * @param  cfg pointer to a USB_OTG_CfgTypeDef structure that contains
119
  *         the configuration information for the specified USBx peripheral.
79
  *         the configuration information for the specified USBx peripheral.
120
  * @retval HAL status
80
  * @retval HAL status
121
  */
81
  */
122
HAL_StatusTypeDef USB_CoreInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
82
HAL_StatusTypeDef USB_CoreInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
123
{
83
{
-
 
84
  HAL_StatusTypeDef ret;
-
 
85
 
-
 
86
  if (cfg.phy_itface == USB_OTG_ULPI_PHY)
-
 
87
  {
-
 
88
    USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
-
 
89
 
-
 
90
    /* Init The ULPI Interface */
-
 
91
    USBx->GUSBCFG &= ~(USB_OTG_GUSBCFG_TSDPS | USB_OTG_GUSBCFG_ULPIFSLS | USB_OTG_GUSBCFG_PHYSEL);
-
 
92
 
-
 
93
    /* Select vbus source */
-
 
94
    USBx->GUSBCFG &= ~(USB_OTG_GUSBCFG_ULPIEVBUSD | USB_OTG_GUSBCFG_ULPIEVBUSI);
-
 
95
    if (cfg.use_external_vbus == 1U)
-
 
96
    {
-
 
97
      USBx->GUSBCFG |= USB_OTG_GUSBCFG_ULPIEVBUSD;
-
 
98
    }
124
  /* Prevent unused argument(s) compilation warning */
99
    /* Reset after a PHY select  */
-
 
100
    ret = USB_CoreReset(USBx);
-
 
101
  }
-
 
102
  else /* FS interface (embedded Phy) */
-
 
103
  {
-
 
104
    /* Select FS Embedded PHY */
-
 
105
    USBx->GUSBCFG |= USB_OTG_GUSBCFG_PHYSEL;
-
 
106
 
-
 
107
    /* Reset after a PHY select */
-
 
108
    ret = USB_CoreReset(USBx);
-
 
109
 
-
 
110
    /* Activate the USB Transceiver */
-
 
111
    USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
-
 
112
  }
-
 
113
 
125
  UNUSED(cfg);
114
  return ret;
-
 
115
}
-
 
116
 
-
 
117
 
-
 
118
/**
-
 
119
  * @brief  Set the USB turnaround time
-
 
120
  * @param  USBx USB Instance
-
 
121
  * @param  hclk: AHB clock frequency
-
 
122
  * @retval USB turnaround time In PHY Clocks number
-
 
123
  */
-
 
124
HAL_StatusTypeDef USB_SetTurnaroundTime(USB_OTG_GlobalTypeDef *USBx,
-
 
125
                                        uint32_t hclk, uint8_t speed)
-
 
126
{
-
 
127
  uint32_t UsbTrd;
-
 
128
 
-
 
129
  /* The USBTRD is configured according to the tables below, depending on AHB frequency
-
 
130
  used by application. In the low AHB frequency range it is used to stretch enough the USB response
-
 
131
  time to IN tokens, the USB turnaround time, so to compensate for the longer AHB read access
-
 
132
  latency to the Data FIFO */
-
 
133
  if (speed == USBD_FS_SPEED)
-
 
134
  {
-
 
135
    if ((hclk >= 14200000U) && (hclk < 15000000U))
-
 
136
    {
-
 
137
      /* hclk Clock Range between 14.2-15 MHz */
-
 
138
      UsbTrd = 0xFU;
-
 
139
    }
-
 
140
    else if ((hclk >= 15000000U) && (hclk < 16000000U))
-
 
141
    {
-
 
142
      /* hclk Clock Range between 15-16 MHz */
-
 
143
      UsbTrd = 0xEU;
-
 
144
    }
-
 
145
    else if ((hclk >= 16000000U) && (hclk < 17200000U))
-
 
146
    {
-
 
147
      /* hclk Clock Range between 16-17.2 MHz */
-
 
148
      UsbTrd = 0xDU;
-
 
149
    }
-
 
150
    else if ((hclk >= 17200000U) && (hclk < 18500000U))
-
 
151
    {
-
 
152
      /* hclk Clock Range between 17.2-18.5 MHz */
-
 
153
      UsbTrd = 0xCU;
-
 
154
    }
-
 
155
    else if ((hclk >= 18500000U) && (hclk < 20000000U))
-
 
156
    {
-
 
157
      /* hclk Clock Range between 18.5-20 MHz */
-
 
158
      UsbTrd = 0xBU;
-
 
159
    }
-
 
160
    else if ((hclk >= 20000000U) && (hclk < 21800000U))
-
 
161
    {
-
 
162
      /* hclk Clock Range between 20-21.8 MHz */
-
 
163
      UsbTrd = 0xAU;
-
 
164
    }
-
 
165
    else if ((hclk >= 21800000U) && (hclk < 24000000U))
-
 
166
    {
-
 
167
      /* hclk Clock Range between 21.8-24 MHz */
-
 
168
      UsbTrd = 0x9U;
-
 
169
    }
-
 
170
    else if ((hclk >= 24000000U) && (hclk < 27700000U))
-
 
171
    {
-
 
172
      /* hclk Clock Range between 24-27.7 MHz */
-
 
173
      UsbTrd = 0x8U;
-
 
174
    }
-
 
175
    else if ((hclk >= 27700000U) && (hclk < 32000000U))
-
 
176
    {
-
 
177
      /* hclk Clock Range between 27.7-32 MHz */
-
 
178
      UsbTrd = 0x7U;
-
 
179
    }
-
 
180
    else /* if(hclk >= 32000000) */
-
 
181
    {
-
 
182
      /* hclk Clock Range between 32-200 MHz */
-
 
183
      UsbTrd = 0x6U;
-
 
184
    }
-
 
185
  }
-
 
186
  else
-
 
187
  {
-
 
188
    UsbTrd = USBD_DEFAULT_TRDT_VALUE;
-
 
189
  }
-
 
190
 
-
 
191
  USBx->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
-
 
192
  USBx->GUSBCFG |= (uint32_t)((UsbTrd << 10) & USB_OTG_GUSBCFG_TRDT);
126
 
193
 
127
  /* Select FS Embedded PHY */
-
 
128
  USBx->GUSBCFG |= USB_OTG_GUSBCFG_PHYSEL;
-
 
129
 
-
 
130
  /* Reset after a PHY select and set Host mode */
-
 
131
  USB_CoreReset(USBx);
-
 
132
 
-
 
133
  /* Deactivate the power down*/
-
 
134
  USBx->GCCFG = USB_OTG_GCCFG_PWRDWN;
-
 
135
 
-
 
136
  return HAL_OK;
194
  return HAL_OK;
137
}
195
}
138
 
196
 
139
/**
197
/**
140
  * @brief  USB_EnableGlobalInt
198
  * @brief  USB_EnableGlobalInt
141
  *         Enables the controller's Global Int in the AHB Config reg
199
  *         Enables the controller's Global Int in the AHB Config reg
142
  * @param  USBx : Selected device
200
  * @param  USBx  Selected device
143
  * @retval HAL status
201
  * @retval HAL status
144
  */
202
  */
145
HAL_StatusTypeDef USB_EnableGlobalInt(USB_OTG_GlobalTypeDef *USBx)
203
HAL_StatusTypeDef USB_EnableGlobalInt(USB_OTG_GlobalTypeDef *USBx)
146
{
204
{
147
  USBx->GAHBCFG |= USB_OTG_GAHBCFG_GINT;
205
  USBx->GAHBCFG |= USB_OTG_GAHBCFG_GINT;
Line 149... Line 207...
149
}
207
}
150
 
208
 
151
/**
209
/**
152
  * @brief  USB_DisableGlobalInt
210
  * @brief  USB_DisableGlobalInt
153
  *         Disable the controller's Global Int in the AHB Config reg
211
  *         Disable the controller's Global Int in the AHB Config reg
154
  * @param  USBx : Selected device
212
  * @param  USBx  Selected device
155
  * @retval HAL status
213
  * @retval HAL status
156
*/
214
  */
157
HAL_StatusTypeDef USB_DisableGlobalInt(USB_OTG_GlobalTypeDef *USBx)
215
HAL_StatusTypeDef USB_DisableGlobalInt(USB_OTG_GlobalTypeDef *USBx)
158
{
216
{
159
  USBx->GAHBCFG &= ~USB_OTG_GAHBCFG_GINT;
217
  USBx->GAHBCFG &= ~USB_OTG_GAHBCFG_GINT;
160
  return HAL_OK;
218
  return HAL_OK;
161
}
219
}
162
 
220
 
163
/**
221
/**
164
  * @brief  USB_SetCurrentMode : Set functional mode
222
  * @brief  USB_SetCurrentMode Set functional mode
165
  * @param  USBx : Selected device
223
  * @param  USBx  Selected device
166
  * @param  mode :  current core mode
224
  * @param  mode  current core mode
167
  *          This parameter can be one of the these values:
225
  *          This parameter can be one of these values:
168
  *            @arg USB_DEVICE_MODE: Peripheral mode mode
226
  *            @arg USB_DEVICE_MODE Peripheral mode
169
  *            @arg USB_HOST_MODE: Host mode
227
  *            @arg USB_HOST_MODE Host mode
170
  *            @arg USB_DRD_MODE: Dual Role Device mode  
-
 
171
  * @retval HAL status
228
  * @retval HAL status
172
  */
229
  */
173
HAL_StatusTypeDef USB_SetCurrentMode(USB_OTG_GlobalTypeDef *USBx , USB_ModeTypeDef mode)
230
HAL_StatusTypeDef USB_SetCurrentMode(USB_OTG_GlobalTypeDef *USBx, USB_ModeTypeDef mode)
174
{
231
{
175
  USBx->GUSBCFG &= ~(USB_OTG_GUSBCFG_FHMOD | USB_OTG_GUSBCFG_FDMOD);
232
  USBx->GUSBCFG &= ~(USB_OTG_GUSBCFG_FHMOD | USB_OTG_GUSBCFG_FDMOD);
176
 
233
 
177
  if ( mode == USB_HOST_MODE)
234
  if (mode == USB_HOST_MODE)
178
  {
235
  {
179
    USBx->GUSBCFG |= USB_OTG_GUSBCFG_FHMOD;
236
    USBx->GUSBCFG |= USB_OTG_GUSBCFG_FHMOD;
180
  }
237
  }
181
  else if (mode == USB_DEVICE_MODE)
238
  else if (mode == USB_DEVICE_MODE)
182
  {
239
  {
183
    USBx->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD;
240
    USBx->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD;
-
 
241
  }
-
 
242
  else
-
 
243
  {
-
 
244
    return HAL_ERROR;
184
  }
245
  }
185
  HAL_Delay(50);
246
  HAL_Delay(50U);
186
 
247
 
187
  return HAL_OK;
248
  return HAL_OK;
188
}
249
}
189
 
250
 
190
/**
251
/**
191
  * @brief  USB_DevInit : Initializes the USB_OTG controller registers
252
  * @brief  USB_DevInit Initializes the USB_OTG controller registers
192
  *         for device mode
253
  *         for device mode
193
  * @param  USBx : Selected device
254
  * @param  USBx  Selected device
194
  * @param  cfg  : pointer to a USB_OTG_CfgTypeDef structure that contains
255
  * @param  cfg   pointer to a USB_OTG_CfgTypeDef structure that contains
195
  *         the configuration information for the specified USBx peripheral.
256
  *         the configuration information for the specified USBx peripheral.
196
  * @retval HAL status
257
  * @retval HAL status
197
  */
258
  */
198
HAL_StatusTypeDef USB_DevInit (USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
259
HAL_StatusTypeDef USB_DevInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
199
{
260
{
-
 
261
  HAL_StatusTypeDef ret = HAL_OK;
-
 
262
  uint32_t USBx_BASE = (uint32_t)USBx;
200
  uint32_t index = 0;
263
  uint32_t i;
201
 
264
 
202
  for (index = 0; index < 15 ; index++)
265
  for (i = 0U; i < 15U; i++)
203
  {
266
  {
204
    USBx->DIEPTXF[index] = 0;
267
    USBx->DIEPTXF[i] = 0U;
205
  }
268
  }
206
 
269
 
207
  /*Activate VBUS Sensing B */
270
  /* Enable HW VBUS sensing */
208
  USBx->GCCFG |= USB_OTG_GCCFG_VBUSBSEN;
271
  USBx->GCCFG |= USB_OTG_GCCFG_VBUSBSEN;
209
 
272
 
210
  /* Restart the Phy Clock */
273
  /* Restart the Phy Clock */
211
  USBx_PCGCCTL = 0;
274
  USBx_PCGCCTL = 0U;
212
 
275
 
213
  /* Device mode configuration */
276
  /* Device mode configuration */
214
  USBx_DEVICE->DCFG |= DCFG_FRAME_INTERVAL_80;
277
  USBx_DEVICE->DCFG |= DCFG_FRAME_INTERVAL_80;
215
 
278
 
216
  /* Set Full speed phy */
279
  /* Set Core speed to Full speed mode */
217
  USB_SetDevSpeed (USBx , USB_OTG_SPEED_FULL);
280
  (void)USB_SetDevSpeed(USBx, USB_OTG_SPEED_FULL);
218
 
281
 
219
  /* Flush the FIFOs */
282
  /* Flush the FIFOs */
220
  USB_FlushTxFifo(USBx , 0x10); /* all Tx FIFOs */
283
  if (USB_FlushTxFifo(USBx, 0x10U) != HAL_OK) /* all Tx FIFOs */
-
 
284
  {
-
 
285
    ret = HAL_ERROR;
-
 
286
  }
-
 
287
 
221
  USB_FlushRxFifo(USBx);
288
  if (USB_FlushRxFifo(USBx) != HAL_OK)
-
 
289
  {
-
 
290
    ret = HAL_ERROR;
222
 
291
  }
-
 
292
 
223
  /* Clear all pending Device Interrupts */
293
  /* Clear all pending Device Interrupts */
224
  USBx_DEVICE->DIEPMSK = 0;
294
  USBx_DEVICE->DIEPMSK = 0U;
225
  USBx_DEVICE->DOEPMSK = 0;
295
  USBx_DEVICE->DOEPMSK = 0U;
226
  USBx_DEVICE->DAINT = 0xFFFFFFFF;
-
 
227
  USBx_DEVICE->DAINTMSK = 0;
296
  USBx_DEVICE->DAINTMSK = 0U;
228
 
297
 
229
  for (index = 0; index < cfg.dev_endpoints; index++)
298
  for (i = 0U; i < cfg.dev_endpoints; i++)
230
  {
299
  {
231
    if ((USBx_INEP(index)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
300
    if ((USBx_INEP(i)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
232
    {
301
    {
-
 
302
      if (i == 0U)
-
 
303
      {
-
 
304
        USBx_INEP(i)->DIEPCTL = USB_OTG_DIEPCTL_SNAK;
-
 
305
      }
-
 
306
      else
-
 
307
      {
233
      USBx_INEP(index)->DIEPCTL = (USB_OTG_DIEPCTL_EPDIS | USB_OTG_DIEPCTL_SNAK);
308
        USBx_INEP(i)->DIEPCTL = USB_OTG_DIEPCTL_EPDIS | USB_OTG_DIEPCTL_SNAK;
-
 
309
      }
234
    }
310
    }
235
    else
311
    else
236
    {
312
    {
237
      USBx_INEP(index)->DIEPCTL = 0;
313
      USBx_INEP(i)->DIEPCTL = 0U;
238
    }
314
    }
239
   
315
 
240
    USBx_INEP(index)->DIEPTSIZ = 0;
316
    USBx_INEP(i)->DIEPTSIZ = 0U;
241
    USBx_INEP(index)->DIEPINT  = 0xFF;
317
    USBx_INEP(i)->DIEPINT  = 0xFB7FU;
242
  }
318
  }
243
 
319
 
244
  for (index = 0; index < cfg.dev_endpoints; index++)
320
  for (i = 0U; i < cfg.dev_endpoints; i++)
245
  {
321
  {
246
    if ((USBx_OUTEP(index)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
322
    if ((USBx_OUTEP(i)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
247
    {
323
    {
-
 
324
      if (i == 0U)
-
 
325
      {
-
 
326
        USBx_OUTEP(i)->DOEPCTL = USB_OTG_DOEPCTL_SNAK;
-
 
327
      }
-
 
328
      else
-
 
329
      {
248
      USBx_OUTEP(index)->DOEPCTL = (USB_OTG_DOEPCTL_EPDIS | USB_OTG_DOEPCTL_SNAK);
330
        USBx_OUTEP(i)->DOEPCTL = USB_OTG_DOEPCTL_EPDIS | USB_OTG_DOEPCTL_SNAK;
-
 
331
      }
249
    }
332
    }
250
    else
333
    else
251
    {
334
    {
252
      USBx_OUTEP(index)->DOEPCTL = 0;
335
      USBx_OUTEP(i)->DOEPCTL = 0U;
253
    }
336
    }
254
   
337
 
255
    USBx_OUTEP(index)->DOEPTSIZ = 0;
338
    USBx_OUTEP(i)->DOEPTSIZ = 0U;
256
    USBx_OUTEP(index)->DOEPINT  = 0xFF;
339
    USBx_OUTEP(i)->DOEPINT  = 0xFB7FU;
257
  }
340
  }
258
 
341
 
259
  USBx_DEVICE->DIEPMSK &= ~(USB_OTG_DIEPMSK_TXFURM);
342
  USBx_DEVICE->DIEPMSK &= ~(USB_OTG_DIEPMSK_TXFURM);
260
 
343
 
261
  /* Disable all interrupts. */
344
  /* Disable all interrupts. */
262
  USBx->GINTMSK = 0;
345
  USBx->GINTMSK = 0U;
263
 
346
 
264
  /* Clear any pending interrupts */
347
  /* Clear any pending interrupts */
265
  USBx->GINTSTS = 0xBFFFFFFF;
348
  USBx->GINTSTS = 0xBFFFFFFFU;
266
 
349
 
267
  /* Enable the common interrupts */
350
  /* Enable the common interrupts */
268
  USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
351
  USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
269
 
352
 
270
  /* Enable interrupts matching to the Device mode ONLY */
353
  /* Enable interrupts matching to the Device mode ONLY */
271
  USBx->GINTMSK |= (USB_OTG_GINTMSK_USBSUSPM | USB_OTG_GINTMSK_USBRST |\
354
  USBx->GINTMSK |= USB_OTG_GINTMSK_USBSUSPM | USB_OTG_GINTMSK_USBRST |
272
                    USB_OTG_GINTMSK_ENUMDNEM | USB_OTG_GINTMSK_IEPINT |\
355
                   USB_OTG_GINTMSK_ENUMDNEM | USB_OTG_GINTMSK_IEPINT |
273
                    USB_OTG_GINTMSK_OEPINT   | USB_OTG_GINTMSK_IISOIXFRM|\
356
                   USB_OTG_GINTMSK_OEPINT   | USB_OTG_GINTMSK_IISOIXFRM |
274
                    USB_OTG_GINTMSK_PXFRM_IISOOXFRM | USB_OTG_GINTMSK_WUIM);
357
                   USB_OTG_GINTMSK_PXFRM_IISOOXFRM | USB_OTG_GINTMSK_WUIM;
275
 
358
 
276
  if(cfg.Sof_enable)
359
  if (cfg.Sof_enable != 0U)
277
  {
360
  {
278
    USBx->GINTMSK |= USB_OTG_GINTMSK_SOFM;
361
    USBx->GINTMSK |= USB_OTG_GINTMSK_SOFM;
279
  }
362
  }
280
 
363
 
281
  if (cfg.vbus_sensing_enable == ENABLE)
364
  if (cfg.vbus_sensing_enable == 1U)
282
  {
365
  {
283
    USBx->GINTMSK |= (USB_OTG_GINTMSK_SRQIM | USB_OTG_GINTMSK_OTGINT);
366
    USBx->GINTMSK |= (USB_OTG_GINTMSK_SRQIM | USB_OTG_GINTMSK_OTGINT);
284
  }
367
  }
285
 
368
 
286
  return HAL_OK;
369
  return ret;
287
}
370
}
288
 
371
 
289
/**
372
/**
290
  * @brief  USB_OTG_FlushTxFifo : Flush a Tx FIFO
373
  * @brief  USB_OTG_FlushTxFifo : Flush a Tx FIFO
291
  * @param  USBx : Selected device
374
  * @param  USBx  Selected device
292
  * @param  num : FIFO number
375
  * @param  num  FIFO number
293
  *         This parameter can be a value from 1 to 15
376
  *         This parameter can be a value from 1 to 15
294
            15 means Flush all Tx FIFOs
377
            15 means Flush all Tx FIFOs
295
  * @retval HAL status
378
  * @retval HAL status
296
  */
379
  */
297
HAL_StatusTypeDef USB_FlushTxFifo (USB_OTG_GlobalTypeDef *USBx, uint32_t num )
380
HAL_StatusTypeDef USB_FlushTxFifo(USB_OTG_GlobalTypeDef *USBx, uint32_t num)
298
{
381
{
299
  uint32_t count = 0;
382
  uint32_t count = 0U;
300
 
383
 
301
  USBx->GRSTCTL = (USB_OTG_GRSTCTL_TXFFLSH |(uint32_t)(num << 6));
384
  USBx->GRSTCTL = (USB_OTG_GRSTCTL_TXFFLSH | (num << 6));
302
 
385
 
303
  do
386
  do
304
  {
387
  {
305
    if (++count > 200000)
388
    if (++count > 200000U)
306
    {
389
    {
307
      return HAL_TIMEOUT;
390
      return HAL_TIMEOUT;
308
    }
391
    }
309
  }
-
 
310
  while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_TXFFLSH) == USB_OTG_GRSTCTL_TXFFLSH);
392
  } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_TXFFLSH) == USB_OTG_GRSTCTL_TXFFLSH);
311
 
393
 
312
  return HAL_OK;
394
  return HAL_OK;
313
}
395
}
314
 
396
 
315
/**
397
/**
316
  * @brief  USB_FlushRxFifo : Flush Rx FIFO
398
  * @brief  USB_FlushRxFifo : Flush Rx FIFO
317
  * @param  USBx : Selected device
399
  * @param  USBx  Selected device
318
  * @retval HAL status
400
  * @retval HAL status
319
  */
401
  */
320
HAL_StatusTypeDef USB_FlushRxFifo(USB_OTG_GlobalTypeDef *USBx)
402
HAL_StatusTypeDef USB_FlushRxFifo(USB_OTG_GlobalTypeDef *USBx)
321
{
403
{
322
  uint32_t count = 0;
404
  uint32_t count = 0;
323
 
405
 
324
  USBx->GRSTCTL = USB_OTG_GRSTCTL_RXFFLSH;
406
  USBx->GRSTCTL = USB_OTG_GRSTCTL_RXFFLSH;
325
 
407
 
326
  do
408
  do
327
  {
409
  {
328
    if (++count > 200000)
410
    if (++count > 200000U)
329
    {
411
    {
330
      return HAL_TIMEOUT;
412
      return HAL_TIMEOUT;
331
    }
413
    }
332
  }
-
 
333
  while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_RXFFLSH) == USB_OTG_GRSTCTL_RXFFLSH);
414
  } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_RXFFLSH) == USB_OTG_GRSTCTL_RXFFLSH);
334
 
415
 
335
  return HAL_OK;
416
  return HAL_OK;
336
}
417
}
337
 
418
 
338
/**
419
/**
339
  * @brief  USB_SetDevSpeed :Initializes the DevSpd field of DCFG register
420
  * @brief  USB_SetDevSpeed  Initializes the DevSpd field of DCFG register
340
  *         depending the PHY type and the enumeration speed of the device.
421
  *         depending the PHY type and the enumeration speed of the device.
341
  * @param  USBx : Selected device
422
  * @param  USBx  Selected device
342
  * @param  speed : device speed
423
  * @param  speed  device speed
343
  *          This parameter can be one of the these values:
424
  *          This parameter can be one of these values:
344
  *            @arg USB_OTG_SPEED_FULL: Full speed mode
425
  *            @arg USB_OTG_SPEED_FULL: Full speed mode
345
  *            @arg USB_OTG_SPEED_LOW: Low speed mode
-
 
346
  * @retval  Hal status
426
  * @retval  Hal status
347
  */
427
  */
348
HAL_StatusTypeDef USB_SetDevSpeed(USB_OTG_GlobalTypeDef *USBx , uint8_t speed)
428
HAL_StatusTypeDef USB_SetDevSpeed(USB_OTG_GlobalTypeDef *USBx, uint8_t speed)
349
{
429
{
350
  /* Prevent unused argument(s) compilation warning */
430
  uint32_t USBx_BASE = (uint32_t)USBx;
351
  UNUSED(USBx);
-
 
352
 
431
 
353
  USBx_DEVICE->DCFG |= speed;
432
  USBx_DEVICE->DCFG |= speed;
354
  return HAL_OK;
433
  return HAL_OK;
355
}
434
}
356
 
435
 
357
/**
436
/**
358
  * @brief  USB_GetDevSpeed :Return the  Dev Speed
437
  * @brief  USB_GetDevSpeed  Return the Dev Speed
359
  * @param  USBx : Selected device
438
  * @param  USBx  Selected device
360
  * @retval speed : device speed
439
  * @retval speed  device speed
361
  *          This parameter can be one of the these values:
440
  *          This parameter can be one of these values:
362
  *            @arg USB_OTG_SPEED_FULL: Full speed mode
441
  *            @arg PCD_SPEED_FULL: Full speed mode
363
  *            @arg USB_OTG_SPEED_LOW: Low speed mode
-
 
364
  */
442
  */
365
uint8_t USB_GetDevSpeed(USB_OTG_GlobalTypeDef *USBx)
443
uint8_t USB_GetDevSpeed(USB_OTG_GlobalTypeDef *USBx)
366
{
444
{
-
 
445
  uint32_t USBx_BASE = (uint32_t)USBx;
367
  uint8_t speed = 0;
446
  uint8_t speed;
368
  /* Prevent unused argument(s) compilation warning */
447
  uint32_t DevEnumSpeed = USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD;
369
  UNUSED(USBx);
-
 
370
 
448
 
371
  if (((USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD) == DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ)||
449
  if ((DevEnumSpeed == DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ) ||
372
      ((USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD) == DSTS_ENUMSPD_FS_PHY_48MHZ))
450
      (DevEnumSpeed == DSTS_ENUMSPD_FS_PHY_48MHZ))
373
  {
451
  {
374
    speed = USB_OTG_SPEED_FULL;
452
    speed = USBD_FS_SPEED;
375
  }
453
  }
376
  else if((USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD) == DSTS_ENUMSPD_LS_PHY_6MHZ)
-
 
-
 
454
  else
377
  {
455
  {
378
    speed = USB_OTG_SPEED_LOW;
456
    speed = 0xFU;
379
  }
457
  }
380
 
458
 
381
  return speed;
459
  return speed;
382
}
460
}
383
 
461
 
384
/**
462
/**
385
  * @brief  Activate and configure an endpoint
463
  * @brief  Activate and configure an endpoint
386
  * @param  USBx : Selected device
464
  * @param  USBx  Selected device
387
  * @param  ep: pointer to endpoint structure
465
  * @param  ep pointer to endpoint structure
388
  * @retval HAL status
466
  * @retval HAL status
389
  */
467
  */
390
HAL_StatusTypeDef USB_ActivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
468
HAL_StatusTypeDef USB_ActivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
391
{
469
{
392
  /* Prevent unused argument(s) compilation warning */
470
  uint32_t USBx_BASE = (uint32_t)USBx;
393
  UNUSED(USBx);
471
  uint32_t epnum = (uint32_t)ep->num;
394
 
472
 
395
  if (ep->is_in)
473
  if (ep->is_in == 1U)
396
  {
474
  {
-
 
475
    USBx_DEVICE->DAINTMSK |= USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK));
-
 
476
 
397
    /* Assign a Tx FIFO */
477
    if ((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_USBAEP) == 0U)
-
 
478
    {
398
    ep->tx_fifo_num = ep->num;
479
      USBx_INEP(epnum)->DIEPCTL |= (ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ) |
-
 
480
                                   ((uint32_t)ep->type << 18) | (epnum << 22) |
-
 
481
                                   USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
-
 
482
                                   USB_OTG_DIEPCTL_USBAEP;
-
 
483
    }
399
  }
484
  }
400
  /* Set initial data PID. */
485
  else
401
  if (ep->type == EP_TYPE_BULK )
-
 
402
  {
486
  {
-
 
487
    USBx_DEVICE->DAINTMSK |= USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16);
-
 
488
 
403
    ep->data_pid_start = 0;
489
    if (((USBx_OUTEP(epnum)->DOEPCTL) & USB_OTG_DOEPCTL_USBAEP) == 0U)
-
 
490
    {
-
 
491
      USBx_OUTEP(epnum)->DOEPCTL |= (ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ) |
-
 
492
                                    ((uint32_t)ep->type << 18) |
-
 
493
                                    USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
-
 
494
                                    USB_OTG_DOEPCTL_USBAEP;
-
 
495
    }
404
  }
496
  }
-
 
497
  return HAL_OK;
405
 
498
}
-
 
499
 
-
 
500
/**
-
 
501
  * @brief  Activate and configure a dedicated endpoint
-
 
502
  * @param  USBx  Selected device
-
 
503
  * @param  ep pointer to endpoint structure
406
  if (ep->is_in == 1)
504
  * @retval HAL status
407
  {
505
  */
408
   USBx_DEVICE->DAINTMSK |= USB_OTG_DAINTMSK_IEPM & ((1 << (ep->num)));
506
HAL_StatusTypeDef USB_ActivateDedicatedEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
-
 
507
{
-
 
508
  uint32_t USBx_BASE = (uint32_t)USBx;
-
 
509
  uint32_t epnum = (uint32_t)ep->num;
409
 
510
 
-
 
511
  /* Read DEPCTLn register */
-
 
512
  if (ep->is_in == 1U)
-
 
513
  {
410
    if (((USBx_INEP(ep->num)->DIEPCTL) & USB_OTG_DIEPCTL_USBAEP) == 0)
514
    if (((USBx_INEP(epnum)->DIEPCTL) & USB_OTG_DIEPCTL_USBAEP) == 0U)
411
    {
515
    {
412
      USBx_INEP(ep->num)->DIEPCTL |= ((ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ ) | (ep->type << 18 ) |\
516
      USBx_INEP(epnum)->DIEPCTL |= (ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ) |
-
 
517
                                   ((uint32_t)ep->type << 18) | (epnum << 22) |
413
        ((ep->num) << 22 ) | (USB_OTG_DIEPCTL_SD0PID_SEVNFRM) | (USB_OTG_DIEPCTL_USBAEP));
518
                                   USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
-
 
519
                                   USB_OTG_DIEPCTL_USBAEP;
414
    }
520
    }
-
 
521
 
-
 
522
    USBx_DEVICE->DEACHMSK |= USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK));
415
  }
523
  }
416
  else
524
  else
417
  {
525
  {
418
     USBx_DEVICE->DAINTMSK |= USB_OTG_DAINTMSK_OEPM & ((1 << (ep->num)) << 16);
-
 
419
     
-
 
420
    if (((USBx_OUTEP(ep->num)->DOEPCTL) & USB_OTG_DOEPCTL_USBAEP) == 0)
526
    if (((USBx_OUTEP(epnum)->DOEPCTL) & USB_OTG_DOEPCTL_USBAEP) == 0U)
421
    {
527
    {
422
      USBx_OUTEP(ep->num)->DOEPCTL |= ((ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ ) | (ep->type << 18 ) |\
528
      USBx_OUTEP(epnum)->DOEPCTL |= (ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ) |
-
 
529
                                    ((uint32_t)ep->type << 18) | (epnum << 22) |
423
       (USB_OTG_DIEPCTL_SD0PID_SEVNFRM)| (USB_OTG_DOEPCTL_USBAEP));
530
                                    USB_OTG_DOEPCTL_USBAEP;
424
    }
531
    }
-
 
532
 
-
 
533
    USBx_DEVICE->DEACHMSK |= USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16);
425
  }
534
  }
426
 
535
 
427
  return HAL_OK;
536
  return HAL_OK;
428
}
537
}
429
 
538
 
430
/**
539
/**
431
  * @brief  De-activate and de-initialize an endpoint
540
  * @brief  De-activate and de-initialize an endpoint
432
  * @param  USBx : Selected device
541
  * @param  USBx  Selected device
433
  * @param  ep: pointer to endpoint structure
542
  * @param  ep pointer to endpoint structure
434
  * @retval HAL status
543
  * @retval HAL status
435
  */
544
  */
436
HAL_StatusTypeDef USB_DeactivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
545
HAL_StatusTypeDef USB_DeactivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
437
{
546
{
438
  /* Prevent unused argument(s) compilation warning */
547
  uint32_t USBx_BASE = (uint32_t)USBx;
439
  UNUSED(USBx);
548
  uint32_t epnum = (uint32_t)ep->num;
440
 
549
 
441
  /* Read DEPCTLn register */
550
  /* Read DEPCTLn register */
442
  if (ep->is_in == 1)
551
  if (ep->is_in == 1U)
443
  {
552
  {
-
 
553
    if ((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
-
 
554
    {
-
 
555
      USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SNAK;
-
 
556
      USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_EPDIS;
-
 
557
    }
-
 
558
 
444
    USBx_DEVICE->DEACHMSK &= ~(USB_OTG_DAINTMSK_IEPM & ((1 << (ep->num))));
559
    USBx_DEVICE->DEACHMSK &= ~(USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK)));
445
    USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_IEPM & ((1 << (ep->num))));
560
    USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK)));
446
    USBx_INEP(ep->num)->DIEPCTL &= ~ USB_OTG_DIEPCTL_USBAEP;
561
    USBx_INEP(epnum)->DIEPCTL &= ~(USB_OTG_DIEPCTL_USBAEP |
-
 
562
                                   USB_OTG_DIEPCTL_MPSIZ |
-
 
563
                                   USB_OTG_DIEPCTL_TXFNUM |
-
 
564
                                   USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
-
 
565
                                   USB_OTG_DIEPCTL_EPTYP);
447
  }
566
  }
448
  else
567
  else
449
  {
568
  {
-
 
569
    if ((USBx_OUTEP(epnum)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
-
 
570
    {
-
 
571
      USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
-
 
572
      USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_EPDIS;
-
 
573
    }
-
 
574
 
450
    USBx_DEVICE->DEACHMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((1 << (ep->num)) << 16));
575
    USBx_DEVICE->DEACHMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16));
451
    USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((1 << (ep->num)) << 16));
576
    USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16));
452
    USBx_OUTEP(ep->num)->DOEPCTL &= ~USB_OTG_DOEPCTL_USBAEP;
577
    USBx_OUTEP(epnum)->DOEPCTL &= ~(USB_OTG_DOEPCTL_USBAEP |
-
 
578
                                    USB_OTG_DOEPCTL_MPSIZ |
-
 
579
                                    USB_OTG_DOEPCTL_SD0PID_SEVNFRM |
-
 
580
                                    USB_OTG_DOEPCTL_EPTYP);
453
  }
581
  }
-
 
582
 
-
 
583
  return HAL_OK;
-
 
584
}
-
 
585
 
-
 
586
/**
-
 
587
  * @brief  De-activate and de-initialize a dedicated endpoint
-
 
588
  * @param  USBx  Selected device
-
 
589
  * @param  ep pointer to endpoint structure
-
 
590
  * @retval HAL status
-
 
591
  */
-
 
592
HAL_StatusTypeDef USB_DeactivateDedicatedEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
-
 
593
{
-
 
594
  uint32_t USBx_BASE = (uint32_t)USBx;
-
 
595
  uint32_t epnum = (uint32_t)ep->num;
-
 
596
 
-
 
597
  /* Read DEPCTLn register */
-
 
598
  if (ep->is_in == 1U)
-
 
599
  {
-
 
600
    if ((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
-
 
601
    {
-
 
602
      USBx_INEP(epnum)->DIEPCTL  |= USB_OTG_DIEPCTL_SNAK;
-
 
603
      USBx_INEP(epnum)->DIEPCTL  |= USB_OTG_DIEPCTL_EPDIS;
-
 
604
    }
-
 
605
 
-
 
606
    USBx_INEP(epnum)->DIEPCTL &= ~ USB_OTG_DIEPCTL_USBAEP;
-
 
607
    USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK)));
-
 
608
  }
-
 
609
  else
-
 
610
  {
-
 
611
    if ((USBx_OUTEP(epnum)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
-
 
612
    {
-
 
613
      USBx_OUTEP(epnum)->DOEPCTL  |= USB_OTG_DOEPCTL_SNAK;
-
 
614
      USBx_OUTEP(epnum)->DOEPCTL  |= USB_OTG_DOEPCTL_EPDIS;
-
 
615
    }
-
 
616
 
-
 
617
    USBx_OUTEP(epnum)->DOEPCTL &= ~USB_OTG_DOEPCTL_USBAEP;
-
 
618
    USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16));
-
 
619
  }
-
 
620
 
454
  return HAL_OK;
621
  return HAL_OK;
455
}
622
}
456
 
623
 
457
/**
624
/**
458
  * @brief  USB_EPStartXfer : setup and starts a transfer over an EP
625
  * @brief  USB_EPStartXfer : setup and starts a transfer over an EP
459
  * @param  USBx : Selected device
626
  * @param  USBx  Selected device
460
  * @param  ep: pointer to endpoint structure
627
  * @param  ep pointer to endpoint structure
461
  * @retval HAL status
628
  * @retval HAL status
462
  */
629
  */
463
HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDef *ep)
630
HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
464
{
631
{
465
  uint16_t pktcnt = 0;
632
  uint32_t USBx_BASE = (uint32_t)USBx;
466
  /* Prevent unused argument(s) compilation warning */
633
  uint32_t epnum = (uint32_t)ep->num;
467
  UNUSED(USBx);
634
  uint16_t pktcnt;
468
 
635
 
469
  /* IN endpoint */
636
  /* IN endpoint */
470
  if (ep->is_in == 1)
637
  if (ep->is_in == 1U)
471
  {
638
  {
472
    /* Zero Length Packet? */
639
    /* Zero Length Packet? */
473
    if (ep->xfer_len == 0)
640
    if (ep->xfer_len == 0U)
474
    {
641
    {
475
      USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
642
      USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
476
      USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1 << 19)) ;
643
      USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1U << 19));
477
      USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
644
      USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
478
    }
645
    }
479
    else
646
    else
480
    {
647
    {
481
      /* Program the transfer size and packet count
648
      /* Program the transfer size and packet count
482
      * as follows: xfersize = N * maxpacket +
649
      * as follows: xfersize = N * maxpacket +
483
      * short_packet pktcnt = N + (short_packet
650
      * short_packet pktcnt = N + (short_packet
484
      * exist ? 1 : 0)
651
      * exist ? 1 : 0)
485
      */
652
      */
486
      USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
653
      USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
487
      USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
654
      USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
488
      USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (((ep->xfer_len + ep->maxpacket -1)/ ep->maxpacket) << 19)) ;
655
      USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (((ep->xfer_len + ep->maxpacket - 1U) / ep->maxpacket) << 19));
489
      USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_XFRSIZ & ep->xfer_len);
656
      USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_XFRSIZ & ep->xfer_len);
490
     
657
 
491
      if (ep->type == EP_TYPE_ISOC)
658
      if (ep->type == EP_TYPE_ISOC)
492
      {
659
      {
493
        USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_MULCNT);
660
        USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_MULCNT);
494
        USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_MULCNT & (1 << 29));
661
        USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_MULCNT & (1U << 29));
495
      }
662
      }
496
    }
663
    }
-
 
664
    /* EP enable, IN data in FIFO */
-
 
665
    USBx_INEP(epnum)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
497
   
666
 
498
    if (ep->type != EP_TYPE_ISOC)
667
    if (ep->type != EP_TYPE_ISOC)
499
    {
668
    {
500
      /* Enable the Tx FIFO Empty Interrupt for this EP */
669
      /* Enable the Tx FIFO Empty Interrupt for this EP */
501
      if (ep->xfer_len > 0)
670
      if (ep->xfer_len > 0U)
502
      {
671
      {
503
        USBx_DEVICE->DIEPEMPMSK |= 1 << ep->num;
672
        USBx_DEVICE->DIEPEMPMSK |= 1UL << (ep->num & EP_ADDR_MSK);
504
      }
673
      }
505
    }
674
    }
506
   
675
    else
507
    if (ep->type == EP_TYPE_ISOC)
-
 
508
    {
676
    {
509
      if ((USBx_DEVICE->DSTS & ( 1 << 8 )) == 0)
677
      if ((USBx_DEVICE->DSTS & (1U << 8)) == 0U)
510
      {
678
      {
511
        USBx_INEP(ep->num)->DIEPCTL |= USB_OTG_DIEPCTL_SODDFRM;
679
        USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SODDFRM;
512
      }
680
      }
513
      else
681
      else
514
      {
682
      {
515
        USBx_INEP(ep->num)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM;
683
        USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM;
516
      }
684
      }
517
    }
-
 
518
   
-
 
519
    /* EP enable, IN data in FIFO */
-
 
520
    USBx_INEP(ep->num)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
-
 
521
   
685
 
522
    if (ep->type == EP_TYPE_ISOC)
-
 
523
    {
-
 
524
      USB_WritePacket(USBx, ep->xfer_buff, ep->num, ep->xfer_len);
686
      (void)USB_WritePacket(USBx, ep->xfer_buff, ep->num, (uint16_t)ep->xfer_len);
525
    }
687
    }
526
  }
688
  }
527
  else /* OUT endpoint */
689
  else /* OUT endpoint */
528
  {
690
  {
529
    /* Program the transfer size and packet count as follows:
691
    /* Program the transfer size and packet count as follows:
530
    * pktcnt = N
692
    * pktcnt = N
531
    * xfersize = N * maxpacket
693
    * xfersize = N * maxpacket
532
    */
694
    */
533
    USBx_OUTEP(ep->num)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_XFRSIZ);
695
    USBx_OUTEP(epnum)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_XFRSIZ);
534
    USBx_OUTEP(ep->num)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_PKTCNT);
696
    USBx_OUTEP(epnum)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_PKTCNT);
535
   
697
 
536
    if (ep->xfer_len == 0)
698
    if (ep->xfer_len == 0U)
537
    {
699
    {
538
      USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & ep->maxpacket);
700
      USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & ep->maxpacket);
539
      USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1 << 19));
701
      USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19));
540
    }
702
    }
541
    else
703
    else
542
    {
704
    {
543
      pktcnt = (ep->xfer_len + ep->maxpacket -1)/ ep->maxpacket;
705
      pktcnt = (uint16_t)((ep->xfer_len + ep->maxpacket - 1U) / ep->maxpacket);
544
      USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (pktcnt << 19));
706
      USBx_OUTEP(epnum)->DOEPTSIZ |= USB_OTG_DOEPTSIZ_PKTCNT & ((uint32_t)pktcnt << 19);
545
      USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & (ep->maxpacket * pktcnt));
707
      USBx_OUTEP(epnum)->DOEPTSIZ |= USB_OTG_DOEPTSIZ_XFRSIZ & (ep->maxpacket * pktcnt);
546
    }
708
    }
547
   
709
 
548
    if (ep->type == EP_TYPE_ISOC)
710
    if (ep->type == EP_TYPE_ISOC)
549
    {
711
    {
550
      if ((USBx_DEVICE->DSTS & ( 1 << 8 )) == 0)
712
      if ((USBx_DEVICE->DSTS & (1U << 8)) == 0U)
551
      {
713
      {
552
        USBx_OUTEP(ep->num)->DOEPCTL |= USB_OTG_DOEPCTL_SODDFRM;
714
        USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SODDFRM;
553
      }
715
      }
554
      else
716
      else
555
      {
717
      {
556
        USBx_OUTEP(ep->num)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM;
718
        USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM;
557
      }
719
      }
558
    }
720
    }
559
    /* EP enable */
721
    /* EP enable */
560
    USBx_OUTEP(ep->num)->DOEPCTL |= (USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
722
    USBx_OUTEP(epnum)->DOEPCTL |= (USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
561
  }
723
  }
562
 
724
 
563
  return HAL_OK;
725
  return HAL_OK;
564
}
726
}
565
 
727
 
566
/**
728
/**
567
  * @brief  USB_EP0StartXfer : setup and starts a transfer over the EP  0
729
  * @brief  USB_EP0StartXfer : setup and starts a transfer over the EP  0
568
  * @param  USBx : Selected device
730
  * @param  USBx  Selected device
569
  * @param  ep: pointer to endpoint structure
731
  * @param  ep pointer to endpoint structure
570
  * @retval HAL status
732
  * @retval HAL status
571
  */
733
  */
572
HAL_StatusTypeDef USB_EP0StartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDef *ep)
734
HAL_StatusTypeDef USB_EP0StartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
573
{
735
{
574
  /* Prevent unused argument(s) compilation warning */
736
  uint32_t USBx_BASE = (uint32_t)USBx;
575
  UNUSED(USBx);
737
  uint32_t epnum = (uint32_t)ep->num;
576
 
738
 
577
  /* IN endpoint */
739
  /* IN endpoint */
578
  if (ep->is_in == 1)
740
  if (ep->is_in == 1U)
579
  {
741
  {
580
    /* Zero Length Packet? */
742
    /* Zero Length Packet? */
581
    if (ep->xfer_len == 0)
743
    if (ep->xfer_len == 0U)
582
    {
744
    {
583
      USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
745
      USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
584
      USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1 << 19));
746
      USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1U << 19));
585
      USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
747
      USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
586
    }
748
    }
587
    else
749
    else
588
    {
750
    {
589
      /* Program the transfer size and packet count
751
      /* Program the transfer size and packet count
590
      * as follows: xfersize = N * maxpacket +
752
      * as follows: xfersize = N * maxpacket +
591
      * short_packet pktcnt = N + (short_packet
753
      * short_packet pktcnt = N + (short_packet
592
      * exist ? 1 : 0)
754
      * exist ? 1 : 0)
593
      */
755
      */
594
      USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
756
      USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
595
      USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
757
      USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
596
     
758
 
597
      if(ep->xfer_len > ep->maxpacket)
759
      if (ep->xfer_len > ep->maxpacket)
598
      {
760
      {
599
        ep->xfer_len = ep->maxpacket;
761
        ep->xfer_len = ep->maxpacket;
600
      }
762
      }
601
      USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1 << 19));
763
      USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1U << 19));
602
      USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_XFRSIZ & ep->xfer_len);
764
      USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_XFRSIZ & ep->xfer_len);
603
    }
765
    }
-
 
766
 
-
 
767
    /* EP enable, IN data in FIFO */
-
 
768
    USBx_INEP(epnum)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
604
   
769
 
605
    /* Enable the Tx FIFO Empty Interrupt for this EP */
770
    /* Enable the Tx FIFO Empty Interrupt for this EP */
606
    if (ep->xfer_len > 0)
771
    if (ep->xfer_len > 0U)
607
    {
772
    {
608
      USBx_DEVICE->DIEPEMPMSK |= 1 << (ep->num);
773
      USBx_DEVICE->DIEPEMPMSK |= 1UL << (ep->num & EP_ADDR_MSK);
609
    }
774
    }
610
   
-
 
611
    /* EP enable, IN data in FIFO */
-
 
612
    USBx_INEP(ep->num)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);  
-
 
613
  }
775
  }
614
  else /* OUT endpoint */
776
  else /* OUT endpoint */
615
  {
777
  {
616
    /* Program the transfer size and packet count as follows:
778
    /* Program the transfer size and packet count as follows:
617
    * pktcnt = N
779
    * pktcnt = N
618
    * xfersize = N * maxpacket
780
    * xfersize = N * maxpacket
619
    */
781
    */
620
    USBx_OUTEP(ep->num)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_XFRSIZ);
782
    USBx_OUTEP(epnum)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_XFRSIZ);
621
    USBx_OUTEP(ep->num)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_PKTCNT);
783
    USBx_OUTEP(epnum)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_PKTCNT);
622
   
784
 
623
    if (ep->xfer_len > 0)
785
    if (ep->xfer_len > 0U)
624
    {
786
    {
625
      ep->xfer_len = ep->maxpacket;
787
      ep->xfer_len = ep->maxpacket;
626
    }
788
    }
627
   
789
 
628
    USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1 << 19));
790
    USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19));
629
    USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & (ep->maxpacket));
791
    USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & (ep->maxpacket));
630
   
792
 
631
    /* EP enable */
793
    /* EP enable */
632
    USBx_OUTEP(ep->num)->DOEPCTL |= (USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
794
    USBx_OUTEP(epnum)->DOEPCTL |= (USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
633
  }
795
  }
634
 
796
 
635
  return HAL_OK;
797
  return HAL_OK;
636
}
798
}
637
 
799
 
638
/**
800
/**
639
  * @brief  USB_WritePacket : Writes a packet into the Tx FIFO associated
801
  * @brief  USB_WritePacket : Writes a packet into the Tx FIFO associated
640
  *         with the EP/channel
802
  *         with the EP/channel
641
  * @param  USBx : Selected device
803
  * @param  USBx  Selected device
642
  * @param  src :  pointer to source buffer
804
  * @param  src   pointer to source buffer
643
  * @param  ch_ep_num : endpoint or host channel number
805
  * @param  ch_ep_num  endpoint or host channel number
644
  * @param  len : Number of bytes to write
806
  * @param  len  Number of bytes to write
645
  * @retval HAL status
807
  * @retval HAL status
646
  */
808
  */
647
HAL_StatusTypeDef USB_WritePacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *src, uint8_t ch_ep_num, uint16_t len)
809
HAL_StatusTypeDef USB_WritePacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *src,
-
 
810
                                  uint8_t ch_ep_num, uint16_t len)
648
{
811
{
649
  uint32_t count32b = 0 , index = 0;
812
  uint32_t USBx_BASE = (uint32_t)USBx;
650
  /* Prevent unused argument(s) compilation warning */
813
  uint32_t *pSrc = (uint32_t *)src;
651
  UNUSED(USBx);
814
  uint32_t count32b, i;
652
 
815
 
653
  count32b =  (len + 3) / 4;
816
  count32b = ((uint32_t)len + 3U) / 4U;
654
  for (index = 0; index < count32b; index++, src += 4)
817
  for (i = 0U; i < count32b; i++)
655
  {
818
  {
656
    USBx_DFIFO(ch_ep_num) = *((__packed uint32_t *)src);
819
    USBx_DFIFO((uint32_t)ch_ep_num) = __UNALIGNED_UINT32_READ(pSrc);
-
 
820
    pSrc++;
657
  }
821
  }
-
 
822
 
658
  return HAL_OK;
823
  return HAL_OK;
659
}
824
}
660
 
825
 
661
/**
826
/**
662
  * @brief  USB_ReadPacket : read a packet from the Tx FIFO associated
827
  * @brief  USB_ReadPacket : read a packet from the RX FIFO
663
  *         with the EP/channel
-
 
664
  * @param  USBx : Selected device
828
  * @param  USBx  Selected device
665
  * @param  dest : destination pointer
829
  * @param  dest  source pointer
666
  * @param  len : Number of bytes to read
830
  * @param  len  Number of bytes to read
667
  * @retval pointer to destination buffer
831
  * @retval pointer to destination buffer
668
  */
832
  */
669
void *USB_ReadPacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *dest, uint16_t len)
833
void *USB_ReadPacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *dest, uint16_t len)
670
{
834
{
671
  uint32_t index = 0;
835
  uint32_t USBx_BASE = (uint32_t)USBx;
672
  uint32_t count32b = (len + 3) / 4;
836
  uint32_t *pDest = (uint32_t *)dest;
673
  /* Prevent unused argument(s) compilation warning */
-
 
674
  UNUSED(USBx);
837
  uint32_t i;
-
 
838
  uint32_t count32b = ((uint32_t)len + 3U) / 4U;
675
 
839
 
676
  for ( index = 0; index < count32b; index++, dest += 4 )
840
  for (i = 0U; i < count32b; i++)
677
  {
841
  {
678
    *(__packed uint32_t *)dest = USBx_DFIFO(0);
842
    __UNALIGNED_UINT32_WRITE(pDest, USBx_DFIFO(0U));
679
   
843
    pDest++;
680
  }
844
  }
-
 
845
 
681
  return ((void *)dest);
846
  return ((void *)pDest);
682
}
847
}
683
 
848
 
684
/**
849
/**
685
  * @brief  USB_EPSetStall : set a stall condition over an EP
850
  * @brief  USB_EPSetStall : set a stall condition over an EP
686
  * @param  USBx : Selected device
851
  * @param  USBx  Selected device
687
  * @param  ep: pointer to endpoint structure  
852
  * @param  ep pointer to endpoint structure
688
  * @retval HAL status
853
  * @retval HAL status
689
  */
854
  */
690
HAL_StatusTypeDef USB_EPSetStall(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDef *ep)
855
HAL_StatusTypeDef USB_EPSetStall(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
691
{
856
{
692
  /* Prevent unused argument(s) compilation warning */
857
  uint32_t USBx_BASE = (uint32_t)USBx;
693
  UNUSED(USBx);
858
  uint32_t epnum = (uint32_t)ep->num;
694
 
859
 
695
  if (ep->is_in == 1)
860
  if (ep->is_in == 1U)
696
  {
861
  {
697
    if (((USBx_INEP(ep->num)->DIEPCTL) & USB_OTG_DIEPCTL_EPENA) == 0)
862
    if (((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == 0U) && (epnum != 0U))
698
    {
863
    {
699
      USBx_INEP(ep->num)->DIEPCTL &= ~(USB_OTG_DIEPCTL_EPDIS);
864
      USBx_INEP(epnum)->DIEPCTL &= ~(USB_OTG_DIEPCTL_EPDIS);
700
    }
865
    }
701
    USBx_INEP(ep->num)->DIEPCTL |= USB_OTG_DIEPCTL_STALL;
866
    USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_STALL;
702
  }
867
  }
703
  else
868
  else
704
  {
869
  {
705
    if (((USBx_OUTEP(ep->num)->DOEPCTL) & USB_OTG_DOEPCTL_EPENA) == 0)
870
    if (((USBx_OUTEP(epnum)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == 0U) && (epnum != 0U))
706
    {
871
    {
707
      USBx_OUTEP(ep->num)->DOEPCTL &= ~(USB_OTG_DOEPCTL_EPDIS);
872
      USBx_OUTEP(epnum)->DOEPCTL &= ~(USB_OTG_DOEPCTL_EPDIS);
708
    }
873
    }
709
    USBx_OUTEP(ep->num)->DOEPCTL |= USB_OTG_DOEPCTL_STALL;
874
    USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_STALL;
710
  }
875
  }
-
 
876
 
711
  return HAL_OK;
877
  return HAL_OK;
712
}
878
}
713
 
879
 
714
/**
880
/**
715
  * @brief  USB_EPClearStall : Clear a stall condition over an EP
881
  * @brief  USB_EPClearStall : Clear a stall condition over an EP
716
  * @param  USBx : Selected device
882
  * @param  USBx  Selected device
717
  * @param  ep: pointer to endpoint structure
883
  * @param  ep pointer to endpoint structure
718
  * @retval HAL status
884
  * @retval HAL status
719
  */
885
  */
720
HAL_StatusTypeDef USB_EPClearStall(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
886
HAL_StatusTypeDef USB_EPClearStall(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
721
{
887
{
722
  /* Prevent unused argument(s) compilation warning */
888
  uint32_t USBx_BASE = (uint32_t)USBx;
723
  UNUSED(USBx);
889
  uint32_t epnum = (uint32_t)ep->num;
724
 
890
 
725
  if (ep->is_in == 1)
891
  if (ep->is_in == 1U)
726
  {
892
  {
727
    USBx_INEP(ep->num)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
893
    USBx_INEP(epnum)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
728
    if (ep->type == EP_TYPE_INTR || ep->type == EP_TYPE_BULK)
894
    if ((ep->type == EP_TYPE_INTR) || (ep->type == EP_TYPE_BULK))
729
    {
895
    {
730
       USBx_INEP(ep->num)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM; /* DATA0 */
896
      USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM; /* DATA0 */
731
    }
897
    }
732
  }
898
  }
733
  else
899
  else
734
  {
900
  {
735
    USBx_OUTEP(ep->num)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
901
    USBx_OUTEP(epnum)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
736
    if (ep->type == EP_TYPE_INTR || ep->type == EP_TYPE_BULK)
902
    if ((ep->type == EP_TYPE_INTR) || (ep->type == EP_TYPE_BULK))
737
    {
903
    {
738
      USBx_OUTEP(ep->num)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM; /* DATA0 */
904
      USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM; /* DATA0 */
739
    }
905
    }
740
  }
906
  }
741
  return HAL_OK;
907
  return HAL_OK;
742
}
908
}
743
 
909
 
744
/**
910
/**
745
  * @brief  USB_StopDevice : Stop the usb device mode
911
  * @brief  USB_StopDevice : Stop the usb device mode
746
  * @param  USBx : Selected device
912
  * @param  USBx  Selected device
747
  * @retval HAL status
913
  * @retval HAL status
748
  */
914
  */
749
HAL_StatusTypeDef USB_StopDevice(USB_OTG_GlobalTypeDef *USBx)
915
HAL_StatusTypeDef USB_StopDevice(USB_OTG_GlobalTypeDef *USBx)
750
{
916
{
-
 
917
  HAL_StatusTypeDef ret;
-
 
918
  uint32_t USBx_BASE = (uint32_t)USBx;
751
  uint32_t index = 0;
919
  uint32_t i;
752
 
920
 
753
  /* Clear Pending interrupt */
921
  /* Clear Pending interrupt */
754
  for (index = 0; index < 15 ; index++)
922
  for (i = 0U; i < 15U; i++)
755
  {
923
  {
756
    USBx_INEP(index)->DIEPINT  = 0xFF;
924
    USBx_INEP(i)->DIEPINT = 0xFB7FU;
757
    USBx_OUTEP(index)->DOEPINT  = 0xFF;
925
    USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
758
  }
926
  }
759
  USBx_DEVICE->DAINT = 0xFFFFFFFF;
-
 
760
 
927
 
761
  /* Clear interrupt masks */
928
  /* Clear interrupt masks */
762
  USBx_DEVICE->DIEPMSK  = 0;
929
  USBx_DEVICE->DIEPMSK  = 0U;
763
  USBx_DEVICE->DOEPMSK  = 0;
930
  USBx_DEVICE->DOEPMSK  = 0U;
764
  USBx_DEVICE->DAINTMSK = 0;
931
  USBx_DEVICE->DAINTMSK = 0U;
765
 
932
 
766
  /* Flush the FIFO */
933
  /* Flush the FIFO */
767
  USB_FlushRxFifo(USBx);
934
  ret = USB_FlushRxFifo(USBx);
-
 
935
  if (ret != HAL_OK)
-
 
936
  {
-
 
937
    return ret;
-
 
938
  }
-
 
939
 
768
  USB_FlushTxFifo(USBx ,  0x10 );
940
  ret = USB_FlushTxFifo(USBx,  0x10U);
-
 
941
  if (ret != HAL_OK)
-
 
942
  {
-
 
943
    return ret;
769
 
944
  }
-
 
945
 
770
  return HAL_OK;
946
  return ret;
771
}
947
}
772
 
948
 
773
/**
949
/**
774
  * @brief  USB_SetDevAddress : Stop the usb device mode
950
  * @brief  USB_SetDevAddress : Stop the usb device mode
775
  * @param  USBx : Selected device
951
  * @param  USBx  Selected device
776
  * @param  address : new device address to be assigned
952
  * @param  address  new device address to be assigned
777
  *          This parameter can be a value from 0 to 255
953
  *          This parameter can be a value from 0 to 255
778
  * @retval HAL status
954
  * @retval HAL status
779
  */
955
  */
780
HAL_StatusTypeDef  USB_SetDevAddress (USB_OTG_GlobalTypeDef *USBx, uint8_t address)
956
HAL_StatusTypeDef  USB_SetDevAddress(USB_OTG_GlobalTypeDef *USBx, uint8_t address)
781
{
957
{
782
  /* Prevent unused argument(s) compilation warning */
958
  uint32_t USBx_BASE = (uint32_t)USBx;
783
  UNUSED(USBx);
959
 
784
  UNUSED(address);
-
 
785
  USBx_DEVICE->DCFG &= ~ (USB_OTG_DCFG_DAD);
960
  USBx_DEVICE->DCFG &= ~(USB_OTG_DCFG_DAD);
786
  USBx_DEVICE->DCFG |= (address << 4) & USB_OTG_DCFG_DAD;
961
  USBx_DEVICE->DCFG |= ((uint32_t)address << 4) & USB_OTG_DCFG_DAD;
787
 
962
 
788
  return HAL_OK;
963
  return HAL_OK;
789
}
964
}
790
 
965
 
791
/**
966
/**
792
  * @brief  USB_DevConnect : Connect the USB device by enabling the pull-up/pull-down
967
  * @brief  USB_DevConnect : Connect the USB device by enabling Rpu
793
  * @param  USBx : Selected device
968
  * @param  USBx  Selected device
794
  * @retval HAL status
969
  * @retval HAL status
795
  */
970
  */
796
HAL_StatusTypeDef  USB_DevConnect (USB_OTG_GlobalTypeDef *USBx)
971
HAL_StatusTypeDef  USB_DevConnect(USB_OTG_GlobalTypeDef *USBx)
797
{
972
{
-
 
973
  uint32_t USBx_BASE = (uint32_t)USBx;
-
 
974
 
798
  /* Prevent unused argument(s) compilation warning */
975
  /* In case phy is stopped, ensure to ungate and restore the phy CLK */
-
 
976
  USBx_PCGCCTL &= ~(USB_OTG_PCGCCTL_STOPCLK | USB_OTG_PCGCCTL_GATECLK);
-
 
977
 
799
  UNUSED(USBx);
978
  USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_SDIS;
800
 
979
 
801
  USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_SDIS ;
-
 
802
  HAL_Delay(3);
-
 
803
 
-
 
804
  return HAL_OK;
980
  return HAL_OK;
805
}
981
}
806
 
982
 
807
/**
983
/**
808
  * @brief  USB_DevDisconnect : Disconnect the USB device by disabling the pull-up/pull-down
984
  * @brief  USB_DevDisconnect : Disconnect the USB device by disabling Rpu
809
  * @param  USBx : Selected device
985
  * @param  USBx  Selected device
810
  * @retval HAL status
986
  * @retval HAL status
811
  */
987
  */
812
HAL_StatusTypeDef  USB_DevDisconnect (USB_OTG_GlobalTypeDef *USBx)
988
HAL_StatusTypeDef  USB_DevDisconnect(USB_OTG_GlobalTypeDef *USBx)
813
{
989
{
-
 
990
  uint32_t USBx_BASE = (uint32_t)USBx;
-
 
991
 
814
  /* Prevent unused argument(s) compilation warning */
992
  /* In case phy is stopped, ensure to ungate and restore the phy CLK */
815
  UNUSED(USBx);
993
  USBx_PCGCCTL &= ~(USB_OTG_PCGCCTL_STOPCLK | USB_OTG_PCGCCTL_GATECLK);
816
 
994
 
817
  USBx_DEVICE->DCTL |= USB_OTG_DCTL_SDIS;
995
  USBx_DEVICE->DCTL |= USB_OTG_DCTL_SDIS;
818
  HAL_Delay(3);
-
 
819
 
996
 
820
  return HAL_OK;
997
  return HAL_OK;
821
}
998
}
822
 
999
 
823
/**
1000
/**
824
  * @brief  USB_ReadInterrupts: return the global USB interrupt status
1001
  * @brief  USB_ReadInterrupts: return the global USB interrupt status
825
  * @param  USBx : Selected device
1002
  * @param  USBx  Selected device
826
  * @retval HAL status
1003
  * @retval HAL status
827
  */
1004
  */
828
uint32_t  USB_ReadInterrupts (USB_OTG_GlobalTypeDef *USBx)
1005
uint32_t  USB_ReadInterrupts(USB_OTG_GlobalTypeDef *USBx)
829
{
1006
{
830
  uint32_t tmpreg = 0;
1007
  uint32_t tmpreg;
831
 
1008
 
832
  tmpreg = USBx->GINTSTS;
1009
  tmpreg = USBx->GINTSTS;
833
  tmpreg &= USBx->GINTMSK;
1010
  tmpreg &= USBx->GINTMSK;
-
 
1011
 
834
  return tmpreg;
1012
  return tmpreg;
835
}
1013
}
836
 
1014
 
837
/**
1015
/**
838
  * @brief  USB_ReadDevAllOutEpInterrupt: return the USB device OUT endpoints interrupt status
1016
  * @brief  USB_ReadDevAllOutEpInterrupt: return the USB device OUT endpoints interrupt status
839
  * @param  USBx : Selected device
1017
  * @param  USBx  Selected device
840
  * @retval HAL status
1018
  * @retval HAL status
841
  */
1019
  */
842
uint32_t USB_ReadDevAllOutEpInterrupt (USB_OTG_GlobalTypeDef *USBx)
1020
uint32_t USB_ReadDevAllOutEpInterrupt(USB_OTG_GlobalTypeDef *USBx)
843
{
1021
{
844
  uint32_t tmpreg = 0;
1022
  uint32_t USBx_BASE = (uint32_t)USBx;
845
  /* Prevent unused argument(s) compilation warning */
-
 
846
  UNUSED(USBx);
1023
  uint32_t tmpreg;
847
 
1024
 
848
  tmpreg  = USBx_DEVICE->DAINT;
1025
  tmpreg  = USBx_DEVICE->DAINT;
849
  tmpreg &= USBx_DEVICE->DAINTMSK;
1026
  tmpreg &= USBx_DEVICE->DAINTMSK;
-
 
1027
 
850
  return ((tmpreg & 0xffff0000) >> 16);
1028
  return ((tmpreg & 0xffff0000U) >> 16);
851
}
1029
}
852
 
1030
 
853
/**
1031
/**
854
  * @brief  USB_ReadDevAllInEpInterrupt: return the USB device IN endpoints interrupt status
1032
  * @brief  USB_ReadDevAllInEpInterrupt: return the USB device IN endpoints interrupt status
855
  * @param  USBx : Selected device
1033
  * @param  USBx  Selected device
856
  * @retval HAL status
1034
  * @retval HAL status
857
  */
1035
  */
858
uint32_t USB_ReadDevAllInEpInterrupt (USB_OTG_GlobalTypeDef *USBx)
1036
uint32_t USB_ReadDevAllInEpInterrupt(USB_OTG_GlobalTypeDef *USBx)
859
{
1037
{
860
  uint32_t tmpreg = 0;
1038
  uint32_t USBx_BASE = (uint32_t)USBx;
861
  /* Prevent unused argument(s) compilation warning */
-
 
862
  UNUSED(USBx);
1039
  uint32_t tmpreg;
863
 
1040
 
864
  tmpreg  = USBx_DEVICE->DAINT;
1041
  tmpreg  = USBx_DEVICE->DAINT;
865
  tmpreg &= USBx_DEVICE->DAINTMSK;
1042
  tmpreg &= USBx_DEVICE->DAINTMSK;
-
 
1043
 
866
  return ((tmpreg & 0xFFFF));
1044
  return ((tmpreg & 0xFFFFU));
867
}
1045
}
868
 
1046
 
869
/**
1047
/**
870
  * @brief  Returns Device OUT EP Interrupt register
1048
  * @brief  Returns Device OUT EP Interrupt register
871
  * @param  USBx : Selected device
1049
  * @param  USBx  Selected device
872
  * @param  epnum : endpoint number
1050
  * @param  epnum  endpoint number
873
  *          This parameter can be a value from 0 to 15
1051
  *          This parameter can be a value from 0 to 15
874
  * @retval Device OUT EP Interrupt register
1052
  * @retval Device OUT EP Interrupt register
875
  */
1053
  */
876
uint32_t USB_ReadDevOutEPInterrupt (USB_OTG_GlobalTypeDef *USBx , uint8_t epnum)
1054
uint32_t USB_ReadDevOutEPInterrupt(USB_OTG_GlobalTypeDef *USBx, uint8_t epnum)
877
{
1055
{
878
  /* Prevent unused argument(s) compilation warning */
1056
  uint32_t USBx_BASE = (uint32_t)USBx;
879
  UNUSED(USBx);
1057
  uint32_t tmpreg;
880
 
1058
 
881
  uint32_t tmpreg = 0;
-
 
882
  tmpreg  = USBx_OUTEP(epnum)->DOEPINT;
1059
  tmpreg  = USBx_OUTEP((uint32_t)epnum)->DOEPINT;
883
  tmpreg &= USBx_DEVICE->DOEPMSK;
1060
  tmpreg &= USBx_DEVICE->DOEPMSK;
-
 
1061
 
884
  return tmpreg;
1062
  return tmpreg;
885
}
1063
}
886
 
1064
 
887
/**
1065
/**
888
  * @brief  Returns Device IN EP Interrupt register
1066
  * @brief  Returns Device IN EP Interrupt register
889
  * @param  USBx : Selected device
1067
  * @param  USBx  Selected device
890
  * @param  epnum : endpoint number
1068
  * @param  epnum  endpoint number
891
  *          This parameter can be a value from 0 to 15
1069
  *          This parameter can be a value from 0 to 15
892
  * @retval Device IN EP Interrupt register
1070
  * @retval Device IN EP Interrupt register
893
  */
1071
  */
894
uint32_t USB_ReadDevInEPInterrupt (USB_OTG_GlobalTypeDef *USBx , uint8_t epnum)
1072
uint32_t USB_ReadDevInEPInterrupt(USB_OTG_GlobalTypeDef *USBx, uint8_t epnum)
895
{
1073
{
896
  uint32_t tmpreg = 0, msk = 0, emp = 0;
1074
  uint32_t USBx_BASE = (uint32_t)USBx;
897
  /* Prevent unused argument(s) compilation warning */
1075
  uint32_t tmpreg, msk, emp;
898
  UNUSED(USBx);
-
 
899
 
1076
 
900
  msk = USBx_DEVICE->DIEPMSK;
1077
  msk = USBx_DEVICE->DIEPMSK;
901
  emp = USBx_DEVICE->DIEPEMPMSK;
1078
  emp = USBx_DEVICE->DIEPEMPMSK;
902
  msk |= ((emp >> epnum) & 0x1) << 7;
1079
  msk |= ((emp >> (epnum & EP_ADDR_MSK)) & 0x1U) << 7;
903
  tmpreg = USBx_INEP(epnum)->DIEPINT & msk;
1080
  tmpreg = USBx_INEP((uint32_t)epnum)->DIEPINT & msk;
-
 
1081
 
904
  return tmpreg;
1082
  return tmpreg;
905
}
1083
}
906
 
1084
 
907
/**
1085
/**
908
  * @brief  USB_ClearInterrupts: clear a USB interrupt
1086
  * @brief  USB_ClearInterrupts: clear a USB interrupt
909
  * @param  USBx : Selected device
1087
  * @param  USBx  Selected device
910
  * @param  interrupt : interrupt flag
1088
  * @param  interrupt  flag
911
  * @retval None
1089
  * @retval None
912
  */
1090
  */
913
void  USB_ClearInterrupts (USB_OTG_GlobalTypeDef *USBx, uint32_t interrupt)
1091
void  USB_ClearInterrupts(USB_OTG_GlobalTypeDef *USBx, uint32_t interrupt)
914
{
1092
{
915
  USBx->GINTSTS |= interrupt;
1093
  USBx->GINTSTS |= interrupt;
916
}
1094
}
917
 
1095
 
918
/**
1096
/**
919
  * @brief  Returns USB core mode
1097
  * @brief  Returns USB core mode
920
  * @param  USBx : Selected device
1098
  * @param  USBx  Selected device
921
  * @retval return core mode : Host or Device
1099
  * @retval return core mode : Host or Device
922
  *          This parameter can be one of the these values:
1100
  *          This parameter can be one of these values:
923
  *           0 : Host
1101
  *           0 : Host
924
  *           1 : Device
1102
  *           1 : Device
925
  */
1103
  */
926
uint32_t USB_GetMode(USB_OTG_GlobalTypeDef *USBx)
1104
uint32_t USB_GetMode(USB_OTG_GlobalTypeDef *USBx)
927
{
1105
{
928
  /* Prevent unused argument(s) compilation warning */
-
 
929
  UNUSED(USBx);
-
 
930
 
-
 
931
  return ((USBx->GINTSTS ) & 0x1);
1106
  return ((USBx->GINTSTS) & 0x1U);
932
}
1107
}
933
 
1108
 
934
/**
1109
/**
935
  * @brief  Activate EP0 for Setup transactions
1110
  * @brief  Activate EP0 for Setup transactions
936
  * @param  USBx : Selected device
1111
  * @param  USBx  Selected device
937
  * @retval HAL status
1112
  * @retval HAL status
938
  */
1113
  */
939
HAL_StatusTypeDef  USB_ActivateSetup (USB_OTG_GlobalTypeDef *USBx)
1114
HAL_StatusTypeDef  USB_ActivateSetup(USB_OTG_GlobalTypeDef *USBx)
940
{
1115
{
941
  /* Prevent unused argument(s) compilation warning */
1116
  uint32_t USBx_BASE = (uint32_t)USBx;
942
  UNUSED(USBx);
1117
 
943
  /* Set the MPS of the IN EP based on the enumeration speed */
1118
  /* Set the MPS of the IN EP0 to 64 bytes */
944
  USBx_INEP(0)->DIEPCTL &= ~USB_OTG_DIEPCTL_MPSIZ;
1119
  USBx_INEP(0U)->DIEPCTL &= ~USB_OTG_DIEPCTL_MPSIZ;
945
 
1120
 
946
  if((USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD) == DSTS_ENUMSPD_LS_PHY_6MHZ)
-
 
947
  {
-
 
948
    USBx_INEP(0)->DIEPCTL |= 3;
-
 
949
  }
-
 
950
  USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGINAK;
1121
  USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGINAK;
951
 
1122
 
952
  return HAL_OK;
1123
  return HAL_OK;
953
}
1124
}
954
 
1125
 
955
/**
1126
/**
956
  * @brief  Prepare the EP0 to start the first control setup
1127
  * @brief  Prepare the EP0 to start the first control setup
957
  * @param  USBx : Selected device
1128
  * @param  USBx  Selected device
958
  * @param  psetup : pointer to setup packet
1129
  * @param  psetup  pointer to setup packet
959
  * @retval HAL status
1130
  * @retval HAL status
960
  */
1131
  */
961
HAL_StatusTypeDef USB_EP0_OutStart(USB_OTG_GlobalTypeDef *USBx, uint8_t *psetup)
1132
HAL_StatusTypeDef USB_EP0_OutStart(USB_OTG_GlobalTypeDef *USBx, uint8_t *psetup)
962
{
1133
{
963
  /* Prevent unused argument(s) compilation warning */
-
 
964
  UNUSED(USBx);
-
 
965
  UNUSED(psetup);
1134
  UNUSED(psetup);
-
 
1135
  uint32_t USBx_BASE = (uint32_t)USBx;
-
 
1136
  uint32_t gSNPSiD = *(__IO uint32_t *)(&USBx->CID + 0x1U);
-
 
1137
 
-
 
1138
  if (gSNPSiD > USB_OTG_CORE_ID_300A)
-
 
1139
  {
-
 
1140
    if ((USBx_OUTEP(0U)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
-
 
1141
    {
-
 
1142
      return HAL_OK;
-
 
1143
    }
-
 
1144
  }
-
 
1145
 
966
  USBx_OUTEP(0)->DOEPTSIZ = 0;
1146
  USBx_OUTEP(0U)->DOEPTSIZ = 0U;
967
  USBx_OUTEP(0)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1 << 19));
1147
  USBx_OUTEP(0U)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19));
968
  USBx_OUTEP(0)->DOEPTSIZ |= (3 * 8);
1148
  USBx_OUTEP(0U)->DOEPTSIZ |= (3U * 8U);
969
  USBx_OUTEP(0)->DOEPTSIZ |=  USB_OTG_DOEPTSIZ_STUPCNT;
1149
  USBx_OUTEP(0U)->DOEPTSIZ |=  USB_OTG_DOEPTSIZ_STUPCNT;
970
 
1150
 
971
  return HAL_OK;
1151
  return HAL_OK;
972
}
1152
}
973
 
1153
 
974
/**
1154
/**
975
  * @brief  USB_HostInit : Initializes the USB OTG controller registers
1155
  * @brief  Reset the USB Core (needed after USB clock settings change)
976
  *         for Host mode
-
 
977
  * @param  USBx : Selected device
1156
  * @param  USBx  Selected device
978
  * @param  cfg  : pointer to a USB_OTG_CfgTypeDef structure that contains
-
 
979
  *         the configuration information for the specified USBx peripheral.
-
 
980
  * @retval HAL status
1157
  * @retval HAL status
981
  */
1158
  */
982
HAL_StatusTypeDef USB_HostInit (USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
1159
static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx)
983
{
1160
{
984
  uint32_t index = 0;
1161
  uint32_t count = 0U;
985
 
1162
 
986
  /* Restart the Phy Clock */
1163
  /* Wait for AHB master IDLE state. */
987
  USBx_PCGCCTL = 0;
-
 
988
 
-
 
989
  /* no VBUS sensing*/
-
 
990
  USBx->GCCFG &=~ (USB_OTG_GCCFG_VBUSASEN);
-
 
991
  USBx->GCCFG &=~ (USB_OTG_GCCFG_VBUSBSEN);
-
 
992
 
1164
  do
993
  /* Disable the FS/LS support mode only */
-
 
994
  if((cfg.speed == USB_OTG_SPEED_FULL)&&
-
 
995
     (USBx != USB_OTG_FS))
-
 
996
  {
1165
  {
-
 
1166
    if (++count > 200000U)
-
 
1167
    {
997
    USBx_HOST->HCFG |= USB_OTG_HCFG_FSLSS;
1168
      return HAL_TIMEOUT;
998
  }
1169
    }
-
 
1170
  } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
-
 
1171
 
-
 
1172
  /* Core Soft Reset */
-
 
1173
  count = 0U;
-
 
1174
  USBx->GRSTCTL |= USB_OTG_GRSTCTL_CSRST;
-
 
1175
 
999
  else
1176
  do
1000
  {
1177
  {
-
 
1178
    if (++count > 200000U)
-
 
1179
    {
-
 
1180
      return HAL_TIMEOUT;
-
 
1181
    }
1001
    USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSS);  
1182
  } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_CSRST) == USB_OTG_GRSTCTL_CSRST);
-
 
1183
 
-
 
1184
  return HAL_OK;
1002
  }
1185
}
-
 
1186
 
-
 
1187
/**
-
 
1188
  * @brief  USB_HostInit : Initializes the USB OTG controller registers
-
 
1189
  *         for Host mode
-
 
1190
  * @param  USBx  Selected device
-
 
1191
  * @param  cfg   pointer to a USB_OTG_CfgTypeDef structure that contains
-
 
1192
  *         the configuration information for the specified USBx peripheral.
-
 
1193
  * @retval HAL status
1003
 
1194
  */
-
 
1195
HAL_StatusTypeDef USB_HostInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
-
 
1196
{
-
 
1197
  uint32_t USBx_BASE = (uint32_t)USBx;
-
 
1198
  uint32_t i;
-
 
1199
 
-
 
1200
  /* Restart the Phy Clock */
-
 
1201
  USBx_PCGCCTL = 0U;
-
 
1202
 
-
 
1203
  /* Disable VBUS sensing */
-
 
1204
  USBx->GCCFG &= ~(USB_OTG_GCCFG_VBUSASEN);
-
 
1205
  USBx->GCCFG &= ~(USB_OTG_GCCFG_VBUSBSEN);
-
 
1206
  /* Set default Max speed support */
-
 
1207
  USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSS);
-
 
1208
 
1004
  /* Make sure the FIFOs are flushed. */
1209
  /* Make sure the FIFOs are flushed. */
1005
  USB_FlushTxFifo(USBx, 0x10 ); /* all Tx FIFOs */
1210
  (void)USB_FlushTxFifo(USBx, 0x10U); /* all Tx FIFOs */
1006
  USB_FlushRxFifo(USBx);
1211
  (void)USB_FlushRxFifo(USBx);
1007
 
1212
 
1008
  /* Clear all pending HC Interrupts */
1213
  /* Clear all pending HC Interrupts */
1009
  for (index = 0; index < cfg.Host_channels; index++)
1214
  for (i = 0U; i < cfg.Host_channels; i++)
1010
  {
1215
  {
1011
    USBx_HC(index)->HCINT = 0xFFFFFFFF;
1216
    USBx_HC(i)->HCINT = 0xFFFFFFFFU;
1012
    USBx_HC(index)->HCINTMSK = 0;
1217
    USBx_HC(i)->HCINTMSK = 0U;
1013
  }
1218
  }
1014
 
1219
 
1015
  /* Enable VBUS driving */
1220
  /* Enable VBUS driving */
1016
  USB_DriveVbus(USBx, 1);
1221
  (void)USB_DriveVbus(USBx, 1U);
1017
 
1222
 
1018
  HAL_Delay(200);
1223
  HAL_Delay(200U);
1019
 
1224
 
1020
  /* Disable all interrupts. */
1225
  /* Disable all interrupts. */
1021
  USBx->GINTMSK = 0;
1226
  USBx->GINTMSK = 0U;
1022
 
1227
 
1023
  /* Clear any pending interrupts */
1228
  /* Clear any pending interrupts */
1024
  USBx->GINTSTS = 0xFFFFFFFF;
1229
  USBx->GINTSTS = 0xFFFFFFFFU;
1025
 
1230
 
1026
  if(USBx == USB_OTG_FS)
-
 
1027
  {
-
 
1028
    /* set Rx FIFO size */
1231
  /* set Rx FIFO size */
1029
    USBx->GRXFSIZ  = (uint32_t )0x80;
1232
  USBx->GRXFSIZ  = 0x80U;
1030
    USBx->DIEPTXF0_HNPTXFSIZ = (uint32_t )(((0x60 << 16)& USB_OTG_NPTXFD) | 0x80);
1233
  USBx->DIEPTXF0_HNPTXFSIZ = (uint32_t)(((0x60U << 16) & USB_OTG_NPTXFD) | 0x80U);
1031
    USBx->HPTXFSIZ = (uint32_t )(((0x40 << 16)& USB_OTG_HPTXFSIZ_PTXFD) | 0xE0);
1234
  USBx->HPTXFSIZ = (uint32_t)(((0x40U << 16)& USB_OTG_HPTXFSIZ_PTXFD) | 0xE0U);
1032
  }
-
 
1033
 
-
 
1034
  /* Enable the common interrupts */
1235
  /* Enable the common interrupts */
1035
  USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
1236
  USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
1036
 
1237
 
1037
  /* Enable interrupts matching to the Host mode ONLY */
1238
  /* Enable interrupts matching to the Host mode ONLY */
1038
  USBx->GINTMSK |= (USB_OTG_GINTMSK_PRTIM            | USB_OTG_GINTMSK_HCIM |\
1239
  USBx->GINTMSK |= (USB_OTG_GINTMSK_PRTIM            | USB_OTG_GINTMSK_HCIM | \
1039
                    USB_OTG_GINTMSK_SOFM             |USB_OTG_GINTSTS_DISCINT|\
1240
                    USB_OTG_GINTMSK_SOFM             | USB_OTG_GINTSTS_DISCINT | \
1040
                    USB_OTG_GINTMSK_PXFRM_IISOOXFRM  | USB_OTG_GINTMSK_WUIM);
1241
                    USB_OTG_GINTMSK_PXFRM_IISOOXFRM  | USB_OTG_GINTMSK_WUIM);
1041
 
1242
 
1042
  return HAL_OK;
1243
  return HAL_OK;
1043
}
1244
}
1044
 
1245
 
1045
/**
1246
/**
1046
  * @brief  USB_InitFSLSPClkSel : Initializes the FSLSPClkSel field of the
1247
  * @brief  USB_InitFSLSPClkSel : Initializes the FSLSPClkSel field of the
1047
  *         HCFG register on the PHY type and set the right frame interval
1248
  *         HCFG register on the PHY type and set the right frame interval
1048
  * @param  USBx : Selected device
1249
  * @param  USBx  Selected device
1049
  * @param  freq : clock frequency
1250
  * @param  freq  clock frequency
1050
  *          This parameter can be one of the these values:
1251
  *          This parameter can be one of these values:
1051
  *           HCFG_48_MHZ : Full Speed 48 MHz Clock
1252
  *           HCFG_48_MHZ : Full Speed 48 MHz Clock
1052
  *           HCFG_6_MHZ : Low Speed 6 MHz Clock
1253
  *           HCFG_6_MHZ : Low Speed 6 MHz Clock
1053
  * @retval HAL status
1254
  * @retval HAL status
1054
  */
1255
  */
1055
HAL_StatusTypeDef USB_InitFSLSPClkSel(USB_OTG_GlobalTypeDef *USBx , uint8_t freq)
1256
HAL_StatusTypeDef USB_InitFSLSPClkSel(USB_OTG_GlobalTypeDef *USBx, uint8_t freq)
1056
{
1257
{
1057
  /* Prevent unused argument(s) compilation warning */
1258
  uint32_t USBx_BASE = (uint32_t)USBx;
1058
  UNUSED(USBx);
-
 
1059
 
1259
 
1060
  USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSPCS);
1260
  USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSPCS);
1061
  USBx_HOST->HCFG |= (freq & USB_OTG_HCFG_FSLSPCS);
1261
  USBx_HOST->HCFG |= (uint32_t)freq & USB_OTG_HCFG_FSLSPCS;
-
 
1262
 
-
 
1263
  if (freq == HCFG_48_MHZ)
-
 
1264
  {
-
 
1265
    USBx_HOST->HFIR = 48000U;
1062
 
1266
  }
1063
  if (freq ==  HCFG_48_MHZ)
1267
  else if (freq == HCFG_6_MHZ)
1064
  {
1268
  {
1065
    USBx_HOST->HFIR = (uint32_t)48000;
1269
    USBx_HOST->HFIR = 6000U;
1066
  }
1270
  }
1067
  else if (freq ==  HCFG_6_MHZ)
1271
  else
1068
  {
1272
  {
1069
    USBx_HOST->HFIR = (uint32_t)6000;
1273
    /* ... */
1070
  }
1274
  }
-
 
1275
 
1071
  return HAL_OK;
1276
  return HAL_OK;
1072
}
1277
}
1073
 
1278
 
1074
/**
1279
/**
1075
* @brief  USB_OTG_ResetPort : Reset Host Port
1280
  * @brief  USB_OTG_ResetPort : Reset Host Port
1076
  * @param  USBx : Selected device
1281
  * @param  USBx  Selected device
1077
  * @retval HAL status
1282
  * @retval HAL status
1078
  * @note : (1)The application must wait at least 10 ms
1283
  * @note (1)The application must wait at least 10 ms
1079
  *   before clearing the reset bit.
1284
  *   before clearing the reset bit.
1080
  */
1285
  */
1081
HAL_StatusTypeDef USB_ResetPort(USB_OTG_GlobalTypeDef *USBx)
1286
HAL_StatusTypeDef USB_ResetPort(USB_OTG_GlobalTypeDef *USBx)
1082
{
1287
{
-
 
1288
  uint32_t USBx_BASE = (uint32_t)USBx;
-
 
1289
 
1083
  __IO uint32_t hprt0 = 0;
1290
  __IO uint32_t hprt0 = 0U;
1084
  /* Prevent unused argument(s) compilation warning */
-
 
1085
  UNUSED(USBx);
-
 
1086
 
1291
 
1087
  hprt0 = USBx_HPRT0;
1292
  hprt0 = USBx_HPRT0;
1088
 
1293
 
1089
  hprt0 &= ~(USB_OTG_HPRT_PENA    | USB_OTG_HPRT_PCDET |\
1294
  hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |
1090
    USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
1295
             USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
1091
 
1296
 
1092
  USBx_HPRT0 = (USB_OTG_HPRT_PRST | hprt0);  
1297
  USBx_HPRT0 = (USB_OTG_HPRT_PRST | hprt0);
1093
  HAL_Delay (10);                                /* See Note #1 */
1298
  HAL_Delay(100U);                                 /* See Note #1 */
1094
  USBx_HPRT0 = ((~USB_OTG_HPRT_PRST) & hprt0);
1299
  USBx_HPRT0 = ((~USB_OTG_HPRT_PRST) & hprt0);
-
 
1300
  HAL_Delay(10U);
-
 
1301
 
1095
  return HAL_OK;
1302
  return HAL_OK;
1096
}
1303
}
1097
 
1304
 
1098
/**
1305
/**
1099
  * @brief  USB_DriveVbus : activate or de-activate vbus
1306
  * @brief  USB_DriveVbus : activate or de-activate vbus
1100
  * @param  state : VBUS state
1307
  * @param  state  VBUS state
1101
  *          This parameter can be one of the these values:
1308
  *          This parameter can be one of these values:
1102
  *           0 : VBUS Active
1309
  *           0 : Deactivate VBUS
1103
  *           1 : VBUS Inactive
1310
  *           1 : Activate VBUS
1104
  * @retval HAL status
1311
  * @retval HAL status
1105
*/
1312
  */
1106
HAL_StatusTypeDef USB_DriveVbus (USB_OTG_GlobalTypeDef *USBx, uint8_t state)
1313
HAL_StatusTypeDef USB_DriveVbus(USB_OTG_GlobalTypeDef *USBx, uint8_t state)
1107
{
1314
{
-
 
1315
  uint32_t USBx_BASE = (uint32_t)USBx;
1108
  __IO uint32_t hprt0 = 0;
1316
  __IO uint32_t hprt0 = 0U;
1109
  /* Prevent unused argument(s) compilation warning */
-
 
1110
  UNUSED(USBx);
-
 
1111
 
1317
 
1112
  hprt0 = USBx_HPRT0;
1318
  hprt0 = USBx_HPRT0;
-
 
1319
 
1113
  hprt0 &= ~(USB_OTG_HPRT_PENA    | USB_OTG_HPRT_PCDET |\
1320
  hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |
1114
          USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
1321
             USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
1115
 
1322
 
1116
  if (((hprt0 & USB_OTG_HPRT_PPWR) == 0 ) && (state == 1 ))
1323
  if (((hprt0 & USB_OTG_HPRT_PPWR) == 0U) && (state == 1U))
1117
  {
1324
  {
1118
    USBx_HPRT0 = (USB_OTG_HPRT_PPWR | hprt0);
1325
    USBx_HPRT0 = (USB_OTG_HPRT_PPWR | hprt0);
1119
  }
1326
  }
1120
  if (((hprt0 & USB_OTG_HPRT_PPWR) == USB_OTG_HPRT_PPWR) && (state == 0 ))
1327
  if (((hprt0 & USB_OTG_HPRT_PPWR) == USB_OTG_HPRT_PPWR) && (state == 0U))
1121
  {
1328
  {
1122
    USBx_HPRT0 = ((~USB_OTG_HPRT_PPWR) & hprt0);
1329
    USBx_HPRT0 = ((~USB_OTG_HPRT_PPWR) & hprt0);
1123
  }
1330
  }
1124
  return HAL_OK;
1331
  return HAL_OK;
1125
}
1332
}
1126
 
1333
 
1127
/**
1334
/**
1128
  * @brief  Return Host Core speed
1335
  * @brief  Return Host Core speed
1129
  * @param  USBx : Selected device
1336
  * @param  USBx  Selected device
1130
  * @retval speed : Host speed
1337
  * @retval speed : Host speed
1131
  *          This parameter can be one of the these values:
1338
  *          This parameter can be one of these values:
1132
  *            @arg USB_OTG_SPEED_FULL: Full speed mode
1339
  *            @arg HCD_SPEED_FULL: Full speed mode
1133
  *            @arg USB_OTG_SPEED_LOW: Low speed mode
1340
  *            @arg HCD_SPEED_LOW: Low speed mode
1134
  */
1341
  */
1135
uint32_t USB_GetHostSpeed (USB_OTG_GlobalTypeDef *USBx)
1342
uint32_t USB_GetHostSpeed(USB_OTG_GlobalTypeDef *USBx)
1136
{
1343
{
-
 
1344
  uint32_t USBx_BASE = (uint32_t)USBx;
1137
  __IO uint32_t hprt0 = 0;
1345
  __IO uint32_t hprt0 = 0U;
1138
  /* Prevent unused argument(s) compilation warning */
-
 
1139
  UNUSED(USBx);
-
 
1140
 
1346
 
1141
  hprt0 = USBx_HPRT0;
1347
  hprt0 = USBx_HPRT0;
1142
  return ((hprt0 & USB_OTG_HPRT_PSPD) >> 17);
1348
  return ((hprt0 & USB_OTG_HPRT_PSPD) >> 17);
1143
}
1349
}
1144
 
1350
 
1145
/**
1351
/**
1146
  * @brief  Return Host Current Frame number
1352
  * @brief  Return Host Current Frame number
1147
  * @param  USBx : Selected device
1353
  * @param  USBx  Selected device
1148
  * @retval current frame number
1354
  * @retval current frame number
1149
*/
1355
  */
1150
uint32_t USB_GetCurrentFrame (USB_OTG_GlobalTypeDef *USBx)
1356
uint32_t USB_GetCurrentFrame(USB_OTG_GlobalTypeDef *USBx)
1151
{
1357
{
1152
  /* Prevent unused argument(s) compilation warning */
1358
  uint32_t USBx_BASE = (uint32_t)USBx;
1153
  UNUSED(USBx);
-
 
1154
 
1359
 
1155
  return (USBx_HOST->HFNUM & USB_OTG_HFNUM_FRNUM);
1360
  return (USBx_HOST->HFNUM & USB_OTG_HFNUM_FRNUM);
1156
}
1361
}
1157
 
1362
 
1158
/**
1363
/**
1159
  * @brief  Initialize a host channel
1364
  * @brief  Initialize a host channel
1160
  * @param  USBx : Selected device
1365
  * @param  USBx  Selected device
1161
  * @param  ch_num : Channel number
1366
  * @param  ch_num  Channel number
1162
  *         This parameter can be a value from 1 to 15
1367
  *         This parameter can be a value from 1 to 15
1163
  * @param  epnum : Endpoint number
1368
  * @param  epnum  Endpoint number
1164
  *          This parameter can be a value from 1 to 15
1369
  *          This parameter can be a value from 1 to 15
1165
  * @param  dev_address : Current device address
1370
  * @param  dev_address  Current device address
1166
  *          This parameter can be a value from 0 to 255
1371
  *          This parameter can be a value from 0 to 255
1167
  * @param  speed : Current device speed
1372
  * @param  speed  Current device speed
1168
  *          This parameter can be one of the these values:
1373
  *          This parameter can be one of these values:
1169
  *            @arg USB_OTG_SPEED_FULL: Full speed mode
1374
  *            @arg USB_OTG_SPEED_FULL: Full speed mode
1170
  *            @arg USB_OTG_SPEED_LOW: Low speed mode
1375
  *            @arg USB_OTG_SPEED_LOW: Low speed mode
1171
  * @param  ep_type : Endpoint Type
1376
  * @param  ep_type  Endpoint Type
1172
  *          This parameter can be one of the these values:
1377
  *          This parameter can be one of these values:
1173
  *            @arg EP_TYPE_CTRL: Control type
1378
  *            @arg EP_TYPE_CTRL: Control type
1174
  *            @arg EP_TYPE_ISOC: Isochronous type
1379
  *            @arg EP_TYPE_ISOC: Isochronous type
1175
  *            @arg EP_TYPE_BULK: Bulk type
1380
  *            @arg EP_TYPE_BULK: Bulk type
1176
  *            @arg EP_TYPE_INTR: Interrupt type
1381
  *            @arg EP_TYPE_INTR: Interrupt type
1177
  * @param  mps : Max Packet Size
1382
  * @param  mps  Max Packet Size
1178
  *          This parameter can be a value from 0 to32K
1383
  *          This parameter can be a value from 0 to32K
1179
  * @retval HAL state
1384
  * @retval HAL state
1180
  */
1385
  */
1181
HAL_StatusTypeDef USB_HC_Init(USB_OTG_GlobalTypeDef *USBx,  
1386
HAL_StatusTypeDef USB_HC_Init(USB_OTG_GlobalTypeDef *USBx, uint8_t ch_num,
1182
                              uint8_t ch_num,
-
 
1183
                              uint8_t epnum,
1387
                              uint8_t epnum, uint8_t dev_address, uint8_t speed,
1184
                              uint8_t dev_address,
1388
                              uint8_t ep_type, uint16_t mps)
-
 
1389
{
1185
                              uint8_t speed,
1390
  HAL_StatusTypeDef ret = HAL_OK;
1186
                              uint8_t ep_type,
1391
  uint32_t USBx_BASE = (uint32_t)USBx;
1187
                              uint16_t mps)
1392
  uint32_t HCcharEpDir, HCcharLowSpeed;
1188
{
1393
 
1189
  /* Clear old interrupt conditions for this host channel. */
1394
  /* Clear old interrupt conditions for this host channel. */
1190
  USBx_HC(ch_num)->HCINT = 0xFFFFFFFF;
1395
  USBx_HC((uint32_t)ch_num)->HCINT = 0xFFFFFFFFU;
1191
 
1396
 
1192
  /* Enable channel interrupts required for this transfer. */
1397
  /* Enable channel interrupts required for this transfer. */
1193
  switch (ep_type)
1398
  switch (ep_type)
1194
  {
1399
  {
1195
  case EP_TYPE_CTRL:
1400
    case EP_TYPE_CTRL:
1196
  case EP_TYPE_BULK:
1401
    case EP_TYPE_BULK:
1197
    USBx_HC(ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM  |\
1402
      USBx_HC((uint32_t)ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM  |
1198
                                USB_OTG_HCINTMSK_STALLM |\
1403
                                            USB_OTG_HCINTMSK_STALLM |
1199
                                USB_OTG_HCINTMSK_TXERRM |\
1404
                                            USB_OTG_HCINTMSK_TXERRM |
1200
                                USB_OTG_HCINTMSK_DTERRM |\
1405
                                            USB_OTG_HCINTMSK_DTERRM |
1201
                                USB_OTG_HCINTMSK_AHBERR |\
1406
                                            USB_OTG_HCINTMSK_AHBERR |
1202
                                USB_OTG_HCINTMSK_NAKM ;
1407
                                            USB_OTG_HCINTMSK_NAKM;
1203
 
1408
 
1204
    if (epnum & 0x80)
1409
      if ((epnum & 0x80U) == 0x80U)
1205
    {
1410
      {
1206
      USBx_HC(ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
1411
        USBx_HC((uint32_t)ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
1207
    }
1412
      }
1208
    break;
1413
      break;
1209
 
1414
 
1210
  case EP_TYPE_INTR:
1415
    case EP_TYPE_INTR:
1211
    USBx_HC(ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM  |\
1416
      USBx_HC((uint32_t)ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM  |
1212
                                USB_OTG_HCINTMSK_STALLM |\
1417
                                            USB_OTG_HCINTMSK_STALLM |
1213
                                USB_OTG_HCINTMSK_TXERRM |\
1418
                                            USB_OTG_HCINTMSK_TXERRM |
1214
                                USB_OTG_HCINTMSK_DTERRM |\
1419
                                            USB_OTG_HCINTMSK_DTERRM |
1215
                                USB_OTG_HCINTMSK_NAKM   |\
1420
                                            USB_OTG_HCINTMSK_NAKM   |
1216
                                USB_OTG_HCINTMSK_AHBERR |\
1421
                                            USB_OTG_HCINTMSK_AHBERR |
1217
                                USB_OTG_HCINTMSK_FRMORM ;
1422
                                            USB_OTG_HCINTMSK_FRMORM;
1218
   
1423
 
1219
    if (epnum & 0x80)
1424
      if ((epnum & 0x80U) == 0x80U)
1220
    {
1425
      {
1221
      USBx_HC(ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
1426
        USBx_HC((uint32_t)ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
1222
    }
1427
      }
1223
   
1428
 
1224
    break;
1429
      break;
1225
 
1430
 
1226
  case EP_TYPE_ISOC:
1431
    case EP_TYPE_ISOC:
1227
    USBx_HC(ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM  |\
1432
      USBx_HC((uint32_t)ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM  |
1228
                                USB_OTG_HCINTMSK_ACKM   |\
1433
                                            USB_OTG_HCINTMSK_ACKM   |
1229
                                USB_OTG_HCINTMSK_AHBERR |\
1434
                                            USB_OTG_HCINTMSK_AHBERR |
1230
                                USB_OTG_HCINTMSK_FRMORM ;
1435
                                            USB_OTG_HCINTMSK_FRMORM;
1231
   
1436
 
1232
    if (epnum & 0x80)
1437
      if ((epnum & 0x80U) == 0x80U)
1233
    {
1438
      {
1234
      USBx_HC(ch_num)->HCINTMSK |= (USB_OTG_HCINTMSK_TXERRM | USB_OTG_HCINTMSK_BBERRM);
1439
        USBx_HC((uint32_t)ch_num)->HCINTMSK |= (USB_OTG_HCINTMSK_TXERRM | USB_OTG_HCINTMSK_BBERRM);
1235
    }
1440
      }
-
 
1441
      break;
-
 
1442
 
-
 
1443
    default:
-
 
1444
      ret = HAL_ERROR;
1236
    break;
1445
      break;
1237
  }
1446
  }
1238
 
1447
 
1239
  /* Enable the top level host channel interrupt. */
1448
  /* Enable the top level host channel interrupt. */
1240
  USBx_HOST->HAINTMSK |= (1 << ch_num);
1449
  USBx_HOST->HAINTMSK |= 1UL << (ch_num & 0xFU);
1241
 
1450
 
1242
  /* Make sure host channel interrupts are enabled. */
1451
  /* Make sure host channel interrupts are enabled. */
1243
  USBx->GINTMSK |= USB_OTG_GINTMSK_HCIM;
1452
  USBx->GINTMSK |= USB_OTG_GINTMSK_HCIM;
1244
 
1453
 
1245
  /* Program the HCCHAR register */
1454
  /* Program the HCCHAR register */
-
 
1455
  if ((epnum & 0x80U) == 0x80U)
-
 
1456
  {
1246
  USBx_HC(ch_num)->HCCHAR = (((dev_address << 22) & USB_OTG_HCCHAR_DAD)  |\
1457
    HCcharEpDir = (0x1U << 15) & USB_OTG_HCCHAR_EPDIR;
-
 
1458
  }
-
 
1459
  else
-
 
1460
  {
-
 
1461
    HCcharEpDir = 0U;
-
 
1462
  }
-
 
1463
 
-
 
1464
  if (speed == HPRT0_PRTSPD_LOW_SPEED)
-
 
1465
  {
1247
                             (((epnum & 0x7F)<< 11) & USB_OTG_HCCHAR_EPNUM)|\
1466
    HCcharLowSpeed = (0x1U << 17) & USB_OTG_HCCHAR_LSDEV;
-
 
1467
  }
-
 
1468
  else
-
 
1469
  {
-
 
1470
    HCcharLowSpeed = 0U;
-
 
1471
  }
-
 
1472
 
1248
                             ((((epnum & 0x80) == 0x80)<< 15) & USB_OTG_HCCHAR_EPDIR)|\
1473
  USBx_HC((uint32_t)ch_num)->HCCHAR = (((uint32_t)dev_address << 22) & USB_OTG_HCCHAR_DAD) |
1249
                             (((speed == HPRT0_PRTSPD_LOW_SPEED)<< 17) & USB_OTG_HCCHAR_LSDEV)|\
1474
                                      ((((uint32_t)epnum & 0x7FU) << 11) & USB_OTG_HCCHAR_EPNUM) |
1250
                             ((ep_type << 18) & USB_OTG_HCCHAR_EPTYP)|\
1475
                                      (((uint32_t)ep_type << 18) & USB_OTG_HCCHAR_EPTYP) |
1251
                             (mps & USB_OTG_HCCHAR_MPSIZ));
1476
                                      ((uint32_t)mps & USB_OTG_HCCHAR_MPSIZ) | HCcharEpDir | HCcharLowSpeed;
1252
 
1477
 
1253
  if (ep_type == EP_TYPE_INTR)
1478
  if (ep_type == EP_TYPE_INTR)
1254
  {
1479
  {
1255
    USBx_HC(ch_num)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM ;
1480
    USBx_HC((uint32_t)ch_num)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM ;
1256
  }
1481
  }
1257
 
1482
 
1258
  return HAL_OK;
1483
  return ret;
1259
}
1484
}
1260
 
1485
 
1261
/**
1486
/**
1262
  * @brief  Start a transfer over a host channel
1487
  * @brief  Start a transfer over a host channel
1263
  * @param  USBx : Selected device
1488
  * @param  USBx  Selected device
1264
  * @param  hc : pointer to host channel structure
1489
  * @param  hc  pointer to host channel structure
1265
  * @retval HAL state
1490
  * @retval HAL state
1266
  */
1491
  */
1267
#if defined   (__CC_ARM) /*!< ARM Compiler */
-
 
1268
#pragma O0
-
 
1269
#elif defined (__GNUC__) /*!< GNU Compiler */
-
 
1270
#pragma GCC optimize ("O0")
-
 
1271
#endif /* __CC_ARM */
-
 
1272
HAL_StatusTypeDef USB_HC_StartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_HCTypeDef *hc)
1492
HAL_StatusTypeDef USB_HC_StartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_HCTypeDef *hc)
1273
{
1493
{
-
 
1494
  uint32_t USBx_BASE = (uint32_t)USBx;
-
 
1495
  uint32_t ch_num = (uint32_t)hc->ch_num;
-
 
1496
  static __IO uint32_t tmpreg = 0U;
1274
  uint8_t  is_oddframe = 0;
1497
  uint8_t  is_oddframe;
1275
  uint16_t len_words = 0;
1498
  uint16_t len_words;
1276
  uint16_t num_packets = 0;
1499
  uint16_t num_packets;
1277
  uint16_t max_hc_pkt_count = 256;
1500
  uint16_t max_hc_pkt_count = 256U;
1278
  uint32_t tmpreg = 0;
-
 
1279
 
1501
 
1280
  /* Compute the expected number of packets associated to the transfer */
1502
  /* Compute the expected number of packets associated to the transfer */
1281
  if (hc->xfer_len > 0)
1503
  if (hc->xfer_len > 0U)
1282
  {
1504
  {
1283
    num_packets = (hc->xfer_len + hc->max_packet - 1) / hc->max_packet;
1505
    num_packets = (uint16_t)((hc->xfer_len + hc->max_packet - 1U) / hc->max_packet);
1284
   
1506
 
1285
    if (num_packets > max_hc_pkt_count)
1507
    if (num_packets > max_hc_pkt_count)
1286
    {
1508
    {
1287
      num_packets = max_hc_pkt_count;
1509
      num_packets = max_hc_pkt_count;
1288
      hc->xfer_len = num_packets * hc->max_packet;
1510
      hc->xfer_len = (uint32_t)num_packets * hc->max_packet;
1289
    }
1511
    }
1290
  }
1512
  }
1291
  else
1513
  else
1292
  {
1514
  {
1293
    num_packets = 1;
1515
    num_packets = 1U;
1294
  }
1516
  }
1295
  if (hc->ep_is_in)
1517
  if (hc->ep_is_in != 0U)
1296
  {
1518
  {
1297
    hc->xfer_len = num_packets * hc->max_packet;
1519
    hc->xfer_len = (uint32_t)num_packets * hc->max_packet;
1298
  }
1520
  }
1299
 
1521
 
1300
  /* Initialize the HCTSIZn register */
1522
  /* Initialize the HCTSIZn register */
1301
  USBx_HC(hc->ch_num)->HCTSIZ = (((hc->xfer_len) & USB_OTG_HCTSIZ_XFRSIZ)) |\
1523
  USBx_HC(ch_num)->HCTSIZ = (hc->xfer_len & USB_OTG_HCTSIZ_XFRSIZ) |
1302
    ((num_packets << 19) & USB_OTG_HCTSIZ_PKTCNT) |\
1524
                            (((uint32_t)num_packets << 19) & USB_OTG_HCTSIZ_PKTCNT) |
1303
      (((hc->data_pid) << 29) & USB_OTG_HCTSIZ_DPID);
1525
                            (((uint32_t)hc->data_pid << 29) & USB_OTG_HCTSIZ_DPID);
1304
 
1526
 
1305
  is_oddframe = (USBx_HOST->HFNUM & 0x01) ? 0 : 1;
1527
  is_oddframe = (((uint32_t)USBx_HOST->HFNUM & 0x01U) != 0U) ? 0U : 1U;
1306
  USBx_HC(hc->ch_num)->HCCHAR &= ~USB_OTG_HCCHAR_ODDFRM;
1528
  USBx_HC(ch_num)->HCCHAR &= ~USB_OTG_HCCHAR_ODDFRM;
1307
  USBx_HC(hc->ch_num)->HCCHAR |= (is_oddframe << 29);
1529
  USBx_HC(ch_num)->HCCHAR |= (uint32_t)is_oddframe << 29;
1308
 
1530
 
1309
  /* Set host channel enable */
1531
  /* Set host channel enable */
1310
  tmpreg = USBx_HC(hc->ch_num)->HCCHAR;
1532
  tmpreg = USBx_HC(ch_num)->HCCHAR;
1311
  tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
1533
  tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
-
 
1534
 
-
 
1535
  /* make sure to set the correct ep direction */
-
 
1536
  if (hc->ep_is_in != 0U)
-
 
1537
  {
-
 
1538
    tmpreg |= USB_OTG_HCCHAR_EPDIR;
-
 
1539
  }
-
 
1540
  else
-
 
1541
  {
-
 
1542
    tmpreg &= ~USB_OTG_HCCHAR_EPDIR;
-
 
1543
  }
1312
  tmpreg |= USB_OTG_HCCHAR_CHENA;
1544
  tmpreg |= USB_OTG_HCCHAR_CHENA;
1313
  USBx_HC(hc->ch_num)->HCCHAR = tmpreg;
1545
  USBx_HC(ch_num)->HCCHAR = tmpreg;
1314
 
1546
 
1315
  if((hc->ep_is_in == 0) && (hc->xfer_len > 0))
1547
  if ((hc->ep_is_in == 0U) && (hc->xfer_len > 0U))
1316
  {
1548
  {
1317
    switch(hc->ep_type)
1549
    switch (hc->ep_type)
1318
    {
1550
    {
1319
      /* Non periodic transfer */
1551
      /* Non periodic transfer */
1320
    case EP_TYPE_CTRL:
1552
      case EP_TYPE_CTRL:
1321
    case EP_TYPE_BULK:
1553
      case EP_TYPE_BULK:
-
 
1554
 
1322
      len_words = (hc->xfer_len + 3) / 4;
1555
        len_words = (uint16_t)((hc->xfer_len + 3U) / 4U);
1323
     
1556
 
1324
      /* check if there is enough space in FIFO space */
1557
        /* check if there is enough space in FIFO space */
1325
      if(len_words > (USBx->HNPTXSTS & 0xFFFF))
1558
        if (len_words > (USBx->HNPTXSTS & 0xFFFFU))
1326
      {
1559
        {
1327
        /* need to process data in nptxfempty interrupt */
1560
          /* need to process data in nptxfempty interrupt */
1328
        USBx->GINTMSK |= USB_OTG_GINTMSK_NPTXFEM;
1561
          USBx->GINTMSK |= USB_OTG_GINTMSK_NPTXFEM;
1329
      }
1562
        }
1330
      break;
1563
        break;
1331
     
1564
 
1332
      /* Periodic transfer */
1565
      /* Periodic transfer */
1333
    case EP_TYPE_INTR:
1566
      case EP_TYPE_INTR:
1334
    case EP_TYPE_ISOC:
1567
      case EP_TYPE_ISOC:
1335
      len_words = (hc->xfer_len + 3) / 4;
1568
        len_words = (uint16_t)((hc->xfer_len + 3U) / 4U);
1336
      /* check if there is enough space in FIFO space */
1569
        /* check if there is enough space in FIFO space */
1337
      if(len_words > (USBx_HOST->HPTXSTS & 0xFFFF)) /* split the transfer */
1570
        if (len_words > (USBx_HOST->HPTXSTS & 0xFFFFU)) /* split the transfer */
1338
      {
1571
        {
1339
        /* need to process data in ptxfempty interrupt */
1572
          /* need to process data in ptxfempty interrupt */
1340
        USBx->GINTMSK |= USB_OTG_GINTMSK_PTXFEM;          
1573
          USBx->GINTMSK |= USB_OTG_GINTMSK_PTXFEM;
1341
      }
1574
        }
1342
      break;
1575
        break;
1343
     
1576
 
1344
    default:
1577
      default:
1345
      break;
1578
        break;
1346
    }
1579
    }
1347
   
1580
 
1348
    /* Write packet into the Tx FIFO. */
1581
    /* Write packet into the Tx FIFO. */
1349
    USB_WritePacket(USBx, hc->xfer_buff, hc->ch_num, hc->xfer_len);
1582
    (void)USB_WritePacket(USBx, hc->xfer_buff, hc->ch_num, (uint16_t)hc->xfer_len);
1350
  }
1583
  }
1351
 
1584
 
1352
  return HAL_OK;
1585
  return HAL_OK;
1353
}
1586
}
1354
 
1587
 
1355
/**
1588
/**
1356
  * @brief Read all host channel interrupts status
1589
  * @brief Read all host channel interrupts status
1357
  * @param  USBx : Selected device
1590
  * @param  USBx  Selected device
1358
  * @retval HAL state
1591
  * @retval HAL state
1359
  */
1592
  */
1360
uint32_t USB_HC_ReadInterrupt (USB_OTG_GlobalTypeDef *USBx)
1593
uint32_t USB_HC_ReadInterrupt(USB_OTG_GlobalTypeDef *USBx)
1361
{
1594
{
1362
  /* Prevent unused argument(s) compilation warning */
1595
  uint32_t USBx_BASE = (uint32_t)USBx;
1363
  UNUSED(USBx);
-
 
1364
 
1596
 
1365
  return ((USBx_HOST->HAINT) & 0xFFFF);
1597
  return ((USBx_HOST->HAINT) & 0xFFFFU);
1366
}
1598
}
1367
 
1599
 
1368
/**
1600
/**
1369
  * @brief  Halt a host channel
1601
  * @brief  Halt a host channel
1370
  * @param  USBx : Selected device
1602
  * @param  USBx  Selected device
1371
  * @param  hc_num : Host Channel number
1603
  * @param  hc_num  Host Channel number
1372
  *         This parameter can be a value from 1 to 15
1604
  *         This parameter can be a value from 1 to 15
1373
  * @retval HAL state
1605
  * @retval HAL state
1374
  */
1606
  */
1375
HAL_StatusTypeDef USB_HC_Halt(USB_OTG_GlobalTypeDef *USBx , uint8_t hc_num)
1607
HAL_StatusTypeDef USB_HC_Halt(USB_OTG_GlobalTypeDef *USBx, uint8_t hc_num)
1376
{
1608
{
1377
  uint32_t count = 0;
1609
  uint32_t USBx_BASE = (uint32_t)USBx;
1378
  /* Prevent unused argument(s) compilation warning */
1610
  uint32_t hcnum = (uint32_t)hc_num;
1379
  UNUSED(USBx);
1611
  uint32_t count = 0U;
-
 
1612
  uint32_t HcEpType = (USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_EPTYP) >> 18;
1380
 
1613
 
1381
  /* Check for space in the request queue to issue the halt. */
1614
  /* Check for space in the request queue to issue the halt. */
1382
  if (((((USBx_HC(hc_num)->HCCHAR) & USB_OTG_HCCHAR_EPTYP) >> 18) == HCCHAR_CTRL) ||
-
 
1383
     (((((USBx_HC(hc_num)->HCCHAR) & USB_OTG_HCCHAR_EPTYP) >> 18) == HCCHAR_BULK)))
1615
  if ((HcEpType == HCCHAR_CTRL) || (HcEpType == HCCHAR_BULK))
1384
  {
1616
  {
1385
    USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHDIS;
1617
    USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHDIS;
1386
   
1618
 
1387
    if ((USBx->HNPTXSTS & 0xFFFF) == 0)
1619
    if ((USBx->HNPTXSTS & (0xFFU << 16)) == 0U)
1388
    {
1620
    {
1389
      USBx_HC(hc_num)->HCCHAR &= ~USB_OTG_HCCHAR_CHENA;
1621
      USBx_HC(hcnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHENA;
1390
      USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
1622
      USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
1391
      USBx_HC(hc_num)->HCCHAR &= ~USB_OTG_HCCHAR_EPDIR;
1623
      USBx_HC(hcnum)->HCCHAR &= ~USB_OTG_HCCHAR_EPDIR;
1392
      do
1624
      do
1393
      {
1625
      {
1394
        if (++count > 1000)
1626
        if (++count > 1000U)
1395
        {
1627
        {
1396
          break;
1628
          break;
1397
        }
1629
        }
1398
      }
-
 
1399
      while ((USBx_HC(hc_num)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
1630
      } while ((USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
1400
    }
1631
    }
1401
    else
1632
    else
1402
    {
1633
    {
1403
      USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
1634
      USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
1404
    }
1635
    }
1405
  }
1636
  }
1406
  else
1637
  else
1407
  {
1638
  {
1408
    USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHDIS;
1639
    USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHDIS;
1409
   
1640
 
1410
    if ((USBx_HOST->HPTXSTS & 0xFFFF) == 0)
1641
    if ((USBx_HOST->HPTXSTS & (0xFFU << 16)) == 0U)
1411
    {
1642
    {
1412
      USBx_HC(hc_num)->HCCHAR &= ~USB_OTG_HCCHAR_CHENA;
1643
      USBx_HC(hcnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHENA;
1413
      USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
1644
      USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
1414
      USBx_HC(hc_num)->HCCHAR &= ~USB_OTG_HCCHAR_EPDIR;
1645
      USBx_HC(hcnum)->HCCHAR &= ~USB_OTG_HCCHAR_EPDIR;
1415
      do
1646
      do
1416
      {
1647
      {
1417
        if (++count > 1000)
1648
        if (++count > 1000U)
1418
        {
1649
        {
1419
          break;
1650
          break;
1420
        }
1651
        }
1421
      }
-
 
1422
      while ((USBx_HC(hc_num)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
1652
      } while ((USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
1423
    }
1653
    }
1424
    else
1654
    else
1425
    {
1655
    {
1426
       USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
1656
      USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
1427
    }
1657
    }
1428
  }
1658
  }
1429
 
1659
 
1430
  return HAL_OK;
1660
  return HAL_OK;
1431
}
1661
}
1432
 
1662
 
1433
/**
1663
/**
1434
  * @brief  Initiate Do Ping protocol
1664
  * @brief  Initiate Do Ping protocol
1435
  * @param  USBx : Selected device
1665
  * @param  USBx  Selected device
1436
  * @param  hc_num : Host Channel number
1666
  * @param  hc_num  Host Channel number
1437
  *         This parameter can be a value from 1 to 15
1667
  *         This parameter can be a value from 1 to 15
1438
  * @retval HAL state
1668
  * @retval HAL state
1439
  */
1669
  */
1440
HAL_StatusTypeDef USB_DoPing(USB_OTG_GlobalTypeDef *USBx , uint8_t ch_num)
1670
HAL_StatusTypeDef USB_DoPing(USB_OTG_GlobalTypeDef *USBx, uint8_t ch_num)
1441
{
1671
{
-
 
1672
  uint32_t USBx_BASE = (uint32_t)USBx;
-
 
1673
  uint32_t chnum = (uint32_t)ch_num;
1442
  uint8_t  num_packets = 1;
1674
  uint32_t num_packets = 1U;
1443
  uint32_t tmpreg = 0;
1675
  uint32_t tmpreg;
-
 
1676
 
1444
  /* Prevent unused argument(s) compilation warning */
1677
  USBx_HC(chnum)->HCTSIZ = ((num_packets << 19) & USB_OTG_HCTSIZ_PKTCNT) |
1445
  UNUSED(USBx);
1678
                           USB_OTG_HCTSIZ_DOPING;
1446
 
1679
 
1447
  USBx_HC(ch_num)->HCTSIZ = ((num_packets << 19) & USB_OTG_HCTSIZ_PKTCNT) |\
-
 
1448
                                USB_OTG_HCTSIZ_DOPING;
-
 
1449
 
-
 
1450
  /* Set host channel enable */
1680
  /* Set host channel enable */
1451
  tmpreg = USBx_HC(ch_num)->HCCHAR;
1681
  tmpreg = USBx_HC(chnum)->HCCHAR;
1452
  tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
1682
  tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
1453
  tmpreg |= USB_OTG_HCCHAR_CHENA;
1683
  tmpreg |= USB_OTG_HCCHAR_CHENA;
1454
  USBx_HC(ch_num)->HCCHAR = tmpreg;
1684
  USBx_HC(chnum)->HCCHAR = tmpreg;
1455
 
1685
 
1456
  return HAL_OK;  
1686
  return HAL_OK;
1457
}
1687
}
1458
 
1688
 
1459
/**
1689
/**
1460
  * @brief  Stop Host Core
1690
  * @brief  Stop Host Core
1461
  * @param  USBx : Selected device
1691
  * @param  USBx  Selected device
1462
  * @retval HAL state
1692
  * @retval HAL state
1463
  */
1693
  */
1464
HAL_StatusTypeDef USB_StopHost(USB_OTG_GlobalTypeDef *USBx)
1694
HAL_StatusTypeDef USB_StopHost(USB_OTG_GlobalTypeDef *USBx)
1465
{
1695
{
1466
  uint8_t index;
1696
  uint32_t USBx_BASE = (uint32_t)USBx;
1467
  uint32_t count = 0;
1697
  uint32_t count = 0U;
1468
  uint32_t value = 0;
1698
  uint32_t value;
-
 
1699
  uint32_t i;
1469
 
1700
 
1470
  USB_DisableGlobalInt(USBx);
1701
  (void)USB_DisableGlobalInt(USBx);
1471
 
1702
 
1472
    /* Flush FIFO */
1703
  /* Flush FIFO */
1473
  USB_FlushTxFifo(USBx, 0x10);
1704
  (void)USB_FlushTxFifo(USBx, 0x10U);
1474
  USB_FlushRxFifo(USBx);
1705
  (void)USB_FlushRxFifo(USBx);
1475
 
1706
 
1476
  /* Flush out any leftover queued requests. */
1707
  /* Flush out any leftover queued requests. */
1477
  for (index = 0; index <= 15; index++)
1708
  for (i = 0U; i <= 15U; i++)
1478
  {
1709
  {
1479
    value = USBx_HC(index)->HCCHAR;
1710
    value = USBx_HC(i)->HCCHAR;
1480
    value |=  USB_OTG_HCCHAR_CHDIS;
1711
    value |=  USB_OTG_HCCHAR_CHDIS;
1481
    value &= ~USB_OTG_HCCHAR_CHENA;
1712
    value &= ~USB_OTG_HCCHAR_CHENA;
1482
    value &= ~USB_OTG_HCCHAR_EPDIR;
1713
    value &= ~USB_OTG_HCCHAR_EPDIR;
1483
    USBx_HC(index)->HCCHAR = value;
1714
    USBx_HC(i)->HCCHAR = value;
1484
  }
1715
  }
1485
 
1716
 
1486
  /* Halt all channels to put them into a known state. */
1717
  /* Halt all channels to put them into a known state. */
1487
  for (index = 0; index <= 15; index++)
1718
  for (i = 0U; i <= 15U; i++)
1488
  {
1719
  {
1489
    value = USBx_HC(index)->HCCHAR ;
1720
    value = USBx_HC(i)->HCCHAR;
1490
    value |= USB_OTG_HCCHAR_CHDIS;
1721
    value |= USB_OTG_HCCHAR_CHDIS;
1491
    value |= USB_OTG_HCCHAR_CHENA;
1722
    value |= USB_OTG_HCCHAR_CHENA;
1492
    value &= ~USB_OTG_HCCHAR_EPDIR;
1723
    value &= ~USB_OTG_HCCHAR_EPDIR;
1493
    USBx_HC(index)->HCCHAR = value;
1724
    USBx_HC(i)->HCCHAR = value;
1494
   
1725
 
1495
    do
1726
    do
1496
    {
1727
    {
1497
      if (++count > 1000)
1728
      if (++count > 1000U)
1498
      {
1729
      {
1499
        break;
1730
        break;
1500
      }
1731
      }
1501
    }
-
 
1502
    while ((USBx_HC(index)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
1732
    } while ((USBx_HC(i)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
1503
  }
1733
  }
1504
 
1734
 
1505
  /* Clear any pending Host interrupts */
1735
  /* Clear any pending Host interrupts */
1506
  USBx_HOST->HAINT = 0xFFFFFFFF;
1736
  USBx_HOST->HAINT = 0xFFFFFFFFU;
1507
  USBx->GINTSTS = 0xFFFFFFFF;
1737
  USBx->GINTSTS = 0xFFFFFFFFU;
-
 
1738
 
1508
  USB_EnableGlobalInt(USBx);
1739
  (void)USB_EnableGlobalInt(USBx);
1509
 
1740
 
1510
  return HAL_OK;
1741
  return HAL_OK;
1511
}
1742
}
1512
 
1743
 
1513
/**
1744
/**
1514
  * @brief  USB_ActivateRemoteWakeup : active remote wakeup signalling
1745
  * @brief  USB_ActivateRemoteWakeup active remote wakeup signalling
1515
  * @param  USBx : Selected device
1746
  * @param  USBx Selected device
1516
  * @retval HAL status
1747
  * @retval HAL status
1517
  */
1748
  */
1518
HAL_StatusTypeDef USB_ActivateRemoteWakeup(USB_OTG_GlobalTypeDef *USBx)
1749
HAL_StatusTypeDef USB_ActivateRemoteWakeup(USB_OTG_GlobalTypeDef *USBx)
1519
{
1750
{
1520
  /* Prevent unused argument(s) compilation warning */
1751
  uint32_t USBx_BASE = (uint32_t)USBx;
1521
  UNUSED(USBx);
1752
 
1522
  if((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
1753
  if ((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
1523
  {
1754
  {
1524
    /* active Remote wakeup signalling */
1755
    /* active Remote wakeup signalling */
1525
    USBx_DEVICE->DCTL |= USB_OTG_DCTL_RWUSIG;
1756
    USBx_DEVICE->DCTL |= USB_OTG_DCTL_RWUSIG;
1526
  }
1757
  }
-
 
1758
 
1527
  return HAL_OK;
1759
  return HAL_OK;
1528
}
1760
}
1529
 
1761
 
1530
/**
1762
/**
1531
  * @brief  USB_DeActivateRemoteWakeup : de-active remote wakeup signalling
1763
  * @brief  USB_DeActivateRemoteWakeup de-active remote wakeup signalling
1532
  * @param  USBx : Selected device
1764
  * @param  USBx Selected device
1533
  * @retval HAL status
1765
  * @retval HAL status
1534
  */
1766
  */
1535
HAL_StatusTypeDef USB_DeActivateRemoteWakeup(USB_OTG_GlobalTypeDef *USBx)
1767
HAL_StatusTypeDef USB_DeActivateRemoteWakeup(USB_OTG_GlobalTypeDef *USBx)
1536
{
1768
{
1537
  /* Prevent unused argument(s) compilation warning */
1769
  uint32_t USBx_BASE = (uint32_t)USBx;
1538
  UNUSED(USBx);
1770
 
1539
  /* active Remote wakeup signalling */
1771
  /* active Remote wakeup signalling */
1540
   USBx_DEVICE->DCTL &= ~(USB_OTG_DCTL_RWUSIG);
1772
  USBx_DEVICE->DCTL &= ~(USB_OTG_DCTL_RWUSIG);
-
 
1773
 
1541
  return HAL_OK;
1774
  return HAL_OK;
1542
}
1775
}
-
 
1776
#endif /* defined (USB_OTG_FS) */
1543
 
1777
 
1544
#endif /* USB_OTG_FS */
-
 
1545
 
-
 
1546
/*==============================================================================
-
 
1547
    USB Device FS peripheral available on STM32F102xx and STM32F103xx devices
-
 
1548
==============================================================================*/
-
 
1549
#if defined (USB)
1778
#if defined (USB)
1550
/**
1779
/**
1551
  * @brief  Initializes the USB Core
1780
  * @brief  Initializes the USB Core
1552
  * @param  USBx: USB Instance
1781
  * @param  USBx USB Instance
1553
  * @param  cfg : pointer to a USB_CfgTypeDef structure that contains
1782
  * @param  cfg pointer to a USB_CfgTypeDef structure that contains
1554
  *         the configuration information for the specified USBx peripheral.
1783
  *         the configuration information for the specified USBx peripheral.
1555
  * @retval HAL status
1784
  * @retval HAL status
1556
  */
1785
  */
1557
HAL_StatusTypeDef USB_CoreInit(USB_TypeDef *USBx, USB_CfgTypeDef cfg)
1786
HAL_StatusTypeDef USB_CoreInit(USB_TypeDef *USBx, USB_CfgTypeDef cfg)
1558
{
1787
{
1559
  /* Prevent unused argument(s) compilation warning */
1788
  /* Prevent unused argument(s) compilation warning */
1560
  UNUSED(USBx);
1789
  UNUSED(USBx);
1561
  UNUSED(cfg);
1790
  UNUSED(cfg);
-
 
1791
 
1562
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
1792
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
1563
              only by USB OTG FS peripheral.
1793
              only by USB OTG FS peripheral.
1564
            - This function is added to ensure compatibility across platforms.
1794
            - This function is added to ensure compatibility across platforms.
1565
   */
1795
   */
-
 
1796
 
1566
  return HAL_OK;
1797
  return HAL_OK;
1567
}
1798
}
1568
 
1799
 
1569
/**
1800
/**
1570
  * @brief  USB_EnableGlobalInt
1801
  * @brief  USB_EnableGlobalInt
1571
  *         Enables the controller's Global Int in the AHB Config reg
1802
  *         Enables the controller's Global Int in the AHB Config reg
1572
  * @param  USBx : Selected device
1803
  * @param  USBx Selected device
1573
  * @retval HAL status
1804
  * @retval HAL status
1574
  */
1805
  */
1575
HAL_StatusTypeDef USB_EnableGlobalInt(USB_TypeDef *USBx)
1806
HAL_StatusTypeDef USB_EnableGlobalInt(USB_TypeDef *USBx)
1576
{
1807
{
1577
  uint32_t winterruptmask = 0;
1808
  uint32_t winterruptmask;
-
 
1809
 
-
 
1810
  /* Clear pending interrupts */
-
 
1811
  USBx->ISTR = 0U;
1578
 
1812
 
1579
  /* Set winterruptmask variable */
1813
  /* Set winterruptmask variable */
1580
  winterruptmask = USB_CNTR_CTRM  | USB_CNTR_WKUPM | USB_CNTR_SUSPM | USB_CNTR_ERRM \
1814
  winterruptmask = USB_CNTR_CTRM  | USB_CNTR_WKUPM |
-
 
1815
                   USB_CNTR_SUSPM | USB_CNTR_ERRM |
1581
     | USB_CNTR_SOFM | USB_CNTR_ESOFM | USB_CNTR_RESETM;
1816
                   USB_CNTR_SOFM | USB_CNTR_ESOFM |
-
 
1817
                   USB_CNTR_RESETM;
1582
 
1818
 
1583
  /* Set interrupt mask */
1819
  /* Set interrupt mask */
1584
  USBx->CNTR |= winterruptmask;
1820
  USBx->CNTR = (uint16_t)winterruptmask;
1585
 
1821
 
1586
  return HAL_OK;
1822
  return HAL_OK;
1587
}
1823
}
1588
 
1824
 
1589
/**
1825
/**
1590
  * @brief  USB_DisableGlobalInt
1826
  * @brief  USB_DisableGlobalInt
1591
  *         Disable the controller's Global Int in the AHB Config reg
1827
  *         Disable the controller's Global Int in the AHB Config reg
1592
  * @param  USBx : Selected device
1828
  * @param  USBx Selected device
1593
  * @retval HAL status
1829
  * @retval HAL status
1594
*/
1830
  */
1595
HAL_StatusTypeDef USB_DisableGlobalInt(USB_TypeDef *USBx)
1831
HAL_StatusTypeDef USB_DisableGlobalInt(USB_TypeDef *USBx)
1596
{
1832
{
1597
  uint32_t winterruptmask = 0;
1833
  uint32_t winterruptmask;
1598
 
1834
 
1599
  /* Set winterruptmask variable */
1835
  /* Set winterruptmask variable */
1600
  winterruptmask = USB_CNTR_CTRM  | USB_CNTR_WKUPM | USB_CNTR_SUSPM | USB_CNTR_ERRM \
1836
  winterruptmask = USB_CNTR_CTRM  | USB_CNTR_WKUPM |
-
 
1837
                   USB_CNTR_SUSPM | USB_CNTR_ERRM |
-
 
1838
                   USB_CNTR_SOFM | USB_CNTR_ESOFM |
1601
    | USB_CNTR_ESOFM | USB_CNTR_RESETM;
1839
                   USB_CNTR_RESETM;
1602
 
1840
 
1603
  /* Clear interrupt mask */
1841
  /* Clear interrupt mask */
1604
  USBx->CNTR &= ~winterruptmask;
1842
  USBx->CNTR &= (uint16_t)(~winterruptmask);
1605
 
1843
 
1606
  return HAL_OK;
1844
  return HAL_OK;
1607
}
1845
}
1608
 
1846
 
1609
/**
1847
/**
1610
  * @brief  USB_SetCurrentMode : Set functional mode
1848
  * @brief  USB_SetCurrentMode Set functional mode
1611
  * @param  USBx : Selected device
1849
  * @param  USBx Selected device
1612
  * @param  mode :  current core mode
1850
  * @param  mode current core mode
1613
  *          This parameter can be one of the these values:
1851
  *          This parameter can be one of the these values:
1614
  *            @arg USB_DEVICE_MODE: Peripheral mode mode
1852
  *            @arg USB_DEVICE_MODE Peripheral mode
1615
  * @retval HAL status
1853
  * @retval HAL status
1616
  */
1854
  */
1617
HAL_StatusTypeDef USB_SetCurrentMode(USB_TypeDef *USBx , USB_ModeTypeDef mode)
1855
HAL_StatusTypeDef USB_SetCurrentMode(USB_TypeDef *USBx, USB_ModeTypeDef mode)
1618
{
1856
{
1619
  /* Prevent unused argument(s) compilation warning */
1857
  /* Prevent unused argument(s) compilation warning */
1620
  UNUSED(USBx);
1858
  UNUSED(USBx);
1621
  UNUSED(mode);
1859
  UNUSED(mode);
-
 
1860
 
1622
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
1861
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
1623
              only by USB OTG FS peripheral.
1862
              only by USB OTG FS peripheral.
1624
            - This function is added to ensure compatibility across platforms.
1863
            - This function is added to ensure compatibility across platforms.
1625
   */
1864
   */
1626
  return HAL_OK;
1865
  return HAL_OK;
1627
}
1866
}
1628
 
1867
 
1629
/**
1868
/**
1630
  * @brief  USB_DevInit : Initializes the USB controller registers
1869
  * @brief  USB_DevInit Initializes the USB controller registers
1631
  *         for device mode
1870
  *         for device mode
1632
  * @param  USBx : Selected device
1871
  * @param  USBx Selected device
1633
  * @param  cfg  : pointer to a USB_CfgTypeDef structure that contains
1872
  * @param  cfg  pointer to a USB_CfgTypeDef structure that contains
1634
  *         the configuration information for the specified USBx peripheral.
1873
  *         the configuration information for the specified USBx peripheral.
1635
  * @retval HAL status
1874
  * @retval HAL status
1636
  */
1875
  */
1637
HAL_StatusTypeDef USB_DevInit (USB_TypeDef *USBx, USB_CfgTypeDef cfg)
1876
HAL_StatusTypeDef USB_DevInit(USB_TypeDef *USBx, USB_CfgTypeDef cfg)
1638
{
1877
{
1639
  /* Prevent unused argument(s) compilation warning */
1878
  /* Prevent unused argument(s) compilation warning */
1640
  UNUSED(cfg);
1879
  UNUSED(cfg);
1641
 
1880
 
1642
  /* Init Device */
1881
  /* Init Device */
1643
  /*CNTR_FRES = 1*/
1882
  /* CNTR_FRES = 1 */
1644
  USBx->CNTR = USB_CNTR_FRES;
1883
  USBx->CNTR = (uint16_t)USB_CNTR_FRES;
1645
 
1884
 
1646
  /*CNTR_FRES = 0*/
1885
  /* CNTR_FRES = 0 */
1647
  USBx->CNTR = 0;
1886
  USBx->CNTR = 0U;
1648
 
1887
 
1649
  /*Clear pending interrupts*/
1888
  /* Clear pending interrupts */
1650
  USBx->ISTR = 0;
1889
  USBx->ISTR = 0U;
1651
 
1890
 
1652
  /*Set Btable Address*/
1891
  /*Set Btable Address*/
1653
  USBx->BTABLE = BTABLE_ADDRESS;
1892
  USBx->BTABLE = BTABLE_ADDRESS;
1654
 
1893
 
1655
  /* Enable USB Device Interrupt mask */
-
 
1656
  USB_EnableGlobalInt(USBx);
-
 
1657
   
-
 
1658
  return HAL_OK;
1894
  return HAL_OK;
1659
}
1895
}
1660
 
1896
 
1661
/**
1897
/**
1662
  * @brief  USB_FlushTxFifo : Flush a Tx FIFO
1898
  * @brief  USB_FlushTxFifo : Flush a Tx FIFO
Line 1664... Line 1900...
1664
  * @param  num : FIFO number
1900
  * @param  num : FIFO number
1665
  *         This parameter can be a value from 1 to 15
1901
  *         This parameter can be a value from 1 to 15
1666
            15 means Flush all Tx FIFOs
1902
            15 means Flush all Tx FIFOs
1667
  * @retval HAL status
1903
  * @retval HAL status
1668
  */
1904
  */
1669
HAL_StatusTypeDef USB_FlushTxFifo (USB_TypeDef *USBx, uint32_t num)
1905
HAL_StatusTypeDef USB_FlushTxFifo(USB_TypeDef *USBx, uint32_t num)
1670
{
1906
{
1671
  /* Prevent unused argument(s) compilation warning */
1907
  /* Prevent unused argument(s) compilation warning */
1672
  UNUSED(USBx);
1908
  UNUSED(USBx);
1673
  UNUSED(num);
1909
  UNUSED(num);
-
 
1910
 
1674
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
1911
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
1675
              only by USB OTG FS peripheral.
1912
              only by USB OTG FS peripheral.
1676
            - This function is added to ensure compatibility across platforms.
1913
            - This function is added to ensure compatibility across platforms.
1677
   */
1914
   */
-
 
1915
 
1678
  return HAL_OK;
1916
  return HAL_OK;
1679
}
1917
}
1680
 
1918
 
1681
/**
1919
/**
1682
  * @brief  USB_FlushRxFifo : Flush Rx FIFO
1920
  * @brief  USB_FlushRxFifo : Flush Rx FIFO
Line 1685... Line 1923...
1685
  */
1923
  */
1686
HAL_StatusTypeDef USB_FlushRxFifo(USB_TypeDef *USBx)
1924
HAL_StatusTypeDef USB_FlushRxFifo(USB_TypeDef *USBx)
1687
{
1925
{
1688
  /* Prevent unused argument(s) compilation warning */
1926
  /* Prevent unused argument(s) compilation warning */
1689
  UNUSED(USBx);
1927
  UNUSED(USBx);
-
 
1928
 
1690
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
1929
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
1691
              only by USB OTG FS peripheral.
1930
              only by USB OTG FS peripheral.
1692
            - This function is added to ensure compatibility across platforms.
1931
            - This function is added to ensure compatibility across platforms.
1693
   */
1932
   */
-
 
1933
 
1694
  return HAL_OK;
1934
  return HAL_OK;
1695
}
1935
}
1696
 
1936
 
1697
/**
1937
/**
1698
  * @brief  Activate and configure an endpoint
1938
  * @brief  Activate and configure an endpoint
1699
  * @param  USBx : Selected device
1939
  * @param  USBx Selected device
1700
  * @param  ep: pointer to endpoint structure
1940
  * @param  ep pointer to endpoint structure
1701
  * @retval HAL status
1941
  * @retval HAL status
1702
  */
1942
  */
1703
HAL_StatusTypeDef USB_ActivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep)
1943
HAL_StatusTypeDef USB_ActivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep)
1704
{
1944
{
-
 
1945
  HAL_StatusTypeDef ret = HAL_OK;
-
 
1946
  uint16_t wEpRegVal;
-
 
1947
 
-
 
1948
  wEpRegVal = PCD_GET_ENDPOINT(USBx, ep->num) & USB_EP_T_MASK;
-
 
1949
 
1705
  /* initialize Endpoint */
1950
  /* initialize Endpoint */
1706
  switch (ep->type)
1951
  switch (ep->type)
1707
  {
1952
  {
1708
  case EP_TYPE_CTRL:
1953
    case EP_TYPE_CTRL:
1709
    PCD_SET_EPTYPE(USBx, ep->num, USB_EP_CONTROL);
1954
      wEpRegVal |= USB_EP_CONTROL;
1710
    break;
1955
      break;
-
 
1956
 
1711
  case EP_TYPE_BULK:
1957
    case EP_TYPE_BULK:
1712
    PCD_SET_EPTYPE(USBx, ep->num, USB_EP_BULK);
1958
      wEpRegVal |= USB_EP_BULK;
1713
    break;
1959
      break;
-
 
1960
 
1714
  case EP_TYPE_INTR:
1961
    case EP_TYPE_INTR:
1715
    PCD_SET_EPTYPE(USBx, ep->num, USB_EP_INTERRUPT);
1962
      wEpRegVal |= USB_EP_INTERRUPT;
1716
    break;
1963
      break;
-
 
1964
 
1717
  case EP_TYPE_ISOC:
1965
    case EP_TYPE_ISOC:
1718
    PCD_SET_EPTYPE(USBx, ep->num, USB_EP_ISOCHRONOUS);
1966
      wEpRegVal |= USB_EP_ISOCHRONOUS;
1719
    break;
1967
      break;
-
 
1968
 
1720
  default:
1969
    default:
-
 
1970
      ret = HAL_ERROR;
1721
      break;
1971
      break;
1722
  }
1972
  }
-
 
1973
 
-
 
1974
  PCD_SET_ENDPOINT(USBx, ep->num, (wEpRegVal | USB_EP_CTR_RX | USB_EP_CTR_TX));
1723
 
1975
 
1724
  PCD_SET_EP_ADDRESS(USBx, ep->num, ep->num);
1976
  PCD_SET_EP_ADDRESS(USBx, ep->num, ep->num);
1725
 
1977
 
1726
  if (ep->doublebuffer == 0)
1978
  if (ep->doublebuffer == 0U)
1727
  {
1979
  {
1728
    if (ep->is_in)
1980
    if (ep->is_in != 0U)
1729
    {
1981
    {
1730
      /*Set the endpoint Transmit buffer address */
1982
      /*Set the endpoint Transmit buffer address */
1731
      PCD_SET_EP_TX_ADDRESS(USBx, ep->num, ep->pmaadress);
1983
      PCD_SET_EP_TX_ADDRESS(USBx, ep->num, ep->pmaadress);
1732
      PCD_CLEAR_TX_DTOG(USBx, ep->num);
1984
      PCD_CLEAR_TX_DTOG(USBx, ep->num);
-
 
1985
 
-
 
1986
      if (ep->type != EP_TYPE_ISOC)
-
 
1987
      {
1733
      /* Configure NAK status for the Endpoint*/
1988
        /* Configure NAK status for the Endpoint */
1734
      PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
1989
        PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
-
 
1990
      }
-
 
1991
      else
-
 
1992
      {
-
 
1993
        /* Configure TX Endpoint to disabled state */
-
 
1994
        PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
-
 
1995
      }
1735
    }
1996
    }
1736
    else
1997
    else
1737
    {
1998
    {
1738
      /*Set the endpoint Receive buffer address */
1999
      /*Set the endpoint Receive buffer address */
1739
      PCD_SET_EP_RX_ADDRESS(USBx, ep->num, ep->pmaadress);
2000
      PCD_SET_EP_RX_ADDRESS(USBx, ep->num, ep->pmaadress);
-
 
2001
 
1740
      /*Set the endpoint Receive buffer counter*/
2002
      /*Set the endpoint Receive buffer counter*/
1741
      PCD_SET_EP_RX_CNT(USBx, ep->num, ep->maxpacket);
2003
      PCD_SET_EP_RX_CNT(USBx, ep->num, ep->maxpacket);
1742
      PCD_CLEAR_RX_DTOG(USBx, ep->num);
2004
      PCD_CLEAR_RX_DTOG(USBx, ep->num);
-
 
2005
 
1743
      /* Configure VALID status for the Endpoint*/
2006
      /* Configure VALID status for the Endpoint*/
1744
      PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
2007
      PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
1745
    }
2008
    }
1746
  }
2009
  }
1747
  /*Double Buffer*/
2010
  /*Double Buffer*/
1748
  else
2011
  else
1749
  {
2012
  {
1750
    /*Set the endpoint as double buffered*/
2013
    /* Set the endpoint as double buffered */
1751
    PCD_SET_EP_DBUF(USBx, ep->num);
2014
    PCD_SET_EP_DBUF(USBx, ep->num);
-
 
2015
 
1752
    /*Set buffer address for double buffered mode*/
2016
    /* Set buffer address for double buffered mode */
1753
    PCD_SET_EP_DBUF_ADDR(USBx, ep->num,ep->pmaaddr0, ep->pmaaddr1);
2017
    PCD_SET_EP_DBUF_ADDR(USBx, ep->num, ep->pmaaddr0, ep->pmaaddr1);
1754
   
2018
 
1755
    if (ep->is_in==0)
2019
    if (ep->is_in == 0U)
1756
    {
2020
    {
1757
      /* Clear the data toggle bits for the endpoint IN/OUT*/
2021
      /* Clear the data toggle bits for the endpoint IN/OUT */
1758
      PCD_CLEAR_RX_DTOG(USBx, ep->num);
2022
      PCD_CLEAR_RX_DTOG(USBx, ep->num);
1759
      PCD_CLEAR_TX_DTOG(USBx, ep->num);
2023
      PCD_CLEAR_TX_DTOG(USBx, ep->num);
1760
     
-
 
1761
      /* Reset value of the data toggle bits for the endpoint out*/
-
 
1762
      PCD_TX_DTOG(USBx, ep->num);
-
 
1763
     
2024
 
1764
      PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
2025
      PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
1765
      PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
2026
      PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
1766
    }
2027
    }
1767
    else
2028
    else
1768
    {
2029
    {
1769
      /* Clear the data toggle bits for the endpoint IN/OUT*/
2030
      /* Clear the data toggle bits for the endpoint IN/OUT */
1770
      PCD_CLEAR_RX_DTOG(USBx, ep->num);
2031
      PCD_CLEAR_RX_DTOG(USBx, ep->num);
1771
      PCD_CLEAR_TX_DTOG(USBx, ep->num);
2032
      PCD_CLEAR_TX_DTOG(USBx, ep->num);
-
 
2033
 
1772
      PCD_RX_DTOG(USBx, ep->num);
2034
      if (ep->type != EP_TYPE_ISOC)
-
 
2035
      {
1773
      /* Configure DISABLE status for the Endpoint*/
2036
        /* Configure NAK status for the Endpoint */
-
 
2037
        PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
-
 
2038
      }
-
 
2039
      else
-
 
2040
      {
-
 
2041
        /* Configure TX Endpoint to disabled state */
1774
      PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
2042
        PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
-
 
2043
      }
-
 
2044
 
1775
      PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
2045
      PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
1776
    }
2046
    }
1777
  }
2047
  }
1778
 
2048
 
1779
  return HAL_OK;
2049
  return ret;
1780
}
2050
}
1781
 
2051
 
1782
/**
2052
/**
1783
  * @brief  De-activate and de-initialize an endpoint
2053
  * @brief  De-activate and de-initialize an endpoint
1784
  * @param  USBx : Selected device
2054
  * @param  USBx Selected device
1785
  * @param  ep: pointer to endpoint structure
2055
  * @param  ep pointer to endpoint structure
1786
  * @retval HAL status
2056
  * @retval HAL status
1787
  */
2057
  */
1788
HAL_StatusTypeDef USB_DeactivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep)
2058
HAL_StatusTypeDef USB_DeactivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep)
1789
{
2059
{
1790
  if (ep->doublebuffer == 0)
2060
  if (ep->doublebuffer == 0U)
1791
  {
2061
  {
1792
    if (ep->is_in)
2062
    if (ep->is_in != 0U)
1793
    {
2063
    {
1794
      PCD_CLEAR_TX_DTOG(USBx, ep->num);
2064
      PCD_CLEAR_TX_DTOG(USBx, ep->num);
-
 
2065
 
1795
      /* Configure DISABLE status for the Endpoint*/
2066
      /* Configure DISABLE status for the Endpoint*/
1796
      PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
2067
      PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
1797
    }
2068
    }
1798
    else
2069
    else
1799
    {
2070
    {
1800
      PCD_CLEAR_RX_DTOG(USBx, ep->num);
2071
      PCD_CLEAR_RX_DTOG(USBx, ep->num);
-
 
2072
 
1801
      /* Configure DISABLE status for the Endpoint*/
2073
      /* Configure DISABLE status for the Endpoint*/
1802
      PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
2074
      PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
1803
    }
2075
    }
1804
  }
2076
  }
1805
  /*Double Buffer*/
2077
  /*Double Buffer*/
1806
  else
2078
  else
1807
  {
2079
  {
1808
    if (ep->is_in==0)
2080
    if (ep->is_in == 0U)
1809
    {
2081
    {
1810
      /* Clear the data toggle bits for the endpoint IN/OUT*/
2082
      /* Clear the data toggle bits for the endpoint IN/OUT*/
1811
      PCD_CLEAR_RX_DTOG(USBx, ep->num);
2083
      PCD_CLEAR_RX_DTOG(USBx, ep->num);
1812
      PCD_CLEAR_TX_DTOG(USBx, ep->num);
2084
      PCD_CLEAR_TX_DTOG(USBx, ep->num);
1813
     
2085
 
1814
      /* Reset value of the data toggle bits for the endpoint out*/
2086
      /* Reset value of the data toggle bits for the endpoint out*/
1815
      PCD_TX_DTOG(USBx, ep->num);
2087
      PCD_TX_DTOG(USBx, ep->num);
1816
     
2088
 
1817
      PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
2089
      PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
1818
      PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
2090
      PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
1819
    }
2091
    }
1820
    else
2092
    else
1821
    {
2093
    {
1822
      /* Clear the data toggle bits for the endpoint IN/OUT*/
2094
      /* Clear the data toggle bits for the endpoint IN/OUT*/
1823
      PCD_CLEAR_RX_DTOG(USBx, ep->num);
2095
      PCD_CLEAR_RX_DTOG(USBx, ep->num);
1824
      PCD_CLEAR_TX_DTOG(USBx, ep->num);
2096
      PCD_CLEAR_TX_DTOG(USBx, ep->num);
1825
      PCD_RX_DTOG(USBx, ep->num);
2097
      PCD_RX_DTOG(USBx, ep->num);
-
 
2098
 
1826
      /* Configure DISABLE status for the Endpoint*/
2099
      /* Configure DISABLE status for the Endpoint*/
1827
      PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
2100
      PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
1828
      PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
2101
      PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
1829
    }
2102
    }
1830
  }
2103
  }
1831
 
2104
 
1832
  return HAL_OK;
2105
  return HAL_OK;
1833
}
2106
}
1834
 
2107
 
1835
/**
2108
/**
1836
  * @brief  USB_EPStartXfer : setup and starts a transfer over an EP
2109
  * @brief  USB_EPStartXfer setup and starts a transfer over an EP
1837
  * @param  USBx : Selected device
2110
  * @param  USBx Selected device
1838
  * @param  ep: pointer to endpoint structure
2111
  * @param  ep pointer to endpoint structure
1839
  * @retval HAL status
2112
  * @retval HAL status
1840
  */
2113
  */
1841
HAL_StatusTypeDef USB_EPStartXfer(USB_TypeDef *USBx , USB_EPTypeDef *ep)
2114
HAL_StatusTypeDef USB_EPStartXfer(USB_TypeDef *USBx, USB_EPTypeDef *ep)
1842
{
2115
{
-
 
2116
  uint32_t len;
1843
  uint16_t pmabuffer = 0;
2117
  uint16_t pmabuffer;
1844
  uint32_t len = ep->xfer_len;
2118
  uint16_t wEPVal;
1845
 
2119
 
1846
  /* IN endpoint */
2120
  /* IN endpoint */
1847
  if (ep->is_in == 1)
2121
  if (ep->is_in == 1U)
1848
  {
2122
  {
1849
    /*Multi packet transfer*/
2123
    /*Multi packet transfer*/
1850
    if (ep->xfer_len > ep->maxpacket)
2124
    if (ep->xfer_len > ep->maxpacket)
1851
    {
2125
    {
1852
      len=ep->maxpacket;
2126
      len = ep->maxpacket;
1853
      ep->xfer_len-=len;
-
 
1854
    }
2127
    }
1855
    else
2128
    else
1856
    {  
2129
    {
1857
      len=ep->xfer_len;
2130
      len = ep->xfer_len;
1858
      ep->xfer_len =0;
-
 
1859
    }
2131
    }
1860
   
2132
 
1861
    /* configure and validate Tx endpoint */
2133
    /* configure and validate Tx endpoint */
1862
    if (ep->doublebuffer == 0)
2134
    if (ep->doublebuffer == 0U)
1863
    {
2135
    {
1864
      USB_WritePMA(USBx, ep->xfer_buff, ep->pmaadress, len);
2136
      USB_WritePMA(USBx, ep->xfer_buff, ep->pmaadress, (uint16_t)len);
1865
      PCD_SET_EP_TX_CNT(USBx, ep->num, len);
2137
      PCD_SET_EP_TX_CNT(USBx, ep->num, len);
1866
    }
2138
    }
1867
    else
2139
    else
1868
    {
2140
    {
1869
      /* Write the data to the USB endpoint */
2141
      /* double buffer bulk management */
1870
      if (PCD_GET_ENDPOINT(USBx, ep->num)& USB_EP_DTOG_TX)
2142
      if (ep->type == EP_TYPE_BULK)
1871
      {
2143
      {
-
 
2144
        if (ep->xfer_len_db > ep->maxpacket)
-
 
2145
        {
-
 
2146
          /* enable double buffer */
-
 
2147
          PCD_SET_EP_DBUF(USBx, ep->num);
-
 
2148
 
-
 
2149
          /* each Time to write in PMA xfer_len_db will */
-
 
2150
          ep->xfer_len_db -= len;
-
 
2151
 
-
 
2152
          /* Fill the two first buffer in the Buffer0 & Buffer1 */
-
 
2153
          if ((PCD_GET_ENDPOINT(USBx, ep->num) & USB_EP_DTOG_TX) != 0U)
-
 
2154
          {
-
 
2155
            /* Set the Double buffer counter for pmabuffer1 */
-
 
2156
            PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
-
 
2157
            pmabuffer = ep->pmaaddr1;
-
 
2158
 
-
 
2159
            /* Write the user buffer to USB PMA */
-
 
2160
            USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
-
 
2161
            ep->xfer_buff += len;
-
 
2162
 
-
 
2163
            if (ep->xfer_len_db > ep->maxpacket)
-
 
2164
            {
-
 
2165
              ep->xfer_len_db -= len;
-
 
2166
            }
-
 
2167
            else
-
 
2168
            {
-
 
2169
              len = ep->xfer_len_db;
-
 
2170
              ep->xfer_len_db = 0U;
-
 
2171
            }
-
 
2172
 
-
 
2173
            /* Set the Double buffer counter for pmabuffer0 */
-
 
2174
            PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len);
-
 
2175
            pmabuffer = ep->pmaaddr0;
-
 
2176
 
-
 
2177
            /* Write the user buffer to USB PMA */
-
 
2178
            USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
-
 
2179
          }
-
 
2180
          else
-
 
2181
          {
-
 
2182
            /* Set the Double buffer counter for pmabuffer0 */
-
 
2183
            PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len);
-
 
2184
            pmabuffer = ep->pmaaddr0;
-
 
2185
 
-
 
2186
            /* Write the user buffer to USB PMA */
-
 
2187
            USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
-
 
2188
            ep->xfer_buff += len;
-
 
2189
 
-
 
2190
            if (ep->xfer_len_db > ep->maxpacket)
-
 
2191
            {
-
 
2192
              ep->xfer_len_db -= len;
-
 
2193
            }
-
 
2194
            else
-
 
2195
            {
-
 
2196
              len = ep->xfer_len_db;
-
 
2197
              ep->xfer_len_db = 0U;
-
 
2198
            }
-
 
2199
 
1872
        /* Set the Double buffer counter for pmabuffer1 */
2200
            /* Set the Double buffer counter for pmabuffer1 */
1873
        PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
2201
            PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
-
 
2202
            pmabuffer = ep->pmaaddr1;
-
 
2203
 
-
 
2204
            /* Write the user buffer to USB PMA */
-
 
2205
            USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
-
 
2206
          }
-
 
2207
        }
-
 
2208
        /* auto Switch to single buffer mode when transfer <Mps no need to manage in double buffer */
-
 
2209
        else
-
 
2210
        {
-
 
2211
          len = ep->xfer_len_db;
-
 
2212
 
-
 
2213
          /* disable double buffer mode */
-
 
2214
          PCD_CLEAR_EP_DBUF(USBx, ep->num);
-
 
2215
 
-
 
2216
          /* Set Tx count with nbre of byte to be transmitted */
-
 
2217
          PCD_SET_EP_TX_CNT(USBx, ep->num, len);
1874
        pmabuffer = ep->pmaaddr1;
2218
          pmabuffer = ep->pmaaddr0;
-
 
2219
 
-
 
2220
          /* Write the user buffer to USB PMA */
-
 
2221
          USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
1875
      }
2222
        }
-
 
2223
      }/* end if bulk double buffer */
-
 
2224
 
-
 
2225
      /* manage isochronous double buffer IN mode */
1876
      else
2226
      else
1877
      {
2227
      {
-
 
2228
        /* Write the data to the USB endpoint */
-
 
2229
        if ((PCD_GET_ENDPOINT(USBx, ep->num) & USB_EP_DTOG_TX) != 0U)
-
 
2230
        {
-
 
2231
          /* Set the Double buffer counter for pmabuffer1 */
-
 
2232
          PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
-
 
2233
          pmabuffer = ep->pmaaddr1;
-
 
2234
        }
-
 
2235
        else
-
 
2236
        {
1878
        /* Set the Double buffer counter for pmabuffer0 */
2237
          /* Set the Double buffer counter for pmabuffer0 */
1879
        PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len);
2238
          PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len);
1880
        pmabuffer = ep->pmaaddr0;
2239
          pmabuffer = ep->pmaaddr0;
-
 
2240
        }
-
 
2241
 
-
 
2242
        USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
-
 
2243
        PCD_FreeUserBuffer(USBx, ep->num, ep->is_in);
1881
      }
2244
      }
1882
      USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, len);
-
 
1883
      PCD_FreeUserBuffer(USBx, ep->num, ep->is_in);
-
 
1884
    }
2245
    }
1885
   
2246
 
1886
    PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_VALID);
2247
    PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_VALID);
1887
  }
2248
  }
1888
  else /* OUT endpoint */
2249
  else /* OUT endpoint */
1889
  {
2250
  {
1890
    /* Multi packet transfer*/
-
 
1891
    if (ep->xfer_len > ep->maxpacket)
2251
    if (ep->doublebuffer == 0U)
1892
    {
2252
    {
-
 
2253
      /* Multi packet transfer */
-
 
2254
      if (ep->xfer_len > ep->maxpacket)
-
 
2255
      {
1893
      len=ep->maxpacket;
2256
        len = ep->maxpacket;
1894
      ep->xfer_len-=len;
2257
        ep->xfer_len -= len;
1895
    }
2258
      }
1896
    else
2259
      else
1897
    {
2260
      {
1898
      len=ep->xfer_len;
2261
        len = ep->xfer_len;
1899
      ep->xfer_len =0;
2262
        ep->xfer_len = 0U;
1900
    }
2263
      }
1901
   
-
 
1902
    /* configure and validate Rx endpoint */
2264
      /* configure and validate Rx endpoint */
1903
    if (ep->doublebuffer == 0)
-
 
1904
    {
-
 
1905
      /*Set RX buffer count*/
-
 
1906
      PCD_SET_EP_RX_CNT(USBx, ep->num, len);
2265
      PCD_SET_EP_RX_CNT(USBx, ep->num, len);
1907
    }
2266
    }
1908
    else
2267
    else
1909
    {
2268
    {
-
 
2269
      /* First Transfer Coming From HAL_PCD_EP_Receive & From ISR */
1910
      /*Set the Double buffer counter*/
2270
      /* Set the Double buffer counter */
-
 
2271
      if (ep->type == EP_TYPE_BULK)
-
 
2272
      {
-
 
2273
        PCD_SET_EP_DBUF_CNT(USBx, ep->num, ep->is_in, ep->maxpacket);
-
 
2274
 
-
 
2275
        /* Coming from ISR */
-
 
2276
        if (ep->xfer_count != 0U)
-
 
2277
        {
-
 
2278
          /* update last value to check if there is blocking state */
-
 
2279
          wEPVal = PCD_GET_ENDPOINT(USBx, ep->num);
-
 
2280
 
-
 
2281
          /*Blocking State */
-
 
2282
          if ((((wEPVal & USB_EP_DTOG_RX) != 0U) && ((wEPVal & USB_EP_DTOG_TX) != 0U)) ||
-
 
2283
              (((wEPVal & USB_EP_DTOG_RX) == 0U) && ((wEPVal & USB_EP_DTOG_TX) == 0U)))
-
 
2284
          {
-
 
2285
            PCD_FreeUserBuffer(USBx, ep->num, 0U);
-
 
2286
          }
-
 
2287
        }
-
 
2288
      }
-
 
2289
      /* iso out double */
-
 
2290
      else if (ep->type == EP_TYPE_ISOC)
-
 
2291
      {
-
 
2292
        /* Multi packet transfer */
-
 
2293
        if (ep->xfer_len > ep->maxpacket)
-
 
2294
        {
-
 
2295
          len = ep->maxpacket;
-
 
2296
          ep->xfer_len -= len;
-
 
2297
        }
-
 
2298
        else
-
 
2299
        {
-
 
2300
          len = ep->xfer_len;
-
 
2301
          ep->xfer_len = 0U;
-
 
2302
        }
1911
      PCD_SET_EP_DBUF_CNT(USBx, ep->num, ep->is_in, len);
2303
        PCD_SET_EP_DBUF_CNT(USBx, ep->num, ep->is_in, len);
-
 
2304
      }
-
 
2305
      else
-
 
2306
      {
-
 
2307
        return HAL_ERROR;
-
 
2308
      }
1912
    }
2309
    }
1913
   
2310
 
1914
    PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
2311
    PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
1915
  }
2312
  }
1916
 
-
 
1917
  return HAL_OK;
-
 
1918
}
-
 
1919
 
2313
 
1920
/**
-
 
1921
  * @brief  USB_WritePacket : Writes a packet into the Tx FIFO associated
-
 
1922
  *         with the EP/channel
-
 
1923
  * @param  USBx : Selected device
-
 
1924
  * @param  src :  pointer to source buffer
-
 
1925
  * @param  ch_ep_num : endpoint or host channel number
-
 
1926
  * @param  len : Number of bytes to write
-
 
1927
  * @retval HAL status
-
 
1928
  */
-
 
1929
HAL_StatusTypeDef USB_WritePacket(USB_TypeDef *USBx, uint8_t *src, uint8_t ch_ep_num, uint16_t len)
-
 
1930
{
-
 
1931
  /* Prevent unused argument(s) compilation warning */
-
 
1932
  UNUSED(USBx);
-
 
1933
  UNUSED(src);
-
 
1934
  UNUSED(ch_ep_num);
-
 
1935
  UNUSED(len);
-
 
1936
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
-
 
1937
              only by USB OTG FS peripheral.
-
 
1938
            - This function is added to ensure compatibility across platforms.
-
 
1939
   */
-
 
1940
  return HAL_OK;
2314
  return HAL_OK;
1941
}
2315
}
1942
 
2316
 
1943
/**
-
 
1944
  * @brief  USB_ReadPacket : read a packet from the Tx FIFO associated
-
 
1945
  *         with the EP/channel
-
 
1946
  * @param  USBx : Selected device
-
 
1947
  * @param  dest : destination pointer
-
 
1948
  * @param  len : Number of bytes to read
-
 
1949
  * @retval pointer to destination buffer
-
 
1950
  */
-
 
1951
void *USB_ReadPacket(USB_TypeDef *USBx, uint8_t *dest, uint16_t len)
-
 
1952
{
-
 
1953
  /* Prevent unused argument(s) compilation warning */
-
 
1954
  UNUSED(USBx);
-
 
1955
  UNUSED(dest);
-
 
1956
  UNUSED(len);
-
 
1957
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
-
 
1958
              only by USB OTG FS peripheral.
-
 
1959
            - This function is added to ensure compatibility across platforms.
-
 
1960
   */
-
 
1961
  return ((void *)NULL);
-
 
1962
}
-
 
1963
 
2317
 
1964
/**
2318
/**
1965
  * @brief  USB_EPSetStall : set a stall condition over an EP
2319
  * @brief  USB_EPSetStall set a stall condition over an EP
1966
  * @param  USBx : Selected device
2320
  * @param  USBx Selected device
1967
  * @param  ep: pointer to endpoint structure  
2321
  * @param  ep pointer to endpoint structure
1968
  * @retval HAL status
2322
  * @retval HAL status
1969
  */
2323
  */
1970
HAL_StatusTypeDef USB_EPSetStall(USB_TypeDef *USBx , USB_EPTypeDef *ep)
2324
HAL_StatusTypeDef USB_EPSetStall(USB_TypeDef *USBx, USB_EPTypeDef *ep)
1971
{
2325
{
1972
  if (ep->num == 0)
2326
  if (ep->is_in != 0U)
1973
  {
2327
  {
1974
    /* This macro sets STALL status for RX & TX*/
-
 
1975
    PCD_SET_EP_TXRX_STATUS(USBx, ep->num, USB_EP_RX_STALL, USB_EP_TX_STALL);
2328
    PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_STALL);
1976
  }
2329
  }
1977
  else
2330
  else
1978
  {
2331
  {
1979
    if (ep->is_in)
-
 
1980
    {
-
 
1981
      PCD_SET_EP_TX_STATUS(USBx, ep->num , USB_EP_TX_STALL);
-
 
1982
    }
-
 
1983
    else
-
 
1984
    {
-
 
1985
      PCD_SET_EP_RX_STATUS(USBx, ep->num , USB_EP_RX_STALL);
2332
    PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_STALL);
1986
    }
-
 
1987
  }
2333
  }
-
 
2334
 
1988
  return HAL_OK;
2335
  return HAL_OK;
1989
}
2336
}
1990
 
2337
 
1991
/**
2338
/**
1992
  * @brief  USB_EPClearStall : Clear a stall condition over an EP
2339
  * @brief  USB_EPClearStall Clear a stall condition over an EP
1993
  * @param  USBx : Selected device
2340
  * @param  USBx Selected device
1994
  * @param  ep: pointer to endpoint structure
2341
  * @param  ep pointer to endpoint structure
1995
  * @retval HAL status
2342
  * @retval HAL status
1996
  */
2343
  */
1997
HAL_StatusTypeDef USB_EPClearStall(USB_TypeDef *USBx, USB_EPTypeDef *ep)
2344
HAL_StatusTypeDef USB_EPClearStall(USB_TypeDef *USBx, USB_EPTypeDef *ep)
1998
{
2345
{
1999
  if (ep->is_in)
2346
  if (ep->doublebuffer == 0U)
2000
  {
-
 
2001
    PCD_CLEAR_TX_DTOG(USBx, ep->num);
-
 
2002
    PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_VALID);
-
 
2003
  }
-
 
2004
  else
-
 
2005
  {
2347
  {
-
 
2348
    if (ep->is_in != 0U)
-
 
2349
    {
-
 
2350
      PCD_CLEAR_TX_DTOG(USBx, ep->num);
-
 
2351
 
-
 
2352
      if (ep->type != EP_TYPE_ISOC)
-
 
2353
      {
-
 
2354
        /* Configure NAK status for the Endpoint */
-
 
2355
        PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
-
 
2356
      }
-
 
2357
    }
-
 
2358
    else
-
 
2359
    {
2006
    PCD_CLEAR_RX_DTOG(USBx, ep->num);
2360
      PCD_CLEAR_RX_DTOG(USBx, ep->num);
-
 
2361
 
-
 
2362
      /* Configure VALID status for the Endpoint */
2007
    PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
2363
      PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
-
 
2364
    }
2008
  }
2365
  }
-
 
2366
 
2009
  return HAL_OK;
2367
  return HAL_OK;
2010
}
2368
}
2011
 
2369
 
2012
/**
2370
/**
2013
  * @brief  USB_StopDevice : Stop the usb device mode
2371
  * @brief  USB_StopDevice Stop the usb device mode
2014
  * @param  USBx : Selected device
2372
  * @param  USBx Selected device
2015
  * @retval HAL status
2373
  * @retval HAL status
2016
  */
2374
  */
2017
HAL_StatusTypeDef USB_StopDevice(USB_TypeDef *USBx)
2375
HAL_StatusTypeDef USB_StopDevice(USB_TypeDef *USBx)
2018
{
2376
{
2019
  /* disable all interrupts and force USB reset */
2377
  /* disable all interrupts and force USB reset */
2020
  USBx->CNTR = USB_CNTR_FRES;
2378
  USBx->CNTR = (uint16_t)USB_CNTR_FRES;
2021
 
2379
 
2022
  /* clear interrupt status register */
2380
  /* clear interrupt status register */
2023
  USBx->ISTR = 0;
2381
  USBx->ISTR = 0U;
2024
 
2382
 
2025
  /* switch-off device */
2383
  /* switch-off device */
2026
  USBx->CNTR = (USB_CNTR_FRES | USB_CNTR_PDWN);
2384
  USBx->CNTR = (uint16_t)(USB_CNTR_FRES | USB_CNTR_PDWN);
2027
 
2385
 
2028
  return HAL_OK;
2386
  return HAL_OK;
2029
}
2387
}
2030
 
2388
 
2031
/**
2389
/**
2032
  * @brief  USB_SetDevAddress : Stop the usb device mode
2390
  * @brief  USB_SetDevAddress Stop the usb device mode
2033
  * @param  USBx : Selected device
2391
  * @param  USBx Selected device
2034
  * @param  address : new device address to be assigned
2392
  * @param  address new device address to be assigned
2035
  *          This parameter can be a value from 0 to 255
2393
  *          This parameter can be a value from 0 to 255
2036
  * @retval HAL status
2394
  * @retval HAL status
2037
  */
2395
  */
2038
HAL_StatusTypeDef  USB_SetDevAddress (USB_TypeDef *USBx, uint8_t address)
2396
HAL_StatusTypeDef  USB_SetDevAddress(USB_TypeDef *USBx, uint8_t address)
2039
{
2397
{
2040
  if(address == 0)
2398
  if (address == 0U)
2041
  {
2399
  {
2042
   /* set device address and enable function */
2400
    /* set device address and enable function */
2043
   USBx->DADDR = USB_DADDR_EF;
2401
    USBx->DADDR = (uint16_t)USB_DADDR_EF;
2044
  }
2402
  }
2045
 
2403
 
2046
  return HAL_OK;
2404
  return HAL_OK;
2047
}
2405
}
2048
 
2406
 
2049
/**
2407
/**
2050
  * @brief  USB_DevConnect : Connect the USB device by enabling the pull-up/pull-down
2408
  * @brief  USB_DevConnect Connect the USB device by enabling the pull-up/pull-down
2051
  * @param  USBx : Selected device
2409
  * @param  USBx Selected device
2052
  * @retval HAL status
2410
  * @retval HAL status
2053
  */
2411
  */
2054
HAL_StatusTypeDef  USB_DevConnect (USB_TypeDef *USBx)
2412
HAL_StatusTypeDef  USB_DevConnect(USB_TypeDef *USBx)
2055
{
2413
{
2056
  /* Prevent unused argument(s) compilation warning */
2414
  /* Prevent unused argument(s) compilation warning */
2057
  UNUSED(USBx);
2415
  UNUSED(USBx);
-
 
2416
 
2058
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2417
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2059
              only by USB OTG FS peripheral.
2418
              only by USB OTG FS peripheral.
2060
            - This function is added to ensure compatibility across platforms.
2419
            - This function is added to ensure compatibility across platforms.
2061
   */
2420
   */
-
 
2421
 
2062
  return HAL_OK;
2422
  return HAL_OK;
2063
}
2423
}
2064
 
2424
 
2065
/**
2425
/**
2066
  * @brief  USB_DevDisconnect : Disconnect the USB device by disabling the pull-up/pull-down
2426
  * @brief  USB_DevDisconnect Disconnect the USB device by disabling the pull-up/pull-down
2067
  * @param  USBx : Selected device
2427
  * @param  USBx Selected device
2068
  * @retval HAL status
2428
  * @retval HAL status
2069
  */
2429
  */
2070
HAL_StatusTypeDef  USB_DevDisconnect (USB_TypeDef *USBx)
2430
HAL_StatusTypeDef  USB_DevDisconnect(USB_TypeDef *USBx)
2071
{
2431
{
2072
  /* Prevent unused argument(s) compilation warning */
2432
  /* Prevent unused argument(s) compilation warning */
2073
  UNUSED(USBx);
2433
  UNUSED(USBx);
-
 
2434
 
2074
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2435
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2075
              only by USB OTG FS peripheral.
2436
              only by USB OTG FS peripheral.
2076
            - This function is added to ensure compatibility across platforms.
2437
            - This function is added to ensure compatibility across platforms.
2077
   */
2438
   */
-
 
2439
 
2078
  return HAL_OK;
2440
  return HAL_OK;
2079
}
2441
}
2080
 
2442
 
2081
/**
2443
/**
2082
  * @brief  USB_ReadInterrupts: return the global USB interrupt status
2444
  * @brief  USB_ReadInterrupts return the global USB interrupt status
2083
  * @param  USBx : Selected device
2445
  * @param  USBx Selected device
2084
  * @retval HAL status
2446
  * @retval HAL status
2085
  */
2447
  */
2086
uint32_t  USB_ReadInterrupts (USB_TypeDef *USBx)
2448
uint32_t  USB_ReadInterrupts(USB_TypeDef *USBx)
2087
{
2449
{
2088
  uint32_t tmpreg = 0;
2450
  uint32_t tmpreg;
2089
 
2451
 
2090
  tmpreg = USBx->ISTR;
2452
  tmpreg = USBx->ISTR;
2091
  return tmpreg;
2453
  return tmpreg;
2092
}
2454
}
2093
 
2455
 
2094
/**
2456
/**
2095
  * @brief  USB_ReadDevAllOutEpInterrupt: return the USB device OUT endpoints interrupt status
2457
  * @brief  USB_ReadDevAllOutEpInterrupt return the USB device OUT endpoints interrupt status
2096
  * @param  USBx : Selected device
2458
  * @param  USBx Selected device
2097
  * @retval HAL status
2459
  * @retval HAL status
2098
  */
2460
  */
2099
uint32_t USB_ReadDevAllOutEpInterrupt (USB_TypeDef *USBx)
2461
uint32_t USB_ReadDevAllOutEpInterrupt(USB_TypeDef *USBx)
2100
{
2462
{
2101
  /* Prevent unused argument(s) compilation warning */
2463
  /* Prevent unused argument(s) compilation warning */
2102
  UNUSED(USBx);
2464
  UNUSED(USBx);
2103
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2465
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2104
              only by USB OTG FS peripheral.
2466
              only by USB OTG FS peripheral.
2105
            - This function is added to ensure compatibility across platforms.
2467
            - This function is added to ensure compatibility across platforms.
2106
   */
2468
   */
2107
  return (0);
2469
  return (0);
2108
}
2470
}
2109
 
2471
 
2110
/**
2472
/**
2111
  * @brief  USB_ReadDevAllInEpInterrupt: return the USB device IN endpoints interrupt status
2473
  * @brief  USB_ReadDevAllInEpInterrupt return the USB device IN endpoints interrupt status
2112
  * @param  USBx : Selected device
2474
  * @param  USBx Selected device
2113
  * @retval HAL status
2475
  * @retval HAL status
2114
  */
2476
  */
2115
uint32_t USB_ReadDevAllInEpInterrupt (USB_TypeDef *USBx)
2477
uint32_t USB_ReadDevAllInEpInterrupt(USB_TypeDef *USBx)
2116
{
2478
{
2117
  /* Prevent unused argument(s) compilation warning */
2479
  /* Prevent unused argument(s) compilation warning */
2118
  UNUSED(USBx);
2480
  UNUSED(USBx);
2119
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2481
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2120
              only by USB OTG FS peripheral.
2482
              only by USB OTG FS peripheral.
2121
            - This function is added to ensure compatibility across platforms.
2483
            - This function is added to ensure compatibility across platforms.
2122
   */
2484
   */
2123
  return (0);
2485
  return (0);
2124
}
2486
}
2125
 
2487
 
2126
/**
2488
/**
2127
  * @brief  Returns Device OUT EP Interrupt register
2489
  * @brief  Returns Device OUT EP Interrupt register
2128
  * @param  USBx : Selected device
2490
  * @param  USBx Selected device
2129
  * @param  epnum : endpoint number
2491
  * @param  epnum endpoint number
2130
  *          This parameter can be a value from 0 to 15
2492
  *          This parameter can be a value from 0 to 15
2131
  * @retval Device OUT EP Interrupt register
2493
  * @retval Device OUT EP Interrupt register
2132
  */
2494
  */
2133
uint32_t USB_ReadDevOutEPInterrupt (USB_TypeDef *USBx , uint8_t epnum)
2495
uint32_t USB_ReadDevOutEPInterrupt(USB_TypeDef *USBx, uint8_t epnum)
2134
{
2496
{
2135
  /* Prevent unused argument(s) compilation warning */
2497
  /* Prevent unused argument(s) compilation warning */
2136
  UNUSED(USBx);
2498
  UNUSED(USBx);
2137
  UNUSED(epnum);
2499
  UNUSED(epnum);
2138
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2500
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2139
              only by USB OTG FS peripheral.
2501
              only by USB OTG FS peripheral.
2140
            - This function is added to ensure compatibility across platforms.
2502
            - This function is added to ensure compatibility across platforms.
2141
   */
2503
   */
2142
  return (0);
2504
  return (0);
2143
}
2505
}
2144
 
2506
 
2145
/**
2507
/**
2146
  * @brief  Returns Device IN EP Interrupt register
2508
  * @brief  Returns Device IN EP Interrupt register
2147
  * @param  USBx : Selected device
2509
  * @param  USBx Selected device
2148
  * @param  epnum : endpoint number
2510
  * @param  epnum endpoint number
2149
  *          This parameter can be a value from 0 to 15
2511
  *          This parameter can be a value from 0 to 15
2150
  * @retval Device IN EP Interrupt register
2512
  * @retval Device IN EP Interrupt register
2151
  */
2513
  */
2152
uint32_t USB_ReadDevInEPInterrupt (USB_TypeDef *USBx , uint8_t epnum)
2514
uint32_t USB_ReadDevInEPInterrupt(USB_TypeDef *USBx, uint8_t epnum)
2153
{
2515
{
2154
  /* Prevent unused argument(s) compilation warning */
2516
  /* Prevent unused argument(s) compilation warning */
2155
  UNUSED(USBx);
2517
  UNUSED(USBx);
2156
  UNUSED(epnum);
2518
  UNUSED(epnum);
2157
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2519
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2158
              only by USB OTG FS peripheral.
2520
              only by USB OTG FS peripheral.
2159
            - This function is added to ensure compatibility across platforms.
2521
            - This function is added to ensure compatibility across platforms.
2160
   */
2522
   */
2161
  return (0);
2523
  return (0);
2162
}
2524
}
2163
 
2525
 
2164
/**
2526
/**
2165
  * @brief  USB_ClearInterrupts: clear a USB interrupt
2527
  * @brief  USB_ClearInterrupts: clear a USB interrupt
2166
  * @param  USBx : Selected device
2528
  * @param  USBx Selected device
2167
  * @param  interrupt : interrupt flag
2529
  * @param  interrupt  flag
2168
  * @retval None
2530
  * @retval None
2169
  */
2531
  */
2170
void  USB_ClearInterrupts (USB_TypeDef *USBx, uint32_t interrupt)
2532
void  USB_ClearInterrupts(USB_TypeDef *USBx, uint32_t interrupt)
2171
{
2533
{
2172
  /* Prevent unused argument(s) compilation warning */
2534
  /* Prevent unused argument(s) compilation warning */
2173
  UNUSED(USBx);
2535
  UNUSED(USBx);
2174
  UNUSED(interrupt);
2536
  UNUSED(interrupt);
2175
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2537
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2176
              only by USB OTG FS peripheral.
2538
              only by USB OTG FS peripheral.
2177
            - This function is added to ensure compatibility across platforms.
2539
            - This function is added to ensure compatibility across platforms.
2178
   */
2540
   */
2179
}
2541
}
2180
 
2542
 
2181
/**
2543
/**
2182
  * @brief  Prepare the EP0 to start the first control setup
2544
  * @brief  Prepare the EP0 to start the first control setup
2183
  * @param  USBx : Selected device
2545
  * @param  USBx Selected device
2184
  * @param  psetup : pointer to setup packet
2546
  * @param  psetup pointer to setup packet
2185
  * @retval HAL status
2547
  * @retval HAL status
2186
  */
2548
  */
2187
HAL_StatusTypeDef USB_EP0_OutStart(USB_TypeDef *USBx, uint8_t *psetup)
2549
HAL_StatusTypeDef USB_EP0_OutStart(USB_TypeDef *USBx, uint8_t *psetup)
2188
{
2550
{
2189
  /* Prevent unused argument(s) compilation warning */
2551
  /* Prevent unused argument(s) compilation warning */
2190
  UNUSED(USBx);
2552
  UNUSED(USBx);
2191
  UNUSED(psetup);
2553
  UNUSED(psetup);
2192
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2554
  /* NOTE : - This function is not required by USB Device FS peripheral, it is used
2193
              only by USB OTG FS peripheral.
2555
              only by USB OTG FS peripheral.
2194
            - This function is added to ensure compatibility across platforms.
2556
            - This function is added to ensure compatibility across platforms.
2195
   */
2557
   */
2196
  return HAL_OK;
2558
  return HAL_OK;
2197
}
2559
}
2198
 
2560
 
2199
/**
2561
/**
2200
  * @brief  USB_ActivateRemoteWakeup : active remote wakeup signalling
2562
  * @brief  USB_ActivateRemoteWakeup : active remote wakeup signalling
2201
  * @param  USBx : Selected device
2563
  * @param  USBx Selected device
2202
  * @retval HAL status
2564
  * @retval HAL status
2203
  */
2565
  */
2204
HAL_StatusTypeDef USB_ActivateRemoteWakeup(USB_TypeDef *USBx)
2566
HAL_StatusTypeDef USB_ActivateRemoteWakeup(USB_TypeDef *USBx)
2205
{
2567
{
2206
  USBx->CNTR |= USB_CNTR_RESUME;
2568
  USBx->CNTR |= (uint16_t)USB_CNTR_RESUME;
2207
 
2569
 
2208
  return HAL_OK;
2570
  return HAL_OK;
2209
}
2571
}
2210
 
2572
 
2211
/**
2573
/**
2212
  * @brief  USB_DeActivateRemoteWakeup : de-active remote wakeup signalling
2574
  * @brief  USB_DeActivateRemoteWakeup de-active remote wakeup signalling
2213
  * @param  USBx : Selected device
2575
  * @param  USBx Selected device
2214
  * @retval HAL status
2576
  * @retval HAL status
2215
  */
2577
  */
2216
HAL_StatusTypeDef USB_DeActivateRemoteWakeup(USB_TypeDef *USBx)
2578
HAL_StatusTypeDef USB_DeActivateRemoteWakeup(USB_TypeDef *USBx)
2217
{
2579
{
2218
  USBx->CNTR &= ~(USB_CNTR_RESUME);
2580
  USBx->CNTR &= (uint16_t)(~USB_CNTR_RESUME);
-
 
2581
 
2219
  return HAL_OK;
2582
  return HAL_OK;
2220
}
2583
}
2221
 
2584
 
2222
/**
2585
/**
2223
  * @brief  Copy a buffer from user memory area to packet memory area (PMA)
2586
  * @brief Copy a buffer from user memory area to packet memory area (PMA)
2224
  * @param  USBx : pointer to USB register.
2587
  * @param   USBx USB peripheral instance register address.
2225
  * @param  pbUsrBuf : pointer to user memory area.
2588
  * @param   pbUsrBuf pointer to user memory area.
2226
  * @param  wPMABufAddr : address into PMA.
2589
  * @param   wPMABufAddr address into PMA.
2227
  * @param  wNBytes : number of bytes to be copied.
2590
  * @param   wNBytes no. of bytes to be copied.
2228
  * @retval None
2591
  * @retval None
2229
  */
2592
  */
2230
void USB_WritePMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
2593
void USB_WritePMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
2231
{
2594
{
2232
  uint32_t nbytes = (wNBytes + 1) >> 1;   /* nbytes = (wNBytes + 1) / 2 */
2595
  uint32_t n = ((uint32_t)wNBytes + 1U) >> 1;
-
 
2596
  uint32_t BaseAddr = (uint32_t)USBx;
2233
  uint32_t index = 0, temp1 = 0, temp2 = 0;
2597
  uint32_t i, temp1, temp2;
2234
  uint16_t *pdwVal = NULL;
2598
  __IO uint16_t *pdwVal;
-
 
2599
  uint8_t *pBuf = pbUsrBuf;
2235
 
2600
 
2236
  pdwVal = (uint16_t *)(wPMABufAddr * 2 + (uint32_t)USBx + 0x400);
2601
  pdwVal = (__IO uint16_t *)(BaseAddr + 0x400U + ((uint32_t)wPMABufAddr * PMA_ACCESS));
-
 
2602
 
2237
  for (index = nbytes; index != 0; index--)
2603
  for (i = n; i != 0U; i--)
2238
  {
2604
  {
2239
    temp1 = (uint16_t) * pbUsrBuf;
2605
    temp1 = *pBuf;
2240
    pbUsrBuf++;
2606
    pBuf++;
2241
    temp2 = temp1 | (uint16_t) * pbUsrBuf << 8;
2607
    temp2 = temp1 | ((uint16_t)((uint16_t) *pBuf << 8));
2242
    *pdwVal++ = temp2;
2608
    *pdwVal = (uint16_t)temp2;
-
 
2609
    pdwVal++;
-
 
2610
 
-
 
2611
#if PMA_ACCESS > 1U
2243
    pdwVal++;
2612
    pdwVal++;
-
 
2613
#endif
-
 
2614
 
2244
    pbUsrBuf++;
2615
    pBuf++;
2245
  }
2616
  }
2246
}
2617
}
2247
 
2618
 
2248
/**
2619
/**
2249
  * @brief  Copy a buffer from user memory area to packet memory area (PMA)
2620
  * @brief Copy data from packet memory area (PMA) to user memory buffer
2250
  * @param  USBx : pointer to USB register.
2621
  * @param   USBx USB peripheral instance register address.
2251
* @param  pbUsrBuf : pointer to user memory area.
2622
  * @param   pbUsrBuf pointer to user memory area.
2252
  * @param  wPMABufAddr : address into PMA.
2623
  * @param   wPMABufAddr address into PMA.
2253
  * @param  wNBytes : number of bytes to be copied.
2624
  * @param   wNBytes no. of bytes to be copied.
2254
  * @retval None
2625
  * @retval None
2255
  */
2626
  */
2256
void USB_ReadPMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
2627
void USB_ReadPMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
2257
{
2628
{
2258
  uint32_t nbytes = (wNBytes + 1) >> 1;/* /2*/
2629
  uint32_t n = (uint32_t)wNBytes >> 1;
2259
  uint32_t index = 0;
2630
  uint32_t BaseAddr = (uint32_t)USBx;
2260
  uint32_t *pdwVal = NULL;
2631
  uint32_t i, temp;
2261
 
-
 
2262
  pdwVal = (uint32_t *)(wPMABufAddr * 2 + (uint32_t)USBx + 0x400);
-
 
2263
  for (index = nbytes; index != 0; index--)
-
 
2264
  {
-
 
2265
    *(uint16_t*)pbUsrBuf++ = *pdwVal++;
2632
  __IO uint16_t *pdwVal;
2266
    pbUsrBuf++;
2633
  uint8_t *pBuf = pbUsrBuf;
2267
  }
-
 
2268
}
-
 
2269
 
-
 
2270
#endif /* USB */
-
 
2271
 
2634
 
2272
/**
-
 
2273
  * @}
-
 
2274
  */
-
 
2275
/**
-
 
2276
  * @}
-
 
2277
  */
-
 
-
 
2635
  pdwVal = (__IO uint16_t *)(BaseAddr + 0x400U + ((uint32_t)wPMABufAddr * PMA_ACCESS));
2278
 
2636
 
2279
#if defined (USB_OTG_FS)
-
 
2280
/** @addtogroup USB_LL_Private_Functions
-
 
2281
  * @{
-
 
2282
  */
-
 
2283
/**
-
 
2284
  * @brief  Reset the USB Core (needed after USB clock settings change)
-
 
2285
  * @param  USBx : Selected device
-
 
2286
  * @retval HAL status
-
 
2287
  */
-
 
2288
static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx)
-
 
2289
{
-
 
2290
  uint32_t count = 0;
2637
  for (i = n; i != 0U; i--)
2291
 
-
 
2292
  /* Wait for AHB master IDLE state. */
-
 
2293
  do
-
 
2294
  {
2638
  {
2295
    if (++count > 200000)
2639
    temp = *(__IO uint16_t *)pdwVal;
-
 
2640
    pdwVal++;
-
 
2641
    *pBuf = (uint8_t)((temp >> 0) & 0xFFU);
2296
    {
2642
    pBuf++;
2297
      return HAL_TIMEOUT;
2643
    *pBuf = (uint8_t)((temp >> 8) & 0xFFU);
2298
    }
2644
    pBuf++;
-
 
2645
 
-
 
2646
#if PMA_ACCESS > 1U
-
 
2647
    pdwVal++;
-
 
2648
#endif
2299
  }
2649
  }
2300
  while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0);
-
 
2301
 
2650
 
2302
  /* Core Soft Reset */
2651
  if ((wNBytes % 2U) != 0U)
2303
  count = 0;
-
 
2304
  USBx->GRSTCTL |= USB_OTG_GRSTCTL_CSRST;
-
 
2305
 
-
 
2306
  do
-
 
2307
  {
2652
  {
2308
    if (++count > 200000)
2653
    temp = *pdwVal;
2309
    {
-
 
2310
      return HAL_TIMEOUT;
2654
    *pBuf = (uint8_t)((temp >> 0) & 0xFFU);
2311
    }
-
 
2312
  }
2655
  }
2313
  while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_CSRST) == USB_OTG_GRSTCTL_CSRST);
-
 
2314
 
-
 
2315
  return HAL_OK;
-
 
2316
}
2656
}
-
 
2657
#endif /* defined (USB) */
-
 
2658
 
2317
/**
2659
/**
2318
  * @}
2660
  * @}
2319
  */
2661
  */
2320
#endif /* USB_OTG_FS */
-
 
2321
 
-
 
2322
#endif /* STM32F102x6 || STM32F102xB || */
-
 
2323
       /* STM32F103x6 || STM32F103xB || */
-
 
2324
       /* STM32F103xE || STM32F103xG || */
-
 
2325
       /* STM32F105xC || STM32F107xC    */
-
 
2326
 
-
 
2327
#endif /* defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED) */
-
 
2328
 
2662
 
2329
/**
2663
/**
2330
  * @}
2664
  * @}
2331
  */
2665
  */
-
 
2666
#endif /* defined (USB) || defined (USB_OTG_FS) */
-
 
2667
#endif /* defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED) */
2332
 
2668
 
2333
/**
2669
/**
2334
  * @}
2670
  * @}
2335
  */
2671
  */
-
 
2672
 
2336
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
2673
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/