Hardware Reference
In-Depth Information
Communicating with an SPI device consists of the following system calls:
open(2) : Opens the SPI device driver node
read(2) : Reads with 0 bytes being transmitted
write(2) : Writes data while discarding received data
ioctl(2) : For configuration and bidirectional I/O
close(2) : Closes the SPI device driver node
In SPI communication, the use of read(2) and write(2) is unusual. Normally,
ioctl(2) is used to facilitate simultaneous read and write transfers.
Open Device
In order to perform SPI communication through the kernel driver, you need to open one
of the device nodes by using open(2) . The general format of the device pathname is
/dev/spidev<bus>.<device>
as we saw earlier. The following is a code snippet opening bus 0, device 0.
int fd;
fd = open("/dev/spidev0.0",O_RDWR);
if ( fd < 0 ) {
perror("Unable to open SPI driver");
exit(1);
}
SPI communication involves both reading and writing, so the driver is opened for
read and write ( O_RDWR ).
SPI Mode Macros
Before SPI communications can be performed, the mode of communication needs to be
configured. Table 13-5 lists the C language macros that can be used to configure the SPI
mode to be used.
Table 13-5. SPI Mode Macros
Macro
Effect
Comments
SPI_CPOL
CPOL = 1
Or use SPI_MODE_x
SPI_CPHA
CPHA = 1
Or use SPI_MODE_x
SPI_CS_HIGH
SS is active high
Unusual
SPI_NO_CS
Don't assert select
Not used/application controlled
 
 
Search WWH ::




Custom Search