EZR32 Wonder Gecko Software Documentation  ezr32wg-doc-4.2.1
em_usbd.h
Go to the documentation of this file.
1 /***************************************************************************/
16 #ifndef __EM_USBD_H
17 #define __EM_USBD_H
18 
19 #include "em_device.h"
20 #if defined( USB_PRESENT ) && ( USB_COUNT == 1 )
21 #include "em_usb.h"
22 #if defined( USB_DEVICE )
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
30 #if defined( DEBUG_USB_API )
31 #define DEBUG_TRACE_ABORT( x ) \
32 { \
33  if ( x == USB_STATUS_EP_STALLED ) \
34  { DEBUG_USB_API_PUTS( "\nEP cb(), EP stalled" ); } \
35  else if ( x == USB_STATUS_EP_ABORTED ) \
36  { DEBUG_USB_API_PUTS( "\nEP cb(), EP aborted" ); } \
37  else if ( x == USB_STATUS_DEVICE_UNCONFIGURED ) \
38  { DEBUG_USB_API_PUTS( "\nEP cb(), device unconfigured" ); } \
39  else if ( x == USB_STATUS_DEVICE_SUSPENDED ) \
40  { DEBUG_USB_API_PUTS( "\nEP cb(), device suspended" ); } \
41  else /* ( x == USB_STATUS_DEVICE_RESET ) */ \
42  { DEBUG_USB_API_PUTS( "\nEP cb(), device reset" ); } \
43 }
44 #else
45 #define DEBUG_TRACE_ABORT( x )
46 #endif
47 
48 extern USBD_Device_TypeDef *dev;
49 extern volatile bool USBD_poweredDown;
50 
51 __STATIC_INLINE void USBD_ArmEp0( USBD_Ep_TypeDef *ep );
52 __STATIC_INLINE void USBD_ArmEpN( USBD_Ep_TypeDef *ep );
53 __STATIC_INLINE void USBD_AbortEp( USBD_Ep_TypeDef *ep );
54 
55 void USBD_SetUsbState( USBD_State_TypeDef newState );
56 
57 int USBDCH9_SetupCmd( USBD_Device_TypeDef *device );
58 
59 void USBDEP_Ep0Handler( USBD_Device_TypeDef *device );
60 void USBDEP_EpHandler( uint8_t epAddr );
61 
62 __STATIC_INLINE void USBD_ActivateAllEps( bool forceIdle )
63 {
64  int i;
65 
66  for ( i = 1; i <= NUM_EP_USED; i++ )
67  {
68  USBDHAL_ActivateEp( &dev->ep[ i ], forceIdle );
69  }
70 }
71 
72 __STATIC_INLINE void USBD_ArmEp( USBD_Ep_TypeDef *ep )
73 {
74  if ( ep->num == 0 )
75  {
76  USBD_ArmEp0( ep );
77  }
78  else
79  {
80  USBD_ArmEpN( ep );
81  }
82 }
83 
84 __STATIC_INLINE void USBD_ArmEp0( USBD_Ep_TypeDef *ep )
85 {
86  if ( ep->in )
87  {
88  if ( ep->remaining == 0 ) /* Zero Length Packet? */
89  {
90  ep->zlp = 1;
91  }
92 
93  USBDHAL_SetEp0InDmaPtr( ep->buf );
94  USBDHAL_StartEp0In( EFM32_MIN( ep->remaining, ep->packetSize ),
95  dev->ep0MpsCode );
96  }
97  else
98  {
99  USBDHAL_SetEp0OutDmaPtr( ep->buf );
100  USBDHAL_StartEp0Out( ep->packetSize, dev->ep0MpsCode );
101  }
102 }
103 
104 __STATIC_INLINE void USBD_ArmEpN( USBD_Ep_TypeDef *ep )
105 {
106  if ( ep->in )
107  {
108  USBDHAL_StartEpIn( ep );
109  }
110  else
111  {
112  USBDHAL_StartEpOut( ep );
113  }
114 }
115 
116 __STATIC_INLINE void USBD_DeactivateAllEps( USB_Status_TypeDef reason )
117 {
118  int i;
119  USBD_Ep_TypeDef *ep;
120 
121  for ( i = 1; i <= NUM_EP_USED; i++ )
122  {
123  ep = &dev->ep[ i ];
124 
125  if ( ep->state == D_EP_IDLE )
126  {
127  USBDHAL_DeactivateEp( ep );
128  }
129  }
130 
131  USBDHAL_AbortAllTransfers( reason );
132 }
133 
134 __STATIC_INLINE USBD_Ep_TypeDef *USBD_GetEpFromAddr( uint8_t epAddr )
135 {
136  int epIndex;
137  USBD_Ep_TypeDef *ep = NULL;
138 
139  if ( epAddr & USB_SETUP_DIR_MASK )
140  {
141  epIndex = dev->inEpAddr2EpIndex[ epAddr & USB_EPNUM_MASK ];
142  }
143  else
144  {
145  epIndex = dev->outEpAddr2EpIndex[ epAddr & USB_EPNUM_MASK ];
146  }
147 
148  if ( epIndex )
149  {
150  ep = &dev->ep[ epIndex ];
151  }
152  else if ( ( epAddr & USB_EPNUM_MASK ) == 0 )
153  {
154  ep = &dev->ep[ 0 ];
155  }
156 
157  return ep;
158 }
159 
160 __STATIC_INLINE void USBD_ReArmEp0( USBD_Ep_TypeDef *ep )
161 {
162  if ( ep->in )
163  {
164  USBDHAL_StartEp0In( EFM32_MIN( ep->remaining, ep->packetSize ),
165  dev->ep0MpsCode );
166  }
167  else
168  {
169  USBDHAL_StartEp0Out( ep->packetSize, dev->ep0MpsCode );
170  }
171 }
172 
173 __STATIC_INLINE void USBD_AbortEp( USBD_Ep_TypeDef *ep )
174 {
175  if ( ep->state == D_EP_IDLE )
176  {
177  return;
178  }
179 
180  if ( ep->in )
181  {
182  USBDHAL_AbortEpIn( ep );
183  }
184  else
185  {
186  USBDHAL_AbortEpOut( ep );
187  }
188 }
189 
192 #ifdef __cplusplus
193 }
194 #endif
195 
196 #endif /* defined( USB_DEVICE ) */
197 #endif /* defined( USB_PRESENT ) && ( USB_COUNT == 1 ) */
198 #endif /* __EM_USBD_H */
#define EFM32_MIN(a, b)
Definition: em_common.h:79
#define USB_EPNUM_MASK
Definition: em_usb.h:179
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
#define USB_SETUP_DIR_MASK
Definition: em_usb.h:60
USBD_State_TypeDef
USB device state enumerator.
Definition: em_usb.h:356
USB_Status_TypeDef
USB transfer status enumerator.
Definition: em_usb.h:316
USB protocol stack library API for EFM32/EZR32.