@@ -166,10 +166,6 @@ class SPIMaster(Module):
166
166
Notes:
167
167
* M = 32 is the data width (width of the data register,
168
168
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.
173
169
* Every transfer consists of a write_length 0-M bit write followed
174
170
by a read_length 0-M bit read.
175
171
* cs_n is asserted at the beginning and deasserted at the end of the
@@ -184,29 +180,37 @@ class SPIMaster(Module):
184
180
"cs_n all deasserted" means "all cs_n bits high".
185
181
* cs is not mandatory in pads. Framing and chip selection can also
186
182
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.
187
193
* The first bit output on mosi is always the MSB/LSB (depending on
188
194
config.lsb_first) of the data register, independent of
189
195
xfer.write_len. The last bit input from miso always ends up in
190
196
the LSB/MSB (respectively) of the data register, independent of
191
197
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.
198
198
* Data output on mosi in 4-wire SPI during the read cycles is what
199
199
is found in the data register at the time.
200
200
Data in the data register outside the least/most (depending
201
201
on config.lsb_first) significant read_length bits is what is
202
202
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.
207
208
* A wishbone transaction is ack-ed when the transfer has been written
208
209
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.
210
214
211
215
Transaction Sequence:
212
216
* If desired, write the config register to set up the core.
@@ -215,28 +219,30 @@ class SPIMaster(Module):
215
219
writing triggers the transfer and when the transfer is accepted to
216
220
the inermediate buffer, the write is ack-ed.
217
221
* 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.
219
223
220
224
Register address and bit map:
221
225
222
226
config (address 2):
223
227
1 offline: all pins high-z (reset=1)
224
- 1 active: cs/transfer active
228
+ 1 active: cs/transfer active (read-only)
225
229
1 pending: transfer pending in intermediate buffer, bus writes will
226
- block
230
+ block (read-only)
227
231
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)
229
233
1 clk_phase: first edge after cs assertion to sample data on (reset=0)
234
+ (clk_polarity, clk_phase) == (CPOL, CPHA) in Freescale language.
230
235
(0, 0): idle low, output on falling, input on rising
231
236
(0, 1): idle low, output on rising, input on falling
232
237
(1, 0): idle high, output on rising, input on falling
233
238
(1, 1): idle high, output on falling, input on rising
239
+ There is never a clk edge during a cs edge.
234
240
1 lsb_first: LSB is the first bit on the wire (reset=0)
235
241
1 half_duplex: 3-wire SPI, in/out on mosi (reset=0)
236
242
8 undefined
237
243
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
240
246
8 div_read: ditto for the read clock
241
247
242
248
xfer (address 1):
@@ -338,12 +344,14 @@ def __init__(self, pads, bus=None, data_width=32):
338
344
339
345
clk_t = TSTriple ()
340
346
self .specials += clk_t .get_tristate (pads .clk )
341
- mosi_t = TSTriple ()
342
- self .specials += mosi_t .get_tristate (pads .mosi )
343
-
344
347
self .comb += [
345
348
clk_t .oe .eq (~ config .offline ),
346
349
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 += [
347
355
mosi_t .oe .eq (~ config .offline & spi .cs &
348
356
(spi .oe | ~ config .half_duplex )),
349
357
mosi_t .o .eq (spi .reg .o ),
0 commit comments