1 /**
2   ******************************************************************************
3   * @file    stm32f7xx_ll_usb.h
4   * @author  MCD Application Team
5   * @version V1.0.1
6   * @date    25-June-2015
7   * @brief   Header file of USB Core HAL module.
8   ******************************************************************************
9   * @attention
10   *
11   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
12   *
13   * Redistribution and use in source and binary forms, with or without modification,
14   * are permitted provided that the following conditions are met:
15   *   1. Redistributions of source code must retain the above copyright notice,
16   *      this list of conditions and the following disclaimer.
17   *   2. Redistributions in binary form must reproduce the above copyright notice,
18   *      this list of conditions and the following disclaimer in the documentation
19   *      and/or other materials provided with the distribution.
20   *   3. Neither the name of STMicroelectronics nor the names of its contributors
21   *      may be used to endorse or promote products derived from this software
22   *      without specific prior written permission.
23   *
24   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
28   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34   *
35   ******************************************************************************
36   */
37 
38 /* Define to prevent recursive inclusion -------------------------------------*/
39 #ifndef __STM32F7xx_LL_USB_H
40 #define __STM32F7xx_LL_USB_H
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /* Includes ------------------------------------------------------------------*/
47 #include "stm32f7xx_hal_def.h"
48 
49 /** @addtogroup STM32F7xx_HAL
50   * @{
51   */
52 
53 /** @addtogroup USB_Core
54   * @{
55   */
56 
57 /* Exported types ------------------------------------------------------------*/
58 
59 /**
60   * @brief  USB Mode definition
61   */
62 typedef enum {
63     USB_OTG_DEVICE_MODE  = 0,
64     USB_OTG_HOST_MODE    = 1,
65     USB_OTG_DRD_MODE     = 2
66 
67 } USB_OTG_ModeTypeDef;
68 
69 /**
70   * @brief  URB States definition
71   */
72 typedef enum {
73     URB_IDLE = 0,
74     URB_DONE,
75     URB_NOTREADY,
76     URB_NYET,
77     URB_ERROR,
78     URB_STALL
79 
80 } USB_OTG_URBStateTypeDef;
81 
82 /**
83   * @brief  Host channel States  definition
84   */
85 typedef enum {
86     HC_IDLE = 0,
87     HC_XFRC,
88     HC_HALTED,
89     HC_NAK,
90     HC_NYET,
91     HC_STALL,
92     HC_XACTERR,
93     HC_BBLERR,
94     HC_DATATGLERR
95 
96 } USB_OTG_HCStateTypeDef;
97 
98 /**
99   * @brief  PCD Initialization Structure definition
100   */
101 typedef struct {
102     uint32_t dev_endpoints;        /*!< Device Endpoints number.
103                                       This parameter depends on the used USB core.
104                                       This parameter must be a number between Min_Data = 1 and Max_Data = 15 */
105 
106     uint32_t Host_channels;        /*!< Host Channels number.
107                                       This parameter Depends on the used USB core.
108                                       This parameter must be a number between Min_Data = 1 and Max_Data = 15 */
109 
110     uint32_t speed;                /*!< USB Core speed.
111                                       This parameter can be any value of @ref USB_Core_Speed_                */
112 
113     uint32_t dma_enable;           /*!< Enable or disable of the USB embedded DMA.                             */
114 
115     uint32_t ep0_mps;              /*!< Set the Endpoint 0 Max Packet size.
116                                       This parameter can be any value of @ref USB_EP0_MPS_                   */
117 
118     uint32_t phy_itface;           /*!< Select the used PHY interface.
119                                       This parameter can be any value of @ref USB_Core_PHY_                  */
120 
121     uint32_t Sof_enable;           /*!< Enable or disable the output of the SOF signal.                        */
122 
123     uint32_t low_power_enable;     /*!< Enable or disable the low power mode.                                  */
124 
125     uint32_t lpm_enable;           /*!< Enable or disable Link Power Management.                               */
126 
127     uint32_t vbus_sensing_enable;  /*!< Enable or disable the VBUS Sensing feature.                            */
128 
129     uint32_t use_dedicated_ep1;    /*!< Enable or disable the use of the dedicated EP1 interrupt.              */
130 
131     uint32_t use_external_vbus;    /*!< Enable or disable the use of the external VBUS.                        */
132 
133 } USB_OTG_CfgTypeDef;
134 
135 typedef struct {
136     uint8_t   num;            /*!< Endpoint number
137                                 This parameter must be a number between Min_Data = 1 and Max_Data = 15    */
138 
139     uint8_t   is_in;          /*!< Endpoint direction
140                                 This parameter must be a number between Min_Data = 0 and Max_Data = 1     */
141 
142     uint8_t   is_stall;       /*!< Endpoint stall condition
143                                 This parameter must be a number between Min_Data = 0 and Max_Data = 1     */
144 
145     uint8_t   type;           /*!< Endpoint type
146                                  This parameter can be any value of @ref USB_EP_Type_                     */
147 
148     uint8_t   data_pid_start; /*!< Initial data PID
149                                 This parameter must be a number between Min_Data = 0 and Max_Data = 1     */
150 
151     uint8_t   even_odd_frame; /*!< IFrame parity
152                                  This parameter must be a number between Min_Data = 0 and Max_Data = 1    */
153 
154     uint16_t  tx_fifo_num;    /*!< Transmission FIFO number
155                                  This parameter must be a number between Min_Data = 1 and Max_Data = 15   */
156 
157     uint32_t  maxpacket;      /*!< Endpoint Max packet size
158                                  This parameter must be a number between Min_Data = 0 and Max_Data = 64KB */
159 
160     uint8_t   *xfer_buff;     /*!< Pointer to transfer buffer                                               */
161 
162     uint32_t  dma_addr;       /*!< 32 bits aligned transfer buffer address                                  */
163 
164     uint32_t  xfer_len;       /*!< Current transfer length                                                  */
165 
166     uint32_t  xfer_count;     /*!< Partial transfer length in case of multi packet transfer                 */
167 
168 } USB_OTG_EPTypeDef;
169 
170 typedef struct {
171     uint8_t   dev_addr ;     /*!< USB device address.
172                                 This parameter must be a number between Min_Data = 1 and Max_Data = 255    */
173 
174     uint8_t   ch_num;        /*!< Host channel number.
175                                 This parameter must be a number between Min_Data = 1 and Max_Data = 15     */
176 
177     uint8_t   ep_num;        /*!< Endpoint number.
178                                 This parameter must be a number between Min_Data = 1 and Max_Data = 15     */
179 
180     uint8_t   ep_is_in;      /*!< Endpoint direction
181                                 This parameter must be a number between Min_Data = 0 and Max_Data = 1      */
182 
183     uint8_t   speed;         /*!< USB Host speed.
184                                 This parameter can be any value of @ref USB_Core_Speed_                    */
185 
186     uint8_t   do_ping;       /*!< Enable or disable the use of the PING protocol for HS mode.                */
187 
188     uint8_t   process_ping;  /*!< Execute the PING protocol for HS mode.                                     */
189 
190     uint8_t   ep_type;       /*!< Endpoint Type.
191                                 This parameter can be any value of @ref USB_EP_Type_                       */
192 
193     uint16_t  max_packet;    /*!< Endpoint Max packet size.
194                                 This parameter must be a number between Min_Data = 0 and Max_Data = 64KB   */
195 
196     uint8_t   data_pid;      /*!< Initial data PID.
197                                 This parameter must be a number between Min_Data = 0 and Max_Data = 1      */
198 
199     uint8_t   *xfer_buff;    /*!< Pointer to transfer buffer.                                                */
200 
201     uint32_t  xfer_len;      /*!< Current transfer length.                                                   */
202 
203     uint32_t  xfer_count;    /*!< Partial transfer length in case of multi packet transfer.                  */
204 
205     uint8_t   toggle_in;     /*!< IN transfer current toggle flag.
206                                 This parameter must be a number between Min_Data = 0 and Max_Data = 1      */
207 
208     uint8_t   toggle_out;    /*!< OUT transfer current toggle flag
209                                 This parameter must be a number between Min_Data = 0 and Max_Data = 1      */
210 
211     uint32_t  dma_addr;      /*!< 32 bits aligned transfer buffer address.                                   */
212 
213     uint32_t  ErrCnt;        /*!< Host channel error count.*/
214 
215     USB_OTG_URBStateTypeDef  urb_state;  /*!< URB state.
216                                            This parameter can be any value of @ref USB_OTG_URBStateTypeDef */
217 
218     USB_OTG_HCStateTypeDef   state;     /*!< Host Channel state.
219                                            This parameter can be any value of @ref USB_OTG_HCStateTypeDef  */
220 
221 } USB_OTG_HCTypeDef;
222 
223 /* Exported constants --------------------------------------------------------*/
224 
225 /** @defgroup PCD_Exported_Constants PCD Exported Constants
226   * @{
227   */
228 
229 /** @defgroup USB_Core_Mode_ USB Core Mode
230   * @{
231   */
232 #define USB_OTG_MODE_DEVICE                    0
233 #define USB_OTG_MODE_HOST                      1
234 #define USB_OTG_MODE_DRD                       2
235 /**
236   * @}
237   */
238 
239 /** @defgroup USB_Core_Speed_   USB Core Speed
240   * @{
241   */
242 #define USB_OTG_SPEED_HIGH                     0
243 #define USB_OTG_SPEED_HIGH_IN_FULL             1
244 #define USB_OTG_SPEED_LOW                      2
245 #define USB_OTG_SPEED_FULL                     3
246 /**
247   * @}
248   */
249 
250 /** @defgroup USB_Core_PHY_   USB Core PHY
251   * @{
252   */
253 #define USB_OTG_ULPI_PHY                       1
254 #define USB_OTG_EMBEDDED_PHY                   2
255 /**
256   * @}
257   */
258 
259 /** @defgroup USB_Core_MPS_   USB Core MPS
260   * @{
261   */
262 #define USB_OTG_HS_MAX_PACKET_SIZE           512
263 #define USB_OTG_FS_MAX_PACKET_SIZE           64
264 #define USB_OTG_MAX_EP0_SIZE                 64
265 /**
266   * @}
267   */
268 
269 /** @defgroup USB_Core_Phy_Frequency_   USB Core Phy Frequency
270   * @{
271   */
272 #define DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ     (0 << 1)
273 #define DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ     (1 << 1)
274 #define DSTS_ENUMSPD_LS_PHY_6MHZ               (2 << 1)
275 #define DSTS_ENUMSPD_FS_PHY_48MHZ              (3 << 1)
276 /**
277   * @}
278   */
279 
280 /** @defgroup USB_CORE_Frame_Interval_   USB CORE Frame Interval
281   * @{
282   */
283 #define DCFG_FRAME_INTERVAL_80                 0
284 #define DCFG_FRAME_INTERVAL_85                 1
285 #define DCFG_FRAME_INTERVAL_90                 2
286 #define DCFG_FRAME_INTERVAL_95                 3
287 /**
288   * @}
289   */
290 
291 /** @defgroup USB_EP0_MPS_  USB EP0 MPS
292   * @{
293   */
294 #define DEP0CTL_MPS_64                         0
295 #define DEP0CTL_MPS_32                         1
296 #define DEP0CTL_MPS_16                         2
297 #define DEP0CTL_MPS_8                          3
298 /**
299   * @}
300   */
301 
302 /** @defgroup USB_EP_Speed_  USB EP Speed
303   * @{
304   */
305 #define EP_SPEED_LOW                           0
306 #define EP_SPEED_FULL                          1
307 #define EP_SPEED_HIGH                          2
308 /**
309   * @}
310   */
311 
312 /** @defgroup USB_EP_Type_  USB EP Type
313   * @{
314   */
315 #define EP_TYPE_CTRL                           0
316 #define EP_TYPE_ISOC                           1
317 #define EP_TYPE_BULK                           2
318 #define EP_TYPE_INTR                           3
319 #define EP_TYPE_MSK                            3
320 /**
321   * @}
322   */
323 
324 /** @defgroup USB_STS_Defines_   USB STS Defines
325   * @{
326   */
327 #define STS_GOUT_NAK                           1
328 #define STS_DATA_UPDT                          2
329 #define STS_XFER_COMP                          3
330 #define STS_SETUP_COMP                         4
331 #define STS_SETUP_UPDT                         6
332 /**
333   * @}
334   */
335 
336 /** @defgroup HCFG_SPEED_Defines_   HCFG SPEED Defines
337   * @{
338   */
339 #define HCFG_30_60_MHZ                         0
340 #define HCFG_48_MHZ                            1
341 #define HCFG_6_MHZ                             2
342 /**
343   * @}
344   */
345 
346 /** @defgroup HPRT0_PRTSPD_SPEED_Defines_  HPRT0 PRTSPD SPEED Defines
347   * @{
348   */
349 #define HPRT0_PRTSPD_HIGH_SPEED                0
350 #define HPRT0_PRTSPD_FULL_SPEED                1
351 #define HPRT0_PRTSPD_LOW_SPEED                 2
352 /**
353   * @}
354   */
355 
356 #define HCCHAR_CTRL                            0
357 #define HCCHAR_ISOC                            1
358 #define HCCHAR_BULK                            2
359 #define HCCHAR_INTR                            3
360 
361 #define HC_PID_DATA0                           0
362 #define HC_PID_DATA2                           1
363 #define HC_PID_DATA1                           2
364 #define HC_PID_SETUP                           3
365 
366 #define GRXSTS_PKTSTS_IN                       2
367 #define GRXSTS_PKTSTS_IN_XFER_COMP             3
368 #define GRXSTS_PKTSTS_DATA_TOGGLE_ERR          5
369 #define GRXSTS_PKTSTS_CH_HALTED                7
370 
371 #define USBx_PCGCCTL    *(__IO uint32_t *)((uint32_t)USBx + USB_OTG_PCGCCTL_BASE)
372 #define USBx_HPRT0      *(__IO uint32_t *)((uint32_t)USBx + USB_OTG_HOST_PORT_BASE)
373 
374 #define USBx_DEVICE     ((USB_OTG_DeviceTypeDef *)((uint32_t )USBx + USB_OTG_DEVICE_BASE))
375 #define USBx_INEP(i)    ((USB_OTG_INEndpointTypeDef *)((uint32_t)USBx + USB_OTG_IN_ENDPOINT_BASE + (i)*USB_OTG_EP_REG_SIZE))
376 #define USBx_OUTEP(i)   ((USB_OTG_OUTEndpointTypeDef *)((uint32_t)USBx + USB_OTG_OUT_ENDPOINT_BASE + (i)*USB_OTG_EP_REG_SIZE))
377 #define USBx_DFIFO(i)   *(__IO uint32_t *)((uint32_t)USBx + USB_OTG_FIFO_BASE + (i) * USB_OTG_FIFO_SIZE)
378 
379 #define USBx_HOST       ((USB_OTG_HostTypeDef *)((uint32_t )USBx + USB_OTG_HOST_BASE))
380 #define USBx_HC(i)      ((USB_OTG_HostChannelTypeDef *)((uint32_t)USBx + USB_OTG_HOST_CHANNEL_BASE + (i)*USB_OTG_HOST_CHANNEL_SIZE))
381 /**
382   * @}
383   */
384 /* Exported macro ------------------------------------------------------------*/
385 #define USB_MASK_INTERRUPT(__INSTANCE__, __INTERRUPT__)     ((__INSTANCE__)->GINTMSK &= ~(__INTERRUPT__))
386 #define USB_UNMASK_INTERRUPT(__INSTANCE__, __INTERRUPT__)   ((__INSTANCE__)->GINTMSK |= (__INTERRUPT__))
387 
388 #define CLEAR_IN_EP_INTR(__EPNUM__, __INTERRUPT__)          (USBx_INEP(__EPNUM__)->DIEPINT = (__INTERRUPT__))
389 #define CLEAR_OUT_EP_INTR(__EPNUM__, __INTERRUPT__)         (USBx_OUTEP(__EPNUM__)->DOEPINT = (__INTERRUPT__))
390 
391 /* Exported functions --------------------------------------------------------*/
392 HAL_StatusTypeDef USB_CoreInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef Init);
393 HAL_StatusTypeDef USB_DevInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef Init);
394 HAL_StatusTypeDef USB_EnableGlobalInt(USB_OTG_GlobalTypeDef *USBx);
395 HAL_StatusTypeDef USB_DisableGlobalInt(USB_OTG_GlobalTypeDef *USBx);
396 HAL_StatusTypeDef USB_SetCurrentMode(USB_OTG_GlobalTypeDef *USBx , USB_OTG_ModeTypeDef mode);
397 HAL_StatusTypeDef USB_SetDevSpeed(USB_OTG_GlobalTypeDef *USBx , uint8_t speed);
398 HAL_StatusTypeDef USB_FlushRxFifo (USB_OTG_GlobalTypeDef *USBx);
399 HAL_StatusTypeDef USB_FlushTxFifo (USB_OTG_GlobalTypeDef *USBx, uint32_t num );
400 HAL_StatusTypeDef USB_ActivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep);
401 HAL_StatusTypeDef USB_DeactivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep);
402 HAL_StatusTypeDef USB_ActivateDedicatedEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep);
403 HAL_StatusTypeDef USB_DeactivateDedicatedEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep);
404 HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDef *ep, uint8_t dma);
405 HAL_StatusTypeDef USB_EP0StartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDef *ep, uint8_t dma);
406 HAL_StatusTypeDef USB_WritePacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *src, uint8_t ch_ep_num, uint16_t len, uint8_t dma);
407 void *            USB_ReadPacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *dest, uint16_t len);
408 HAL_StatusTypeDef USB_EPSetStall(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDef *ep);
409 HAL_StatusTypeDef USB_EPClearStall(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDef *ep);
410 HAL_StatusTypeDef USB_SetDevAddress (USB_OTG_GlobalTypeDef *USBx, uint8_t address);
411 HAL_StatusTypeDef USB_DevConnect (USB_OTG_GlobalTypeDef *USBx);
412 HAL_StatusTypeDef USB_DevDisconnect (USB_OTG_GlobalTypeDef *USBx);
413 HAL_StatusTypeDef USB_StopDevice(USB_OTG_GlobalTypeDef *USBx);
414 HAL_StatusTypeDef USB_ActivateSetup (USB_OTG_GlobalTypeDef *USBx);
415 HAL_StatusTypeDef USB_EP0_OutStart(USB_OTG_GlobalTypeDef *USBx, uint8_t dma, uint8_t *psetup);
416 uint8_t           USB_GetDevSpeed(USB_OTG_GlobalTypeDef *USBx);
417 uint32_t          USB_GetMode(USB_OTG_GlobalTypeDef *USBx);
418 uint32_t          USB_ReadInterrupts (USB_OTG_GlobalTypeDef *USBx);
419 uint32_t          USB_ReadDevAllOutEpInterrupt (USB_OTG_GlobalTypeDef *USBx);
420 uint32_t          USB_ReadDevOutEPInterrupt (USB_OTG_GlobalTypeDef *USBx , uint8_t epnum);
421 uint32_t          USB_ReadDevAllInEpInterrupt (USB_OTG_GlobalTypeDef *USBx);
422 uint32_t          USB_ReadDevInEPInterrupt (USB_OTG_GlobalTypeDef *USBx , uint8_t epnum);
423 void              USB_ClearInterrupts (USB_OTG_GlobalTypeDef *USBx, uint32_t interrupt);
424 
425 HAL_StatusTypeDef USB_HostInit (USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg);
426 HAL_StatusTypeDef USB_InitFSLSPClkSel(USB_OTG_GlobalTypeDef *USBx , uint8_t freq);
427 HAL_StatusTypeDef USB_ResetPort(USB_OTG_GlobalTypeDef *USBx);
428 HAL_StatusTypeDef USB_DriveVbus (USB_OTG_GlobalTypeDef *USBx, uint8_t state);
429 uint32_t          USB_GetHostSpeed (USB_OTG_GlobalTypeDef *USBx);
430 uint32_t          USB_GetCurrentFrame (USB_OTG_GlobalTypeDef *USBx);
431 HAL_StatusTypeDef USB_HC_Init(USB_OTG_GlobalTypeDef *USBx,
432                               uint8_t ch_num,
433                               uint8_t epnum,
434                               uint8_t dev_address,
435                               uint8_t speed,
436                               uint8_t ep_type,
437                               uint16_t mps);
438 HAL_StatusTypeDef USB_HC_StartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_HCTypeDef *hc, uint8_t dma);
439 uint32_t          USB_HC_ReadInterrupt (USB_OTG_GlobalTypeDef *USBx);
440 HAL_StatusTypeDef USB_HC_Halt(USB_OTG_GlobalTypeDef *USBx , uint8_t hc_num);
441 HAL_StatusTypeDef USB_DoPing(USB_OTG_GlobalTypeDef *USBx , uint8_t ch_num);
442 HAL_StatusTypeDef USB_StopHost(USB_OTG_GlobalTypeDef *USBx);
443 
444 /**
445   * @}
446   */
447 
448 /**
449   * @}
450   */
451 
452 #ifdef __cplusplus
453 }
454 #endif
455 
456 
457 #endif /* __STM32F7xx_LL_USB_H */
458 
459 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
460