1 /**
2   ******************************************************************************
3   * @file    usb_core.h
4   * @author  MCD Application Team
5   * @version V2.0.0
6   * @date    22-July-2011
7   * @brief   Header of the Core Layer
8   ******************************************************************************
9   * @attention
10   *
11   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17   *
18   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
19   ******************************************************************************
20   */
21 
22 /* Define to prevent recursive inclusion -------------------------------------*/
23 #ifndef __USB_CORE_H__
24 #define __USB_CORE_H__
25 
26 /* Includes ------------------------------------------------------------------*/
27 #include "usb_conf.h"
28 #include "usb_regs.h"
29 #include "usb_defines.h"
30 
31 
32 /** @addtogroup USB_OTG_DRIVER
33   * @{
34   */
35 
36 /** @defgroup USB_CORE
37   * @brief usb otg driver core layer
38   * @{
39   */
40 
41 
42 /** @defgroup USB_CORE_Exported_Defines
43   * @{
44   */
45 
46 #define USB_OTG_EP0_IDLE                          0
47 #define USB_OTG_EP0_SETUP                         1
48 #define USB_OTG_EP0_DATA_IN                       2
49 #define USB_OTG_EP0_DATA_OUT                      3
50 #define USB_OTG_EP0_STATUS_IN                     4
51 #define USB_OTG_EP0_STATUS_OUT                    5
52 #define USB_OTG_EP0_STALL                         6
53 
54 #define USB_OTG_EP_TX_DIS       0x0000
55 #define USB_OTG_EP_TX_STALL     0x0010
56 #define USB_OTG_EP_TX_NAK       0x0020
57 #define USB_OTG_EP_TX_VALID     0x0030
58 
59 #define USB_OTG_EP_RX_DIS       0x0000
60 #define USB_OTG_EP_RX_STALL     0x1000
61 #define USB_OTG_EP_RX_NAK       0x2000
62 #define USB_OTG_EP_RX_VALID     0x3000
63 /**
64   * @}
65   */
66 #define   MAX_DATA_LENGTH                        0xFF
67 
68 /** @defgroup USB_CORE_Exported_Types
69   * @{
70   */
71 
72 
73 typedef enum {
74   USB_OTG_OK = 0,
75   USB_OTG_FAIL
76 }USB_OTG_STS;
77 
78 typedef enum {
79   HC_IDLE = 0,
80   HC_XFRC,
81   HC_HALTED,
82   HC_NAK,
83   HC_NYET,
84   HC_STALL,
85   HC_XACTERR,
86   HC_BBLERR,
87   HC_DATATGLERR,
88 }HC_STATUS;
89 
90 typedef enum {
91   URB_IDLE = 0,
92   URB_DONE,
93   URB_NOTREADY,
94   URB_ERROR,
95   URB_STALL
96 }URB_STATE;
97 
98 typedef enum {
99   CTRL_START = 0,
100   CTRL_XFRC,
101   CTRL_HALTED,
102   CTRL_NAK,
103   CTRL_STALL,
104   CTRL_XACTERR,
105   CTRL_BBLERR,
106   CTRL_DATATGLERR,
107   CTRL_FAIL
108 }CTRL_STATUS;
109 
110 
111 typedef struct USB_OTG_hc
112 {
113   uint8_t       dev_addr ;
114   uint8_t       ep_num;
115   uint8_t       ep_is_in;
116   uint8_t       speed;
117   uint8_t       do_ping;
118   uint8_t       ep_type;
119   uint16_t      max_packet;
120   uint8_t       data_pid;
121   uint8_t       *xfer_buff;
122   uint32_t      xfer_len;
123   uint32_t      xfer_count;
124   uint8_t       toggle_in;
125   uint8_t       toggle_out;
126   uint32_t       dma_addr;
127 }
128 USB_OTG_HC , *PUSB_OTG_HC;
129 
130 typedef struct USB_OTG_ep
131 {
132   uint8_t        num;
133   uint8_t        is_in;
134   uint8_t        is_stall;
135   uint8_t        type;
136   uint8_t        data_pid_start;
137   uint8_t        even_odd_frame;
138   uint16_t       tx_fifo_num;
139   uint32_t       maxpacket;
140   /* transaction level variables*/
141   uint8_t        *xfer_buff;
142   uint32_t       dma_addr;
143   uint32_t       xfer_len;
144   uint32_t       xfer_count;
145   /* Transfer level variables*/
146   uint32_t       rem_data_len;
147   uint32_t       total_data_len;
148   uint32_t       ctl_data_len;
149 
150 }
151 
152 USB_OTG_EP , *PUSB_OTG_EP;
153 
154 
155 
156 typedef struct USB_OTG_core_cfg
157 {
158   uint8_t       host_channels;
159   uint8_t       dev_endpoints;
160   uint8_t       speed;
161   uint8_t       dma_enable;
162   uint16_t      mps;
163   uint16_t      TotalFifoSize;
164   uint8_t       phy_itface;
165   uint8_t       Sof_output;
166   uint8_t       low_power;
167   uint8_t       coreID;
168 
169 }
170 USB_OTG_CORE_CFGS, *PUSB_OTG_CORE_CFGS;
171 
172 
173 
174 typedef  struct  usb_setup_req {
175 
176     uint8_t   bmRequest;
177     uint8_t   bRequest;
178     uint16_t  wValue;
179     uint16_t  wIndex;
180     uint16_t  wLength;
181 } USB_SETUP_REQ;
182 
183 typedef struct _Device_TypeDef
184 {
185   uint8_t  *(*GetDeviceDescriptor)( uint8_t speed , uint16_t *length);
186   uint8_t  *(*GetLangIDStrDescriptor)( uint8_t speed , uint16_t *length);
187   uint8_t  *(*GetManufacturerStrDescriptor)( uint8_t speed , uint16_t *length);
188   uint8_t  *(*GetProductStrDescriptor)( uint8_t speed , uint16_t *length);
189   uint8_t  *(*GetSerialStrDescriptor)( uint8_t speed , uint16_t *length);
190   uint8_t  *(*GetConfigurationStrDescriptor)( uint8_t speed , uint16_t *length);
191   uint8_t  *(*GetInterfaceStrDescriptor)( uint8_t speed , uint16_t *length);
192 } USBD_DEVICE, *pUSBD_DEVICE;
193 
194 typedef struct USB_OTG_hPort
195 {
196   void (*Disconnect) (void *phost);
197   void (*Connect) (void *phost);
198   uint8_t ConnStatus;
199   uint8_t DisconnStatus;
200   uint8_t ConnHandled;
201   uint8_t DisconnHandled;
202 } USB_OTG_hPort_TypeDef;
203 
204 typedef struct _Device_cb
205 {
206   uint8_t  (*Init)         (void *pdev , uint8_t cfgidx);
207   uint8_t  (*DeInit)       (void *pdev , uint8_t cfgidx);
208  /* Control Endpoints*/
209   uint8_t  (*Setup)        (void *pdev , USB_SETUP_REQ  *req);
210   uint8_t  (*EP0_TxSent)   (void *pdev );
211   uint8_t  (*EP0_RxReady)  (void *pdev );
212   /* Class Specific Endpoints*/
213   uint8_t  (*DataIn)       (void *pdev , uint8_t epnum);
214   uint8_t  (*DataOut)      (void *pdev , uint8_t epnum);
215   uint8_t  (*SOF)          (void *pdev);
216   uint8_t  (*IsoINIncomplete)  (void *pdev);
217   uint8_t  (*IsoOUTIncomplete)  (void *pdev);
218 
219   uint8_t  *(*GetConfigDescriptor)( uint8_t speed , uint16_t *length);
220 #ifdef USB_OTG_HS_CORE
221   uint8_t  *(*GetOtherConfigDescriptor)( uint8_t speed , uint16_t *length);
222 #endif
223 
224 #ifdef USB_SUPPORT_USER_STRING_DESC
225   uint8_t  *(*GetUsrStrDescriptor)( uint8_t speed ,uint8_t index,  uint16_t *length);
226 #endif
227 
228 } USBD_Class_cb_TypeDef;
229 
230 
231 
232 typedef struct _USBD_USR_PROP
233 {
234   void (*Init)(void);
235   void (*DeviceReset)(uint8_t speed);
236   void (*DeviceConfigured)(void);
237   void (*DeviceSuspended)(void);
238   void (*DeviceResumed)(void);
239 
240   void (*DeviceConnected)(void);
241   void (*DeviceDisconnected)(void);
242 
243 }
244 USBD_Usr_cb_TypeDef;
245 
246 typedef struct _DCD
247 {
248   uint8_t        device_config;
249   uint8_t        device_state;
250   uint8_t        device_status;
251   uint8_t        device_address;
252   uint32_t       DevRemoteWakeup;
253   USB_OTG_EP     in_ep   [USB_OTG_MAX_TX_FIFOS];
254   USB_OTG_EP     out_ep  [USB_OTG_MAX_TX_FIFOS];
255   uint8_t        setup_packet [8*3];
256   USBD_Class_cb_TypeDef         *class_cb;
257   USBD_Usr_cb_TypeDef           *usr_cb;
258   USBD_DEVICE                   *usr_device;
259   uint8_t        *pConfig_descriptor;
260  }
261 DCD_DEV , *DCD_PDEV;
262 
263 
264 typedef struct _HCD
265 {
266   uint8_t                  Rx_Buffer [MAX_DATA_LENGTH];
267   __IO uint32_t            ConnSts;
268   __IO uint32_t            ErrCnt[USB_OTG_MAX_TX_FIFOS];
269   __IO uint32_t            XferCnt[USB_OTG_MAX_TX_FIFOS];
270   __IO HC_STATUS           HC_Status[USB_OTG_MAX_TX_FIFOS];
271   __IO URB_STATE           URB_State[USB_OTG_MAX_TX_FIFOS];
272   USB_OTG_HC               hc [USB_OTG_MAX_TX_FIFOS];
273   uint16_t                 channel [USB_OTG_MAX_TX_FIFOS];
274   USB_OTG_hPort_TypeDef    *port_cb;
275 }
276 HCD_DEV , *USB_OTG_USBH_PDEV;
277 
278 
279 typedef struct _OTG
280 {
281   uint8_t    OTG_State;
282   uint8_t    OTG_PrevState;
283   uint8_t    OTG_Mode;
284 }
285 OTG_DEV , *USB_OTG_USBO_PDEV;
286 
287 typedef struct USB_OTG_handle
288 {
289   USB_OTG_CORE_CFGS    cfg;
290   USB_OTG_CORE_REGS    regs;
291 #ifdef USE_DEVICE_MODE
292   DCD_DEV     dev;
293 #endif
294 #ifdef USE_HOST_MODE
295   HCD_DEV     host;
296 #endif
297 #ifdef USE_OTG_MODE
298   OTG_DEV     otg;
299 #endif
300 }
301 USB_OTG_CORE_HANDLE , *PUSB_OTG_CORE_HANDLE;
302 
303 /**
304   * @}
305   */
306 
307 
308 /** @defgroup USB_CORE_Exported_Macros
309   * @{
310   */
311 
312 /**
313   * @}
314   */
315 
316 /** @defgroup USB_CORE_Exported_Variables
317   * @{
318   */
319 /**
320   * @}
321   */
322 
323 /** @defgroup USB_CORE_Exported_FunctionsPrototype
324   * @{
325   */
326 
327 
328 USB_OTG_STS  USB_OTG_CoreInit        (USB_OTG_CORE_HANDLE *pdev);
329 USB_OTG_STS  USB_OTG_SelectCore      (USB_OTG_CORE_HANDLE *pdev,
330                                       USB_OTG_CORE_ID_TypeDef coreID);
331 USB_OTG_STS  USB_OTG_EnableGlobalInt (USB_OTG_CORE_HANDLE *pdev);
332 USB_OTG_STS  USB_OTG_DisableGlobalInt(USB_OTG_CORE_HANDLE *pdev);
333 void*           USB_OTG_ReadPacket   (USB_OTG_CORE_HANDLE *pdev ,
334     uint8_t *dest,
335     uint16_t len);
336 USB_OTG_STS  USB_OTG_WritePacket     (USB_OTG_CORE_HANDLE *pdev ,
337     uint8_t *src,
338     uint8_t ch_ep_num,
339     uint16_t len);
340 USB_OTG_STS  USB_OTG_FlushTxFifo     (USB_OTG_CORE_HANDLE *pdev , uint32_t num);
341 USB_OTG_STS  USB_OTG_FlushRxFifo     (USB_OTG_CORE_HANDLE *pdev);
342 
343 uint32_t     USB_OTG_ReadCoreItr     (USB_OTG_CORE_HANDLE *pdev);
344 uint32_t     USB_OTG_ReadOtgItr      (USB_OTG_CORE_HANDLE *pdev);
345 uint8_t      USB_OTG_IsHostMode      (USB_OTG_CORE_HANDLE *pdev);
346 uint8_t      USB_OTG_IsDeviceMode    (USB_OTG_CORE_HANDLE *pdev);
347 uint32_t     USB_OTG_GetMode         (USB_OTG_CORE_HANDLE *pdev);
348 USB_OTG_STS  USB_OTG_PhyInit         (USB_OTG_CORE_HANDLE *pdev);
349 USB_OTG_STS  USB_OTG_SetCurrentMode  (USB_OTG_CORE_HANDLE *pdev,
350     uint8_t mode);
351 
352 /*********************** HOST APIs ********************************************/
353 #ifdef USE_HOST_MODE
354 USB_OTG_STS  USB_OTG_CoreInitHost    (USB_OTG_CORE_HANDLE *pdev);
355 USB_OTG_STS  USB_OTG_EnableHostInt   (USB_OTG_CORE_HANDLE *pdev);
356 USB_OTG_STS  USB_OTG_HC_Init         (USB_OTG_CORE_HANDLE *pdev, uint8_t hc_num);
357 USB_OTG_STS  USB_OTG_HC_Halt         (USB_OTG_CORE_HANDLE *pdev, uint8_t hc_num);
358 USB_OTG_STS  USB_OTG_HC_StartXfer    (USB_OTG_CORE_HANDLE *pdev, uint8_t hc_num);
359 USB_OTG_STS  USB_OTG_HC_DoPing       (USB_OTG_CORE_HANDLE *pdev , uint8_t hc_num);
360 uint32_t     USB_OTG_ReadHostAllChannels_intr    (USB_OTG_CORE_HANDLE *pdev);
361 uint32_t     USB_OTG_ResetPort       (USB_OTG_CORE_HANDLE *pdev);
362 uint32_t     USB_OTG_ReadHPRT0       (USB_OTG_CORE_HANDLE *pdev);
363 void         USB_OTG_DriveVbus       (USB_OTG_CORE_HANDLE *pdev, uint8_t state);
364 void         USB_OTG_InitFSLSPClkSel (USB_OTG_CORE_HANDLE *pdev ,uint8_t freq);
365 uint8_t      USB_OTG_IsEvenFrame     (USB_OTG_CORE_HANDLE *pdev) ;
366 void         USB_OTG_StopHost        (USB_OTG_CORE_HANDLE *pdev);
367 #endif
368 /********************* DEVICE APIs ********************************************/
369 #ifdef USE_DEVICE_MODE
370 USB_OTG_STS  USB_OTG_CoreInitDev         (USB_OTG_CORE_HANDLE *pdev);
371 USB_OTG_STS  USB_OTG_EnableDevInt        (USB_OTG_CORE_HANDLE *pdev);
372 uint32_t     USB_OTG_ReadDevAllInEPItr           (USB_OTG_CORE_HANDLE *pdev);
373 enum USB_OTG_SPEED USB_OTG_GetDeviceSpeed (USB_OTG_CORE_HANDLE *pdev);
374 USB_OTG_STS  USB_OTG_EP0Activate (USB_OTG_CORE_HANDLE *pdev);
375 USB_OTG_STS  USB_OTG_EPActivate  (USB_OTG_CORE_HANDLE *pdev , USB_OTG_EP *ep);
376 USB_OTG_STS  USB_OTG_EPDeactivate(USB_OTG_CORE_HANDLE *pdev , USB_OTG_EP *ep);
377 USB_OTG_STS  USB_OTG_EPStartXfer (USB_OTG_CORE_HANDLE *pdev , USB_OTG_EP *ep);
378 USB_OTG_STS  USB_OTG_EP0StartXfer(USB_OTG_CORE_HANDLE *pdev , USB_OTG_EP *ep);
379 USB_OTG_STS  USB_OTG_EPSetStall          (USB_OTG_CORE_HANDLE *pdev , USB_OTG_EP *ep);
380 USB_OTG_STS  USB_OTG_EPClearStall        (USB_OTG_CORE_HANDLE *pdev , USB_OTG_EP *ep);
381 uint32_t     USB_OTG_ReadDevAllOutEp_itr (USB_OTG_CORE_HANDLE *pdev);
382 uint32_t     USB_OTG_ReadDevOutEP_itr    (USB_OTG_CORE_HANDLE *pdev , uint8_t epnum);
383 uint32_t     USB_OTG_ReadDevAllInEPItr   (USB_OTG_CORE_HANDLE *pdev);
384 void         USB_OTG_InitDevSpeed        (USB_OTG_CORE_HANDLE *pdev , uint8_t speed);
385 uint8_t      USBH_IsEvenFrame (USB_OTG_CORE_HANDLE *pdev);
386 void         USB_OTG_EP0_OutStart(USB_OTG_CORE_HANDLE *pdev);
387 void         USB_OTG_ActiveRemoteWakeup(USB_OTG_CORE_HANDLE *pdev);
388 void         USB_OTG_UngateClock(USB_OTG_CORE_HANDLE *pdev);
389 void         USB_OTG_StopDevice(USB_OTG_CORE_HANDLE *pdev);
390 void         USB_OTG_SetEPStatus (USB_OTG_CORE_HANDLE *pdev , USB_OTG_EP *ep , uint32_t Status);
391 uint32_t     USB_OTG_GetEPStatus(USB_OTG_CORE_HANDLE *pdev ,USB_OTG_EP *ep);
392 #endif
393 /**
394   * @}
395   */
396 
397 #endif  /* __USB_CORE_H__ */
398 
399 
400 /**
401   * @}
402   */
403 
404 /**
405   * @}
406   */
407 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
408 
409