2015-02-18 09:32:34 -05:00
|
|
|
import serial
|
|
|
|
from struct import *
|
2015-05-01 14:27:31 -04:00
|
|
|
from misoclib.tools.litescope.software.driver.reg import *
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:18:21 -04:00
|
|
|
|
2015-02-18 09:32:34 -05:00
|
|
|
def write_b(uart, data):
|
2015-04-13 07:23:48 -04:00
|
|
|
uart.write(pack('B', data))
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:18:21 -04:00
|
|
|
|
2015-05-02 04:24:56 -04:00
|
|
|
class UARTWishboneBridgeDriver:
|
2015-04-13 07:09:44 -04:00
|
|
|
cmds = {
|
2015-04-13 07:25:27 -04:00
|
|
|
"write": 0x01,
|
2015-04-18 09:37:38 -04:00
|
|
|
"read": 0x02
|
2015-04-13 07:09:44 -04:00
|
|
|
}
|
|
|
|
def __init__(self, port, baudrate=115200, addrmap=None, busword=8, debug=False):
|
|
|
|
self.port = port
|
|
|
|
self.baudrate = str(baudrate)
|
|
|
|
self.debug = debug
|
|
|
|
self.uart = serial.Serial(port, baudrate, timeout=0.25)
|
|
|
|
if addrmap is not None:
|
|
|
|
self.regs = build_map(addrmap, busword, self.read, self.write)
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
def open(self):
|
|
|
|
self.uart.flushOutput()
|
|
|
|
self.uart.close()
|
|
|
|
self.uart.open()
|
|
|
|
self.uart.flushInput()
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
def close(self):
|
|
|
|
self.uart.flushOutput()
|
|
|
|
self.uart.close()
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-18 09:37:38 -04:00
|
|
|
def read(self, addr, burst_length=1):
|
2015-04-13 07:09:44 -04:00
|
|
|
datas = []
|
2015-04-18 09:37:38 -04:00
|
|
|
self.uart.flushInput()
|
|
|
|
write_b(self.uart, self.cmds["read"])
|
|
|
|
write_b(self.uart, burst_length)
|
|
|
|
word_addr = addr//4
|
|
|
|
write_b(self.uart, (word_addr >> 24) & 0xff)
|
|
|
|
write_b(self.uart, (word_addr >> 16) & 0xff)
|
|
|
|
write_b(self.uart, (word_addr >> 8) & 0xff)
|
|
|
|
write_b(self.uart, (word_addr >> 0) & 0xff)
|
|
|
|
for i in range(burst_length):
|
|
|
|
data = 0
|
|
|
|
for k in range(4):
|
|
|
|
data = data << 8
|
|
|
|
data |= ord(self.uart.read())
|
|
|
|
if self.debug:
|
|
|
|
print("RD {:08X} @ {:08X}".format(data, addr + 4*i))
|
|
|
|
datas.append(data)
|
|
|
|
if burst_length == 1:
|
|
|
|
return datas[0]
|
|
|
|
else:
|
|
|
|
return datas
|
2015-02-18 09:32:34 -05:00
|
|
|
|
2015-04-13 07:09:44 -04:00
|
|
|
def write(self, addr, data):
|
|
|
|
if isinstance(data, list):
|
|
|
|
burst_length = len(data)
|
|
|
|
else:
|
|
|
|
burst_length = 1
|
2015-04-18 09:37:38 -04:00
|
|
|
data = [data]
|
2015-04-13 07:09:44 -04:00
|
|
|
write_b(self.uart, self.cmds["write"])
|
|
|
|
write_b(self.uart, burst_length)
|
2015-04-18 09:37:38 -04:00
|
|
|
word_addr = addr//4
|
|
|
|
write_b(self.uart, (word_addr >> 24) & 0xff)
|
|
|
|
write_b(self.uart, (word_addr >> 16) & 0xff)
|
|
|
|
write_b(self.uart, (word_addr >> 8) & 0xff)
|
|
|
|
write_b(self.uart, (word_addr >> 0) & 0xff)
|
|
|
|
for i in range(len(data)):
|
|
|
|
dat = data[i]
|
2015-04-13 07:09:44 -04:00
|
|
|
for j in range(4):
|
2015-04-18 09:37:38 -04:00
|
|
|
write_b(self.uart, (dat >> 24) & 0xff)
|
2015-04-13 07:09:44 -04:00
|
|
|
dat = dat << 8
|
|
|
|
if self.debug:
|
2015-04-18 09:37:38 -04:00
|
|
|
print("WR {:08X} @ {:08X}".format(data[i], addr + 4*i))
|