Hardware Reference
In-Depth Information
rc = ioctl(fd,SPI_IOC_MESSAGE(1),&tr);
if ( rc < 1 ) {
perror("Can't send spi message");
Here a single I/O transmission occurs, with data being sent from array tx and
received into array rx .
The return value from the ioctl(2) call returns the number of bytes transferred
(32 in the example). Otherwise, -1 is returned to indicate that an error has occurred.
Close
Like all Unix I/O operations, the device is closed when the open file descriptor is no
longer required:
close(fd);
Write
The write(2) system call can be used, if the received data is unimportant. Note, however,
that no delay is applied with this call.
Read
The read(2) system call is actually inappropriate for SPI since the master must transmit
data on MOSI in order for the slave to send bits back on the MISO line. However, when
read(2) is used, the driver will automatically send out 0 bits as necessary to accomplish
the read. (Be careful that your peripheral will accept 0 bytes without unintended
consequences.) Like the write(2) call, no delay is provided.
SPI Testing
When developing your SPI communication software, you can perform a simple loopback
test to test your framework. Once the framework checks out, you can then turn your
attention to communicating with the actual device.
While the Raspbian Linux driver does not support the SPI_LOOP mode bit (in the
wheezy release), you can still physically loop your SPI bus by connecting a wire from the
MOSI output back to the MISO input pin (connect GPIO 10 to GPIO 9).
A simple program, shown next, demonstrates this type of loopback test. It will write
out 4 bytes (0x12, 0x23, 0x45, and 0x67) to the SPI driver. Because you have wired the
MOSI pin to the MISO input, anything transmitted will also be received.
When the program executes, it will report the number of bytes received and four
hexadecimal values:
$ sudo ./spiloop
rc=4 12 23 45 67
$
 
Search WWH ::




Custom Search