|
10 | 10 | )
|
11 | 11 | from micropython import const
|
12 | 12 | import ustruct
|
| 13 | +import time |
13 | 14 |
|
14 | 15 | _DEV_CLASS_MISC = const(0xef)
|
15 | 16 | _CS_DESC_TYPE = const(0x24) # CS Interface type communication descriptor
|
@@ -69,18 +70,38 @@ def get_endpoint_descriptors(self, ep_addr, str_idx):
|
69 | 70 | class CDCDataInterface(USBInterface):
|
70 | 71 | # Implements the CDC Data Interface
|
71 | 72 |
|
72 |
| - def __init__(self, interface_str): |
| 73 | + def __init__(self, interface_str, timeout=1): |
73 | 74 | super().__init__(_CDC_ITF_DATA_CLASS, _CDC_ITF_DATA_SUBCLASS,
|
74 | 75 | _CDC_ITF_DATA_PROT)
|
| 76 | + self.rx_buf = bytearray(256) |
| 77 | + self.mv_buf = memoryview(self.rx_buf) |
| 78 | + self.rx_done = False |
| 79 | + self.rx_nbytes = 0 |
| 80 | + self.timeout = timeout |
75 | 81 |
|
76 | 82 | def get_endpoint_descriptors(self, ep_addr, str_idx):
|
77 | 83 | # XXX OUT = 0x00 but is defined as 0x80?
|
78 | 84 | self.ep_in = (ep_addr + 2) | EP_OUT_FLAG
|
79 | 85 | self.ep_out = (ep_addr + 2) & ~EP_OUT_FLAG
|
80 |
| - print("cdc in={} out={}".format(self.ep_in, self.ep_out)) |
81 | 86 | # one IN / OUT Endpoint
|
82 | 87 | e_out = endpoint_descriptor(self.ep_out, "bulk", 64, 0)
|
83 | 88 | e_in = endpoint_descriptor(self.ep_in, "bulk", 64, 0)
|
84 |
| - desc = e_out + e_in |
85 |
| - return (desc, [], (self.ep_out, self.ep_in)) |
86 |
| - |
| 89 | + return (e_out + e_in, [], (self.ep_out, self.ep_in)) |
| 90 | + |
| 91 | + def write(self, data): |
| 92 | + super().submit_xfer(self.ep_in, data) |
| 93 | + |
| 94 | + def read(self, nbytes=0): |
| 95 | + # XXX PoC.. When returning, it should probably |
| 96 | + # copy it to a ringbuffer instead of leaving it here |
| 97 | + super().submit_xfer(self.ep_out, self.rx_buf, self._cb_rx) |
| 98 | + now = time.time() |
| 99 | + self.rx_done = False |
| 100 | + self.rx_nbytes = 0 |
| 101 | + while ((time.time() - now) < self.timeout) and not self.rx_done: |
| 102 | + time.sleep_ms(10) |
| 103 | + return bytes(self.mv_buf[:self.rx_nbytes]) if self.rx_done else None |
| 104 | + |
| 105 | + def _cb_rx(self, ep, res, num_bytes): |
| 106 | + self.rx_done = True |
| 107 | + self.rx_nbytes = num_bytes |
0 commit comments