EFM32 Happy Gecko Software Documentation  efm32hg-doc-5.1.2
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 void USBDINT_RemoteWakeup(void);
63 
64 __STATIC_INLINE void USBD_ActivateAllEps( bool forceIdle )
65 {
66  int i;
67 
68  for ( i = 1; i <= NUM_EP_USED; i++ )
69  {
70  USBDHAL_ActivateEp( &dev->ep[ i ], forceIdle );
71  }
72 }
73 
74 __STATIC_INLINE void USBD_ArmEp( USBD_Ep_TypeDef *ep )
75 {
76  if ( ep->num == 0 )
77  {
78  USBD_ArmEp0( ep );
79  }
80  else
81  {
82  USBD_ArmEpN( ep );
83  }
84 }
85 
86 __STATIC_INLINE void USBD_ArmEp0( USBD_Ep_TypeDef *ep )
87 {
88  if ( ep->in )
89  {
90  if ( ep->remaining == 0 ) /* Zero Length Packet? */
91  {
92  ep->zlp = 1;
93  }
94 
95  USBDHAL_SetEp0InDmaPtr( ep->buf );
96  USBDHAL_StartEp0In( SL_MIN( ep->remaining, ep->packetSize ),
97  dev->ep0MpsCode );
98  }
99  else
100  {
101  USBDHAL_SetEp0OutDmaPtr( ep->buf );
102  USBDHAL_StartEp0Out( ep->packetSize, dev->ep0MpsCode );
103  }
104 }
105 
106 __STATIC_INLINE void USBD_ArmEpN( USBD_Ep_TypeDef *ep )
107 {
108  if ( ep->in )
109  {
110  USBDHAL_StartEpIn( ep );
111  }
112  else
113  {
114  USBDHAL_StartEpOut( ep );
115  }
116 }
117 
118 __STATIC_INLINE void USBD_DeactivateAllEps( USB_Status_TypeDef reason )
119 {
120  int i;
121  USBD_Ep_TypeDef *ep;
122 
123  for ( i = 1; i <= NUM_EP_USED; i++ )
124  {
125  ep = &dev->ep[ i ];
126 
127  if ( ep->state == D_EP_IDLE )
128  {
129  USBDHAL_DeactivateEp( ep );
130  }
131  }
132 
133  USBDHAL_AbortAllTransfers( reason );
134 }
135 
136 __STATIC_INLINE USBD_Ep_TypeDef *USBD_GetEpFromAddr( uint8_t epAddr )
137 {
138  int epIndex;
139  USBD_Ep_TypeDef *ep = NULL;
140 
141  if ( epAddr & USB_SETUP_DIR_MASK )
142  {
143  epIndex = dev->inEpAddr2EpIndex[ epAddr & USB_EPNUM_MASK ];
144  }
145  else
146  {
147  epIndex = dev->outEpAddr2EpIndex[ epAddr & USB_EPNUM_MASK ];
148  }
149 
150  if ( epIndex )
151  {
152  ep = &dev->ep[ epIndex ];
153  }
154  else if ( ( epAddr & USB_EPNUM_MASK ) == 0 )
155  {
156  ep = &dev->ep[ 0 ];
157  }
158 
159  return ep;
160 }
161 
162 __STATIC_INLINE void USBD_ReArmEp0( USBD_Ep_TypeDef *ep )
163 {
164  if ( ep->in )
165  {
166  USBDHAL_StartEp0In( SL_MIN( ep->remaining, ep->packetSize ),
167  dev->ep0MpsCode );
168  }
169  else
170  {
171  USBDHAL_StartEp0Out( ep->packetSize, dev->ep0MpsCode );
172  }
173 }
174 
175 __STATIC_INLINE void USBD_AbortEp( USBD_Ep_TypeDef *ep )
176 {
177  if ( ep->state == D_EP_IDLE )
178  {
179  return;
180  }
181 
182  if ( ep->in )
183  {
184  USBDHAL_AbortEpIn( ep );
185  }
186  else
187  {
188  USBDHAL_AbortEpOut( ep );
189  }
190 }
191 
194 #ifdef __cplusplus
195 }
196 #endif
197 
198 #endif /* defined( USB_DEVICE ) */
199 #endif /* defined( USB_PRESENT ) && ( USB_COUNT == 1 ) */
200 #endif /* __EM_USBD_H */
#define USB_EPNUM_MASK
Definition: em_usb.h:187
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
#define USB_SETUP_DIR_MASK
Definition: em_usb.h:68
USBD_State_TypeDef
USB device state enumerator.
Definition: em_usb.h:358
USB_Status_TypeDef
USB transfer status enumerator.
Definition: em_usb.h:319
USB protocol stack library API for EFM32/EZR32.
#define SL_MIN(a, b)
Macro for getting minimum value. No sideeffects, a and b are evaluated once only. ...
Definition: em_common.h:137