-
Notifications
You must be signed in to change notification settings - Fork 210
/
programmer.py
65 lines (45 loc) · 2 KB
/
programmer.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
import os
import subprocess
from migen.build.generic_programmer import GenericProgrammer
from migen.build import tools
class LatticeProgrammer(GenericProgrammer):
needs_bitreverse = False
def __init__(self, xcf_template):
self.xcf_template = xcf_template
def load_bitstream(self, bitstream_file):
xcf_file = bitstream_file.replace(".bit", ".xcf")
xcf_content = self.xcf_template.format(bitstream_file=bitstream_file)
tools.write_to_file(xcf_file, xcf_content)
subprocess.call(["pgrcmd", "-infile", xcf_file])
class IceStormProgrammer(GenericProgrammer):
needs_bitreverse = False
def flash(self, address, bitstream_file):
subprocess.call(["iceprog", "-o", str(address), bitstream_file])
def load_bitstream(self, bitstream_file):
subprocess.call(["iceprog", "-S", bitstream_file])
class IceBurnProgrammer(GenericProgrammer):
def __init__(self, iceburn_path):
GenericProgrammer.__init__(self)
self.iceburn = iceburn_path
needs_bitreverse = False
def load_bitstream(self, bitstream_file):
subprocess.call([self.iceburn, "-evw", bitstream_file])
class TinyFpgaBProgrammer(GenericProgrammer):
needs_bitreverse = False
# The default flash address you probably want is 0x30000; the image at
# address 0 is for the bootloader.
def flash(self, address, bitstream_file):
subprocess.call(["tinyfpgab", "-a", str(address), "-p",
bitstream_file])
# Force user image to boot if a user reset tinyfpga, the bootloader
# is active, and the user image need not be reprogrammed.
def boot(self):
subprocess.call(["tinyfpgab", "-b"])
class MyStormProgrammer(GenericProgrammer):
def __init__(self, serial_port):
self.serial_port = serial_port
def load_bitstream(self, bitstream_file):
import serial
with serial.Serial(self.serial_port) as port:
with open(bitstream_file, "rb") as f:
port.write(f.read())