EMAC SPI Programming
SPI (Serial Peripheral Interface) is a synchronous full duplex serial data communication standard used to interface many types of memory and I/O devices. SPI requires four lines per slave device connection, three of which are shared across the SPI bus. MISO, MOSI and SCLK are the three shared bus lines and chip selects (CSn) are used to determine which device is the target for communication.
The MISO (Master In Slave Out) line is a input signal to the SPI master controller which provides serial data from a slave SPI device. The MOSI (Master Out Slave In) line is an output signal from the SPI master providing serial data to a slave SPI device. SCLK (Synchronous Clock) is an output clock from the SPI master controller used to synchronize the data on the MISO and MOSI lines. The SPI protocol uses both edges of the synchronous clock to drive the transaction on the bus. The leading transition is use to toggle the output of the MOSI while the trailing transition latches the output of MISO. The phase and polarity (CPHA and CPOL) parameters of the SCLK are used to determine the SPI Mode being used. For each slave device there is a chip select output signal from the SPI master controller. Each SPI slave uses the chip select to determine when to communicate back to the SPI master controller.
For more information about the SPI protocol see the following page: http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
This article pertains to the usage of the EMAC SPI class driver. The SPI class was developed to abstract the SPI interface such that it could be accessed from Linux user space and real-time applications. Much of the development occurred before the spidev driver was introduced in the Linux kernel. Though spidev provides a similar interface they are not compatible.
For more information on the Linux spidev driver implementation see: https://www.kernel.org/doc/Documentation/spi/spidev
Accessing EMAC SPI Class Devices
Each device that uses an EMAC SPI Class driver exposes a character interface to the Linux user space. Once properly registered the device node can be accessed directly using a C program.
To open the SPI Class device node, use the standard C
int fd; ⋮ fd = open("/dev/spi0cs1", O_RDWR);
The mode of the SCLK as well as the bits per transaction can be changed using the CONFWRITE
For instance, to set the SPI interface for 8 bits mode 3 (CPHA=1 CPOL=1) the follow
ioctl command will be issued:
#include "spi_char.h" ⋮ int ret; struct spi_control config = SPICL_EIGHTBIT | SPICL_CPHA | SPICL_CPOL; ⋮ ret = ioctl(fd, CONFWRITE, &config);
|The spi_char.h file which defines the ioctls and data structures used for the interface is include with the SPI demo program in the EMAC SDK.|
The frequency of the SCLK is defined by kernel registration but it can also be configured using SPEEDWRITE
For example, to set the SCLK to 1MHz:
#include "spi_char.h" ⋮ int ret; struct spi_control speed = 1000000; ⋮ ret = ioctl(fd, SPEEDWRITE, &speed);
The speed and configuration can be read in a similar way using the SPEEDREAD and CONFREAD
ioctl commands respectively.
For example reading the speed is done as follows:
#include "spi_char.h" ⋮ int ret; struct spi_control speed; ⋮ ret = ioctl(fd, SPEEDWRITE, &speed); printf("The clock frequency is %d Hz\n", speed);
After configuring the SPI device, data can be transferred using the XMIT
#include "spi_char.h" #define TRANS_LEN 4 ⋮ int ret; spi_transfer_t data; __u8 moso[TRANS_LEN]; __u8 miso[TRANS_LEN]; ⋮ data.size = TRANS_LEN; mosi = 0xF0; mosi = 0x0D; mosi = 0xFA; mosi = 0xCE; date.mosi = mosi; data.miso = miso; ⋮ ret = ioctl(fd, XMIT,&data);