Skip to content

Commit 5630084

Browse files
author
Michael Sparmann
committed
- Load usb/serial libraries only when used
- FTDIJTAG: Accept devices that have a dead JTAG bus - Various small fixes - Whitespace cleanup
1 parent 101ac10 commit 5630084

File tree

10 files changed

+78
-72
lines changed

10 files changed

+78
-72
lines changed

modules/theseven/bflsingle/bflsinglehotplug.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,21 @@
3636

3737
# Worker main class, referenced from __init__.py
3838
class BFLSingleHotplugWorker(BaseWorker):
39-
39+
4040
version = "theseven.bflsingle hotplug manager v0.1.0beta"
4141
default_name = "BFL Single hotplug manager"
4242
can_autodetect = True
4343
settings = dict(BaseWorker.settings, **{
4444
"scaninterval": {"title": "Bus scan interval", "type": "float", "position": 2200},
4545
})
46-
47-
46+
47+
4848
@classmethod
4949
def autodetect(self, core):
5050
try:
5151
import serial
5252
found = False
53-
for port in glob("/dev/serial/by-id/usb-Butterfly_Labs_Inc._BitFORCE_SHA256*"):
53+
for port in glob("/dev/serial/by-id/usb-Butterfly_Labs_Inc._BitFORCE_SHA256-*"):
5454
try:
5555
handle = serial.Serial(port, 115200, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE, 1, False, False, 5, False, None)
5656
handle.close()
@@ -59,8 +59,8 @@ def autodetect(self, core):
5959
except: pass
6060
if found: core.add_worker(self(core))
6161
except: pass
62-
63-
62+
63+
6464
# Constructor, gets passed a reference to the miner core and the saved worker state, if present
6565
def __init__(self, core, state = None):
6666
# Initialize bus scanner wakeup event
@@ -69,7 +69,7 @@ def __init__(self, core, state = None):
6969
# Let our superclass do some basic initialization and restore the state if neccessary
7070
super(BFLSingleHotplugWorker, self).__init__(core, state)
7171

72-
72+
7373
# Validate settings, filling them with default values if neccessary.
7474
# Called from the constructor and after every settings change.
7575
def apply_settings(self):
@@ -78,7 +78,7 @@ def apply_settings(self):
7878
if not "scaninterval" in self.settings or not self.settings.scaninterval: self.settings.scaninterval = 10
7979
# Rescan the bus immediately to apply the new settings
8080
with self.wakeup: self.wakeup.notify()
81-
81+
8282

8383
# Reset our state. Called both from the constructor and from self.start().
8484
def _reset(self):
@@ -100,8 +100,8 @@ def _start(self):
100100
self.mainthread = Thread(None, self.main, self.settings.name + "_main")
101101
self.mainthread.daemon = True
102102
self.mainthread.start()
103-
104-
103+
104+
105105
# Shut down the worker module. This is protected against multiple calls and concurrency by a wrapper.
106106
def _stop(self):
107107
# Let our superclass handle everything that isn't specific to this worker module
@@ -121,34 +121,33 @@ def _stop(self):
121121
except Exception as e:
122122
self.core.log(self, "Could not stop worker %s: %s\n" % (child.settings.name, traceback.format_exc()), 100, "rB")
123123

124-
124+
125125
# Main thread entry point
126126
# This thread is responsible for scanning for boards and spawning worker modules for them
127127
def main(self):
128+
import serial
128129
number = 0
129-
130+
130131
# Loop until we are shut down
131132
while not self.shutdown:
132-
133-
import serial
134133

135134
try:
136135
boards = {}
137-
for port in glob("/dev/serial/by-id/usb-Butterfly_Labs_Inc._BitFORCE_SHA256*"):
136+
for port in glob("/dev/serial/by-id/usb-Butterfly_Labs_Inc._BitFORCE_SHA256-*"):
138137
available = False
139138
try:
140139
handle = serial.Serial(port, 115200, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE, 1, False, False, 5, False, None)
141140
handle.close()
142141
available = True
143142
except: pass
144143
boards[port] = available
145-
144+
146145
kill = []
147-
for serial, child in self.childmap.items():
148-
if not serial in boards:
149-
kill.append((serial, child))
150-
151-
for serial, child in kill:
146+
for port, child in self.childmap.items():
147+
if not port in boards:
148+
kill.append((port, child))
149+
150+
for port, child in kill:
152151
try:
153152
self.core.log(self, "Shutting down worker %s...\n" % (child.settings.name), 800)
154153
child.stop()
@@ -162,7 +161,7 @@ def main(self):
162161
del self.childmap[port]
163162
try: self.children.remove(child)
164163
except: pass
165-
164+
166165
for port, available in boards.items():
167166
if port in self.childmap or not available: continue
168167
number += 1
@@ -177,7 +176,7 @@ def main(self):
177176
child.start()
178177
except Exception as e:
179178
self.core.log(self, "Could not start worker %s: %s\n" % (child.settings.name, traceback.format_exc()), 100, "rB")
180-
179+
181180
except: self.core.log(self, "Caught exception: %s\n" % traceback.format_exc(), 100, "rB")
182-
181+
183182
with self.wakeup: self.wakeup.wait(self.settings.scaninterval)

modules/theseven/bflsingle/bflsingleworker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828

2929
import os
30-
import serial
3130
import time
3231
import traceback
3332
from threading import Condition, Thread
@@ -154,6 +153,7 @@ def main(self):
154153
self.job = None
155154

156155
# Open the serial port
156+
import serial
157157
self.handle = serial.Serial(self.port, 115200, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE, 1, False, False, 5, False, None)
158158

159159
# We keep control of the wakeup lock at all times unless we're sleeping

modules/theseven/ftdijtag/driver.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,15 @@ def __init__(self, proxy, deviceid, takeover, firmware):
411411
script["tdomask"] = byte2int(script["tdo"])
412412
script["ir_to_dr"] = script["leave_shift"] + script["shift_dr"]
413413
self.outmask |= script["tckmask"] | script["tmsmask"] | script["tdimask"]
414-
self.handle.controlMsg(0x40, 3, None, 0, 0, 1000)
414+
self.handle.controlMsg(0x40, 3, None, 0, 0)
415415
self._switch_async()
416+
self.initialized = {}
416417
self.busdevices = {}
417418
self.devices = []
418-
for bus in sorted(self.jtagscript.keys()): self._init_bus(bus)
419+
for bus in sorted(self.jtagscript.keys()):
420+
self.initialized[bus] = False
421+
try: self._init_bus(bus)
422+
except Exception as e: self.proxy.log("%s\n" % e, 150, "rB")
419423

420424

421425
def _init_bus(self, bus):
@@ -487,6 +491,7 @@ def _init_bus(self, bus):
487491
+ script["shift_dr"]
488492
script["readnonce_pull"] = self._tmstail(bus, clock * readnonce_pull_len)
489493
script["readnonce_tail"] = script["leave_shift"]
494+
self.initialized[bus] = True
490495

491496

492497
def register(self, device):
@@ -542,16 +547,16 @@ def _tmstail(self, bus, data):
542547

543548

544549
def _purge_buffers(self):
545-
self.handle.controlMsg(0x40, 0, None, 1, self.index, 1000)
546-
self.handle.controlMsg(0x40, 0, None, 2, self.index, 1000)
550+
self.handle.controlMsg(0x40, 0, None, 1, self.index)
551+
self.handle.controlMsg(0x40, 0, None, 2, self.index)
547552

548553

549554
def _set_bit_mode(self, mask, mode):
550-
self.handle.controlMsg(0x40, 0xb, None, (mode << 8) | mask, self.index, 1000)
555+
self.handle.controlMsg(0x40, 0xb, None, (mode << 8) | mask, self.index)
551556

552557

553558
def _get_bit_mode(self):
554-
return struct.unpack("B", bytes(bytearray(self.handle.controlMsg(0xc0, 0xc, 1, 0, self.index, 1000))))[0]
559+
return struct.unpack("B", bytes(bytearray(self.handle.controlMsg(0xc0, 0xc, 1, 0, self.index))))[0]
555560

556561

557562
def _switch_async(self):
@@ -569,7 +574,7 @@ def _write(self, data):
569574
offset = 0
570575
while offset < size:
571576
write_size = min(4096, size - offset)
572-
ret = self.handle.bulkWrite(self.outep, data[offset : offset + write_size], 1000)
577+
ret = self.handle.bulkWrite(self.outep, data[offset : offset + write_size])
573578
offset = offset + ret
574579

575580

@@ -578,7 +583,7 @@ def _read(self, size, timeout = 1):
578583
data = b""
579584
offset = 0
580585
while offset < size and time.time() < timeout:
581-
ret = bytes(bytearray(self.handle.bulkRead(self.inep, min(64, size - offset + 2), 1000)))
586+
ret = bytes(bytearray(self.handle.bulkRead(self.inep, min(64, size - offset + 2))))
582587
data += ret[2:]
583588
offset += len(ret) - 2
584589
return data
@@ -637,6 +642,7 @@ def read_nonces(self):
637642
nonces = {}
638643
for bus in self.jtagscript:
639644
script = self.jtagscript[bus]
645+
if not self.initialized[bus]: continue
640646
with self.lock:
641647
self._write(script["readnonce_head"])
642648
data = self._shift(bus, script["readnonce_pull"])

modules/theseven/ftdijtag/ftdijtaghotplug.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727

2828

29-
import usb
3029
import traceback
3130
from threading import Condition, Thread
3231
from core.baseworker import BaseWorker
@@ -75,6 +74,7 @@ class FTDIJTAGHotplugWorker(BaseWorker):
7574
def autodetect(self, core):
7675
return #TODO: Remove modules/fpgamining tree and activate this once ztexmerge has been evicted
7776
try:
77+
import usb
7878
found = False
7979
try:
8080
import usb
@@ -196,6 +196,8 @@ def _stop(self):
196196
# Main thread entry point
197197
# This thread is responsible for scanning for boards and spawning worker modules for them
198198
def main(self):
199+
import usb
200+
199201
# Loop until we are shut down
200202
while not self.shutdown:
201203
try:

modules/theseven/icarus/icarusworker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727

2828

29-
import serial
3029
import time
3130
import struct
3231
import traceback
@@ -70,7 +69,7 @@ def apply_settings(self):
7069
if not "jobinterval" in self.settings or not self.settings.jobinterval: self.settings.jobinterval = 60
7170
# We can't change the port name or baud rate on the fly, so trigger a restart if they changed.
7271
# self.port/self.baudrate are cached copys of self.settings.port/self.settings.baudrate
73-
if self.started and self.settings.port != self.port or self.settings.baudrate != self.baudrate: self.async_restart()
72+
if self.started and (self.settings.port != self.port or self.settings.baudrate != self.baudrate): self.async_restart()
7473

7574

7675
# Reset our state. Called both from the constructor and from self.start().
@@ -172,6 +171,7 @@ def main(self):
172171
self.oldjob = None
173172

174173
# Open the serial port
174+
import serial
175175
self.handle = serial.Serial(self.port, self.baudrate, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE, 1, False, False, 5, False, None)
176176

177177
# We keep control of the wakeup lock at all times unless we're sleeping

modules/theseven/simplers232/simplers232worker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727

2828

29-
import serial
3029
import time
3130
import struct
3231
import traceback
@@ -70,7 +69,7 @@ def apply_settings(self):
7069
if not "jobinterval" in self.settings or not self.settings.jobinterval: self.settings.jobinterval = 60
7170
# We can't change the port name or baud rate on the fly, so trigger a restart if they changed.
7271
# self.port/self.baudrate are cached copys of self.settings.port/self.settings.baudrate
73-
if self.started and self.settings.port != self.port or self.settings.baudrate != self.baudrate: self.async_restart()
72+
if self.started and (self.settings.port != self.port or self.settings.baudrate != self.baudrate): self.async_restart()
7473

7574

7675
# Reset our state. Called both from the constructor and from self.start().
@@ -173,6 +172,7 @@ def main(self):
173172
self.nextjob = None
174173

175174
# Open the serial port
175+
import serial
176176
self.handle = serial.Serial(self.port, self.baudrate, serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE, 1, False, False, 5, False, None)
177177

178178
# Send enough zero bytes to make sure that the device is not expecting data any more.

modules/theseven/webui/api/debug.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,5 @@ def dumpthreadstates(core, webui, httprequest, path, request, privileges):
3535
code.append("\n# Thread: %s(%d)" % (id2name.get(threadId,""), threadId))
3636
for filename, lineno, name, line in traceback.extract_stack(stack):
3737
code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
38-
if line:
39-
code.append(" %s" % (line.strip()))
38+
if line: code.append(" %s" % (line.strip()))
4039
return {"data": "\n".join(code)}

0 commit comments

Comments
 (0)