all descriptors read - OK, interfaces work - bad (only first two works - why? need to debug)

This commit is contained in:
Edward Emelianov
2026-02-11 23:34:14 +03:00
parent 3a903d7d8c
commit e17058b2de
16 changed files with 565 additions and 259 deletions

View File

@@ -15,6 +15,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h> // memcpy
#include "flash.h" // descriptors
#include "hardware.h" // `config` flag
#include "usb_descr.h"
// low/high for uint16_t
@@ -55,7 +58,88 @@ static const uint8_t USB_DeviceQualifierDescriptor[] = {
0 // Reserved
};
#define wTotalLength (USB_DT_CONFIG_SIZE + (bNumInterfaces * USB_DT_INTERFACE_SIZE) + (bTotNumEndpoints * USB_DT_ENDPOINT_SIZE) + (bNumCsInterfaces * USB_DT_CS_INTERFACE_SIZE) - 1)
#define wTotalLength (USB_DT_CONFIG_SIZE + (bTotNumEndpoints * 66))
/*
* _1stI - number of first interface
* IDidx - 0 or index of string descriptor for given interface
* EPx - number of virtual interrupt EP
* EP - number of real EP in/out
*/
#define USB_IAD(_1stI, IDidx, EPx, EP) \
USB_DT_IAD_SIZE, /* bLength: IAD size */ \
USB_DT_IAD, /* bDescriptorType: IAD */ \
_1stI, /* bFirstInterface */ \
2, /* bInterfaceCount */ \
2, /* bFunctionClass: CDC */ \
2, /* bFunctionSubClass */ \
0, /* bFunctionProtocol */ \
0, /* iFunction */ \
/* Interface Descriptor */ \
USB_DT_INTERFACE_SIZE, /* bLength: Interface Descriptor size */ \
USB_DT_INTERFACE, /* bDescriptorType: Interface */ \
_1stI, /* bInterfaceNumber: Number of Interface */ \
0, /* bAlternateSetting: Alternate setting */ \
1, /* bNumEndpoints: one for this */ \
USB_CLASS_COMM, /* bInterfaceClass */ \
2, /* bInterfaceSubClass: ACM */ \
0, /* bInterfaceProtocol */ \
IDidx, /* iInterface */ \
/* CDC descriptor */ \
USB_DT_CS_INTERFACE_SIZE, /* bLength */ \
USB_DT_CS_INTERFACE, /* bDescriptorType: CS_INTERFACE */ \
0, /* bDescriptorSubtype: Header Func Desc */ \
0x10, /* bcdCDC: spec release number */ \
1, /* bDataInterface */ \
USB_DT_CS_INTERFACE_SIZE, /* bLength */ \
USB_DT_CS_INTERFACE, /* bDescriptorType: CS_INTERFACE */ \
1, /* bDescriptorSubtype: Call Management Func Desc */ \
0, /* bmCapabilities: D0+D1 */ \
(_1stI+1), /* bDataInterface */ \
USB_DT_CS_INTERFACE_SIZE-1, /* bLength */ \
USB_DT_CS_INTERFACE, /* bDescriptorType: CS_INTERFACE */ \
2, /* bDescriptorSubtype: Abstract Control Management desc */ \
2, /* bmCapabilities */ \
USB_DT_CS_INTERFACE_SIZE, /* bLength */ \
USB_DT_CS_INTERFACE, /* bDescriptorType: CS_INTERFACE */ \
6, /* bDescriptorSubtype: Union func desc */ \
_1stI, /* bMasterInterface: Communication class interface */ \
(_1stI+1), /* bSlaveInterface0: Data Class Interface */ \
/* Virtual endpoint 1 Descriptor */ \
USB_DT_ENDPOINT_SIZE, /* bLength: Endpoint Descriptor size */ \
USB_DT_ENDPOINT, /* bDescriptorType: Endpoint */ \
(0x80+EPx), /* bEndpointAddress IN8 - non-existant */ \
USB_BM_ATTR_INTERRUPT, /* bmAttributes: Interrupt */ \
L16(USB_EP1BUFSZ), /* wMaxPacketSize LO */ \
H16(USB_EP1BUFSZ), /* wMaxPacketSize HI */ \
0x10, /* bInterval: 16ms */ \
/* DATA endpoint */ \
USB_DT_INTERFACE_SIZE, /* bLength: Interface Descriptor size */ \
USB_DT_INTERFACE, /* bDescriptorType: Interface */ \
(_1stI+1), /* bInterfaceNumber: Number of Interface */ \
0, /* bAlternateSetting: Alternate setting */ \
2, /* bNumEndpoints: in and out */ \
USB_CLASS_DATA, /* bInterfaceClass */ \
2, /* bInterfaceSubClass: ACM */ \
0, /* bInterfaceProtocol */ \
0, /* iInterface */ \
/*Endpoint IN1 Descriptor */ \
USB_DT_ENDPOINT_SIZE, /* bLength: Endpoint Descriptor size */ \
USB_DT_ENDPOINT, /* bDescriptorType: Endpoint */ \
(0x80+EP), /* bEndpointAddress: IN1 */ \
USB_BM_ATTR_BULK, /* bmAttributes: Bulk */ \
L16(USB_TXBUFSZ), /* wMaxPacketSize LO */ \
H16(USB_TXBUFSZ), /* wMaxPacketSize HI */ \
0, /* bInterval: ignore for Bulk transfer */ \
/* Endpoint OUT1 Descriptor */ \
USB_DT_ENDPOINT_SIZE, /* bLength: Endpoint Descriptor size */ \
USB_DT_ENDPOINT, /* bDescriptorType: Endpoint */ \
EP, /* bEndpointAddress: OUT1 */ \
USB_BM_ATTR_BULK, /* bmAttributes: Bulk */ \
L16(USB_RXBUFSZ), /* wMaxPacketSize LO */ \
H16(USB_RXBUFSZ), /* wMaxPacketSize HI */ \
0 /* bInterval: ignore for Bulk transfer */
static const uint8_t USB_ConfigDescriptor[] = {
// Configuration Descriptor
@@ -68,89 +152,52 @@ static const uint8_t USB_ConfigDescriptor[] = {
0, // iConfiguration: Index of string descriptor describing the configuration or 0
BusPowered, // bmAttributes - Bus powered
50, // MaxPower in 2mA units
//---------------------------------------------------------------------------
// Virtual command Interface Descriptor
USB_DT_INTERFACE_SIZE, // bLength: Interface Descriptor size
USB_DT_INTERFACE, // bDescriptorType: Interface
0, // bInterfaceNumber: Number of Interface
0, // bAlternateSetting: Alternate setting
1, // bNumEndpoints: one for this
USB_CLASS_COMM, // bInterfaceClass
2, // bInterfaceSubClass: ACM
1, // bInterfaceProtocol: Common AT commands
iINTERFACE_DESCR1, // iInterface
// ---- CS Interfaces
USB_DT_CS_INTERFACE_SIZE, // bLength
USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE
0, // bDescriptorSubtype: Header Func Desc
0x10, // bcdCDC: spec release number
1, // bDataInterface
USB_DT_CS_INTERFACE_SIZE, // bLength
USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE
1, // bDescriptorSubtype: Call Management Func Desc
0, // bmCapabilities: D0+D1
1, // bDataInterface
USB_DT_CS_INTERFACE_SIZE-1, // bLength
USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE
2, // bDescriptorSubtype: Abstract Control Management desc
2, // bmCapabilities
USB_DT_CS_INTERFACE_SIZE, // bLength
USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE
6, // bDescriptorSubtype: Union func desc
0, // bMasterInterface: Communication class interface
1, // bSlaveInterface0: Data Class Interface
// Virtual endpoint 1 Descriptor
USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size
USB_DT_ENDPOINT, // bDescriptorType: Endpoint
0x8A, // bEndpointAddress IN10
USB_BM_ATTR_INTERRUPT, // bmAttributes: Interrupt
L16(USB_EP1BUFSZ), // wMaxPacketSize LO
H16(USB_EP1BUFSZ), // wMaxPacketSize HI
0x10, // bInterval: 16ms
//---------------------------------------------------------------------------
// Data interface
USB_DT_INTERFACE_SIZE, // bLength: Interface Descriptor size
USB_DT_INTERFACE, // bDescriptorType: Interface
1, // bInterfaceNumber: Number of Interface
0, // bAlternateSetting: Alternate setting
2, // bNumEndpoints: in and out
USB_CLASS_DATA, // bInterfaceClass
2, // bInterfaceSubClass: ACM
0, // bInterfaceProtocol
0, // iInterface
//Endpoint IN1 Descriptor
USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size
USB_DT_ENDPOINT, // bDescriptorType: Endpoint
0x81, // bEndpointAddress: IN1
USB_BM_ATTR_BULK, // bmAttributes: Bulk
L16(USB_TXBUFSZ), // wMaxPacketSize LO
H16(USB_TXBUFSZ), // wMaxPacketSize HI
0, // bInterval: ignore for Bulk transfer
// Endpoint OUT1 Descriptor
USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size
USB_DT_ENDPOINT, // bDescriptorType: Endpoint
0x01, // bEndpointAddress: OUT1
USB_BM_ATTR_BULK, // bmAttributes: Bulk
L16(USB_RXBUFSZ), // wMaxPacketSize LO
H16(USB_RXBUFSZ), // wMaxPacketSize HI
0, // bInterval: ignore for Bulk transfer
//--IADs-------------------------------------------------------------------------
USB_IAD(0, iINTERFACE_DESCR1, 8, 1),
USB_IAD(2, iINTERFACE_DESCR2, 9, 2),
USB_IAD(4, iINTERFACE_DESCR3, 10, 3),
USB_IAD(6, iINTERFACE_DESCR4, 11, 4),
USB_IAD(8, iINTERFACE_DESCR5, 12, 5),
USB_IAD(10, iINTERFACE_DESCR6, 13, 6),
USB_IAD(12, iINTERFACE_DESCR7, 14, 7),
};
//const uint8_t HID_ReportDescriptor[];
_USB_LANG_ID_(LD, LANG_US);
_USB_STRING_(SD, u"0.0.1");
_USB_STRING_(MD, u"eddy@sao.ru");
_USB_STRING_(PD, u"USB-CDC");
_USB_STRING_(ID, u"usbcdc");
// iInterface will change on initialisation by config
#define _USB_IIDESCR_(str) {sizeof(str), 0x03, str}
typedef struct{
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bString[MAX_IINTERFACE_SZ];
}iidescr_t;
static iidescr_t iids[InterfacesAmount] = {
_USB_IIDESCR_(u"iface0"),
_USB_IIDESCR_(u"iface1"),
_USB_IIDESCR_(u"iface2"),
_USB_IIDESCR_(u"iface3"),
_USB_IIDESCR_(u"iface4"),
_USB_IIDESCR_(u"iface5"),
_USB_IIDESCR_(u"iface6"),
};
static const void* const StringDescriptor[iDESCR_AMOUNT] = {
[iLANGUAGE_DESCR] = &LD,
[iMANUFACTURER_DESCR] = &MD,
[iPRODUCT_DESCR] = &PD,
[iSERIAL_DESCR] = &SD,
[iINTERFACE_DESCR1] = &ID
[iINTERFACE_DESCR1] = &iids[0],
[iINTERFACE_DESCR2] = &iids[1],
[iINTERFACE_DESCR3] = &iids[2],
[iINTERFACE_DESCR4] = &iids[3],
[iINTERFACE_DESCR5] = &iids[4],
[iINTERFACE_DESCR6] = &iids[5],
[iINTERFACE_DESCR7] = &iids[6],
};
static void wr0(const uint8_t *buf, uint16_t size, uint16_t askedsize){
@@ -208,3 +255,18 @@ void get_descriptor(config_pack_t *pack){
break;
}
}
// change values of iInterface by content of global config
void setup_interfaces(){
for(int i = 0; i < InterfacesAmount; ++i){
if(the_conf.iIlengths[i]){
iids[i].bLength = the_conf.iIlengths[i] + 2; // +2 - for bLength and bDescriptorType
memcpy(iids[i].bString, the_conf.iInterface[i], the_conf.iIlengths[i]);
}
iids[i].bDescriptorType = 0x03;
}
if(Config_mode){
// configuration interface identificator is hardware fixed
memcpy(iids[ICFG].bString, u"multiportCFG", 24);
iids[ICFG].bLength = 48;
}
}