RNDIS "Composite device" cannot start - usb

I am developing a RNDIS composite device which includes a RNDIS device, a usbnet device ,a modem device and a self-defined CDC device.
The PC is running Win7 32bit, and I am using the default RNDIS driver.
When I answers the Windows enumeration with only RNDIS device, everything works fine.
But when I compose all 4 device into the descriptor as composite device, Windows still enumerates correctly and the other 3 devices works fine, but RNDIS device encounters with "cannot start error code 10" issue.
I traced the USB data flow with a USB analyzer, found that Windows RNDIS driver did not send RNDIS initialize messages after “Set Configuration”.
The composite device descriptor is as posted:
devDesc[0] = 0x12; // bLength - Descriptor length
devDesc[1] = 0x01; // bDescriptorType - Descriptor Type
devDesc[2] = bcdUSB_LSB; // bcdUSB (LSB) - Device Compliant to USB specification ..
devDesc[3] = bcdUSB_MSB; // bcdUSB (MSB)
devDesc[4] = 0x00; // bDeviceClass - class of the device
devDesc[5] = 0x00; // bDeviceSubClass - subclass of the device
devDesc[6] = 0x00; // bDeviceProtocol - protocol of the device
devDesc[7] = bEp0MaxPacketSize; // bMaxPacketSize0 - Max Packet Size for EP zero
devDesc[8] = 0x86; // idVendor (LSB) - Vendor ID
devDesc[9] = 0x12; // idVendor (MSB)
devDesc[10] = 0x0E; // idProduct (LSB) - Product ID
devDesc[11] = 0x81; // idProduct (MSB)
devDesc[12] = 0x02; // bcdDevice (LSB) - The device release number
devDesc[13] = 0x00; // bcdDevice (MSB)
devDesc[14] = 0x01; // iManufacturer - Index of string descriptor describing Manufacturer
devDesc[15] = 0x03; // iProduct - Index of string descriptor describing Product
devDesc[16] = 0x04; // iSerialNumber - Index of string descriptor describing Serial number
devDesc[17] = 0x01; // bNumConfigurations - Number of configurations
//Configuration Descriptor
configDesc[0] = 0x09; // bLength - Descriptor length
configDesc[1] = 0x02; // bDescriptorType - Descriptor Type
configDesc[2] = 177; // wTotalLength (LSB) - Total Data length for the configuration,
configDesc[3] = 0x00; // wTotalLength (MSB) - includes all descriptors for this configuration
configDesc[4] = 0x05; // bNumInterfaces - Number of interfaces this configuration supports
configDesc[5] = 0x01; // bConfigurationValue - The Value that should be used to select this configuration
configDesc[6] = 0x00; // iConfiguration - Index of string descriptor describing this configuration
configDesc[7] = 0xC0; // bmAttributes - bit6: Self-Powered, bit5: RemoteWakeup
configDesc[8] = 0xfa; // MaxPower - Maximum power consumption for this configuration (mA)
//Interface Descriptor 0 usbnet
configDesc[9] = 0x09; // bLength - Descriptor length
configDesc[10] = 0x04; // bDescriptorType - Descriptor Type
configDesc[11] = 0x00; // bInterfaceNumber - Index (Number) of this interfaces
configDesc[12] = 0x00; // bAlternateSetting - The value to select alternate setting of this interface
configDesc[13] = 0x03; // bNumEndpoints - Number endpoints used by this interface
configDesc[14] = 0xff; // bInterfaceClass - Class of this Interface
configDesc[15] = 0xff; // bInterfaceSubClass - Sub class of this Interface
configDesc[16] = 0x00; // bInterfaceProtocol - Protocol of this Interface
configDesc[17] = 0x05; // iInterface - Index of string descriptor describing this Interface
// usbnet Endpoint Descriptor
configDesc[18] = 0x07; // bLength - Descriptor length
configDesc[19] = 0x05; // bDescriptorType - Descriptor Type
configDesc[20] = 0x85; // bEndpointAddress - Endpoint Address & Direction
configDesc[21] = 0x03; // bmAttributes - BULK,ISO,Interrupt
configDesc[22] = 0x0F; // wMaxPacketSize (LSB) - Max packet size
configDesc[23] = 0x00; // wMaxPacketSize (MSB)
configDesc[24] = bInterval; // bInterval - Polling Interval (ms)
configDesc[25] = 0x07; // bLength - Descriptor length
configDesc[26] = 0x05; // bDescriptorType - Descriptor Type
configDesc[27] = 0x81; // bEndpointAddress - Endpoint Address & Direction
configDesc[28] = 0x02; // bmAttributes - BULK,ISO,Interrupt
configDesc[29] = bBulkMaxPacketSize_LSB; // wMaxPacketSize (LSB) - Max packet size
configDesc[30] = bBulkMaxPacketSize_MSB; // wMaxPacketSize (MSB)
configDesc[31] = 0x00; // bInterval - Polling Interval (ms)
configDesc[32] = 0x07; // bLength - Descriptor length
configDesc[33] = 0x05; // bDescriptorType - Descriptor Type
configDesc[34] = 0x02; // bEndpointAddress - Endpoint Address & Direction
configDesc[35] = 0x02; // bmAttributes - BULK,ISO,Interrupt
configDesc[36] = bBulkMaxPacketSize_LSB; // wMaxPacketSize (LSB) - Max packet size
configDesc[37] = bBulkMaxPacketSize_MSB; // wMaxPacketSize (MSB)
configDesc[38] = 0x00; // bInterval - Polling Interval (ms)
//Interface descriptor 1 ICAT
configDesc[39] = 0x09; // bLength - Descriptor length
configDesc[40] = 0x04; // bDescriptorType - Descriptor Type
configDesc[41] = 0x01; // bInterfaceNumber - Index (Number) of this interfaces
configDesc[42] = 0x00; // bAlternateSetting - The value to select alternate setting of this interface
configDesc[43] = 0x02; // bNumEndpoints - Number endpoints used by this interface
configDesc[44] = 0xFF; // bInterfaceClass - Class of this Interface
configDesc[45] = 0x00; // bInterfaceSubClass - Sub class of this Interface
configDesc[46] = 0x00; // bInterfaceProtocol - Protocol of this Interface
configDesc[47] = 0x00; // iInterface - Index of string descriptor describing this Interface
//ICAT endpoints
configDesc[48] = 0x07; // bLength - Descriptor length
configDesc[49] = 0x05; // bDescriptorType - Descriptor Type
configDesc[50] = 0x87; // bEndpointAddress - Endpoint Address & Direction
configDesc[51] = 0x02; // bmAttributes - BULK,ISO,Interrupt
configDesc[52] = bBulkMaxPacketSize_LSB; // wMaxPacketSize (LSB) - Max packet size
configDesc[53] = bBulkMaxPacketSize_MSB; // wMaxPacketSize (MSB)
configDesc[54] = 0x00; // bInterval - Polling Interval (ms)
configDesc[55] = 0x07; // bLength - Descriptor length
configDesc[56] = 0x05; // bDescriptorType - Descriptor Type
configDesc[57] = 0x08; // bEndpointAddress - Endpoint Address & Direction
configDesc[58] = 0x02; // bmAttributes - BULK,ISO,Interrupt
configDesc[59] = bBulkMaxPacketSize_LSB; // wMaxPacketSize (LSB) - Max packet size
configDesc[60] = bBulkMaxPacketSize_MSB; // wMaxPacketSize (MSB)
configDesc[61] = 0x00; // bInterval - Polling Interval (ms)
//Modem class specific descriptors
configDesc[62] = 0x09; // bLength - Descriptor length
configDesc[63] = 0x04; // bDescriptorType - Descriptor Type
configDesc[64] = 0x02; // bInterfaceNumber - Index (Number) of this interfaces
configDesc[65] = 0x00; // bAlternateSetting - The value to select alternate setting of this interface
configDesc[66] = 0x03; // bNumEndpoints - Number endpoints used by this interface
configDesc[67] = 0x02; // bInterfaceClass - Class of this Interface
configDesc[68] = 0x02; // bInterfaceSubClass - Sub class of this Interface
configDesc[69] = 0x01; // bInterfaceProtocol - Protocol of this Interface
configDesc[70] = 0x00; // iInterface - Index of string descriptor describing this Interface
configDesc[71] = 0x05; // descriptor length Comms Class CS_INTERFACE 'Functional Descriptors' Triplet
configDesc[72] = 0x24; // descriptor type
configDesc[73] = 0x00; // subtype = header
configDesc[74] = 0x10; // BCD
configDesc[75] = 0x01; //
configDesc[76] = 0x05; // 2. descriptor length - Call Manangement Func Desc
configDesc[77] = 0x24; // descriptor type
configDesc[78] = 0x01; // subtype = Call management
configDesc[79] = 0x00; //bmcapabilities; MS driver usbser.sys seems to ignore this bit and sends AT over the data,interface anyway
configDesc[80] = 0x01; // Interface number of data class interface
configDesc[81] = 0x04; // 3.descriptor length Abstract Control Func Desc
configDesc[82] = 0x24; // descriptor type
configDesc[83] = 0x02; // subtype = Abstract CM
configDesc[84] = 0x00; // bmcapabilities
configDesc[85] = 0x05; // Size of descriptor. Number of interfaces plus 3 bytes of header
configDesc[86] = 0x24; // 0x24 - CS_INTERFACE
configDesc[87] = 0x06; // 0x06 - See table 25 of document "USB Class definitions for Comms Devices"
configDesc[88] = 0x00;
configDesc[89] = 0x01;
//Endpoint Descriptors MODEM
configDesc[90] = 0x07; // bLength - Descriptor length
configDesc[91] = 0x05; // bDescriptorType - Descriptor Type
configDesc[92] = 0x86; // bEndpointAddress - Endpoint Address & Direction
configDesc[93] = 0x03; // bmAttributes - BULK,ISO,Interrupt
configDesc[94] = 0x0F; // wMaxPacketSize (LSB) - Max packet size
configDesc[95] = 0x00; // wMaxPacketSize (MSB)
configDesc[96] = bInterval; // bInterval - Polling Interval (ms)
configDesc[97] = 0x07; // bLength - Descriptor length
configDesc[98] = 0x05; // bDescriptorType - Descriptor Type
configDesc[99] = 0x83; // bEndpointAddress - Endpoint Address & Direction
configDesc[100] = 0x02; // bmAttributes - BULK,ISO,Interrupt
configDesc[101] = bBulkMaxPacketSize_LSB; // wMaxPacketSize (LSB) - Max packet size
configDesc[102] = bBulkMaxPacketSize_MSB; // wMaxPacketSize (MSB)
configDesc[103] = 0x00; // bInterval - Polling Interval (ms)
configDesc[104] = 0x07; // bLength - Descriptor length
configDesc[105] = 0x05; // bDescriptorType - Descriptor Type
configDesc[106] = 0x04; // bEndpointAddress - Endpoint Address & Direction
configDesc[107] = 0x02; // bmAttributes - BULK,ISO,Interrupt
configDesc[108] = bBulkMaxPacketSize_LSB; // wMaxPacketSize (LSB) - Max packet size
configDesc[109] = bBulkMaxPacketSize_MSB; // wMaxPacketSize (MSB)
configDesc[110] = 0x00; // bInterval - Polling Interval (ms)
//rndis IAD
configDesc[111] = 0x08; // bLength
configDesc[112] = 0x0b; // INTERFACE ASSOCIATION DESCRIPTOR bDescriptorType
configDesc[113] = 0x03; // bFirstInterface
configDesc[114] = 0x02; // bInterfaceCount
configDesc[115] = 0x02; // bFunctionClass
configDesc[116] = 0x02; // bFunctionSubClass
configDesc[117] = 0xFF; // bFunctionProtocol
configDesc[118] = 0x03; // Index of string descriptor describing this function
//rndis comm interface
configDesc[119] = 0x09; // bLength - Descriptor length
configDesc[120] = 0x04; // bDescriptorType - Descriptor Type
configDesc[121] = 0x03; //bInterfaceNumber - for RNDIS
configDesc[122] = 0x00; // bAlt
configDesc[123] = 0x01; // bNumEnd
configDesc[124] = 0x02; //bInterfaceClass
configDesc[125] = 0x02; // bInterfaceSubclass
configDesc[126] = 0xFF; // bInterfaceprotocol
configDesc[127] = 0x00; // iInterface +++++++++++++++++++++++++++++++++++++need modified
// Class specified descriptor
configDesc[128] = 0x05; //
configDesc[129] = 0x24; // bDescriptorType
configDesc[130] = 0x00; //bDescriptorSubtype
configDesc[131] = 0x10; // BCD
configDesc[132] = 0x01; //
configDesc[133] = 0x05; //
configDesc[134] = 0x24; // bDescriptorType
configDesc[135] = 0x01; // bDescriptorSubtype
configDesc[136] = 0x00; // bmCapabilities
configDesc[137] = 0x04; //bDataInterface
configDesc[138] = 0x04; //
configDesc[139] = 0x24; // bDescriptorType
configDesc[140] = 0x02; // bDescriptorSubtype
configDesc[141] = 0x00; // bmCapabilities
configDesc[142] = 0x05; //
configDesc[143] = 0x24; // bDescriptorType
configDesc[144] = 0x06; // bDescriptorSubtype
configDesc[145] = 0x03; // bControlInterface
configDesc[146] = 0x04; // bsubordinateInterface
//Interrupt In endpoint
configDesc[147] = 0x07; //
configDesc[148] = 0x05; //
configDesc[149] = 0x8C; //bendpointAddress
configDesc[150] = 0x03; //bmAttributes
configDesc[151] = 0x08; // wMaxPacketSize
configDesc[152] = 0x00; //
configDesc[153] = 0x01; // bInterval
//rndis data interface
configDesc[154] = 0x09; // bLength - Descriptor length
configDesc[155] = 0x04; // bDescriptorType - Descriptor Type
configDesc[156] = 0x04; //bInterfaceNumber --- for RNDIS
configDesc[157] = 0x00; // bAlt
configDesc[158] = 0x02; // NumEndpoints
configDesc[159] = 0x0a; //bInterfaceClass
configDesc[160] = 0x00; // bInterfaceSubclass
configDesc[161] = 0x00; // bInterfaceprotocol
configDesc[162] = 0x00; // iInterface +++++++++++++++++++++++++++++++++++++need modified
//Bulk In endpoint
configDesc[163] = 0x07; //
configDesc[164] = 0x05; //
configDesc[165] = 0x8E; //bendpointAddress
configDesc[166] = 0x02; //bmAttributes
configDesc[167] = 0x00; // wMaxPacketSize
configDesc[168] = 0x02; //
configDesc[169] = 0x00; // bInterval
//Bulk Out endpoint
configDesc[170] = 0x07; //
configDesc[171] = 0x05; //
configDesc[172] = 0x0D; //bendpointAddress
configDesc[173] = 0x02; //bmAttributes
configDesc[174] = 0x00; // wMaxPacketSize
configDesc[175] = 0x02; //
configDesc[176] = 0x00; // bInterva

Let me answer my own question, I have figured out this issue:
The RNDIS IAD interface must be the first and second of the composite device.
Although the problem is solved, it still puzzles me.
Hope this note will help others encountering similar issues.

For those of you out there that come across this thread, because you are having similar problems with RNDIS and windows code 10 errors, using the built in configfs libcomposite device, after tons of googling, and going over code on the internet, code in the libcomposite module..... I found that instead of patching the driver I could work around this if I force loaded the usb_f_rndis module directly after libcomposite module, this forces usb_f_rndis to first and second iad slot. The rest of the modules for libcomposite devices will load, as you add the devices in configfs, and Windows will be happy with it.
Also, to force windows to load the right driver, make sure you set up the os_desc stuff. There is a nice guide that goes over that here: http://irq5.io/2016/12/22/raspberry-pi-zero-as-multiple-usb-gadgets/

As I said in my comment on the answer mentioning the preload workaround, I couldn't get preloading the usb_f_rndis module to make any difference when using configfs to set up a composite gadget.
I have found that the order in which the function directories are linked into the configuration directory makes a difference, however.
ln -s gadget/functions/acm.usb0 gadget/configs/c.1
ln -s gadget/functions/rndis.usb0 gadget/configs/c.1
Leaves me with a functional USB serial port, but broken RNDIS adapter showing error code 10.
ln -s gadget/functions/rndis.usb0 gadget/configs/c.1
ln -s gadget/functions/acm.usb0 gadget/configs/c.1
Gives me both a functional serial port and RNDIS adapter.
The question is of course how stable this will be over time. I suspect it could be sort of a fluke, similar to what the usb_f_rndis preload trick appears to be.
This was on Debian 10.9 with Linux raspberrypi 5.15.74-v7l+, by the way.

Related

Composite USB CDC/HID device on STMF32F303

I'm following the advice:
It is possible to achieve your own composite USB Device, by combining the desired classes' drivers into one class, under your own folder within: Middlewares\ST\STM32_USB Device Library\Class
from https://community.st.com/s/question/0D50X00009XkgYtSAJ/stm32f4-hal-composite-usb-device-example
I now have a unified configuration descriptor.
/* USB CDC device Configuration Descriptor */
__ALIGN_BEGIN uint8_t USBD_CDC_CfgFSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END =
{
/*Configuration Descriptor*/
0x09, /* bLength: Configuration Descriptor size */
USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */
0x00,
0x03, /* bNumInterfaces: 2 interface */
0x01, /* bConfigurationValue: Configuration value */
0x00, /* iConfiguration: Index of string descriptor describing the configuration */
0xC0, /* bmAttributes: self powered */
0x32, /* MaxPower 0 mA */
/*---------------------------------------------------------------------------*/
//////////////////////////////////////////////////////////////////////////////////////
//Add 1 IAD class here // this one is for COM port
0x08, // bLength: Interface Descriptor size
0x0B, // bDescriptorType: IAD
0x00, // bFirstInterface //starting of interface
0x02, // bInterfaceCount //interfaces under this IAD class
0x02, // bFunctionClass: CDC
0x02, // bFunctionSubClass
0x01, // bFunctionProtocol
0x02, // iFunction
//////////////////////////////////////////////////////////////////////////////////////
/*Interface Descriptor */
0x09, /* bLength: Interface Descriptor size */
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */
/* Interface descriptor type */
0x00, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x01, /* bNumEndpoints: One endpoints used */
0x02, /* bInterfaceClass: Communication Interface Class */
0x02, /* bInterfaceSubClass: Abstract Control Model */
0x01, /* bInterfaceProtocol: Common AT commands */
0x00, /* iInterface: */
/*Header Functional Descriptor*/
0x05, /* bLength: Endpoint Descriptor size */
0x24, /* bDescriptorType: CS_INTERFACE */
0x00, /* bDescriptorSubtype: Header Func Desc */
0x10, /* bcdCDC: spec release number */
0x01,
/*Call Management Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x01, /* bDescriptorSubtype: Call Management Func Desc */
0x00, /* bmCapabilities: D0+D1 */
0x01, /* bDataInterface: 1 */
/*ACM Functional Descriptor*/
0x04, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x02, /* bDescriptorSubtype: Abstract Control Management desc */
0x02, /* bmCapabilities */
/*Union Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x06, /* bDescriptorSubtype: Union func desc */
0x00, /* bMasterInterface: Communication class interface */
0x01, /* bSlaveInterface0: Data Class Interface */
/*Endpoint 2 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_CMD_EP, /* bEndpointAddress */
0x03, /* bmAttributes: Interrupt */
LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_CMD_PACKET_SIZE),
CDC_FS_BINTERVAL, /* bInterval: */
/*---------------------------------------------------------------------------*/
/*Data class interface descriptor*/
0x09, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */
0x01, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints: Two endpoints used */
0x0A, /* bInterfaceClass: CDC */
0x00, /* bInterfaceSubClass: */
0x00, /* bInterfaceProtocol: */
0x00, /* iInterface: */
/*Endpoint OUT Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_OUT_EP, /* bEndpointAddress */
0x02, /* bmAttributes: Bulk */
LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),
0x00, /* bInterval: ignore for Bulk transfer */
/*Endpoint IN Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_IN_EP, /* bEndpointAddress */
0x02, /* bmAttributes: Bulk */
LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),
0x00, /* bInterval: ignore for Bulk transfer */
///////////////////////////////////////////////////////////////////////////////////////////////
// IAD Mouse
0x08, // bLength: Interface Descriptor size
0x0B, // bDescriptorType: IAD
0x02, // bFirstInterface //starts from zero .. hence 0-1- and now 2
0x01, // bInterfaceCount //no of interfaces used in function. for above cdc it was 2
0x03, //*bInterfaceClass: HID*/
0x01, //*bInterfaceSubClass : 1=BOOT, 0=no boot*/
0x02, //*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
0x02, //*iInterface: Index of string descriptor*/
////////////////////////////////////////////////////////////////////////////////////////////////
/************** Descriptor of Joystick Mouse interface ****************/
/* 09 */
0x09, /*bLength: Interface Descriptor size*/
USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/
0x02, /*bInterfaceNumber: Number of Interface*/
0x00, /*bAlternateSetting: Alternate setting*/
0x01, /*bNumEndpoints*/
0x03, /*bInterfaceClass: HID*/
0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
0x02, /*iInterface: Index of string descriptor*/
/******************** Descriptor of Joystick Mouse HID ********************/
/* 18 */
0x09, /*bLength: HID Descriptor size*/
HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
0x11, /*bcdHID: HID Class Spec release number*/
0x01,
0x00, /*bCountryCode: Hardware target country*/
0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/
0x22, /*bDescriptorType*/
HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/
0x00,
/******************** Descriptor of Mouse endpoint ********************/
/* 27 */
0x07, /*bLength: Endpoint Descriptor size*/
USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/
HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/
0x03, /*bmAttributes: Interrupt endpoint*/
HID_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */
0x00,
HID_FS_BINTERVAL, /*bInterval: Polling Interval */
/* 34 */
} ;
and a unified init:
static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
{
uint8_t ret = 0U;
USBD_CDC_HandleTypeDef *hcdc;
if (pdev->dev_speed == USBD_SPEED_HIGH)
{
/* Open EP IN */
USBD_LL_OpenEP(pdev, CDC_IN_EP, USBD_EP_TYPE_BULK,
CDC_DATA_HS_IN_PACKET_SIZE);
pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 1U;
/* Open EP OUT */
USBD_LL_OpenEP(pdev, CDC_OUT_EP, USBD_EP_TYPE_BULK,
CDC_DATA_HS_OUT_PACKET_SIZE);
pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 1U;
}
else
{
/* Open EP IN */
USBD_LL_OpenEP(pdev, CDC_IN_EP, USBD_EP_TYPE_BULK,
CDC_DATA_FS_IN_PACKET_SIZE);
pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 1U;
/* Open EP OUT */
USBD_LL_OpenEP(pdev, CDC_OUT_EP, USBD_EP_TYPE_BULK,
CDC_DATA_FS_OUT_PACKET_SIZE);
pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 1U;
}
/* Open Command IN EP */
USBD_LL_OpenEP(pdev, CDC_CMD_EP, USBD_EP_TYPE_INTR, CDC_CMD_PACKET_SIZE);
pdev->ep_in[CDC_CMD_EP & 0xFU].is_used = 1U;
pdev->pClassData = USBD_malloc(sizeof(USBD_CDC_HandleTypeDef));
if (pdev->pClassData == NULL)
{
ret = 1U;
}
else
{
hcdc = (USBD_CDC_HandleTypeDef *) pdev->pClassData;
/* Init physical Interface components */
((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Init();
/* Init Xfer states */
hcdc->TxState = 0U;
hcdc->RxState = 0U;
if (pdev->dev_speed == USBD_SPEED_HIGH)
{
/* Prepare Out endpoint to receive next packet */
USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer,
CDC_DATA_HS_OUT_PACKET_SIZE);
}
else
{
/* Prepare Out endpoint to receive next packet */
USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer,
CDC_DATA_FS_OUT_PACKET_SIZE);
}
}
// HID
/* Open EP IN */
USBD_LL_OpenEP(pdev, HID_EPIN_ADDR, USBD_EP_TYPE_INTR, HID_EPIN_SIZE);
pdev->ep_in[HID_EPIN_ADDR & 0xFU].is_used = 1U;
pdev->pClassData = USBD_malloc(sizeof(USBD_HID_HandleTypeDef));
if (pdev->pClassData == NULL)
{
return USBD_FAIL;
}
((USBD_HID_HandleTypeDef *)pdev->pClassData)->state = HID_IDLE;
return ret;
}
I am concerned that the value of CDC_IN_EP (0x81) clashes with HID_EPIN_ADDR (also 0x81).
Is this a valid concern?
Is there an example of making a composite CDC/HID using CubeMX?
I needed to use 0x83 as the HID endpoint. I confirmed that this worked in the simple HID example, too.
There are still many other obstacles in the way of getting a working CDC/HID composite device on STM32.

dsPic33E : UART without DMA example not working

I am using dsPic33EP512GM604. I have designed a test circuit to test UART Communication.
I have downloaded a sample code from Microchip website and modified accordingly for my device and circuit designed.
I am facing two issues while debugging.
PLL settings not working. Everytime it gets stuck at " while( OSCCONbits.COSC != 0b011 ); ". Hence I commented the clock configuration and Using simple Internal Oscillator FRC.
UART communication is not working. I m using RPI25 as an RX while RP20 as TX on my circuit.
Here is the final code I am using :
/*******************************************************************************/
#include <xc.h>
#include <stdint.h>
#if __XC16_VERSION < 1011
#warning "Please upgrade to XC16 v1.11 or newer."
#endif
//-----------------------------------------------------------------------------
#pragma config ICS = PGD3 // ICD Communication Channel Select bits (Communicate on PGEC1 and PGED1)
#pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)
// FPOR
#pragma config BOREN = ON // Brown-out Reset (BOR) Detection Enable bit (BOR is enabled)
#pragma config ALTI2C1 = OFF // Alternate I2C1 pins (I2C1 mapped to SDA1/SCL1 pins)
#pragma config ALTI2C2 = OFF // Alternate I2C2 pins (I2C2 mapped to SDA2/SCL2 pins)
#pragma config WDTWIN = WIN25 // Watchdog Window Select bits (WDT Window is 25% of WDT period)
// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler bits (1:32,768)
#pragma config WDTPRE = PR128 // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = OFF // PLL Lock Enable bit (Clock switch to PLL source will wait until the PLL lock signal is valid.)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)
// FOSC
#pragma config POSCMD = NONE // Primary Oscillator Mode Select bits (XT Crystal Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = OFF // Peripheral pin select configuration (Allow multiple reconfigurations)
#pragma config FCKSM = CSDCMD // Clock Switching Mode bits (Clock switching is enabled,Fail-safe Clock Monitor is disabled)
// FOSCSEL
#pragma config FNOSC = FRC // Oscillator Source Selection (Internal Fast RC (FRC))
#pragma config PWMLOCK = ON // PWM Lock Enable bit (Certain PWM registers may only be written after key sequence)
#pragma config IESO = ON // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)
// FGS
#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)
#pragma config GCP = OFF // General Segment Code-Protect bit (General Segment Code protect is Disabled)
// *****************************************************************************
#define TRUE 1
#define FALSE 0
#define DELAY_105uS asm volatile ("REPEAT, #4201"); Nop();// 105uS delay
// *****************************************************************************
#define FCY 60000000
#define BAUDRATE 9600
#define BRGVAL ( (FCY / BAUDRATE) / 16 ) - 1
uint8_t s3flag, s4flag, s5flag, S6Flag;
/*****************************************************************************/
void __attribute__ ( (interrupt, no_auto_psv) ) _U1RXInterrupt( void )
{
LATA = U1RXREG;
U1TXREG = LATA;
IFS0bits.U1RXIF = 0;
}
/******************************************************************************/
void __attribute__ ( (interrupt, no_auto_psv) ) _U1TXInterrupt( void )
{
IFS0bits.U1TXIF = 0;
}
/******************************************************************************/
void InitClock( void )
{
PLLFBD = 58; // M = 60
CLKDIVbits.PLLPOST = 0; // N1 = 2
CLKDIVbits.PLLPRE = 0; // N2 = 2
OSCTUN = 0;
RCONbits.SWDTEN = 0;
// Clock switch to incorporate PLL
__builtin_write_OSCCONH( 0x03 ); // Initiate Clock Switch to
// External oscillator with PLL (NOSC=0b011)
__builtin_write_OSCCONL( OSCCON || 0x01 ); // Start clock switching
while( OSCCONbits.COSC != 0b011 );
// Wait for Clock switch to occur
while( OSCCONbits.LOCK != 1 )
{ };
}
/******************************************************************************/
void InitUART2( void )
{
// configure U1MODE
U1MODEbits.UARTEN = 0; // Bit15 TX, RX DISABLED, ENABLE at end of func
//U1MODEbits.notimplemented;// Bit14
U1MODEbits.USIDL = 0; // Bit13 Continue in Idle
U1MODEbits.IREN = 0; // Bit12 No IR translation
U1MODEbits.RTSMD = 0; // Bit11 Simplex Mode
//U1MODEbits.notimplemented;// Bit10
U1MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not
U1MODEbits.WAKE = 0; // Bit7 No Wake up (since we don't sleep here)
U1MODEbits.LPBACK = 0; // Bit6 No Loop Back
U1MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55')
U1MODEbits.BRGH = 0; // Bit3 16 clocks per bit period
U1MODEbits.PDSEL = 0; // Bits1,2 8bit, No Parity
U1MODEbits.STSEL = 0; // Bit0 One Stop Bit
U1BRG = BRGVAL; // 60Mhz osc, 9600 Baud
// Load all values in for U1STA SFR
U1STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
U1STAbits.UTXINV = 0; //Bit14 N/A, IRDA config
U1STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15
//U1STAbits.notimplemented = 0;//Bit12
U1STAbits.UTXBRK = 0; //Bit11 Disabled
U1STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph
U1STAbits.UTXBF = 0; //Bit9 *Read Only Bit*
U1STAbits.TRMT = 0; //Bit8 *Read Only bit*
U1STAbits.URXISEL = 0; //Bits6,7 Int. on character recieved
U1STAbits.ADDEN = 0; //Bit5 Address Detect Disabled
U1STAbits.RIDLE = 0; //Bit4 *Read Only Bit*
U1STAbits.PERR = 0; //Bit3 *Read Only Bit*
U1STAbits.FERR = 0; //Bit2 *Read Only Bit*
U1STAbits.OERR = 0; //Bit1 *Read Only Bit*
U1STAbits.URXDA = 0; //Bit0 *Read Only Bit*
IPC7 = 0x4400; // Mid Range Interrupt Priority level, no urgent reason
IFS0bits.U1TXIF = 0; // Clear the Transmit Interrupt Flag
IEC0bits.U1TXIE = 1; // Enable Transmit Interrupts
IFS0bits.U1RXIF = 0; // Clear the Recieve Interrupt Flag
IEC0bits.U1RXIE = 1; // Enable Recieve Interrupts
// RPOR1bits.RP36R = 1; //RB4 as U1TX
// RPINR18bits.U1RXR = 24; //RA8 as U1RX
RPOR0bits.RP20R = 1; // dsPic33EP512GM604 => RP20 as U1TX
_U1RXR = 19; // dsPic33EP512GM604 => RPI25 as U1RX
U1MODEbits.UARTEN = 1; // And turn the peripheral on
U1STAbits.UTXEN = 1;
}
/******************************************************************************/
void InitPorts( void )
{
ANSELA = 0;
// TRISAbits.TRISA9 = 1;
// TRISAbits.TRISA4 = 0;
TRISAbits.TRISA10 = 0; //Output
}
/******************************************************************************
int main( void )
{
char recChar = 'a';
int i = 0;
// int count = 0;
// InitClock(); // This is the PLL settings
InitUART2(); // Initialize UART2 for 9600,8,N,1 TX/RX
InitPorts(); // LEDs outputs, Switches Inputs
/* Wait at least 105 microseconds (1/9600) before sending first char */
DELAY_105uS;
while( 1 )
{
PORTAbits.RA10 = 0;
for (i = 0; i < 1000; i++){
DELAY_105uS;
}
U1TXREG = recChar;
recChar++;
if (recChar == 122){
recChar = 48;
}
if (U1STAbits.OERR == 1){
U1STAbits.OERR = 0;
continue;
}
PORTAbits.RA10 = 0;
for (i = 0; i < 1000; i++){
DELAY_105uS;
}
}
}
/*******************************************************************************
I have tested the Circuit by adding LED at RA10 and its working. So, I guess there might be error in my code.
Got the Issues. Now its working
Edit the Test Code as follow :
void InitClock( void )
{
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD = 63; // M=65
CLKDIVbits.PLLPOST = 0; // N2=2
CLKDIVbits.PLLPRE = 0; // N1=2
// Initiate Clock Switch to FRC oscillator with PLL (NOSC=0b001)
__builtin_write_OSCCONH(0x01);
__builtin_write_OSCCONL(OSCCON | 0x01);
// Wait for Clock switch to occur
while (OSCCONbits.COSC!= 0b001);
// Wait for PLL to lock
while (OSCCONbits.LOCK!= 1);
}
void InitUART2( void )
{
// configure U1MODE
U1MODEbits.UARTEN = 0; // Bit15 TX, RX DISABLED, ENABLE at end of func
//U1MODEbits.notimplemented;// Bit10
U1MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not
U1MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55')
U1MODEbits.BRGH = 0; // Bit3 16 clocks per bit period
U1MODEbits.PDSEL = 0; // Bits1,2 8bit, No Parity
U1MODEbits.STSEL = 0; // Bit0 One Stop Bit
// Load a value into Baud Rate Generator.
U1BRG = BRGVAL; // 60Mhz osc, 9600 Baud
// Load all values in for U1STA SFR
U1STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
U1STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15
U1STAbits.UTXBRK = 0; //Bit11 Disabled
U1STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph
U1STAbits.URXISEL = 0; //Bits6,7 Int. on character recieved
IPC7 = 0x4400; // Mid Range Interrupt Priority level, no urgent reason
IFS0bits.U1TXIF = 0; // Clear the Transmit Interrupt Flag
IEC0bits.U1TXIE = 1; // Enable Transmit Interrupts
IFS0bits.U1RXIF = 0; // Clear the Recieve Interrupt Flag
IEC0bits.U1RXIE = 1; // Enable Recieve Interrupts
RPOR0bits.RP20R = 1; // dsPic33EP512GM604 => RP20 as U1TX
_U1RXR = 0x19; // dsPic33EP512GM604 => RPI25 as U1RX
U1MODEbits.UARTEN = 1; // And turn the peripheral on
U1STAbits.UTXEN = 1;
}
Applying these changes made my code working.

USB DUAL CDC endpoints for transferring data

I have emulated two VCPs with only one USB Device. I can send data in one VCP that has the following Endpoint Address.
#define CDC_IN_EP 0x81 /* EP1 for data IN */
#define CDC_OUT_EP 0x01 /* EP1 for data OUT */
#define CDC_CMD_EP 0x82 /* EP2 for CDC commands */
I would like to know if it's possible to send data in the other VCP with other Endpoint Address like:
#define CDC_IN_EP3 0x83 /* EP3 for data IN */
#define CDC_OUT_EP3 0x03 /* EP3 for data OUT */
#define CDC_CMD_EP4 0x84 /* EP4 for CDC commands */
My problem is that addresses others than 0x81 don't allow me to send data over the second VCP. I'm using two CDC interfaces Full-Speed. Normally we can use seven endpoints with CDC_FS and that's what I'm trying to do... I have six Endpoints: three for each VCP and the seventh is Endpoint 0 for configuration.
Have I missed something in the Endpoints Configuration or in the Composite Device Descriptors?
/* USB CDC device Configuration Descriptor */
__ALIGN_BEGIN uint8_t USBD_CDC_CfgFSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END =
{
/* Configuration Descriptor */
0x09, /* bLength: Configuration Descriptor size */
USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */
0x00,
0x04, /* bNumInterfaces: 4 interface */
0x01, /* bConfigurationValue: Configuration value */
0x00, /* iConfiguration: Index of string descriptor describing the configuration */
0xC0, /* bmAttributes: Self powered */
0x32, /* MaxPower 100 mA */
/*---------------------------------------------------------------------------*/
// IAD0
0x08, // bLength: Interface Descriptor size
0x0B, // bDescriptorType: IAD
0x00, // bFirstInterface
0x02, // bInterfaceCount
0x02, // bFunctionClass: CDC
0x02, // bFunctionSubClass
0x01, // bFunctionProtocol
0x02, // iFunction
/* Interface0 Descriptor */
0x09, /* bLength: Interface Descriptor size */
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */
/* Interface descriptor type */
0x00, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x01, /* bNumEndpoints: One endpoints used */
0x02, /* bInterfaceClass: Communication Interface Class */
0x02, /* bInterfaceSubClass: Abstract Control Model */
0x01, /* bInterfaceProtocol: Common AT commands */
0x00, /* iInterface: */
/* Header Functional Descriptor */
0x05, /* bLength: Endpoint Descriptor size */
0x24, /* bDescriptorType: CS_INTERFACE */
0x00, /* bDescriptorSubtype: Header Func Desc */
0x10, /* bcdCDC: spec release number */
0x01,
/* Call Management Functional Descriptor */
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x01, /* bDescriptorSubtype: Call Management Func Desc */
0x03, /* bmCapabilities: D0+D1 */
0x01, /* bDataInterface: 1 */
/* ACM Functional Descriptor */
0x04, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x02, /* bDescriptorSubtype: Abstract Control Management desc */
0x02, /* bmCapabilities */
/* Union Functional Descriptor */
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x06, /* bDescriptorSubtype: Union func desc */
0x00, /* bMasterInterface: Communication class interface */
0x01, /* bSlaveInterface0: Data Class Interface */
/* Endpoint 2 Descriptor */
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_CMD_EP, /* bEndpointAddress */
0x03, /* bmAttributes: Interrupt */
LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_CMD_PACKET_SIZE),
0xFF, /* bInterval: */
/*---------------------------------------------------------------------------*/
/*Data class interface descriptor*/
0x09, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */
0x01, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints: Two endpoints used */
0x0A, /* bInterfaceClass: CDC */
0x00, /* bInterfaceSubClass: */
0x00, /* bInterfaceProtocol: */
0x00, /* iInterface: */
/*Endpoint OUT Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_OUT_EP, /* bEndpointAddress */
0x02, /* bmAttributes: Bulk */
LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),
0x00, /* bInterval: ignore for Bulk transfer */
/*Endpoint IN Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_IN_EP, /* bEndpointAddress */
0x02, /* bmAttributes: Bulk */
LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),
0x00, /* bInterval: ignore for Bulk transfer */
/*---------------------------------------------------------------------------*/
// IAD1
0x08, // bLength: Interface Descriptor size
0x0B, // bDescriptorType: IAD
0x02, // bFirstInterface
0x02, // bInterfaceCount
0x02, // bFunctionClass: CDC
0x02, // bFunctionSubClass
0x01, // bFunctionProtocol
0x02, // iFunction
/* Interface1 Descriptor */
0x09, /* bLength: Interface Descriptor size */
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */
/* Interface descriptor type */
0x02, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x01, /* bNumEndpoints: One endpoints used */
0x02, /* bInterfaceClass: Communication Interface Class */
0x02, /* bInterfaceSubClass: Abstract Control Model */
0x01, /* bInterfaceProtocol: Common AT commands */
0x00, /* iInterface: */
/*Header Functional Descriptor*/
0x05, /* bLength: Endpoint Descriptor size */
0x24, /* bDescriptorType: CS_INTERFACE */
0x00, /* bDescriptorSubtype: Header Func Desc */
0x10, /* bcdCDC: spec release number */
0x01,
/*Call Management Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x01, /* bDescriptorSubtype: Call Management Func Desc */
0x03, /* bmCapabilities: D0+D1 */
0x03, /* bDataInterface: 1 */
/*ACM Functional Descriptor*/
0x04, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x02, /* bDescriptorSubtype: Abstract Control Management desc */
0x02, /* bmCapabilities */
/*Union Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x06, /* bDescriptorSubtype: Union func desc */
0x02, /* bMasterInterface: Communication class interface */
0x03, /* bSlaveInterface0: Data Class Interface */
/*Endpoint 2 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_CMD_EP4, /* bEndpointAddress */
0x03, /* bmAttributes: Interrupt */
LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_CMD_PACKET_SIZE),
0xFF, /* bInterval: */
/*---------------------------------------------------------------------------*/
/*Data class interface descriptor*/
0x09, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */
0x03, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints: Two endpoints used */
0x0A, /* bInterfaceClass: CDC */
0x00, /* bInterfaceSubClass: */
0x00, /* bInterfaceProtocol: */
0x00, /* iInterface: */
/*Endpoint OUT Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_OUT_EP3, /* bEndpointAddress */
0x02, /* bmAttributes: Bulk */
LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),
0x00, /* bInterval: ignore for Bulk transfer */
/*Endpoint IN Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
CDC_IN_EP3, /* bEndpointAddress */
0x02, /* bmAttributes: Bulk */
LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */
HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),
0x00 /* bInterval: ignore for Bulk transfer */
};
I had a similar problem with the STM32 USB device library. When you modify or add a new endpoint address in the configuration descriptor, you also have to set the endpoint address in the PMA (packet memory area). You can do that in the USBD_LL_INIT block with the HAL_PCDEx_PMAConfig function:
USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev)
{
/* Init USB_IP */
/* enable USB power on Pwrctrl CR2 register */
HAL_PWREx_EnableVddUSB();
/* Link The driver to the stack */
hpcd_USB_FS.pData = pdev;
pdev->pData = &hpcd_USB_FS;
hpcd_USB_FS.Instance = USB;
hpcd_USB_FS.Init.dev_endpoints = 8;
hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_8;
hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
hpcd_USB_FS.Init.Sof_enable = DISABLE;
hpcd_USB_FS.Init.low_power_enable = DISABLE;
hpcd_USB_FS.Init.lpm_enable = DISABLE;
hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
{
Error_Handler();
}
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x00 , PCD_SNG_BUF, 0x18);
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x80 , PCD_SNG_BUF, 0x58);
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x81 , PCD_SNG_BUF, 0xC0);
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x01 , PCD_SNG_BUF, 0x110);
HAL_PCDEx_PMAConfig((PCD_HandleTypeDef*)pdev->pData , 0x82 , PCD_SNG_BUF, 0x100);
return USBD_OK;
}
After that I could use the new endpoints for data transfer.

My USB device (microcontroller) prevents Windows from booting

I've been developing an application for the NXP LPC1788 microcontroller that involves communicating with a host PC using USB.
In general the application works fine. However, something I've noticed with multiple different computers is that if the microcontroller is powered-on and connected to a powered-off computer, and the computer is then turned on, the computer will not boot. Unplugging the microcontroller and trying to boot the computer again will resolve the problem.
Why is the microcontroller preventing the computer from booting? I've Googled the problem and it seems that this issue occurs when Windows tries to treat a USB device as bootable when it shouldn't be.
Is there a way I can prevent this happening with my application? What information should I provide to help others diagnose the problem?
In case it's useful, below is the micro's device descriptor:
/* USB Standard Device Descriptor */
const uint8_t USB_DeviceDescriptor[] = {
USB_DEVICE_DESC_SIZE, /* bLength */
USB_DEVICE_DESCRIPTOR_TYPE, /* bDescriptorType */
WBVAL(0x0200), /* 2.00 */ /* bcdUSB */
0xFF, /* bDeviceClass */
0x00, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
USB_MAX_PACKET_SIZE, /* bMaxPacketSize0 */
WBVAL(<...>), /* idVendor */
WBVAL(<...>), /* idProduct */
WBVAL(0x0100), /* 1.00 */ /* bcdDevice */
0x04, /* iManufacturer */
0x30, /* iProduct */
0x42, /* iSerialNumber */
0x01 /* bNumConfigurations */
};
Here is the configuration descriptor:
const uint8_t USB_ConfigDescriptor[] = {
/* Configuration 1 */
USB_CONFIGURATION_DESC_SIZE, /* bDescriptorType */
USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType */
WBVAL( /* wTotalLength */
1*USB_CONFIGURATION_DESC_SIZE +
1*USB_INTERFACE_DESC_SIZE +
2*USB_ENDPOINT_DESC_SIZE
),
0x01, /* bNumInterfaces */
0x01, /* bConfigurationValue */
0x00, /* iConfiguration */
USB_CONFIG_SELF_POWERED /*|*/ /* bmAttributes */
/*USB_CONFIG_REMOTE_WAKEUP*/,
USB_CONFIG_POWER_MA(100), /* bMaxPower */
/* Interface 0, Alternate Setting 0, MSC Class */
USB_INTERFACE_DESC_SIZE, /* bLength */
USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
0x00, /* bInterfaceNumber */
0x00, /* bAlternateSetting */
0x02, /* bNumEndpoints */
0xFF, /* bInterfaceClass */
0x0, /* bInterfaceSubClass */
0x0, /* bInterfaceProtocol */
0x5C, /* iInterface */
/* Bulk In Endpoint (data) */
USB_ENDPOINT_DESC_SIZE, /* bLength */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType */
USB_ENDPOINT_IN(2), /* bEndpointAddress */
USB_ENDPOINT_TYPE_BULK, /* bmAttributes */
WBVAL(0x0040), /* wMaxPacketSize */
0x0, /* bInterval */
/* Bulk Out Endpoint (data) */
USB_ENDPOINT_DESC_SIZE, /* bLength */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType */
USB_ENDPOINT_OUT(2), /* bEndpointAddress */
USB_ENDPOINT_TYPE_BULK, /* bmAttributes */
WBVAL(0x0040), /* wMaxPacketSize */
0x0, /* bInterval */
/* Terminator */
0 /* bLength */
};
EDIT
String descriptor (replaced letters with underscores):
const uint8_t USB_StringDescriptor[] = {
/* Index 0x00: LANGID Codes */
0x04, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
WBVAL(0x0409), /* US English */ /* wLANGID */
/* Index 0x04: Manufacturer */
0x2C, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
'_',0,
'_',0,
'_',0,
' ',0,
'_',0,
'_',0,
'_',0,
'_',0,
'_',0,
'_',0,
'_',0,
'_',0,
'_',0,
'_',0,
' ',0,
'_',0,
'_',0,
'_',0,
'_',0,
'_',0,
' ',0,
/* Index 0x30: Product */
0x14, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
'_',0,
'_',0,
'_',0,
'_',0,
'_',0,
'_',0,
'_',0,
'_',0,
' ',0,
/* Index 0x42: Serial Number */
0x1A, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
'0',0,
'0',0,
'0',0,
'0',0,
'0',0,
'0',0,
'0',0,
'0',0,
'0',0,
'0',0,
'0',0,
'0',0,
/* Index 0x5C: Interface 0, Alternate Setting 0 */
0x0E, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
'0',0,
'0',0,
'0',0,
'0',0,
'0',0,
'0',0,
};
EDIT 2
Changing iManufacturer, iProduct and iSerialNumber to 0x1, 0x2 and 0x3 respectively makes it so that USBLyzer doesn't fetch the string descriptors, which isn't a promising sign. If I use the original index values, it does. I haven't checked how this affects the computer's ability to boot.
EDIT 3
I noticed that my iSerialNumber and iInterface parameters were 2 off. 0x30+0x14 = 0x44.
I'll see if that's fixed anything.
EDIT 4
After correcting the two parameters above, my issue seems to be fixed. TurboJ brought my attention to the fact that index values are meant to be small e.g. 0x1, 0x2, 0x3...
NXP seem to have a different idea of how to do it and I copied their way, but I think I can modify the code at some later time to make it work like it does for other USB devices.
0x04, /* iManufacturer */
0x30, /* iProduct */
0x42, /* iSerialNumber */
Your iProduct and iSerialNumber bytes seem to be off - these are index numbers, ususally small ones. Your descriptor would require 66 strings in the string descriptor. These unusual high numbers could cause an overflow in a BIOS USB implementation.
I corrected my problem and improved over NXP's method.
NXP's example USB projects kept all the string descriptors in a single array. They then implemented the "USB Get Descriptor" functionality in the following way:
case USB_STRING_DESCRIPTOR_TYPE:
EP0Data.pData = (uint8_t *)USB_StringDescriptor + SetupPacket.wValue.WB.L;
len = ((USB_STRING_DESCRIPTOR *)EP0Data.pData)->bLength;
break;
This meant that they had to specify iManufacturer, iProduct, etc. using ridiculous index values such as (in their case) 0x04, 0x20 and 0x48.
In my own code, I modified their approach. I maintain the string descriptors as an array of arrays:
const uint8_t USB_StringDescriptor[][100] = {
{
/* Index 0x00: LANGID Codes */
0x04, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
WBVAL(0x0409), /* US English */ /* wLANGID */
},
{
/* Index 0x01: Manufacturer */
0x2A, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
// ...
},
{
/* Index 0x02: Product */
0x12, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
// ...
},
{
/* Index 0x03: Serial Number */
0x1A, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
// ...
},
{
/* Index 0x04: Interface 0, Alternate Setting 0 */
0x0E, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
// ...
}
};
I then change the way that string descriptor requests are processed:
case USB_STRING_DESCRIPTOR_TYPE:
EP0Data.pData = (uint8_t *)USB_StringDescriptor[SetupPacket.wValue.WB.L];
len = ((USB_STRING_DESCRIPTOR *)EP0Data.pData)->bLength;
break;
This way, within my device descriptor I can specify iManufacturer, iProduct, etc. as simply 0x01, 0x02...
I've tested this with two computers and both are able to boot successfully when my application is connected and running.

I'm looking for sample code to service the USCI (UART) on an MSP430 via DMA not interrupts

I have code that works "ok" for reading the USCI (UART) via interrupts, but the TI SimpliciTI stack is a CPU hog and it drops UART bytes when servicing the radio.
I assume DMA is the way to go, but I couldn't find a full example of DMA using USCI as input.
Here's what I ended up doing. It works!
struct {
#ifndef USE_DMA
volatile uint8_t rx_head ;
#endif
volatile uint8_t rx_tail ;
uint8_t rx_buffer[128];
} uart = { 0,0};
void UART_Init(void)
{
#ifndef USE_DMA
uart.rx_head = 0;
#endif
uart.rx_tail = 0;
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
PMAPPWD = 0x02D52; // Get write-access to port mapping regs
P1MAP5 = PM_UCA0RXD; // Map UCA0RXD output to P1.5
P1MAP6 = PM_UCA0TXD; // Map UCA0TXD output to P1.6
PMAPPWD = 0; // Lock port mapping registers
P1DIR |= BIT6; // Set P1.6 as TX output
P1SEL |= BIT5 + BIT6; // Select P1.5 & P1.6 to UART function
UCA0CTL1 = UCSWRST; // **Put state machine in reset**
#ifdef UART_9600
UCA0CTL1 |= UCSSEL_1; // CLK = ACLK
UCA0BR0 = 0x03; // 32kHz/9600=3.41 (see User's Guide)
UCA0BR1 = 0x00; //
UCA0MCTL = UCBRS_3+UCBRF_0; // Modulation UCBRSx=3, UCBRFx=0
#elif defined(UART_9600_SMCLK)
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 0xE2; // 12MHz/12500
UCA0BR1 = 0x04; //
UCA0MCTL = UCBRS_2+UCBRF_0; // Modulation UCBRSx=3, UCBRFx=0
#elif defined(UART_115200)
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 12MHz/115200
UCA0BR1 = 0; //
UCA0MCTL = UCBRS_1 + UCBRF_0; // Modulation UCBRSx=1, UCBRFx=0
#else
#error Please select one of the supported baudrates.
#endif
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
#ifdef USE_DMA
memset(uart.rx_buffer,0,sizeof(uart.rx_buffer));
DMACTL0 = DMA0TSEL_16; // USCIA0 RX trigger
DMA0SAL = (uint16_t) &UCA0RXBUF; // Source address
DMA0DAL = (uint16_t) uart.rx_buffer; // Destination address
DMA0SZ = sizeof(uart.rx_buffer); // Block size. this counts down to 0, then reloads.
DMA0CTL = DMADSTINCR_3 + DMASBDB + DMADT_4 + DMALEVEL;
DMA0CTL |= DMAEN;
#else
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
#endif
}
int UART_GetChar(void)
{
#ifdef USE_DMA
if (DMA0SZ + uart.rx_tail != sizeof(uart.rx_buffer))
#else
if ( uart.rx_head != uart.rx_tail )
#endif
{
int c;
c = uart.rx_buffer[uart.rx_tail];
uint8_t next = uart.rx_tail + 1;
if (next >= sizeof(uart.rx_buffer)) next = 0;
uart.rx_tail = next;
return c;
}
return -1;
}