EFR32 Blue Gecko 13 Software Documentation  efr32bg13-doc-5.1.2
em_usbxpress_callback.c
1 /***************************************************************************/
16 #include <stddef.h>
17 #include "em_usbxpress_internal.h"
19 #include "em_usbxpress.h"
20 
21 // -----------------------------------------------------------------------------
22 // Variables
23 
25 SL_ALIGN(4)
26 static uint8_t usbxpressPartNumber SL_ATTRIBUTE_ALIGN(4) =
28 
30 SL_ALIGN(4)
31 static uint8_t usbxpressVersion[2] SL_ATTRIBUTE_ALIGN(4) =
32 {
34  ((USBXPRESS_LIBRARY_VERSION & 0xFF00) >> 8)
35 };
36 
37 // -----------------------------------------------------------------------------
38 // Extern Variable Declarations
39 
40 extern uint8_t USBXCORE_overflowBuffer[];
41 extern bool USBXCORE_zlpActive;
43 extern uint32_t USBXCORE_rxOverflowPacketSize;
44 
45 // -----------------------------------------------------------------------------
46 // Functions
47 
48 /**************************************************************************/
54 void USBX_ResetCb(void)
55 {
58 
60  {
61  // Jump to API ISR
63  }
64 }
65 
66 /**************************************************************************/
72 void USBX_DeviceStateChangeCb(USBD_State_TypeDef oldState,
73  USBD_State_TypeDef newState)
74 {
75  (void) oldState; // Suppress compiler warning: unused parameter
76 
77  // Entering suspend mode, power internal and external blocks down
78  if (newState == USBD_STATE_SUSPENDED)
79  {
82  }
83  if (newState == USBD_STATE_CONFIGURED)
84  {
86  }
87  if (newState < USBD_STATE_CONFIGURED)
88  {
90  }
91 
93  {
94  // Call to assembly function to cleanup stack and jump to API ISR
96  }
97 }
98 
99 /**************************************************************************/
106 int USBX_SetupCmdCb(const USB_Setup_TypeDef *setup)
107 {
108  USB_Status_TypeDef retval = USB_STATUS_REQ_UNHANDLED;
109  uint16_t length;
110 
111  // Handle open and close events
112  if (setup->Type == USB_SETUP_TYPE_VENDOR)
113  {
114  // Look for vendor-specific requests
115  switch (setup->bRequest)
116  {
117  // Requests directed to a USBXpress Device
119  switch (setup->wValue)
120  {
121  // Flush Buffers
124  {
125  USBXCORE_apiEa &= ~APIEA_GIE; // Turn off bit 1
126  USBXCORE_apiEa |= APIEA_GIE_TEMP; // Turn on bit 2
127  }
128  else
129  {
130  USBXCORE_apiEa &= ~APIEA_GIE_TEMP; // Turn off bit 2
131  }
132 
133  // Abort the current write transfer.
134  // This will flush any data in the FIFO.
135  USBD_AbortTransfer(USBXPRESS_IN_EP_ADDR);
136 
138 
139  // Clear all other interrupts, set flush buffer interrupt
141 
143  {
145  }
146  retval = USB_STATUS_OK;
147  break;
148 
149  // Enable
152  retval = USB_STATUS_OK;
153  break;
154 
155  // Disable
158  retval = USB_STATUS_OK;
159  break;
160 
161  // Get Device Library Version
163  USBD_Write(USBXPRESS_SETUP_EP_ADDR, &usbxpressVersion, 2, NULL);
164  retval = USB_STATUS_OK;
165  break;
166  }
167  break;
168 
169  // Requests directed to a CP210x Device
170  case SI_CP210X_REQUEST:
171  // Get Part Number
172  if (setup->wValue == SI_CP210X_GET_PART_NUMBER)
173  {
174  USBD_Write(USBXPRESS_SETUP_EP_ADDR, &usbxpressPartNumber, 1, NULL);
175  retval = USB_STATUS_OK;
176  }
177  break;
178  }
179  }
180 
181  // Jump to API ISR if a valid command was received.
182  if (retval == USB_STATUS_OK)
183  {
185  {
186  // Jump to API ISR
188  }
189 
190  return retval;
191  }
192 
193  // Intercept the Microsoft OS Descriptor Requests
194  if (setup->bmRequestType == (USB_SETUP_DIR_D2H
195  | USB_SETUP_TYPE_STANDARD
196  | USB_SETUP_RECIPIENT_DEVICE))
197  {
198  if ((setup->bRequest == GET_DESCRIPTOR)
199  && ((setup->wValue >> 8) == USB_STRING_DESCRIPTOR))
200  {
201  if ((setup->wValue & 0xFF) == 0xEE)
202  {
203  USBD_Write(USBXPRESS_SETUP_EP_ADDR,
204  (uint8_t *) &USBXCORE_microsoftOsDesc,
205  USBXCORE_microsoftOsDesc[0],
206  NULL);
207 
208  retval = USB_STATUS_OK;
209  }
210  }
211  }
212 
213  // Vendor specific IN request - Get Windows OS Compatibility ID Descriptor
214  else if ((setup->bmRequestType == (USB_SETUP_DIR_D2H
215  | USB_SETUP_TYPE_VENDOR_MASK
216  | USB_SETUP_RECIPIENT_DEVICE))
217  && (setup->bRequest == EXT_COMP_VENDOR_CODE)
218  && (setup->wIndex == FEATURE_EXTENDED_COMPATIBILITY_ID))
219 
220  {
221  length = EXT_COMP_DESC_SIZE;
222 
223  if (length > setup->wLength)
224  {
225  length = setup->wLength;
226  }
227 
228  USBD_Write(USBXPRESS_SETUP_EP_ADDR,
229  (uint8_t *) &USBXCORE_extendedCompatIdOsFeatureDesc,
230  length,
231  NULL);
232  retval = USB_STATUS_OK;
233  }
234 
235  // Vendor specific IN request - Get Windows OS Extended Properties Descriptor
236  else if ((setup->bmRequestType == (USB_SETUP_DIR_D2H
237  | USB_SETUP_TYPE_VENDOR_MASK
238  | USB_SETUP_RECIPIENT_INTERFACE))
239  && (setup->bRequest == EXT_COMP_VENDOR_CODE)
240  && ((setup->wIndex == FEATURE_EXTENDED_PROPERTIES_ID)
241  || (setup->wIndex == 0)))
242 
243  {
245 
246  if (length > setup->wLength)
247  {
248  length = setup->wLength;
249  }
250 
251  USBD_Write(USBXPRESS_SETUP_EP_ADDR,
252  (uint8_t *) &USBXCORE_extendedPropertiesDesc,
253  length,
254  NULL);
255  retval = USB_STATUS_OK;
256  }
257 
258  return retval;
259 }
260 
261 /**************************************************************************/
268 int USBX_inXferCompleteCb(USB_Status_TypeDef status,
269  uint16_t xferred,
270  uint16_t remaining)
271 {
272  (void) remaining; // Suppress compiler warning: unused parameter
273 
274  if (status == USB_STATUS_OK)
275  {
276  *USBXCORE_byteCountInPtr += xferred;
277  USBXCORE_writeSize -= xferred;
278 
279  // If the transfer was a multiple of the maximum packet size, send a ZLP
280  // to the host to signal the end of the transfer.
281  if (USBXCORE_writeSize == 0)
282  {
283  if ((xferred) && (xferred % USB_FS_BULK_EP_MAXSIZE == 0))
284  {
285  USBD_Write(USBXPRESS_IN_EP_ADDR,
286  NULL,
287  0,
288  (USB_XferCompleteCb_TypeDef) USBX_inXferCompleteCb);
289  }
290  else
291  {
292  // Notify of transmit complete
294  }
295  }
296  }
297 
299  {
300  // Call to assembly function to cleanup stack and jump to API ISR
302  }
303 
304  return 0;
305 }
306 
307 /**************************************************************************/
314 int USBX_outXferCompleteCb(USB_Status_TypeDef status,
315  uint16_t xferred,
316  uint16_t remaining)
317 {
318  (void) remaining; // Suppress compiler warning: unused parameter
319 
320  if (status == USB_STATUS_OK)
321  {
322  if (xferred <= USBXCORE_readSize)
323  {
324  USBXCORE_readSize -= xferred;
325  *USBXCORE_byteCountOutPtr += xferred;
326  }
327  else
328  {
330  USBXCORE_readSize = 0;
331  }
332 
333  // If the total read size is not decremented to zero, the transfer has ended.
334  if (USBXCORE_readSize)
335  {
336  // Notify of receive complete
338  }
339  // If this was a ZLP, mark USBX_RX_COMPLETE and USBX_RX_OVERRUN, if necessary
340  else if (USBXCORE_zlpActive)
341  {
342  // Notify of receive complete
344 
345  USBXCORE_zlpActive = false;
346 
347  // If we received data, notify of receive overrun
348  if (xferred > 0)
349  {
351  USBXCORE_rxOverflowPacketAvailable = true;
352  USBXCORE_rxOverflowPacketSize = xferred;
353  }
354  }
355  else
356  {
357  USBXCORE_zlpActive = true;
358  USBD_Read(USBXPRESS_OUT_EP_ADDR,
359  USBXCORE_overflowBuffer,
360  0,
361  (USB_XferCompleteCb_TypeDef) USBX_outXferCompleteCb);
362  }
363  }
364 
366  {
367  // Call to assembly function to cleanup stack and jump to API ISR
369  }
370 
371  return 0;
372 }
uint32_t USBXCORE_apiEa
Enable or disable status of USB_API interrupts.
Definition: em_usbxpress.c:41
void USBX_jumpCallback(void)
Conditionally jumps to the user call-back routine.
Definition: em_usbxpress.c:264
bool USBXCORE_zlpActive
Boolean indicating whether a ZLP read is active.
Definition: em_usbxpress.c:47
bool USBXCORE_rxOverflowPacketAvailable
Boolean indicating if data was received while expecting a ZLP.
Definition: em_usbxpress.c:50
#define SL_ALIGN(X)
Macro for aligning a variable. Use this macro before the variable definition. X denotes the stora...
Definition: em_common.h:168
#define SI_USBXPRESS_REQUEST
Request.
void USBXCORE_resetState(void)
Resets internal USBXpress variables.
Definition: em_usbxpress.c:276
#define SI_USBXPRESS_CLEAR_TO_SEND
Value.
#define USBX_RX_OVERRUN
Data received with no place to put it.
Definition: em_usbxpress.h:119
uint32_t USBXCORE_apiIntValue
Byte holding the current USB_API interrupts.
Definition: em_usbxpress.c:38
#define SI_USBXPRESS_NOT_CLEAR_TO_SEND
Value.
#define USBX_RX_COMPLETE
Receive Complete Interrupt has occurred.
Definition: em_usbxpress.h:113
#define FEATURE_EXTENDED_COMPATIBILITY_ID
Extended Compatibility ID Descriptor.
#define USBX_TX_COMPLETE
Transmit Complete Interrupt has occurred.
Definition: em_usbxpress.h:112
#define APIEA_GIE_TEMP
#define SI_CP210X_GET_PART_NUMBER
Value.
uint32_t USBXCORE_rxOverflowPacketSize
Size of Rx Overflow Packet.
Definition: em_usbxpress.c:53
#define SI_CP210X_REQUEST
Request.
int USBX_SetupCmdCb(const USB_Setup_TypeDef *setup)
USB setup command call-back.
uint32_t * USBXCORE_byteCountOutPtr
Pointer to variable holding number of bytes read.
Definition: em_usbxpress.c:64
Primary header file. Contains internal global declarations and definitions.
#define SL_ATTRIBUTE_ALIGN(X)
GCC style macro for aligning a variable.
Definition: em_common.h:160
uint32_t USBXCORE_readSize
Number of bytes sent to USBX_blockRead() as numBytes.
Definition: em_usbxpress.c:67
#define USBX_DEV_SUSPEND
USB suspend signaling present on bus.
Definition: em_usbxpress.h:118
#define EXT_COMP_VENDOR_CODE
Vendor-defined Extended Compatibility Code.
uint32_t * USBXCORE_byteCountInPtr
Pointer to variable holding number of bytes written.
Definition: em_usbxpress.c:61
#define FEATURE_EXTENDED_PROPERTIES_ID
Extended Properties ID Descriptor.
void USBX_ResetCb(void)
USB Reset call-back.
Header file for the USBXpress firmware library. Includes function prototypes, type definitions...
#define USBX_DEV_CONFIGURED
Device has entered configured state.
Definition: em_usbxpress.h:117
#define USBX_DEV_OPEN
Device Instance Opened on host side.
Definition: em_usbxpress.h:115
#define USBX_FIFO_PURGE
Receive and Transmit FIFO's were purged.
Definition: em_usbxpress.h:114
#define APIEA_GIE
Enable.
#define SI_USBXPRESS_FLUSH_BUFFERS
Value.
int USBX_inXferCompleteCb(USB_Status_TypeDef, uint16_t, uint16_t remaining)
USBXpress IN Endpoint Transfer Complete Callback.
uint32_t USBXCORE_writeSize
Number of bytes sent to USBX_blockWrite() as numBytes.
Definition: em_usbxpress.c:70
void USBX_DeviceStateChangeCb(USBD_State_TypeDef oldState, USBD_State_TypeDef newState)
USB device state change call-back.
#define USBX_DEV_CLOSE
Device Instance Closed on host side.
Definition: em_usbxpress.h:116
#define SI_USBXPRESS_GET_VERSION
Value.
Header file for USB and VCP Initial Descriptors.
#define SI_USBXPRESS_PART_NUMBER_EFM32
EFM32 USBXpress Part Number.
#define EXT_COMP_DESC_SIZE
Size of Extended Compatibility Descriptor.
#define USBX_RESET
USB Reset Interrupt has occurred.
Definition: em_usbxpress.h:111
int USBX_outXferCompleteCb(USB_Status_TypeDef, uint16_t, uint16_t remaining)
USBXpress OUT Endpoint Transfer Complete Callback.
#define USBXPRESS_LIBRARY_VERSION
#define PROPERTIES_DESCRIPTOR_SIZE
Total Size of Extended Properties Descriptor.