EFR32 Mighty Gecko 1 Software Documentation  efr32mg1-doc-5.1.2
em_usbxpress.c
Go to the documentation of this file.
1 /***************************************************************************/
17 #include <stddef.h>
18 #include <stdbool.h>
19 #include "em_usbxpress_internal.h"
21 #include "em_usbxpress.h"
22 
23 // -----------------------------------------------------------------------------
24 // Variable Definitions
25 
27 SL_ALIGN(4)
28 USB_DeviceDescriptor_TypeDef USBXCORE_deviceDesc SL_ATTRIBUTE_ALIGN(4);
29 
31 SL_ALIGN(4)
32 uint8_t USBXCORE_configDesc[32] SL_ATTRIBUTE_ALIGN(4);
33 
35 USB_StringDescriptor_TypeDef const * USBXCORE_stringDescTable[4];
36 
39 
41 uint32_t USBXCORE_apiEa;
42 
44 USBX_BUF(USBXCORE_overflowBuffer, USB_FS_BULK_EP_MAXSIZE);
45 
47 bool USBXCORE_zlpActive = false;
48 
51 
54 
57 void (*USBXCORE_apiCallback)(void);
59 
62 
65 
68 
71 
72 // -----------------------------------------------------------------------------
73 // Functions
74 
75 /**************************************************************************/
88 {
89  static const USBD_Callbacks_TypeDef callbacks =
90  {
91  .usbReset = USBX_ResetCb,
92  .usbStateChange = USBX_DeviceStateChangeCb,
93  .setupCmd = (USBD_SetupCmdCb_TypeDef) USBX_SetupCmdCb,
94  .isSelfPowered = NULL,
95  .sofInt = NULL
96  };
97 
98  // Initialization structure for EFM32 USB library
99  static const USBD_Init_TypeDef USBXCORE_initStruct =
100  {
101  .deviceDescriptor = (USB_DeviceDescriptor_TypeDef*) &USBXCORE_deviceDesc,
102  .configDescriptor = USBXCORE_configDesc,
103  .stringDescriptors = (const void *) &USBXCORE_stringDescTable,
104  .numberOfStrings = 4,
105  .callbacks = &callbacks,
106  .bufferingMultiplier = USBXCORE_buffMult,
107  .reserved = 0
108  };
109 
110  uint32_t i;
111 
112  // Initialize USB descriptors from code memory and passed startup parameters
113  // Copy Device Descriptor from code space to RAM so that it can be
114  // altered by user software.
115  for (i = 0; i < 18; i++)
116  {
117  *((uint8_t*)&USBXCORE_deviceDesc + i) =
118  *((uint8_t*)&USBXCORE_deviceDescInit + i);
119  }
120 
121  // Copy Configuration Descriptor from code space to RAM
122  for (i = 0; i < 32; i++)
123  {
125  }
126 
127  // Load the Vendor and Product ID.
128  USBXCORE_deviceDesc.idVendor = p->vendorId;
129  USBXCORE_deviceDesc.idProduct = p->productId;
130 
131  // Load the string language and descriptors.
132  USBXCORE_stringDescTable[0] =
133  (USB_StringDescriptor_TypeDef *) &USBXCORE_stringLangDesc;
134  USBXCORE_stringDescTable[1] = p->manufacturerString;
135  USBXCORE_stringDescTable[2] = p->productString;
136  USBXCORE_stringDescTable[3] = p->serialString;
137 
138  // Modify default value if user value is less than 0xFA (500 mA).
139  if(p->maxPower < 0xFA)
140  {
141  USBXCORE_configDesc[8] = (uint8_t)(p->maxPower);
142  }
143 
144  // Load BCD-coded release number and device power attribute to descriptors.
145  USBXCORE_deviceDesc.bcdDevice = p->releaseBcd;
146  USBXCORE_configDesc[7] = (uint8_t)(p->powerAttribute);
147 
148  // Clear USBXpress API interrupts and interrupt enable.
149  // User must explicitly enable via USB_Int_Enable().
150  USBXCORE_apiIntValue = 0;
151  USBXCORE_apiEa = 0;
152 
153  // EFM32 USB Library initialization routine.
154  // Last part of USBXpress USBX_init().
155  USBD_Init(&USBXCORE_initStruct);
156 }
157 
159 {
160  // Clear USBXCORE_API_EA.0 to disable USB API call-back generation.
161  USBXCORE_apiEa &= ~APIEA_GIE;
162 }
163 
165 {
166  USBXCORE_apiCallback = f; // Save API call-back pointer
167 
168  USBXCORE_apiEa |= APIEA_GIE; // Set USBXCORE_API_EA.0 to enable
169  // USB API call-back generation
170 }
171 
172 int USBX_blockRead(uint8_t *block,
173  uint32_t numBytes,
174  uint32_t *countPtr)
175 {
176  uint32_t i;
177 
178  USBXCORE_byteCountOutPtr = countPtr;
179  *USBXCORE_byteCountOutPtr = 0;
180 
181  // If the Rx Overflow Packet has data in it, copy that data to the buffer.
183  {
184  for (i = 0; i < USBXCORE_rxOverflowPacketSize; i++)
185  {
186  *block = USBXCORE_overflowBuffer[i];
187  block++;
188  }
189 
191 
192  // If the amount of data in the overflow queue was less than the requested
193  // amount of data, issue a read for the remaining data.
194  if (((numBytes - USBXCORE_rxOverflowPacketSize) > 0)
195  && (USBXCORE_rxOverflowPacketSize % USB_FS_BULK_EP_MAXSIZE) == 0)
196  {
197  *USBXCORE_byteCountOutPtr += USBXCORE_rxOverflowPacketSize;
198  USBXCORE_readSize = numBytes;
199  numBytes -= USBXCORE_rxOverflowPacketSize;
200  USBXCORE_rxOverflowPacketSize = 0;
201 
202  return USBD_Read(USBXPRESS_OUT_EP_ADDR,
203  block,
204  numBytes,
205  (USB_XferCompleteCb_TypeDef) USBX_outXferCompleteCb);
206  }
207  else
208  {
210  USBXCORE_rxOverflowPacketSize = 0;
211  USBXCORE_readSize = numBytes;
212 
213  return USBX_outXferCompleteCb(USB_STATUS_OK, i, 0);
214  }
215  }
216  else
217  {
218  USBXCORE_readSize = numBytes;
219  return USBD_Read(USBXPRESS_OUT_EP_ADDR,
220  block,
221  numBytes,
222  (USB_XferCompleteCb_TypeDef) USBX_outXferCompleteCb);
223  }
224 }
225 
226 int USBX_blockWrite(uint8_t *block,
227  uint32_t numBytes,
228  uint32_t *countPtr)
229 {
230  USBXCORE_byteCountInPtr = countPtr;
231  *USBXCORE_byteCountInPtr = 0;
232  USBXCORE_writeSize = numBytes;
233  return USBD_Write(USBXPRESS_IN_EP_ADDR,
234  block,
235  numBytes,
236  (USB_XferCompleteCb_TypeDef) USBX_inXferCompleteCb);
237 }
238 
240 {
241  uint32_t temp = USBXCORE_apiIntValue;
242 
243  USBXCORE_apiIntValue = 0;
244  return temp;
245 }
246 
247 void USBX_disable(void)
248 {
249  USBD_Stop();
250 }
251 
252 uint_least16_t USBX_getLibraryVersion(void)
253 {
255 }
256 
257 /**************************************************************************/
265 {
266  (*USBXCORE_apiCallback)();
267 }
268 
269 /**************************************************************************/
277 {
278  USBXCORE_readSize = 0;
279  USBXCORE_writeSize = 0;
280  USBXCORE_zlpActive = false;
282  USBXCORE_rxOverflowPacketSize = 0;
283 }
void USBX_apiCallbackDisable(void)
Inhibits user API call-backs and does NOT disable the USB interrupt.
Definition: em_usbxpress.c:158
const uint8_t USBXCORE_configDescInit[]
USB Configuration Descriptor.
uint32_t USBXCORE_apiIntValue
Byte holding the current USB_API interrupts.
Definition: em_usbxpress.c:38
bool USBXCORE_zlpActive
Boolean indicating whether a ZLP read is active.
Definition: em_usbxpress.c:47
const uint8_t USBXCORE_stringLangDesc[]
USB Language String Descriptor.
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
void USBX_init(USBX_Init_t *p)
User API USB initialization function.
Definition: em_usbxpress.c:87
USB_StringDescriptor_TypeDef const * USBXCORE_stringDescTable[4]
Table of pointers to various string descriptors.
Definition: em_usbxpress.c:35
const uint8_t USBXCORE_buffMult[]
USB Buffer Multiplier.
void USBX_jumpCallback(void)
Conditionally jumps to the user call-back routine.
Definition: em_usbxpress.c:264
USBXpress initialization function parameter typedef.
Definition: em_usbxpress.h:231
uint32_t * USBXCORE_byteCountOutPtr
Pointer to variable holding number of bytes read.
Definition: em_usbxpress.c:64
uint_least16_t USBX_getLibraryVersion(void)
Returns the USBXpress library version.
Definition: em_usbxpress.c:252
uint32_t USBXCORE_rxOverflowPacketSize
Size of Rx Overflow Packet.
Definition: em_usbxpress.c:53
uint32_t USBXCORE_apiEa
Enable or disable status of USB_API interrupts.
Definition: em_usbxpress.c:41
int USBX_SetupCmdCb(const USB_Setup_TypeDef *setup)
USB setup command call-back.
int USBX_blockRead(uint8_t *block, uint32_t numBytes, uint32_t *countPtr)
User API function to get data from host.
Definition: em_usbxpress.c:172
Primary header file. Contains internal global declarations and definitions.
int USBX_blockWrite(uint8_t *block, uint32_t numBytes, uint32_t *countPtr)
User API function to send data to host.
Definition: em_usbxpress.c:226
uint32_t USBX_getCallbackSource(void)
User API function to get the call-back source.
Definition: em_usbxpress.c:239
void(* USBX_apiCallback_t)(void)
Definition: em_usbxpress.h:97
uint32_t USBXCORE_readSize
Number of bytes sent to USBX_blockRead() as numBytes.
Definition: em_usbxpress.c:67
uint32_t * USBXCORE_byteCountInPtr
Pointer to variable holding number of bytes written.
Definition: em_usbxpress.c:61
void USBX_ResetCb(void)
USB Reset call-back.
Header file for the USBXpress firmware library. Includes function prototypes, type definitions...
#define APIEA_GIE
Enable.
void USBX_apiCallbackEnable(USBX_apiCallback_t f)
Enables user API call-backs.
Definition: em_usbxpress.c:164
int USBX_inXferCompleteCb(USB_Status_TypeDef, uint16_t, uint16_t remaining)
USBXpress IN Endpoint Transfer Complete Callback.
USBX_BUF(USBXCORE_overflowBuffer, USB_FS_BULK_EP_MAXSIZE)
Buffer to hold overflow rx data if a ZLP read returns more than zero bytes.
USB_DeviceDescriptor_TypeDef USBXCORE_deviceDesc SL_ATTRIBUTE_ALIGN(4)
Copy of device descriptor, so it can be edited.
void USBX_DeviceStateChangeCb(USBD_State_TypeDef oldState, USBD_State_TypeDef newState)
USB device state change call-back.
Header file for USB and VCP Initial Descriptors.
void USBXCORE_resetState(void)
Resets internal USBXpress variables.
Definition: em_usbxpress.c:276
void USBX_disable(void)
Disables the USB interface.
Definition: em_usbxpress.c:247
int USBX_outXferCompleteCb(USB_Status_TypeDef, uint16_t, uint16_t remaining)
USBXpress OUT Endpoint Transfer Complete Callback.
uint32_t USBXCORE_writeSize
Number of bytes sent to USBX_blockWrite() as numBytes.
Definition: em_usbxpress.c:70
uint8_t USBXCORE_configDesc[32]
Copy of configuration descriptor.
#define USBXPRESS_LIBRARY_VERSION