📄 standard.c
字号:
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "common.h"
#include "device.h"
#include "board.h"
//#include "trace.h"
#include "usb.h"
#include "standard.h"
//------------------------------------------------------------------------------
// Internal functions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// \brief Callback for the STD_SetConfiguration function.
//
// Configures the device and the endpoints
// \param pClass Pointer to a class driver instance
//------------------------------------------------------------------------------
static void STD_ConfigureEndpoints(const S_std_class *pClass)
{
unsigned char i;
// Enter the Configured state
USB_SetConfiguration(pClass->pUsb);
// Configure endpoints
for (i = 0; i < (pClass->pUsb->dNumEndpoints-1); i++) {
USB_ConfigureEndpoint(pClass->pUsb,
pClass->pDescriptors->pEndpoints[i]);
}
}
//------------------------------------------------------------------------------
// \brief Sends a zero-length packet and starts the configuration procedure.
// \param pClass Pointer to a class driver instance
// \param bConfiguration Newly selected configuration
//------------------------------------------------------------------------------
static void STD_SetConfiguration(S_std_class *pClass,
unsigned char bConfiguration)
{
USB_SendZLP0(pClass->pUsb,
(Callback_f) STD_ConfigureEndpoints,
pClass);
}
//------------------------------------------------------------------------------
// \brief Sends the currently selected configuration to the host.
// \param pClass Pointer to a class driver instance
//------------------------------------------------------------------------------
static void STD_GetConfiguration(S_std_class *pClass)
{
if (ISSET(USB_GetState(pClass->pUsb), USB_STATE_CONFIGURED)) {
pClass->wData = 1;
}
else {
pClass->wData = 0;
}
USB_Write(pClass->pUsb, 0, &(pClass->wData), 1, 0, 0);
}
//------------------------------------------------------------------------------
// \brief Sends the current device status to the host.
// \param pClass Pointer to a class driver interface
//------------------------------------------------------------------------------
static void STD_GetDeviceStatus(S_std_class *pClass)
{
// Bus or self-powered ?
if (ISSET(pClass->pDescriptors->pConfiguration->bmAttibutes,
USB_CONFIG_SELF_NOWAKEUP)) {
// Self powered device
pClass->wDeviceStatus |= SELF_POWERED;
}
else {
// Bus powered device
pClass->wDeviceStatus &= ~SELF_POWERED;
}
// Return the device status
USB_Write(pClass->pUsb, 0, &(pClass->wDeviceStatus), 2, 0, 0);
}
//------------------------------------------------------------------------------
// \brief Sends the current status of specified endpoint to the host.
// \param pClass Pointer to a class driver instance
// \param bEndpoint Endpoint number
//------------------------------------------------------------------------------
static void STD_GetEndpointStatus(S_std_class *pClass,
unsigned char bEndpoint)
{
//! Retrieve the endpoint current status
pClass->wData = (unsigned short) USB_Halt(pClass->pUsb,
bEndpoint,
USB_GET_STATUS);
//! Return the endpoint status
USB_Write(pClass->pUsb, 0, &(pClass->wData), 2, 0, 0);
}
//------------------------------------------------------------------------------
// \brief Sends the device descriptor to the host.
//
// The number of bytes actually sent depends on both the length
// requested by the host and the actual length of the descriptor.
// \param pClass Pointer to a class driver instance
// \param wLength Number of bytes requested by the host
//------------------------------------------------------------------------------
static void STD_GetDeviceDescriptor(const S_std_class *pClass,
unsigned short wLength)
{
USB_Write(pClass->pUsb,
0,
(void *) pClass->pDescriptors->pDevice,
min(sizeof(S_usb_device_descriptor), wLength),
0,
0);
}
//------------------------------------------------------------------------------
// \brief Sends the configuration descriptor to the host.
//
// The number of bytes actually sent depends on both the length
// requested by the host and the actual length of the descriptor.
// \param pClass Pointer to a class driver instance
// \param wLength Number of bytes requested by the host
//------------------------------------------------------------------------------
static void STD_GetConfigurationDescriptor(const S_std_class *pClass,
unsigned short wLength)
{
USB_Write(pClass->pUsb,
0,
(void *) pClass->pDescriptors->pConfiguration,
min(pClass->pDescriptors->pConfiguration->wTotalLength,
wLength),
0,
0);
}
#if defined(HIGHSPEED)
//------------------------------------------------------------------------------
// \brief Sends the qualifier descriptor to the host.
//
// The number of bytes actually sent depends on both the length
// requested by the host and the actual length of the descriptor.
// \param pClass Pointer to a class driver instance
// \param wLength Number of bytes requested by the host
//------------------------------------------------------------------------------
static void STD_GetQualifierDescriptor(const S_std_class *pClass,
unsigned short wLength)
{
USB_Write(pClass->pUsb,
0,
(void *) pClass->pDescriptors->pQualifier,
min(pClass->pDescriptors->pQualifier->bLength, wLength),
0,
0);
}
//------------------------------------------------------------------------------
// \brief Sends the other speed configuration descriptor to the host.
//
// The number of bytes actually sent depends on both the length
// requested by the host and the actual length of the descriptor.
// \param pClass Pointer to a class driver instance
// \param wLength Number of bytes requested by the host
//------------------------------------------------------------------------------
static void STD_GetOSCDescriptor(const S_std_class *pClass,
unsigned short wLength)
{
USB_Write(pClass->pUsb,
0,
(void *) pClass->pDescriptors->pOtherSpeedConfiguration,
min(pClass->pDescriptors->pOtherSpeedConfiguration->wTotalLength,
wLength),
0,
0);
}
#endif
//------------------------------------------------------------------------------
// \brief Sends the specified string descriptor to the host
//
// The number of bytes actually sent depends on both the length
// requested by the host and the actual length of the descriptor.
// \param pClass Pointer to a class driver instance
// \param wLength Number of bytes requested by the host
// \param wIndex Index of requested string descriptor
//------------------------------------------------------------------------------
static void STD_GetStringDescriptor(const S_std_class *pClass,
unsigned short wLength,
unsigned char bIndex)
{
USB_Write(pClass->pUsb,
0,
(void *) pClass->pDescriptors->pStrings[bIndex],
min( *(pClass->pDescriptors->pStrings[bIndex]), wLength),
0,
0);
}
//------------------------------------------------------------------------------
// Exported functions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//! \ingroup usb_std_req_hlr
//! \brief Handles standard SETUP requests
//! \param pClass Pointer to a class driver instance
//------------------------------------------------------------------------------
void STD_RequestHandler(S_std_class *pClass)
{
S_usb_request *pSetup = USB_GetSetup(pClass->pUsb);
// Handle incoming request
switch (pSetup->bRequest) {
//----------------------
case USB_GET_DESCRIPTOR:
//----------------------
// The HBYTE macro returns the upper byte of a word
switch (HBYTE(pSetup->wValue)) {
//-------------------------
case USB_DEVICE_DESCRIPTOR:
//-------------------------
STD_GetDeviceDescriptor(pClass, pSetup->wLength);
break;
//--------------------------------
case USB_CONFIGURATION_DESCRIPTOR:
//--------------------------------
STD_GetConfigurationDescriptor(pClass, pSetup->wLength);
break;
#if defined(HIGHSPEED)
//-----------------------------------
case USB_DEVICE_QUALIFIER_DESCRIPTOR:
//-----------------------------------
STD_GetQualifierDescriptor(pClass, pSetup->wLength);
break;
//--------------------------------------------
case USB_OTHER_SPEED_CONFIGURATION_DESCRIPTOR:
//--------------------------------------------
STD_GetOSCDescriptor(pClass, pSetup->wLength);
break;
#endif
//-------------------------
case USB_STRING_DESCRIPTOR:
//-------------------------
STD_GetStringDescriptor(pClass,
pSetup->wLength,
LBYTE(pSetup->wValue));
break;
//------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -