EFM32 Happy Gecko Software Documentation  efm32hg-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 /**************************************************************************/
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 /**************************************************************************/
107 {
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
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
217  && (setup->bRequest == EXT_COMP_VENDOR_CODE)
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
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 /**************************************************************************/
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,
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 /**************************************************************************/
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,
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
#define USB_SETUP_TYPE_VENDOR_MASK
Definition: em_usb.h:78
#define USB_SETUP_DIR_D2H
Definition: em_usb.h:69
#define USB_SETUP_TYPE_VENDOR
Definition: em_usb.h:75
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
uint8_t bmRequestType
Definition: em_usb.h:391
#define USB_STRING_DESCRIPTOR
Definition: em_usb.h:126
uint16_t wValue
Definition: em_usb.h:394
#define USB_FS_BULK_EP_MAXSIZE
Definition: em_usb.h:175
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
USB Setup request package.
Definition: em_usb.h:377
uint32_t USBXCORE_apiIntValue
Byte holding the current USB_API interrupts.
Definition: em_usbxpress.c:38
#define SI_USBXPRESS_NOT_CLEAR_TO_SEND
Value.
USBD_State_TypeDef
USB device state enumerator.
Definition: em_usb.h:358
#define USBX_RX_COMPLETE
Receive Complete Interrupt has occurred.
Definition: em_usbxpress.h:113
int(* USB_XferCompleteCb_TypeDef)(USB_Status_TypeDef status, uint32_t xferred, uint32_t remaining)
USB transfer callback function.
Definition: em_usb.h:663
USB_Status_TypeDef
USB transfer status enumerator.
Definition: em_usb.h:319
#define FEATURE_EXTENDED_COMPATIBILITY_ID
Extended Compatibility ID Descriptor.
#define USBX_TX_COMPLETE
Transmit Complete Interrupt has occurred.
Definition: em_usbxpress.h:112
uint16_t wIndex
Definition: em_usb.h:395
#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.
uint8_t Type
Definition: em_usb.h:388
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
int USBD_AbortTransfer(int epAddr)
Abort a pending transfer on a specific endpoint.
Definition: em_usbd.c:73
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
#define USB_SETUP_RECIPIENT_DEVICE
Definition: em_usb.h:81
#define USB_SETUP_RECIPIENT_INTERFACE
Definition: em_usb.h:82
#define USB_SETUP_TYPE_STANDARD
Definition: em_usb.h:73
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
uint16_t wLength
Definition: em_usb.h:396
#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...
int USBD_Write(int epAddr, void *data, int byteCount, USB_XferCompleteCb_TypeDef callback)
Start a write (IN) transfer on an endpoint.
Definition: em_usbd.c:851
#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 GET_DESCRIPTOR
Definition: em_usb.h:91
#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
uint8_t bRequest
Definition: em_usb.h:393
int USBX_outXferCompleteCb(USB_Status_TypeDef, uint16_t, uint16_t remaining)
USBXpress OUT Endpoint Transfer Complete Callback.
#define USBXPRESS_LIBRARY_VERSION
int USBD_Read(int epAddr, void *data, int byteCount, USB_XferCompleteCb_TypeDef callback)
Start a read (OUT) transfer on an endpoint.
Definition: em_usbd.c:573
#define PROPERTIES_DESCRIPTOR_SIZE
Total Size of Extended Properties Descriptor.