Hardware Reference
In-Depth Information
The
cs_change
member is treated as a Boolean value. When 0, no chip select is
performed by the driver. The application is expected to do what is necessary to notify
the peripheral that it is selected (usually a GPIO pin is brought low). Once the I/O has
completed, the application then must normally unselect the slave peripheral.
When the
cs_change
member is
true
(non-zero), the slave selected will
depend on
the device pathname that was opened
. The bus and the slave address are embedded in the
device name:
/dev/spidev<bus>.<device>
When
cs_change
is
true
, the driver asserts
GPIO
8
for
spidev0.0
and asserts
GPIO
7
for
spidev0.1
prior to I/O and then deactivates the same upon completion. Of course,
using these two nodes requires two different
open(2)
calls.
The
SPI_IOC_MESSAGE(n)
macro is used in the
ioctl(2)
call to perform one or
more SPI I/O operations. The macro is unusual because it requires an argument
n
.
(Perhaps someone will take it upon themselves someday to clean this interface up to
work like I2C.) This specifies how many I/O transfers you would like to perform.
An array of
spi_ioc_transfer
structures is declared and configured for each transfer
required, as shown in the next example:
struct spi_ioc_transfer io[3]; /
Define 3 transfers
/
∗
∗
int rc;
io[0].tx_buf = . . . ; /
Configure I/O
/
∗
∗
...
io[2].bits_per_word = 8;
rc = ioctl(fd,SPI_IOC_MESSAGE(3),& io[0]);
The preceding example will perform three I/O transfers. Since the application
never gets to perform any GPIO manipulation between these I/Os, this applies to
communicating with one particular slave device.
The following example code brings all of the concepts together, to demonstrate one
I/O. The
spi_ioc_transfer
structure is initialized so that 32 bytes are transmitted and
simultaneously 32 are received.
uint8_t tx[32], rx[32];
struct spi_ioc_transfer tr;
int rc;
tr.tx_buf = (unsigned long) tx;
tr.rx_buf = (unsigned long) rx;
tr.len = 32;
tr.delay_usecs = delay;
tr.speed_hz = speed;
tr.bits_per_word = bits;