1 /**
2   ******************************************************************************
3   * @file    stm32g4xx_ll_usb.h
4   * @author  MCD Application Team
5   * @brief   Header file of USB Low Layer HAL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2019 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 
19 /* Define to prevent recursive inclusion -------------------------------------*/
20 #ifndef STM32G4xx_LL_USB_H
21 #define STM32G4xx_LL_USB_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif /* __cplusplus */
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32g4xx_hal_def.h"
29 
30 #if defined (USB)
31 /** @addtogroup STM32G4xx_HAL_Driver
32   * @{
33   */
34 
35 /** @addtogroup USB_LL
36   * @{
37   */
38 
39 /* Exported types ------------------------------------------------------------*/
40 
41 /**
42   * @brief  USB Mode definition
43   */
44 
45 
46 
47 typedef enum
48 {
49   USB_DEVICE_MODE  = 0
50 } USB_ModeTypeDef;
51 
52 /**
53   * @brief  USB Initialization Structure definition
54   */
55 typedef struct
56 {
57   uint32_t dev_endpoints;           /*!< Device Endpoints number.
58                                          This parameter depends on the used USB core.
59                                          This parameter must be a number between Min_Data = 1 and Max_Data = 15 */
60 
61   uint32_t speed;                   /*!< USB Core speed.
62                                          This parameter can be any value of @ref PCD_Speed/HCD_Speed
63                                                                                  (HCD_SPEED_xxx, HCD_SPEED_xxx) */
64 
65   uint32_t ep0_mps;                 /*!< Set the Endpoint 0 Max Packet size.                                    */
66 
67   uint32_t phy_itface;              /*!< Select the used PHY interface.
68                                          This parameter can be any value of @ref PCD_PHY_Module/HCD_PHY_Module  */
69 
70   uint32_t Sof_enable;              /*!< Enable or disable the output of the SOF signal.                        */
71 
72   uint32_t low_power_enable;        /*!< Enable or disable Low Power mode                                       */
73 
74   uint32_t lpm_enable;              /*!< Enable or disable Battery charging.                                    */
75 
76   uint32_t battery_charging_enable; /*!< Enable or disable Battery charging.                                    */
77 } USB_CfgTypeDef;
78 
79 typedef struct
80 {
81   uint8_t   num;             /*!< Endpoint number
82                                   This parameter must be a number between Min_Data = 1 and Max_Data = 15    */
83 
84   uint8_t   is_in;           /*!< Endpoint direction
85                                   This parameter must be a number between Min_Data = 0 and Max_Data = 1     */
86 
87   uint8_t   is_stall;        /*!< Endpoint stall condition
88                                   This parameter must be a number between Min_Data = 0 and Max_Data = 1     */
89 
90   uint8_t   type;            /*!< Endpoint type
91                                   This parameter can be any value of @ref USB_EP_Type                       */
92 
93   uint8_t   data_pid_start;  /*!< Initial data PID
94                                   This parameter must be a number between Min_Data = 0 and Max_Data = 1     */
95 
96   uint16_t  pmaadress;       /*!< PMA Address
97                                   This parameter can be any value between Min_addr = 0 and Max_addr = 1K    */
98 
99   uint16_t  pmaaddr0;        /*!< PMA Address0
100                                   This parameter can be any value between Min_addr = 0 and Max_addr = 1K    */
101 
102   uint16_t  pmaaddr1;        /*!< PMA Address1
103                                   This parameter can be any value between Min_addr = 0 and Max_addr = 1K    */
104 
105   uint8_t   doublebuffer;    /*!< Double buffer enable
106                                   This parameter can be 0 or 1                                              */
107 
108   uint16_t  tx_fifo_num;     /*!< This parameter is not required by USB Device FS peripheral, it is used
109                                   only by USB OTG FS peripheral
110                                   This parameter is added to ensure compatibility across USB peripherals    */
111 
112   uint32_t  maxpacket;       /*!< Endpoint Max packet size
113                                   This parameter must be a number between Min_Data = 0 and Max_Data = 64KB  */
114 
115   uint8_t   *xfer_buff;      /*!< Pointer to transfer buffer                                                */
116 
117   uint32_t  xfer_len;        /*!< Current transfer length                                                   */
118 
119   uint32_t  xfer_count;      /*!< Partial transfer length in case of multi packet transfer                  */
120 
121   uint32_t  xfer_len_db;      /*!< double buffer transfer length used with bulk double buffer in           */
122 
123   uint8_t   xfer_fill_db;     /*!< double buffer Need to Fill new buffer  used with bulk_in                */
124 
125 } USB_EPTypeDef;
126 
127 
128 /* Exported constants --------------------------------------------------------*/
129 
130 /** @defgroup PCD_Exported_Constants PCD Exported Constants
131   * @{
132   */
133 
134 
135 /** @defgroup USB_LL_EP0_MPS USB Low Layer EP0 MPS
136   * @{
137   */
138 #define EP_MPS_64                              0U
139 #define EP_MPS_32                              1U
140 #define EP_MPS_16                              2U
141 #define EP_MPS_8                               3U
142 /**
143   * @}
144   */
145 
146 /** @defgroup USB_LL_EP_Type USB Low Layer EP Type
147   * @{
148   */
149 #define EP_TYPE_CTRL                           0U
150 #define EP_TYPE_ISOC                           1U
151 #define EP_TYPE_BULK                           2U
152 #define EP_TYPE_INTR                           3U
153 #define EP_TYPE_MSK                            3U
154 /**
155   * @}
156   */
157 
158 /** @defgroup USB_LL Device Speed
159   * @{
160   */
161 #define USBD_FS_SPEED                          2U
162 /**
163   * @}
164   */
165 
166 #define BTABLE_ADDRESS                         0x000U
167 #define PMA_ACCESS                             1U
168 
169 #define EP_ADDR_MSK                            0x7U
170 
171 #ifndef USE_USB_DOUBLE_BUFFER
172 #define USE_USB_DOUBLE_BUFFER                  1U
173 #endif /* USE_USB_DOUBLE_BUFFER */
174 /**
175   * @}
176   */
177 
178 /* Exported macro ------------------------------------------------------------*/
179 /**
180   * @}
181   */
182 
183 /* Exported functions --------------------------------------------------------*/
184 /** @addtogroup USB_LL_Exported_Functions USB Low Layer Exported Functions
185   * @{
186   */
187 
188 
189 HAL_StatusTypeDef USB_CoreInit(USB_TypeDef *USBx, USB_CfgTypeDef cfg);
190 HAL_StatusTypeDef USB_DevInit(USB_TypeDef *USBx, USB_CfgTypeDef cfg);
191 HAL_StatusTypeDef USB_EnableGlobalInt(USB_TypeDef *USBx);
192 HAL_StatusTypeDef USB_DisableGlobalInt(USB_TypeDef *USBx);
193 HAL_StatusTypeDef USB_SetCurrentMode(USB_TypeDef *USBx, USB_ModeTypeDef mode);
194 
195 #if defined (HAL_PCD_MODULE_ENABLED)
196 HAL_StatusTypeDef USB_ActivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep);
197 HAL_StatusTypeDef USB_DeactivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep);
198 HAL_StatusTypeDef USB_EPStartXfer(USB_TypeDef *USBx, USB_EPTypeDef *ep);
199 HAL_StatusTypeDef USB_EPSetStall(USB_TypeDef *USBx, USB_EPTypeDef *ep);
200 HAL_StatusTypeDef USB_EPClearStall(USB_TypeDef *USBx, USB_EPTypeDef *ep);
201 #endif /* defined (HAL_PCD_MODULE_ENABLED) */
202 
203 HAL_StatusTypeDef USB_SetDevAddress(USB_TypeDef *USBx, uint8_t address);
204 HAL_StatusTypeDef USB_DevConnect(USB_TypeDef *USBx);
205 HAL_StatusTypeDef USB_DevDisconnect(USB_TypeDef *USBx);
206 HAL_StatusTypeDef USB_StopDevice(USB_TypeDef *USBx);
207 uint32_t          USB_ReadInterrupts(USB_TypeDef *USBx);
208 HAL_StatusTypeDef USB_ActivateRemoteWakeup(USB_TypeDef *USBx);
209 HAL_StatusTypeDef USB_DeActivateRemoteWakeup(USB_TypeDef *USBx);
210 
211 void              USB_WritePMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf,
212                                uint16_t wPMABufAddr, uint16_t wNBytes);
213 
214 void              USB_ReadPMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf,
215                               uint16_t wPMABufAddr, uint16_t wNBytes);
216 
217 /**
218   * @}
219   */
220 
221 /**
222   * @}
223   */
224 
225 /**
226   * @}
227   */
228 
229 /**
230   * @}
231   */
232 #endif /* defined (USB) */
233 
234 #ifdef __cplusplus
235 }
236 #endif /* __cplusplus */
237 
238 
239 #endif /* STM32G4xx_LL_USB_H */
240