|
| 1 | +"""Standard USB CDC ACM serial driver.""" |
| 2 | + |
| 3 | +import struct |
| 4 | + |
| 5 | +import usb.core |
| 6 | +import usb.util |
| 7 | + |
| 8 | +from serial_pyusb.drivers.base import USBSerialDriver |
| 9 | + |
| 10 | +# CDC class requests |
| 11 | +_SET_LINE_CODING = 0x20 |
| 12 | +_GET_LINE_CODING = 0x21 |
| 13 | +_SET_CONTROL_LINE_STATE = 0x22 |
| 14 | +_SEND_BREAK = 0x23 |
| 15 | + |
| 16 | +# Line coding: parity |
| 17 | +_PARITY = {"N": 0, "O": 1, "E": 2, "M": 3, "S": 4} |
| 18 | + |
| 19 | +# Line coding: stop bits |
| 20 | +_STOP_BITS = {1: 0, 1.5: 1, 2: 2} |
| 21 | + |
| 22 | +# USB CDC interface class/subclass |
| 23 | +_CDC_CLASS = 0x02 |
| 24 | +_CDC_ACM_SUBCLASS = 0x02 |
| 25 | +_CDC_DATA_CLASS = 0x0A |
| 26 | + |
| 27 | + |
| 28 | +class CDCACMDriver(USBSerialDriver): |
| 29 | + """Driver for standard USB CDC ACM serial devices.""" |
| 30 | + |
| 31 | + def __init__(self, dev: usb.core.Device): |
| 32 | + self._ctrl_intf = 0 |
| 33 | + self._data_intf = 1 |
| 34 | + self._dtr = False |
| 35 | + self._rts = False |
| 36 | + self._baudrate = 9600 |
| 37 | + self._stop_val = 0 |
| 38 | + self._parity_val = 0 |
| 39 | + self._data_bits = 8 |
| 40 | + super().__init__(dev) |
| 41 | + self._detect_interfaces() |
| 42 | + |
| 43 | + def _detect_interfaces(self): |
| 44 | + """Find the CDC control and data interfaces from descriptors.""" |
| 45 | + try: |
| 46 | + cfg = self.dev.get_active_configuration() |
| 47 | + except usb.core.USBError: |
| 48 | + self.dev.set_configuration() |
| 49 | + cfg = self.dev.get_active_configuration() |
| 50 | + for intf in cfg: |
| 51 | + if (intf.bInterfaceClass == _CDC_CLASS and |
| 52 | + intf.bInterfaceSubClass == _CDC_ACM_SUBCLASS): |
| 53 | + self._ctrl_intf = intf.bInterfaceNumber |
| 54 | + elif intf.bInterfaceClass == _CDC_DATA_CLASS: |
| 55 | + self._data_intf = intf.bInterfaceNumber |
| 56 | + |
| 57 | + def _get_data_interface(self, cfg): |
| 58 | + return cfg[(self._data_intf, 0)] |
| 59 | + |
| 60 | + def _send_line_state(self): |
| 61 | + """Send SET_CONTROL_LINE_STATE with current DTR/RTS.""" |
| 62 | + value = (int(self._dtr)) | (int(self._rts) << 1) |
| 63 | + if self.logger: |
| 64 | + self.logger.debug("cdc_acm SET_CONTROL_LINE_STATE val=0x%04x (DTR=%s RTS=%s)", value, self._dtr, self._rts) |
| 65 | + self.dev.ctrl_transfer( |
| 66 | + 0x21, _SET_CONTROL_LINE_STATE, value, self._ctrl_intf, timeout=1000 |
| 67 | + ) |
| 68 | + |
| 69 | + def open(self): |
| 70 | + self._dtr = True |
| 71 | + self._rts = True |
| 72 | + self._send_line_state() |
| 73 | + |
| 74 | + def close(self): |
| 75 | + self._dtr = False |
| 76 | + self._rts = False |
| 77 | + self._send_line_state() |
| 78 | + |
| 79 | + def _send_line_coding(self): |
| 80 | + """Send SET_LINE_CODING with current cached values.""" |
| 81 | + payload = struct.pack( |
| 82 | + "<IBBB", |
| 83 | + self._baudrate, |
| 84 | + self._stop_val, |
| 85 | + self._parity_val, |
| 86 | + self._data_bits, |
| 87 | + ) |
| 88 | + if self.logger: |
| 89 | + self.logger.debug( |
| 90 | + "cdc_acm SET_LINE_CODING baud=%d stop=%d parity=%d bits=%d payload=%s", |
| 91 | + self._baudrate, self._stop_val, self._parity_val, self._data_bits, |
| 92 | + payload.hex(), |
| 93 | + ) |
| 94 | + self.dev.ctrl_transfer( |
| 95 | + 0x21, _SET_LINE_CODING, 0, self._ctrl_intf, payload, timeout=1000 |
| 96 | + ) |
| 97 | + |
| 98 | + def set_baudrate(self, baudrate: int): |
| 99 | + self._baudrate = baudrate |
| 100 | + self._send_line_coding() |
| 101 | + |
| 102 | + def set_line_params(self, bytesize: int, parity: str, stopbits: float): |
| 103 | + self._stop_val = _STOP_BITS.get(stopbits, 0) |
| 104 | + self._parity_val = _PARITY.get(parity, 0) |
| 105 | + self._data_bits = bytesize |
| 106 | + self._send_line_coding() |
| 107 | + |
| 108 | + def set_dtr(self, state: bool): |
| 109 | + self._dtr = state |
| 110 | + self._send_line_state() |
| 111 | + |
| 112 | + def set_rts(self, state: bool): |
| 113 | + self._rts = state |
| 114 | + self._send_line_state() |
| 115 | + |
| 116 | + def set_break(self, state: bool): |
| 117 | + # SEND_BREAK: wValue = duration in ms, 0xFFFF = on until cleared, 0 = off |
| 118 | + value = 0xFFFF if state else 0x0000 |
| 119 | + if self.logger: |
| 120 | + self.logger.debug("cdc_acm SEND_BREAK val=0x%04x", value) |
| 121 | + self.dev.ctrl_transfer( |
| 122 | + 0x21, _SEND_BREAK, value, self._ctrl_intf, timeout=1000 |
| 123 | + ) |
| 124 | + |
| 125 | + @classmethod |
| 126 | + def matches(cls, dev: usb.core.Device) -> bool: |
| 127 | + """Match any device with a CDC ACM interface.""" |
| 128 | + try: |
| 129 | + cfg = dev.get_active_configuration() |
| 130 | + except usb.core.USBError: |
| 131 | + try: |
| 132 | + dev.set_configuration() |
| 133 | + cfg = dev.get_active_configuration() |
| 134 | + except usb.core.USBError: |
| 135 | + return False |
| 136 | + for intf in cfg: |
| 137 | + if (intf.bInterfaceClass == _CDC_CLASS and |
| 138 | + intf.bInterfaceSubClass == _CDC_ACM_SUBCLASS): |
| 139 | + return True |
| 140 | + return False |
0 commit comments