OpenCores
URL https://opencores.org/ocsvn/sdspi/sdspi/trunk

Subversion Repositories sdspi

[/] [sdspi/] [trunk/] [doc/] [src/] [spec.tex] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dgisselq
\documentclass{gqtekspec}
2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3
%%
4
%% Filename:    spec.tex
5
%%
6
%% Project:     SD-Card controller, using a shared SPI interface
7
%%
8
%% Purpose:     This LaTeX file contains all of the documentation/description
9
%%              currently provided with the SDSPI controller core.  For those
10
%%      looking into here, this document is not nearly as interesting as the
11
%%      spec.pdf file it creates, so I'd strongly recommend reading that before
12
%%      diving into this document.  However, if you are interested in producing
13
%%      documents looking like this one, or using LaTeX in a similar fashion,
14
%%      you may find this document of use.
15
%%
16
%%      You should be able to find the PDF this file produces in the SVN
17
%%      distribution together with this LaTeX file and a copy of the GPL-3.0
18
%%      license this file is distributed under.  If not, just type 'make' in
19
%%      the doc directory (one up from this one), and it (should) build both
20
%%      pdf's without a problem.
21
%%
22
%% Creator:     Dan Gisselquist, Ph.D.
23
%%              Gisselquist Technology, LLC
24
%%
25
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
26
%%
27
%% Copyright (C) 2016, Gisselquist Technology, LLC
28
%%
29
%% This program is free software (firmware): you can redistribute it and/or
30
%% modify it under the terms of  the GNU General Public License as published
31
%% by the Free Software Foundation, either version 3 of the License, or (at
32
%% your option) any later version.
33
%%
34
%% This program is distributed in the hope that it will be useful, but WITHOUT
35
%% ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
36
%% FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
37
%% for more details.
38
%%
39
%% You should have received a copy of the GNU General Public License along
40
%% with this program.  (It's in the $(ROOT)/doc directory, run make with no
41
%% target there if the PDF file isn't present.)  If not, see
42
%% <http://www.gnu.org/licenses/> for a copy.
43
%%
44
%% License:     GPL, v3, as defined and found on www.gnu.org,
45
%%              http://www.gnu.org/licenses/gpl.html
46
%%
47
%%
48
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49
%%
50
%%
51
\usepackage{import}
52
\usepackage{bytefield}
53
\project{SDSPI Controller}
54
\title{Specification}
55
\author{Dan Gisselquist, Ph.D.}
56
\email{dgisselq (at) opencores.org}
57
\revision{Rev.~0.1}
58
\begin{document}
59
\pagestyle{gqtekspecplain}
60
\titlepage
61
\begin{license}
62
Copyright (C) \theyear\today, Gisselquist Technology, LLC
63
 
64
This project is free software (firmware): you can redistribute it and/or
65
modify it under the terms of  the GNU General Public License as published
66
by the Free Software Foundation, either version 3 of the License, or (at
67
your option) any later version.
68
 
69
This program is distributed in the hope that it will be useful, but WITHOUT
70
ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
71
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
72
for more details.
73
 
74
You should have received a copy of the GNU General Public License along
75
with this program.  If not, see \texttt{http://www.gnu.org/licenses/} for a copy.
76
\end{license}
77
\begin{revisionhistory}
78
0.1 & 6/18/2016 & Gisselquist & First Draft \\\hline
79
\end{revisionhistory}
80
% Revision History
81
% Table of Contents, named Contents
82
\tableofcontents
83
\listoffigures
84
\listoftables
85
\begin{preface}
86
When I started this project, I was informed that other projects similar to this
87
one existed.  The OpenRISC project has used an SD--Card controller, for example,
88
as has the Google project vault.  Of these two, the first uses the full SD--Card
89
interface which is unavailable on the XuLA2 board, and I could never find the
90
code for the second.
91
 
92
Still, had I found such interfaces, I would've still had another reason for
93
building my own: controlling the license.  By rolling my own interface, I can
94
offer it to anyone interested in it under the GPL license, such as you have
95
here.  Further, by not using code belonging to others, I am not restricted or
96
encumbered by any of their licenses--whether it be the GPL or otherwise.  This
97
code, and specification document, are therefore completely the product of
98
Gisselquist Technology, LLC.
99
 
100
That said, I am indebted to Mr.~Tambe for sharing his interface.  While the
101
current work does not use his approach and represents a complete rewrite, his
102
approach was valuable in helping me understand what the SD--Card specification
103
meant at several points along the way.
104
\end{preface}
105
 
106
\chapter{Introduction}
107
\pagenumbering{arabic}
108
\setcounter{page}{1}
109
 
110
% What is old
111
This Verilog core exports an SD card controller interface from internal to an
112
FPGA to the rest of the FPGA core, while taking care of the lower level details
113
internal to the interface.
114
% What does the old lack?
115
Unlike the other OpenCores SD Card controller\footnote{See
116
\texttt{http://www.opencores.org/project,sdcard\_mass\_storage\_controller}.}
117
which offers a full SD--interface, this controller focuses on the SPI interface
118
of the SD Card.  While this is a slower interface, the SPI interface is
119
necessary to access the card when using a XuLA2
120
board\footnote{See \texttt{http://www.xess.com/shop/product/xula2-lx25/}}, or
121
in general any time the full 9--bit, bi--directional interface to the SD card
122
has not been implemented.
123
Further, for those who are die--hard Verilog authors, this core is written in
124
Verilog as opposed to the XESS provided demonstration SD Card controller
125
found on GitHub\footnote{See
126
\texttt{https://github.com/xesscorp/VHDL\_Lib/SDCard.vhd}}, which was written
127
in VHDL.  For those who are not such die--hard Verilog authors, this controller
128
provides a lower level interface to the card than these other controllers.
129
Whereas the XESS controller will automatically start up the card and interact
130
with it, this controller requires external software to be used when interacting
131
with the card.  This makes the SDSPI controller both more versatile, in the
132
face of potential changes to the card interface, but also less--turn key.
133
% What is new
134
% What does the new have that the old lacks
135
% What performance gain can be expected?
136
 
137
While this core was written for the purpose of being used with the ZipCPU,
138
as enhanced by the Wishbone DMA controller used by the ZipCPU, nothing in this
139
core prevents it from being used with any other architecture that supports
140
the 32--bit Wishbone interface of this core.
141
 
142
This core has been written as a wishbone slave, not a master.  Using the core
143
together with a separate master, such as a CPU or a DMA controller, only makes
144
sense.  This design, however, also restricts the core from being able to use
145
the multiple block write or multiple block read commands, restricting us to
146
single block read and write commands alone.
147
 
148
\chapter{Architecture}\label{ch:arch}
149
 
150
This SD Card interface is designed to provide a means of commanding an SD Card,
151
via the SPI port, and returning its results.
152
 
153
The first thing to know about the SDSPI interface is that it is designed to work
154
over a shared SPI port.  Hence, when the SDSPI controller lowers the CS line to
155
select the SD--Card, the lowered line is only a request to an external
156
controller.  Once that external controller grants access to the SPI port to this
157
device, it will tell this controller and processing can continue.  If your
158
application does not have any contention for the SPI interface, you may simply
159
wire this grant line high.  The controller will still work, although it will
160
wait one clock more than necessary in that case before starting any transaction.
161
 
162
Second, the SDSPI core is completely controlled via the wishbone bus.  In
163
particular a command register is used to initiate interaction across the bus.
164
A separate data register is used to provide an argument to the command, and
165
two FIFO registers are used when transferring larger amounts of data to the
166
card.  We'll examine each of these interactions in turn.
167
 
168
Writes to the command register (CMD) will initiate actions across the port,
169
whether they be reads from or writes to the card.  These writes take the
170
form of sending a 48--bit command to the card.  The command sent to the card
171
is taken from the lower 8--bits of the command register, and the argument to the
172
command is taken from the DATA register.  The last 8--bits of the command sent
173
to the card are formed from a command CRC byte which the core generates
174
internally.  From the perspective of the Wishbone bus, writes to the register
175
will complete immediately, even though the action they initiate will may take
176
much longer to complete.  Further, writes made to the CMD register will be
177
silently ignored if the device is already busy.
178
 
179
Reads from the CMD register will always return immediately.  In particular, the
180
{\tt busy} bit, as returned by the CMD register, can be used to determine if the
181
interface is busy.
182
 
183
There is one exception to the rule that writes take many clocks to complete,
184
and that is writes which configure the SDSPI port.  Internal to the SDSPI
185
port is a configuration register, which determines the speed of the port
186
clock as well as the length of the FIFO (up to 128~samples, or 512--bytes).
187
To read the current speed and FIFO configuration, write an {\tt 0x00bf} value to
188
the command register.  This will cause the DATA register to be filled with
189
the internal configuration register.  Likewise writing a {\tt 0x0ff} to the
190
CMD register will cause the current DATA value, or specifically those non-zero
191
parts, to be transferred to the internal configuration register possibly setting
192
the clock divider and/or the FIFO length.
193
 
194
As part of each write to the CMD register, the controller must also be told
195
which type of response to expect from the SDSPI card.  Responses can be either
196
R1 (single byte), R1b (single byte, followed by a variable delay), or R1
197
followed by up to four bytes, such as the R2, R3, or R7 responses.
198
(Expected responses for particular commands may be found in the SD
199
Specifications documents.\footnote{This particular interface, and the examples
200
using it, were built using the SD Specifications, Part 1: Physical Layer
201
Simplified Specification, Version 4.10, dated 22 January, 2013.}
202
 
203
Finally, individual commands may or may not use the FIFO.  Commands that need
204
use of the FIFO will be specified by the {\tt use\_fifo} bit of the CMD
205
register.  Commands writing to the device will also set the {\tt fifo\_wr} bit
206
of the CMD register, whereas commands simply reading from the fifo will set the
207
{\tt use\_fifo} bit alone.
208
 
209
The DATA register is used during these transactions to first provide the
210
argument to the CMD interaction, and second to provide a place to put the
211
R2, R3, or R7 response after the transaction has completed.  The register will
212
be set to {\tt 0xffffffff} if not set by the response.
213
 
214
Finally, this core supports two separate FIFO's.  This allows a program to
215
fill (or read) one FIFO while the second FIFO can be read/written from the
216
SD card.  The FIFO internal address will be cleared and reset to the beginning
217
upon any write to the CMD register.  After clearing, the FIFO may be written
218
(read) one value at a time.  Reading both FIFO's in any interleaved fashion,
219
however, is not allowed as they share a common internal address.
220
 
221
Currently, the core will detect two types of errors in the interface.  The
222
first is a CRC error, and the second a timeout error.  Either error will set
223
an error bit in the CMD register.  Once set, only writing the error bit back
224
to the CMD register will clear it.
225
 
226
Now, if this discussion isn't thoroughly confusing, let's move on to the
227
Operation chapter to see some examples of how this might be used.
228
 
229
\chapter{Operation}\label{ch:ops}
230
This chapter will walk through some constants that can be used to simplify
231
interaction with the controller, the logic necessary to start up the card,
232
to read its registers, and then examples of how to read and write sectors
233
from the SD Card using this interface.
234
 
235
\section{Constants}
236
Since so much of the interface is controlled by the CMD register, it helps to
237
define several constants which can be used when issuing commands to the SD
238
Card.  Lets discuss some of these constants.
239
 
240
First, as discussed in the last chapter, the SDSPI core maintains an auxiliary
241
register to handle FIFO length and clock speed.  To set this register, we
242
define {\tt SD\_SETAUX} to {\tt 0x0ff}.  Thus, when {\tt SD\_SETAUX} is written
243
to the CMD register, the value of the DATA register is transferred to the
244
internal configuration.  Likewise, we also define {\tt SD\_READAUX} to
245
{\tt 0x0bf}.  When this value is written to the SD--Card, the internal
246
configuration registers value will be copied to the DATA regiseter.
247
\begin{tabular}{lll}
248
{\tt \#define} & {\tt SD\_SETAUX} & {\tt 0x0ff} \\
249
{\tt \#define} & {\tt SD\_READAUX}  & {\tt 0x0bf}
250
\end{tabular}
251
 
252
Second, every command to the SD--Card starts with a single byte.  Of that byte,
253
bit-7 must be clear and bit 6 set.  For this purpose, we define {\tt SD\_CMD}
254
to be {\tt 0x040}.  Thus, {\tt SD\_CMD+0} can be used to send an SD command
255
{\tt CMD0}, and {\tt SD\_CMD+1} can be used to send an SD command {\tt CMD1}.
256
\begin{tabular}{lll}
257
{\tt \#define} & {\tt SD\_CMD} & {\tt 0x040}
258
\end{tabular}
259
 
260
Third, for those commands that will read an SD--Card register, such as those
261
expecting an R2, R3, or R7 response from the card, we define {\tt SD\_READREG}
262
to be {\tt 0x0200}.  Thus, we can send a CMD8 by writing
263
\hbox{\tt SD\_CMD|SD\_READREG} to the port.
264
\begin{tabular}{lll}
265
{\tt \#define} & {\tt SD\_READREG} & {\tt 0x0200}
266
\end{tabular}
267
 
268
The next thing we'll want to be able to do is use the FIFO.  There are two
269
types of commands that use the FIFO, those that read from the card and those
270
that write to the card.  Both need the FIFO bit set, so we'll set
271
{\tt SD\_FIFO\_OP} to {\tt 0x0800} to be a read operation from the card, and
272
the same but with the write bit set {\tt SD\_WRITEOP} will be set to
273
{\tt 0x0c00} to write to the card.
274
 
275
\begin{tabular}{lll}
276
{\tt \#define} & {\tt SD\_FIFO\_OP} & {\tt 0x800} \\
277
{\tt \#define} & {\tt SD\_WRITEOP}  & {\tt 0xc00}
278
\end{tabular}
279
 
280
Finally, we want to be able to choose which FIFO we are using.  For this
281
purpose, we define {\tt SD\_ALTFIFO} to be {\tt 0x01000}.  When this bitmask
282
is included in a command, FIFO number one will be used for the command data,
283
otherwise FIFO zero.  (Note that this is separate from the DATA register,
284
which is still used for any command argument.)
285
 
286
\begin{tabular}{lll}
287
{\tt \#define} & {\tt SD\_ALTFIFO} & {\tt 0x1000}
288
\end{tabular}
289
 
290
Two other constants are necessary: {\tt SD\_BUSY}, set to {\tt 0x04000}, which
291
can be used to test when the SD interface is still busy, and {\tt SD\_ERROR},
292
set to {\tt 0x08000} which can be used to tell if an error has occurred.
293
Clearing an error may be done by writing {\tt SD\_ERROR} back to the card, but
294
to make things simpler we also create {\tt SD\_CLEARERR} for the same purpose.
295
 
296
\begin{tabular}{lll}
297
{\tt \#define} & {\tt SD\_BUSY} & {\tt 0x4000} \\
298
{\tt \#define} & {\tt SD\_ERROR} & {\tt 0x8000} \\
299
{\tt \#define} & {\tt SD\_CLEARERR} & {\tt 0x8000}
300
\end{tabular}
301
 
302
The two most important commands, though, are probably going to be those that
303
read and write a sector.  For these, we shall define {\tt SD\_READ\_SECTOR}
304
and {\tt SD\_WRITE\_SECTOR}.  As the first is a CMD17 to the card and the second
305
a CMD24, these can be defined as:
306
 
307
\begin{tabular}{lll}
308
{\tt \#define} & {\tt SD\_READ\_SECTOR} & {\tt ((SD\_CMD|SD\_CLEARERR|SD\_FIFO\_OP)+17)} \\
309
{\tt \#define} & {\tt SD\_WRITE\_SECTOR} & {\tt ((SD\_CMD|SD\_CLEARERR|SD\_WRITEOP)+24)}
310
\end{tabular}
311
 
312
`Or'ing the {\tt SD\_ALTFIFO} mask to either of these commands will cause the
313
interface to read from or write to the alternate FIFO.
314
 
315
As a very last \#define, we can define the macro {\tt SD\_WAIT\_WHILE\_BUSY}
316
to wait until the SD operation completes:
317
 
318
\begin{tabular}{lll}
319
{\tt \#define} & {\tt SD\_WAIT\_WHILE\_BUSY}&{\tt while(CMD \& SD\_BUSY)}
320
\end{tabular}
321
 
322
Alternatively, we could wait for an interrupt instead since the SDSPI core
323
will create an interrupt upon completion.  For now, and for this example,
324
we'll ignore interrupts.
325
 
326
\section{SD--Card Setup}
327
Setting up an SD--Card takes a bit of work.  There's a series of commands
328
and interactions that need to take place with the card before the card can
329
be used.  You can read about how to do this within the SD--Specification, so
330
we won't repeat the how's or why's here.  Instead, let's focus for now on
331
how this interaction can be made to take place using this controller.
332
 
333
The first step in any start up sequence is to clear the card from any
334
prior condition.  Hence we wait for the card to be no longer busy (it
335
shouldn't be busy anyway), and we then clear any errors:
336
\begin{tabbing}
337
{\tt SD\_WAIT\_WHILE\_BUSY;} \\
338
{\tt CMD} \= {\tt SD\_CLEARERR};
339
\end{tabbing}
340
 
341
Now that the controller is idle (which it should've been from startup anyway),
342
we can now set up our interface.  For this, we'll set our clock rate to 400~KHz.
343
The clock division register, sometimes erroneously called the speed, is found
344
in the lower eight bits of the soft-core configuration register.  The actual
345
SPI clock frequency, given this value, will be:
346
\begin{eqnarray}
347
f_{\mbox{\tiny SDSPI}} &=& \frac{f_{\mbox{\tiny CLK}}}{2\left(\mbox{\tt CLKDIV}+1\right)}
348
\end{eqnarray}
349
Hence, since the XuLA2-LX25 SoC runs at an 80~MHz clock, setting this value to
350
{\tt 0x63} sets the SPI clock to 400~kHz.
351
\begin{tabbing}
352
{\tt DATA} \= {\tt = 0x063}; \\
353
{\tt CMD} \> {\tt = SD\_SETAUX};
354
\end{tabbing}
355
Note that we could have also set the higher order configuration bits to set
356
the size of the FIFO.  In particular, the next four bits, bits 8--11, set
357
the FIFO length.  Setting these to zero will cause the controller to ignore
358
the change, whereas setting the value to three will set the FIFO length to
359
$4\cdot2^3$ bytes, and setting it to seven will set the FIFO length to the
360
nominal $4\cdot 2^7$ or $512$~bytes.
361
 
362
The controller is now ready to send commands to the SD card.  The first command
363
to the card is always a command zero, with zero data.  This is sometimes
364
called the \hbox{\tt GO\_IDLE\_STATE} command.  We then wait for the
365
command to complete:
366
\begin{tabbing}
367
{\tt DATA} \= {\tt = 0;} \\
368
{\tt CMD} \> {\tt = SD\_CMD+0;} \\
369
{\tt SD\_WAIT\_WHILE\_BUSY;}
370
\end{tabbing}
371
The card should now be in its idle state.
372
 
373
The next part of the negotiation tells the card whether or not we are able to
374
handle high capacity cards.  For this, we send a command one,
375
\hbox{\tt SEND\_OP\_COND}, with an argument of {\tt 0x40000000} to tell it
376
that we are able to support high capacity cards.  (An argument of zero would
377
mean that we could not.)
378
\begin{tabbing}
379
{\tt DATA} \= {\tt = 0x40000000;} \\
380
{\tt CMD} \> {\tt = SD\_CMD+1;} \\
381
{\tt SD\_WAIT\_WHILE\_BUSY;}
382
\end{tabbing}
383
 
384
Then the card needs to know what voltage it will be run at.  We communicate this
385
via a \hbox{\tt SEND\_IF\_COND} command, or CMD8.  Since the XuLA2
386
only operates the card at 3.3V, we tell the card we wish to run at 3.3V in the
387
argument.  The last eight bits of the argument, however, are simply to determine
388
whether communication has taken place.  We set these bits to {\tt 0x0a5},
389
although they could be anything.  The card will echo this value back in the
390
response:
391
\begin{tabbing}
392
{\tt DATA} \= {\tt = 0x1a5;} \\
393
{\tt CMD} \> {\tt = SD\_CMD+8;} \\
394
{\tt SD\_WAIT\_WHILE\_BUSY;} \\
395
{\em // assert((DATA\&0x0ff)==0x01a5);}
396
\end{tabbing}
397
The card will also echo back the voltage range, if it accepts it.  Thus,
398
we should receive {\tt 0x01a5} as a response.
399
 
400
The card will now try to start up its own internal state machines.  This could
401
take a while.  We therefore poll the device, and wait for its startup sequence
402
to complete:
403
\begin{tabbing}
404
{\tt bool dev\_busy = false;} \\
405
{\tt do \{}\=\\
406
\> {\em // CMD55 gives us access to SD specific commands}\\
407
\> {\tt DATA = 0;}\\
408
\> {\tt CMD = SD\_CMD+55;}\\
409
\> {\tt SD\_WAIT\_WHILE\_BUSY;}\\
410
\\
411
\> {\em // Now we can issue the ACMD41, to get the idle}\\
412
\> {\em // status}\\
413
\> {\tt DATA = 0x40000000;} \\
414
\> {\tt CMD = SD\_CMD+41;} \\
415
\> {\tt DATA} \= {\tt = 0x1a5;} \\
416
\> {\tt CMD} \> {\tt = SD\_CMD+8;} \\
417
\> {\tt SD\_WAIT\_WHILE\_BUSY;} \\
418
\\
419
\> {\em // The R1 response can be found in the lower 8 bits}\\
420
\> {\em // of the CMD register after the command is complete.}\\
421
\> {\em // Bit 1 of R1 indicates the card hasn't finished its}\\
422
\> {\em // startup}\\
423
\> {\tt dev\_busy = CMD\&1;}\\
424
{\tt \} while(dev\_busy);}
425
\end{tabbing}
426
 
427
\section{Reading Card Registers}
428
 
429
Once the card has started, we can request its operating conditions register,
430
or OCR register as it is called.  For this, we issue a {\tt READ\_OCR} command,
431
or CMD58 by number.  Since this command returns a 32--bit value, we use the
432
{\tt SD\_READREG} macro as well:
433
\begin{tabbing}
434
{\tt int OCR;}\\
435
\\
436
{\tt DATA} \= {\tt = 0;} \\
437
{\tt CMD} \> {\tt = (SD\_READREG|SD\_CMD)+58;} \\
438
{\tt SD\_WAIT\_WHILE\_BUSY;} \\
439
{\tt OCR} \= {\tt = DATA;}\\
440
\end{tabbing}
441
When I issue this command on my card, I get a {\tt 0xc0ff8000} response telling
442
me that my card can handle between 2.7 and 3.6~Volts, that it is a higher
443
capacity card, and that it has completed its startup sequence.
444
 
445
Now let's switch up to a higher speed, and read the 16--byte Card Specific
446
Data (CSD) register field from the card.  First, the switch to a 20~MHz clock
447
and a 16--byte fifo,
448
\begin{tabbing}
449
{\tt DATA} \= {\tt = 0x0201}; \\
450
{\tt CMD} \> {\tt = SD\_SETAUX};
451
\end{tabbing}
452
Remember that the {\tt 0x0200} switches to a FIFO of length~$2^2$ words, or
453
equivalently $4\cdot 2^2=16$ bytes.  Likewise the {\tt 0x01} component switches
454
our frequency to 20~MHz.  Now we can issue the {\tt SEND\_CSD\_COND}, or
455
CMD9, command itself.  Note that we didn't need to wait for the {\tt SD\_SETAUX}
456
command to complete.  Further, since this command is going to read from our
457
FIFO, we need to include the {\tt SD\_FIFO\_OP} part of the command:
458
\begin{tabbing}
459
{\tt int CSD[4];}\\
460
{\tt DATA} \= {\tt = 0;} \\
461
{\tt CMD} \> {\tt = (SD\_FIFO\_OP|SD\_CMD)+9;}
462
{\tt SD\_WAIT\_WHILE\_BUSY;} \\
463
{\tt for(int i=0; i<4; i++) } \\
464
\> {\tt CSD[i] = FIFO[0];}
465
\end{tabbing}
466
Once the command is complete, we can read the four 32--bit words of the CSD
467
register from the FIFO, as shown above.  Alternatively, we could have issued
468
another command first, before reading that FIFO result.
469
 
470
We could read the Card Identification (CID) register as well, but this would've
471
been the same sequence as above, save only that we would've written a
472
{\tt (SD\_READREG|SD\_CMD)+10)} to CMD.
473
 
474
Reading the STATUS is similar, only the response to the {\tt SEND\_STATUS}
475
command is an 8--bit value from an R2 response, not the 32--bit values of
476
the R3 (OCR) or R7 responses.  Still, the R2 response is provided in the
477
DATA register, so we only need to send {\tt (SD\_READREG|SD\_CMD)+13} to
478
the CMD register in order to read its result from the DATA register.  The status
479
register will be returned in the top eight bits of the DATA register (the
480
interface still reads 32--bits, even though the other 24 can be ignored), so:
481
\begin{tabbing}
482
{\tt int card\_status;}\\
483
{\tt DATA} \= {\tt = 0;} \\
484
{\tt CMD} \> {\tt = (SD\_READREG|SD\_CMD)+13;}
485
{\tt SD\_WAIT\_WHILE\_BUSY;} \\
486
{\tt card\_status = DATA>>24;}
487
\end{tabbing}
488
 
489
As a final register example, let's read the SD Card Configuration Register
490
(SCR).  This register is read in a fashion very similar to the CSD register,
491
except that because of its width the FIFO needs to be set for a shorter
492
register width:
493
\begin{tabbing}
494
{\tt int SCR[2];}\\
495
{\em // Set the FIFO length to two words, $2^1$.}\\
496
{\tt DATA} \= {\tt = 0x0100}; \\
497
{\tt CMD} \> {\tt = SD\_SETAUX};\\
498
{\em // Issue an ALT command, to get the other command set.}\\
499
{\tt DATA} \= {\tt = 0;} \\
500
{\tt CMD} \> {\tt = (SD\_CMD)+55;}\\
501
{\tt SD\_WAIT\_WHILE\_BUSY;} \\
502
{\em // Now get the SCR register.}\\
503
{\tt DATA} \= {\tt = 0;} \\
504
{\tt CMD} \> {\tt = (SD\_FIFO\_OP|SD\_CMD)+51;}\\
505
{\tt SD\_WAIT\_WHILE\_BUSY;} \\
506
{\tt for(int i=0; i<2; i++) } \\
507
\> {\tt SCR[i] = FIFO[0];}\\
508
\end{tabbing}
509
 
510
 
511
\section{Reading and Writing}
512
 
513
For our first example, let's read the boot sector from our card.  For this,
514
we set our FIFO back to 128~words (512~bytes), and then issue a read sector
515
command:
516
\begin{tabbing}
517
{\tt void} \= {\tt read(int sector\_num, int *buf) \{}\\
518
\> {\em // Set the FIFO length to 128 words, $2^7$.}\\
519
\> {\tt DATA} \= {\tt = 0x0700}; \\
520
\> {\tt CMD} \> {\tt = SD\_SETAUX};\\
521
\> {\em // Read from the requested sector}\\
522
\> {\tt DATA} \= {\tt = sector\_num;} \\
523
\> {\tt CMD} \> {\tt = SD\_READ\_SECTOR;}\\
524
\> {\tt SD\_WAIT\_WHILE\_BUSY;} \\
525
\> {\tt for(int i=0; i<128; i++) } \\
526
\> \> {\tt buf[i] = FIFO[0];}\\
527
{\tt \}}
528
\end{tabbing}
529
 
530
We could also write to any sector on the card in a very similar fashion:
531
\begin{tabbing}
532
{\tt void} \= {\tt write(int sector\_num, int *buf) \{}\\
533
\> {\em // Set the FIFO length to 128 words, $2^7$.}\\
534
\> {\tt DATA} \= {\tt = 0x0700}; \\
535
\> {\tt CMD} \> {\tt = SD\_SETAUX};\\
536
\> {\em // Fill the FIFO with our data}\\
537
\> {\tt for(int i=0; i<128; i++) } \\
538
\> \> {\tt FIFO[0] = buf[i];}\\
539
\> {\em // Issue the write command}\\
540
\> {\tt DATA} \= {\tt = sector\_num;} \\
541
\> {\tt CMD} \> {\tt = SD\_WRITE\_SECTOR;}\\
542
\> {\tt SD\_WAIT\_WHILE\_BUSY;} \\
543
{\tt \}}
544
\end{tabbing}
545
 
546
As mentioned in the introductory chapter, this interface does not support
547
reading or writing multiple blocks at once.  Hence, I expect all interaction
548
using this card controller to be accomplished through these two commands:
549
reading a single sector, and writing to a single sector.
550
 
551
\chapter{Registers}\label{ch:regs}
552
 
553
As mentioned in the last two chapters, the SDSPI core has only four registers,
554
and one internal register.  These are shown in Tbl.~\ref{tbl:ioregs}.
555
\begin{table}[htbp]
556
\begin{center}\begin{reglist}
557
CMD     &{\tt 0x00} & 32 & R/W & SDSPI Command register\\\hline
558
DAT     &{\tt 0x01} & 32 & R/W & SDSPI return data/argument register\\\hline
559
FIFO[0] &{\tt 0x02} & 32 & R/W & FIFO[0] data\\\hline
560
FIFO[1] &{\tt 0x03} & 32 & R/W & FIFO[1] data\\\hline
561
CONFIG  &  & 12 & R/W & Internal configuration register\\\hline
562
\end{reglist}
563
\caption{I/O Peripheral Registers}\label{tbl:ioregs}
564
\end{center}\end{table}
565
The most powerful of these is the command register, CMD, so we'll spend most of
566
our time discussing that one.
567
 
568
\section{CMD Register}
569
Writes to the CMD register will cause the device to act, or if the device is
570
already busy then any writes will be ignored.  The CMD register itself is
571
composed of several packed bit fields, as shown in Fig.~\ref{fig:CMD}.
572
\begin{figure}\begin{center}
573
\begin{bytefield}[endianness=big]{32}
574
\bitheader{0-31}\\
575
\bitbox{15}{Unused}
576
\bitbox{1}{C}
577
\bitbox{1}{E}
578
\bitbox{1}{B}
579
\bitbox{1}{0}
580
\bitbox{1}{I}
581
\bitbox{1}{F}
582
\bitbox{1}{W}
583
\bitbox{2}{R}
584
\bitbox{8}{R1/CMD}
585
\end{bytefield}
586
\caption{CMD Register fields}\label{fig:CMD}
587
\end{center}\end{figure}
588
Perhaps the most important of these is the R1/CMD field.  If bits 7--6 are
589
the two bits 2'b01, then the write is a command and the bottom six bits
590
specify the rest of the 8--bit command identifier.  Once the command is
591
complete, these 8--bits represent the R1 response from the device.  R1
592
should be one while the device is still starting, or zero in the case of no
593
error.  Further interpretation of this value may be found in the SD--Card
594
Specification.
595
 
596
Of next importance is the $R$ field.  This specifies the response the
597
controller should expect from the card given the command that was issued
598
to the card.  There are three possible values for this field: $2'b00$, meaning
599
the controller should expect an R1 response, $2'b01$, meaning the controller
600
should expect an R1b response, and $2'b10$ meaning the controller should
601
expect an R2/R3/R7 32--bit response.
602
 
603
The $F$, or FIFO, field should be set if the command being given requires the
604
use of the FIFO.  $W$ should be set at the same time if the controller will be
605
writing to the card from the FIFO, and cleared if the controller will be
606
reading from the card into the FIFO.  Finally, $I$ specifies which FIFO will
607
be used: 0 for the primary, or 1 for the alternate.
608
 
609
While the command is running, the BUSY or $B$ bit will be set.
610
 
611
Once the command has completed, the $E$ bit may be set if either the command
612
timed out, or a CRC error was noted while reading from the card.  (To detect
613
a CRC error when writing to the card, check the R1 response according to the
614
SD Card specification.)  Errors may be cleared by writing a 1 to this bit.
615
Until such time as an error is cleared, the error condition will simply
616
persist.
617
 
618
\section{DATA Register}
619
Compared to the CMD register, the DATA register is quite simple.  Like the
620
CMD register, the DATA register may only be written when the interface is
621
idle.  When issuing a command to the device, the 32--bit argument for the
622
command is taken from the DATA register.  When reading the results of a device
623
command, the DATA register will contain the R2 response in the upper 8--bits,
624
or an R3 or R7 response in the full 32--bits.
625
 
626
\section{FIFO Registers}
627
The SDSPI controller maintains two 128~word (512~byte) FIFOs.  Reads from the
628
card will read data into one of the two FIFO's, whereas writes to the card will
629
write data out from one of the FIFO's.  Which FIFO the card uses is determined
630
by the $I$ bit in the CMD register (above).
631
 
632
Further, upon any write to the CMD register, the FIFO address will be set
633
to point to the beginning of the FIFO.
634
 
635
The purpose of the FIFO's is to allow one to issue a command to read into one
636
FIFO, then when that command is complete to read into a second FIFO.  While
637
the second command is ongoing, a CPU or DMA may read the data out of the first
638
FIFO and place it wherever into memory.  Then, when the second read is complete,
639
a third read may be issued into the first buffer while the data is read out of
640
the second and so forth.
641
 
642
This interleaving approach, sometimes called ping-pong buffering, can also be
643
used for writing: Write into one FIFO, issue a write command, write into the
644
second FIFO, wait for the first write command to complete, issue a second
645
write command, and so forth.
646
 
647
One item to note before closing: there is only one internal address register
648
when accessing the FIFO from the wishbone bus.  Attempts to read from or write
649
to either FIFO from the wishbone bus will increment this address register.
650
Interleaved read, or write attempts, such as reading one item from
651
FIFO[0] and writing another item to FIFO[1], will each increment the internal
652
address pointer so that the result is likely to be undesirable.  For this
653
reason, it is recommended that only one FIFO be read from or written by the
654
wishbone bus at a time.
655
 
656
\section{CONFIG Register}
657
The CONFIG register controls the SPI clock rate and the FIFO size.
658
Specifically, with regards to the FIFO size, it controls how many bytes will
659
be written into the FIFO (which is really of a fixed size) before the expecting
660
a CRC, or equivalently how many bytes to read out of the FIFO before adding a
661
CRC.  The fields of this register are shown in Fig.~\ref{fig:CONFIG}.
662
\begin{figure}\begin{center}
663
\begin{bytefield}[endianness=big]{32}
664
\bitheader{0-31}\\
665
\bitbox{8}{Unused}
666
\bitbox{8}{Max LgFIFO}
667
\bitbox{8}{LgFIFO}
668
\bitbox{2}{0}
669
\bitbox{6}{CLKDIV}
670
\end{bytefield}
671
\caption{CONFIG Register fields}\label{fig:CONFIG}
672
\end{center}\end{figure}
673
 
674
The CLKDIV field sets a divisor from the current clock to create a SPI clock.
675
The minimum value of this field is `1', corresponding to dividing the input
676
clock by `4'.  As discussed earlier, the input clock will be divided by
677
twice this field plus one.  Hence, setting this field to one will cause the
678
original clock to be divided by $2(1+{\tt CLKDIV})$ or $4$.  Thus an 80~MHz
679
input clock will become a 20~MHz SPI clock.  Setting this value to zero will
680
cause the set to be ignored.
681
 
682
The LgFIFO field sets the log, base two, of the FIFO size.  The actual FIFO
683
size used will be $2^{\mbox{\tiny LGFIFO}}$ words.  The maximum size the
684
device will support is returned by the {\tt Max LgFIFO} field, which is
685
currently set to $7$ for a 128~word (512~byte) FIFO.
686
 
687
To set the CONFIG register, first set the DATA register to the new config
688
value (or zero for the fields that will not change), and then write
689
{\tt 0x0ff} to the CMD register.  Likewise, to read the CONFIG register
690
write a {\tt 0x0bf} to the CMD register and read the CONFIG register from the
691
DATA register.
692
 
693
\chapter{Wishbone Datasheet}\label{ch:wb}
694
Tbl.~\ref{tbl:wishbone}
695
\begin{table}[htbp]
696
\begin{center}
697
\begin{wishboneds}
698
Revision level of wishbone & WB B4 spec \\\hline
699
Type of interface & Slave, (Block/pipelined) Read/Write \\\hline
700
Port size & 32--bit \\\hline
701
Port granularity & 32--bit \\\hline
702
Maximum Operand Size & 32--bit \\\hline
703
Data transfer ordering & Big Endian \\\hline
704
Clock constraints & (See below)\\\hline
705
Signal Names & \begin{tabular}{ll}
706
                Signal Name & Wishbone Equivalent \\\hline
707
                {\tt i\_clk} & {\tt CLK\_I} \\
708
                {\tt i\_wb\_cyc} & {\tt CYC\_I} \\
709
                {\tt i\_wb\_stb} & {\tt STB\_I} \\
710
                {\tt i\_wb\_we} & {\tt WE\_I} \\
711
                {\tt i\_wb\_addr} & {\tt ADR\_I} \\
712
                {\tt i\_wb\_data} & {\tt DAT\_I} \\
713
                {\tt o\_wb\_ack} & {\tt ACK\_O} \\
714
                {\tt o\_wb\_stall} & {\tt STALL\_O} \\
715
                {\tt o\_wb\_data} & {\tt DAT\_O}
716
                \end{tabular}\\\hline
717
\end{wishboneds}
718
\caption{Wishbone Slave Datasheet}\label{tbl:wishbone}
719
\end{center}\end{table}
720
is required by the wishbone specification, and so it is included here.  Note
721
that all wishbone operations may be pipelined, to include FIFO operations,
722
for speed.
723
 
724
The particular constraint on the clock is not really a wishbone constraint, but
725
rather an SD--Card constraint.  Not all cards can handle clocks faster than
726
25~MHz.  For this reason, the wishbone clock, which forms the master clock for
727
this entire controller, must be divided down so that the SPI clock is within
728
the limits the card can handle.
729
 
730
\chapter{Clocks}\label{ch:clk}
731
 
732
This core is based upon the XuLA2-LX25 SoC design.  The XuLA2 development board
733
contains one external 12~MHz clock, which is internally boosted within the SoC
734
to 80~MHz.  This clock is divided by a programmable divider, often set to
735
four, to create the 20~MHz clock used to drive the device.  (Maximum device
736
speed is usually 25MHz, although some devices can run at 50~MHz.)   This
737
controller does not support setting the SPI clock frequency any faster than
738
one quarter of the input clock rate.
739
 
740
\chapter{I/O Ports}\label{ch:io}
741
 
742
Table.~\ref{tbl:ioports}
743
\begin{table}[htbp]
744
\begin{center}
745
\begin{portlist}
746
i\_clk & 1 & Input & Clock\\\hline\hline
747
i\_wb\_cyc & 1 & Output & Wishbone bus cycle active\\\hline
748
i\_wb\_stb & 1 & Output & Wishbone Strobe, true one clock only for each interaction\\\hline
749
i\_wb\_we & 1 & Output & Wishbone Write-Enable line\\\hline
750
i\_wb\_addr & 2 & Output & Selects our I/O register\\\hline
751
i\_wb\_data & 32 & Output & Incoming wishbone bus data\\\hline
752
o\_wb\_ack & 1 & Output & Acknowledge a WB request, always true one clock after the request\\\hline
753
o\_wb\_stall & 1 & Output & Always zero\\\hline
754
o\_wb\_data & 32 & Output & 32--bit wishbone data response\\\hline\hline
755
o\_cs\_n & 1 & Output & Chip--select and SPI request line\\\hline
756
o\_sck & 1 & Output & SD Card clock\\\hline
757
o\_mosi & 1 & Output & Output data wire to the SD Card\\\hline
758
i\_miso & 1 & Input & Input data wire from the SD Card\\\hline\hline
759
o\_int & 1 & Output & An interrupt line to the CPU controller\\\hline
760
i\_bus\_grant & 1 & Input  & True if the SDSPI controller is controlling the bus\\\hline
761
o\_debug & 32 & Output & See Verilog for details\\\hline
762
\end{portlist}
763
\caption{List of IO ports}\label{tbl:ioports}
764
\end{center}\end{table}
765
lists all of the input and output ports to this core.  You may notice these
766
inputs and outputs are divided into sections: the master clock, the wishbone
767
bus, the SPI interface to the card, and three other wires.  Of these, the
768
last two chapters discussed the wishbone bus interface and the clock.  The
769
SPI interface should be fairly straightforward, so we'll move on and discuss
770
the other three wires.
771
 
772
This controller supports an interrupt line, {\tt o\_int}.  Upon completion of
773
any operation, when the SPI chip select line is deactivated (raised high),
774
{\tt o\_int} will be strobed for one cycle.  It is up to the logic using this
775
chip to catch and use that interrupt line or ignore it.  In particular, it is
776
possible to use that interrupt line to trigger a DMA service to move data
777
in or out of the FIFO, although the details of that are beyond this discussion
778
here.
779
 
780
The second wire, {\tt i\_bus\_grant}, exists because this controller is
781
expected to operate together with other SPI controllers that may also wish
782
to drive the same three wires ({\tt o\_sck} and {\tt o\_mosi} to the device,
783
and {\tt i\_miso} from the device).  When the controller therefore lowers the
784
{\tt o\_cs\_n} line to make it active, it then waits for {\tt i\_bus\_grant}
785
to go high.  This is its signal, from the outside world, that its chip has been
786
selected and that it is now driving the {\tt o\_sck} and {\tt o\_mosi} pins.
787
Should you not need this in your environment, you can simply leave this line
788
wired high.
789
 
790
The final bus of 32--wires, {\tt o\_debug}, is defined internally and used
791
when/if necessary to debug the core and watch what is going on within it.
792
These wires may be left unconnected in most implementations, as they are not
793
necessary for using the actually controller.
794
 
795
% Appendices
796
% Index
797
\end{document}
798
 
799
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.