Skip to content

Commit 5d0267e

Browse files
committed
Work around circuitpython file copying issue
1 parent f9024a1 commit 5d0267e

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

factory/factory_setup.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from libwinter import utils
1212

1313
DEVICE_NAME = "winterbloom_big_honking_button"
14+
USB_DEVICE_ID = "239A:6005"
1415
JLINK_DEVICE = "ATSAMD21G18"
1516
JLINK_SCRIPT = "scripts/flash.jlink"
1617

@@ -54,6 +55,9 @@ def deploy_circuitpython_code(destination=None):
5455
print("Waiting for CIRCUITPY drive...")
5556
destination = utils.wait_for_drive("CIRCUITPY")
5657

58+
print("Forcing BHB into repl (workaround for CircuitPython issue #3986)")
59+
utils.force_into_repl(USB_DEVICE_ID)
60+
5761
print("Cleaning temporary files from src directories...")
5862
utils.clean_pycache(FIRMWARE_DIR)
5963
utils.clean_pycache(EXAMPLES_DIR)
@@ -62,6 +66,10 @@ def deploy_circuitpython_code(destination=None):
6266
print("Copying files...")
6367
utils.deploy_files(FILES_TO_DEPLOY, destination)
6468

69+
print("Done copying files, resetting...")
70+
utils.reset_via_serial(USB_DEVICE_ID)
71+
print("Done!")
72+
6573

6674
def main():
6775
if len(sys.argv) > 1 and sys.argv[1] == "publish":
@@ -86,4 +94,4 @@ def main():
8694

8795

8896
if __name__ == "__main__":
89-
main()
97+
main()

factory/libwinter/utils.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def wait_for_drive(name, timeout=10):
8383

8484

8585
def flush(path):
86+
print("Flushing... 🚽")
8687
if WINDOWS:
8788
drive, _ = os.path.splitdrive(path)
8889
subprocess.run(
@@ -278,3 +279,38 @@ def removeprefix(self: str, prefix: str) -> str:
278279
return self[len(prefix) :]
279280
else:
280281
return self[:]
282+
283+
284+
def serial_connect(usb_device_id: str):
285+
import serial.tools.list_ports
286+
import serial
287+
288+
port_info = list(serial.tools.list_ports.grep(usb_device_id))[0]
289+
port = serial.Serial(port_info.device, baudrate=115200, timeout=1)
290+
291+
return port
292+
293+
294+
def force_into_repl(usb_device_id: str):
295+
"""Forces a circuitpython device into the REPL."""
296+
port = serial_connect(usb_device_id)
297+
298+
# Interrupt with Ctrl + C
299+
port.write(b"\x03")
300+
# Enter repl with enter after slight delay
301+
time.sleep(0.2)
302+
port.write(b"\n")
303+
port.close()
304+
305+
306+
def reset_via_serial(usb_device_id: str):
307+
port = serial_connect(usb_device_id)
308+
309+
# Interrupt with Ctrl + C - not need if already in repl, but doesn't hurt.
310+
port.write(b"\x03")
311+
# Enter repl with enter after slight delay
312+
time.sleep(0.2)
313+
port.write(b"\n")
314+
# Reset using Ctrl + D
315+
port.write(b"\04")
316+
port.close()

firmware/winterbloom_bhb/bhb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
try:
3232
import _bhb
3333
except ImportError:
34-
raise EnvironmentError("This BHB library requires CircuitPython >= 6.0.0")
34+
raise RuntimeError("This BHB library requires CircuitPython >= 6.0.0")
3535

3636

3737
def _detect_board_revision():

0 commit comments

Comments
 (0)