2013-05-22 11:10:13 -04:00
|
|
|
import collections
|
|
|
|
|
|
|
|
from migen.fhdl.std import *
|
2013-04-18 12:33:25 -04:00
|
|
|
from migen.bank.description import *
|
|
|
|
from migen.genlib.misc import optree
|
2013-04-19 06:32:12 -04:00
|
|
|
from migen.genlib.cdc import MultiReg
|
2013-04-18 12:33:25 -04:00
|
|
|
|
|
|
|
class CounterADC(Module, AutoCSR):
|
2013-04-19 06:32:12 -04:00
|
|
|
def __init__(self, charge, sense, width=24):
|
2013-04-18 12:33:25 -04:00
|
|
|
if not isinstance(sense, collections.Iterable):
|
|
|
|
sense = [sense]
|
|
|
|
|
|
|
|
channels = len(sense)
|
|
|
|
|
|
|
|
self._start_busy = CSR()
|
|
|
|
self._overflow = CSRStatus(channels)
|
|
|
|
self._polarity = CSRStorage()
|
|
|
|
|
|
|
|
count = Signal(width)
|
|
|
|
busy = Signal(channels)
|
|
|
|
|
|
|
|
res = []
|
|
|
|
for i in range(channels):
|
|
|
|
res.append(CSRStatus(width, name="res"+str(i)))
|
|
|
|
setattr(self, "_res"+str(i), res[-1])
|
|
|
|
|
|
|
|
any_busy = Signal()
|
|
|
|
self.comb += [
|
|
|
|
any_busy.eq(optree("|",
|
|
|
|
[busy[i] for i in range(channels)])),
|
|
|
|
self._start_busy.w.eq(any_busy)
|
|
|
|
]
|
|
|
|
|
|
|
|
carry = Signal()
|
|
|
|
|
|
|
|
self.sync += [
|
|
|
|
If(self._start_busy.re,
|
|
|
|
count.eq(0),
|
|
|
|
busy.eq((1 << channels)-1),
|
|
|
|
self._overflow.status.eq(0),
|
2013-04-19 06:32:12 -04:00
|
|
|
charge.eq(~self._polarity.storage)
|
2013-04-18 12:33:25 -04:00
|
|
|
).Elif(any_busy,
|
|
|
|
Cat(count, carry).eq(count + 1),
|
|
|
|
If(carry,
|
|
|
|
self._overflow.status.eq(busy),
|
|
|
|
busy.eq(0)
|
|
|
|
)
|
|
|
|
).Else(
|
|
|
|
charge.eq(self._polarity.storage)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
for i in range(channels):
|
2013-04-19 06:32:12 -04:00
|
|
|
sense_synced = Signal()
|
|
|
|
self.specials += MultiReg(sense[i], sense_synced)
|
2013-04-18 12:33:25 -04:00
|
|
|
self.sync += If(busy[i],
|
2013-04-19 06:32:12 -04:00
|
|
|
If(sense_synced != self._polarity.storage,
|
2013-04-18 12:33:25 -04:00
|
|
|
res[i].status.eq(count),
|
|
|
|
busy[i].eq(0)
|
|
|
|
)
|
|
|
|
)
|