Skip to content
18 changes: 11 additions & 7 deletions micropython/usbd/cdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ class CDCControlInterface(USBInterface):
def __init__(self, interface_str):
super().__init__(_CDC_ITF_CONTROL_CLASS, _CDC_ITF_CONTROL_SUBCLASS,
_CDC_ITF_CONTROL_PROT)
self.ep_in = None

def get_itf_descriptor(self, num_eps, itf_idx, str_idx):
# CDC needs a Interface Association Descriptor (IAD)
# first interface is zero, two interfaces in total
desc = ustruct.pack("<BBBBBBBB", 8, _ITF_ASSOCIATION_DESC_TYPE, itf_idx, 2,
_CDC_ITF_CONTROL_CLASS, _CDC_ITF_CONTROL_SUBCLASS,
_CDC_ITF_CONTROL_PROT, 0) # "IAD"
_CDC_ITF_CONTROL_PROT, 4) # "IAD"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand, this is an index to a string descriptor describing the function. Do we have a string descriptor for that? Otherwise this value is 0.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly don't remember why I made it 4, just that it worked.


itf, strs = super().get_itf_descriptor(num_eps, itf_idx, str_idx)
desc += itf
Expand All @@ -59,12 +60,13 @@ def get_itf_descriptor(self, num_eps, itf_idx, str_idx):
desc += ustruct.pack("<BBBH", 5, _CS_DESC_TYPE, 0, 0x0120) # "Header"
desc += ustruct.pack("<BBBBB", 5, _CS_DESC_TYPE, 1, 0, 1) # "Call Management"
desc += ustruct.pack("<BBBB", 4, _CS_DESC_TYPE, 2, 2) # "Abstract Control"
desc += ustruct.pack("<BBBH", 5, _CS_DESC_TYPE, 6, itf_idx, 1) # "Union"
desc += ustruct.pack("<BBBBB", 5, _CS_DESC_TYPE, 6, itf_idx, itf_idx+1) # "Union"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, I corrected the itf_idx+1 already, but missed the struct pack pattern.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the big problem that fixed most things. It took me a while to realize it as well.

return desc, strs

def get_endpoint_descriptors(self, ep_addr, str_idx):
self.ep_in = endpoint_descriptor((ep_addr + 1) | EP_IN_FLAG, "interrupt", 8, 16)
return (self.ep_in, [], ((ep_addr+1) | EP_IN_FLAG,))
self.ep_in = (ep_addr) | EP_IN_FLAG
desc = endpoint_descriptor(self.ep_in, "interrupt", 8, 16)
return (desc, [], (self.ep_in,))
Comment on lines +71 to +73
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that looks good



class CDCDataInterface(USBInterface):
Expand All @@ -78,18 +80,20 @@ def __init__(self, interface_str, timeout=1):
self.rx_done = False
self.rx_nbytes = 0
self.timeout = timeout
self.ep_in = None
self.ep_out = None

def get_endpoint_descriptors(self, ep_addr, str_idx):
# XXX OUT = 0x00 but is defined as 0x80?
self.ep_in = (ep_addr + 2) | EP_IN_FLAG
self.ep_out = (ep_addr + 2) & ~EP_IN_FLAG
self.ep_in = (ep_addr) | EP_IN_FLAG
self.ep_out = (ep_addr) & ~EP_IN_FLAG
# one IN / OUT Endpoint
e_out = endpoint_descriptor(self.ep_out, "bulk", 64, 0)
e_in = endpoint_descriptor(self.ep_in, "bulk", 64, 0)
return (e_out + e_in, [], (self.ep_out, self.ep_in))

def write(self, data):
super().submit_xfer(self.ep_in, data)
self.submit_xfer(self.ep_in, data)

def read(self, nbytes=0):
# XXX PoC.. When returning, it should probably
Expand Down