Skip to content

Commit

Permalink
spi: add divisor_bits; fix simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryMakes committed Feb 6, 2020
1 parent 259225c commit b9ab781
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions nmigen_stdio/spiflash.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def _format_cmd(self):
fcmd &= ~(1 << (bit*self.spi_width))
return fcmd

def __init__(self, *, protocol, data_width,
divisor=1, pins=None):
def __init__(self, *, protocol, data_width=32, divisor=1, divisor_bits=None, pins=None):
if protocol not in ["standard", "dual", "quad"]:
raise ValueError("Invalid SPI protocol {!r}; must be one of "
"\"standard\", \"dual\", or \"quad\""
Expand All @@ -53,7 +52,7 @@ def __init__(self, *, protocol, data_width,
if divisor < 1:
raise ValueError("Invalid divisor value; must be at least 1"
.format(divisor))
self.divisor = Signal(bits_for(divisor), reset=divisor)
self.divisor = Signal(divisor_bits or bits_for(divisor), reset=divisor)
self._divisor_val = divisor + 1

self._pins = pins
Expand Down Expand Up @@ -108,12 +107,12 @@ def _add_spi_hardware_logic(self, platform, module):
module.submodules += FFSynchronizer(self._pins.miso.i, self.miso)
module.d.comb += self._pins.mosi.o.eq(self.mosi)
elif self._protocol in ["dual", "quad"]:
dq_oe = Signal()
self.dq_oe = Signal()
module.submodules.dq = platform.get_tristate(self.dq, self._pins.dq, None, False)
# If the user doesn't give pins, create dq Pins for Dual & Quad
else:
if self._protocol in ["dual", "quad"]:
dq_oe = Signal()
self.dq_oe = Signal()
if self._protocol == "dual":
dq_pins = Record([
("dq", Pin(width=2, dir="io", xdr=0).layout)
Expand All @@ -126,7 +125,7 @@ def _add_spi_hardware_logic(self, platform, module):
tristate_submodule.submodules += Instance("$tribuf",
p_WIDTH=self.dq.width,
i_EN=self.dq.oe,
i_A=Platformodule._invert_if(False, self.dq.o),
i_A=self.dq.o,
o_Y=dq_pins
)
module.submodules.dq = tristate_submodule
Expand Down Expand Up @@ -168,7 +167,7 @@ def _add_spi_hardware_logic(self, platform, module):
elif self._protocol in ["dual", "quad"]:
module.d.comb += [
self.dq.o.eq(shreg[-self.spi_width:]),
self.dq.oe.eq(dq_oe)
self.dq.oe.eq(self.dq_oe)
]


Expand Down Expand Up @@ -228,7 +227,7 @@ def elaborate(self, platform):
shreg[-self.cmd_width:].eq(fcmd)
]
if self._protocol in ["dual", "quad"]:
m.d.sync += dq_oe.eq(1)
m.d.sync += self.dq_oe.eq(1)
m.next = "SLOWREAD-CMD"
# State: Command, MOSI
with m.State("SLOWREAD-CMD"):
Expand All @@ -245,7 +244,7 @@ def elaborate(self, platform):
with m.If(state_counter == state_durations["SLOWREAD-ADDR"] - 1):
m.d.sync += state_counter.eq(0)
if self._protocol in ["dual", "quad"]:
m.d.sync += dq_oe.eq(0)
m.d.sync += self.dq_oe.eq(0)
m.next = "SLOWREAD-READ"
with m.Else():
m.d.sync += state_counter.eq(state_counter + 1)
Expand All @@ -257,7 +256,7 @@ def elaborate(self, platform):
self.r_rdy.eq(1)
]
if self._protocol in ["dual", "quad"]:
m.d.sync += dq_oe.eq(0)
m.d.sync += self.dq_oe.eq(0)
m.next = "SLOWREAD-RDYWAIT"
with m.Else():
m.d.sync += state_counter.eq(state_counter + 1)
Expand Down Expand Up @@ -339,7 +338,7 @@ def elaborate(self, platform):
shreg[-self.cmd_width:].eq(fcmd)
]
if self._protocol in ["dual", "quad"]:
m.d.sync += dq_oe.eq(1)
m.d.sync += self.dq_oe.eq(1)
m.next = "FASTREAD-CMD"
# State: Command, MOSI
with m.State("FASTREAD-CMD"):
Expand All @@ -356,7 +355,7 @@ def elaborate(self, platform):
with m.If(state_counter == state_durations["FASTREAD-ADDR"] - 1):
m.d.sync += state_counter.eq(0)
if self._protocol in ["dual", "quad"]:
m.d.sync += dq_oe.eq(0)
m.d.sync += self.dq_oe.eq(0)
m.next = "FASTREAD-WAITREAD"
with m.Else():
m.d.sync += state_counter.eq(state_counter + 1)
Expand All @@ -368,7 +367,7 @@ def elaborate(self, platform):
self.r_rdy.eq(1)
]
if self._protocol in ["dual", "quad"]:
m.d.sync += dq_oe.eq(0)
m.d.sync += self.dq_oe.eq(0)
m.next = "FASTREAD-RDYWAIT"
with m.Else():
m.d.sync += state_counter.eq(state_counter + 1)
Expand Down

0 comments on commit b9ab781

Please sign in to comment.