Created
January 6, 2021 17:42
-
-
Save dmitriy-serdyuk/8ec1db9d6d654a0391d105dbd54206bb to your computer and use it in GitHub Desktop.
CP2110 fake interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cp2110 | |
cp2110.cp2110 = cp2110 | |
class CP2110Serial: | |
def __init__(self, baud): | |
self.device = cp2110.CP2110Device() | |
self.device.set_uart_config(cp2110.UARTConfig( | |
baud=baud, | |
parity=cp2110.PARITY.NONE, | |
flow_control=cp2110.FLOW_CONTROL.ENABLED, | |
data_bits=cp2110.DATA_BITS.EIGHT, | |
stop_bits=cp2110.STOP_BITS.SHORT)) | |
if not self.device.is_uart_enabled(): | |
self.device.enable_uart() | |
self.bootloader_pin = 13 | |
self.reset_pin = 12 | |
def flushInput(self): | |
self.device.purge_fifos(cp2110.FIFO.BOTH) | |
def read(self, size=None): | |
return self.device.read(size) | |
def write(self, data): | |
return self.device.write(data) | |
def getDTR(self): | |
report, *pins = self.device.device.get_feature_report(0x44, 3) | |
assert report == 0x44 | |
return (int.from_bytes(pins, 'big') & (1 << self.bootloader_pin)) >> self.bootloader_pin | |
def getRTS(self): | |
report, *pins = self.device.device.get_feature_report(0x44, 3) | |
assert report == 0x44 | |
return (int.from_bytes(pins, 'big') & (1 << self.reset_pin)) >> self.reset_pin | |
def setDTR(self, value): | |
new_vals = int.to_bytes(value << self.bootloader_pin, 2, 'big') | |
self.device.device.send_feature_report( | |
cp2110.outbuf(0x45, *new_vals, *int.to_bytes(1 << self.bootloader_pin, 2, 'big'))) | |
def setRTS(self, value): | |
new_vals = int.to_bytes(value << self.reset_pin, 2, 'big') | |
self.device.device.send_feature_report( | |
cp2110.outbuf(0x45, *new_vals, *int.to_bytes(1 << self.reset_pin, 2, 'big'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment