efm8 usb bridge working

This commit is contained in:
Conor Patrick
2018-07-04 12:48:18 -04:00
parent ca882dc38a
commit 344a104ece
39 changed files with 9027 additions and 812 deletions

View File

@@ -0,0 +1,63 @@
-------------------------------------------------------------------------------
Readme.txt
-------------------------------------------------------------------------------
Copyright 2014 Silicon Laboratories, Inc.
http://developer.silabs.com/legal/version/v11/Silicon_Labs_Software_License_Agreement.txt
Program Description:
-------------------
This is the generic EFM8 USB Firmware Library. Please see the EFM8 Libraries
Documentation for more information (/doc/EFM8/software/Lib/index.html).
Known Issues and Limitations:
----------------------------
1) The library does not reset its Data Toggle after receiving a SET_INTERFACE
request.
Target and Tool Chain Information:
---------------------------------
Target: EFM8UB1, EFM8UB2, EFM8UB3, EFM8UB4, C8051F320/1, C8051F326/7, C8051F34x, C8051F38x
Tool chain: Keil
File List:
---------
/inc/efm8_usb.h
/src/efm8_usbd.c
/src/efm8_usbdch9.c
/src/efm8_usbdep.c
/src/efm8_usbdint.c
Release Information:
-------------------
Version 1.0.0
- Initial release.
Version 1.0.1
- Fixed bug in logic of remote wakeup feature where the device would
attempt to wake the host before enabling its USB transceiver.
- Fixed bug where the device would stall the Data Phase instead of the
Setup Phase when sending a procedural stall on Endpoint 0.
- Fixed bug where a bus-powered device would look at VBUS after a USB Reset
to determine if it should enter the Default or Attached State. VBUS is
always present on a bus-powered device, so it should automatically enter
the Default State.
- Removed code that generated a compiler warning when
USB_PWRSAVE_MODE_FASTWAKE was enabled.
- Improved documentation of USB_PWRSAVE_MODE_FASTWAKE feature.
Version 1.0.2
- Added ability to detect short OUT packet in Isochronous mode and
stuff the buffer with zeroes to keep isochronous stream in sync.
Version 1.0.3
- Added support for EFM8UB3 and EFM8UB4 devices.
-------------------------------------------------------------------------------
End Of File
-------------------------------------------------------------------------------

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,780 @@
/**************************************************************************//**
* Copyright (c) 2015 by Silicon Laboratories Inc. All rights reserved.
*
* http://developer.silabs.com/legal/version/v11/Silicon_Labs_Software_License_Agreement.txt
*****************************************************************************/
#include "si_toolchain.h"
#include "efm8_usb.h"
#include "assert.h"
#include <stdint.h>
// -----------------------------------------------------------------------------
// Global Variables
/// Tracks the state of the USB device and endpoints and contains pointers
/// to all descriptors.
SI_SEGMENT_VARIABLE(myUsbDevice, USBD_Device_TypeDef, MEM_MODEL_SEG);
// -----------------------------------------------------------------------------
// Macros
/// Returns the requested endpoint object of type USBD_Ep_TypeDef
/// This macro does not check that epAddr is valid, so the calling function
/// should verify epAddr before using the macro.
#define GetEp(epAddr) (&myUsbDevice.ep0 + epAddr)
#if SLAB_USB_POLLED_MODE
#define DISABLE_USB_INTS {}
#define ENABLE_USB_INTS {}
#else
/// Saves the current state of the USB Interrupt Enable to a variable called
/// usbIntsEnabled, then disables USB interrupts.
#define DISABLE_USB_INTS { usbIntsEnabled = USB_GetIntsEnabled(); USB_DisableInts(); }
/// Sets the USB Interrupt Enable bit to the value of usbIntsEnabled.
/// @ref DISABLE_USB_INTS must be used before this macro is used.
#define ENABLE_USB_INTS { if (usbIntsEnabled) {USB_EnableInts(); } }
#endif // SLAB_USB_POLLED_MODE
// Function in efm8_usbdint.c to force load the module for libraries
extern void forceModuleLoad_usbint(void);
// -----------------------------------------------------------------------------
// USB API Functions
void USBD_AbortAllTransfers(void)
{
uint8_t i;
bool usbIntsEnabled;
USB_SaveSfrPage();
DISABLE_USB_INTS;
// Call USBD_AbortTransfer() for each endpoint
for (i = 1; i < SLAB_USB_NUM_EPS_USED; i++)
{
USBD_AbortTransfer(i);
}
ENABLE_USB_INTS;
USB_RestoreSfrPage();
}
int8_t USBD_AbortTransfer(uint8_t epAddr)
{
SI_VARIABLE_SEGMENT_POINTER(ep, USBD_Ep_TypeDef, MEM_MODEL_SEG);
int8_t retVal = USB_STATUS_OK;
bool usbIntsEnabled;
USB_SaveSfrPage();
// Verify this is a valid endpoint address and is not Endpoint 0.
if ((epAddr == EP0) || (epAddr >= SLAB_USB_NUM_EPS_USED))
{
SLAB_ASSERT(false);
retVal = USB_STATUS_ILLEGAL;
}
else
{
DISABLE_USB_INTS;
ep = GetEp(epAddr);
// If the state of the endpoint is already idle, there is not need to abort
// a transfer
if (ep->state != D_EP_IDLE)
{
switch (epAddr)
{
#if SLAB_USB_EP1IN_USED
case EP1IN:
USB_AbortInEp(1);
break;
#endif
#if SLAB_USB_EP2IN_USED
case EP2IN:
USB_AbortInEp(2);
break;
#endif
#if SLAB_USB_EP3IN_USED
case EP3IN:
USB_AbortInEp(3);
break;
#endif
#if SLAB_USB_EP1OUT_USED
case EP1OUT:
USB_AbortOutEp(1);
break;
#endif
#if SLAB_USB_EP2OUT_USED
case EP2OUT:
USB_AbortOutEp(2);
break;
#endif
#if SLAB_USB_EP3OUT_USED
case EP3OUT:
USB_AbortOutEp(3);
break;
#endif
}
// Set the endpoint state to idle and clear out endpoint state variables
ep->state = D_EP_IDLE;
ep->misc.c = 0;
}
}
ENABLE_USB_INTS;
USB_RestoreSfrPage();
return retVal;
}
void USBD_Connect(void)
{
USB_SaveSfrPage();
myUsbDevice.ep0.state = D_EP_IDLE;
USB_EnablePullUpResistor();
USB_EnableTransceiver();
USB_RestoreSfrPage();
}
void USBD_Disconnect(void)
{
USB_SaveSfrPage();
USB_DisablePullUpResistor();
USB_RestoreSfrPage();
}
bool USBD_EpIsBusy(uint8_t epAddr)
{
SI_VARIABLE_SEGMENT_POINTER(ep, USBD_Ep_TypeDef, MEM_MODEL_SEG);
// Verify this is a valid endpoint address
if (epAddr >= SLAB_USB_NUM_EPS_USED)
{
SLAB_ASSERT(false);
return true;
}
ep = GetEp(epAddr);
if (ep->state == D_EP_IDLE)
{
return false;
}
return true;
}
USBD_State_TypeDef USBD_GetUsbState(void)
{
return myUsbDevice.state;
}
int8_t USBD_Init(SI_VARIABLE_SEGMENT_POINTER(p, const USBD_Init_TypeDef, SI_SEG_GENERIC))
{
uint8_t i;
USB_SaveSfrPage();
USB_DisableInts();
// This forces the liner to bring in the contents efm8_usbdint
// It is place here since all users MUST call this function
// for the library to work properly
forceModuleLoad_usbint();
// Zero out the myUsbDevice struct, then initialize all non-zero members
for (i = 0; i < sizeof(myUsbDevice); i++)
{
*((SI_VARIABLE_SEGMENT_POINTER(, uint8_t, MEM_MODEL_SEG))&myUsbDevice + i) = 0;
}
// Get the USB descriptors from p
myUsbDevice.deviceDescriptor = p->deviceDescriptor;
myUsbDevice.configDescriptor = p->configDescriptor;
myUsbDevice.stringDescriptors = p->stringDescriptors;
myUsbDevice.numberOfStrings = p->numberOfStrings;
// Enable USB clock
#if SLAB_USB_FULL_SPEED
USB_SetClockIntOsc();
USB_SelectFullSpeed();
#else
USB_SetClockIntOscDiv8();
USB_SelectLowSpeed();
#endif // SLAB_USB_FULL_SPEED
// Enable or disable VBUS detection
#if SLAB_USB_BUS_POWERED
USB_VbusDetectDisable();
#else
USB_VbusDetectEnable();
#endif
USB_ForceReset();
USB_EnableDeviceInts();
USBD_Connect();
// If VBUS is present, the state should be Default.
// Otherwise, it is Attached.
#if SLAB_USB_BUS_POWERED
myUsbDevice.state = USBD_STATE_DEFAULT;
#else
if (USB_IsVbusOn())
{
myUsbDevice.state = USBD_STATE_DEFAULT;
}
else
{
myUsbDevice.state = USBD_STATE_ATTACHED;
}
#endif
// Only enable USB interrupts when not in polled mode
#if (SLAB_USB_POLLED_MODE == 0)
USB_EnableInts();
#endif
USB_RestoreSfrPage();
USB_DisableInhibit();
return USB_STATUS_OK;
}
int8_t USBD_Read(uint8_t epAddr,
SI_VARIABLE_SEGMENT_POINTER(dat, uint8_t, SI_SEG_GENERIC),
uint16_t byteCount,
bool callback)
{
bool usbIntsEnabled;
SI_VARIABLE_SEGMENT_POINTER(ep, USBD_Ep_TypeDef, MEM_MODEL_SEG);
USB_SaveSfrPage();
// Verify the endpoint address is valid.
switch (epAddr)
{
case EP0:
#if SLAB_USB_EP1OUT_USED
case EP1OUT:
#endif
#if SLAB_USB_EP2OUT_USED
case EP2OUT:
#endif
#if SLAB_USB_EP3OUT_USED
case EP3OUT:
#endif
break;
#if SLAB_USB_EP1IN_USED
case EP1IN:
#endif
#if SLAB_USB_EP2IN_USED
case EP2IN:
#endif
#if SLAB_USB_EP3IN_USED
case EP3IN:
#endif
default:
SLAB_ASSERT(false);
return USB_STATUS_ILLEGAL;
}
// If the device has not been configured, we cannot start a transfer.
if ((epAddr != EP0) && (myUsbDevice.state != USBD_STATE_CONFIGURED))
{
return USB_STATUS_DEVICE_UNCONFIGURED;
}
ep = GetEp(epAddr);
// If the endpoint is not idle, we cannot start a new transfer.
// Return the appropriate error code.
if (ep->state != D_EP_IDLE)
{
if (ep->state == D_EP_STALL)
{
return USB_STATUS_EP_STALLED;
}
else
{
return USB_STATUS_EP_BUSY;
}
}
DISABLE_USB_INTS;
ep->buf = dat;
ep->remaining = byteCount;
ep->state = D_EP_RECEIVING;
ep->misc.bits.callback = callback;
ep->misc.bits.waitForRead = false;
// If isochronous, set the buffer index to 0
#if ((SLAB_USB_EP3OUT_USED) && (SLAB_USB_EP3OUT_TRANSFER_TYPE == USB_EPTYPE_ISOC))
if (epAddr == EP3OUT)
{
myUsbDevice.ep3outIsoIdx = 0;
}
#endif
ENABLE_USB_INTS;
USB_RestoreSfrPage();
return USB_STATUS_OK;
}
#if SLAB_USB_REMOTE_WAKEUP_ENABLED
int8_t USBD_RemoteWakeup(void)
{
// The device must be suspended and Remote Wakeup must have been previously
// configured with a SET_FEATURE (Remote Wakeup) command.
if ((myUsbDevice.state != USBD_STATE_SUSPENDED) ||
(myUsbDevice.remoteWakeupEnabled == false))
{
return USB_STATUS_ILLEGAL;
}
USB_ForceResume();
USBD_RemoteWakeupDelay(); // Application will provide the delay between
// starting and stopping the resume signal.
USB_ClearResume();
return USB_STATUS_OK;
}
#endif // SLAB_USB_REMOTE_WAKEUP_ENABLED
#if SLAB_USB_POLLED_MODE
void USBD_Run(void)
{
usbIrqHandler();
}
#endif // SLAB_USB_POLLED_MODE
int8_t USBD_StallEp(uint8_t epAddr)
{
bool usbIntsEnabled;
USB_SaveSfrPage();
// Verify the endpoint address is valid and not Endpoint 0.
if ((epAddr == EP0) || (epAddr >= SLAB_USB_NUM_EPS_USED))
{
SLAB_ASSERT(false);
return USB_STATUS_ILLEGAL;
}
DISABLE_USB_INTS;
// Halt the appropriate endpoint by sending a stall and setting the endpoint
// state to Halted (D_EP_HALT).
switch (epAddr)
{
#if SLAB_USB_EP1IN_USED
case (EP1IN):
myUsbDevice.ep1in.state = D_EP_HALT;
USB_SetIndex(1);
USB_EpnInStall();
break;
#endif
#if SLAB_USB_EP2IN_USED
case (EP2IN):
myUsbDevice.ep2in.state = D_EP_HALT;
USB_SetIndex(2);
USB_EpnInStall();
break;
#endif
#if SLAB_USB_EP3IN_USED
case (EP3IN):
myUsbDevice.ep3in.state = D_EP_HALT;
USB_SetIndex(3);
USB_EpnInStall();
break;
#endif
#if SLAB_USB_EP1OUT_USED
case (EP1OUT):
myUsbDevice.ep1out.state = D_EP_HALT;
USB_SetIndex(1);
USB_EpnOutStall();
break;
#endif
#if SLAB_USB_EP2OUT_USED
case (EP2OUT):
myUsbDevice.ep2out.state = D_EP_HALT;
USB_SetIndex(2);
USB_EpnOutStall();
break;
#endif
#if SLAB_USB_EP3OUT_USED
case (EP3OUT):
myUsbDevice.ep3out.state = D_EP_HALT;
USB_SetIndex(3);
USB_EpnOutStall();
break;
#endif
}
ENABLE_USB_INTS;
USB_RestoreSfrPage();
return USB_STATUS_OK;
}
void USBD_Stop(void)
{
USB_DisableInts();
USBD_Disconnect();
USBD_SetUsbState(USBD_STATE_NONE);
}
void USBD_Suspend(void)
{
#if (!(SLAB_USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_FASTWAKE))
uint8_t i;
#endif
bool regulatorEnabled, prefetchEnabled;
#if SLAB_USB_REMOTE_WAKEUP_ENABLED
bool remoteWakeup = false;
#endif
USB_SaveSfrPage();
// If the USB_PWRSAVE_MODE_ONVBUSOFF is enabled, we can enter suspend if VBUS
// is not present even if the USB has not detected a suspend event.
#if ((!(SLAB_USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF)) || \
(SLAB_USB_BUS_POWERED))
if (USB_IsSuspended() == true)
#else
if ((USB_IsSuspended() == true) || (USB_IsVbusOn() == false))
#endif
{
USB_SuspendTransceiver();
#if SLAB_USB_FULL_SPEED
USB_SetSuspendClock();
#endif
// Get the state of the prefetch engine enable bit and disable the prefetch
// engine
prefetchEnabled = USB_IsPrefetchEnabled();
USB_DisablePrefetch();
// Get the state of the internal regulator before suspending it.
if (USB_IsRegulatorEnabled() == true)
{
regulatorEnabled = true;
#if (SLAB_USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_FASTWAKE)
USB_SuspendRegulatorFastWake();
#else
USB_SuspendRegulator();
// Wait at least 12 clock instructions before halting the internal oscillator
for (i = 0; i < 3; i++)
{
}
#endif
}
else
{
regulatorEnabled = false;
}
do
{
USB_SuspendOscillator();
// When we arrive here, the device has waked from suspend mode.
#if SLAB_USB_REMOTE_WAKEUP_ENABLED
// If remote wakeup is enabled, query the application if the remote
// wakeup event occurred. If so, exit USBD_Suspend().
if (USB_IsSuspended() == true)
{
remoteWakeup = USBD_RemoteWakeupCb();
if (remoteWakeup == true)
{
break;
}
}
#endif
#if ((!(SLAB_USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF)) && \
(SLAB_USB_BUS_POWERED == 0))
// If the USB_PWRSAVE_MODE_ONVBUSOFF mode is disabled, VBUS has been
// removed, so exit USBD_Suspend().
if (USB_IsVbusOn() == false)
{
break;
}
#endif
#if ((!(SLAB_USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF)) || \
(SLAB_USB_BUS_POWERED))
} while (USB_IsSuspended() == true);
#else
} while ((USB_IsSuspended() == true) || (USB_IsVbusOn() == false));
#endif
// Restore the internal regulator
if (regulatorEnabled == true)
{
USB_UnsuspendRegulator();
}
// Restore the prefetch engine
if (prefetchEnabled == true)
{
USB_EnablePrefetch();
}
#if SLAB_USB_FULL_SPEED
// Restore the clock
USB_SetNormalClock();
#endif
USB_EnableTransceiver();
#if SLAB_USB_REMOTE_WAKEUP_ENABLED
// If the device woke from suspend due to a remote wakeup source, call
// USBD_RemoteWakeup() here to wake up the host.
if (remoteWakeup == true)
{
// Wake up the host
if (USBD_RemoteWakeup() == USB_STATUS_OK)
{
// If the remote wakeup succeeded, transition out of USB suspend state
USBD_SetUsbState(myUsbDevice.savedState);
}
}
#endif
}
USB_RestoreSfrPage();
}
int8_t USBD_UnStallEp(uint8_t epAddr)
{
bool usbIntsEnabled;
USB_SaveSfrPage();
// Verify the endpoint address is valid and not Endpoint 0.
if ((epAddr == EP0) || (epAddr >= SLAB_USB_NUM_EPS_USED))
{
SLAB_ASSERT(false);
return USB_STATUS_ILLEGAL;
}
else
{
DISABLE_USB_INTS;
// End the stall condition and set the endpoint state to idle.
switch (epAddr)
{
#if SLAB_USB_EP1IN_USED
case (EP1IN):
myUsbDevice.ep1in.state = D_EP_IDLE;
USB_SetIndex(1);
USB_EpnInEndStall();
break;
#endif
#if SLAB_USB_EP2IN_USED
case (EP2IN):
myUsbDevice.ep2in.state = D_EP_IDLE;
USB_SetIndex(2);
USB_EpnInEndStall();
break;
#endif
#if SLAB_USB_EP3IN_USED
case (EP3IN):
myUsbDevice.ep3in.state = D_EP_IDLE;
USB_SetIndex(3);
USB_EpnInEndStall();
break;
#endif
#if SLAB_USB_EP1OUT_USED
case (EP1OUT):
myUsbDevice.ep1out.state = D_EP_IDLE;
USB_SetIndex(1);
USB_EpnOutEndStall();
break;
#endif
#if SLAB_USB_EP2OUT_USED
case (EP2OUT):
myUsbDevice.ep2out.state = D_EP_IDLE;
USB_SetIndex(2);
USB_EpnOutEndStall();
break;
#endif
#if SLAB_USB_EP3OUT_USED
case (EP3OUT):
myUsbDevice.ep3out.state = D_EP_IDLE;
USB_SetIndex(3);
USB_EpnOutEndStall();
break;
#endif
}
ENABLE_USB_INTS;
USB_RestoreSfrPage();
}
return USB_STATUS_OK;
}
int8_t USBD_Write(uint8_t epAddr,
SI_VARIABLE_SEGMENT_POINTER(dat, uint8_t, SI_SEG_GENERIC),
uint16_t byteCount,
bool callback)
{
bool usbIntsEnabled;
SI_VARIABLE_SEGMENT_POINTER(ep, USBD_Ep_TypeDef, MEM_MODEL_SEG);
USB_SaveSfrPage();
// Verify the endpoint address is valid.
switch (epAddr)
{
case EP0:
#if SLAB_USB_EP1IN_USED
case EP1IN:
#endif
#if SLAB_USB_EP2IN_USED
case EP2IN:
#endif
#if SLAB_USB_EP3IN_USED
case EP3IN:
#endif
break;
#if SLAB_USB_EP1OUT_USED
case EP1OUT:
#endif
#if SLAB_USB_EP2OUT_USED
case EP2OUT:
#endif
#if SLAB_USB_EP3OUT_USED
case EP3OUT:
#endif
default:
SLAB_ASSERT(false);
return USB_STATUS_ILLEGAL;
}
// If the device is not configured and it is not Endpoint 0, we cannot begin
// a transfer.
if ((epAddr != EP0) && (myUsbDevice.state != USBD_STATE_CONFIGURED))
{
return USB_STATUS_DEVICE_UNCONFIGURED;
}
ep = GetEp(epAddr);
// If the endpoint is not idle, we cannot start a new transfer.
// Return the appropriate error code.
if (ep->state != D_EP_IDLE)
{
if (ep->state == D_EP_STALL)
{
return USB_STATUS_EP_STALLED;
}
else
{
return USB_STATUS_EP_BUSY;
}
}
DISABLE_USB_INTS;
ep->buf = dat;
ep->remaining = byteCount;
ep->state = D_EP_TRANSMITTING;
ep->misc.bits.callback = callback;
switch (epAddr)
{
// For Endpoint 0, set the inPacketPending flag to true. The USB handler
// will see this on the next SOF and begin the transfer.
case (EP0):
myUsbDevice.ep0.misc.bits.inPacketPending = true;
break;
// For data endpoints, we will call USB_WriteFIFO here to reduce latency
// between the call to USBD_Write() and the first packet being sent.
#if SLAB_USB_EP1IN_USED
case (EP1IN):
USB_WriteFIFO(1,
(byteCount > SLAB_USB_EP1IN_MAX_PACKET_SIZE) ? SLAB_USB_EP1IN_MAX_PACKET_SIZE : byteCount,
myUsbDevice.ep1in.buf,
true);
break;
#endif // SLAB_USB_EP1IN_USED
#if SLAB_USB_EP2IN_USED
case (EP2IN):
USB_WriteFIFO(2,
(byteCount > SLAB_USB_EP2IN_MAX_PACKET_SIZE) ? SLAB_USB_EP2IN_MAX_PACKET_SIZE : byteCount,
myUsbDevice.ep2in.buf,
true);
break;
#endif // SLAB_USB_EP2IN_USED
#if SLAB_USB_EP3IN_USED
case (EP3IN):
#if ((SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_BULK) || (SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_INTR))
USB_WriteFIFO(3,
(byteCount > SLAB_USB_EP3IN_MAX_PACKET_SIZE) ? SLAB_USB_EP3IN_MAX_PACKET_SIZE : byteCount,
myUsbDevice.ep3in.buf,
true);
#elif (SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_ISOC)
myUsbDevice.ep3in.misc.bits.inPacketPending = true;
myUsbDevice.ep3inIsoIdx = 0;
#endif
break;
#endif // SLAB_USB_EP3IN_USED
}
ENABLE_USB_INTS;
USB_RestoreSfrPage();
return USB_STATUS_OK;
}
// -----------------------------------------------------------------------------
// UtilityFunctions
void USBD_SetUsbState(USBD_State_TypeDef newState)
{
#if (SLAB_USB_SUPPORT_ALT_INTERFACES)
uint8_t i;
#endif
USBD_State_TypeDef currentState;
currentState = myUsbDevice.state;
// If the device is un-configuring, disable the data endpoints and clear out
// alternate interface settings
if ((currentState >= USBD_STATE_SUSPENDED)
&& (newState < USBD_STATE_SUSPENDED))
{
USBD_AbortAllTransfers();
#if (SLAB_USB_SUPPORT_ALT_INTERFACES)
for (i = 0; i < SLAB_USB_NUM_INTERFACES; i++)
{
myUsbDevice.interfaceAltSetting[i] = 0;
}
#endif
}
if (newState == USBD_STATE_SUSPENDED)
{
myUsbDevice.savedState = currentState;
}
myUsbDevice.state = newState;
#if SLAB_USB_STATE_CHANGE_CB
if (currentState != newState)
{
USBD_DeviceStateChangeCb(currentState, newState);
}
#endif
}

View File

@@ -0,0 +1,870 @@
/**************************************************************************//**
* Copyright (c) 2015 by Silicon Laboratories Inc. All rights reserved.
*
* http://developer.silabs.com/legal/version/v11/Silicon_Labs_Software_License_Agreement.txt
*****************************************************************************/
#include "si_toolchain.h"
#include "efm8_usb.h"
#include <stdint.h>
#include <endian.h>
// -----------------------------------------------------------------------------
// Function Prototypes
static USB_Status_TypeDef ClearFeature(void);
static USB_Status_TypeDef GetConfiguration(void);
static USB_Status_TypeDef GetDescriptor(void);
static USB_Status_TypeDef GetInterface(void);
static USB_Status_TypeDef GetStatus(void);
static USB_Status_TypeDef SetAddress(void);
static USB_Status_TypeDef SetConfiguration(void);
static USB_Status_TypeDef SetFeature(void);
static USB_Status_TypeDef SetInterface(void);
static void USBD_ActivateAllEps(bool forceIdle);
static void EP0_Write(SI_VARIABLE_SEGMENT_POINTER(dat, uint8_t, SI_SEG_GENERIC), uint16_t numBytes);
// -----------------------------------------------------------------------------
// Global Variables
extern SI_SEGMENT_VARIABLE(myUsbDevice, USBD_Device_TypeDef, MEM_MODEL_SEG);
const SI_SEGMENT_VARIABLE(txZero[2], uint8_t, SI_SEG_CODE);
// -----------------------------------------------------------------------------
// Static Global Variables
static uint16_t pStatus;
// -----------------------------------------------------------------------------
// Chapter 9 Functions
/***************************************************************************//**
* @brief Processes Standard Request (Chapter 9 Command)
* @return Status of request (type @ref USB_Status_TypeDef)
* @note This function takes no parameters, but it uses the setup command
* stored in @ref myUsbDevice.setup.
******************************************************************************/
USB_Status_TypeDef USBDCH9_SetupCmd(void)
{
USB_Status_TypeDef status = USB_STATUS_OK;
switch (myUsbDevice.setup.bRequest)
{
case GET_STATUS:
status = GetStatus();
break;
case CLEAR_FEATURE:
status = ClearFeature();
break;
case SET_FEATURE:
status = SetFeature();
break;
case SET_ADDRESS:
status = SetAddress();
break;
case GET_DESCRIPTOR:
status = GetDescriptor();
break;
case GET_CONFIGURATION:
status = GetConfiguration();
break;
case SET_CONFIGURATION:
status = SetConfiguration();
break;
case GET_INTERFACE:
status = GetInterface();
break;
case SET_INTERFACE:
status = SetInterface();
break;
default:
status = USB_STATUS_REQ_ERR;
break;
}
return status;
}
/***************************************************************************//**
* @brief Clears the requested feature
* @details Supports CLEAR_FEATURE for Remote Wakeup and Endpoint Halt
* @return Status of request (type @ref USB_Status_TypeDef)
* @note This function takes no parameters, but it uses the setup command
* stored in @ref myUsbDevice.setup.
******************************************************************************/
static USB_Status_TypeDef ClearFeature(void)
{
USB_Status_TypeDef retVal = USB_STATUS_REQ_ERR;
if (myUsbDevice.setup.wLength == 0)
{
switch (myUsbDevice.setup.bmRequestType.Recipient)
{
#if SLAB_USB_REMOTE_WAKEUP_ENABLED
case USB_SETUP_RECIPIENT_DEVICE:
if ((myUsbDevice.setup.wIndex == 0)
&& (myUsbDevice.setup.wValue == USB_FEATURE_DEVICE_REMOTE_WAKEUP)
&& (myUsbDevice.state >= USBD_STATE_ADDRESSED))
{
// Remote wakeup feature clear
myUsbDevice.remoteWakeupEnabled = false;
retVal = USB_STATUS_OK;
}
break;
#endif // SLAB_USB_REMOTE_WAKEUP_ENABLED
case USB_SETUP_RECIPIENT_ENDPOINT:
if (myUsbDevice.setup.wValue == USB_FEATURE_ENDPOINT_HALT)
{
// Device does not support halting endpoint 0, but do not return
// an error as this is a valid request
if (((myUsbDevice.setup.wIndex & ~USB_EP_DIR_IN) == 0)
&& (myUsbDevice.state >= USBD_STATE_ADDRESSED))
{
retVal = USB_STATUS_OK;
}
else if (((myUsbDevice.setup.wIndex & ~USB_SETUP_DIR_D2H) < SLAB_USB_NUM_EPS_USED)
&& (myUsbDevice.state == USBD_STATE_CONFIGURED))
{
retVal = USB_STATUS_OK;
USB_SetIndex((myUsbDevice.setup.wIndex & 0xFF) & ~USB_SETUP_DIR_D2H);
#if (SLAB_USB_EP1IN_USED || SLAB_USB_EP2IN_USED || SLAB_USB_EP3IN_USED)
if ((myUsbDevice.setup.wIndex & 0xFF) & USB_EP_DIR_IN)
{
USB_EpnInEndStallAndClearDataToggle();
}
#endif
#if (SLAB_USB_EP1OUT_USED || SLAB_USB_EP2OUT_USED || SLAB_USB_EP3OUT_USED)
if (((myUsbDevice.setup.wIndex & 0xFF) & USB_EP_DIR_IN) == 0)
{
USB_EpnOutEndStallAndClearDataToggle();
}
#endif
switch (myUsbDevice.setup.wIndex & 0xFF)
{
#if SLAB_USB_EP1OUT_USED
case (USB_EP_DIR_OUT | 1):
if (myUsbDevice.ep1out.state != D_EP_RECEIVING)
{
myUsbDevice.ep1out.state = D_EP_IDLE;
}
break;
#endif
#if SLAB_USB_EP2OUT_USED
case (USB_EP_DIR_OUT | 2):
if (myUsbDevice.ep2out.state != D_EP_RECEIVING)
{
myUsbDevice.ep2out.state = D_EP_IDLE;
}
break;
#endif
#if SLAB_USB_EP3OUT_USED
case (USB_EP_DIR_OUT | 3):
if (myUsbDevice.ep3out.state != D_EP_RECEIVING)
{
myUsbDevice.ep3out.state = D_EP_IDLE;
}
break;
#endif
#if SLAB_USB_EP1IN_USED
case (USB_EP_DIR_IN | 1):
if (myUsbDevice.ep1in.state != D_EP_TRANSMITTING)
{
myUsbDevice.ep1in.state = D_EP_IDLE;
}
break;
#endif
#if SLAB_USB_EP2IN_USED
case (USB_EP_DIR_IN | 2):
if (myUsbDevice.ep2in.state != D_EP_TRANSMITTING)
{
myUsbDevice.ep2in.state = D_EP_IDLE;
}
break;
#endif
#if SLAB_USB_EP3IN_USED
case (USB_EP_DIR_IN | 3):
if (myUsbDevice.ep3in.state != D_EP_TRANSMITTING)
{
myUsbDevice.ep3in.state = D_EP_IDLE;
}
break;
#endif
}
}
}
}
}
return retVal;
}
/***************************************************************************//**
* @brief Gets the current configuration value
* @details Zero means the device is not configured, a non-zero value
* is the configuration value of the configured device.
* @return Status of request (type @ref USB_Status_TypeDef)
* @note This function takes no parameters, but it uses the setup command
* stored in @ref myUsbDevice.setup.
******************************************************************************/
static USB_Status_TypeDef GetConfiguration(void)
{
USB_Status_TypeDef retVal = USB_STATUS_REQ_ERR;
if ((myUsbDevice.setup.wIndex == 0)
&& (myUsbDevice.setup.wValue == 0)
&& (myUsbDevice.setup.wLength == 1)
&& (myUsbDevice.setup.bmRequestType.Direction == USB_SETUP_DIR_IN)
&& (myUsbDevice.setup.bmRequestType.Recipient == USB_SETUP_RECIPIENT_DEVICE))
{
if (myUsbDevice.state == USBD_STATE_ADDRESSED)
{
EP0_Write((SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_GENERIC))txZero, 1);
retVal = USB_STATUS_OK;
}
else if (myUsbDevice.state == USBD_STATE_CONFIGURED)
{
EP0_Write(&myUsbDevice.configurationValue, 1);
retVal = USB_STATUS_OK;
}
}
return retVal;
}
/***************************************************************************//**
* @brief Sends the requested USB Descriptor
* @details Supports single or multiple languages (configured by
* @ref SLAB_USB_NUM_LANGUAGES).
* @return Status of request (type @ref USB_Status_TypeDef)
* @note This function takes no parameters, but it uses the setup command
* stored in @ref myUsbDevice.setup.
******************************************************************************/
static USB_Status_TypeDef GetDescriptor(void)
{
#if (SLAB_USB_NUM_LANGUAGES > 1)
bool langSupported;
uint8_t lang;
#endif
uint8_t index;
uint16_t length = 0;
SI_VARIABLE_SEGMENT_POINTER(dat, uint8_t, SI_SEG_GENERIC);
USB_Status_TypeDef retVal = USB_STATUS_REQ_ERR;
if (*((uint8_t *)&myUsbDevice.setup.bmRequestType) ==
(USB_SETUP_DIR_D2H | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_DEVICE))
{
index = myUsbDevice.setup.wValue & 0xFF;
switch (myUsbDevice.setup.wValue >> 8)
{
case USB_DEVICE_DESCRIPTOR:
if (index != 0)
{
break;
}
dat = (SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_GENERIC))myUsbDevice.deviceDescriptor;
length = myUsbDevice.deviceDescriptor->bLength;
break;
case USB_CONFIG_DESCRIPTOR:
if (index != 0)
{
break;
}
dat = (SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_GENERIC))myUsbDevice.configDescriptor;
length = le16toh(myUsbDevice.configDescriptor->wTotalLength);
break;
case USB_STRING_DESCRIPTOR:
#if (SLAB_USB_NUM_LANGUAGES == 1)
dat = (SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_GENERIC))myUsbDevice.stringDescriptors[index];
// Index 0 is the language string. If SLAB_USB_NUM_LANGUAGES == 1, we
// know the length will be 4 and the format will be UTF16LE.
if (index == 0)
{
length = 4;
myUsbDevice.ep0String.encoding.type = USB_STRING_DESCRIPTOR_UTF16LE;
}
// Otherwise, verify the language is correct (either the value set as
// SLAB_USB_LANGUAGE in usbconfig.h, or 0).
else if ((myUsbDevice.setup.wIndex == 0) || (myUsbDevice.setup.wIndex == SLAB_USB_LANGUAGE))
{
// Verify the index is valid
if (index < myUsbDevice.numberOfStrings)
{
length = *(dat + USB_STRING_DESCRIPTOR_LENGTH);
myUsbDevice.ep0String.encoding.type = *(dat + USB_STRING_DESCRIPTOR_ENCODING);
dat += USB_STRING_DESCRIPTOR_LENGTH;
myUsbDevice.ep0String.encoding.init = true;
}
}
#elif (SLAB_USB_NUM_LANGUAGES > 1)
langSupported = false;
// Index 0 is the language.
if (index == 0)
{
dat = ((SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_GENERIC))myUsbDevice.stringDescriptors->languageArray[0][index]);
length = *((uint8_t *)dat);
myUsbDevice.ep0String.encoding.type = USB_STRING_DESCRIPTOR_UTF16LE;
}
else
{
// Otherwise, verify the language is one of the supported languages or 0.
for (lang = 0; lang < SLAB_USB_NUM_LANGUAGES; lang++)
{
if ((myUsbDevice.stringDescriptors->languageIDs[lang] == myUsbDevice.setup.wIndex)
|| (myUsbDevice.stringDescriptors->languageIDs[lang] == 0))
{
langSupported = true;
break;
}
}
if ((langSupported == true) && (index < myUsbDevice.numberOfStrings))
{
dat = ((SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_GENERIC))myUsbDevice.stringDescriptors->languageArray[lang][index]);
length = *(dat + USB_STRING_DESCRIPTOR_LENGTH);
myUsbDevice.ep0String.encoding.type = *(dat + USB_STRING_DESCRIPTOR_ENCODING);
dat += USB_STRING_DESCRIPTOR_LENGTH;
if (myUsbDevice.ep0String.encoding.type == USB_STRING_DESCRIPTOR_UTF16LE_PACKED)
{
myUsbDevice.ep0String.encoding.init = true;
}
else
{
myUsbDevice.ep0String.encoding.init = false;
}
}
}
#endif // ( SLAB_USB_NUM_LANGUAGES == 1 )
}
// If there is a descriptor to send, get the proper length, then call
// EP0_Write() to send.
if (length)
{
if (length > myUsbDevice.setup.wLength)
{
length = myUsbDevice.setup.wLength;
}
EP0_Write(dat, length);
retVal = USB_STATUS_OK;
}
}
return retVal;
}
/***************************************************************************//**
* @brief Sends the current interface alternate setting
* @details Sends 0x0000 if alternate interfaces are not supported.
* @return Status of request (type @ref USB_Status_TypeDef)
* @note This function takes no parameters, but it uses the setup command
* stored in @ref myUsbDevice.setup.
******************************************************************************/
static USB_Status_TypeDef GetInterface(void)
{
uint16_t interface = myUsbDevice.setup.wIndex;
USB_Status_TypeDef retVal = USB_STATUS_REQ_ERR;
if ((interface < SLAB_USB_NUM_INTERFACES)
&& (myUsbDevice.setup.wLength == 1)
&& (myUsbDevice.setup.wValue == 0)
&& (*((uint8_t *)&myUsbDevice.setup.bmRequestType) ==
(USB_SETUP_DIR_D2H | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_INTERFACE)))
{
if (myUsbDevice.state == USBD_STATE_CONFIGURED)
{
#if (SLAB_USB_SUPPORT_ALT_INTERFACES)
// Return the alternate setting for the specified interface
EP0_Write((SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_GENERIC))&myUsbDevice.interfaceAltSetting[interface], 1);
#else
// Alternate interfaces are not supported, so return 0x0000.
EP0_Write((SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_GENERIC))&txZero, 1);
#endif
retVal = USB_STATUS_OK;
}
}
return retVal;
}
/***************************************************************************//**
* @brief Sends the requested Remote Wakeup, Self-Powered, or
* Endpoint Status
* @return Status of request (type @ref USB_Status_TypeDef)
* @note This function takes no parameters, but it uses the setup command
* stored in @ref myUsbDevice.setup.
******************************************************************************/
static USB_Status_TypeDef GetStatus(void)
{
USB_Status_TypeDef retVal = USB_STATUS_REQ_ERR;
if ((myUsbDevice.setup.wLength == 2)
&& (myUsbDevice.setup.wValue == 0)
&& (myUsbDevice.setup.bmRequestType.Direction == USB_SETUP_DIR_IN)
&& (myUsbDevice.state >= USBD_STATE_ADDRESSED))
{
pStatus = htole16(0); // Default return value is 0x0000
switch (myUsbDevice.setup.bmRequestType.Recipient)
{
case USB_SETUP_RECIPIENT_DEVICE:
if (myUsbDevice.setup.wIndex == 0)
{
#if SLAB_USB_REMOTE_WAKEUP_ENABLED
// Remote wakeup feature status
if (myUsbDevice.remoteWakeupEnabled)
{
pStatus |= htole16(REMOTE_WAKEUP_ENABLED);
}
#endif // SLAB_USB_REMOTE_WAKEUP_ENABLED
#if SLAB_USB_IS_SELF_POWERED_CB
// Current self/bus power status
if (USBD_IsSelfPoweredCb())
{
pStatus |= htole16(DEVICE_IS_SELFPOWERED);
}
#elif (SLAB_USB_BUS_POWERED == 0)
pStatus |= htole16(DEVICE_IS_SELFPOWERED);
#endif // SLAB_USB_IS_SELF_POWERED_CB
retVal = USB_STATUS_OK;
}
break;
case USB_SETUP_RECIPIENT_INTERFACE:
if (myUsbDevice.setup.wIndex < SLAB_USB_NUM_INTERFACES)
{
retVal = USB_STATUS_OK;
}
break;
case USB_SETUP_RECIPIENT_ENDPOINT:
// Device does not support halting endpoint 0, but do not give
// an error as this is a valid request
if (((myUsbDevice.setup.wIndex & ~USB_EP_DIR_IN) == 0)
&& (myUsbDevice.state == USBD_STATE_ADDRESSED))
{
retVal = USB_STATUS_OK;
}
else if (myUsbDevice.state == USBD_STATE_CONFIGURED)
{
switch (myUsbDevice.setup.wIndex & 0xFF)
{
#if SLAB_USB_EP1OUT_USED
case (USB_EP_DIR_OUT | 1):
if (myUsbDevice.ep1out.state == D_EP_HALT)
{
pStatus = htole16(1);
}
retVal = USB_STATUS_OK;
break;
#endif
#if SLAB_USB_EP2OUT_USED
case (USB_EP_DIR_OUT | 2):
if (myUsbDevice.ep2out.state == D_EP_HALT)
{
pStatus = htole16(1);
}
retVal = USB_STATUS_OK;
break;
#endif
#if SLAB_USB_EP3OUT_USED
case (USB_EP_DIR_OUT | 3):
if (myUsbDevice.ep3out.state == D_EP_HALT)
{
pStatus = htole16(1);
}
retVal = USB_STATUS_OK;
break;
#endif
#if SLAB_USB_EP1IN_USED
case (USB_EP_DIR_IN | 1):
if (myUsbDevice.ep1in.state == D_EP_HALT)
{
pStatus = htole16(1);
}
retVal = USB_STATUS_OK;
break;
#endif
#if SLAB_USB_EP2IN_USED
case (USB_EP_DIR_IN | 2):
if (myUsbDevice.ep2in.state == D_EP_HALT)
{
pStatus = htole16(1);
}
retVal = USB_STATUS_OK;
break;
#endif
#if SLAB_USB_EP3IN_USED
case (USB_EP_DIR_IN | 3):
if (myUsbDevice.ep3in.state == D_EP_HALT)
{
pStatus = htole16(1);
}
retVal = USB_STATUS_OK;
break;
#endif
}
}
break;
}
// If the command was valid, send the requested status.
if (retVal == USB_STATUS_OK)
{
EP0_Write((SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_GENERIC))&pStatus, 2);
}
}
return retVal;
}
/***************************************************************************//**
* @brief Sets the Address
* @return Status of request (type @ref USB_Status_TypeDef)
* @note This function takes no parameters, but it uses the setup command
* stored in @ref myUsbDevice.setup.
******************************************************************************/
static USB_Status_TypeDef SetAddress(void)
{
USB_Status_TypeDef retVal = USB_STATUS_REQ_ERR;
if ((myUsbDevice.setup.wValue < 128)
&& (myUsbDevice.setup.wLength == 0)
&& (myUsbDevice.setup.bmRequestType.Recipient == USB_SETUP_RECIPIENT_DEVICE)
&& (myUsbDevice.setup.wIndex == 0))
{
// If the device is in the Default state and the address is non-zero, put
// the device in the Addressed state.
if (myUsbDevice.state == USBD_STATE_DEFAULT)
{
if (myUsbDevice.setup.wValue != 0)
{
USBD_SetUsbState(USBD_STATE_ADDRESSED);
}
retVal = USB_STATUS_OK;
}
// If the device is already addressed and the address is zero, put the
// device in the Default state.
else if (myUsbDevice.state == USBD_STATE_ADDRESSED)
{
if (myUsbDevice.setup.wValue == 0)
{
USBD_SetUsbState(USBD_STATE_DEFAULT);
}
retVal = USB_STATUS_OK;
}
// Set the new address if the request was valid.
if (retVal == USB_STATUS_OK)
{
USB_SetAddress(myUsbDevice.setup.wValue);
}
}
return retVal;
}
/***************************************************************************//**
* @brief Sets the Configuration
* @return Status of request (type @ref USB_Status_TypeDef)
* @note This function takes no parameters, but it uses the setup command
* stored in @ref myUsbDevice.setup.
******************************************************************************/
static USB_Status_TypeDef SetConfiguration(void)
{
USB_Status_TypeDef retVal = USB_STATUS_REQ_ERR;
if (((myUsbDevice.setup.wValue >> 8) == 0)
&& (myUsbDevice.setup.bmRequestType.Recipient == USB_SETUP_RECIPIENT_DEVICE)
&& (myUsbDevice.setup.wLength == 0)
&& (myUsbDevice.setup.wIndex == 0))
{
// If the device is in the Addressed state and a valid Configuration value
// was sent, enter the Configured state.
if (myUsbDevice.state == USBD_STATE_ADDRESSED)
{
if ((myUsbDevice.setup.wValue == 0)
|| (myUsbDevice.setup.wValue == myUsbDevice.configDescriptor->bConfigurationValue))
{
myUsbDevice.configurationValue = myUsbDevice.setup.wValue;
if (myUsbDevice.setup.wValue == myUsbDevice.configDescriptor->bConfigurationValue)
{
USBD_ActivateAllEps(true);
USBD_SetUsbState(USBD_STATE_CONFIGURED);
}
retVal = USB_STATUS_OK;
}
}
// If the device is in the Configured state and Configuration zero is sent,
// abort all transfer and enter the Addressed state.
else if (myUsbDevice.state == USBD_STATE_CONFIGURED)
{
if ((myUsbDevice.setup.wValue == 0)
|| (myUsbDevice.setup.wValue == myUsbDevice.configDescriptor->bConfigurationValue))
{
myUsbDevice.configurationValue = myUsbDevice.setup.wValue;
if (myUsbDevice.setup.wValue == 0)
{
USBD_SetUsbState(USBD_STATE_ADDRESSED);
USBD_AbortAllTransfers();
}
else
{
// Reenable device endpoints, will reset data toggles
USBD_ActivateAllEps(false);
}
retVal = USB_STATUS_OK;
}
}
}
return retVal;
}
/***************************************************************************//**
* @brief Sets the Remote Wakeup or Endpoint Halt Feature
* @return Status of request (type @ref USB_Status_TypeDef)
* @note This function takes no parameters, but it uses the setup command
* stored in @ref myUsbDevice.setup.
******************************************************************************/
static USB_Status_TypeDef SetFeature(void)
{
USB_Status_TypeDef retVal = USB_STATUS_REQ_ERR;
if (myUsbDevice.setup.wLength == 0)
{
switch (myUsbDevice.setup.bmRequestType.Recipient)
{
#if SLAB_USB_REMOTE_WAKEUP_ENABLED
case USB_SETUP_RECIPIENT_DEVICE:
if ((myUsbDevice.setup.wIndex == 0) // ITF no. 0
&& (myUsbDevice.setup.wValue == USB_FEATURE_DEVICE_REMOTE_WAKEUP)
&& (myUsbDevice.state == USBD_STATE_CONFIGURED))
{
myUsbDevice.remoteWakeupEnabled = true;
retVal = USB_STATUS_OK;
}
break;
#endif // SLAB_USB_REMOTE_WAKEUP_ENABLED
case USB_SETUP_RECIPIENT_ENDPOINT:
// Device does not support halting endpoint 0, but do not return
// an error as this is a valid request
if (((myUsbDevice.setup.wIndex & ~USB_EP_DIR_IN) == 0)
&& (myUsbDevice.state >= USBD_STATE_ADDRESSED))
{
retVal = USB_STATUS_OK;
}
else if ((((myUsbDevice.setup.wIndex) & ~USB_SETUP_DIR_D2H) < SLAB_USB_NUM_EPS_USED)
&& (myUsbDevice.setup.wValue == USB_FEATURE_ENDPOINT_HALT)
&& (myUsbDevice.state == USBD_STATE_CONFIGURED))
{
retVal = USB_STATUS_OK;
USB_SetIndex((myUsbDevice.setup.wIndex & 0xFF) & ~USB_SETUP_DIR_D2H);
// Enable Stalls on the specified endpoint.
#if (SLAB_USB_EP1IN_USED || SLAB_USB_EP2IN_USED || SLAB_USB_EP3IN_USED)
if ((myUsbDevice.setup.wIndex & 0xFF) & USB_EP_DIR_IN)
{
USB_EpnInStall();
}
#endif
#if (SLAB_USB_EP1OUT_USED || SLAB_USB_EP2OUT_USED || SLAB_USB_EP3OUT_USED)
if (((myUsbDevice.setup.wIndex & 0xFF) & USB_EP_DIR_IN) == 0)
{
USB_EpnOutStall();
}
#endif
// Put the specified endpoint in the Halted state.
switch (myUsbDevice.setup.wIndex & 0xFF)
{
#if SLAB_USB_EP1OUT_USED
case (USB_EP_DIR_OUT | 1):
myUsbDevice.ep1out.state = D_EP_HALT;
break;
#endif
#if SLAB_USB_EP2OUT_USED
case (USB_EP_DIR_OUT | 2):
myUsbDevice.ep2out.state = D_EP_HALT;
break;
#endif
#if SLAB_USB_EP3OUT_USED
case (USB_EP_DIR_OUT | 3):
myUsbDevice.ep3out.state = D_EP_HALT;
break;
#endif
#if SLAB_USB_EP1IN_USED
case (USB_EP_DIR_IN | 1):
myUsbDevice.ep1in.state = D_EP_HALT;
break;
#endif
#if SLAB_USB_EP2IN_USED
case (USB_EP_DIR_IN | 2):
myUsbDevice.ep2in.state = D_EP_HALT;
break;
#endif
#if SLAB_USB_EP3IN_USED
case (USB_EP_DIR_IN | 3):
myUsbDevice.ep3in.state = D_EP_HALT;
break;
#endif
}
}
}
}
return retVal;
}
/***************************************************************************//**
* @brief Sets the Interface and Alternate Interface (if supported)
* @return Status of request (type @ref USB_Status_TypeDef)
* @note This function takes no parameters, but it uses the setup command
* stored in @ref myUsbDevice.setup.
******************************************************************************/
static USB_Status_TypeDef SetInterface(void)
{
USB_Status_TypeDef retVal = USB_STATUS_REQ_ERR;
uint8_t interface = (uint8_t)myUsbDevice.setup.wIndex;
uint8_t altSetting = (uint8_t)myUsbDevice.setup.wValue;
if ((interface < SLAB_USB_NUM_INTERFACES)
&& (myUsbDevice.state == USBD_STATE_CONFIGURED)
&& (myUsbDevice.setup.wLength == 0)
#if (SLAB_USB_SUPPORT_ALT_INTERFACES == 0)
&& (altSetting == 0)
#endif
&& (myUsbDevice.setup.bmRequestType.Recipient == USB_SETUP_RECIPIENT_INTERFACE))
{
#if (SLAB_USB_SUPPORT_ALT_INTERFACES)
if (USBD_SetInterfaceCb(interface, altSetting) == USB_STATUS_OK)
{
myUsbDevice.interfaceAltSetting[interface] = altSetting;
retVal = USB_STATUS_OK;
}
#else
#if (SLAB_USB_NUM_INTERFACES == 1)
// Reset data toggles on EP's
USBD_ActivateAllEps(false);
#endif // ( SLAB_USB_NUM_INTERFACES == 1 )
retVal = USB_STATUS_OK;
#endif // ( SLAB_USB_SUPPORT_ALT_INTERFACES )
}
return retVal;
}
// -----------------------------------------------------------------------------
// Utility Functions
/***************************************************************************//**
* @brief Enables all endpoints for data transfers
* @return Status of request (type @ref USB_Status_TypeDef)
* @note This function takes no parameters, but it uses the setup command
* stored in @ref myUsbDevice.setup.
******************************************************************************/
static void USBD_ActivateAllEps(bool forceIdle)
{
if (forceIdle == true)
{
#if SLAB_USB_EP1IN_USED
myUsbDevice.ep1in.state = D_EP_IDLE;
#endif
#if SLAB_USB_EP2IN_USED
myUsbDevice.ep2in.state = D_EP_IDLE;
#endif
#if SLAB_USB_EP3IN_USED
myUsbDevice.ep3in.state = D_EP_IDLE;
#endif
#if SLAB_USB_EP1OUT_USED
myUsbDevice.ep1out.state = D_EP_IDLE;
#endif
#if SLAB_USB_EP2OUT_USED
myUsbDevice.ep2out.state = D_EP_IDLE;
#endif
#if SLAB_USB_EP3OUT_USED
myUsbDevice.ep3out.state = D_EP_IDLE;
#endif
}
#if SLAB_USB_EP1IN_USED
USB_ActivateEp(1, // ep
SLAB_USB_EP1IN_MAX_PACKET_SIZE, // packetSize
1, // inDir
SLAB_USB_EP1OUT_USED, // splitMode
0); // isoMod
#endif // SLAB_USB_EP1IN_USED
#if SLAB_USB_EP2IN_USED
USB_ActivateEp(2, // ep
SLAB_USB_EP2IN_MAX_PACKET_SIZE, // packetSize
1, // inDir
SLAB_USB_EP2OUT_USED, // splitMode
0); // isoMod
#endif // SLAB_USB_EP2IN_USED
#if SLAB_USB_EP3IN_USED
USB_ActivateEp(3, // ep
SLAB_USB_EP3IN_MAX_PACKET_SIZE, // packetSize
1, // inDir
SLAB_USB_EP3OUT_USED, // splitMode
(SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_ISOC)); // isoMod
#endif // SLAB_USB_EP3IN_USED
#if SLAB_USB_EP1OUT_USED
USB_ActivateEp(1, // ep
SLAB_USB_EP1OUT_MAX_PACKET_SIZE, // packetSize
0, // inDir
SLAB_USB_EP1IN_USED, // splitMode
0); // isoMod
#endif // SLAB_USB_EP1OUT_USED
#if SLAB_USB_EP2OUT_USED
USB_ActivateEp(2, // ep
SLAB_USB_EP2OUT_MAX_PACKET_SIZE, // packetSize
0, // inDir
SLAB_USB_EP2IN_USED, // splitMode
0); // isoMod
#endif // SLAB_USB_EP2OUT_USED
#if SLAB_USB_EP3OUT_USED
USB_ActivateEp(3, // ep
SLAB_USB_EP3OUT_MAX_PACKET_SIZE, // packetSize
0, // inDir
SLAB_USB_EP3IN_USED, // splitMode
(SLAB_USB_EP3OUT_TRANSFER_TYPE == USB_EPTYPE_ISOC)); // isoMod
#endif // SLAB_USB_EP1OUT_USED
}
/***************************************************************************//**
* @brief Sets up an Endpoint 0 Write
* @param dat
* Data to transmit on Endpoint 0
* @param numBytes
* Number of bytes to transmit on Endpoint 0
******************************************************************************/
static void EP0_Write(SI_VARIABLE_SEGMENT_POINTER(dat, uint8_t, SI_SEG_GENERIC), uint16_t numBytes)
{
if (myUsbDevice.ep0.state == D_EP_IDLE)
{
myUsbDevice.ep0.buf = dat;
myUsbDevice.ep0.remaining = numBytes;
myUsbDevice.ep0.state = D_EP_TRANSMITTING;
myUsbDevice.ep0.misc.c = 0;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,687 @@
/**************************************************************************//**
* Copyright (c) 2015 by Silicon Laboratories Inc. All rights reserved.
*
* http://developer.silabs.com/legal/version/v11/Silicon_Labs_Software_License_Agreement.txt
*****************************************************************************/
#include "si_toolchain.h"
#include "efm8_usb.h"
#include <stdint.h>
#include <endian.h>
// -----------------------------------------------------------------------------
// Global variables
extern SI_SEGMENT_VARIABLE(myUsbDevice, USBD_Device_TypeDef, MEM_MODEL_SEG);
extern SI_SEGMENT_VARIABLE(txZero[2], const uint8_t, SI_SEG_CODE);
// -----------------------------------------------------------------------------
// Function prototypes
static void handleUsbEp0Int(void);
static void handleUsbResetInt(void);
static void handleUsbSuspendInt(void);
static void handleUsbResumeInt(void);
static void handleUsbEp0Tx(void);
static void handleUsbEp0Rx(void);
static void USB_ReadFIFOSetup(void);
#if (SLAB_USB_EP1IN_USED)
void handleUsbIn1Int(void);
#endif // SLAB_USB_EP1IN_USED
#if (SLAB_USB_EP2IN_USED)
void handleUsbIn2Int(void);
#endif // SLAB_USB_EP2IN_USED
#if (SLAB_USB_EP3IN_USED)
void handleUsbIn3Int(void);
#endif // SLAB_USB_EP3IN_USED
#if (SLAB_USB_EP1OUT_USED)
void handleUsbOut1Int(void);
#endif // SLAB_USB_EP1OUT_USED
#if (SLAB_USB_EP2OUT_USED)
void handleUsbOut2Int(void);
#endif // SLAB_USB_EP2OUT_USED
#if (SLAB_USB_EP3OUT_USED)
void handleUsbOut3Int(void);
#endif // SLAB_USB_EP3OUT_USED
void SendEp0Stall(void);
#if SLAB_USB_UTF8_STRINGS == 1
static uint8_t decodeUtf8toUcs2(
const uint8_t *pUtf8in,
SI_VARIABLE_SEGMENT_POINTER(pUcs2out, uint16_t, MEM_MODEL_SEG));
#endif
// -----------------------------------------------------------------------------
// Functions
/***************************************************************************//**
* @brief First-level handler for USB peripheral interrupt
* @details If @ref SLAB_USB_POLLED_MODE is 1, this becomes a regular
* function instead of an ISR and must be called by the application
* periodically.
******************************************************************************/
#if (SLAB_USB_POLLED_MODE == 0)
SI_INTERRUPT(usbIrqHandler, USB0_IRQn)
#else
void usbIrqHandler(void)
#endif
{
uint8_t statusCommon, statusIn, statusOut, indexSave;
#if SLAB_USB_HANDLER_CB
// Callback to user before processing
USBD_EnterHandler();
#endif
// Get the interrupt sources
statusCommon = USB_GetCommonInts();
statusIn = USB_GetInInts();
statusOut = USB_GetOutInts();
#if SLAB_USB_POLLED_MODE
if ((statusCommon == 0) && (statusIn == 0) && (statusOut == 0))
{
return;
}
#endif
// Save the current index
indexSave = USB_GetIndex();
// Check Common USB Interrupts
if (USB_IsSofIntActive(statusCommon))
{
#if SLAB_USB_SOF_CB
USBD_SofCb(USB_GetSofNumber());
#endif // SLAB_USB_SOF_CB
// Check for unhandled USB packets on EP0 and set the corresponding IN or
// OUT interrupt active flag if necessary.
if (((myUsbDevice.ep0.misc.bits.outPacketPending == true) && (myUsbDevice.ep0.state == D_EP_RECEIVING)) ||
((myUsbDevice.ep0.misc.bits.inPacketPending == true) && (myUsbDevice.ep0.state == D_EP_TRANSMITTING)))
{
USB_SetEp0IntActive(statusIn);
}
// Check for unhandled USB OUT packets and set the corresponding OUT
// interrupt active flag if necessary.
#if SLAB_USB_EP1OUT_USED
if ((myUsbDevice.ep1out.misc.bits.outPacketPending == true) && (myUsbDevice.ep1out.state == D_EP_RECEIVING))
{
USB_SetOut1IntActive(statusOut);
}
#endif
#if SLAB_USB_EP2OUT_USED
if ((myUsbDevice.ep2out.misc.bits.outPacketPending == true) && (myUsbDevice.ep2out.state == D_EP_RECEIVING))
{
USB_SetOut2IntActive(statusOut);
}
#endif
#if SLAB_USB_EP3OUT_USED
if ((myUsbDevice.ep3out.misc.bits.outPacketPending == true) && (myUsbDevice.ep3out.state == D_EP_RECEIVING))
{
USB_SetOut3IntActive(statusOut);
}
#endif
#if (SLAB_USB_EP3IN_USED && (SLAB_USB_EP3IN_TRANSFER_TYPE == USB_EPTYPE_ISOC))
if ((myUsbDevice.ep3in.misc.bits.inPacketPending == true) && (myUsbDevice.ep3in.state == D_EP_TRANSMITTING))
{
USB_SetIn3IntActive(statusIn);
}
#endif
}
if (USB_IsResetIntActive(statusCommon))
{
handleUsbResetInt();
// If VBUS is not present on detection of a USB reset, enter suspend mode.
#if (SLAB_USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF)
if (USB_IsVbusOn() == false)
{
USB_SetSuspendIntActive(statusCommon);
}
#endif
}
if (USB_IsResumeIntActive(statusCommon))
{
handleUsbResumeInt();
}
if (USB_IsSuspendIntActive(statusCommon))
{
handleUsbSuspendInt();
}
#if SLAB_USB_EP3IN_USED
if (USB_IsIn3IntActive(statusIn))
{
handleUsbIn3Int();
}
#endif // EP3IN_USED
#if SLAB_USB_EP3OUT_USED
if (USB_IsOut3IntActive(statusOut))
{
handleUsbOut3Int();
}
#endif // EP3OUT_USED
#if SLAB_USB_EP2IN_USED
if (USB_IsIn2IntActive(statusIn))
{
handleUsbIn2Int();
}
#endif // EP2IN_USED
#if SLAB_USB_EP1IN_USED
if (USB_IsIn1IntActive(statusIn))
{
handleUsbIn1Int();
}
#endif // EP1IN_USED
#if SLAB_USB_EP2OUT_USED
if (USB_IsOut2IntActive(statusOut))
{
handleUsbOut2Int();
}
#endif // EP2OUT_USED
#if SLAB_USB_EP1OUT_USED
if (USB_IsOut1IntActive(statusOut))
{
handleUsbOut1Int();
}
#endif // EP1OUT_USED
// Check USB Endpoint 0 Interrupt
if (USB_IsEp0IntActive(statusIn))
{
handleUsbEp0Int();
}
// Restore index
USB_SetIndex(indexSave);
#if SLAB_USB_HANDLER_CB
// Callback to user before exiting
USBD_ExitHandler();
#endif
}
/***************************************************************************//**
* @brief Handles Endpoint 0 transfer interrupt
******************************************************************************/
static void handleUsbEp0Int(void)
{
USB_Status_TypeDef retVal = USB_STATUS_REQ_UNHANDLED;
USB_SetIndex(0);
if (USB_Ep0SentStall() || USB_GetSetupEnd())
{
USB_Ep0ClearSentStall();
USB_ServicedSetupEnd();
myUsbDevice.ep0.state = D_EP_IDLE;
myUsbDevice.ep0.misc.c = 0;
}
if (USB_Ep0OutPacketReady())
{
if (myUsbDevice.ep0.misc.bits.waitForRead == true)
{
myUsbDevice.ep0.misc.bits.outPacketPending = true;
}
else if (myUsbDevice.ep0.state == D_EP_IDLE)
{
myUsbDevice.ep0String.c = USB_STRING_DESCRIPTOR_UTF16LE;
USB_ReadFIFOSetup();
// Vendor unique, Class or Standard setup commands override?
#if SLAB_USB_SETUP_CMD_CB
retVal = USBD_SetupCmdCb(&myUsbDevice.setup);
if (retVal == USB_STATUS_REQ_UNHANDLED)
{
#endif
if (myUsbDevice.setup.bmRequestType.Type == USB_SETUP_TYPE_STANDARD)
{
retVal = USBDCH9_SetupCmd();
}
#if SLAB_USB_SETUP_CMD_CB
}
#endif
// Reset index to 0 in case the call to USBD_SetupCmdCb() or
// USBDCH9_SetupCmd() changed it.
USB_SetIndex(0);
// Put the Enpoint 0 hardware into the correct state here.
if (retVal == USB_STATUS_OK)
{
// If wLength is 0, there is no Data Phase
// Set both the Serviced Out Packet Ready and Data End bits
if (myUsbDevice.setup.wLength == 0)
{
USB_Ep0SetLastOutPacketReady();
}
// If wLength is non-zero, there is a Data Phase.
// Set only the Serviced Out Packet Ready bit.
else
{
USB_Ep0ServicedOutPacketReady();
#if SLAB_USB_SETUP_CMD_CB
// If OUT packet but callback didn't set up a USBD_Read and we are expecting a
// data byte then we need to wait for the read to be setup and NACK packets until
// USBD_Read is called.
if ((myUsbDevice.setup.bmRequestType.Direction == USB_SETUP_DIR_OUT)
&& (myUsbDevice.ep0.state != D_EP_RECEIVING))
{
myUsbDevice.ep0.misc.bits.waitForRead = true;
}
#endif
}
}
// If the setup transaction detected an error, send a stall
else
{
SendEp0Stall();
}
}
else if (myUsbDevice.ep0.state == D_EP_RECEIVING)
{
handleUsbEp0Rx();
}
else
{
myUsbDevice.ep0.misc.bits.outPacketPending = true;
}
}
if ((myUsbDevice.ep0.state == D_EP_TRANSMITTING) && (USB_Ep0InPacketReady() == 0))
{
handleUsbEp0Tx();
}
}
/***************************************************************************//**
* @brief Reads and formats a setup packet
******************************************************************************/
static void USB_ReadFIFOSetup(void)
{
SI_VARIABLE_SEGMENT_POINTER(ptr, uint16_t, MEM_MODEL_SEG) = (SI_VARIABLE_SEGMENT_POINTER(, uint16_t, MEM_MODEL_SEG))&myUsbDevice.setup;
USB_ReadFIFO(0, 8, (SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_GENERIC))ptr);
// Modify for Endian-ness of the compiler
ptr[1] = le16toh(ptr[1]);
ptr[2] = le16toh(ptr[2]);
ptr[3] = le16toh(ptr[3]);
}
/***************************************************************************//**
* @brief Handles USB port reset interrupt
* @details After receiving a USB reset, halt all endpoints except for
* Endpoint 0, set the device state, and configure USB hardware.
******************************************************************************/
static void handleUsbResetInt(void)
{
// Setup EP0 to receive SETUP packets
myUsbDevice.ep0.state = D_EP_IDLE;
// Halt all other endpoints
#if SLAB_USB_EP1IN_USED
myUsbDevice.ep1in.state = D_EP_HALT;
#endif
#if SLAB_USB_EP2IN_USED
myUsbDevice.ep2in.state = D_EP_HALT;
#endif
#if SLAB_USB_EP3IN_USED
myUsbDevice.ep3in.state = D_EP_HALT;
#endif
#if SLAB_USB_EP1OUT_USED
myUsbDevice.ep1out.state = D_EP_HALT;
#endif
#if SLAB_USB_EP2OUT_USED
myUsbDevice.ep2out.state = D_EP_HALT;
#endif
#if SLAB_USB_EP3OUT_USED
myUsbDevice.ep3out.state = D_EP_HALT;
#endif
// After a USB reset, some USB hardware configurations will be reset and must
// be reconfigured.
// Re-enable clock recovery
#if SLAB_USB_CLOCK_RECOVERY_ENABLED
#if SLAB_USB_FULL_SPEED
USB_EnableFullSpeedClockRecovery();
#else
USB_EnableLowSpeedClockRecovery();
#endif
#endif
// Re-enable USB interrupts
USB_EnableSuspendDetection();
USB_EnableDeviceInts();
// If the device is bus-powered, always put it in the Default state.
// If the device is self-powered and VBUS is present, put the device in the
// Default state. Otherwise, put it in the Attached state.
#if (!SLAB_USB_BUS_POWERED) && \
(!(SLAB_USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF))
if (USB_IsVbusOn())
{
USBD_SetUsbState(USBD_STATE_DEFAULT);
}
else
{
USBD_SetUsbState(USBD_STATE_ATTACHED);
}
#else
USBD_SetUsbState(USBD_STATE_DEFAULT);
#endif // (!(SLAB_USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONVBUSOFF))
#if SLAB_USB_RESET_CB
// Make the USB Reset Callback
USBD_ResetCb();
#endif
}
/***************************************************************************//**
* @brief Handle USB port suspend interrupt
* @details After receiving a USB reset, set the device state and
* call @ref USBD_Suspend() if configured to do so in
* @ref SLAB_USB_PWRSAVE_MODE
******************************************************************************/
static void handleUsbSuspendInt(void)
{
if (myUsbDevice.state >= USBD_STATE_POWERED)
{
USBD_SetUsbState(USBD_STATE_SUSPENDED);
#if (SLAB_USB_PWRSAVE_MODE & USB_PWRSAVE_MODE_ONSUSPEND)
USBD_Suspend();
#endif
}
}
/***************************************************************************//**
* @brief Handles USB port resume interrupt
* @details Restore the device state to its previous value.
******************************************************************************/
static void handleUsbResumeInt(void)
{
USBD_SetUsbState(myUsbDevice.savedState);
}
/***************************************************************************//**
* @brief Handles transmit data phase on Endpoint 0
******************************************************************************/
static void handleUsbEp0Tx(void)
{
uint8_t count, count_snapshot, i;
bool callback = myUsbDevice.ep0.misc.bits.callback;
// The number of bytes to send in the next packet must be less than or equal
// to the maximum EP0 packet size.
count = (myUsbDevice.ep0.remaining >= USB_EP0_SIZE) ?
USB_EP0_SIZE : myUsbDevice.ep0.remaining;
// Save the packet size for future use.
count_snapshot = count;
// Strings can use the USB_STRING_DESCRIPTOR_UTF16LE_PACKED type to pack
// UTF16LE data without the zero's between each character.
// If the current string is of type USB_STRING_DESCRIPTOR_UTF16LE_PACKED,
// unpack it by inserting a zero between each character in the string.
if ((myUsbDevice.ep0String.encoding.type == USB_STRING_DESCRIPTOR_UTF16LE_PACKED)
#if SLAB_USB_UTF8_STRINGS == 1
|| (myUsbDevice.ep0String.encoding.type == USB_STRING_DESCRIPTOR_UTF8)
#endif
)
{
// If ep0String.encoding.init is true, this is the beginning of the string.
// The first two bytes of the string are the bLength and bDescriptorType
// fields. These are not packed like the reset of the string, so write them
// to the FIFO and set ep0String.encoding.init to false.
if (myUsbDevice.ep0String.encoding.init == true)
{
USB_WriteFIFO(0, 2, myUsbDevice.ep0.buf, false);
myUsbDevice.ep0.buf += 2;
count -= 2;
myUsbDevice.ep0String.encoding.init = false;
}
// Insert a 0x00 between each character of the string.
for (i = 0; i < count / 2; i++)
{
#if SLAB_USB_UTF8_STRINGS == 1
if (myUsbDevice.ep0String.encoding.type == USB_STRING_DESCRIPTOR_UTF8)
{
SI_SEGMENT_VARIABLE(ucs2, uint16_t, MEM_MODEL_SEG);
uint8_t utf8count;
// decode the utf8 into ucs2 for usb string
utf8count = decodeUtf8toUcs2(myUsbDevice.ep0.buf, &ucs2);
// if consumed utf8 bytes is 0, it means either null byte was
// input or bad utf8 byte sequence. Either way its an error and
// there's not much we can do. So just advance the input string
// by one character and keep going until count is expired.
if (utf8count == 0)
{
utf8count = 1;
}
// adjust to next char in utf8 byte sequence
myUsbDevice.ep0.buf += utf8count;
ucs2 = htole16(ucs2); // usb 16-bit chars are little endian
USB_WriteFIFO(0, 2, (SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_GENERIC))&ucs2, false);
}
else
#endif
{
USB_WriteFIFO(0, 1, (SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_GENERIC))myUsbDevice.ep0.buf, false);
myUsbDevice.ep0.buf++;
USB_WriteFIFO(0, 1, (SI_VARIABLE_SEGMENT_POINTER(, uint8_t, SI_SEG_GENERIC))&txZero, false);
}
}
}
// For any data other than USB_STRING_DESCRIPTOR_UTF16LE_PACKED, just send the
// data normally.
else
{
USB_WriteFIFO(0, count, myUsbDevice.ep0.buf, false);
myUsbDevice.ep0.buf += count;
}
myUsbDevice.ep0.misc.bits.inPacketPending = false;
myUsbDevice.ep0.remaining -= count_snapshot;
// If the last packet of the transfer is exactly the maximum EP0 packet size,
// we will have to send a ZLP (zero-length packet) after the last data packet
// to signal to the host that the transfer is complete.
// Check for the ZLP packet case here.
if ((myUsbDevice.ep0.remaining == 0) && (count_snapshot != USB_EP0_SIZE))
{
USB_Ep0SetLastInPacketReady();
myUsbDevice.ep0.state = D_EP_IDLE;
myUsbDevice.ep0String.c = USB_STRING_DESCRIPTOR_UTF16LE;
myUsbDevice.ep0.misc.c = 0;
}
else
{
// Do not call USB_Ep0SetLastInPacketReady() because we still need to send
// the ZLP.
USB_Ep0SetInPacketReady();
}
// Make callback if requested
if (callback == true)
{
USBD_XferCompleteCb(EP0, USB_STATUS_OK, count_snapshot, myUsbDevice.ep0.remaining);
}
}
/***************************************************************************//**
* @brief Handles receive data phase on Endpoint 0
******************************************************************************/
void handleUsbEp0Rx(void)
{
uint8_t count;
USB_Status_TypeDef status;
bool callback = myUsbDevice.ep0.misc.bits.callback;
// Get the number of bytes received
count = USB_Ep0GetCount();
// If the call to USBD_Read() did not give a large enough buffer to hold this
// data, set the outPacketPending flag and signal an RX overrun.
if (myUsbDevice.ep0.remaining < count)
{
myUsbDevice.ep0.state = D_EP_IDLE;
myUsbDevice.ep0.misc.bits.outPacketPending = true;
status = USB_STATUS_EP_RX_BUFFER_OVERRUN;
}
else
{
USB_ReadFIFO(0, count, myUsbDevice.ep0.buf);
myUsbDevice.ep0.buf += count;
myUsbDevice.ep0.remaining -= count;
status = USB_STATUS_OK;
// If the last packet of the transfer is exactly the maximum EP0 packet
// size, we will must wait to receive a ZLP (zero-length packet) after the
// last data packet. This signals that the host has completed the transfer.
// Check for the ZLP packet case here.
if ((myUsbDevice.ep0.remaining == 0) && (count != USB_EP0_SIZE))
{
USB_Ep0SetLastOutPacketReady();
myUsbDevice.ep0.state = D_EP_IDLE;
myUsbDevice.ep0.misc.bits.callback = false;
}
else
{
// Do not call USB_Ep0SetLastOutPacketReady() until we get the ZLP.
USB_Ep0ServicedOutPacketReady();
}
}
// Make callback if requested
if (callback == true)
{
USBD_XferCompleteCb(EP0, status, count, myUsbDevice.ep0.remaining);
}
}
/***************************************************************************//**
* @brief Send a procedural stall on Endpoint 0
******************************************************************************/
void SendEp0Stall(void)
{
USB_SetIndex(0);
myUsbDevice.ep0.state = D_EP_STALL;
USB_Ep0SendStall();
}
#if SLAB_USB_UTF8_STRINGS == 1
/***************************************************************************//**
* Decodes UTF-8 to UCS-2 (16-bit) character encoding that is used
* for USB string descriptors.
*
* @param pUtf8in pointer to next character in UTF-8 string
* @param pUcs2out pointer to location for 16-bit character output
*
* Decodes a UTF-8 byte sequence into a single UCS-2 character. This
* will only decode up to 16-bit code point and will not handle the
* 21-bit case (4 bytes input).
*
* For valid cases, the UTF8 character sequence decoded into a 16-bit
* character and stored at the location pointed at by _pUcs2out_.
* The function will then return the number of input bytes that were
* consumed (1, 2, or 3). The caller can use the return value to find
* the start of the next character sequence in a utf-8 string.
*
* If either of the input pointers are NULL, then 0 is returned.
*
* If the first input character is NULL, then the output 16-bit value
* will be set to NULL and the function will return 0.
*
* If any other invalid sequence is detected, then the 16-bit output
* will be set to the equivalent of the question mark character (0x003F)
* and the return code will be 0.
*
* @return count of UTF8 bytes consumed
******************************************************************************/
static uint8_t decodeUtf8toUcs2(
const uint8_t *pUtf8in,
SI_VARIABLE_SEGMENT_POINTER(pUcs2out, uint16_t, MEM_MODEL_SEG))
{
uint8_t ret = 0;
// check the input pointers
if (!pUtf8in || !pUcs2out)
{
return 0;
}
// set default decode to error '?';
*pUcs2out = '?';
// valid cases:
// 0xxxxxxx (7 bits)
// 110xxxxx 10xxxxxx (11 bits)
// 1110xxxx 10xxxxxx 10xxxxxx (16 bits)
// null input
if (pUtf8in[0] == 0)
{
*pUcs2out = 0;
ret = 0;
}
// 7-bit char
else if (pUtf8in[0] < 128)
{
*pUcs2out = pUtf8in[0];
ret = 1;
}
// 11-bit char
else if ((pUtf8in[0] & 0xE0) == 0xC0)
{
if ((pUtf8in[1] & 0xC0) == 0x80)
{
*pUcs2out = ((pUtf8in[0] & 0x1F) << 6) | (pUtf8in[1] & 0x3F);
ret = 2;
}
}
// 16-bit char
else if ((pUtf8in[0] & 0xF0) == 0xE0)
{
if ((pUtf8in[1] & 0xC0) == 0x80)
{
if ((pUtf8in[2] & 0xC0) == 0x80)
{
*pUcs2out = ((pUtf8in[0] & 0x0F) << 12)
| ((pUtf8in[1] & 0x3F) << 6)
| (pUtf8in[2] & 0x3F);
ret = 3;
}
}
}
return ret;
}
#endif // SLAB_USB_UTF8_STRINGS
// This function is called from USBD_Init(). It forces the user project to pull
// this module from the library so that the declared ISR can be seen and
// included. If this is not done then this entire module by never be included
// and the ISR will not be present.
void forceModuleLoad_usbint(void){}