litex/miscope/miio.py

44 lines
1.1 KiB
Python
Raw Normal View History

2012-09-12 16:19:42 -04:00
from migen.fhdl.structure import *
from migen.bus import csr
from migen.bank import description, csrgen
from migen.bank.description import *
2013-02-22 08:28:05 -05:00
class MiIo:
2012-09-17 11:00:47 -04:00
#
# Definition
#
2013-03-18 16:45:07 -04:00
def __init__(self, address, width, mode="IO", interface=None):
2012-09-13 07:14:27 -04:00
self.address = address
2012-09-12 16:19:42 -04:00
self.width = width
2013-03-18 16:45:07 -04:00
self.mode = mode.upper()
2012-09-14 06:57:09 -04:00
self.interface = interface
2013-03-18 16:45:07 -04:00
self.words = int((2**bits_for(width-1))/8)
2013-02-22 08:28:05 -05:00
2012-09-12 16:19:42 -04:00
if "I" in self.mode:
2013-01-03 16:57:26 -05:00
self.i = Signal(self.width)
2012-09-12 16:19:42 -04:00
self.ireg = description.RegisterField("i", self.width, READ_ONLY, WRITE_ONLY)
2013-02-22 08:28:05 -05:00
2012-09-12 16:19:42 -04:00
if "O" in self.mode:
2013-01-03 16:57:26 -05:00
self.o = Signal(self.width)
2012-09-12 16:19:42 -04:00
self.oreg = description.RegisterField("o", self.width)
2013-02-22 08:28:05 -05:00
2012-09-13 07:14:27 -04:00
self.bank = csrgen.Bank([self.oreg, self.ireg], address=self.address)
2012-09-17 11:00:47 -04:00
2012-09-12 16:19:42 -04:00
def get_fragment(self):
comb = []
2013-02-22 08:28:05 -05:00
2012-09-13 07:14:27 -04:00
if "I" in self.mode:
comb += [self.ireg.field.w.eq(self.i)]
2013-02-22 08:28:05 -05:00
2012-09-13 07:14:27 -04:00
if "O" in self.mode:
comb += [self.o.eq(self.oreg.field.r)]
2013-02-22 08:28:05 -05:00
return Fragment(comb) + self.bank.get_fragment()
2012-09-17 11:00:47 -04:00
#
2013-03-22 07:35:12 -04:00
# Driver
2012-09-17 11:00:47 -04:00
#
2013-03-22 07:35:12 -04:00
def set(self, data):
2013-03-21 07:23:44 -04:00
self.interface.write(self.bank.get_base(), data)
2012-09-17 11:00:47 -04:00
2013-03-22 07:35:12 -04:00
def get(self):
2013-03-21 07:23:44 -04:00
return self.interface.read(self.bank.get_base() + self.words)