forked from qualcomm/pytac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugboard.py
More file actions
729 lines (611 loc) · 23.8 KB
/
Copy pathdebugboard.py
File metadata and controls
729 lines (611 loc) · 23.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# Copyright (c)i 2025-2026 Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause
import json
import logging
import os
import re
import sys
from time import sleep
from types import MethodType
import pyudev
import serial
import usb
from pexpect import fdpexpect
from pyftdi.gpio import GpioAsyncController
logger = logging.getLogger()
try:
import hid
except ImportError as e:
# ignore importing hid. It's only required for bughopper v2.
logger.error(e)
pass
# wait time for other GPIOs to be truly set before touching the reset GPIO
PRE_RESET_DELAY = 0.1
BITMODE_CBUS = 0x20
# USB ctrl transfer request for FT230X CBUS
SIO_SET_BITMODE_REQUEST = 0x0B
class TACException(Exception):
status_code = 420
detail = "Unable to perform requested operation"
class Board(dict):
ID_VENDOR_FTDI = 0x0403
ID_PRODUCT_FTDI = 0x6011
ID_VENDOR_QCOM = 0x05C6
ID_PRODUCT_QCOM = 0x9302
ID_PRODUCT_BUGHOPPER_V1 = 0x6015
ID_VENDOR_BUGHOPPER_V2 = 0x2341
ID_PRODUCT_BUGHOPPER_V2 = 0xB001
@classmethod
def create_from_config(cls, config_file_path):
return DummyBoard(config_file_path)
@classmethod
def create_board(cls, serial, tac_config_path):
device = usb.core.find(serial_number=serial)
if device:
if (
device.idVendor == Board.ID_VENDOR_FTDI
and device.idProduct == Board.ID_PRODUCT_BUGHOPPER_V1
):
logger.debug("Found Bughopper V1")
return BughopperV1Board(device)
if (
device.idProduct == Board.ID_PRODUCT_FTDI
and device.idVendor == Board.ID_VENDOR_FTDI
):
logger.debug("Found FTDI Board")
return FtdiBoard(device, tac_config_path)
if (
device.idProduct == Board.ID_PRODUCT_QCOM
and device.idVendor == Board.ID_VENDOR_QCOM
):
logger.debug("Found Psoc Board")
return PsocBoard(device, tac_config_path)
if (
device.idVendor == Board.ID_VENDOR_BUGHOPPER_V2
and device.idProduct == Board.ID_PRODUCT_BUGHOPPER_V2
):
logger.debug("Found Bughopper V2")
if "hid" not in sys.modules:
logger.error(
"hid module not loaded. Check if the required packages are installed"
)
sys.exit(1)
return BughopperV2Board(device)
def __init__(self):
self.ports = {}
self.pins = {}
self.commands = {}
self.quick_methods = {}
self.usb_device = None
dict.__init__(
self, ports=self.ports, pins=self.pins, quick_methods=self.quick_methods
)
def logComment(self, comment):
print(comment)
def delay(self, length):
d = float(length / 1000)
self.logComment(f"Sleeping for {d} seconds")
sleep(d)
def create_pins(self):
raise NotImplementedError()
def create_ports(self):
raise NotImplementedError()
def parse_script(self):
if self.full_config:
initial_script = self.full_config["script"]
new_script = initial_script
# replace variables with actual values
variables = self.full_config.get("variables", [])
for variable in variables:
# create global variables
var_name = variable.get("name")
if var_name:
var_re = re.compile(rf"\${var_name}")
new_script = var_re.sub(variable["default_value"], new_script)
# remove commented lines
fix_comments = re.compile(r"\/\/.*")
new_script = fix_comments.sub("\r", new_script)
fix_comments = re.compile(r"\/\/.*\r")
new_script = fix_comments.sub("\r", new_script)
# fix function definitions
fix_functions = re.compile(r"\(\)[\s]?", re.MULTILINE)
new_script = fix_functions.sub("(self):\n", new_script)
fix_functions2 = re.compile(r"\(\)[\s]?\r", re.MULTILINE)
new_script = fix_functions2.sub("(self):\r", new_script)
# add brackets to function calls
fix_no_parenthesis = re.compile(
r"([A-Za-z0-9_]+)\s([0-9]+)\s?", re.MULTILINE
)
new_script = fix_no_parenthesis.sub(r"self.\1(\2)\n", new_script)
# fixes syntax of internal function calls
fix_no_parenthesis_func = re.compile(r"\t([A-Za-z0-9_]+)$", re.MULTILINE)
new_script = fix_no_parenthesis_func.sub(r"\tself.\1()", new_script)
fix_no_parenthesis_empty = re.compile(
r"self.([A-Za-z0-9_]+)\s?$", re.MULTILINE
)
new_script = fix_no_parenthesis_empty.sub(r"self.\1()", new_script)
fix_log_comment = re.compile(
r"logComment\s([a-zA-Z0-9=_\ ]+)\s?$", re.MULTILINE
)
new_script = fix_log_comment.sub(r'self.logComment("\1")', new_script)
d = {}
# nosemgrep: python.lang.security.audit.exec-detected.exec-detected
exec(new_script, d) # pylint: disable=exec-used
# create ports
self.create_ports()
# create pins
self.create_pins()
for name in d.keys(): # pylint: disable=consider-iterating-dictionary
if not name.startswith("__"):
logger.debug(f"Adding {name}")
self.quick_methods.update({name: QuickMethod(self, name)})
method = MethodType(d.get(name), self)
setattr(self, name, method)
class DummyBoard(Board):
def __init__(self, config_path):
Board.__init__(self)
self.config_path = config_path
self.usb_device = lambda: None
self.usb_device.serial_number = "123456"
with open(self.config_path) as cf:
self.full_config = json.loads(cf.read())
self.parse_script()
def create_ports(self):
logger.debug("creating ports")
if "FTDI" in self.config_path:
for p in self.full_config.get("bus"):
bus_name = p.get("bus")
if p.get("bus_function") == 2:
self.ports.update({bus_name: DummyPort(bus_name, "123456")})
if "PSOC" in self.config_path:
self.ports.update({0: DummyPort("123456", None)})
def create_pins(self):
logger.debug("creating pins")
for config in self.full_config.get("pins"):
pin = DummyPin(self, config)
pin.setPort(self.ports.get(0))
logger.debug(f"Adding {pin.command}")
self.pins.update({f"{pin.pin_number}": pin})
self.commands.update({f"{pin.command}": f"{pin.command}"})
setattr(self, pin.command, pin.set)
class BughopperV1Board(Board):
def __init__(self, usb_device):
Board.__init__(self)
self.usb_device = usb_device
self.quick_methods.update({"powerOn": QuickMethod(self, "powerOn")})
self.quick_methods.update({"bootToEDL": QuickMethod(self, "bootToEDL")})
self.quick_methods.update({"powerOff": QuickMethod(self, "powerOff")})
self.quick_methods.update({"reset": QuickMethod(self, "reset")})
self.quick_methods.update(
{"forceUsbcHostMode": QuickMethod(self, "forceUsbcHostMode")}
)
self.EDL_BIT = 0b00000001
self.POWER_DISABLE_BIT = 0b00000100
self.VOL_DOWN_BIT = 0b00001000
self.EDL_MASK = self.EDL_BIT << 4
self.POWER_DISABLE_MASK = self.POWER_DISABLE_BIT << 4
self.VOL_DOWN_MASK = self.VOL_DOWN_BIT << 4
def _ftdi_set_bitmode(self, bitmask):
bmRequestType = usb.util.build_request_type(
usb.util.CTRL_OUT, usb.util.CTRL_TYPE_VENDOR, usb.util.CTRL_RECIPIENT_DEVICE
)
wValue = bitmask | (BITMODE_CBUS << 8)
self.usb_device.ctrl_transfer(bmRequestType, SIO_SET_BITMODE_REQUEST, wValue)
def powerOn(self):
logger.debug("Power cycle to normal boot mode")
self._ftdi_set_bitmode(self.POWER_DISABLE_MASK | self.POWER_DISABLE_BIT)
sleep(PRE_RESET_DELAY)
self._ftdi_set_bitmode(
self.POWER_DISABLE_MASK | self.EDL_MASK | self.VOL_DOWN_MASK
)
def bootToEDL(self):
logger.debug("Power cycle to USB boot mode")
self._ftdi_set_bitmode(self.POWER_DISABLE_MASK | self.POWER_DISABLE_BIT)
sleep(PRE_RESET_DELAY)
self._ftdi_set_bitmode(self.POWER_DISABLE_MASK | self.EDL_MASK | self.EDL_BIT)
def reset(self):
logger.debug("MPU reset pulse")
self._ftdi_set_bitmode(self.POWER_DISABLE_MASK | self.POWER_DISABLE_BIT)
sleep(PRE_RESET_DELAY)
self._ftdi_set_bitmode(
self.POWER_DISABLE_MASK | self.EDL_MASK | self.VOL_DOWN_MASK
)
def powerOff(self):
logger.debug("MPU poweroff")
self._ftdi_set_bitmode(self.POWER_DISABLE_MASK | self.POWER_DISABLE_BIT)
def forceUsbcHostMode(self):
logger.debug("Forcing host mode")
self._ftdi_set_bitmode(
self.POWER_DISABLE_MASK
| self.VOL_DOWN_MASK
| self.POWER_DISABLE_BIT
| self.VOL_DOWN_BIT
)
sleep(PRE_RESET_DELAY)
self._ftdi_set_bitmode(
self.POWER_DISABLE_MASK
| self.EDL_MASK
| self.VOL_DOWN_MASK
| self.VOL_DOWN_BIT
)
class BughopperV2Board(Board):
def __init__(self, usb_device):
Board.__init__(self)
self.usb_device = hid.Device(usb_device.idVendor, usb_device.idProduct)
self.usb_device.serial_number = self.usb_device.serial
self.quick_methods.update({"powerOn": QuickMethod(self, "powerOn")})
self.quick_methods.update({"bootToEDL": QuickMethod(self, "bootToEDL")})
self.quick_methods.update({"powerOff": QuickMethod(self, "powerOff")})
self.quick_methods.update({"reset": QuickMethod(self, "reset")})
self.quick_methods.update(
{"forceUsbcHostMode": QuickMethod(self, "forceUsbcHostMode")}
)
self.CMD_GPIO = 0x1
self.EDL_BIT = 0x1
self.POWER_DISABLE_BIT = 0x4
self.VOL_DOWN_BIT = 0x8
def _hid_set_bitmode(self, command, gpio_value, mask=0xF):
self.usb_device.write(bytes([command, gpio_value, mask]))
def powerOn(self):
logger.debug("Power cycle to normal boot mode")
self._hid_set_bitmode(
self.CMD_GPIO, self.POWER_DISABLE_BIT, self.POWER_DISABLE_BIT
)
sleep(PRE_RESET_DELAY)
self._hid_set_bitmode(
self.CMD_GPIO,
0x0,
self.POWER_DISABLE_BIT | self.EDL_BIT | self.VOL_DOWN_BIT,
)
def bootToEDL(self):
logger.debug("Power cycle to USB boot mode")
self._hid_set_bitmode(
self.CMD_GPIO, self.POWER_DISABLE_BIT, self.POWER_DISABLE_BIT
)
sleep(PRE_RESET_DELAY)
self._hid_set_bitmode(
self.CMD_GPIO, self.EDL_BIT, self.EDL_BIT | self.POWER_DISABLE_BIT
)
def reset(self):
logger.debug("MPU reset pulse")
self._hid_set_bitmode(
self.CMD_GPIO, self.POWER_DISABLE_BIT, self.POWER_DISABLE_BIT
)
sleep(PRE_RESET_DELAY)
self._hid_set_bitmode(
self.CMD_GPIO,
0x0,
self.POWER_DISABLE_BIT | self.EDL_BIT | self.VOL_DOWN_BIT,
)
def powerOff(self):
logger.debug("MPU poweroff")
self._hid_set_bitmode(
self.CMD_GPIO, self.POWER_DISABLE_BIT, self.POWER_DISABLE_BIT
)
def forceUsbcHostMode(self):
logger.debug("Forcing host mode")
self._hid_set_bitmode(
self.CMD_GPIO,
self.POWER_DISABLE_BIT | self.VOL_DOWN_BIT,
self.POWER_DISABLE_BIT | self.VOL_DOWN_BIT,
)
sleep(PRE_RESET_DELAY)
self._hid_set_bitmode(
self.CMD_GPIO,
self.VOL_DOWN_BIT,
self.POWER_DISABLE_BIT | self.VOL_DOWN_BIT | self.EDL_BIT,
)
class FtdiBoard(Board):
def __init__(self, usb_device, tac_config_path):
Board.__init__(self)
self.usb_device = usb_device
conf = os.path.join(
tac_config_path, "TAC_FTDI_13.tcnf"
) # default config for FTDI Alpaca-lite
f = open(os.path.join(tac_config_path, "devicelist.json"), "r")
device_list = json.loads(f.read())
f.close()
catalog = device_list.get("catalog")
conf_dict = next(
(x for x in catalog if x.get("usb_descriptor") == self.usb_device.product),
None,
)
if conf_dict:
conf = os.path.join(
tac_config_path, os.path.basename(conf_dict.get("configPath"))
)
if conf is None:
logger.error("No matching FTDI config found")
sys.exit(1)
f = open(conf, "rb")
self.full_config = json.loads(f.read())
f.close()
self.parse_script()
def create_ports(self):
for p in self.full_config.get("bus"):
bus_name = p.get("bus")
if p.get("bus_function") == 2:
self.ports.update(
{bus_name: FtdiPort(bus_name, self.usb_device.serial_number)}
)
def create_pins(self):
for p in self.full_config.get("pins"):
if not p.get("enabled", True):
continue
pin = FtdiPin(self, p)
pin.setPort(self.ports.get(pin.bus))
logger.debug(f"Adding {pin.command}")
self.pins.update({f"{pin.bus}{pin.pin_number}": pin})
self.commands.update({f"{pin.command}": f"{pin.command}"})
setattr(self, pin.command, pin.set)
class PsocBoard(Board):
def __init__(self, usb_device, tac_config_path):
Board.__init__(self)
self.usb_device = usb_device
conf = None
f = open(os.path.join(tac_config_path, "devicelist.json"), "r")
device_list = json.loads(f.read())
f.close()
catalog = device_list.get("catalog")
self.board_id = self.__get_board_id()
conf_dict = next(
(x for x in catalog if x.get("platform_id") == self.board_id), None
)
if conf_dict:
conf = os.path.join(
tac_config_path, os.path.basename(conf_dict.get("configPath"))
)
if conf is None:
logger.error("No matching PSOC config found")
sys.exit(1)
f = open(conf, "rb")
self.full_config = json.loads(f.read())
f.close()
self.parse_script()
self.quick_methods.update({"devicePowerOn": QuickMethod(self, "devicePowerOn")})
self.quick_methods.update(
{"devicePowerOff": QuickMethod(self, "devicePowerOff")}
)
self.quick_methods.update(
{"usbDevicePowerOn": QuickMethod(self, "usbDevicePowerOn")}
)
self.quick_methods.update(
{"usbDevicePowerOff": QuickMethod(self, "usbDevicePowerOff")}
)
def devicePower(self, value):
logger.debug(f"Calling devicePower {value}")
if value:
self.ports[0].call_method("devicePower", "on")
else:
self.ports[0].call_method("devicePower", "off")
def usbDevicePower(self, value):
logger.debug(f"Calling usbDevicePower {value}")
if value:
self.ports[0].call_method("usbDevicePower", "on")
else:
self.ports[0].call_method("usbDevicePower", "off")
def devicePowerOn(self):
self.devicePower(True)
def devicePowerOff(self):
self.devicePower(False)
def usbDevicePowerOn(self):
self.usbDevicePower(True)
def usbDevicePowerOff(self):
self.usbDevicePower(False)
def __get_board_id(self):
# connect to serial and call "getboardid"
serial_port = None
context = pyudev.Context()
for d in context.list_devices(ID_SERIAL_SHORT=self.usb_device.serial_number):
for l in d.device_links:
serial_port = l
break
logger.info(f"Opening {serial_port}")
expect_connection = None
connection = serial.Serial()
connection.baudrate = 115200
connection.port = serial_port
try:
connection.open()
except serial.SerialException as e:
logger.error("Serial Exception")
logger.error(e)
sys.exit(1)
expect_connection = fdpexpect.fdspawn(connection)
if not expect_connection.isalive():
logger.error("Expect connection not created")
sys.exit(1)
expect_connection.send("\r")
expect_connection.expect("CMD")
expect_connection.send("getboardid\r")
expect_connection.expect("ok")
ret_value = expect_connection.before
expect_connection.close()
# find returned value
r = re.search(r"\d+", ret_value.decode())
if r:
return int(r.group(0))
return None
def create_ports(self):
self.ports.update({0: PsocPort(self.usb_device.serial_number)})
def create_pins(self):
for config in self.full_config.get("pins"):
if not config.get("enabled", True):
continue
pin = PsocPin(self, config)
pin.setPort(self.ports.get(0))
logger.debug(f"Adding {pin.command}")
self.pins.update({f"{pin.pin_number}": pin})
self.commands.update({f"{pin.command}": f"{pin.command}"})
setattr(self, pin.command, pin.set)
class QuickMethod(dict):
def __init__(self, board, name):
self.name = name
self.board = board
dict.__init__(self, name=self.name, board=self.board.usb_device.serial_number)
def call(self):
try:
getattr(self.board, self.name)()
except Exception as e:
logger.error(e)
raise TACException
return {}
class Pin(dict):
def __init__(self, board, config):
self.board = board
self.bus = config.get("bus")
self.command = config.get("command")
self.initial_value = config.get("initial_value")
self.value = self.initial_value
self.input = config.get("input")
self.inverted = config.get("inverted")
self.pin_number = int(config.get("pin_number"))
self.help_hint = config.get("help_hint")
self.port = None
dict.__init__(
self,
bus=self.bus,
command=self.command,
initial_value=self.initial_value,
value=self.value,
input=self.input,
inverted=self.inverted,
pin_number=self.pin_number,
help_hint=self.help_hint,
port=self.port,
)
def set(self, value):
self.value = value
super().__setitem__("value", self.value)
def initialize(self):
raise NotImplementedError()
def setPort(self, port):
self.port = port
super().__setitem__("port", self.port)
class DummyPin(Pin):
def initialize(self):
logger.info(f"Initializing pin: {self.bus} {self.pin_number}")
class FtdiPin(Pin):
def __init__(self, board, config):
Pin.__init__(self, board, config)
self.pin_number_mask = 1 << self.pin_number
def set(self, value):
super().set(value)
if not self.port:
logger.warning(f"No port set for pin {self.bus}{self.pin_number}")
return
logger.debug(f"Port {self.bus} status: 0x{self.port.status:02X}")
logger.debug(f"Setting {self.bus}{self.pin_number} to {value}")
logger.debug(f"Mask: 0x{self.pin_number_mask:02X}")
if self.value:
self.port.write(self.port.status | self.pin_number_mask)
else:
self.port.write(self.port.status & ~self.pin_number_mask)
def initialize(self):
self.set(int(self.initial_value))
def setPort(self, port):
super().setPort(port)
self.initialize()
class PsocPin(Pin):
def __init__(self, board, config):
Pin.__init__(self, board, config)
def set(self, value):
lcl_value = None
try:
lcl_value = int(value)
except ValueError:
logger.error("Value has to be int")
logger.error(f"Received {value}")
return
super().set(lcl_value)
if not self.port:
logger.warning(f"No port set for pin {self.pin_number}")
return
logger.debug(f"Setting {self.pin_number} to {lcl_value}")
self.port.write(lcl_value, self.pin_number)
def initialize(self):
self.set(int(self.initial_value))
def setPort(self, port):
super().setPort(port)
self.initialize()
class Port(dict):
def __init__(self, bus, serial):
self.serial = serial
self.bus = bus
dict.__init__(self, serial=self.serial, bus=self.bus)
def write(self, value, pin=None):
raise NotImplementedError()
def close(self):
raise NotImplementedError()
class DummyPort(Port):
def write(self, value, pin=None):
logger.info(f"Writing to port {self.bus} {self.serial}")
def close(self):
logger.info(f"Closing port {self.bus}")
class PsocPort(Port):
def __init__(self, serialid):
Port.__init__(self, None, serialid)
self.serial_port = None
context = pyudev.Context()
for d in context.list_devices(ID_SERIAL_SHORT=serialid):
for l in d.device_links:
self.serial_port = l
break
self.expect_connection = None
self.connection = serial.Serial()
self.connection.baudrate = 115200
self.connection.port = self.serial_port
try:
self.connection.open()
except serial.SerialException as e:
logger.error("Serial Exception")
logger.error(e)
sys.exit(1)
self.expect_connection = fdpexpect.fdspawn(self.connection)
if not self.expect_connection.isalive():
logger.error("Expect connection not created")
sys.exit(1)
def close(self):
if self.connection and self.connection.is_open:
self.connection.close()
def call_method(self, method, value):
logger.debug(f"Calling {method} {value}")
if self.expect_connection and self.expect_connection.isalive():
self.expect_connection.send("\r")
self.expect_connection.expect("CMD")
message = f"{method} {value}\r"
self.expect_connection.send(message)
self.expect_connection.expect("ok")
else:
logger.error("No expect connection")
sys.exit(1)
def write(self, value, pin=None):
if not pin:
logger.warning("No pin selected")
return
if self.expect_connection and self.expect_connection.isalive():
self.expect_connection.send("\r")
self.expect_connection.expect("CMD")
message = f"pin {value} {pin}\r"
self.expect_connection.send(message)
self.expect_connection.expect("ok")
else:
logger.error("No expect connection")
sys.exit(1)
class FtdiPort(Port):
def __init__(self, bus, serial, direction=0xFF):
Port.__init__(self, bus, serial)
self.direction = direction
self.port = ord(self.bus) - ord("A") + 1
self.url = f"ftdi://::{self.serial}/{self.port}"
self.pins = {}
self.status = 0x0
self.gpio = GpioAsyncController()
self.gpio.configure(self.url, direction=self.direction)
def write(self, value, pin=None):
self.status = value
logger.debug(f"Port {self.bus} value: 0x{self.status:02X}")
self.gpio.write(self.status)
def close(self):
logger.debug(f"Closing port {self.bus}")
self.gpio.close()