Skip to content

Commit eb01b0b

Browse files
committedFeb 29, 2016
gateware.spi: cleanup doc
1 parent aeae565 commit eb01b0b

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed
 

Diff for: ‎artiq/gateware/spi.py

+32-24
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,6 @@ class SPIMaster(Module):
166166
Notes:
167167
* M = 32 is the data width (width of the data register,
168168
maximum write bits, maximum read bits)
169-
* If there is a miso wire in pads, the input and output can be done
170-
with two signals (a.k.a. 4-wire SPI), else mosi must be used for
171-
both output and input (a.k.a. 3-wire SPI) and config.half_duplex
172-
must to be set when reading data is desired.
173169
* Every transfer consists of a write_length 0-M bit write followed
174170
by a read_length 0-M bit read.
175171
* cs_n is asserted at the beginning and deasserted at the end of the
@@ -184,29 +180,37 @@ class SPIMaster(Module):
184180
"cs_n all deasserted" means "all cs_n bits high".
185181
* cs is not mandatory in pads. Framing and chip selection can also
186182
be handled independently through other means.
183+
* If there is a miso wire in pads, the input and output can be done
184+
with two signals (a.k.a. 4-wire SPI), else mosi must be used for
185+
both output and input (a.k.a. 3-wire SPI) and config.half_duplex
186+
must to be set when reading data is desired.
187+
* For 4-wire SPI only the sum of read_length and write_length matters.
188+
The behavior is the same no matter how the total transfer length is
189+
divided between the two. For 3-wire SPI, the direction of mosi/miso
190+
is switched from output to input after write_len cycles, at the
191+
"shift_out" clk edge corresponding to bit write_length + 1 of the
192+
transfer.
187193
* The first bit output on mosi is always the MSB/LSB (depending on
188194
config.lsb_first) of the data register, independent of
189195
xfer.write_len. The last bit input from miso always ends up in
190196
the LSB/MSB (respectively) of the data register, independent of
191197
read_len.
192-
* For 4-wire SPI only the sum of read_len and write_len matters. The
193-
behavior is the same no matter how the total transfer length is
194-
divided between the two. For 3-wire SPI, the direction of mosi/miso
195-
is switched from output to input after write_len cycles, at the
196-
"shift_out" clk edge corresponding to bit write_length + 1 of the
197-
transfer.
198198
* Data output on mosi in 4-wire SPI during the read cycles is what
199199
is found in the data register at the time.
200200
Data in the data register outside the least/most (depending
201201
on config.lsb_first) significant read_length bits is what is
202202
seen on miso during the write cycles.
203-
* The SPI data register is double-buffered: once a transfer completes,
204-
the previous transfer's read data is available in the data register
205-
and new write data can be written, queuing a new transfer. Transfers
206-
submitted this way are chained and executed without deasserting cs.
203+
* The SPI data register is double-buffered: Once a transfer has
204+
started, new write data can be written, queuing a new transfer.
205+
Transfers submitted this way are chained and executed without
206+
deasserting cs. Once a transfer completes, the previous transfer's
207+
read data is available in the data register.
207208
* A wishbone transaction is ack-ed when the transfer has been written
208209
to the intermediate buffer. It will be started when there are no
209-
other transactions being executed.
210+
other transactions being executed. Writes take one cycle when
211+
there is either no transfer being executed, no data in the
212+
intermediate buffer, or a transfer just completing. Reads always
213+
finish in one cycle.
210214
211215
Transaction Sequence:
212216
* If desired, write the config register to set up the core.
@@ -215,28 +219,30 @@ class SPIMaster(Module):
215219
writing triggers the transfer and when the transfer is accepted to
216220
the inermediate buffer, the write is ack-ed.
217221
* If desired, read the data register.
218-
* If desired write xfer and data for the next, chained, transfer
222+
* If desired, write data for the next, chained, transfer.
219223
220224
Register address and bit map:
221225
222226
config (address 2):
223227
1 offline: all pins high-z (reset=1)
224-
1 active: cs/transfer active
228+
1 active: cs/transfer active (read-only)
225229
1 pending: transfer pending in intermediate buffer, bus writes will
226-
block
230+
block (read-only)
227231
1 cs_polarity: active level of chip select (reset=0)
228-
1 clk_polarity: idle level for clk (reset=0)
232+
1 clk_polarity: idle level of clk (reset=0)
229233
1 clk_phase: first edge after cs assertion to sample data on (reset=0)
234+
(clk_polarity, clk_phase) == (CPOL, CPHA) in Freescale language.
230235
(0, 0): idle low, output on falling, input on rising
231236
(0, 1): idle low, output on rising, input on falling
232237
(1, 0): idle high, output on rising, input on falling
233238
(1, 1): idle high, output on falling, input on rising
239+
There is never a clk edge during a cs edge.
234240
1 lsb_first: LSB is the first bit on the wire (reset=0)
235241
1 half_duplex: 3-wire SPI, in/out on mosi (reset=0)
236242
8 undefined
237243
8 div_write: counter load value to divide this module's clock
238-
to the SPI write clk.
239-
f_clk/f_spi_write == 2*(div_write + 1) (reset=0)
244+
to generate the SPI write clk (reset=0)
245+
f_clk/f_spi_write == div_write + 2
240246
8 div_read: ditto for the read clock
241247
242248
xfer (address 1):
@@ -338,12 +344,14 @@ def __init__(self, pads, bus=None, data_width=32):
338344

339345
clk_t = TSTriple()
340346
self.specials += clk_t.get_tristate(pads.clk)
341-
mosi_t = TSTriple()
342-
self.specials += mosi_t.get_tristate(pads.mosi)
343-
344347
self.comb += [
345348
clk_t.oe.eq(~config.offline),
346349
clk_t.o.eq((spi.cg.clk & spi.cs) ^ config.clk_polarity),
350+
]
351+
352+
mosi_t = TSTriple()
353+
self.specials += mosi_t.get_tristate(pads.mosi)
354+
self.comb += [
347355
mosi_t.oe.eq(~config.offline & spi.cs &
348356
(spi.oe | ~config.half_duplex)),
349357
mosi_t.o.eq(spi.reg.o),

0 commit comments

Comments
 (0)
Please sign in to comment.