1 |
786 |
skrzyp |
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
SPI Support
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
Overview
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
Overview
|
43 |
|
|
eCos Support for SPI, the Serial Peripheral Interface
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
Description
|
47 |
|
|
|
48 |
|
|
The Serial Peripheral Interface (SPI) is one of a number of serial bus
|
49 |
|
|
technologies. It can be used to connect a processor to one or more
|
50 |
|
|
peripheral chips, for example analog-to-digital convertors or real
|
51 |
|
|
time clocks, using only a small number of pins and PCB tracks. The
|
52 |
|
|
technology was originally developed by Motorola but is now also
|
53 |
|
|
supported by other vendors.
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
A typical SPI system might look like this:
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
At the start of a data transfer the master cpu asserts one of the chip
|
67 |
|
|
select signals and then generates a clock signal. During each clock
|
68 |
|
|
tick the cpu will output one bit on its master-out-slave-in line and
|
69 |
|
|
read one bit on the master-in-slave-out line. Each device is connected
|
70 |
|
|
to the clock line, the two data lines, and has its own chip select. If
|
71 |
|
|
a device's chip select is not asserted then it will ignore any
|
72 |
|
|
incoming data and will tristate its output. If a device's chip select
|
73 |
|
|
is asserted then during each clock tick it will read one bit of data
|
74 |
|
|
on its input pin and output one bit on its output pin.
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
The net effect is that the cpu can write an arbitrary amount of data
|
78 |
|
|
to one of the attached devices at a time, and simultaneously read the
|
79 |
|
|
same amount of data. Some devices are inherently uni-directional. For
|
80 |
|
|
example an LED unit would only accept data from the cpu: it will not
|
81 |
|
|
send anything meaningful back; the cpu will still sample its input
|
82 |
|
|
every clock tick, but this should be discarded.
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
A useful feature of SPI is that there is no flow control from the
|
86 |
|
|
device back to the cpu. If the cpu tries to communicate with a device
|
87 |
|
|
that is not currently present, for example an MMC socket which does
|
88 |
|
|
not contain a card, then the I/O will still proceed. However the cpu
|
89 |
|
|
will read random data. Typically software-level CRC checksums or
|
90 |
|
|
similar techniques will be used to allow the cpu to detect this.
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
SPI communication is not fully standardized. Variations between
|
94 |
|
|
devices include the following:
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
Many devices involve byte transfers, where the unit of data is 8 bits.
|
99 |
|
|
Others use larger units, up to 16 bits.
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
Chip selects may be active-high or active-low. If the attached devices
|
103 |
|
|
use a mixture of polarities then this can complicate things.
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
Clock rates can vary from 128KHz to 20MHz or greater. With some
|
107 |
|
|
devices it is necessary to interrogate the device using a slow clock,
|
108 |
|
|
then use the obtained information to select a faster clock for
|
109 |
|
|
subsequent transfers.
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
The clock is inactive between data transfers. When inactive the
|
113 |
|
|
clock's polarity can be high or low.
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
Devices depend on the phase of the clock. Data may be sampled on
|
117 |
|
|
either the rising edge or the falling edge of the clock.
|
118 |
|
|
|
119 |
|
|
|
120 |
|
|
A device may need additional delays, for example between asserting
|
121 |
|
|
the chip select and the first clock tick.
|
122 |
|
|
|
123 |
|
|
|
124 |
|
|
Some devices involve complicated transactions: perhaps a command from
|
125 |
|
|
cpu to device; then an initial status response from the device; a data
|
126 |
|
|
transfer; and a final status response. From the cpu's perspective
|
127 |
|
|
these are separate stages and it may be necessary to abort the
|
128 |
|
|
operation after the initial status response. However the device may
|
129 |
|
|
require that the chip select remain asserted for the whole
|
130 |
|
|
transaction. A side effect of this is that it is not possible to do a
|
131 |
|
|
quick transfer with another device in the middle of the transaction.
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
Certain devices, for example MMC cards, depend on a clock signal after
|
135 |
|
|
a transfer has completed and the chip select has dropped. This clock
|
136 |
|
|
is used to finish some processing within the device.
|
137 |
|
|
|
138 |
|
|
|
139 |
|
|
|
140 |
|
|
Inside the cpu the clock and data signals are usually managed by
|
141 |
|
|
dedicated hardware. Alternatively SPI can be implemented using
|
142 |
|
|
bit-banging, but that approach is normally used for other serial bus
|
143 |
|
|
technologies such as I2C. The chip selects may also be implemented by
|
144 |
|
|
the dedicated SPI hardware, but often GPIO pins are used instead.
|
145 |
|
|
|
146 |
|
|
|
147 |
|
|
|
148 |
|
|
eCos Support for SPI
|
149 |
|
|
|
150 |
|
|
The eCos SPI support for any given platform is spread over a number of
|
151 |
|
|
different packages:
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
This package, CYGPKG_IO_SPI, exports an API for
|
156 |
|
|
accessing devices attached to an SPI bus. This API handles issues such
|
157 |
|
|
as locking between threads. The package does not contain any
|
158 |
|
|
hardware-specific code, instead it will call into an SPI bus driver
|
159 |
|
|
package.
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
In future this package may be extended with a bit-banging
|
163 |
|
|
implementation of an SPI bus driver. This would depend on lower-level
|
164 |
|
|
code for manipulating the GPIO pins used for the clock, data and chip
|
165 |
|
|
select signals, but timing and framing could be handled by generic code.
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
There will be a bus driver package for the specific SPI hardware on
|
169 |
|
|
the target hardware, for example
|
170 |
|
|
CYGPKG_DEVS_SPI_MCF52xx_QSPI. This is responsible
|
171 |
|
|
for the actual I/O. A bus driver may be used on many different boards,
|
172 |
|
|
all with the same SPI bus but with different devices attached to that
|
173 |
|
|
bus. Details of the actual devices should be supplied by other code.
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
The generic API depends on cyg_spi_device
|
177 |
|
|
data structures. These contain the information needed by a bus driver,
|
178 |
|
|
for example the appropriate clock rate and the chip select to use.
|
179 |
|
|
Usually the data structures are provided by the platform HAL since it
|
180 |
|
|
is that package which knows about all the devices on the board.
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
On some development boards the SPI pins are brought out to expansion
|
184 |
|
|
connectors, allowing end users to add extra devices. In such cases the
|
185 |
|
|
platform HAL may not know about all the devices on the board. Data
|
186 |
|
|
structures for the additional devices can instead be supplied by
|
187 |
|
|
application code.
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
Some types of SPI devices may have their own driver package. For
|
191 |
|
|
example one common use for SPI buses is to provide low-cost
|
192 |
|
|
MultiMediaCard (MMC) support. An MMC is a non-trivial device so there
|
193 |
|
|
is an eCos package specially for that, providing a block device
|
194 |
|
|
interface for higher-level code such as file systems. Other SPI
|
195 |
|
|
devices such as analog-to-digital converters are much simpler and
|
196 |
|
|
come in many varieties. There are no dedicated packages to support
|
197 |
|
|
each such device: the chances are low that another board would use the
|
198 |
|
|
exact same device, so there are no opportunities for code re-use.
|
199 |
|
|
Instead the devices may be accessed directly by application code or by
|
200 |
|
|
extra functions in the platform HAL.
|
201 |
|
|
|
202 |
|
|
|
203 |
|
|
|
204 |
|
|
Typically all appropriate packages will be loaded automatically when
|
205 |
|
|
you configure eCos for a given target. If the application does not use
|
206 |
|
|
any of the SPI I/O facilities, directly or indirectly, then linker
|
207 |
|
|
garbage collection should eliminate all unnecessary code and data. All
|
208 |
|
|
necessary initialization should happen automatically. However the
|
209 |
|
|
exact details may depend on the target, so the platform HAL
|
210 |
|
|
documentation should be checked for further details.
|
211 |
|
|
|
212 |
|
|
|
213 |
|
|
There is one important exception to this: if the SPI devices are
|
214 |
|
|
attached to an expansion connector then the platform HAL will not know
|
215 |
|
|
about these devices. Instead more work will have to be done by
|
216 |
|
|
application code.
|
217 |
|
|
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
SPI Interface
|
225 |
|
|
|
226 |
|
|
|
227 |
|
|
SPI Functions
|
228 |
|
|
allow applications and other packages to access SPI devices
|
229 |
|
|
|
230 |
|
|
|
231 |
|
|
|
232 |
|
|
|
233 |
|
|
#include <cyg/io/spi.h>
|
234 |
|
|
|
235 |
|
|
|
236 |
|
|
void cyg_spi_transfer
|
237 |
|
|
cyg_spi_device* device
|
238 |
|
|
cyg_bool polled
|
239 |
|
|
cyg_uint32 count
|
240 |
|
|
const cyg_uint8* tx_data
|
241 |
|
|
cyg_uint8* rx_data
|
242 |
|
|
|
243 |
|
|
|
244 |
|
|
void cyg_spi_tick
|
245 |
|
|
cyg_spi_device* device
|
246 |
|
|
cyg_bool polled
|
247 |
|
|
cyg_uint32 count
|
248 |
|
|
|
249 |
|
|
|
250 |
|
|
int cyg_spi_get_config
|
251 |
|
|
cyg_spi_device* device
|
252 |
|
|
cyg_uint32 key
|
253 |
|
|
void* buf
|
254 |
|
|
cyg_uint32* len
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
int cyg_spi_set_config
|
258 |
|
|
cyg_spi_device* device
|
259 |
|
|
cyg_uint32 key
|
260 |
|
|
const void* buf
|
261 |
|
|
cyg_uint32* len
|
262 |
|
|
|
263 |
|
|
|
264 |
|
|
void cyg_spi_transaction_begin
|
265 |
|
|
cyg_spi_device* device
|
266 |
|
|
|
267 |
|
|
|
268 |
|
|
cyg_bool cyg_spi_transaction_begin_nb
|
269 |
|
|
cyg_spi_device* device
|
270 |
|
|
|
271 |
|
|
|
272 |
|
|
void cyg_spi_transaction_transfer
|
273 |
|
|
cyg_spi_device* device
|
274 |
|
|
cyg_bool polled
|
275 |
|
|
cyg_uint32 count
|
276 |
|
|
const cyg_uint8* tx_data
|
277 |
|
|
cyg_uint8* rx_data
|
278 |
|
|
cyg_bool drop_cs
|
279 |
|
|
|
280 |
|
|
|
281 |
|
|
void cyg_spi_transaction_tick
|
282 |
|
|
cyg_spi_device* device
|
283 |
|
|
cyg_bool polled
|
284 |
|
|
cyg_uint32 count
|
285 |
|
|
|
286 |
|
|
|
287 |
|
|
void cyg_spi_transaction_end
|
288 |
|
|
cyg_spi_device* device
|
289 |
|
|
|
290 |
|
|
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
Description
|
294 |
|
|
|
295 |
|
|
All SPI functions take a pointer to a
|
296 |
|
|
cyg_spi_device structure as their first
|
297 |
|
|
argument. This is an opaque data structure, usually provided by the
|
298 |
|
|
platform HAL. It contains the information needed by the SPI bus driver
|
299 |
|
|
to interact with the device, for example the required clock rate and
|
300 |
|
|
polarity.
|
301 |
|
|
|
302 |
|
|
|
303 |
|
|
An SPI transfer involves the following stages:
|
304 |
|
|
|
305 |
|
|
|
306 |
|
|
|
307 |
|
|
Perform thread-level locking on the bus. Only one thread at a time is
|
308 |
|
|
allowed to access an SPI bus. This eliminates the need to worry about
|
309 |
|
|
locking at the bus driver level. If a platform involves multiple SPI
|
310 |
|
|
buses then each one will have its own lock. Prepare the bus for
|
311 |
|
|
transfers to the specified device, for example by making sure it will
|
312 |
|
|
tick at the right clock rate.
|
313 |
|
|
|
314 |
|
|
|
315 |
|
|
Assert the chip select on the specified device, then transfer data to
|
316 |
|
|
and from the device. There may be a single data transfer or a
|
317 |
|
|
sequence. It may or may not be necessary to keep the chip select
|
318 |
|
|
asserted throughout a sequence.
|
319 |
|
|
|
320 |
|
|
|
321 |
|
|
Optionally generate some number of clock ticks without asserting a
|
322 |
|
|
chip select, for those devices which need this to complete an
|
323 |
|
|
operation.
|
324 |
|
|
|
325 |
|
|
|
326 |
|
|
Return the bus to a quiescent state. Then unlock the bus, allowing
|
327 |
|
|
other threads to perform SPI operations on devices attached to this
|
328 |
|
|
bus.
|
329 |
|
|
|
330 |
|
|
|
331 |
|
|
|
332 |
|
|
The simple functions cyg_spi_transfer and
|
333 |
|
|
cyg_spi_tick perform all these steps in a single
|
334 |
|
|
call. These are suitable for simple I/O operations. The alternative
|
335 |
|
|
transaction-oriented functions each perform just one of these steps.
|
336 |
|
|
This makes it possible to perform multiple transfers while only
|
337 |
|
|
locking and unlocking the bus once, as required for more complicated
|
338 |
|
|
devices.
|
339 |
|
|
|
340 |
|
|
|
341 |
|
|
With the exception of
|
342 |
|
|
cyg_spi_transaction_begin_nb all the functions
|
343 |
|
|
will block until completion. There are no error conditions. An SPI
|
344 |
|
|
transfer will always take a predictable amount of time, depending on
|
345 |
|
|
the transfer size and the clock rate. The SPI bus does not receive any
|
346 |
|
|
feedback from a device about possible errors, instead those have to be
|
347 |
|
|
handled by software at a higher level. If a thread cannot afford the
|
348 |
|
|
time it will take to perform a complete large transfer then a number
|
349 |
|
|
of smaller transfers can be used instead.
|
350 |
|
|
|
351 |
|
|
|
352 |
|
|
SPI operations should always be performed at thread-level or during
|
353 |
|
|
system initialization, and not inside an ISR or DSR. This greatly
|
354 |
|
|
simplifies locking. Also a typical ISR or DSR should not perform a
|
355 |
|
|
blocking operation such as an SPI transfer.
|
356 |
|
|
|
357 |
|
|
|
358 |
|
|
SPI transfers can happen in either polled or interrupt-driven mode.
|
359 |
|
|
Typically polled mode should be used during system initialization,
|
360 |
|
|
before the scheduler has been started and interrupts have been
|
361 |
|
|
enabled. Polled mode should also be used in single-threaded
|
362 |
|
|
applications such as RedBoot. A typical multi-threaded application
|
363 |
|
|
should normally use interrupt-driven mode because this allows for more
|
364 |
|
|
efficient use of cpu cycles. Polled mode may be used in a
|
365 |
|
|
multi-threaded application but this is generally undesirable: the cpu
|
366 |
|
|
will spin while waiting for a transfer to complete, wasting cycles;
|
367 |
|
|
also the current thread may get preempted or timesliced, making the
|
368 |
|
|
timing of an SPI transfer much less predictable. On some hardware
|
369 |
|
|
interrupt-driven mode is impossible or would be very inefficient. In
|
370 |
|
|
such cases the bus drivers will only support polled mode and will
|
371 |
|
|
ignore the polled argument.
|
372 |
|
|
|
373 |
|
|
|
374 |
|
|
|
375 |
|
|
Simple Transfers
|
376 |
|
|
|
377 |
|
|
cyg_spi_transfer can be used for SPI operations
|
378 |
|
|
to simple devices. It takes the following arguments:
|
379 |
|
|
|
380 |
|
|
|
381 |
|
|
|
382 |
|
|
cyg_spi_device* device
|
383 |
|
|
|
384 |
|
|
This identifies the SPI device that should be used.
|
385 |
|
|
|
386 |
|
|
|
387 |
|
|
|
388 |
|
|
cyg_bool polled
|
389 |
|
|
|
390 |
|
|
Polled mode should be used during system initialization and in a
|
391 |
|
|
single-threaded application. Interrupt-driven mode should normally be
|
392 |
|
|
used in a multi-threaded application.
|
393 |
|
|
|
394 |
|
|
|
395 |
|
|
|
396 |
|
|
cyg_uint32 count
|
397 |
|
|
|
398 |
|
|
This identifies the number of data items to be transferred. Usually
|
399 |
|
|
each data item is a single byte, but some devices use a larger size up
|
400 |
|
|
to 16 bits.
|
401 |
|
|
|
402 |
|
|
|
403 |
|
|
|
404 |
|
|
const cyg_uint8* tx_data
|
405 |
|
|
|
406 |
|
|
The data to be transferred to the device. If the device will only
|
407 |
|
|
output data and ignore its input then a null pointer can be used.
|
408 |
|
|
Otherwise the array should contain count data
|
409 |
|
|
items, usually bytes. For devices where each data item is larger than
|
410 |
|
|
one byte the argument will be interpreted as an array of shorts
|
411 |
|
|
instead, and should be aligned to a 2-byte boundary. The bottom n bits
|
412 |
|
|
of each short will be sent to the device. The buffer need not be
|
413 |
|
|
aligned to a cache-line boundary, even for SPI devices which use DMA
|
414 |
|
|
transfers, but some bus drivers may provide better performance if the
|
415 |
|
|
buffer is suitably aligned. The buffer will not be modified by the
|
416 |
|
|
transfer.
|
417 |
|
|
|
418 |
|
|
|
419 |
|
|
|
420 |
|
|
cyg_uint8* rx_data
|
421 |
|
|
|
422 |
|
|
A buffer for the data to be received from the device. If the device
|
423 |
|
|
does not generate any output then a null pointer can be used.
|
424 |
|
|
The same size and alignment rules apply as for tx_data.
|
425 |
|
|
|
426 |
|
|
|
427 |
|
|
|
428 |
|
|
|
429 |
|
|
cyg_spi_transfer performs all the stages of an
|
430 |
|
|
SPI transfer: locking the bus; setting it up correctly for the
|
431 |
|
|
specified device; asserting the chip select and transferring the data;
|
432 |
|
|
dropping the chip select at the end of the transfer; returning the bus
|
433 |
|
|
to a quiescent state; and unlocking the bus.
|
434 |
|
|
|
435 |
|
|
|
436 |
|
|
|
437 |
|
|
Additional Clock Ticks
|
438 |
|
|
|
439 |
|
|
Some devices require a number of clock ticks on the SPI bus between
|
440 |
|
|
transfers so that they can complete some internal processing. These
|
441 |
|
|
ticks must happen at the appropriate clock rate but no chip select
|
442 |
|
|
should be asserted and no data transfer will happen.
|
443 |
|
|
cyg_spi_tick provides this functionality.
|
444 |
|
|
The device argument identifies the SPI bus, the
|
445 |
|
|
required clock rate and the size of each data item. The
|
446 |
|
|
polled argument has the usual meaning. The
|
447 |
|
|
count argument specifies the number of data items
|
448 |
|
|
that would be transferred, which in conjunction with the size of each
|
449 |
|
|
data item determines the number of clock ticks.
|
450 |
|
|
|
451 |
|
|
|
452 |
|
|
|
453 |
|
|
Transactions
|
454 |
|
|
|
455 |
|
|
A transaction-oriented API is available for interacting with more
|
456 |
|
|
complicated devices. This provides separate functions for each of the
|
457 |
|
|
steps in an SPI transfer.
|
458 |
|
|
|
459 |
|
|
|
460 |
|
|
cyg_spi_transaction_begin must be used at the
|
461 |
|
|
start of a transaction. This performs thread-level locking on the bus,
|
462 |
|
|
blocking if it is currently in use by another thread. Then it prepares
|
463 |
|
|
the bus for transfers to the specified device, for example by making
|
464 |
|
|
sure it will tick at the right clock rate.
|
465 |
|
|
|
466 |
|
|
|
467 |
|
|
cyg_spi_transaction_begin_nb is a non-blocking
|
468 |
|
|
variant, useful for threads which cannot afford to block for an
|
469 |
|
|
indefinite period. If the bus is currently locked the function returns
|
470 |
|
|
false immediately. If the bus is not locked then it acts as
|
471 |
|
|
cyg_spi_transaction_begin and returns true.
|
472 |
|
|
|
473 |
|
|
|
474 |
|
|
Once the bus has been locked it is possible to perform one or more
|
475 |
|
|
data transfers by calling
|
476 |
|
|
cyg_spi_transaction_transfer. This takes the same
|
477 |
|
|
arguments as cyg_spi_transfer, plus an additional
|
478 |
|
|
one drop_cs. A non-zero value specifies that
|
479 |
|
|
the device's chip select should be dropped at the end of the transfer,
|
480 |
|
|
otherwise the chip select remains asserted. It is essential that the
|
481 |
|
|
chip select be dropped in the final transfer of a transaction. If the
|
482 |
|
|
protocol makes this difficult then
|
483 |
|
|
cyg_spi_transaction_tick can be used to generate
|
484 |
|
|
dummy ticks with all chip selects dropped.
|
485 |
|
|
|
486 |
|
|
|
487 |
|
|
If the device requires additional clock ticks in the middle of a
|
488 |
|
|
transaction without being selected,
|
489 |
|
|
cyg_spi_transaction_tick can be used. This will
|
490 |
|
|
drop the device's chip select if necessary, then generate the
|
491 |
|
|
appropriate number of ticks. The arguments are the same as for
|
492 |
|
|
cyg_spi_tick.
|
493 |
|
|
|
494 |
|
|
|
495 |
|
|
cyg_spi_transaction_end should be called at the
|
496 |
|
|
end of a transaction. It returns the SPI bus to a quiescent state,
|
497 |
|
|
then unlocks it so that other threads can perform I/O.
|
498 |
|
|
|
499 |
|
|
|
500 |
|
|
A typical transaction might involve the following. First a command
|
501 |
|
|
should be sent to the device, consisting of four bytes. The device
|
502 |
|
|
will then respond with a single status byte, zero for failure,
|
503 |
|
|
non-zero for success. If successful then the device can accept another
|
504 |
|
|
n bytes of data, and will generate a 2-byte response including a
|
505 |
|
|
checksum. The device's chip select should remain asserted throughout.
|
506 |
|
|
The code for this would look something like:
|
507 |
|
|
|
508 |
|
|
|
509 |
|
|
#include <cyg/io/spi.h>
|
510 |
|
|
#include <cyg/hal/hal_io.h> // Defines the SPI devices
|
511 |
|
|
…
|
512 |
|
|
cyg_spi_transaction_begin(&hal_spi_eprom);
|
513 |
|
|
// Interrupt-driven transfer, four bytes of command
|
514 |
|
|
cyg_spi_transaction_transfer(&hal_spi_eprom, 0, 4, command, NULL, 0);
|
515 |
|
|
// Read back the status
|
516 |
|
|
cyg_spi_transaction_transfer(&hal_spi_eprom, 0, 1, NULL, status, 0);
|
517 |
|
|
if (!status[0]) {
|
518 |
|
|
// Command failed, generate some extra ticks to drop the chip select
|
519 |
|
|
cyg_spi_transaction_tick(&hal_spi_eprom, 0, 1);
|
520 |
|
|
} else {
|
521 |
|
|
// Transfer the data, then read back the final status. The
|
522 |
|
|
// chip select should be dropped at the end of this.
|
523 |
|
|
cyg_spi_transaction_transfer(&hal_spi_eprom, 0, n, data, NULL, 0);
|
524 |
|
|
cyg_spi_transaction_transfer(&hal_spi_eprom, 0, 2, NULL, status, 1);
|
525 |
|
|
// Code for checking the final status should go here
|
526 |
|
|
}
|
527 |
|
|
// Transaction complete so clean up
|
528 |
|
|
cyg_spi_transaction_end(&hal_spi_eprom);
|
529 |
|
|
|
530 |
|
|
|
531 |
|
|
A number of variations are possible. For example the command and
|
532 |
|
|
status could be packed into the beginning and end of two 5-byte
|
533 |
|
|
arrays, allowing a single transfer.
|
534 |
|
|
|
535 |
|
|
|
536 |
|
|
|
537 |
|
|
Device Configuration
|
538 |
|
|
|
539 |
|
|
The functions cyg_spi_get_config and
|
540 |
|
|
cyg_spi_set_config can be used to examine and
|
541 |
|
|
change parameters associated with SPI transfers. The only keys that
|
542 |
|
|
are defined for all devices are
|
543 |
|
|
CYG_IO_GET_CONFIG_SPI_CLOCKRATE and
|
544 |
|
|
CYG_IO_SET_CONFIG_SPI_CLOCKRATE. Some types of
|
545 |
|
|
device, for example MMC cards, support a range of clock rates. The
|
546 |
|
|
cyg_spi_device structure will be initialized
|
547 |
|
|
with a low clock rate. During system initialization the device will be
|
548 |
|
|
queried for the optimal clock rate, and the
|
549 |
|
|
cyg_spi_device should then be updated. The
|
550 |
|
|
argument should be a clock rate in Hertz. For example the following
|
551 |
|
|
code switches communication to 1Mbit/s:
|
552 |
|
|
|
553 |
|
|
|
554 |
|
|
cyg_uint32 new_clock_rate = 1000000;
|
555 |
|
|
cyg_uint32 len = sizeof(cyg_uint32);
|
556 |
|
|
if (cyg_spi_set_config(&hal_mmc_device,
|
557 |
|
|
CYG_IO_SET_CONFIG_SPI_CLOCKRATE,
|
558 |
|
|
(const void *)&new_clock_rate, &len)) {
|
559 |
|
|
// Error recovery code
|
560 |
|
|
}
|
561 |
|
|
|
562 |
|
|
|
563 |
|
|
If an SPI bus driver does not support the exact clock rate specified
|
564 |
|
|
it will normally use the nearest valid one. SPI bus drivers may define
|
565 |
|
|
additional keys appropriate for specific hardware. This means that the
|
566 |
|
|
valid keys are not known by the generic code, and theoretically it is
|
567 |
|
|
possible to use a key that is not valid for the SPI bus to which the
|
568 |
|
|
device is attached. It is also possible that the argument used with
|
569 |
|
|
one of these keys is invalid. Hence both
|
570 |
|
|
cyg_spi_get_config and
|
571 |
|
|
cyg_spi_set_config can return error codes. The
|
572 |
|
|
return value will be 0 for success, non-zero for failure. The SPI bus
|
573 |
|
|
driver's documentation should be consulted for further details.
|
574 |
|
|
|
575 |
|
|
|
576 |
|
|
Both configuration functions will lock the bus, in the same way as
|
577 |
|
|
cyg_spi_transfer. Changing the clock rate in the
|
578 |
|
|
middle of a transfer or manipulating other parameters would have
|
579 |
|
|
unexpected consequences.
|
580 |
|
|
|
581 |
|
|
|
582 |
|
|
|
583 |
|
|
|
584 |
|
|
|
585 |
|
|
|
586 |
|
|
|
587 |
|
|
Porting to New Hardware
|
588 |
|
|
|
589 |
|
|
|
590 |
|
|
Porting
|
591 |
|
|
Adding SPI support to new hardware
|
592 |
|
|
|
593 |
|
|
|
594 |
|
|
Description
|
595 |
|
|
|
596 |
|
|
Adding SPI support to an eCos port can take two forms. If there is
|
597 |
|
|
already an SPI bus driver for the target hardware then both that
|
598 |
|
|
driver and this generic SPI package CYGPKG_IO_SPI
|
599 |
|
|
should be included in the ecos.db target entry.
|
600 |
|
|
Typically the platform HAL will need to supply some platform-specific
|
601 |
|
|
information needed by the bus driver. In addition the platform HAL
|
602 |
|
|
should provide cyg_spi_device structures for
|
603 |
|
|
every device attached to the bus. The exact details of this depend on
|
604 |
|
|
the bus driver so its documentation should be consulted for further
|
605 |
|
|
details. If there is no suitable SPI bus driver yet then a new driver
|
606 |
|
|
package will have to be written.
|
607 |
|
|
|
608 |
|
|
|
609 |
|
|
|
610 |
|
|
Adding a Device
|
611 |
|
|
|
612 |
|
|
The generic SPI package CYGPKG_IO_SPI defines a
|
613 |
|
|
data structure cyg_spi_device. This contains
|
614 |
|
|
the information needed by the generic package, but not the additional
|
615 |
|
|
information needed by a bus driver to interact with the device. Each
|
616 |
|
|
bus driver will define a larger data structure, for example
|
617 |
|
|
cyg_mcf52xx_qspi_device, which contains a
|
618 |
|
|
cyg_spi_device as its first field. This is
|
619 |
|
|
analogous to C++ base and derived classes, but without any use of
|
620 |
|
|
virtual functions. The bus driver package should be consulted for the
|
621 |
|
|
details.
|
622 |
|
|
|
623 |
|
|
|
624 |
|
|
During initialization an SPI bus driver may need to know about all the
|
625 |
|
|
devices attached to that bus. For example it may need to know which
|
626 |
|
|
cpu pins should be configured as chip selects rather than GPIO pins.
|
627 |
|
|
To achieve this all device definitions should specify the particular
|
628 |
|
|
bus to which they are attached, for example:
|
629 |
|
|
|
630 |
|
|
|
631 |
|
|
struct cyg_mcf52xx_qspi_device hal_spi_atod CYG_SPI_DEVICE_ON_BUS(0) =
|
632 |
|
|
{
|
633 |
|
|
.spi_common.spi_bus = &cyg_mcf52xx_qspi_bus,
|
634 |
|
|
…
|
635 |
|
|
};
|
636 |
|
|
|
637 |
|
|
|
638 |
|
|
The CYG_SPI_DEVICE_ON_BUS macro adds information
|
639 |
|
|
to the structure which causes the linker to group all such structures
|
640 |
|
|
in a single table. The bus driver's initialization code can then
|
641 |
|
|
iterate over this table.
|
642 |
|
|
|
643 |
|
|
|
644 |
|
|
|
645 |
|
|
Adding Bus Support
|
646 |
|
|
|
647 |
|
|
An SPI bus driver usually involves a new hardware package. This needs
|
648 |
|
|
to perform the following:
|
649 |
|
|
|
650 |
|
|
|
651 |
|
|
|
652 |
|
|
Define a device structure which contains a
|
653 |
|
|
cyg_spi_device as its first element. This
|
654 |
|
|
should contain all the information needed by the bus driver to
|
655 |
|
|
interact with a device on that bus.
|
656 |
|
|
|
657 |
|
|
|
658 |
|
|
Provide functions for the following operations:
|
659 |
|
|
|
660 |
|
|
|
661 |
|
|
spi_transaction_begin
|
662 |
|
|
spi_transaction_transfer
|
663 |
|
|
spi_transaction_tick
|
664 |
|
|
spi_transaction_end
|
665 |
|
|
spi_get_config
|
666 |
|
|
spi_set_config
|
667 |
|
|
|
668 |
|
|
|
669 |
|
|
These correspond to the main API functions, but can assume that the
|
670 |
|
|
bus is already locked so no other thread will be manipulating the bus
|
671 |
|
|
or any of the attached devices. Some of these operations may be no-ops.
|
672 |
|
|
|
673 |
|
|
|
674 |
|
|
Define a bus structure which contains a
|
675 |
|
|
cyg_spi_bus as its first element. This should
|
676 |
|
|
contain any additional information needed by the bus driver.
|
677 |
|
|
|
678 |
|
|
|
679 |
|
|
Optionally, instantiate the bus structure. The instance should have a
|
680 |
|
|
well-known name since it needs to be referenced by the device
|
681 |
|
|
structure initializers. For some drivers it may be best to create the
|
682 |
|
|
bus inside the driver package. For other drivers it may be better to
|
683 |
|
|
leave this to the platform HAL or the application. It depends on how
|
684 |
|
|
much platform-specific knowledge is needed to fill in the bus
|
685 |
|
|
structure.
|
686 |
|
|
|
687 |
|
|
|
688 |
|
|
Create a HAL table for the devices attached to this bus.
|
689 |
|
|
|
690 |
|
|
|
691 |
|
|
Arrange for the bus to be initialized early on during system
|
692 |
|
|
initialization. Typically this will happen via a prioritized static
|
693 |
|
|
constructor with priority CYG_INIT_BUS_SPI.
|
694 |
|
|
As part of this initialization the bus driver should
|
695 |
|
|
invoke the CYG_SPI_BUS_COMMON_INIT macro on its
|
696 |
|
|
cyg_spi_bus field.
|
697 |
|
|
|
698 |
|
|
|
699 |
|
|
Provide the appropriate documentation, including details of how the
|
700 |
|
|
SPI device structures should be initialized.
|
701 |
|
|
|
702 |
|
|
|
703 |
|
|
|
704 |
|
|
There are no standard SPI testcases. It is not possible to write SPI
|
705 |
|
|
code without knowing about the devices attached to the bus, and those
|
706 |
|
|
are inherently hardware-specific.
|
707 |
|
|
|
708 |
|
|
|
709 |
|
|
|
710 |
|
|
|
711 |
|
|
|