refractor descriptor

This commit is contained in:
hathach 2013-11-20 12:52:07 +07:00
parent dbfad50d43
commit 850fcf03f0
5 changed files with 74 additions and 74 deletions

View File

@ -187,7 +187,7 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
.bLength = sizeof(tusb_descriptor_interface_association_t), .bLength = sizeof(tusb_descriptor_interface_association_t),
.bDescriptorType = TUSB_DESC_TYPE_INTERFACE_ASSOCIATION, .bDescriptorType = TUSB_DESC_TYPE_INTERFACE_ASSOCIATION,
.bFirstInterface = INTERFACE_INDEX_CDC, .bFirstInterface = INTERFACE_NUM_CDC,
.bInterfaceCount = 2, .bInterfaceCount = 2,
.bFunctionClass = TUSB_CLASS_CDC, .bFunctionClass = TUSB_CLASS_CDC,
@ -204,7 +204,7 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
{ {
.bLength = sizeof(tusb_descriptor_interface_t), .bLength = sizeof(tusb_descriptor_interface_t),
.bDescriptorType = TUSB_DESC_TYPE_INTERFACE, .bDescriptorType = TUSB_DESC_TYPE_INTERFACE,
.bInterfaceNumber = INTERFACE_INDEX_CDC, .bInterfaceNumber = INTERFACE_NUM_CDC,
.bAlternateSetting = 0, .bAlternateSetting = 0,
.bNumEndpoints = 1, .bNumEndpoints = 1,
.bInterfaceClass = TUSB_CLASS_CDC, .bInterfaceClass = TUSB_CLASS_CDC,
@ -256,7 +256,7 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
{ {
.bLength = sizeof(tusb_descriptor_interface_t), .bLength = sizeof(tusb_descriptor_interface_t),
.bDescriptorType = TUSB_DESC_TYPE_INTERFACE, .bDescriptorType = TUSB_DESC_TYPE_INTERFACE,
.bInterfaceNumber = INTERFACE_INDEX_CDC+1, .bInterfaceNumber = INTERFACE_NUM_CDC+1,
.bAlternateSetting = 0x00, .bAlternateSetting = 0x00,
.bNumEndpoints = 2, .bNumEndpoints = 2,
.bInterfaceClass = TUSB_CLASS_CDC_DATA, .bInterfaceClass = TUSB_CLASS_CDC_DATA,
@ -269,7 +269,7 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
{ {
.bLength = sizeof(tusb_descriptor_endpoint_t), .bLength = sizeof(tusb_descriptor_endpoint_t),
.bDescriptorType = TUSB_DESC_TYPE_ENDPOINT, .bDescriptorType = TUSB_DESC_TYPE_ENDPOINT,
.bEndpointAddress = CDC_EDPT_DATA_OUT, .bEndpointAddress = CDC_EDPT_DATA_OUT_ADDR,
.bmAttributes = { .xfer = TUSB_XFER_BULK }, .bmAttributes = { .xfer = TUSB_XFER_BULK },
.wMaxPacketSize = { .size = CDC_EDPT_DATA_PACKETSIZE }, .wMaxPacketSize = { .size = CDC_EDPT_DATA_PACKETSIZE },
.bInterval = 0 .bInterval = 0
@ -279,7 +279,7 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
{ {
.bLength = sizeof(tusb_descriptor_endpoint_t), .bLength = sizeof(tusb_descriptor_endpoint_t),
.bDescriptorType = TUSB_DESC_TYPE_ENDPOINT, .bDescriptorType = TUSB_DESC_TYPE_ENDPOINT,
.bEndpointAddress = CDC_EDPT_DATA_IN, .bEndpointAddress = CDC_EDPT_DATA_IN_ADDR,
.bmAttributes = { .xfer = TUSB_XFER_BULK }, .bmAttributes = { .xfer = TUSB_XFER_BULK },
.wMaxPacketSize = { .size = CDC_EDPT_DATA_PACKETSIZE }, .wMaxPacketSize = { .size = CDC_EDPT_DATA_PACKETSIZE },
.bInterval = 0 .bInterval = 0
@ -292,7 +292,7 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
{ {
.bLength = sizeof(tusb_descriptor_interface_t), .bLength = sizeof(tusb_descriptor_interface_t),
.bDescriptorType = TUSB_DESC_TYPE_INTERFACE, .bDescriptorType = TUSB_DESC_TYPE_INTERFACE,
.bInterfaceNumber = INTERFACE_INDEX_HID_KEYBOARD, .bInterfaceNumber = INTERFACE_NUM_HID_KEYBOARD,
.bAlternateSetting = 0x00, .bAlternateSetting = 0x00,
.bNumEndpoints = 1, .bNumEndpoints = 1,
.bInterfaceClass = TUSB_CLASS_HID, .bInterfaceClass = TUSB_CLASS_HID,
@ -316,9 +316,9 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
{ {
.bLength = sizeof(tusb_descriptor_endpoint_t), .bLength = sizeof(tusb_descriptor_endpoint_t),
.bDescriptorType = TUSB_DESC_TYPE_ENDPOINT, .bDescriptorType = TUSB_DESC_TYPE_ENDPOINT,
.bEndpointAddress = HID_EDPT_KEYBOARD_ADDR, .bEndpointAddress = HID_KEYBOARD_EDPT_ADDR,
.bmAttributes = { .xfer = TUSB_XFER_INTERRUPT }, .bmAttributes = { .xfer = TUSB_XFER_INTERRUPT },
.wMaxPacketSize = { .size = 0x08 }, .wMaxPacketSize = { .size = HID_KEYBOARD_EDPT_PACKETSIZE },
.bInterval = 0x0A .bInterval = 0x0A
}, },
#endif #endif
@ -329,7 +329,7 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
{ {
.bLength = sizeof(tusb_descriptor_interface_t), .bLength = sizeof(tusb_descriptor_interface_t),
.bDescriptorType = TUSB_DESC_TYPE_INTERFACE, .bDescriptorType = TUSB_DESC_TYPE_INTERFACE,
.bInterfaceNumber = INTERFACE_INDEX_HID_MOUSE, .bInterfaceNumber = INTERFACE_NUM_HID_MOUSE,
.bAlternateSetting = 0x00, .bAlternateSetting = 0x00,
.bNumEndpoints = 1, .bNumEndpoints = 1,
.bInterfaceClass = TUSB_CLASS_HID, .bInterfaceClass = TUSB_CLASS_HID,
@ -353,9 +353,9 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
{ {
.bLength = sizeof(tusb_descriptor_endpoint_t), .bLength = sizeof(tusb_descriptor_endpoint_t),
.bDescriptorType = TUSB_DESC_TYPE_ENDPOINT, .bDescriptorType = TUSB_DESC_TYPE_ENDPOINT,
.bEndpointAddress = HID_MOUSE_EP_IN, // TODO .bEndpointAddress = HID_MOUSE_EDPT_ADDR, // TODO
.bmAttributes = { .xfer = TUSB_XFER_INTERRUPT }, .bmAttributes = { .xfer = TUSB_XFER_INTERRUPT },
.wMaxPacketSize = { .size = 0x08 }, .wMaxPacketSize = { .size = HID_MOUSE_EDPT_PACKETSIZE },
.bInterval = 0x0A .bInterval = 0x0A
}, },
#endif #endif
@ -366,7 +366,7 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
{ {
.bLength = sizeof(tusb_descriptor_interface_t), .bLength = sizeof(tusb_descriptor_interface_t),
.bDescriptorType = TUSB_DESC_TYPE_INTERFACE, .bDescriptorType = TUSB_DESC_TYPE_INTERFACE,
.bInterfaceNumber = INTERFACE_INDEX_MASS_STORAGE, .bInterfaceNumber = INTERFACE_NUM_MSC,
.bAlternateSetting = 0x00, .bAlternateSetting = 0x00,
.bNumEndpoints = 2, .bNumEndpoints = 2,
.bInterfaceClass = TUSB_CLASS_MSC, .bInterfaceClass = TUSB_CLASS_MSC,
@ -379,9 +379,9 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
{ {
.bLength = sizeof(tusb_descriptor_endpoint_t), .bLength = sizeof(tusb_descriptor_endpoint_t),
.bDescriptorType = TUSB_DESC_TYPE_ENDPOINT, .bDescriptorType = TUSB_DESC_TYPE_ENDPOINT,
.bEndpointAddress = MSC_EDPT_IN, .bEndpointAddress = MSC_EDPT_IN_ADDR,
.bmAttributes = { .xfer = TUSB_XFER_BULK }, .bmAttributes = { .xfer = TUSB_XFER_BULK },
.wMaxPacketSize = { .size = TUSB_CFG_DEVICE_FULLSPEED ? 64 : 512 }, .wMaxPacketSize = { .size = MSC_EDPT_PACKETSIZE },
.bInterval = 1 .bInterval = 1
}, },
@ -389,9 +389,9 @@ app_descriptor_configuration_t app_tusb_desc_configuration =
{ {
.bLength = sizeof(tusb_descriptor_endpoint_t), .bLength = sizeof(tusb_descriptor_endpoint_t),
.bDescriptorType = TUSB_DESC_TYPE_ENDPOINT, .bDescriptorType = TUSB_DESC_TYPE_ENDPOINT,
.bEndpointAddress = MSC_EDPT_OUT, .bEndpointAddress = MSC_EDPT_OUT_ADDR,
.bmAttributes = { .xfer = TUSB_XFER_BULK }, .bmAttributes = { .xfer = TUSB_XFER_BULK },
.wMaxPacketSize = { .size = TUSB_CFG_DEVICE_FULLSPEED ? 64 : 512 }, .wMaxPacketSize = { .size = MSC_EDPT_PACKETSIZE },
.bInterval = 1 .bInterval = 1
}, },
#endif #endif

View File

@ -41,33 +41,6 @@
#include "tusb.h" #include "tusb.h"
//--------------------------------------------------------------------+
// Endpoints Address & Max Packet Size
//--------------------------------------------------------------------+
#define EDPT_IN(x) (0x80 | x)
#define EDPT_OUT(x) (x)
//------------- CDC -------------//
#define CDC_EDPT_NOTIFICATION_ADDR EDPT_IN(1)
#define CDC_EDPT_NOTIFICATION_PACKETSIZE 64
#define CDC_EDPT_DATA_OUT EDPT_OUT(2)
#define CDC_EDPT_DATA_IN EDPT_IN(2)
#define CDC_EDPT_DATA_PACKETSIZE 64
//------------- HID Keyboard -------------//
#define HID_EDPT_KEYBOARD_ADDR EDPT_IN(3)
//------------- HID Mouse -------------//
#define HID_MOUSE_EP_IN EDPT_IN(4)
//------------- HID Generic -------------//
//------------- Mass Storage -------------//
#define MSC_EDPT_IN EDPT_IN(3)
#define MSC_EDPT_OUT EDPT_OUT(3)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Descriptors Value (calculated by enabled Classes) // Descriptors Value (calculated by enabled Classes)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -75,36 +48,61 @@
//#define CFG_PRODUCTID 0x4567 // use auto product id to prevent conflict with pc's driver //#define CFG_PRODUCTID 0x4567 // use auto product id to prevent conflict with pc's driver
// each combination of interfaces need to have a unique productid, as windows will bind & remember device driver after the first plug. // each combination of interfaces need to have a unique productid, as windows will bind & remember device driver after the first plug.
#ifndef CFG_PRODUCTID // Auto ProductID layout's Bitmap: MassStorage | Generic | Mouse | Key | CDC #ifndef CFG_PRODUCTID // Auto ProductID layout's Bitmap: (MSB) MassStorage | Generic | Mouse | Key | CDC (LSB)
#define PRODUCTID_BITMAP(interface, n) ( (TUSB_CFG_DEVICE_##interface) << (n) )
#define PRODUCTID_BITMAP(interface, n) ( (TUSB_CFG_DEVICE_##interface) << (n) ) #define CFG_PRODUCTID (0x4000 | ( PRODUCTID_BITMAP(CDC, 0) | PRODUCTID_BITMAP(HID_KEYBOARD, 1) | \
#define CFG_PRODUCTID (0x4000 | ( PRODUCTID_BITMAP(CDC, 0) | PRODUCTID_BITMAP(HID_KEYBOARD, 1) | \ PRODUCTID_BITMAP(HID_MOUSE, 2) | PRODUCTID_BITMAP(HID_GENERIC, 3) | \
PRODUCTID_BITMAP(HID_MOUSE, 2) | PRODUCTID_BITMAP(HID_GENERIC, 3) | \ PRODUCTID_BITMAP(MSC, 4) ) )
PRODUCTID_BITMAP(MSC, 4) ) )
#endif #endif
#define ENDPOINT_OUT_LOGICAL_TO_PHYSICAL(addr) (addr)
#define ENDPOINT_IN_LOGICAL_TO_PHYSICAL(addr) ((addr) | 0x80)
#define INTERFACE_INDEX_CDC 0 #define INTERFACE_NUM_CDC 0
#define INTERFACE_INDEX_HID_KEYBOARD (INTERFACE_INDEX_CDC + 2*TUSB_CFG_DEVICE_CDC ) #define INTERFACE_NUM_HID_KEYBOARD (INTERFACE_NUM_CDC + 2*TUSB_CFG_DEVICE_CDC )
#define INTERFACE_INDEX_HID_MOUSE (INTERFACE_INDEX_HID_KEYBOARD + TUSB_CFG_DEVICE_HID_KEYBOARD ) #define INTERFACE_NUM_HID_MOUSE (INTERFACE_NUM_HID_KEYBOARD + TUSB_CFG_DEVICE_HID_KEYBOARD )
#define INTERFACE_INDEX_HID_GENERIC (INTERFACE_INDEX_HID_MOUSE + TUSB_CFG_DEVICE_HID_MOUSE ) #define INTERFACE_NUM_HID_GENERIC (INTERFACE_NUM_HID_MOUSE + TUSB_CFG_DEVICE_HID_MOUSE )
#define INTERFACE_INDEX_MASS_STORAGE (INTERFACE_INDEX_HID_GENERIC + TUSB_CFG_DEVICE_HID_GENERIC ) #define INTERFACE_NUM_MSC (INTERFACE_NUM_HID_GENERIC + TUSB_CFG_DEVICE_HID_GENERIC )
#define TOTAL_INTEFACES (2*TUSB_CFG_DEVICE_CDC + TUSB_CFG_DEVICE_HID_KEYBOARD + TUSB_CFG_DEVICE_HID_MOUSE + \ #define TOTAL_INTEFACES (2*TUSB_CFG_DEVICE_CDC + TUSB_CFG_DEVICE_HID_KEYBOARD + TUSB_CFG_DEVICE_HID_MOUSE + \
TUSB_CFG_DEVICE_HID_GENERIC + TUSB_CFG_DEVICE_MSC) TUSB_CFG_DEVICE_HID_GENERIC + TUSB_CFG_DEVICE_MSC)
// Interface Assosication Descriptor is required when enable CDC with any other class // Interface Assosication Descriptor is required when enable CDC with any other class
#define IAD_DESC_REQUIRED ( TUSB_CFG_DEVICE_CDC && (TOTAL_INTEFACES > 2) ) #define IAD_DESC_REQUIRED ( TUSB_CFG_DEVICE_CDC && (TOTAL_INTEFACES > 2) )
//--------------------------------------------------------------------+
// Endpoints Address & Max Packet Size
//--------------------------------------------------------------------+
#define EDPT_IN(x) (0x80 | (x))
#define EDPT_OUT(x) (x)
//------------- CDC -------------//
#define CDC_EDPT_NOTIFICATION_ADDR EDPT_IN (INTERFACE_NUM_CDC+1)
#define CDC_EDPT_NOTIFICATION_PACKETSIZE 64
#define CDC_EDPT_DATA_OUT_ADDR EDPT_OUT(INTERFACE_NUM_CDC+2)
#define CDC_EDPT_DATA_IN_ADDR EDPT_IN (INTERFACE_NUM_CDC+2)
#define CDC_EDPT_DATA_PACKETSIZE 64
//------------- HID Keyboard -------------//
#define HID_KEYBOARD_EDPT_ADDR EDPT_IN (INTERFACE_NUM_HID_KEYBOARD+1)
#define HID_KEYBOARD_EDPT_PACKETSIZE 8
//------------- HID Mouse -------------//
#define HID_MOUSE_EDPT_ADDR EDPT_IN (INTERFACE_NUM_HID_MOUSE+1)
#define HID_MOUSE_EDPT_PACKETSIZE 8
//------------- HID Generic -------------//
//------------- Mass Storage -------------//
#define MSC_EDPT_OUT_ADDR EDPT_OUT(INTERFACE_NUM_MSC+1)
#define MSC_EDPT_IN_ADDR EDPT_IN (INTERFACE_NUM_MSC+1)
#define MSC_EDPT_PACKETSIZE (TUSB_CFG_DEVICE_FULLSPEED ? 64 : 512)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// CONFIGURATION DESCRIPTOR // CONFIGURATION DESCRIPTOR
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
typedef ATTR_PACKED_STRUCT(struct) typedef ATTR_PACKED_STRUCT(struct)
{ {
tusb_descriptor_configuration_t configuration; tusb_descriptor_configuration_t configuration;
//------------- CDC -------------// //------------- CDC -------------//
#if TUSB_CFG_DEVICE_CDC #if TUSB_CFG_DEVICE_CDC

View File

@ -224,11 +224,11 @@ tusb_error_t tusb_hid_init(USBD_HANDLE_T hUsb, USB_INTERFACE_DESCRIPTOR const *c
tusb_error_t tusb_hid_configured(USBD_HANDLE_T hUsb) tusb_error_t tusb_hid_configured(USBD_HANDLE_T hUsb)
{ {
#ifdef TUSB_CFG_DEVICE_HID_KEYBOARD #ifdef TUSB_CFG_DEVICE_HID_KEYBOARD
ROM_API->hw->WriteEP(hUsb , HID_EDPT_KEYBOARD_ADDR , (uint8_t* ) &hid_keyboard_report , sizeof(hid_keyboard_report_t) ); // initial packet for IN endpoint , will not work if omitted ROM_API->hw->WriteEP(hUsb , HID_KEYBOARD_EDPT_ADDR , (uint8_t* ) &hid_keyboard_report , sizeof(hid_keyboard_report_t) ); // initial packet for IN endpoint , will not work if omitted
#endif #endif
#ifdef TUSB_CFG_DEVICE_HID_MOUSE #ifdef TUSB_CFG_DEVICE_HID_MOUSE
ROM_API->hw->WriteEP(hUsb , HID_MOUSE_EP_IN , (uint8_t* ) &hid_mouse_report , sizeof(hid_mouse_report_t) ); // initial packet for IN endpoint, will not work if omitted ROM_API->hw->WriteEP(hUsb , HID_MOUSE_EDPT_ADDR , (uint8_t* ) &hid_mouse_report , sizeof(hid_mouse_report_t) ); // initial packet for IN endpoint, will not work if omitted
#endif #endif
return TUSB_ERROR_NONE; return TUSB_ERROR_NONE;

View File

@ -347,7 +347,7 @@ static tusb_error_t pipe_control_write(void const * buffer, uint16_t length)
{ {
ASSERT( length !=0 || buffer == NULL, TUSB_ERROR_INVALID_PARA); ASSERT( length !=0 || buffer == NULL, TUSB_ERROR_INVALID_PARA);
LPC_USB->USBCtrl = SLAVE_CONTROL_WRITE_ENABLE_MASK; // logical endpoint = 0 LPC_USB->USBCtrl = USBCTRL_WRITE_ENABLE_MASK; // logical endpoint = 0
LPC_USB->USBTxPLen = length; LPC_USB->USBTxPLen = length;
for (uint16_t count = 0; count < length_byte2dword(length); count++) for (uint16_t count = 0; count < length_byte2dword(length); count++)
@ -356,9 +356,10 @@ static tusb_error_t pipe_control_write(void const * buffer, uint16_t length)
buffer += 4; buffer += 4;
} }
LPC_USB->USBCtrl = 0; LPC_USB->USBCtrl = 0;
sie_write(SIE_CMDCODE_ENDPOINT_SELECT+1, 0, 0); // select control IN endpoint // select control IN & validate the endpoint
sie_write(SIE_CMDCODE_ENDPOINT_SELECT+1, 0, 0);
sie_write(SIE_CMDCODE_BUFFER_VALIDATE , 0, 0); sie_write(SIE_CMDCODE_BUFFER_VALIDATE , 0, 0);
return TUSB_ERROR_NONE; return TUSB_ERROR_NONE;
@ -366,18 +367,20 @@ static tusb_error_t pipe_control_write(void const * buffer, uint16_t length)
static tusb_error_t pipe_control_read(void * buffer, uint16_t length) static tusb_error_t pipe_control_read(void * buffer, uint16_t length)
{ {
LPC_USB->USBCtrl = SLAVE_CONTROL_READ_ENABLE_MASK; // logical endpoint = 0 LPC_USB->USBCtrl = USBCTRL_READ_ENABLE_MASK; // logical endpoint = 0
while ((LPC_USB->USBRxPLen & SLAVE_RXPLEN_PACKET_READY_MASK) == 0) {} // TODO blocking, should have timeout while ((LPC_USB->USBRxPLen & USBRXPLEN_PACKET_READY_MASK) == 0) {} // TODO blocking, should have timeout
uint16_t actual_length = min16_of(length, (uint16_t) (LPC_USB->USBRxPLen & SLAVE_RXPLEN_PACKET_LENGTH_MASK) ); uint16_t actual_length = min16_of(length, (uint16_t) (LPC_USB->USBRxPLen & USBRXPLEN_PACKET_LENGTH_MASK) );
uint32_t *p_read_data = (uint32_t*) buffer; uint32_t *p_read_data = (uint32_t*) buffer;
for( uint16_t count=0; count < length_byte2dword(actual_length); count++) for( uint16_t count=0; count < length_byte2dword(actual_length); count++)
{ {
*p_read_data = LPC_USB->USBRxData; *p_read_data = LPC_USB->USBRxData;
p_read_data++; // increase by 4 ( sizeof(uint32_t) ) p_read_data++; // increase by 4 ( sizeof(uint32_t) )
} }
LPC_USB->USBCtrl = 0; // TODO not needed ?
LPC_USB->USBCtrl = 0;
// select control OUT & clear the endpoint
sie_write(SIE_CMDCODE_ENDPOINT_SELECT+0, 0, 0); sie_write(SIE_CMDCODE_ENDPOINT_SELECT+0, 0, 0);
sie_write(SIE_CMDCODE_BUFFER_CLEAR , 0, 0); sie_write(SIE_CMDCODE_BUFFER_CLEAR , 0, 0);

View File

@ -122,16 +122,15 @@ enum {
//------------- USBCtrl -------------// //------------- USBCtrl -------------//
enum { enum {
SLAVE_CONTROL_READ_ENABLE_MASK = BIT_(0), USBCTRL_READ_ENABLE_MASK = BIT_(0),
SLAVE_CONTROL_WRITE_ENABLE_MASK = BIT_(1), USBCTRL_WRITE_ENABLE_MASK = BIT_(1),
SLAVE_CONTROL_READ_ENABLE_POS = 2
}; };
//------------- USBRxPLen -------------// //------------- USBRxPLen -------------//
enum { enum {
SLAVE_RXPLEN_PACKET_LENGTH_MASK = (BIT_(10)-1), USBRXPLEN_PACKET_LENGTH_MASK = (BIT_(10)-1),
SLAVE_RXPLEN_DATA_VALID_MASK = BIT_(10), USBRXPLEN_DATA_VALID_MASK = BIT_(10),
SLAVE_RXPLEN_PACKET_READY_MASK = BIT_(11), USBRXPLEN_PACKET_READY_MASK = BIT_(11),
}; };
//------------- SIE Command Code -------------// //------------- SIE Command Code -------------//