Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 1 file changed
  • 0 comments
  • 2 contributors

Showing 1 changed file with 60 additions and 0 deletions. Show diff stats Hide diff stats

  1. 60  milkymist/adc/__init__.py
60  milkymist/adc/__init__.py
... ...
@@ -0,0 +1,60 @@
  1
+from migen.fhdl.structure import *
  2
+from migen.fhdl.module import Module
  3
+from migen.bank.description import *
  4
+from migen.genlib.misc import optree
  5
+from migen.genlib.cdc import MultiReg
  6
+
  7
+class CounterADC(Module, AutoCSR):
  8
+	def __init__(self, charge, sense, width=24):
  9
+		if not isinstance(sense, collections.Iterable):
  10
+			sense = [sense]
  11
+
  12
+		channels = len(sense)
  13
+
  14
+		self._start_busy = CSR()
  15
+		self._overflow = CSRStatus(channels)
  16
+		self._polarity = CSRStorage()
  17
+
  18
+		count = Signal(width)
  19
+		busy = Signal(channels)
  20
+
  21
+		res = []
  22
+		for i in range(channels):
  23
+			res.append(CSRStatus(width, name="res"+str(i)))
  24
+			setattr(self, "_res"+str(i), res[-1])
  25
+
  26
+		any_busy = Signal()
  27
+		self.comb += [
  28
+			any_busy.eq(optree("|",
  29
+			    [busy[i] for i in range(channels)])),
  30
+			self._start_busy.w.eq(any_busy)
  31
+		]
  32
+
  33
+		carry = Signal()
  34
+
  35
+		self.sync += [
  36
+			If(self._start_busy.re,
  37
+				count.eq(0),
  38
+				busy.eq((1 << channels)-1),
  39
+				self._overflow.status.eq(0),
  40
+				charge.eq(~self._polarity.storage)
  41
+			).Elif(any_busy,
  42
+				Cat(count, carry).eq(count + 1),
  43
+				If(carry,
  44
+					self._overflow.status.eq(busy),
  45
+					busy.eq(0)
  46
+				)
  47
+			).Else(
  48
+				charge.eq(self._polarity.storage)
  49
+			)
  50
+		]
  51
+
  52
+		for i in range(channels):
  53
+			sense_synced = Signal()
  54
+			self.specials += MultiReg(sense[i], sense_synced)
  55
+			self.sync += If(busy[i],
  56
+				If(sense_synced != self._polarity.storage,
  57
+					res[i].status.eq(count),
  58
+					busy[i].eq(0)
  59
+				)
  60
+			)

No commit comments for this range

Something went wrong with that request. Please try again.