-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhwt_io_ctl.py
executable file
·124 lines (98 loc) · 2.91 KB
/
hwt_io_ctl.py
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
#! /usr/bin/env python
"""
Control GPIO connected via a FTDI FT232R module using pylibftdi
Copyright (c) 2014 Lukasz Majewski <[email protected]>
License: GPLv2+
"""
import usb
import sys
import string
import argparse
import datetime
from pylibftdi import BitBangDevice
pins = ["TXD", "RXD", "RTS", "CTS", "DTR", "DSR", "DCD", "RI"]
def hwtIOOffAll(dev):
with BitBangDevice(dev) as bbdev:
bbdev.port = 0xFF
def hwtIOOnAll(dev):
with BitBangDevice(dev) as bbdev:
bbdev.port = 0x00
def hwtIOSetPinState(dev, pin, state, debug_en=False):
""" Execution routine """
dev = dev.upper()
pin = pin.upper()
state = state.upper()
if pin not in pins:
print 'HWT PWR - there is no "%s" pin available!' % pin
return
if state not in ["ON", "OFF"]:
print 'HWT PWR - state "%s" not supported!' % state
return
if debug_en:
print "PIN: %s [%s]" % (pin, state)
with BitBangDevice(dev) as bbdev:
tmp = bbdev.port
if debug_en:
sys.stdout.write("Current: 0x%x" % tmp)
idx = pins.index(pin)
if state == "OFF":
tmp |= (1 << idx)
else:
tmp &= ~(1 << idx)
if debug_en:
print " New: 0x%x" % tmp
bbdev.port = tmp
def hwtIOInitDevice(deviceSerial):
""" Initialization routine """
# idVendor=0x0403, idProduct=0x6001
# are for FTDI USB <-> Serial converter
# they shouldn't change -> compare iSerialNumber
dev = usb.core.find(find_all=True, idVendor=0x0403, idProduct=0x6001)
if dev is None:
print 'HWT PWR device is not connected!'
sys.exit()
for dev_instance in dev:
iSerialNumber = usb.util.get_string(dev_instance,
dev_instance.iSerialNumber)
if iSerialNumber == deviceSerial:
return
print "Device with serial: %s not found!" % deviceSerial
sys.exit()
def displayHelp():
print "\nFTDI232RL pins description:"
print "---------------------------"
i = 0
for pin in pins:
print "D%d:\t--->\t%s" % (i, pins[i])
i += 1
print "---------------------------\n"
print "One can also specify ALL to\n"
print "change state of all pins\n"
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=
'HW test (HWT) PWR control \
(via FTDI232R)',)
parser.add_argument('--debug',
action='store_true',
default=False,
dest='debug_en',
help='Enable printing debug information')
parser.add_argument('-d', '--device', required = True,
help='FT232 Device to connect')
parser.add_argument('-p', '--pin', required = True,
help='Pin to control - e.g. CTS, ALL')
parser.add_argument('state', nargs='?', default="OFF",
help='State - on/off')
args = parser.parse_args()
hwtIOInitDevice(args.device)
if args.pin.upper() == "ALL":
if args.state.upper() == "ON":
hwtIOOnAll(args.device)
else:
hwtIOOffAll(args.device)
sys.exit()
if args.debug_en:
print "HWT PWR control (FTDI232RL based) - %s" % \
str(datetime.datetime.now())[:19]
displayHelp()
hwtIOSetPinState(args.device, args.pin, args.state, args.debug_en)