-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpicoprog.py
executable file
·65 lines (49 loc) · 1.47 KB
/
picoprog.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
#!/usr/bin/env python3
# picoprog.py
#
# Copyright (C) 2021 Dan Rodrigues <[email protected]>
#
# SPDX-License-Identifier: MIT
import sys
import usb.core
import usb.util
# Expecting 1 argument with bitstream to load
if len(sys.argv) != 2:
print("Usage: picoprog.py <bistream>")
sys.exit(0)
# Find Pico programmer device
dev = usb.core.find(idVendor=0x2E8A, idProduct=0x0004)
if dev is None:
raise RuntimeError("Device not found")
cfg = dev.get_active_configuration()
intf = cfg[(2, 0)]
outep = usb.util.find_descriptor(
intf,
# First OUT endpoint
custom_match= \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
inep = usb.util.find_descriptor(
intf,
# First IN endpoint
custom_match= \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_IN)
assert inep is not None
assert outep is not None
# Load bitstream to send
bitstream_path = sys.argv[1]
bitstream_file = open(bitstream_path, 'rb')
if bitstream_file is None:
raise ValueError("Failed to open bitstream file: {:s}".format(bitstream_path))
bitstream = bitstream_file.read()
bitstream_file.close()
# Send bitstream over USB
ctrl_request_type = 0x40
ctrl_request_prepare = 0x01
ctrl_request_finalize = 0x02
dev.ctrl_transfer(ctrl_request_type, ctrl_request_prepare, 0, 0)
outep.write(bitstream)
dev.ctrl_transfer(ctrl_request_type, ctrl_request_finalize, 0, 0)