-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
fpga_ctrl.py
109 lines (90 loc) · 3.06 KB
/
fpga_ctrl.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
'''
Main FPGA control script
'''
from time import sleep
from csr_map import CsrMap
from ftdi_dev import FtdiDevice
class FpgaControl(object):
"""Collection of FPGA control functions via FTDI API"""
MAX_LINES = 32
WORDS_PER_LINE = 16384
def __init__(self, ftdi_url, spi_freq=1E6):
"""Initialize FPGA controller.
Keyword arguments:
ftdi_url -- FTDI device url, which can be obtained by Ftdi.show_devices()
freq -- SPI frequency up to 8E6 (for FPGA running on 64 MHz)
"""
self._ftdi = FtdiDevice(ftdi_url, spi_freq)
self.csr = CsrMap(self._ftdi)
def reset(self):
"""Reset FPGA logic"""
self._ftdi.reset_logic_on()
sleep(0.01)
self._ftdi.reset_logic_off()
def reload(self):
"""Reload FPGA configuration from flash"""
self._ftdi.reset_config_on()
sleep(0.01)
self._ftdi.reset_config_off()
sleep(0.5)
def read_lines(self, n):
"""Read 'n' number of lines from SRAM buffer
Maxinum 'n' -- 32
"""
# reset external ram address to read from the memory beginning
self.csr.ramraddrrst = 1
res = []
for _ in range(n):
# read lines (16384 words per line) one by one
line = self.csr.ramdata
res += [line]
return res
def do_acquisition(self, acq_lines=1, gain=None, double_rate=False):
"""Do acquisitions.
Keyword arguments:
acq_lines -- number of lines to sample: int 1 .. 32
gain -- list with gain values: None or list with length of 32
double_rate -- enable/disable interleaving mode: bool
"""
if gain:
self.csr.dacgain = gain
self.csr.nblines = acq_lines - 1
self.csr.drmode = int(double_rate)
self.csr.acqstart = 1
while (not self.csr.acqdone):
sleep(0.01)
return self.read_lines(acq_lines)
def disconnect(self):
"""Disconnect from FTDI and close all open ports"""
self._ftdi.close_connection()
if __name__ == "__main__":
# init FTDI device
fpga = FpgaControl('ftdi://ftdi:2232:/', spi_freq=8E6)
# reload configuration (optional step - just to fill BRAM (DACGAIN registers) with initial values)
fpga.reload()
# reset fpga
fpga.reset()
# read initial state of INITDEL register
print("initdel = 0x%x" % fpga.csr.initdel)
# write new value to the INITDEL
fpga.csr.initdel = 0x20
# read current state of INITDEL register
print("initdel = 0x%x" % fpga.csr.initdel)
# read DACGAIN array initial state
print("dacgain = ", fpga.csr.dacgain)
# write new values to the DACGAIN
fpga.csr.dacgain = [200 + i for i in range(32)]
# read DACGAIN array current state
print("dacgain = ", fpga.csr.dacgain)
# some LED3 blinking
fpga.csr.led3 = 1
sleep(1)
fpga.csr.led3 = 0
sleep(0.3)
fpga.csr.led3 = 1
sleep(0.3)
fpga.csr.led3 = 0
# reset fpga again (optional)
fpga.reset()
# close FTDI interface
fpga.disconnect()