📄 enumeration_example.c
字号:
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include "common.h"
#include "device.h"
#include "board.h"
//#include "trace.h"
#include "usb.h"
#include "standard.h"
//------------------------------------------------------------------------------
// Structures
//------------------------------------------------------------------------------
//! \brief Represents all the data which must be sent when the host requests
//! a configuration descriptor.
//! \param sConfiguration Configuration descriptor
//! \param sInterface First interface descriptor
//! \see S_usb_configuration_descriptor
//! \see S_usb_interface_descriptor
//! \see usb_20.pdf - Section 9.4.3
typedef struct {
S_usb_configuration_descriptor sConfiguration;
S_usb_interface_descriptor sInterface;
S_usb_endpoint_descriptor sBulkOut; //!< Bulk OUT endpoint
S_usb_endpoint_descriptor sBulkIn; //!< Bulk IN endpoint
} S_core_configuration_descriptor;
//------------------------------------------------------------------------------
// Prototypes
//------------------------------------------------------------------------------
//! \brief Initialization callback
static void CBK_Init(const S_usb *pUsb);
//! \brief Suspend callback
static void CBK_Suspend(const S_usb *pUsb);
//! \brief Resume callback
static void CBK_Resume(const S_usb *pUsb);
//! \brief New request callback
static void CBK_NewRequest(const S_usb *pUsb);
//! \brief New reset callback
//static void CBK_Reset(const S_usb *pUsb);
//! \brief New SOF callback
//static void CBK_SOF(const S_usb *pUsb);
//------------------------------------------------------------------------------
// Internal variables
//------------------------------------------------------------------------------
//! \brief List of endpoints (including endpoint 0) used by the device.
//! \see S_usb_endpoint
static S_usb_endpoint pEndpoints[] = {
USB_ENDPOINT_SINGLEBANK, // Control endpoint 0
USB_ENDPOINT_DUALBANK,
USB_ENDPOINT_DUALBANK
};
//! \brief Variable used to store the last received SETUP packet.
//! \see S_usb_request
//! \see S_usb
static S_usb_request sSetup;
//! \brief Variable used to store the current device state
//! \see S_usb
static unsigned int dState;
//! \brief List of implemented callbacks
//! \see S_usb_callbacks
//! \see S_usb
static const S_usb_callbacks sCallbacks = {
CBK_Init,
0,//CBK_Reset
CBK_Suspend,
CBK_Resume,
CBK_NewRequest,// MY_REQUEST_HANDLE,
0 //CBK_SOF
};
//! \brief USB driver instance
//! \see S_usb
static const S_usb sUsb = {
&sDefaultDriver,
pEndpoints,
3,
&sCallbacks,
&sSetup,
&dState
};
// Descriptors
//! Device descriptor
static const S_usb_device_descriptor sDeviceDescriptor = {
sizeof(S_usb_device_descriptor), // Size of this descriptor in bytes
USB_DEVICE_DESCRIPTOR, // DEVICE Descriptor Type
0x0200, // USB Specification 2.0
0xdc, // Class is specified in the interface descriptor.
0x00, // Subclass is specified in the interface descriptor.
0x00, // Protocol is specified in the interface descriptor.
USB_ENDPOINT0_MAXPACKETSIZE, // Maximum packet size for endpoint zero
0x03eb, // Vendor ID "ATMEL"
0x4444, // Product ID
0x0100, // Device release number
0x00, // Index 1: manufacturer string
0x00, // Index 2: product string
0x00, // Index 3: serial number string
0x01 // One possible configurations
};
//! \brief Device configuration, which includes the configuration descriptor
//! and the first interface descriptor in this case.
//! \see S_core_configuration_descriptor
static const S_core_configuration_descriptor sConfigurationDescriptor = {
// Configuration descriptor
{
sizeof(S_usb_configuration_descriptor), // Size of this descriptor
USB_CONFIGURATION_DESCRIPTOR, // CONFIGURATION descriptor
sizeof(S_core_configuration_descriptor), // Total length
0x01, // Number of interfaces
0x01, // Value to select this configuration
0x00, // No index for describing this configuration
USB_CONFIG_SELF_NOWAKEUP,//0x60, // Device attributes
USB_POWER_MA(100) // maximum power consumption in mA
},
// Interface Descriptor
{
sizeof(S_usb_interface_descriptor), // Size of this descriptor in bytes
USB_INTERFACE_DESCRIPTOR, // INTERFACE Descriptor Type
0x00, // Interface number 0
0x00, // Value used to select this setting
COMM_NUM_ENDPOINTS-1, // Number of endpoints used by this
// interface (excluding endpoint 0).
0xdc, // Interface class
0xa0, // Interface subclass
0xb0, // Interface protocol
0x00 // Index of string descriptor
},
// Bulk-OUT Endpoint Descriptor
{
sizeof(S_usb_endpoint_descriptor), // Size of this descriptor in bytes
USB_ENDPOINT_DESCRIPTOR, // ENDPOINT descriptor type
0x01,//USB_ENDPOINT_OUT | BOT_EPT_BULK_OUT, // OUT endpoint, address 01h
ENDPOINT_TYPE_BULK, // Bulk endpoint
64, // Maximum packet size is 64 bytes
0, // Must be 0 for full-speed bulk
},
// Bulk_IN Endpoint Descriptor
{
sizeof(S_usb_endpoint_descriptor), // Size of this descriptor in bytes
USB_ENDPOINT_DESCRIPTOR, // ENDPOINT descriptor type
0x82,//USB_ENDPOINT_IN | BOT_EPT_BULK_IN, // IN endpoint, address 02h
ENDPOINT_TYPE_BULK, // Bulk endpoint
64, // Maximum packet size if 64 bytes
0, // Must be 0 for full-speed bulk
}
};
const S_usb_endpoint_descriptor* InOutEP[2]={&(sConfigurationDescriptor.sBulkOut),&(sConfigurationDescriptor.sBulkIn)};
// String descriptors
//! \brief Language ID
static const S_usb_language_id sLanguageID = {
USB_STRING_DESCRIPTOR_SIZE(1),
USB_STRING_DESCRIPTOR,
USB_LANGUAGE_ENGLISH_US
};
//! \brief Manufacturer description
static const char pManufacturer[] = {
USB_STRING_DESCRIPTOR_SIZE(5),
USB_STRING_DESCRIPTOR,
USB_UNICODE('A'),
USB_UNICODE('T'),
USB_UNICODE('M'),
USB_UNICODE('E'),
USB_UNICODE('L')
};
//! \brief Product descriptor
static const char pProduct[] = {
USB_STRING_DESCRIPTOR_SIZE(15),
USB_STRING_DESCRIPTOR,
USB_UNICODE('A'),
USB_UNICODE('T'),
USB_UNICODE('M'),
USB_UNICODE('E'),
USB_UNICODE('L'),
USB_UNICODE(' '),
USB_UNICODE('A'),
USB_UNICODE('T'),
USB_UNICODE('9'),
USB_UNICODE('1'),
USB_UNICODE(' '),
USB_UNICODE('E'),
USB_UNICODE('N'),
USB_UNICODE('U'),
USB_UNICODE('M')
};
//! \brief Serial number
static const char pSerial[] = {
USB_STRING_DESCRIPTOR_SIZE(12),
USB_STRING_DESCRIPTOR,
USB_UNICODE('0'),
USB_UNICODE('1'),
USB_UNICODE('2'),
USB_UNICODE('3'),
USB_UNICODE('4'),
USB_UNICODE('5'),
USB_UNICODE('6'),
USB_UNICODE('7'),
USB_UNICODE('8'),
USB_UNICODE('9'),
USB_UNICODE('A'),
USB_UNICODE('F')
};
//! \brief List of string descriptors used by the device
static const char *pStringDescriptors[] = {
(char *) &sLanguageID,
pManufacturer,
pProduct,
pSerial
};
//! \brief List of descriptors used by the device
//! \see S_std_descriptors
static S_std_descriptors sDescriptors = {
&sDeviceDescriptor,
(S_usb_configuration_descriptor *) &sConfigurationDescriptor,
pStringDescriptors,
InOutEP
};
//! \brief Standard class driver
//! \see S_std_class
static S_std_class sClass = {
&sUsb,
&sDescriptors,
(unsigned short)0,
(unsigned short)0
};
//------------------------------------------------------------------------------
// Internal Functions
//------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -